use crate::device::compute_pipeline::ComputeDispatch;
use crate::device::{DeviceCapabilities, WorkloadType};
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
const SHADER_F64: &str = include_str!("../shaders/augmentation/random_crop_f64.wgsl");
static SHADER_F32: std::sync::LazyLock<String> =
std::sync::LazyLock::new(|| SHADER_F64.to_string());
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct RandomCropParams {
batch_size: u32,
channels: u32,
in_height: u32,
in_width: u32,
out_height: u32,
out_width: u32,
_padding: [u32; 2],
}
pub struct RandomCrop {
input: Tensor,
crop_positions: Tensor,
out_height: usize,
out_width: usize,
}
impl RandomCrop {
pub fn new(
input: Tensor,
crop_positions: Tensor,
out_height: usize,
out_width: usize,
) -> Result<Self> {
let crop_shape = crop_positions.shape();
if crop_shape.len() != 2 || crop_shape[1] != 2 {
return Err(BarracudaError::invalid_op(
"RandomCrop",
format!("crop_positions must be 2D [batch_size, 2], got shape {crop_shape:?}"),
));
}
Ok(Self {
input,
crop_positions,
out_height,
out_width,
})
}
fn wgsl_shader() -> &'static str {
&SHADER_F32
}
pub fn execute(self) -> Result<Tensor> {
let device = self.input.device();
let input_shape = self.input.shape();
if input_shape.len() != 4 {
return Err(BarracudaError::invalid_op(
"RandomCrop",
format!(
"input must be 4D [batch, channels, height, width], got shape {input_shape:?}"
),
));
}
let batch_size = input_shape[0];
let channels = input_shape[1];
let in_height = input_shape[2];
let in_width = input_shape[3];
if self.crop_positions.shape()[0] != batch_size {
return Err(BarracudaError::invalid_op(
"RandomCrop",
format!(
"crop_positions batch size {} must match input batch size {}",
self.crop_positions.shape()[0],
batch_size
),
));
}
let output_size = batch_size * channels * self.out_height * self.out_width;
let output_buffer = device.create_buffer_f32(output_size)?;
let params = RandomCropParams {
batch_size: batch_size as u32,
channels: channels as u32,
in_height: in_height as u32,
in_width: in_width as u32,
out_height: self.out_height as u32,
out_width: self.out_width as u32,
_padding: [0; 2],
};
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("RandomCrop Params"),
contents: bytemuck::cast_slice(&[params]),
usage: wgpu::BufferUsages::UNIFORM,
});
let caps = DeviceCapabilities::from_device(device.as_ref());
let (wg_x, wg_y, wg_z) = caps.optimal_workgroup_size_3d(WorkloadType::Convolution);
let workgroups_x = (self.out_width as u32).div_ceil(wg_x);
let workgroups_y = (self.out_height as u32).div_ceil(wg_y);
let workgroups_z = ((batch_size * channels) as u32).div_ceil(wg_z);
ComputeDispatch::new(device.as_ref(), "RandomCrop")
.shader(Self::wgsl_shader(), "main")
.storage_read(0, self.input.buffer())
.storage_read(1, self.crop_positions.buffer())
.storage_rw(2, &output_buffer)
.uniform(3, ¶ms_buffer)
.dispatch(workgroups_x, workgroups_y, workgroups_z)
.submit()?;
Ok(Tensor::from_buffer(
output_buffer,
vec![batch_size, channels, self.out_height, self.out_width],
device.clone(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_random_crop_basic() {
let device = crate::device::test_pool::get_test_device().await;
let batch_size = 2;
let channels = 3;
let in_height = 32;
let in_width = 32;
let out_height = 16;
let out_width = 16;
let input = Tensor::from_vec_on(
vec![1.0; batch_size * channels * in_height * in_width],
vec![batch_size, channels, in_height, in_width],
device.clone(),
)
.await
.unwrap();
let crop_positions = Tensor::from_vec_on(
vec![5.0, 5.0, 10.0, 10.0], vec![batch_size, 2],
device.clone(),
)
.await
.unwrap();
let result = RandomCrop::new(input, crop_positions, out_height, out_width)
.unwrap()
.execute()
.unwrap();
assert_eq!(
result.shape(),
&[batch_size, channels, out_height, out_width]
);
}
}