Skip to content
How it works

How it works

github.com • November 23, 2026

How gpu-shift verifies a WebGPU/WGSL port, chooses the CPU/GPU crossover threshold, guards against VRAM leaks, and switches ports safely. Back to the README .

The original CPU function stays the source of truth. stage , capture , diff , bench , and check never modify your original Python file.

Runtime Safety Guards

Software CPU emulator rejection ( auto mode): On GPU-less VMs, Docker containers, or CI runners, WebGPU exposes a software CPU emulator ( Microsoft Basic Render Driver , llvmpipe , SwiftShader , adapter_type="CPU" ). diff and check allow it for correctness testing, while auto runtime automatically falls back to native CPU so code never slows down on an emulated GPU.

Single-call hardware limit fallback ( GpuLimitError ): Before allocating VRAM, runtime.py checks max-compute-workgroups-per-dimension ( 65,535 ), max-storage-buffer-binding-size , and max-uniform-buffer-binding-size . If one huge call exceeds hardware limits, only that call falls back to CPU; subsequent normal calls stay on GPU.

Zero VRAM leaks + fast batch sessions: Default ComputeKernel.dispatch() fuses compute + staging copy into a single queue.submit() , reads via read_mapped(copy=False) , and destroys every buffer in finally ( 0 B resident VRAM). For tight multi-frame loops, with kernel.session() as sess: reuses buffers and BindGroup across calls and destroys 100% of VRAM on __exit__ .

Thread and fork() safety: ComputeKernel and ComputeSession use internal locks; the device cache is keyed by (pid, power_preference) with os.register_at_fork cleanup on Unix.

Stale capture protection: manifest.json hashes the clean original source (stripping switch blocks). Editing the original Python function makes diff and check fail until you re-run capture .

Runtime Modes ( GPU_SHIFT_MODE )

Coding Agent Setup ( gpu-shift setup )

Existing config files are backed up as *.gpu-shift.bak . Undo cleanly at any time with gpu-shift setup --remove .

Self-contained examples under examples/ comparing against vectorized NumPy on an RTX 4060 Laptop GPU:

Extracted Entities

Platforms (2)

Tools (1)