use crate::device::WgpuDevice;
use crate::device::compute_pipeline::{storage_bgl_entry, uniform_bgl_entry};
use crate::error::Result;
use std::sync::Arc;
#[derive(Debug)]
pub enum RecordedOp {
Add {
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
},
Mul {
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
},
Fma {
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
input_c: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
},
Scale {
input: wgpu::Buffer,
scalar: f32,
output: wgpu::Buffer,
size: usize,
},
Custom {
shader_source: String,
buffers: Vec<wgpu::Buffer>,
workgroups: (u32, u32, u32),
},
}
pub struct ComputeGraph {
wgpu_device: Arc<WgpuDevice>,
device: crate::device::wgpu_device::GuardedDeviceHandle,
queue: wgpu::Queue,
device_name: String,
ops: Vec<RecordedOp>,
optimal_workgroup_size: u32,
}
impl ComputeGraph {
#[must_use]
pub fn new(wgpu_device: &WgpuDevice) -> Self {
Self::with_device(Arc::new(wgpu_device.clone()))
}
#[must_use]
pub fn with_device(wgpu_device: Arc<WgpuDevice>) -> Self {
let optimal_wg = wgpu_device.optimal_workgroup_size();
let device = wgpu_device.device.clone();
let queue = wgpu_device.queue.clone();
let device_name = wgpu_device.name().to_string();
Self {
wgpu_device,
device,
queue,
device_name,
ops: Vec::new(),
optimal_workgroup_size: optimal_wg,
}
}
#[must_use]
pub fn device_name(&self) -> &str {
&self.device_name
}
pub fn record_add(
&mut self,
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
) {
self.ops.push(RecordedOp::Add {
input_a,
input_b,
output,
size,
});
}
pub fn record_mul(
&mut self,
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
) {
self.ops.push(RecordedOp::Mul {
input_a,
input_b,
output,
size,
});
}
pub fn record_fma(
&mut self,
input_a: wgpu::Buffer,
input_b: wgpu::Buffer,
input_c: wgpu::Buffer,
output: wgpu::Buffer,
size: usize,
) {
self.ops.push(RecordedOp::Fma {
input_a,
input_b,
input_c,
output,
size,
});
}
#[must_use]
pub fn len(&self) -> usize {
self.ops.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.ops.is_empty()
}
pub fn clear(&mut self) {
self.ops.clear();
}
pub fn execute(&mut self) -> Result<()> {
if self.ops.is_empty() {
return Ok(());
}
let mut encoder =
self.wgpu_device
.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("ComputeGraph Batch"),
});
let add_shader = self.compile_elementwise("output[idx] = a[idx] + b[idx];", "Add");
let mul_shader = self.compile_elementwise("output[idx] = a[idx] * b[idx];", "Mul");
let fma_shader = self.compile_shader(
&format!(
r"
@group(0) @binding(0) var<storage, read> a: array<f32>;
@group(0) @binding(1) var<storage, read> b: array<f32>;
@group(0) @binding(2) var<storage, read> c: array<f32>;
@group(0) @binding(3) var<storage, read_write> output: array<f32>;
@compute @workgroup_size({wg})
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{
let idx = global_id.x;
if (idx >= arrayLength(&output)) {{ return; }}
output[idx] = fma(a[idx], b[idx], c[idx]);
}}",
wg = self.optimal_workgroup_size
),
"FMA",
);
let scale_shader = self.compile_shader(
&format!(
r"
@group(0) @binding(0) var<storage, read> input: array<f32>;
@group(0) @binding(1) var<uniform> scalar: f32;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@compute @workgroup_size({wg})
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {{
let idx = gid.x;
if (idx >= arrayLength(&output)) {{ return; }}
output[idx] = input[idx] * scalar;
}}",
wg = self.optimal_workgroup_size
),
"Scale",
);
for op in &self.ops {
match op {
RecordedOp::Add {
input_a,
input_b,
output,
size,
} => {
self.dispatch_pass(
&mut encoder,
&add_shader,
&[
storage_bgl_entry(0, true),
storage_bgl_entry(1, true),
storage_bgl_entry(2, false),
],
&[input_a, input_b, output],
*size,
);
}
RecordedOp::Mul {
input_a,
input_b,
output,
size,
} => {
self.dispatch_pass(
&mut encoder,
&mul_shader,
&[
storage_bgl_entry(0, true),
storage_bgl_entry(1, true),
storage_bgl_entry(2, false),
],
&[input_a, input_b, output],
*size,
);
}
RecordedOp::Fma {
input_a,
input_b,
input_c,
output,
size,
} => {
self.dispatch_pass(
&mut encoder,
&fma_shader,
&[
storage_bgl_entry(0, true),
storage_bgl_entry(1, true),
storage_bgl_entry(2, true),
storage_bgl_entry(3, false),
],
&[input_a, input_b, input_c, output],
*size,
);
}
RecordedOp::Scale {
input,
scalar,
output,
size,
} => {
let scalar_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Scalar Uniform"),
size: 4,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
self.queue
.write_buffer(&scalar_buffer, 0, bytemuck::bytes_of(scalar));
self.dispatch_pass(
&mut encoder,
&scale_shader,
&[
storage_bgl_entry(0, true),
uniform_bgl_entry(1),
storage_bgl_entry(2, false),
],
&[input, &scalar_buffer, output],
*size,
);
}
RecordedOp::Custom {
shader_source,
buffers,
workgroups,
} => {
self.encode_custom_op(&mut encoder, shader_source, buffers, *workgroups);
}
}
}
self.wgpu_device
.submit_and_poll_inner(Some(encoder.finish()));
self.ops.clear();
if self.wgpu_device.is_lost() {
return Err(crate::error::BarracudaError::device(
"GPU device lost during compute graph execution",
));
}
Ok(())
}
fn compile_shader(&self, source: &str, label: &str) -> wgpu::ShaderModule {
self.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(label),
source: wgpu::ShaderSource::Wgsl(source.into()),
})
}
fn compile_elementwise(&self, body: &str, label: &str) -> wgpu::ShaderModule {
self.compile_shader(
&format!(
r"
@group(0) @binding(0) var<storage, read> a: array<f32>;
@group(0) @binding(1) var<storage, read> b: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;
@compute @workgroup_size({wg})
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{
let idx = global_id.x;
if (idx >= arrayLength(&output)) {{ return; }}
{body}
}}",
wg = self.optimal_workgroup_size
),
label,
)
}
fn dispatch_pass(
&self,
encoder: &mut wgpu::CommandEncoder,
shader: &wgpu::ShaderModule,
bgl_entries: &[wgpu::BindGroupLayoutEntry],
buffers: &[&wgpu::Buffer],
size: usize,
) {
let bgl = self
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: bgl_entries,
});
let bind_entries: Vec<_> = buffers
.iter()
.enumerate()
.map(|(i, buf)| wgpu::BindGroupEntry {
binding: i as u32,
resource: buf.as_entire_binding(),
})
.collect();
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &bgl,
entries: &bind_entries,
});
let pipeline_layout = self
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bgl],
immediate_size: 0,
});
let pipeline = self
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
module: shader,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
let workgroups = (size as u32)
.div_ceil(self.optimal_workgroup_size)
.min(65_535);
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
pass.dispatch_workgroups(workgroups, 1, 1);
}
}
fn encode_custom_op(
&self,
encoder: &mut wgpu::CommandEncoder,
shader_source: &str,
buffers: &[wgpu::Buffer],
workgroups: (u32, u32, u32),
) {
let shader = self.compile_shader(shader_source, "Custom");
let bgl_entries: Vec<_> = buffers
.iter()
.enumerate()
.map(|(i, _)| storage_bgl_entry(i as u32, i < buffers.len() - 1))
.collect();
let bgl = self
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &bgl_entries,
});
let bind_entries: Vec<_> = buffers
.iter()
.enumerate()
.map(|(i, buf)| wgpu::BindGroupEntry {
binding: i as u32,
resource: buf.as_entire_binding(),
})
.collect();
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &bgl,
entries: &bind_entries,
});
let pipeline_layout = self
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[&bgl],
immediate_size: 0,
});
let pipeline = self
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
module: &shader,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor::default());
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
pass.dispatch_workgroups(workgroups.0, workgroups.1, workgroups.2);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::device::test_pool;
fn make_storage_buf(device: &WgpuDevice, label: &str, data: &[f32]) -> wgpu::Buffer {
device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(label),
contents: bytemuck::cast_slice(data),
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
})
}
fn make_output_buf(device: &WgpuDevice, label: &str, n: usize) -> wgpu::Buffer {
device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some(label),
size: (n * 4) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
})
}
#[tokio::test]
async fn test_compute_graph_batching() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let data_a: Vec<f32> = (0..1000).map(|i| i as f32).collect();
let data_b: Vec<f32> = (0..1000).map(|i| (i * 2) as f32).collect();
let buf_a = make_storage_buf(&device, "A", &data_a);
let buf_b = make_storage_buf(&device, "B", &data_b);
let buf_out = make_output_buf(&device, "Out", 1000);
graph.record_add(buf_a, buf_b, buf_out, 1000);
assert_eq!(graph.len(), 1);
graph.execute().unwrap();
assert!(graph.is_empty());
}
#[test]
fn test_graph_empty_execute() {
let graph_len = 0_usize;
assert_eq!(graph_len, 0);
}
#[tokio::test]
async fn test_graph_new_is_empty() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let graph = ComputeGraph::new(&device);
assert!(graph.is_empty());
assert_eq!(graph.len(), 0);
}
#[tokio::test]
async fn test_graph_device_name_populated() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let graph = ComputeGraph::new(&device);
assert!(!graph.device_name().is_empty());
}
#[tokio::test]
async fn test_graph_record_mul_increments_len() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let a = make_storage_buf(&device, "a", &[1.0, 2.0]);
let b = make_storage_buf(&device, "b", &[3.0, 4.0]);
let out = make_output_buf(&device, "o", 2);
graph.record_mul(a, b, out, 2);
assert_eq!(graph.len(), 1);
}
#[tokio::test]
async fn test_graph_record_fma_increments_len() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let a = make_storage_buf(&device, "a", &[1.0, 2.0]);
let b = make_storage_buf(&device, "b", &[3.0, 4.0]);
let c = make_storage_buf(&device, "c", &[5.0, 6.0]);
let out = make_output_buf(&device, "o", 2);
graph.record_fma(a, b, c, out, 2);
assert_eq!(graph.len(), 1);
}
#[tokio::test]
async fn test_graph_clear() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let a = make_storage_buf(&device, "a", &[1.0]);
let b = make_storage_buf(&device, "b", &[2.0]);
let out = make_output_buf(&device, "o", 1);
graph.record_add(a, b, out, 1);
assert_eq!(graph.len(), 1);
graph.clear();
assert!(graph.is_empty());
}
#[tokio::test]
async fn test_graph_multiple_ops_batch() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let n = 64;
let ones: Vec<f32> = vec![1.0; n];
let twos: Vec<f32> = vec![2.0; n];
let threes: Vec<f32> = vec![3.0; n];
let a1 = make_storage_buf(&device, "a1", &ones);
let b1 = make_storage_buf(&device, "b1", &twos);
let o1 = make_output_buf(&device, "o1", n);
graph.record_add(a1, b1, o1, n);
let a2 = make_storage_buf(&device, "a2", &twos);
let b2 = make_storage_buf(&device, "b2", &threes);
let o2 = make_output_buf(&device, "o2", n);
graph.record_mul(a2, b2, o2, n);
let fa = make_storage_buf(&device, "fa", &ones);
let fb = make_storage_buf(&device, "fb", &twos);
let fc = make_storage_buf(&device, "fc", &threes);
let fo = make_output_buf(&device, "fo", n);
graph.record_fma(fa, fb, fc, fo, n);
assert_eq!(graph.len(), 3);
graph.execute().unwrap();
assert!(graph.is_empty());
}
#[tokio::test]
async fn test_graph_execute_empty_is_noop() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
graph.execute().unwrap();
assert!(graph.is_empty());
}
#[tokio::test]
async fn test_graph_reuse_after_execute() {
let Some(device) = test_pool::get_test_device_if_gpu_available().await else {
return;
};
let mut graph = ComputeGraph::new(&device);
let a = make_storage_buf(&device, "a", &[1.0, 2.0, 3.0, 4.0]);
let b = make_storage_buf(&device, "b", &[5.0, 6.0, 7.0, 8.0]);
let o = make_output_buf(&device, "o", 4);
graph.record_add(a, b, o, 4);
graph.execute().unwrap();
assert!(graph.is_empty());
let a2 = make_storage_buf(&device, "a2", &[10.0, 20.0]);
let b2 = make_storage_buf(&device, "b2", &[30.0, 40.0]);
let o2 = make_output_buf(&device, "o2", 2);
graph.record_mul(a2, b2, o2, 2);
assert_eq!(graph.len(), 1);
graph.execute().unwrap();
assert!(graph.is_empty());
}
}