use crate::error::Result;
use crate::tensor::Tensor;
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct BatchMatMulParams {
batch_size: u32,
m: u32, n: u32, k: u32, }
pub struct BatchMatMul {
a: Tensor,
b: Tensor,
}
impl BatchMatMul {
#[must_use]
pub fn new(a: Tensor, b: Tensor) -> Self {
Self { a, b }
}
fn wgsl_shader() -> &'static str {
static SHADER: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
include_str!("../shaders/math/batch_matmul_f64.wgsl").to_string()
});
&SHADER
}
pub fn execute(self) -> Result<Tensor> {
let device = self.a.device();
let a_shape = self.a.shape();
let b_shape = self.b.shape();
let batch_size = a_shape[0];
let m = a_shape[1];
let k = a_shape[2];
let n = b_shape[2];
let output_size = batch_size * m * n;
let output_buffer = device.create_buffer_f32(output_size)?;
let params = BatchMatMulParams {
batch_size: batch_size as u32,
m: m as u32,
n: n as u32,
k: k as u32,
};
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("BatchMatMul Params"),
contents: bytemuck::bytes_of(¶ms),
usage: wgpu::BufferUsages::UNIFORM,
});
let shader = device.compile_shader(Self::wgsl_shader(), Some("BatchMatMul Shader"));
let pipeline = device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("BatchMatMul Pipeline"),
layout: None,
module: &shader,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
let bind_group_layout = pipeline.get_bind_group_layout(0);
let bind_group = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("BatchMatMul Bind Group"),
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.a.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.b.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: output_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: params_buffer.as_entire_binding(),
},
],
});
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("BatchMatMul Encoder"),
});
{
let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("BatchMatMul Pass"),
timestamp_writes: None,
});
compute_pass.set_pipeline(&pipeline);
compute_pass.set_bind_group(0, Some(&bind_group), &[]);
let workgroups_x = n.div_ceil(16) as u32;
let workgroups_y = m.div_ceil(16) as u32;
let workgroups_z = batch_size as u32;
compute_pass.dispatch_workgroups(workgroups_x, workgroups_y, workgroups_z);
}
device.submit_commands(Some(encoder.finish()));
Ok(Tensor::from_buffer(
output_buffer,
vec![batch_size, m, n],
device.clone(),
))
}
}
impl Tensor {
pub fn batch_matmul(self, other: &Self) -> Result<Self> {
BatchMatMul::new(self, other.clone()).execute()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_batch_matmul_basic() {
let device = crate::device::test_pool::get_test_device().await;
let a_data = vec![
1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, ];
let a = Tensor::from_data(&a_data, vec![2, 2, 3], device.clone()).unwrap();
let b_data = vec![
1.0f32, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 0.0, 0.0, 2.0, 1.0, 1.0,
];
let b = Tensor::from_data(&b_data, vec![2, 3, 2], device).unwrap();
let result = a.batch_matmul(&b).unwrap();
let output = result.to_vec().unwrap();
assert_eq!(result.shape(), &[2, 2, 2]);
assert_eq!(output.len(), 8);
assert!(output.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_batch_matmul_edge_cases() {
let device = crate::device::test_pool::get_test_device().await;
let a_data = vec![1.0, 0.0, 0.0, 1.0]; let b_data = vec![1.0, 2.0, 3.0, 4.0];
let a = Tensor::from_data(&a_data, vec![1, 2, 2], device.clone()).unwrap();
let b = Tensor::from_data(&b_data, vec![1, 2, 2], device).unwrap();
let result = a.batch_matmul(&b).unwrap();
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 4);
assert!(output.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_batch_matmul_boundary() {
let device = crate::device::test_pool::get_test_device().await;
let a_data = vec![1.0; 2 * 4 * 3]; let b_data = vec![1.0; 2 * 3 * 5];
let a = Tensor::from_data(&a_data, vec![2, 4, 3], device.clone()).unwrap();
let b = Tensor::from_data(&b_data, vec![2, 3, 5], device).unwrap();
let result = a.batch_matmul(&b).unwrap();
assert_eq!(result.shape(), &[2, 4, 5]);
let output = result.to_vec().unwrap();
assert_eq!(output.len(), 2 * 4 * 5);
assert!(output.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_batch_matmul_large_batch() {
let device = crate::device::test_pool::get_test_device().await;
let batch_size = 4;
let seq_len = 8;
let d_k = 16;
let a_data = vec![1.0; batch_size * seq_len * d_k];
let b_data = vec![1.0; batch_size * d_k * seq_len];
let a = Tensor::from_data(&a_data, vec![batch_size, seq_len, d_k], device.clone()).unwrap();
let b = Tensor::from_data(&b_data, vec![batch_size, d_k, seq_len], device).unwrap();
let result = a.batch_matmul(&b).unwrap();
assert_eq!(result.shape(), &[batch_size, seq_len, seq_len]);
let output = result.to_vec().unwrap();
assert!(output.iter().all(|&x| x.is_finite()));
}
#[tokio::test]
async fn test_batch_matmul_precision() {
let device = crate::device::test_pool::get_test_device().await;
let a_data = vec![1.0, 2.0, 3.0, 4.0]; let b_data = vec![5.0, 6.0, 7.0, 8.0];
let a1 = Tensor::from_data(&a_data, vec![1, 2, 2], device.clone()).unwrap();
let b1 = Tensor::from_data(&b_data, vec![1, 2, 2], device.clone()).unwrap();
let a2 = Tensor::from_data(&a_data, vec![1, 2, 2], device.clone()).unwrap();
let b2 = Tensor::from_data(&b_data, vec![1, 2, 2], device).unwrap();
let result1 = a1.batch_matmul(&b1).unwrap();
let result2 = a2.batch_matmul(&b2).unwrap();
let output1 = result1.to_vec().unwrap();
let output2 = result2.to_vec().unwrap();
assert_eq!(output1, output2);
assert_eq!(output1.len(), 4); assert!(output1.iter().all(|&x| x.is_finite()));
}
}