hs-embed-1 — LiteRT

These LiteRT files run hindsight-ai/hs-embed-1, a multilingual text embedder, on Android phones. On a Snapdragon Galaxy S26, one text of up to 64 tokens takes 2.2 ms on the NPU, 2.7 ms on the GPU at FP16 and 3.5 ms on the CPU — speed still being tuned (updates follow). On that phone and a Pixel 11 Pro, every backend we ran stayed within 0.002 of the publisher's float32 model on JSTS, STS17 and JSQuAD (the NPU on the subset given under Accuracy).

Run it

Each file reads a fixed number of tokens, its window: L64 takes up to 64 tokens, counting the start and end tokens the tokenizer adds. Pick the smallest window that holds your text. Use the _fp16_int8emb files on the CPU or GPU and the _fp16 files on the NPU.

To try it on a computer, put hs_embed_1_L64_fp16_int8emb.tflite and tokenizer.json from this repository in one folder, save the code below there as usage.py, and run:

pip install ai-edge-litert==2.3.0 tokenizers==0.23.3 numpy==2.5.4
python usage.py
import numpy as np
from ai_edge_litert.compiled_model import CompiledModel, CpuOptions, HardwareAccelerator, Options
from tokenizers import Tokenizer

MODEL_FILE = "hs_embed_1_L64_fp16_int8emb.tflite"
TOKENIZER_FILE = "tokenizer.json"
SIGNATURE = "serving_default"

tokenizer = Tokenizer.from_file(TOKENIZER_FILE)
tokenizer.no_truncation()
options = Options(hardware_accelerators=HardwareAccelerator.CPU, cpu_options=CpuOptions(num_threads=4))
model = CompiledModel.from_file(MODEL_FILE, options=options)
window = int(model.get_input_tensor_details(SIGNATURE)["input_ids"]["shape"][1])
inputs = {name: model.create_input_buffer_by_name(SIGNATURE, name) for name in ("input_ids", "attention_mask")}
outputs = {"embedding": model.create_output_buffer_by_name(SIGNATURE, "embedding")}


def encode_inputs(text):
    ids = tokenizer.encode(text, add_special_tokens=True).ids
    if len(ids) > window:
        advice = "Split the text into shorter inputs."
        if window < 512:
            next_file = MODEL_FILE.replace(f"_L{window}_", f"_L{2 * window}_")
            advice = f"Use {next_file}."
        raise ValueError(f"{len(ids)} tokens exceed the {window}-token window. {advice}")
    tokens = np.zeros((1, window), dtype=np.int32)
    mask = np.zeros((1, window), dtype=np.float32)
    tokens[0, :len(ids)] = ids
    mask[0, :len(ids)] = 1.0
    return tokens, mask


def embed(text):
    ids, mask = encode_inputs(text)
    inputs["input_ids"].write(ids)
    inputs["attention_mask"].write(mask)
    model.run_by_name(SIGNATURE, inputs, outputs)
    return outputs["embedding"].read(384, np.float32)


if __name__ == "__main__":
    query = embed("Where did Maya move last spring?")
    for label, text in (
        ("Lisbon", "Maya moved to Lisbon in April."),
        ("Porto", "Maya's brother lives in Porto."),
    ):
        document = embed(text)
        cosine = np.dot(query, document) / (np.linalg.norm(query) * np.linalg.norm(document))
        print(f"{label}: {cosine:.6f}")

It prints:

Lisbon: 0.680422
Porto: 0.478124

The example is the one on the publisher's card. The publisher's float32 model gives 0.680318 and 0.478295 for the same query and documents. With MODEL_FILE = "hs_embed_1_L64_fp16.tflite" the code prints 0.680330 and 0.478252. Tested with ai-edge-litert 2.3.0, tokenizers 0.23.3 and numpy 2.5.4 on Python 3.13, on an arm64 Mac.

