diff --git a/README.md b/README.md index b17b7821..26e8a21a 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,8 @@ The core idea is to forward pass a vocabulary through a sentence transformer mod Our official documentation can be found [here](https://minish.ai/packages/model2vec/introduction). This includes in-depth documentation on [inference](https://minish.ai/packages/model2vec/inference), [distillation](https://minish.ai/packages/model2vec/distillation), [training](https://minish.ai/packages/model2vec/training), and [integrations](https://minish.ai/packages/model2vec/integrations). +For details on the Rust backend and which dtypes to use, see [internals](docs/internals.md). + ## Model List diff --git a/docs/internals.md b/docs/internals.md new file mode 100644 index 00000000..6f755cec --- /dev/null +++ b/docs/internals.md @@ -0,0 +1,72 @@ +# Internals + +## Rust backend + +`StaticModel.encode` uses a Rust backend: [model2vec-rs](https://github.com/MinishLab/model2vec-rs). The Python `StaticModel` has embeddings, weights, token mapping and tokenizer as properties. It builds a `model2vec_rs.StaticModel` from them the first time you call `encode`. Tokenization, embedding lookup, pooling and normalization all happen in Rust. + +When the dtype of the embeddings matches the backend's dtype, the Python model and the backend share the same memory: `model.embedding` and `model.weights` become _views_ of the backend's arrays, so the embeddings are only stored once. + +If you change one of the properties through assignment, the Rust backend is invalidated, and reinstantiated on the next encode. + +Here is a demonstration. It uses the private `_get_rust_backend` method to inspect the backend; don't rely on it in your own code. + +```python +from model2vec import StaticModel + +model = StaticModel.from_pretrained("minishlab/blink-8M") +model.encode("hello") +# Now the Rust backend has been instantiated +backend_1 = model._get_rust_backend() +try: + # Raises an error: the embeddings are read-only + model.embedding *= 10 +except ValueError: + print("Error") +model.embedding = model.embedding * 10 +# Rust backend is invalidated +model.encode("hello2") +# Rust backend is again up to date +backend_2 = model._get_rust_backend() +backend_1.embeddings.mean() +# ~-0.09 +backend_2.embeddings.mean() +# ~-0.9 + +``` + +## Supported dtypes + +The backend works with two embedding dtypes: + +- `float32` +- `int8` + +Models in any other dtype still load and encode, but the embeddings are cast to `float32` when the backend is built: + +| Model dtype | Backend dtype | Memory | Output dtype of `encode` | +|-------------|---------------|--------|--------------------------| +| `int8` | `int8` | shared | `float32` | +| `float32` | `float32` | shared | `float32` | +| `float16` | `float32` | original + `float32` copy | `float16` | +| `float64` | `float32` | original + `float32` copy | `float64` | + +As such, for `float16` and `float64` models you pay for an extra `float32` copy of the embedding matrix, the computation happens in `float32` anyway, and the result is cast back to the original dtype afterwards. A `float16` model therefore uses three times the memory of its embeddings on disk, and a `float64` model does not gain any precision. + +## Picking a dtype + +Our own models are not stored in `float16`, but some models published by others are. For those, and for `float64` models, it is better to quantize when loading: + +- **`float16` models**: load them as `int8`. This is smaller than the `float16` original, uses the backend natively, and the loss in quality is usually negligible. +- **`float64` models**: load them as `float32`. The computation is done in `float32` regardless, so this only removes the extra copy. + +```python +from model2vec import StaticModel + +# A float16 model, loaded as int8 +model = StaticModel.from_pretrained("path/to/float16-model", quantize_to="int8") + +# A float64 model, loaded as float32 +model = StaticModel.from_pretrained("path/to/float64-model", quantize_to="float32") +``` + +If you want to stay as close as possible to the original `float16` embeddings, load them as `float32` instead. This also avoids the extra copy, at the cost of twice the memory of the `float16` original. diff --git a/uv.lock b/uv.lock index 691e7caf..2e1bc678 100644 --- a/uv.lock +++ b/uv.lock @@ -22,6 +22,7 @@ exclude-newer = "0001-01-01T00:00:00Z" # This has no effect and is included for exclude-newer-span = "P3D" [options.exclude-newer-package] +model2vec-rs = false skeletoken = false [[package]] @@ -1318,7 +1319,7 @@ requires-dist = [ { name = "huggingface-hub", specifier = ">=1.0.0" }, { name = "ipython", marker = "extra == 'dev'" }, { name = "jinja2" }, - { name = "model2vec-rs", directory = "../model2vec-rs/python" }, + { name = "model2vec-rs", specifier = ">=0.5.1,<0.6.0" }, { name = "mteb", marker = "extra == 'integration'" }, { name = "mypy", marker = "extra == 'dev'" }, { name = "numpy" }, @@ -1347,18 +1348,23 @@ provides-extras = ["dev", "distill", "onnx", "train", "quantization", "integrati [[package]] name = "model2vec-rs" -source = { directory = "../model2vec-rs/python" } +version = "0.5.1" +source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, { name = "numpy", version = "2.4.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, ] - -[package.metadata] -requires-dist = [ - { name = "numpy" }, - { name = "pytest", marker = "extra == 'dev'" }, +sdist = { url = "https://files.pythonhosted.org/packages/f4/90/a1b1320c93e169f052ff579102915806aa589d75d551add9b80a2d7e7212/model2vec_rs-0.5.1.tar.gz", hash = "sha256:db1ab1a79f06f5e6b0dc024704ab3be656f71bac80a79298ef71458384cd06da", size = 153596, upload-time = "2026-11-24T06:28:09.687Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/43/cf/5eeb55291d1d365419a6ff1f8529be7be9408d690a43f28112de17bc2f86/model2vec_rs-0.5.1-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:19cbb19555229d5232cbd246ae11b7c1941def84048bf3871826db73e71cc8c2", size = 3422771, upload-time = "2026-11-24T06:27:56.665Z" }, + { url = "https://files.pythonhosted.org/packages/53/0e/7c63cd068a92dbfa7f41930460a8ff7a5f30ffb60d49458c76e3018fa994/model2vec_rs-0.5.1-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:aa394460f15edd38cee31951d56bbf1d226ada2dea7baaaed373e3e9eb46c25a", size = 3320494, upload-time = "2026-11-24T06:27:58.298Z" }, + { url = "https://files.pythonhosted.org/packages/35/ad/a2e1f6576599918f731e93687607d30fe0af89c336d738bc5f090765ffe6/model2vec_rs-0.5.1-cp310-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:8814f1a9aa24d291b07f4a934b0fa57ddf72376552f1db4e7f61a99923de6cdf", size = 3785520, upload-time = "2026-11-24T06:27:59.659Z" }, + { url = "https://files.pythonhosted.org/packages/0c/77/7634b609591c3f957a09b289acfde2b5f86488d952fc640438dc2db5df0c/model2vec_rs-0.5.1-cp310-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:4e71f701a1d389d5f7e7c021cd940b97bfc466f4bd64d5e0dd76f4ef81e59ab8", size = 3731888, upload-time = "2026-11-24T06:28:01.104Z" }, + { url = "https://files.pythonhosted.org/packages/53/93/e881640e7e94bda8278f1996ecb7e9755105bc2b34f1477d3b0aa68942d9/model2vec_rs-0.5.1-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:e8271caa08343948d3d7459e065e1f2d27dfed664063247057c11604b54058f7", size = 10227143, upload-time = "2026-11-24T06:28:02.824Z" }, + { url = "https://files.pythonhosted.org/packages/81/ae/dc39169e6f3a38aa1063776b3e596e28e3a811c12771e46c37c8b4dc2b86/model2vec_rs-0.5.1-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:92f0c0ee68920847ca51d4b42bd17d2b2637326fbde22947f9b40f50efef1d17", size = 10553805, upload-time = "2026-11-24T06:28:04.999Z" }, + { url = "https://files.pythonhosted.org/packages/35/4c/4f75e995ce8f480dd6f3cbbc1103f452700422931b92f6d35382a04d4835/model2vec_rs-0.5.1-cp310-abi3-win_amd64.whl", hash = "sha256:34273ad17774ed90baa16c04313a805de5be08621966b45f0eca2a1df4b5b794", size = 3142384, upload-time = "2026-11-24T06:28:06.764Z" }, + { url = "https://files.pythonhosted.org/packages/fd/c6/ba5d52cffd746a0ec160f001d0595baecdd7d5a7be9c4862ced809f5d9e8/model2vec_rs-0.5.1-cp310-abi3-win_arm64.whl", hash = "sha256:aa89947cd02ec2f6b0ffe60504b3efd2d77e8e32205ebbae846f5da234c77a09", size = 2992357, upload-time = "2026-11-24T06:28:08.33Z" }, ] -provides-extras = ["dev"] [[package]] name = "mpmath"