skip to content

In Transformers.js v3 and later, how does the `dtype` option of `pipeline()` decide which ONNX weight file the browser downloads?

level: middleimportance: should knowfreq 7%

answer

  1. a file selector, not a converter
  2. a suffix on the onnx file name
  3. q8 maps to _quantized
  4. replaced the v2 quantized boolean
  5. probe with get_available_dtypes

basics

~20 s

dtype picks which prebuilt ONNX file is fetched from the repo's onnx folder: fp32 loads model.onnx, fp16 model_fp16.onnx, q8 model_quantized.onnx, q4 model_q4.onnx. Nothing is quantized in the browser, so a variant the repo lacks fails to load.

solid answer

~40 s

Since Transformers.js v3, `dtype` has replaced the old `quantized` boolean, and it works as a file selector. The library appends a suffix to the model's base file name inside `onnx/`: `fp32` loads `model.onnx`, `fp16` loads `model_fp16.onnx`, `q8` loads `model_quantized.onnx`, `int8` and `uint8` load `model_int8.onnx` and `model_uint8.onnx`, `q4` loads `model_q4.onnx`, `q4f16` loads `model_q4f16.onnx` and `bnb4` loads `model_bnb4.onnx`. So the choice is mainly a download-size choice for the first visit. If the repo does not ship that variant, the request 404s and loading fails with a `Could not locate file` error, so probe with `ModelRegistry.get_available_dtypes(model)` first. Omitting `dtype` falls back to a per-device default, and encoder-decoder models accept an object with one dtype per module.

code

javascript · 9 lines
javascript
import { pipeline, ModelRegistry } from '@huggingface/transformers';

const model = 'onnx-community/all-MiniLM-L6-v2-ONNX';
const available = await ModelRegistry.get_available_dtypes(model);

// Smallest variant the repo actually ships, else full precision.
const dtype = ['q4', 'q8', 'fp16'].find((d) => available.includes(d)) ?? 'fp32';

const extractor = await pipeline('feature-extraction', model, { dtype });

go deeper

for a junior

Remember that dtype picks a prebuilt weight file, and that smaller variants such as q8 or q4 mean a smaller first download.

for a middle

Explain the suffix mapping, especially q8 to model_quantized.onnx, why a missing variant fails with Could not locate file, and how get_available_dtypes avoids guessing.

for a senior

Catch the silent failures: a leftover quantized option or a misspelled dtype quietly loads another file. Verify the fetched file names, and use per-module dtypes for sensitive encoders.

for a principal

Treat the variant as a product decision: first-visit size against accuracy on your task, measured on your data, and decide whether to offer users a choice of variants.

