Transformers.js v4: Now Available on NPM!
Transformers.js v4 ships on NPM with a new C++ WebGPU runtime built with the ONNX Runtime team, enabling WebGPU-accelerated models in browsers as well as Node, Bun and Deno. Models are re-exported operation by operation using ONNX Runtime contrib operators such as GroupQueryAttention, MatMulNBits and MultiHeadAttention, giving about a 4x speedup for BERT embedding models. The release also adds Mamba, MLA and MoE architectures such as GPT-OSS 20B, which runs at about 60 tokens/s on an M4 Pro Max. Other changes include a move from Webpack to esbuild (10x faster builds, 53% smaller default bundle), a pnpm monorepo, a ModelRegistry API for inspecting and caching pipeline assets, and a standalone 8.8kB tokenizers library.
Related posts
- Transformers.js v3: WebGPU Support, New Models & Tasks, and More…
Hugging Face mediumAdeptTransformers.js v3 adds WebGPU acceleration via ONNX Runtime Web, claimed up to 100x faster than WASM and enabled by passing `device: 'webgpu'` when loading a pipeline. A new `dtype` option replaces the old boolean `quantized` flag and supports fp32, fp16, 8-bit and 4-bit variants, including per-module dtypes for quantization-sensitive encoder-decoder models like Whisper and Florence-2. The release also expands support to 120 architectures and over 1200 pre-converted Hub models, and adds Node.js, Deno and Bun compatibility under the new @huggingface/transformers npm package. WebGPU reaches only about 70% of browsers, so some users need feature flags.
- Introducing @huggingface/kernels: 200+ WebGPU Kernels for Local AI
Hugging Face mediumAdeptHugging Face released @huggingface/kernels, a JavaScript loader for 207 Apache-2.0 WebGPU kernels published as versioned Hub repositories. Each repository bundles a manifest contract, correctness and benchmark cases, and Jinja-templated WGSL shaders with multiple variants per shape and device. On an Apple M4, 809 comparable cases ran 2.57x faster by geometric mean (1.90x median) than ONNX Runtime Web's WebGPU backend, with some outliers in Einsum and CumSum exceeding 300x. Those figures measure GPU time per operation, not full models. Fleet, an in-browser benchmarking tool, crowdsources correctness and performance results across real devices to improve variant selection.
- Tricks from OpenAI gpt-oss YOU 🫵 can use with transformers
Hugging Face hardMasterHugging Face upgraded transformers to support OpenAI's gpt-oss models, and most of the new features carry over to other models. These include pre-built kernels downloaded from the Hub (Liger RMSNorm, MegaBlocks MoE, Flash Attention 3 with attention sinks), native MXFP4 4-bit quantization via Triton kernels, tensor and expert parallelism, dynamic sliding window caching, continuous batching with paged attention, and faster loading. MXFP4 stores E2M1 values in 32-element blocks with shared scales. That lets gpt-oss-20B run in about 16 GB of VRAM and 120B in about 80 GB, versus roughly 4× the memory in the bf16 fallback. Benchmarks show the custom and MXFP4 kernels pay off mainly at larger batch sizes.
- Convert Transformers to ONNX with Hugging Face Optimum
Hugging Face mediumAdeptHugging Face Transformers models can be exported to ONNX in three ways, shown here on a DistilBERT text-classification checkpoint. The low-level torch.onnx.export requires manually specifying input/output names and dynamic axes. transformers.onnx derives these from per-architecture config objects. Optimum's ORTModelForXxx classes convert in one call with from_transformers=True and return a model usable directly in pipelines. Once a model is in ONNX it can go on to ONNX Runtime acceleration, static quantization (~3x latency gains claimed) or TensorRT conversion.
- Accelerating Hugging Face Transformers with AWS Inferentia2
Hugging Face easyNoviceHugging Face benchmarked popular Transformer models on AWS Inferentia2 (inf2.xlarge) against Inferentia1 (inf1.2xlarge) and an NVIDIA A10G GPU (g5.2xlarge), running 144 p95-latency experiments at batch size 1 across six architectures and sequence lengths from 8 to 512. On average Inferentia2 delivered 4.5x lower latency than the A10G and 4x lower than Inferentia1, with BERT-base about 6x faster up to sequence length 256 and ViT-base about 2x faster than the GPU. The GPU runs were unoptimized fp32. Models compile for Inferentia2 through Optimum Neuron and the AWS Neuron SDK, and instances start at $0.76/hour.