sentence-similarity · feature-extraction · text · WASM q8 · 150.34 MB

ModernBERT Embed — long-context, Matryoshka embeddings in your browser

Nomic's modernbert-embed-base fine-tunes the 2024 ModernBERT-base encoder on the Nomic Embed retrieval data. It reads up to 8,192 tokens (16× BERT's 512), emits a 768-d vector you can slice down to 256-d or smaller (Matryoshka), and here it runs entirely on your device in a Web Worker — no server, no API key.

At a glance

Model (upstream & ONNX) nomic-ai/modernbert-embed-base — the same repo ships the ONNX export (onnx/model_quantized.onnx)
Task / pipeline sentence-similarity via Transformers.js feature-extraction (pooling: "mean", normalize: true)
Architecture ModernBertModel · 149M params · 22 layers · 12 heads · hidden 768 · RoPE · global attention every 3rd layer, 128-token local window elsewhere · 50,368-token BPE vocab
Required prefixes Queries: "search_query: " · Documents: "search_document: " (the card says the model requires them)
Context Trained for 8,192 tokens. In-browser WASM we measured 512 tok → 2.0 s, 2,048 → 12.8 s, 4,096 → 38.0 s per forward pass (single thread); 8,192 aborted with ONNX Runtime error 314398768, so pages here cap at 4,096 (default 2,048) and report real token counts.
Matryoshka (MRL) 768-d output; card reports 768 and 256 (61.17 vs 62.62 MTEB avg). This page also measures 64 / 128 / 512 live — slice, then re-normalise.
Quantization & size q8 ONNX · model_quantized.onnx = 150,336,419 B (150.34 MB) + tokenizer
Backend CPU WebAssembly (wasm) via shared lib/webai.js (Transformers.js 3.7.5) — no WebGPU required
License Apache-2.0

Run it — long-context search & Matryoshka explorer

Rank multi-paragraph documents against your question. Documents are separated by a --- line and embedded whole (up to 2,048 tokens each). Flip the prefixes off to see why the card insists on them, and drag the Matryoshka dimension down to watch storage shrink while the ranking holds.

Initialise ModernBERT Embed above to embed the query and documents on your device.

Matryoshka dimension (re-ranks instantly — no re-embedding)

768d · 3,072 B per float32 vector

Top-1 cosine–
Relevant − distractor–
Prefix effect–
Top-k vs 768d–
Latency–
Tokens embedded–

    See inside — the 768-d query vector

    The ONNX graph returns token_embeddings of shape [1, T, 768]; the worker averages the non-padding rows (mean pooling) and L2-normalises, exactly like the pipeline's pooling: "mean", normalize: true. Dashed guides mark the 64 / 128 / 256 / 512 Matryoshka cut points — the leading dimensions carry the most signal.

    Tensor & runtime telemetry

    Pre-norm ‖v‖–
    Post-norm ‖v‖–
    Min / Max–
    NaN count–
    Query tokens–
    Backend–

    Strongest query dimensions

    Top 6 coordinates by magnitude in the live query vector.

    Pipeline parity check

    Runs the stock pipe(text, { pooling: "mean", normalize: true }) next to the worker's manual pooling on the current query and reports the largest per-dimension difference.

    not run

    Why it matters

    Whole documents, not just their first page. A 512-token BERT embedder silently ignores everything after roughly the first 380 words. ModernBERT Embed reads 4× that by default here (and the model itself 16×), so a policy's revised final section, a report's conclusion or a contract's termination clause actually shape the vector you search with.

    Use-case ladder

    How the API works

    Load nomic-ai/modernbert-embed-base as a feature-extraction pipeline on wasm with dtype: "q8". Prefix every query and every document, then mean-pool and normalise. For Matryoshka, slice the vector and re-normalise:

    import { pipeline } from "@huggingface/transformers";
    
    const extractor = await pipeline("feature-extraction", "nomic-ai/modernbert-embed-base", {
      device: "wasm",
      dtype: "q8",
    });
    
    const q = await extractor(["search_query: What is TSNE?"], { pooling: "mean", normalize: true });
    const d = await extractor(
      ["search_document: TSNE is a dimensionality reduction algorithm created by Laurens van Der Maaten"],
      { pooling: "mean", normalize: true },
    );
    // q.dims => [1, 768]; cosine = dot product of unit vectors.
    // Matryoshka 256-d: keep q.data.slice(0, 256), then divide by its L2 norm.

    Measured here with the q8 export: the card's example pair scores 0.6481 (the card's fp32 value is 0.7214) — quantisation shifts absolute cosines, while rankings stay stable.

    Show the code — one long-context forward pass + masked mean pool (worker.js) embedOne()
    Show the code — Matryoshka truncation (modernbert-core.mjs) truncateMrl()

    References