## `dtype` selects a file; it does not convert one In Transformers.js a model repository on the Hugging Face Hub keeps its ONNX graphs in an `onnx/` folder, often in several **variants**: the full-precision export plus smaller copies that were quantized ahead of time. The `dtype` option of `pipeline()` (and of `from_pretrained()`) does not quantize anything in the browser. It chooses **which of those prebuilt files to download**. That makes it the biggest single lever on how long a first visit takes. Before Transformers.js v3 the option was a boolean, `quantized`: `true` meant the 8-bit file and `false` the full-precision one. v3 replaced it with `dtype` and a much longer list of names. In 4.3 the `pipeline()` signature no longer reads `quantized` at all. ## The name-to-file mapping The library appends a suffix to the base file name (normally `model`) and loads it from `onnx/`: | `dtype` | File loaded | What it is | |---|---|---| | `'fp32'` | `onnx/model.onnx` | full precision | | `'fp16'` | `onnx/model_fp16.onnx` | half precision | | `'q8'` | `onnx/model_quantized.onnx` | 8-bit quantized | | `'int8'` / `'uint8'` | `onnx/model_int8.onnx` / `onnx/model_uint8.onnx` | 8-bit, signed or unsigned | | `'q4'` | `onnx/model_q4.onnx` | 4-bit weights | | `'q4f16'` | `onnx/model_q4f16.onnx` | fp16 model with 4-bit block weights | | `'bnb4'` | `onnx/model_bnb4.onnx` | another 4-bit format | Two details matter in practice. First, `q8` maps to `_quantized`, not `_q8`, a leftover from the boolean era. Second, large models may also ship a companion `.onnx_data` file next to the graph, and it downloads too. ## Choosing by download size - **Bits per weight drive the size.** Half precision is roughly half of fp32; an 8-bit file is roughly a quarter; 4-bit variants are smaller again. Read the actual file sizes on the repo rather than computing them, because not every tensor in a graph is necessarily stored at the reduced width. - **The quality cost of fewer bits is a separate question.** What a 4-bit weight costs a given task is covered by the quantization material; here the decision is which file the page fetches. - **Probe before you choose.** `ModelRegistry.get_available_dtypes('onnx-community/all-MiniLM-L6-v2-ONNX')` returns the variants that actually exist, for example `['fp32', 'fp16', 'int8', 'uint8', 'q8', 'q4']`, so a UI can offer only real options or pick the smallest. - **Leaving `dtype` out is also a choice.** A default chosen per execution device applies; which default goes with which device belongs to the backend settings. ## Seeing what you actually fetched - `ModelRegistry.get_pipeline_files(task, model, { dtype })` lists the files a pipeline will request, for example `config.json`, `onnx/model_q4.onnx` and the tokenizer files, without loading anything. - The `file` field of each `progress_callback` event names the file being read, which makes a wrong variant visible in the console. - `dtype: 'auto'` uses a dtype that the model's own configuration declares, when it declares one, and otherwise the device default. ## When the variant does not exist A `dtype` the repository does not ship turns into a request for a file that is not there. The Hub answers 404 and Transformers.js throws a `ModelFileNotFoundError` whose message starts with `Could not locate file`. Nothing falls back to another variant for you. A **misspelled** name behaves differently: the library accepts only the names in its own list, and an unknown string silently falls back to the device default. A typo therefore downloads a different file without any error. ## Per-module dtypes for multi-session models Encoder-decoder and vision-language models are split into several ONNX sessions, and some are far more sensitive to quantization than others. `dtype` can therefore be an object keyed by module name: ```javascript import { Florence2ForConditionalGeneration } from '@huggingface/transformers'; const model = await Florence2ForConditionalGeneration.from_pretrained( 'onnx-community/Florence-2-base-ft', { dtype: { embed_tokens: 'fp16', vision_encoder: 'fp16', encoder_model: 'q4', decoder_model_merged: 'q4', }, }, ); ``` A module missing from the object gets the device default. ## Migrating code written for v2 1. Find every `quantized:` option; left in place, Transformers.js 4 silently ignores it. 2. Replace `quantized: false` with `dtype: 'fp32'`, and `quantized: true` with `dtype: 'q8'`. 3. Check the repo with `ModelRegistry.get_available_dtypes()` and consider a 4-bit variant if one exists.

  • What happens if Transformers.js 4 code still passes `{ quantized: false }` to `pipeline()`?
    Nothing visible: `pipeline()` no longer reads a `quantized` option, so it is ignored and the default `dtype` for the device applies. Code that meant "full precision" silently gets a different file. Replace it with `dtype: 'fp32'`; `dtype` replaced the boolean in v3.
  • How do you give the encoder and decoder of an encoder-decoder model different dtypes in Transformers.js?
    Pass `dtype` as an object keyed by module name, for example `{ encoder_model: 'fp16', decoder_model_merged: 'q4' }`. Each session loads its own file variant; a module missing from the object uses the device default.

  • dtype: 'q4' quantizes the fp32 weights inside the browser at load time
  • Every ONNX repo on the Hub ships every dtype variant
  • quantized: true still selects the 8-bit file in Transformers.js 4
  • dtype: 'q8' downloads a file named model_q8.onnx
  • A misspelled dtype throws, so typos are caught at load
  • A missing variant falls back to the next smaller file automatically

Assumes Transformers.js 4.3; v3 replaced the quantized boolean with dtype, and 4.3 no longer reads quantized.