use super::cache::get_or_create_client;
fn wgpu_err(e: super::device::WgpuError) -> crate::error::Error {
crate::error::Error::Backend(format!("WebGPU client error: {e}"))
}
use super::client::WgpuClient;
use super::device::WgpuDevice;
use super::shaders;
use crate::runtime::{Allocator, NoOpGraph, Runtime, RuntimeClient};
use std::time::Duration;
#[derive(Clone, Debug, Default)]
pub struct WgpuRuntime;
impl Runtime for WgpuRuntime {
type Device = WgpuDevice;
type Client = WgpuClient;
type Allocator = super::WgpuAllocator;
type Graph = NoOpGraph;
type RawHandle = super::WgpuRawHandle;
type DType = crate::dtype::DType;
fn name() -> &'static str {
"wgpu"
}
fn supports_graph_capture() -> bool {
false }
fn computes_float_dtype(dtype: Self::DType) -> bool {
dtype == crate::dtype::DType::F32
}
fn handles_take_byte_offsets() -> bool {
false
}
fn allocate(size_bytes: usize, device: &Self::Device) -> crate::error::Result<u64> {
if size_bytes == 0 {
return Ok(0);
}
let client = get_or_create_client(device).map_err(wgpu_err)?;
client.allocator.allocate(size_bytes)
}
fn deallocate(ptr: u64, size_bytes: usize, device: &Self::Device) {
if ptr == 0 {
return;
}
if let Ok(client) = get_or_create_client(device) {
client.allocator.deallocate(ptr, size_bytes);
}
}
fn copy_to_device(src: &[u8], dst: u64, device: &Self::Device) -> crate::error::Result<()> {
if src.is_empty() || dst == 0 {
return Ok(());
}
let client = get_or_create_client(device).map_err(wgpu_err)?;
let buffer = super::client::get_buffer(dst).ok_or_else(|| {
crate::error::Error::Backend("Buffer not found for copy_to_device".into())
})?;
let tail = src.len() % 4;
if tail == 0 {
client.queue.write_buffer(&buffer, 0, src);
} else {
let head = src.len() - tail;
if head > 0 {
client.queue.write_buffer(&buffer, 0, &src[..head]);
}
let mut word = [0u8; 4];
word[..tail].copy_from_slice(&src[head..]);
client.queue.write_buffer(&buffer, head as u64, &word);
}
client.synchronize();
Ok(())
}
fn copy_from_device(
src: u64,
dst: &mut [u8],
device: &Self::Device,
) -> crate::error::Result<()> {
if dst.is_empty() || src == 0 {
return Ok(());
}
let client = get_or_create_client(device).map_err(wgpu_err)?;
let buffer = super::client::get_buffer(src).ok_or_else(|| {
crate::error::Error::Backend("Buffer not found for copy_from_device".into())
})?;
let aligned_len = dst.len().div_ceil(4) * 4;
let staging = client.create_staging_buffer("copy_staging", aligned_len as u64);
let mut encoder =
client
.wgpu_device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("copy_from_device"),
});
encoder.copy_buffer_to_buffer(&buffer, 0, &staging, 0, aligned_len as u64);
client.submit_and_wait(encoder);
let slice = staging.slice(..);
let (sender, receiver) = std::sync::mpsc::channel();
slice.map_async(wgpu::MapMode::Read, move |result| {
let _ = sender.send(result);
});
client
.wgpu_device
.poll(wgpu::PollType::Wait {
submission_index: None,
timeout: Some(Duration::from_secs(60)),
})
.map_err(|e| {
crate::error::Error::Backend(format!(
"GPU poll failed during copy_from_device: {e}"
))
})?;
let map_result = receiver.recv().map_err(|_| {
crate::error::Error::Backend(
"map_async callback was not invoked during copy_from_device".into(),
)
})?;
map_result.map_err(|e| {
crate::error::Error::Backend(format!("map_async failed during copy_from_device: {e}"))
})?;
{
let data = slice.get_mapped_range().map_err(|e| {
crate::error::Error::Backend(format!(
"get_mapped_range failed during copy_from_device: {e}"
))
})?;
dst.copy_from_slice(&data[..dst.len()]);
}
staging.unmap();
Ok(())
}
fn copy_within_device(
src: u64,
dst: u64,
size_bytes: usize,
device: &Self::Device,
) -> crate::error::Result<()> {
if size_bytes == 0 || src == 0 || dst == 0 {
return Ok(());
}
let client = get_or_create_client(device).map_err(wgpu_err)?;
let src_buffer = super::client::get_buffer(src)
.ok_or_else(|| crate::error::Error::Backend("Source buffer not found".into()))?;
let dst_buffer = super::client::get_buffer(dst)
.ok_or_else(|| crate::error::Error::Backend("Destination buffer not found".into()))?;
let mut encoder =
client
.wgpu_device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("copy_within_device"),
});
encoder.copy_buffer_to_buffer(&src_buffer, 0, &dst_buffer, 0, size_bytes as u64);
client.submit_and_wait(encoder);
Ok(())
}
fn copy_strided(
src_handle: u64,
src_byte_offset: usize,
dst_handle: u64,
shape: &[usize],
strides: &[isize],
elem_size: usize,
device: &Self::Device,
) -> crate::error::Result<()> {
if src_handle == 0 || dst_handle == 0 || shape.iter().product::<usize>() == 0 {
return Ok(());
}
let client = get_or_create_client(device).map_err(wgpu_err)?;
let src_buffer = super::client::get_buffer(src_handle).ok_or_else(|| {
crate::error::Error::Backend("Source buffer not found for copy_strided".into())
})?;
let dst_buffer = super::client::get_buffer(dst_handle).ok_or_else(|| {
crate::error::Error::Backend("Destination buffer not found for copy_strided".into())
})?;
shaders::copy::launch_strided_copy(
&client,
&src_buffer,
&dst_buffer,
shape,
strides,
elem_size,
src_byte_offset,
)
}
fn capture_graph_into<F>(
client: &Self::Client,
inputs: &[&crate::tensor::Tensor<Self>],
outputs: &[&crate::tensor::Tensor<Self>],
f: F,
) -> crate::error::Result<crate::runtime::CapturedGraph<Self>>
where
F: FnOnce(&Self::Client) -> crate::error::Result<()>,
{
f(client)?;
let owned_inputs: Vec<_> = inputs.iter().map(|t| (*t).clone()).collect();
let owned_outputs: Vec<_> = outputs.iter().map(|t| (*t).clone()).collect();
Ok(crate::runtime::CapturedGraph::new(
crate::runtime::NoOpGraph,
owned_inputs,
owned_outputs,
))
}
fn default_device() -> Self::Device {
WgpuDevice::new(0)
}
fn default_client(device: &Self::Device) -> Self::Client {
get_or_create_client(device).expect("Failed to create WebGPU client")
}
fn raw_handle(client: &Self::Client) -> &Self::RawHandle {
&client.raw_handle
}
}
pub fn wgpu_device() -> WgpuDevice {
WgpuDevice::new(0)
}
pub fn wgpu_device_id(index: usize) -> WgpuDevice {
WgpuDevice::new(index)
}
pub fn is_wgpu_available() -> bool {
super::device::query_adapter_info_blocking(0).is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_copy_round_trip_unaligned_length() {
let device = WgpuRuntime::default_device();
if !is_wgpu_available() {
println!("No GPU available, skipping test");
return;
}
let src: Vec<u8> = (0..18u8)
.map(|i| i.wrapping_mul(7).wrapping_add(3))
.collect();
let ptr = WgpuRuntime::allocate(src.len(), &device).expect("allocation should succeed");
WgpuRuntime::copy_to_device(&src, ptr, &device).expect("host to device copy");
let mut dst = vec![0u8; src.len()];
WgpuRuntime::copy_from_device(ptr, &mut dst, &device).expect("device to host copy");
WgpuRuntime::deallocate(ptr, src.len(), &device);
assert_eq!(dst, src, "unaligned round trip must be byte-exact");
}
}