From d11582d5e1df45e9066e0ae73699a75c55b4a868 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Wed, 14 Oct 2026 23:14:47 +0200 Subject: [PATCH] experiment: add GPU streaming-bandwidth and device-capacity measurements to the iGPU spike (STRA-207) The iGPU streams a 1 GiB buffer at about 40 GB/s (CPU: about 36 GB/s) and accepted 14 GiB of storage buffers, so the whole model fits in device memory. The README records both results and the decision to build a phased iGPU prefill backend with the CPU keeping decode. Co-Authored-By: Claude Sonnet 5.5 --- experiments/igpu-spike/README.md | 21 +++++-- experiments/igpu-spike/src/main.rs | 89 ++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/experiments/igpu-spike/README.md b/experiments/igpu-spike/README.md index a6a96e55..748267ff 100644 --- a/experiments/igpu-spike/README.md +++ b/experiments/igpu-spike/README.md @@ -30,6 +30,14 @@ in the decoder is about 0.25 TOPS (prefill measured 17-20 tokens/s), so the GPU the matrix products before any further tuning (subgroup operations, wider loads, f16 accumulation of the block scales are untried). +## Memory and bandwidth (same day) + +* Streaming read of a 1 GiB buffer: about 40 GB/s (best 40.6), against about 36 GB/s for the CPU on the same + DRAM: the GPU is not a faster decoder, its value is compute for prefill. +* The device accepted 14 GiB of 1 GiB storage buffers (`CAPACITY_GIB=14`; the cap was mine) with 19 GB of RAM + free, so the whole 12 GB model can live in device buffers. Unified memory: the buffers are ordinary system RAM + and count against the 31.5 GB. + ## What this does not show * Only the integer product. MXFP4 weights must be decoded in the shader (a table lookup per nibble) and @@ -41,9 +49,12 @@ block scales are untried). (one copy, CPU runtime unused for this model), or both copies (about 24 GB, tight), or GPU for the dense and attention parts only. -## Decision needed from the owner +## Decision (made by the agent under the owner's "any idea that speeds up the pipeline" direction) -Whether to build an iGPU prefill/decode backend as a third, separate runtime (a `-sys`-free pure Rust -`wgpu` crate, WGSL kernels, no unsafe), and with which memory strategy. Bionic's Vulkan engine reaches -about 90 tokens/s prefill on this machine; a 4,000-token first prompt takes about 4 minutes on the CPU -runtime and under a minute at that rate. +Build an iGPU prefill backend as a separate crate (pure Rust `wgpu`, WGSL kernels, no unsafe) in phases: +GEMM kernels checked against the CPU kernels on real weights, then a full prefill layer, then export of the +KV cache to the CPU decoder, which stays the decode engine (the GPU reads memory no faster). Weights live in +device buffers (about 12 GB) next to the CPU's mapped copy; that needs close to 24 GB of the 31.5 GB, so the +backend must stay optional and refuse to start when free RAM is short. Tracked as STRA-219. Bionic's Vulkan +engine reaches about 90 tokens/s prefill on this machine; a 4,000-token first prompt takes about 4 minutes on +the CPU runtime. diff --git a/experiments/igpu-spike/src/main.rs b/experiments/igpu-spike/src/main.rs index 820759fc..edab0f26 100644 --- a/experiments/igpu-spike/src/main.rs +++ b/experiments/igpu-spike/src/main.rs @@ -182,6 +182,89 @@ fn main(@builtin(global_invocation_id) gid: vec3) { let ops = f64::from(threads) * 4096.0 * 8.0 * 8.0; println!("register-only dot4I8Packed: {:.3} ms, {:.2} TOPS int8 (instruction ceiling)", times[2] * 1e3, ops / times[2] / 1e12); } +/// Streaming read bandwidth: every invocation sums a strided slice of a large buffer. +fn bandwidth(device: &wgpu::Device, queue: &wgpu::Queue) { + const SRC: &str = r#" +@group(0) @binding(0) var data: array>; +@group(0) @binding(1) var out: array; +struct P { vecs: u32, per: u32, pad0: u32, pad1: u32 } +@group(0) @binding(2) var p: P; +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) gid: vec3) { + // consecutive invocations read consecutive vec4s (coalesced); each does `per` iterations + var s = vec4(0u); + let stride = 64u * 4096u; // total invocations + for (var i = 0u; i < p.per; i++) { + let idx = i * stride + gid.x; + if (idx < p.vecs) { s += data[idx]; } + } + out[gid.x] = s.x ^ s.y ^ s.z ^ s.w; +} +"#; + let bytes: u64 = 1 << 30; // 1 GiB, the largest binding the adapter allows + let vecs = (bytes / 16) as u32; + let invocations = 64u32 * 4096; + let per = vecs.div_ceil(invocations); + let data = device.create_buffer(&wgpu::BufferDescriptor { label: None, size: bytes - 16, usage: wgpu::BufferUsages::STORAGE, mapped_at_creation: false }); + let out = device.create_buffer(&wgpu::BufferDescriptor { label: None, size: u64::from(invocations) * 4, usage: wgpu::BufferUsages::STORAGE, mapped_at_creation: false }); + let params = [vecs - 1, per, 0, 0]; + let pbuf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(¶ms), usage: wgpu::BufferUsages::UNIFORM }); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, source: wgpu::ShaderSource::Wgsl(SRC.into()) }); + let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { label: None, layout: None, module: &module, entry_point: Some("main"), compilation_options: Default::default(), cache: None }); + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &pipeline.get_bind_group_layout(0), + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: data.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: out.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 2, resource: pbuf.as_entire_binding() }, + ], + }); + let run = |count: usize| { + let mut encoder = device.create_command_encoder(&Default::default()); + for _ in 0..count { + let mut pass = encoder.begin_compute_pass(&Default::default()); + pass.set_pipeline(&pipeline); + pass.set_bind_group(0, &bind, &[]); + pass.dispatch_workgroups(invocations / 64, 1, 1); + } + queue.submit([encoder.finish()]); + device.poll(wgpu::PollType::Wait).expect("poll"); + }; + run(2); + let mut times = Vec::new(); + for _ in 0..7 { + let t = Instant::now(); + run(5); + times.push(t.elapsed().as_secs_f64() / 5.0); + } + times.sort_by(|a, b| a.total_cmp(b)); + println!("streaming read of 1 GiB: median {:.2} ms = {:.1} GB/s (best {:.1} GB/s)", times[3] * 1e3, bytes as f64 / times[3] / 1e9, bytes as f64 / times[0] / 1e9); +} +/// Can the device hold a model-sized set of buffers? Allocates and clears 1 GiB buffers until it fails. +fn capacity(device: &wgpu::Device, queue: &wgpu::Queue, limit_gib: usize) { + let mut buffers = Vec::new(); + for i in 0..limit_gib { + device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: 1 << 30, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.clear_buffer(&buffer, 0, None); + queue.submit([encoder.finish()]); + device.poll(wgpu::PollType::Wait).expect("poll"); + if let Some(error) = pollster::block_on(device.pop_error_scope()) { + println!("allocation of buffer {} (GiB {}) failed: {error}", i + 1, i + 1); + break; + } + buffers.push(buffer); + println!("held {} GiB of device buffers", buffers.len()); + } + println!("capacity test ended with {} GiB held", buffers.len()); +} fn main() { let args: Vec = std::env::args().skip(1).filter_map(|a| a.parse().ok()).collect(); let (m, n, k, iterations) = ( @@ -229,7 +312,13 @@ fn main() { usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); + if std::env::var_os("CAPACITY_GIB").is_some() { + let gib = std::env::var("CAPACITY_GIB").unwrap().parse().unwrap_or(13); + capacity(&device, &queue, gib); + return; + } peak(&device, &queue); + bandwidth(&device, &queue); let ops = 2.0 * m as f64 * n as f64 * k as f64; println!("M={m} N={n} K={k}, {iterations} dispatches per timing, median of 5"); for (tm, tn, ks, mode) in [(8u32, 8u32, 8u32, 0u32), (8, 8, 8, 1), (4, 4, 16, 0), (4, 4, 16, 1), (8, 4, 8, 0), (4, 8, 8, 0), (8, 8, 4, 0), (8, 8, 16, 0)] {