1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Global Max Pooling Operation - Spatial reduction to single value
//!
//! **Deep Debt Evolution**: Modernized from trait-based to direct `impl Tensor`
//!
//! ## Deep Debt Principles
//!
//! - ✅ Modern idiomatic Rust (direct `impl Tensor`, not trait extension)
//! - ✅ Universal compute (WGSL shader for all substrates)
//! - ✅ Safe Rust (no unsafe blocks)
//! - ✅ CNN-friendly (4D input [batch, channels, height, width])
//!
//! ## Evolution History
//!
//! **Before** (Phase 3): `GlobalMaxPoolExt` trait extension\
//! **After** (Phase 6): Direct `impl Tensor` method
//!
//! ## Usage
//!
//! ```ignore
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # use barracuda::tensor::Tensor;
//! # use barracuda::device::test_pool;
//! # let device = test_pool::tokio_block_on(test_pool::get_test_device()).unwrap();
//! # let data = [1.0f32; 2 * 64 * 7 * 7];
//! // Input: [batch=2, channels=64, height=7, width=7]
//! let input = Tensor::from_data(&data, vec![2, 64, 7, 7], device)?;
//! // Output: [batch=2, channels=64, height=1, width=1]
//! let _pooled = input.global_maxpool()?;
//! # Ok(())
//! # }
//! ```
use crate::device::{ComputeDispatch, DeviceCapabilities, WorkloadType};
use crate::error::Result;
use crate::tensor::Tensor;
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct GlobalMaxPoolParams {
batch_size: u32,
channels: u32,
height: u32,
width: u32,
}
/// Global max pooling: reduce spatial dimensions to 1×1 per channel.
pub struct GlobalMaxPool {
input: Tensor,
}
impl GlobalMaxPool {
fn wgsl_shader() -> &'static str {
{
static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
include_str!("../shaders/pooling/global_maxpool_f64.wgsl").to_string()
});
std::sync::LazyLock::force(&SHADER).as_str()
}
}
/// Execute global max pooling over spatial dimensions.
///
/// # 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();
if shape.len() != 4 {
return Err(crate::error::BarracudaError::invalid_op(
"Shape Error",
format!(
"GlobalMaxPool expects 4D input [batch, channels, height, width], got shape {shape:?}"
),
));
}
let batch_size = shape[0];
let channels = shape[1];
let height = shape[2];
let width = shape[3];
let params = GlobalMaxPoolParams {
batch_size: batch_size as u32,
channels: channels as u32,
height: height as u32,
width: width as u32,
};
let output_shape = &[batch_size, channels, 1, 1];
let output_size = output_shape.iter().product::<usize>() * std::mem::size_of::<f32>();
let output_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("global_maxpool_output"),
size: output_size as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("global_maxpool_params"),
contents: bytemuck::cast_slice(&[params]),
usage: wgpu::BufferUsages::UNIFORM,
});
let caps = DeviceCapabilities::from_device(device);
let optimal_wg_size = caps.optimal_workgroup_size(WorkloadType::Convolution);
let num_outputs = (batch_size * channels) as u32;
let workgroups = num_outputs.div_ceil(optimal_wg_size);
ComputeDispatch::new(device, "global_maxpool")
.shader(Self::wgsl_shader(), "main")
.storage_read(0, self.input.buffer())
.storage_rw(1, &output_buffer)
.uniform(2, ¶ms_buffer)
.dispatch(workgroups, 1, 1)
.submit()?;
Ok(Tensor::from_buffer(
output_buffer,
output_shape.to_vec(),
device.clone(),
))
}
}
// ============================================================================
// Modern API: Direct impl Tensor (Phase 6 Evolution)
// ============================================================================
impl Tensor {
/// Apply global max pooling across spatial dimensions
///
/// Reduces [batch, channels, height, width] → [batch, channels, 1, 1]
///
/// **Deep Debt**: Modern direct method, no trait extension needed
///
/// ## Input Shape
///
/// Must be 4D: `[batch, channels, height, width]`
///
/// ## Output Shape
///
/// `[batch, channels, 1, 1]` (max value per channel)
///
/// ## Example
///
/// ```ignore
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use barracuda::tensor::Tensor;
/// # use barracuda::device::test_pool;
/// # let device = test_pool::tokio_block_on(test_pool::get_test_device()).unwrap();
/// # let input = Tensor::from_data(&[1.0f32; 49], vec![1, 1, 7, 7], device).unwrap();
/// // Pool spatial dimensions (7x7 → 1x1)
/// let _pooled = input.global_maxpool()?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn global_maxpool(self) -> Result<Self> {
let op = GlobalMaxPool { input: self };
op.execute()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_global_maxpool_basic() {
let device = crate::device::test_pool::get_test_device().await;
let input = Tensor::from_data(
&vec![
// Batch 0, Channel 0
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, // Batch 0, Channel 1
9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
],
vec![1, 2, 2, 4],
device,
)
.unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(result.shape(), &[1, 2, 1, 1]);
assert_eq!(output.len(), 2);
// Verify operation completed successfully
assert!(output.iter().all(|&x| x.is_finite()));
// Values should be positive (all inputs positive)
assert!(output.iter().all(|&x| x > 0.0));
}
#[tokio::test]
async fn test_global_maxpool_edge_cases() {
let device = crate::device::test_pool::get_test_device().await;
// Single 1x1 spatial
let input = Tensor::from_data(&[42.0, 99.0], vec![1, 2, 1, 1], device.clone()).unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 2);
assert!(output.iter().all(|&x| x.is_finite()));
// All same values
let input = Tensor::from_data(&[5.0; 2 * 3 * 3], vec![1, 2, 3, 3], device).unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 2);
assert!(output.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_global_maxpool_boundary() {
let device = crate::device::test_pool::get_test_device().await;
// Large spatial dimensions
let input =
Tensor::from_data(&vec![1.0; 32 * 32], vec![1, 1, 32, 32], device.clone()).unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 1);
assert!(output[0].is_finite());
// Many channels
let input = Tensor::from_data(&vec![1.0; 64 * 7 * 7], vec![1, 64, 7, 7], device).unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 64);
}
#[tokio::test]
async fn test_global_maxpool_large_batch() {
let device = crate::device::test_pool::get_test_device().await;
// Batch size 16
let batch_size = 16;
let channels = 32;
let input = Tensor::from_data(
&vec![1.0; batch_size * channels * 8 * 8],
vec![batch_size, channels, 8, 8],
device,
)
.unwrap();
let result = input.global_maxpool().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_maxpool_precision() {
let device = crate::device::test_pool::get_test_device().await;
// Known max with varying values
let input = Tensor::from_data(
&[1.0, 5.0, 3.0, 2.0], // Max = 5.0
vec![1, 1, 2, 2],
device,
)
.unwrap();
let result = input.global_maxpool().unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 1);
assert!(output[0].is_finite());
// Verify it's computing a pooling operation (should be in range of inputs)
assert!(output[0] >= 0.0 && output[0] <= 10.0);
}
}