pub struct WebGpuBackend { /* private fields */ }Expand description
Cross-platform GPU compute backend backed by wgpu.
§Lifecycle
WebGpuBackend::new()— create an uninitialised backend.init()— select the best available adapter and create the device.- Use
alloc,copy_htod, compute ops,copy_dtoh,free. synchronize()— wait for all pending GPU work to finish.
Implementations§
Source§impl WebGpuBackend
impl WebGpuBackend
Sourcepub fn supports_f16(&self) -> bool
pub fn supports_f16(&self) -> bool
Whether the initialised device enabled the SHADER_F16 feature, i.e.
whether gemm_f16 can run instead of returning
BackendError::Unsupported. Returns false (never errors) before
init() — callers that want to skip f16-only tests on an
uninitialised or non-f16 backend can check this directly.
Source§impl WebGpuBackend
impl WebGpuBackend
Sourcepub fn gemm_f16(
&self,
trans_a: BackendTranspose,
trans_b: BackendTranspose,
m: usize,
n: usize,
k: usize,
alpha: f64,
a_ptr: u64,
lda: usize,
b_ptr: u64,
ldb: usize,
beta: f64,
c_ptr: u64,
ldc: usize,
) -> BackendResult<()>
pub fn gemm_f16( &self, trans_a: BackendTranspose, trans_b: BackendTranspose, m: usize, n: usize, k: usize, alpha: f64, a_ptr: u64, lda: usize, b_ptr: u64, ldb: usize, beta: f64, c_ptr: u64, ldc: usize, ) -> BackendResult<()>
FP16 GEMM: C = alpha * op(A) * op(B) + beta * C with half-precision
storage (accumulated in f32).
This is an inherent method (not on ComputeBackend) because FP16
support is WebGPU-specific and requires the f16 WGSL extension.
Buffers pointed to by a_ptr, b_ptr, c_ptr must contain f16
elements (2 bytes each). lda / ldb / ldc and trans_a /
trans_b follow exactly the same convention as
gemm — see shader::gemm_wgsl_f16.
Trait Implementations§
Source§impl ComputeBackend for WebGpuBackend
impl ComputeBackend for WebGpuBackend
Source§fn init(&mut self) -> BackendResult<()>
fn init(&mut self) -> BackendResult<()>
Source§fn is_initialized(&self) -> bool
fn is_initialized(&self) -> bool
true if the backend is ready for operations.Source§fn gemm(
&self,
trans_a: BackendTranspose,
trans_b: BackendTranspose,
m: usize,
n: usize,
k: usize,
alpha: f64,
a_ptr: u64,
lda: usize,
b_ptr: u64,
ldb: usize,
beta: f64,
c_ptr: u64,
ldc: usize,
) -> BackendResult<()>
fn gemm( &self, trans_a: BackendTranspose, trans_b: BackendTranspose, m: usize, n: usize, k: usize, alpha: f64, a_ptr: u64, lda: usize, b_ptr: u64, ldb: usize, beta: f64, c_ptr: u64, ldc: usize, ) -> BackendResult<()>
C = alpha * op(A) * op(B) + beta * C. Read moreSource§fn batched_gemm(
&self,
trans_a: BackendTranspose,
trans_b: BackendTranspose,
m: usize,
n: usize,
k: usize,
alpha: f64,
a_ptr: u64,
lda: usize,
stride_a: usize,
b_ptr: u64,
ldb: usize,
stride_b: usize,
beta: f64,
c_ptr: u64,
ldc: usize,
stride_c: usize,
batch_count: usize,
) -> BackendResult<()>
fn batched_gemm( &self, trans_a: BackendTranspose, trans_b: BackendTranspose, m: usize, n: usize, k: usize, alpha: f64, a_ptr: u64, lda: usize, stride_a: usize, b_ptr: u64, ldb: usize, stride_b: usize, beta: f64, c_ptr: u64, ldc: usize, stride_c: usize, batch_count: usize, ) -> BackendResult<()>
b in 0..batch_count,
compute C_b = alpha * op(A_b) * op(B_b) + beta * C_b
where A_b starts at a_ptr + b * stride_a * 4 bytes (f32 elements), etc. Read moreSource§fn conv2d_forward(
&self,
input_ptr: u64,
input_shape: &[usize],
filter_ptr: u64,
filter_shape: &[usize],
output_ptr: u64,
output_shape: &[usize],
stride: &[usize],
padding: &[usize],
) -> BackendResult<()>
fn conv2d_forward( &self, input_ptr: u64, input_shape: &[usize], filter_ptr: u64, filter_shape: &[usize], output_ptr: u64, output_shape: &[usize], stride: &[usize], padding: &[usize], ) -> BackendResult<()>
Source§fn attention(
&self,
q_ptr: u64,
k_ptr: u64,
v_ptr: u64,
o_ptr: u64,
batch: usize,
heads: usize,
seq_q: usize,
seq_kv: usize,
head_dim: usize,
scale: f64,
causal: bool,
) -> BackendResult<()>
fn attention( &self, q_ptr: u64, k_ptr: u64, v_ptr: u64, o_ptr: u64, batch: usize, heads: usize, seq_q: usize, seq_kv: usize, head_dim: usize, scale: f64, causal: bool, ) -> BackendResult<()>
Source§fn reduce(
&self,
op: ReduceOp,
input_ptr: u64,
output_ptr: u64,
shape: &[usize],
axis: usize,
) -> BackendResult<()>
fn reduce( &self, op: ReduceOp, input_ptr: u64, output_ptr: u64, shape: &[usize], axis: usize, ) -> BackendResult<()>
Source§fn unary(
&self,
op: UnaryOp,
input_ptr: u64,
output_ptr: u64,
n: usize,
) -> BackendResult<()>
fn unary( &self, op: UnaryOp, input_ptr: u64, output_ptr: u64, n: usize, ) -> BackendResult<()>
Source§fn binary(
&self,
op: BinaryOp,
a_ptr: u64,
b_ptr: u64,
output_ptr: u64,
n: usize,
) -> BackendResult<()>
fn binary( &self, op: BinaryOp, a_ptr: u64, b_ptr: u64, output_ptr: u64, n: usize, ) -> BackendResult<()>
Source§fn synchronize(&self) -> BackendResult<()>
fn synchronize(&self) -> BackendResult<()>
Source§fn free(&self, ptr: u64) -> BackendResult<()>
fn free(&self, ptr: u64) -> BackendResult<()>
alloc.Source§fn copy_htod(&self, dst: u64, src: &[u8]) -> BackendResult<()>
fn copy_htod(&self, dst: u64, src: &[u8]) -> BackendResult<()>
Source§fn copy_dtoh(&self, dst: &mut [u8], src: u64) -> BackendResult<()>
fn copy_dtoh(&self, dst: &mut [u8], src: u64) -> BackendResult<()>
Source§fn capabilities(&self) -> Capabilities
fn capabilities(&self) -> Capabilities
Source§fn available_devices(&self) -> Result<Vec<DeviceInfo>, BackendError>
fn available_devices(&self) -> Result<Vec<DeviceInfo>, BackendError>
DeviceInfo shape. Read moreSource§fn recommended_tile_for(&self, m: usize, n: usize, k: usize) -> TileShape
fn recommended_tile_for(&self, m: usize, n: usize, k: usize) -> TileShape
(tile_m, tile_n, tile_k) for the given
problem dimensions, to seed an autotuner. Read moreSource§fn gemm_mixed_precision(
&self,
prec: MixedPrecision,
trans_a: BackendTranspose,
trans_b: BackendTranspose,
m: usize,
n: usize,
k: usize,
alpha: f32,
a_ptr: u64,
lda: usize,
b_ptr: u64,
ldb: usize,
beta: f32,
c_ptr: u64,
ldc: usize,
) -> Result<(), BackendError>
fn gemm_mixed_precision( &self, prec: MixedPrecision, trans_a: BackendTranspose, trans_b: BackendTranspose, m: usize, n: usize, k: usize, alpha: f32, a_ptr: u64, lda: usize, b_ptr: u64, ldb: usize, beta: f32, c_ptr: u64, ldc: usize, ) -> Result<(), BackendError>
C = alpha * op(A) * op(B) + beta * C where the
A/B operands are stored in a reduced 16-bit format
(MixedPrecision::F16 or MixedPrecision::Bf16) but the dot
products accumulate in f32 — the Tensor-Core / WMMA contract. Read moreSource§fn conv2d_backward_data(
&self,
grad_output_ptr: u64,
grad_output_shape: &[usize],
filter_ptr: u64,
filter_shape: &[usize],
grad_input_ptr: u64,
grad_input_shape: &[usize],
stride: &[usize],
padding: &[usize],
) -> Result<(), BackendError>
fn conv2d_backward_data( &self, grad_output_ptr: u64, grad_output_shape: &[usize], filter_ptr: u64, filter_shape: &[usize], grad_input_ptr: u64, grad_input_shape: &[usize], stride: &[usize], padding: &[usize], ) -> Result<(), BackendError>
conv2d_forward w.r.t. the
input (data gradient): given the upstream gradient grad_output,
produce grad_input of the same shape as the forward input. Read moreSource§fn conv2d_backward_filter(
&self,
input_ptr: u64,
input_shape: &[usize],
grad_output_ptr: u64,
grad_output_shape: &[usize],
grad_filter_ptr: u64,
grad_filter_shape: &[usize],
stride: &[usize],
padding: &[usize],
) -> Result<(), BackendError>
fn conv2d_backward_filter( &self, input_ptr: u64, input_shape: &[usize], grad_output_ptr: u64, grad_output_shape: &[usize], grad_filter_ptr: u64, grad_filter_shape: &[usize], stride: &[usize], padding: &[usize], ) -> Result<(), BackendError>
conv2d_forward w.r.t. the
filter (weight gradient): given the forward input and the upstream
gradient grad_output, produce grad_filter of the same shape as the
forward filter. Read moreSource§fn softmax(
&self,
input_ptr: u64,
output_ptr: u64,
shape: &[usize],
axis: usize,
) -> Result<(), BackendError>
fn softmax( &self, input_ptr: u64, output_ptr: u64, shape: &[usize], axis: usize, ) -> Result<(), BackendError>
Source§fn gather(
&self,
input_ptr: u64,
indices: &[usize],
output_ptr: u64,
rows: usize,
cols: usize,
) -> Result<(), BackendError>
fn gather( &self, input_ptr: u64, indices: &[usize], output_ptr: u64, rows: usize, cols: usize, ) -> Result<(), BackendError>
indices out of a rows × cols
(f32, row-major) table into a contiguous output of
indices.len() × cols. Read moreSource§fn scatter(
&self,
input_ptr: u64,
indices: &[usize],
output_ptr: u64,
rows: usize,
cols: usize,
) -> Result<(), BackendError>
fn scatter( &self, input_ptr: u64, indices: &[usize], output_ptr: u64, rows: usize, cols: usize, ) -> Result<(), BackendError>
indices.len() × cols, f32) into
output at the destination row given by indices, preserving
unreferenced rows of the rows × cols output table. Read more