Repository navigation
feat: GPU inference via wgpu (Metal on macOS, Vulkan elsewhere) - #6
Merged
Merged
Conversation
- Run all ops on burn-dispatch's Dispatch backend; each model holds its device. load_onnx(path, backend="flex" | "wgpu"), available_backends(). - wgpu is compiled per target to the native shading language (MSL on macOS, SPIR-V elsewhere); the portable WGSL path was ~4x slower on ResNet-18 because it can't use matrix hardware. - Ops take the device from their input tensors; only loading, inputs and inline constants need it passed in. - numpy-style rank broadcasting for elementwise ops and Linear/Gemm bias (Flex tolerated rank mismatches, wgpu does not); scalar ops for Gemm alpha/beta and BN epsilon. - Record the first panic on any thread so worker-thread failures (e.g. no GPU adapter) report the real cause; guard model loading with it. - Tests take BURN_BACKEND; new CI job runs op tests on wgpu via lavapipe. - Replace the unused `burn` umbrella dependency with burn-dispatch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
backend="wgpu"— the RFC's Stage 2 promise. All ops now run on Burn's burn-dispatch backend, which forwards each op to the backend selected by the tensor's device, so the interpreter stays backend-agnostic without making every op generic.ResNet-18 (torchvision export, Apple M-series, median of 15)
wgpu(Metal GPU)flex(CPU)All four ResNet-18 exports match ORT on wgpu too (max logit diff ~1e-5, same top-1).
Design
type B = Dispatch;OnnxModelholds itsDispatchDevice. Ops take the device from their inputs (B::float_device); only weight loading, inputs and inline constants need it passed in.burn-dispatchis declared per target:metalon macOS,vulkanelsewhere. The portablewebgpu(WGSL) path was measured too — ~4× slower (batch 8: 128 ms vs 32 ms) since WGSL can't use matrix hardware. wgpu loads the driver at runtime, so CPU-only machines still work with"flex".burnumbrella crate is replaced byburn-dispatch.Fixes found by running on a stricter backend
[N,C] + [C], Gemm with rank-1C,[N,C,H,W] * [C,1,1]. Burn needs equal ranks; Flex tolerated the mismatch, wgpu doesn't. Newops/broadcast.rsaligns ranks numpy-style for elementwise ops and Linear/Gemm bias.alpha/betaand BNepsilonusefloat_*_scalarinstead of 1-element tensors.RecvError. Newsrc/panic.rsrecords the first panic on any thread; model loading is now guarded too:RuntimeError: failed to load model on backend 'wgpu': No Discrete GPU device found, adapters [], ...Testing
BURN_BACKEND=wgpu|flexselects the backend fortest_ops.py/test_resnet.py. Locally (Metal): 49/49 op checks and 16/16 ResNet checks pass on both backends;test_bridge.py,compare_ort.pypass.ValueError.🤖 Generated with Claude Code