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:
- Dual-Engine Design: To prevent the main UI thread from freezing during heavy computation, WebLLM splits its logic. A lightweight
ServiceWorkerMLCEnginemanages the API on the main thread, while the coreMLCEngineperforms inference within a Web Worker (background thread). - 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.
- 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.
- 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.
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.
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