When it does not work

  • A text longer than the window stops the code with a message. Below L512 it names the file with twice the window: ValueError: 83 tokens exceed the 64-token window. Use hs_embed_1_L128_fp16_int8emb.tflite. At L512 it says Split the text into shorter inputs.
  • These files stop at 512 tokens, while the publisher's model reads up to 8,192. Split longer texts.
  • The first load of a file on the NPU compiles the graph on the phone: 1.3–3.5 s per file in our runs, each started with an empty compile cache, not part of the times below. The Snapdragon Galaxy S26's NPU is the only NPU measured.
  • Of the two phones, only the Snapdragon Galaxy S26 ran the _fp16 files, on its NPU and CPU.
  • On the Mac we tested, LiteRT wrote INFO and WARNING messages to stderr, such as NPU accelerator could not be loaded, and the code still ran on the CPU.

Files

file window, tokens for size
hs_embed_1_L64_fp16_int8emb.tflite 64 CPU, GPU 128.3 MB
hs_embed_1_L128_fp16_int8emb.tflite 128 CPU, GPU 128.6 MB
hs_embed_1_L256_fp16_int8emb.tflite 256 CPU, GPU 129.2 MB
hs_embed_1_L512_fp16_int8emb.tflite 512 CPU, GPU 131.7 MB
hs_embed_1_L64_fp16.tflite 64 NPU 223.6 MB
hs_embed_1_L128_fp16.tflite 128 NPU 223.8 MB
hs_embed_1_L256_fp16.tflite 256 NPU 224.4 MB
hs_embed_1_L512_fp16.tflite 512 NPU 226.9 MB
tokenizer.json — all files 34.4 MB

_fp16_int8emb stores the fully connected weights in float16 and the token table in int8, with one scale per row. _fp16 stores both in float16. tokenizer.json is the publisher's file, byte for byte, and SHA256SUMS lists the sha256 of the nine files above.

Each file takes input_ids as int32 [1, L] and attention_mask as float32 [1, L], with 1 for tokens and 0 for padding. It returns embedding, a float32 [1, 384] vector that is mean-pooled and L2-normalized inside the graph. For 256, 128 or 64 dimensions, keep that many first values and normalize them again, as the publisher's card says.

Accuracy

The checks compare the phones' vectors with the publisher's float32 model, run with sentence-transformers 6.3.0 on a Mac CPU on the same tokens: the two STS17 texts longer than 512 tokens, both Korean, were cut to 512 for both. They use these sets:

  • JSTS: 1,457 Japanese sentence pairs, Spearman correlation at 384, 256, 128 and 64 dimensions.
  • STS17: 4,846 sentence pairs in 9 language pairs, Spearman correlation per language pair.
  • JSQuAD search: 150 Japanese questions over 800 paragraphs, nDCG@10.
  • The example on the publisher's card.

The limits were written down before any file was made and applied unchanged: JSTS within 0.005 at every size, each STS17 language pair within 0.01, JSQuAD nDCG@10 at most 0.01 lower, and Lisbon ranked first in the example. Every phone row below passes all four; the NPU row ran the subset given under the table. Each text ran in the smallest window that holds it.

phone · backend file lowest cosine to the publisher's vector JSTS, largest change STS17, largest change JSQuAD nDCG@10
the publisher's float32 model — — — — 0.9152
Snapdragon Galaxy S26 · NPU _fp16 0.99999 < 0.0001 < 0.0001, en-ar only 0.9152
Snapdragon Galaxy S26 · CPU _fp16 0.999999 < 0.0001 0.0002 0.9152
Snapdragon Galaxy S26 · CPU _fp16_int8emb 0.99971 0.0002 0.0011 0.9152
Snapdragon Galaxy S26 · GPU, FP32 _fp16_int8emb 0.99971 0.0002 0.0011 0.9152
Snapdragon Galaxy S26 · GPU, FP16 with FP32 accumulation _fp16_int8emb 0.99968 0.0002 0.0010 0.9152
Snapdragon Galaxy S26 · GPU, FP16 _fp16_int8emb 0.99930 0.0003 0.0017 0.9160
Pixel 11 Pro · CPU _fp16_int8emb 0.99971 0.0002 0.0011 0.9152
Pixel 11 Pro · GPU, FP32 _fp16_int8emb 0.99971 0.0002 0.0011 0.9152
Pixel 11 Pro · GPU, FP16 with FP32 accumulation _fp16_int8emb 0.99975 0.0002 0.0007 0.9152
Pixel 11 Pro · GPU, FP16 _fp16_int8emb 0.99968 0.0002 0.0013 0.9152

