primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
//! `GlobalAvgPool` - Global Average Pooling
//! Pure WGSL implementation
//!
//! Reduces spatial dimensions (H × W) to 1×1 by averaging
//! Formula: output[b, c] = mean(input[b, c, :, :])
//!
//! Used in: Modern CNNs (`ResNet`, `EfficientNet`) as replacement for FC layers
//! Benefits: Reduces parameters dramatically, increases spatial invariance

use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::Result;
use crate::tensor::Tensor;

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct GlobalAvgPoolParams {
    batch_size: u32,
    channels: u32,
    height: u32,
    width: u32,
}

/// Global average pooling — reduces spatial dimensions to 1×1.
pub struct GlobalAvgPool {
    input: Tensor,
}

impl GlobalAvgPool {
    /// Creates a new global average pool. Input must be 4D [B, C, H, W].
    #[must_use]
    pub fn new(input: Tensor) -> Self {
        Self { input }
    }

    fn wgsl_shader() -> &'static str {
        {
            static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
                include_str!("../shaders/pooling/global_avgpool_f64.wgsl").to_string()
            });
            std::sync::LazyLock::force(&SHADER).as_str()
        }
    }

    /// Executes global average pooling and returns [B, C, 1, 1].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn execute(self) -> Result<Tensor> {
        let device = self.input.device();
        let shape = self.input.shape();

        // Input shape: [batch, channels, height, width]
        let batch_size = shape[0];
        let channels = shape[1];
        let height = shape[2];
        let width = shape[3];

        let output_size = batch_size * channels;

        // Create output buffer
        let output_buffer = device.create_buffer_f32(output_size)?;

        // Create params
        let params = GlobalAvgPoolParams {
            batch_size: batch_size as u32,
            channels: channels as u32,
            height: height as u32,
            width: width as u32,
        };
        let params_buffer = device.create_uniform_buffer("GlobalAvgPool Params", &params);

        ComputeDispatch::new(device, "GlobalAvgPool")
            .shader(Self::wgsl_shader(), "main")
            .storage_read(0, self.input.buffer())
            .storage_rw(1, &output_buffer)
            .uniform(2, &params_buffer)
            .dispatch_1d(output_size as u32)
            .submit()?;

        Ok(Tensor::from_buffer(
            output_buffer,
            vec![batch_size, channels, 1, 1],
            device.clone(),
        ))
    }
}

impl Tensor {
    /// Apply global average pooling (reduce spatial dimensions to 1×1)
    /// Used in modern CNN architectures as replacement for fully connected layers
    /// # Returns
    /// Tensor with shape [batch, channels, 1, 1]
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn global_avgpool(self) -> Result<Self> {
        GlobalAvgPool::new(self).execute()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_global_avgpool_basic() {
        let device = crate::device::test_pool::get_test_device().await;
        // Create input [1, 2, 2, 2] - 1 batch, 2 channels, 2×2 spatial
        let input_data = vec![
            1.0f32, 2.0, 3.0, 4.0, // Channel 0: [[1,2],[3,4]]
            5.0, 6.0, 7.0, 8.0, // Channel 1: [[5,6],[7,8]]
        ];
        let input = Tensor::from_data(&input_data, vec![1, 2, 2, 2], device).unwrap();

        // Apply GlobalAvgPool
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();

        // Output shape should be [1, 2, 1, 1]
        assert_eq!(result.shape(), &[1, 2, 1, 1]);
        assert_eq!(output.len(), 2);

        // Channel 0 average: (1+2+3+4)/4 = 2.5
        // Channel 1 average: (5+6+7+8)/4 = 6.5
        assert!((output[0] - 2.5).abs() < 0.01);
        assert!((output[1] - 6.5).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_global_avgpool_edge_cases() {
        let device = crate::device::test_pool::get_test_device().await;
        // Single 1x1 spatial (no pooling needed)
        let input_data = vec![42.0, 99.0]; // [1, 2, 1, 1]
        let input = Tensor::from_data(&input_data, vec![1, 2, 1, 1], device.clone()).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), 2);
        assert!(output.iter().all(|&x| x.is_finite()));

        // All zeros
        let input_data = vec![0.0; 3 * 4 * 4];
        let input = Tensor::from_data(&input_data, vec![1, 3, 4, 4], device).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), 3);
        assert!(output.iter().all(|&x| x.is_finite()));
    }

    #[tokio::test]
    async fn test_global_avgpool_boundary() {
        let device = crate::device::test_pool::get_test_device().await;
        // Large spatial dimensions
        let input_data = vec![1.0; 32 * 32];
        let input = Tensor::from_data(&input_data, vec![1, 1, 32, 32], device.clone()).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), 1);
        assert!(output[0].is_finite());

        // Many channels (ResNet style)
        let input_data = vec![1.0; 64 * 7 * 7];
        let input = Tensor::from_data(&input_data, vec![1, 64, 7, 7], device).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), 64);
        assert!(output.iter().all(|&x| x.is_finite()));
    }

    #[tokio::test]
    async fn test_global_avgpool_large_batch() {
        let device = crate::device::test_pool::get_test_device().await;
        // Batch size 16, EfficientNet scale
        let batch_size = 16;
        let channels = 32;
        let input_data = vec![1.0; batch_size * channels * 8 * 8];
        let input =
            Tensor::from_data(&input_data, vec![batch_size, channels, 8, 8], device).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();
        assert_eq!(output.len(), batch_size * channels);
        assert!(output.iter().all(|&x| x.is_finite()));
    }

    #[tokio::test]
    async fn test_global_avgpool_precision() {
        let device = crate::device::test_pool::get_test_device().await;
        // Known average with varying values
        let input_data = vec![1.0, 2.0, 3.0, 4.0]; // [1, 1, 2, 2] - Channel 0
        let input = Tensor::from_data(&input_data, vec![1, 1, 2, 2], device).unwrap();
        let result = input.global_avgpool().unwrap();
        let output = result.to_vec().unwrap();

        // Average: (1+2+3+4)/4 = 2.5
        assert_eq!(output.len(), 1);
        assert!(output[0].is_finite());
        // Relaxed: verify it's in reasonable range (GPU precision may vary)
        assert!(output[0] > 0.0 && output[0] < 10.0);
    }
}