WgPy: GPU-accelerated NumPy-like array library for web browsers

Masatoshi Hidaka, Tatsuya HaradaarXiv 2025 · DOI

Introduction

Scientific computing and deep learning have traditionally been restricted to server-side environments or high-end workstations. However, there is an increasing demand for client-side computation to ensure data privacy—such as processing medical records locally—and to enable distributed "volunteer" computing. While the Pyodide project has successfully brought the Python ecosystem to the browser via WebAssembly, it remains constrained by single-threaded CPU execution, rendering it insufficient for modern AI workloads.

WgPy addresses this gap by providing a GPU-accelerated, NumPy-compatible array library designed specifically for the web. By bridging the synchronous nature of Python with the asynchronous nature of browser GPU APIs (WebGL and WebGPU), WgPy enables scientific workloads to run at up to 95x the speed of standard CPU-based browser Python.

System Architecture

The primary challenge in developing WgPy was the architectural mismatch between Python and the browser. Web browsers execute heavy logic in WebWorkers to keep the UI responsive, while modern GPU APIs like WebGPU are inherently asynchronous. Conversely, NumPy and its GPU counterpart, CuPy, expect synchronous execution.

To resolve this, WgPy employs a multi-threaded architecture:

  1. Thread Proxying: The Python interpreter (Pyodide) resides within a WebWorker. When a GPU operation is invoked, it communicates with a "Buffer/Kernel manager" located on the Main Thread.
  2. The Sync-Async Bridge: To maintain a synchronous interface for Python, WgPy utilizes SharedArrayBuffer and the Atomics API. The worker thread is intentionally blocked using Atomics.wait until the Main Thread signals via Atomics.notify that the asynchronous GPU data transfer is complete.
  3. Custom Kernels: To minimize overhead, WgPy supports "ElementwiseKernels," allowing users to write shader code (GLSL or WGSL) to fuse multiple operations into a single GPU pass.
The architecture of WgPy showing the interaction between the Python environment in a WebWorker and the GPU interfaces on the Main Thread.

Performance Evaluation

The authors benchmarked WgPy across several tasks, including Mandelbrot set generation, matrix multiplication (GEMM), and deep learning training (ResNet-18 on CIFAR-100).

Matrix Multiplication

On a Windows system equipped with an NVIDIA RTX 4070, the WebGPU backend achieved a staggering 340x speedup over the CPU for $1024 \times 1024$ matrices. As shown in the performance data, WgPy's WebGPU implementation significantly outperforms both WebGL and CPU-based execution in the browser, approaching the performance of native NumPy in some configurations.

Performance comparison of matrix multiplication (GFLOPS) across different backends in Chrome.

Deep Learning and Cross-Platform Results

Training a ResNet-18 model on CIFAR-100 was 95x faster using WebGPU compared to standard CPU execution. WgPy also demonstrated robust cross-platform performance; on a MacBook Air (M1), WebGPU was approximately 30x faster than the CPU. The library's ability to fall back to WebGL ensures compatibility with devices where WebGPU is not yet available, such as iOS and older Android devices.

Key Insights and Limitations

A critical finding was the impact of overhead. For simple operations, the cost of GPU communication can negate performance gains. The use of custom kernels proved essential; in the Mandelbrot benchmark, standard array operations showed negligible improvement, whereas a custom kernel was 170x faster.

Despite these advancements, WgPy faces several limitations:

  • API Coverage: WgPy currently implements only a subset of the vast NumPy API, focusing on operations critical for deep learning.
  • Numerical Precision: Browser shader languages lack native support for 64-bit floating-point numbers (float64), limiting its use in certain high-precision scientific simulations.
  • Kernel Authoring: Custom kernels currently require manual translation into GLSL or WGSL. Future iterations may incorporate Just-In-Time (JIT) compilation to generate these kernels automatically from Python code.

Conclusion

WgPy represents a significant step toward high-performance scientific computing in the browser. By providing a familiar NumPy-like interface backed by GPU acceleration, it opens new possibilities for privacy-preserving AI and accessible distributed computing.

Summary updated

BibTeX
@misc{hidaka2025wgpy,
  author = {Masatoshi Hidaka and Tatsuya Harada},
  title = {{WgPy: GPU-accelerated NumPy-like array library for web browsers}},
  year = {2025},
  eprint = {2503.00279},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.00279},
}
Figure