WebLLM: A High-Performance In-Browser LLM Inference Engine

Charlie F. Ruan, Yucheng Qin, Xun Zhou, Ruihang Lai, Hongyi Jin, Yixin Dong, Bohan Hou, Meng-Shiun Yu, Yiyan Zhai, Sudeep Agarwal, Hangrui Cao, Siyuan Feng, Tianqi Chen · DOI

Abstract

WebLLM is an open-source JavaScript framework designed to bring high-performance Large Language Model (LLM) inference directly to the web browser. By leveraging WebGPU for hardware acceleration and machine learning compilers to generate optimized kernels, WebLLM achieves up to 80% of native "bare-metal" performance. This architecture enables private, local, and cost-effective AI agents to operate within a standard browser environment, bypassing the need for expensive cloud-based GPU infrastructure.

Introduction

While LLMs have become transformative tools, their deployment typically relies on cloud-based servers equipped with high-end GPUs, raising concerns regarding cost, latency, and data privacy. Moving these models to on-device execution (e.g., laptops or mobile devices) addresses these issues. However, the web browser—the most universal software platform—has historically struggled to support high-performance AI due to the lack of optimized, vendor-specific libraries like CUDA or Metal.

WebLLM bridges this gap by utilizing WebGPU, a modern standard for browser-based graphics hardware access. Since WebGPU lacks pre-built kernels for complex LLM operations such as PagedAttention, WebLLM employs a compiler-first approach to generate high-performance code, making the browser a viable platform for local AI.

System Architecture and Methods

WebLLM's architecture is designed to maximize computational efficiency while ensuring a responsive user interface. The system employs a multi-layered approach:

  1. Dual-Engine Design: To prevent the main UI thread from freezing during heavy computation, WebLLM splits its logic. A lightweight ServiceWorkerMLCEngine manages the API on the main thread, while the core MLCEngine performs inference within a Web Worker (background thread).
  2. Compiler-Driven Optimization: Instead of manual kernel development, the framework utilizes MLC-LLM and Apache TVM. These tools compile model implementations from Python into optimized WebGPU kernels and WebAssembly (WASM) modules, enabling advanced optimizations like kernel fusion and GEMM tiling.
  3. Hybrid Execution Model: The system partitions tasks between the GPU and CPU. WebGPU handles massive parallel operations (e.g., matrix multiplication), while WASM manages complex CPU-bound logic, such as KV cache management and structured output grammars.
  4. Developer-Friendly API: WebLLM provides an OpenAI-compatible API, allowing developers to transition from cloud-based models to local browser-based models with minimal code modifications.
WebLLM Architecture: A dual-engine design leveraging WebGPU and WebAssembly for efficient in-browser inference.

Evaluation

The performance of WebLLM was evaluated using popular open-source models, including Llama-3.1-8B and Phi-3.5-mini (3.8B). The primary goal was to measure the efficiency of the browser-based implementation compared to native execution.

Key Results

Testing was conducted on an Apple MacBook Pro M3 Max. The results demonstrate that WebLLM retains a significant portion of native performance:

  • Performance Retention: WebLLM achieved between 71.2% and 79.6% of the throughput seen in native C++/Python MLC-LLM applications.
  • Inference Speed: The Phi-3.5-mini model reached 71.1 tokens per second, while the larger Llama-3.1-8B model achieved 41.1 tokens per second.
  • Practical Utility: Given that average human reading speeds are approximately 5-10 tokens per second, these results indicate that even 8B parameter models can run in a browser at speeds far exceeding real-time requirements.
Performance comparison of WebLLM against native MLC-LLM on an Apple M3 Max.

Discussion and Future Work

WebLLM demonstrates that the browser is a natural environment for AI agents, as it is the primary workspace for tasks like email, document creation, and scheduling. By using a compiler-driven approach, WebLLM abstracts hardware specifics, allowing the same code to run efficiently across different GPU architectures (e.g., Apple M-series, NVIDIA RTX).

Despite these advancements, a ~20% performance gap remains compared to native execution. Future work will focus on:

  • WebGPU Enhancements: Utilizing new features like "shuffling" to optimize data movement within the GPU.
  • Overhead Reduction: Minimizing the communication overhead between JavaScript and WASM.
  • Memory Management: Improving efficiency for running large models on consumer devices with limited system memory.

WebLLM is an open-source project available at https://github.com/mlc-ai/web-llm.

Summary updated

BibTeX
@misc{ruan2024webllm,
  author = {Charlie F. Ruan and Yucheng Qin and Xun Zhou and Ruihang Lai and Hongyi Jin and Yixin Dong and Bohan Hou and Meng-Shiun Yu and Yiyan Zhai and Sudeep Agarwal and Hangrui Cao and Siyuan Feng and Tianqi Chen},
  title = {{WebLLM: A High-Performance In-Browser LLM Inference Engine}},
  year = {2024},
  eprint = {2412.15803},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2412.15803},
}
Figure