The NPU row ran 3,461 texts: every JSTS text, STS17 en-ar, the card example and the 150 JSQuAD questions, searched against paragraph vectors from the publisher's model. They all fit L64 and ran there in four runs of up to 1,000 texts.

Speed on a Snapdragon Galaxy S26

backend file L64 L128 L256 L512 peak memory
NPU _fp16 2.24 ms 2.97 ms 5.24 ms 7.29 ms 1,207 MiB
GPU, FP16 _fp16_int8emb 2.65 ms 3.17 ms 4.60 ms 9.55 ms 734 MiB
GPU, FP16 with FP32 accumulation _fp16_int8emb 2.75 ms 3.81 ms 6.23 ms 12.57 ms 734 MiB
GPU, FP32 _fp16_int8emb 3.73 ms 4.74 ms 8.11 ms 19.04 ms 732 MiB
CPU, 4 threads _fp16_int8emb 3.49 ms 6.13 ms 11.59 ms 27.76 ms 355 MiB
CPU, 4 threads _fp16 3.44 ms 6.16 ms 11.74 ms 27.72 ms 1,008 MiB

The phone is a Galaxy S26 (Snapdragon, SM8850) on Android API level 36. A time is one call: write the inputs, run, and read the output. Each time is the median of 20 calls after 5 warm-up calls, and each series started on a rested phone: thermal status 0, no cap on the CPU clocks and no thermal limit on the GPU clock. The CPU and GPU calls used an input that fills the window, and the NPU calls used one fixed test input.

A file computes its whole window, so a short text costs about as much as a full one. With _fp16_int8emb on the CPU at L512, a 32-token text took 26.0 ms and a 512-token text 27.8 ms. On the GPU the median run call alone took 0.07–0.50 ms, so time GPU calls with the output read included.

Peak memory is the highest resident memory, VmHWM, of the process that ran the accuracy checks, over the four windows. It was read every 2 s, and the NPU runs lasted 3–5 s, so the NPU figure may be below the real peak.

The phone results in Accuracy and Speed come from LiteRT 2.2.0. The CPU and GPU ran in CompiledModel in an Android app; on the Galaxy S26 the GPU ran through OpenCL with every node of the graph on the GPU. The GPU's FP32, FP16 with FP32 accumulation and FP16 are its CompiledModel.GpuOptions.Precision values FP32, FP16_WITH_FP32_ACCUM and FP16. The NPU ran through LiteRT's C API with the Qualcomm compiler plugin, which put every op of the graph on the NPU; its timings used htp_performance_mode = 2. The Pixel 11 Pro, on Android API level 37 with a PowerVR C-Series GPU, was checked for accuracy only.

How the files were made

  • The source is hindsight-ai/hs-embed-1 at its fp32 revision, stored in float32. Cast to float16, its 44 tensors equal the FP16 weights on the publisher's main branch at 241c31f.
  • The model is a 7-layer ModernBERT encoder of width 384 with a 256,000-token vocabulary. Its vector is the mean of the token outputs.
  • litert-torch 0.10.0 exported the graphs from PyTorch 2.14.1 and Transformers 5.22.1, with the pooling and the normalization inside the graph. Each window's two files differ in how the token table is stored. AI Edge Quantizer 0.10.1 converted the weights to float16 and int8.
  • Two LayerNorms, the last layer's MLP norm and the final norm, run on their input scaled down by a power of two, chosen from the largest values over the texts of these checks, to keep float16 arithmetic in range. In the float32 PyTorch graph this changes no output: the largest difference over those texts is 0.0.

License

MIT, the license of the publisher's model.

Downloads last month
44
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for litert-community/hs-embed-1