Embedding models keep their weights in 32-bit floating point by default, and on a busy inference host those weights compete with everything else for memory. Half-precision inference in Sentence Transformers loads them as float16 or bfloat16 instead, which halves the memory they occupy while the embeddings stay close to the full-precision result.
The setting is a load option rather than a change to the encoding code. A dtype value in model_kwargs reaches Transformers when the model loads, and later encode() calls keep their usual form. The two formats divide their 16 bits differently, so the closer one depends on the model and hardware, and a comparison against float32 on the same device measures the difference instead of assuming it.
Speed is a separate question. The upstream benchmarks report their half-precision speedups on GPUs and suggest float16 there, while a CPU can keep the memory saving and still run slower than float32. Half precision changes the loaded model rather than the size of a vector index, so shrinking stored embeddings remains the separate job of output quantization.
Steps to enable half-precision inference in Sentence Transformers:
- Activate the virtual environment that has Sentence Transformers installed.
$ source .venv/bin/activate
Use Create a Python virtual environment and How to install Sentence Transformers with pip first when the project has neither.
- Create a file named compare_half_precision.py in your text editor, with your own model name and texts in place of the values shown.
- compare_half_precision.py
import argparse import torch from sentence_transformers import SentenceTransformer, util MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2" TOLERANCE = 0.999 TEXTS = [ "Half precision stores each model weight in 16 bits.", "A semantic search index ranks documents by meaning.", "The backup job finished before the maintenance window.", ]
The TOLERANCE value is a starting limit for this comparison, not a library setting. Texts like the ones the application encodes give a more meaningful result than the samples.
- Add the following option at the end of compare_half_precision.py so each run names the half-precision format.
- compare_half_precision.py
parser = argparse.ArgumentParser() parser.add_argument( "--dtype", choices=["float16", "bfloat16"], required=True, ) args = parser.parse_args()
- Add the following float32 baseline at the end of compare_half_precision.py.
- compare_half_precision.py
baseline_model = SentenceTransformer(MODEL_NAME) baseline = baseline_model.encode( TEXTS, convert_to_tensor=True, show_progress_bar=False, )
A model with no dtype value normally loads in float32. convert_to_tensor=True keeps each embedding in the format the model produced, because the default NumPy output converts bfloat16 embeddings to float32.
- Enable half precision with the following load and encoding call at the end of compare_half_precision.py.
- compare_half_precision.py
half_model = SentenceTransformer( MODEL_NAME, model_kwargs={"dtype": args.dtype}, ) half = half_model.encode( TEXTS, convert_to_tensor=True, show_progress_bar=False, )
The model_kwargs argument is the only change an application needs. Neither load sets a device, so both use the same automatic choice, and a device value from How to select a Sentence Transformers inference device belongs on both loads when the comparison needs another device. The older torch_dtype key used in the upstream examples gave the same result in Sentence Transformers 6.2.0.
- Add the following measurements at the end of compare_half_precision.py.
- compare_half_precision.py
def weight_megabytes(model): sizes = [ p.numel() * p.element_size() for p in model.parameters() ] return sum(sizes) / 1e6 finite = torch.isfinite(half).all().item() lowest = util.pairwise_cos_sim(baseline, half).min().item() baseline_size = weight_megabytes(baseline_model) half_size = weight_megabytes(half_model)
util.pairwise_cos_sim compares each half-precision embedding with the float32 embedding of the same text, so one drifting text lowers the reported value.
- Add the following report at the end of compare_half_precision.py.
- compare_half_precision.py
print(f"device: {half_model.device}") print(f"model dtype: {half_model.dtype}") print(f"embeddings: {half.dtype}") print(f"float32 weights: {baseline_size:.1f} MB") print(f"half weights: {half_size:.1f} MB") print(f"all finite: {finite}") print(f"lowest cosine: {lowest:.6f}") print(f"within tolerance: {finite and lowest >= TOLERANCE}")
- Save compare_half_precision.py.
- Verify the bfloat16 embeddings against the float32 baseline.
$ python compare_half_precision.py --dtype bfloat16 ##### snipped ##### device: cpu model dtype: torch.bfloat16 embeddings: torch.bfloat16 float32 weights: 90.9 MB half weights: 45.4 MB all finite: True lowest cosine: 0.999936 within tolerance: True
The first run downloads the model, and the snipped lines hold its progress bars and a Hugging Face notice about unauthenticated requests, which is harmless for a public model. The model dtype line shows the selected format. It stays at torch.float32, with the weights at their full size, when the dtype value did not reach the load. Tested with Sentence Transformers 6.2.0, Transformers 5.22.2 and PyTorch 2.15.0 on an arm64 CPU. CUDA and MPS were not tested.
- Verify the float16 embeddings against the float32 baseline.
$ python compare_half_precision.py --dtype float16 ##### snipped ##### device: cpu model dtype: torch.float16 embeddings: torch.float16 float32 weights: 90.9 MB half weights: 45.4 MB all finite: True lowest cosine: 0.999999 within tolerance: True
within tolerance: False means an embedding is not finite or has moved past the limit, which rules that format out until retrieval quality on your own data has been checked. The format to keep is the one that passes with the closest result on the device the application runs on.