v1.0
Browse filesFiles changed (28)
- .gitattributes+2
- CHANGELOG.mdAdded+32
- CHECKSUMS.sha256Added+26
- LICENSEAdded+202
- NOTICEAdded+85
- README.mdAdded+346
- export/eg2_text.pyAdded+288
- export/export.pyAdded+215
- model/MODEL-ATTRIBUTION.jsonAdded+36
- model/config.jsonAdded+54
- model/embeddinggemma-2-text.aimodel/main.hashAdded+1
- model/embeddinggemma-2-text.aimodel/main.mlirbAdded+3
- model/embeddinggemma-2-text.aimodel/metadata.jsonAdded+8
- model/tokenizer/tokenizer.jsonAdded+3
- model/tokenizer/tokenizer_config.jsonAdded+57
- python/embeddinggemma2_coreai.pyAdded+278
- python/requirements.lockAdded+19
- python/requirements.txtAdded+7
- python/smoke_test.pyAdded+62
- swift/Package.swiftAdded+26
- swift/Sources/EmbeddingGemma2CoreAI/Embedder.swiftAdded+197
- swift/Sources/EmbeddingGemma2CoreAI/ExactJSON.swiftAdded+202
- swift/Sources/EmbeddingGemma2CoreAI/GemmaTokenizer.swiftAdded+281
- swift/Sources/eg2-cli/main.swiftAdded+187
- tests/make_smoke_reference.pyAdded+54
- tests/make_tokenizer_cases.pyAdded+298
- tests/smoke-reference.jsonAdded+2333
- tests/tokenizer-cases.jsonAdded
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines333435363732 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textmodel/embeddinggemma-2-text.aimodel/main.mlirb filter=lfs diff=lfs merge=lfs -textmodel/tokenizer/tokenizer.json filter=lfs diff=lfs merge=lfs -text
1234567891011121314151617181920212223242526272829303132# Changelog
## 1.0 — 2026-10-12
First release package.
- Model: `model/embeddinggemma-2-text.aimodel`, the text encoder of google/embeddinggemma-2 on Apple Core AI (macOS 27), fp16, every function one Neural Engine region plus the token lookup; buckets 32…2048. Parity vs the fp32 reference on 357 fictional texts: cosine min 0.99992, median 0.99997 (min 0.99985 on a second, independent set of 100 texts: expect >= 0.9998); retrieval ranks identical at 768 dims on a 1,712-chunk / 53-query fictional benchmark.- Swift: `EmbeddingGemma2CoreAI` library (no third-party dependencies) with an exact Gemma tokenizer (`GemmaTokenizer`, byte-identical to Hugging Face `tokenizers` on 3,758 fixture texts / 608,225 ids) and the `eg2-cli` tool (embed, smoke, tokcheck, latency, prewarm, cleanup-temp).- Python: `embeddinggemma2_coreai.py` runner (library + CLI) using the `tokenizers` library, `smoke_test.py`, pinned requirements.- Tests: `tests/smoke-reference.json` (3 fictional sentences, fp32 reference vectors), `tests/tokenizer-cases.json` (3,758 fictional texts with reference ids) and the generators for both.- Exporter: `export/eg2_text.py` + `export/export.py` reproduce the bundle from the original checkpoint.- Temp-file hygiene: both runners can delete the MPSGraph temp folder their own process created (`--cleanup-temp`, `removeOwnMPSGraphTempFiles()`, `remove_own_mpsgraph_temp_files()`).- Licensing: full Apache-2.0 text, NOTICE with the modification list and the Gemma Prohibited Use Policy pointer, SHA-256 checksums of every file.
Changes relative to the internal validation build that preceded this package:
- The global-attention layers use the 4-D matmul form (the earlier bundle fell back to the GPU on most buckets); the earlier bundle is not included.- The Swift runner no longer depends on swift-transformers (whose BPE differed from Hugging Face on CRLF, decomposed accents, emoji sequences, Arabic diacritics, Thai and whitespace runs longer than 31).- tokenizer.json is read with an exact JSON reader: Foundation's JSONSerialization drops a leading U+FEFF from string values, which corrupted three vocabulary entries.12345678910111213141516171819202122232425267c059597beb76a3ce4e7cc6e0b08f6aa7e8eeb932ab9b18e13234ca9537809e1 CHANGELOG.mdcfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30 LICENSEdfaaa2df3d3b356d6ab7d37795893ca3916f9855095658700b35f387f8e7d68f NOTICE742bc793350bbe91c402dcdbc339403f671b46a8c953d366a41f803300b3f52b README.mdbb778eee49dab44fb997f67c875a615d754d54477e257d02771e94f250b4cd38 export/eg2_text.py9392857facca6d9b9a735146c72a342ffa8c52c7c752ea2c499acb10686a3cdc export/export.py7ddb494420af4401b1e3c6e0b54f176c218f9d5068079c9ccfa7871fad5f5a19 model/MODEL-ATTRIBUTION.json1791655ef7f1c0c093d200904263989c0f7cfc637869b71c6fc5f90f0f800f18 model/config.jsonc2de75ebee766df0e980f63bc56a60847272cfe393de744e766bee9ba1a8049b model/embeddinggemma-2-text.aimodel/main.hash75d38431abcd2471b0540c863357710d305cb1687295f5b0cf859de6ac9d46eb model/embeddinggemma-2-text.aimodel/main.mlirbc2362545fc6b46be69f394ff0081a6e919a9099b19a84b167fbdeb71ba7c793c model/embeddinggemma-2-text.aimodel/metadata.json4d777ef5bdc1aa36227abdfb77c3e49e7b9c892d16e1b6bda41c393504828be4 model/tokenizer/tokenizer.json17bd5d6e9364ca49a534e1502076593317c298d4a663623091ed45388f004874 model/tokenizer/tokenizer_config.json2559a5e111a31b53e9568c962be2fb51ec84c17912bcfc40f0cafb7f73ca9f01 python/embeddinggemma2_coreai.py3bc2e5931e7dde258a82637e082bf9f12f9a6eed6c4cc703f9b8e4e02a9d3b0c python/requirements.lock2d7b80b555b56787f2a142da1164c359e306b7ad3332661ac158b3a1f114d581 python/requirements.txt3126ad6f90704e025f57632664fe1954a7c88b734a9e695441f471dff79d863e python/smoke_test.pyde934714b85d008c8e56b8027c86c0f1319bb3ca1bb0dec9ffe3ab7ca8c35876 swift/Package.swift5c36c78cf268da78c3a6981bcfd4974b94641f756fa9616950cd39ebe6fc096f swift/Sources/EmbeddingGemma2CoreAI/Embedder.swift7b99edfefc47a343d07df32a3b06a8da2f94a49ae0a8e299e733c1a44c09e38f swift/Sources/EmbeddingGemma2CoreAI/ExactJSON.swift4f5ef8a454a84937e503ba99c9f05e237347178933a442f2352a2c5db0ad87eb swift/Sources/EmbeddingGemma2CoreAI/GemmaTokenizer.swiftf59a75b9eb311777bba3f8b1146b75a11f6587cdca5a2c1dab6fffc40d96a9aa swift/Sources/eg2-cli/main.swift1df8f74c85f4a02be0caaf4b99ee0bb8f4cfc1759ad091fdf65a98fdea917df8 tests/make_smoke_reference.py07daa7415beb15e9049a307aa17bf048ba2edaa0613301293e6250a66ed552b8 tests/make_tokenizer_cases.pydce930ac23f448c55d3e4fbf5e3d1080213395510637d627f67b54a889f339e1 tests/smoke-reference.json0797f417a0fa96270dc2f439478000fe3b3a235eb1e83b3054cf69674e323ae9 tests/tokenizer-cases.json123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485EmbeddingGemma-2-CoreAI-text v1.0=================================
This package contains a derivative of the EmbeddingGemma 2 model.
EmbeddingGemma 2 Authors: Google DeepMind (as stated on the model card) https://huggingface.co/google/embeddinggemma-2 License as linked by the model card: Apache 2.0, https://ai.google.dev/gemma/docs/gemma_4_license Licensed under the Apache License, Version 2.0 (the "License"); you may not use these files except in compliance with the License. A copy of the License is in the file LICENSE and at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
The model card of google/embeddinggemma-2 asks that deployments adhere to the Gemma Prohibited Use Policy:https://ai.google.dev/gemma/prohibited_use_policy. Please read it before using this model in a product.
The tokenizer files in model/tokenizer/ (tokenizer.json, tokenizer_config.json) are copied unchanged fromgoogle/embeddinggemma-2 and are covered by the same license and attribution.
MODIFICATIONS (Apache License, Section 4(b))--------------------------------------------
Changes made to google/embeddinggemma-2, 11-12 October 2026, to produce model/embeddinggemma-2-text.aimodel:
2026-10-11 Text encoder only. The vision and audio encoders of the multimodal checkpoint were dropped. 2026-10-11 The encoder architecture (24 bidirectional Gemma 4 layers, per-layer embeddings, RMSNorm with max-abs rescaling, RoPE, GQA/MQA attention, GELU-tanh MLP, final RMSNorm, 512->768 projection, masked mean pooling, L2 normalization) was re-authored in plain PyTorch for the Core AI converter. The weights are the original ones, rounded from bfloat16 to float16; the sqrt(512) token-embedding scale and the 512^-0.5 per-layer-embedding scale are folded into the tables. 2026-10-11 fp16 handling for the Neural Engine: every RMSNorm and the final L2 normalization divide the row by its max-abs value before squaring (activations reach ~2000, whose square overflows fp16); in the model code every division is expressed as rsqrt(x)*rsqrt(x); the additive attention-mask value is -10000. (The model card advises against float16; this rescaling is what makes fp16 safe here, see the parity figures in README.md.) 2026-10-11 Pooling, projection and normalization run inside the graph; the output is the final embedding. 2026-10-11 Fixed sequence buckets: one function main_<S> per S in 32, 64, 128, 256, 512, 1024, 2048. Input longer than 2048 tokens is not supported by this build (the original accepts 8192). 2026-10-12 Attention of the four global (full-attention, single key/value head) layers rewritten from a 5-D grouped matmul to an equivalent plain 4-D matmul with the key/value tensors broadcast over the four query heads, so that the Neural Engine compiler accepts the whole graph. Mathematically identical to the reference (fp32 max abs diff 3e-7). 2026-10-12 Converted with coreai-torch 0.4.3 / coreai-core 1.0.0b3 on torch 2.9.0 to the Core AI .aimodel format (macOS 27).
The exporter in export/ reproduces these changes from the original checkpoint.
THIRD-PARTY ATTRIBUTIONS------------------------
HF transformers (Apache-2.0, Copyright 2026 The HuggingFace Team). export/eg2_text.py re-authors thearchitecture described by modeling_embedding_gemma2.py / configuration_embedding_gemma2.py fromtransformers 5.19. No transformers source files are included in this package; the re-authored module carriesits own Apache-2.0 header and credits transformers.
FluidInference Core ML port of EmbeddingGemma 2 (Apache-2.0, https://huggingface.co/FluidInference). Theidea of dividing each RMSNorm row by its max-abs value to keep fp16 in range was taken from the README ofthat port and re-derived independently; no code was copied from it. Nothing else in this package derives fromit.
Apple Core AI (coreai-torch, coreai-core, the CoreAI framework): used as tools and runtime, not redistributed.Hugging Face tokenizers (Apache-2.0): used by the Python runner and to generate the test fixtures; notredistributed.
TRADEMARKS----------
Gemma and EmbeddingGemma are trademarks of Google LLC. Apple, Core AI, Apple silicon and Neural Engine aretrademarks of Apple Inc. This port is not affiliated with, sponsored by or endorsed by Google or Apple.
OUR OWN CODE------------
Everything in this package that is not the model weights or the tokenizer files, i.e. the exporter(export/), the Python runner (python/), the Swift library and command-line tool (swift/), the tests (tests/)and the documentation, is
Copyright 2026 Everpaper Licensed under the Apache License, Version 2.0 (LICENSE).123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346---license: apache-2.0base_model: google/embeddinggemma-2library_name: coreaipipeline_tag: feature-extractionlanguage: - multilingualtags: - coreai - apple-neural-engine - on-device - embeddings - sentence-similarity - gemma - macos---
# EmbeddingGemma 2 (text) for Apple Core AI
The text encoder of [google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2) (Gemma 4 family,~270 M parameters, 768-d Matryoshka embeddings) converted to Apple's **Core AI** runtime for **macOS 27 on Applesilicon**. One graph does everything `SentenceTransformer("google/embeddinggemma-2").encode()` does for text: tokenembedding, 24 bidirectional encoder layers, final norm, 512→768 projection, masked mean pooling and L2normalization. Every function runs as one Neural Engine region plus the token lookup (a small GPU gather), so adocument costs about **0.3 ms of CPU** and **0.4–0.5 ms of GPU time**; the Neural Engine does the rest.
Ported by Everpaper — everpaper.app. Weights are the original ones (bf16 rounded to fp16). Apache-2.0, see[LICENSE](LICENSE) and [NOTICE](NOTICE) (modification list, Gemma Prohibited Use Policy pointer).
## Download
```bashhf download iannedev/embeddinggemma-2-coreai --local-dir EmbeddingGemma-2-CoreAI-text-v1.0cd EmbeddingGemma-2-CoreAI-text-v1.0```
(`hf` comes with `pip install -U huggingface_hub`.) Then run one of the quick starts below; both begin with asmoke test that checks the model on your machine.
## What is in the package
```EmbeddingGemma-2-CoreAI-text-v1.0/ README.md, LICENSE, NOTICE, CHANGELOG.md, CHECKSUMS.sha256 model/ embeddinggemma-2-text.aimodel/ Core AI asset: functions main_32 … main_2048 (fp16, 631.5 MB = 602 MiB) config.json buckets, prompts, special ids, Matryoshka dims tokenizer/ tokenizer.json + tokenizer_config.json, unchanged from google/embeddinggemma-2 MODEL-ATTRIBUTION.json base model, checkpoint sha256, exporter versions, modification list swift/ SwiftPM package: library `EmbeddingGemma2CoreAI` + tool `eg2-cli` (no dependencies) python/ embeddinggemma2_coreai.py (library + CLI), smoke_test.py, requirements(.lock) tests/ smoke-reference.json, tokenizer-cases.json (+ the scripts that generate them) export/ eg2_text.py + export.py: reproduce the .aimodel from the original checkpoint```
## Requirements
- macOS 27.0 or later on Apple silicon (the package was validated on an M5 with 24 GB; macOS 27.0 build 26A5406e, Xcode 27 beta).- Swift: Xcode 27 / Swift 6 toolchain; the `CoreAI` framework is part of the macOS 27 SDK.- Python (runtime): Python 3.12 or 3.14 (both validated), Apple's `coreai-core` Python runtime (provides `coreai.runtime`), `numpy`, `tokenizers` (`python/requirements.txt`; the exact validated versions are in `python/requirements.lock`). Only the exporter (`export/`, optional) needs Python 3.12, for torch 2.9.0.- Disk: 664 MB for `model/` (the `.aimodel` is 631.5 MB, the tokenizer 32 MB); the system keeps a per-executable specialization cache of about 1.4 GB under `~/Library/Caches/coreai-cache/`.
## Inputs and outputs
| | ||---|---|| Functions | `main_32`, `main_64`, `main_128`, `main_256`, `main_512`, `main_1024`, `main_2048` (one per sequence bucket S) || `input_ids` | `[1, S]` int32: `<bos>`=2, prompt-prefix tokens, text tokens, `<eos>`=1, right-padded with `<pad>`=0 || `attention_mask` | `[1, S]` float16: 1 for real tokens, 0 for padding || `embedding` | `[1, 768]` float16, already L2-normalized || Max length | 2048 tokens including `<bos>`/`<eos>` and the prefix. Longer input is **truncated** by the runners (the original model accepts 8192; chunk long documents at ≤ 512 tokens for retrieval, the embedding is a mean over tokens anyway). || Bucket choice | the smallest S ≥ token count. Padding does not change the result (verified: padded and unpadded runs are bit-identical). || Matryoshka | 512 / 256 / 128 dims: take the first N values and re-normalize (`embed(..., dim:)` does it). |
**Task prompts** (exactly the upstream `config_sentence_transformers.json`; the runners prepend them by `kind`):
| kind | prefix ||---|---|| `document` | `title: none \| text: ` — for titled documents use `title: {title} \| text: {content}` yourself || `search_query` | `task: search result \| query: ` || `question_answering` | `task: question answering \| query: ` || `fact_checking` | `task: fact checking \| query: ` || `code_retrieval` | `task: code retrieval \| query: ` || `classification` | `task: classification \| query: ` || `clustering` | `task: clustering \| query: ` || `sentence_similarity` | `task: sentence similarity \| query: ` |
The tokenizer is the original Gemma `tokenizer.json`. The runners add `<bos>` and `<eos>` themselves, assentence-transformers does. Note that special tokens written literally in a text (e.g. `<eos>`) are recognised asspecial tokens, exactly as in Hugging Face `tokenizers`; strip them if that is not what you want.
## Quick start — Swift
```swift// Package.swift: .package(path: "…/EmbeddingGemma-2-CoreAI-text-v1.0/swift"), product "EmbeddingGemma2CoreAI"import EmbeddingGemma2CoreAI
let model = try await EmbeddingGemma2(bundle: URL(fileURLWithPath: "…/model"), compute: .neuralEngine)try await model.prewarm() // optional, one call per bucket (~0.4 s warm)
let q = try await model.embed("When does the warehouse lease expire?", kind: "search_query") // [Float], 768let d = try await model.embed("The lease runs until the last day of March.", kind: "document", dim: 256)let score = cosineSimilarity(q, try await model.embed("The lease runs until the last day of March.", kind: "document"))
// lower level: your own ids -> raw output of one bucketlet ids = model.tokenize("some text", kind: "document") // <bos> + prefix + text + <eos>, truncated to 2048let raw = try await model.embed(ids: ids) // runs main_<smallest bucket that fits>
// when the model is released (or at exit) you may delete the temp files this process created, see "Temp files"EmbeddingGemma2.removeOwnMPSGraphTempFiles()```
`GemmaTokenizer` is public too (`encode`, `encodeWithSpecials`, `decode`): an exact re-implementation of theHugging Face `tokenizers` pipeline for this `tokenizer.json`, with no dependencies (see "Tokenizer" below).
Command line (`cd swift && swift build -c release`; the binary is `.build/release/eg2-cli` or`.build/out/Products/Release/eg2-cli` depending on the toolchain):
```eg2-cli smoke ../model ../tests/smoke-reference.json --compute ane --cleanup-tempeg2-cli embed ../model --kind search_query "When does the warehouse lease expire?"eg2-cli tokcheck ../model ../tests/tokenizer-cases.jsoneg2-cli latency ../model --compute ane --trials 50eg2-cli prewarm ../model```
## Quick start — Python
From the package folder:
```python3 -m venv .venv && .venv/bin/pip install -r python/requirements.txtsource .venv/bin/activatecd python && python3 smoke_test.py --compute ane --cleanup-temp # first run: up to ~145 s to load, see below```
```pythonfrom embeddinggemma2_coreai import EmbeddingGemma2CoreAI, remove_own_mpsgraph_temp_files
m = EmbeddingGemma2CoreAI("../model", compute="ane") # ane | gpu | cpu | defaultm.prewarm()q = m.embed("When does the warehouse lease expire?", kind="search_query") # numpy float32 [768]d = m.embed("The lease runs until the last day of March.", kind="document", dim=256)del mremove_own_mpsgraph_temp_files() # the Python runtime leaks ~358 MB per load, see below```
`python3 embeddinggemma2_coreai.py embed|smoke|tokcheck|latency|prewarm …` mirrors the Swift tool (run it with thevenv's Python, as above).
## Accuracy (measured)
Reference: sentence-transformers 6.1 / transformers 5.19, fp32, CPU, original checkpoint. Test set: 357 fictionaltexts (English and Italian documents and queries, code, mixed-language, Unicode/number/repetition stress cases, andtexts whose length lands exactly on each bucket boundary), 2 to 2048 tokens. The people, companies and events inthe test material are fictional; a few real place names appear in the multilingual stress texts.
**Parity** — cosine similarity between this port and the fp32 reference, full 768 dims (128-dim Matryoshka inbrackets):
| route | min | median | worst category ||---|---|---|---|| Neural Engine (`compute: .neuralEngine`), Swift and Python | 0.99992 (0.99995) | 0.99997 (0.99998) | 33-token bucket-boundary text || GPU (`compute: .gpu`) | 0.99998 (0.99999) | 0.999999 (1.000000) | code || CPU only (`compute: .cpu`), Swift and Python | 0.99916 (0.99914) | 0.99996 (0.99998) | 2047-token text (the CPU fp16 path is the least precise) || for scale: the original model itself in bf16 on the GPU (PyTorch MPS) | 0.99956 | 0.99996 | |
Per bucket on the Neural Engine the minimum is 0.99992–0.99996 and the median 0.99997–0.99998; no NaN/Inf on anyinput, including fully padded and 1-token inputs. Texts longer than 2048 tokens are truncated by design and theircosine to the full-length reference falls as expected (0.998 at 2100 tokens, 0.98 at 4500).
**Independent check.** A second, separate set of 100 new fictional texts (Italian, emoji, CRLF, Thai and otherscripts, NFD accents, code, one-word inputs, long texts, exact bucket boundaries, all 8 prompt kinds) against afreshly downloaded fp32 sentence-transformers reference, Neural Engine route: minimum cosine 0.99985 (an emoji-heavytext), median 0.99997 at 768 dims; 0.99993 / 0.999985 at 128 dims; Swift and Python identical (largest difference2.7e-7). **Expect a cosine ≥ 0.9998 to the fp32 model on the Neural Engine and GPU routes, ≥ 0.999 on the CPU route.**
**About float16.** The upstream model card says *"Do not use float16"*: the model's activations exceed the fp16range and a naive fp16 run returns NaN or degraded vectors. This port runs in fp16 on purpose (the Neural Engine isfp16-only) and avoids the overflow by dividing every RMSNorm row and the final L2 normalization by its max-abs valuebefore squaring (see NOTICE). The parity figures above are the evidence that this works; if you change the graph, keepthat rescaling.
**Retrieval** — 1,712 fictional passages, 53 queries (16 Italian), gold ranks compared rank-by-rank with the fp32reference:
| dims | fp32 reference MRR | this port, Neural Engine, MRR | queries with the same first-relevant rank ||---|---|---|---|| 768 | 0.766 | 0.766 | 53 / 53 || 512 | 0.790 | 0.782 | 51 / 53 || 256 | 0.786 | 0.785 | 52 / 53 || 128 | 0.735 | 0.735 | 52 / 53 |
The flipped ranks are gold passages moving by one rank among near-tied neighbours (e.g. fp32 scores 0.82757 vs0.82755 at 512 dims) plus one query whose gold passage sits outside the top 10 in fp32 and at rank 10 here.Italian MRR is identical at every size (0.831 / 0.830 / 0.834 / 0.716). The GPU route of this bundle was checkedfor parity (table above) but not re-run on the retrieval benchmark.
**Smoke test** (`tests/smoke-reference.json`, 3 fictional sentences, threshold 0.999): Neural Engine cosine0.999948 / 0.999979 / 0.999976 (Swift and Python); Matryoshka 512 / 256 / 128 of the second sentence 0.999979 /0.999981 / 0.999989. GPU route 0.999999 / 0.999999 / 1.000000, CPU route 0.999970 / 0.999966 / 0.999970.
Caveats: these are one machine's numbers (M5, macOS 27.0 26A5406e). Core AI specializes the graph per machineand OS build; the CPU-only route in particular depends on the OS's fp16 CPU kernels. Re-run `smoke` and, if youcare about the last digit, `tests/` on your target machines.
## Performance (measured, Swift `eg2-cli latency`, 50 trials per bucket, M5)
Single runs on one machine; repeated runs differ by a few percent (e.g. 195–199 ms at 2048 tokens on the NeuralEngine).
| bucket (tokens) | Neural Engine p50 | CPU ms per call (NE) | GPU route p50 | CPU ms per call (GPU) ||---|---|---|---|---|| 32 | 4.8 ms | 0.24 | 7.6 ms | 10.3 || 64 | 5.3 ms | 0.24 | 7.9 ms | 10.3 || 128 | 6.6 ms | 0.26 | 10.3 ms | 11.1 || 256 | 11.6 ms | 0.27 | 12.2 ms | 11.7 || 512 | 27.8 ms | 0.26 | 21.9 ms | 14.8 || 1024 | 69 ms | 0.33 | 49 ms | 20.1 || 2048 | 195 ms | 0.39 | 142 ms | 30.6 |
The Python runner gives the same Neural Engine latencies within a few percent (CPU per call 0.4–0.9 ms). On theNeural Engine route the GPU is used only for the int32 token gather: 0.38–0.54 ms of GPU time per call (32–1024tokens), measured during validation with `task_power_info_v2` over 100-call batches. The GPU route is faster from 512tokens up but burns 10–30 ms of CPU per call (MPSGraph encoding) and 1.5 GB of memory.Throughput on a realistic corpus (1,712 passages, median 194 tokens, one text per call): about 50 passages/s,0.17 CPU-seconds per 100 passages.
Memory: process footprint ~230 MB after a warm load (peak ~700 MB during the first-ever load) plus ~600–770 MB ofNeural Engine footprint reported by the kernel; the GPU route peaks at ~1.5 GB.
**Load time** (`AIModel(contentsOf:)` + `loadFunction` for the 7 buckets):
| situation | time ||---|---|| warm (this executable has loaded this bundle before) | 0.4–1.5 s, plus 0.35 s for `prewarm()` || **first load by an executable that has not loaded this bundle before** (a new app, a new binary name, the Python interpreter, a new machine) | **~140 s** (139–144 s measured with three different executables) || first load by a new executable on a machine where another executable already compiled this exact bundle | can be shorter (~25–30 s observed), do not rely on it || GPU route, first load | ~9–22 s |
Recommendation: load and `prewarm()` in the background at app start (or in an installer / first-run step) and showprogress; never on the main thread and never lazily in front of the first query. The cache lives in`~/Library/Caches/coreai-cache/<OS build>/<executable name>/` (about 1.4 GB per bundle per executable name, keyedby the bundle's content hash) and survives reboots but not OS updates, a renamed executable or a re-exportedbundle. The tokenizer loads in 0.22 s (Swift) / 0.23 s (Python).
## Temp files (please read)
Each Neural Engine load writes ~358 MB of compiled-graph copies to`$TMPDIR/com.apple.MetalPerformanceShadersGraph/mpsgraph-<pid>-<date>-<n>/`. Measured on macOS 27.0:
- Swift (`CoreAI` framework): the folder is deleted again by the runtime when the model is released; our CLI, which releases the model and then exits, leaves nothing behind in any of our runs. If your app exits or is killed while the model is loaded, the folder may remain.- Python (`coreai.runtime`): the folder is **never** deleted, not even after `del model` and a clean interpreter exit (358 MB per load, verified for normal exit, `os._exit`, and `kill -9`). Over a day of development this fills a disk.
Both runners therefore ship a cleanup that removes only the folders belonging to the **current process** (its pid,created after the process started): `EmbeddingGemma2.removeOwnMPSGraphTempFiles()` and`remove_own_mpsgraph_temp_files()`, or `--cleanup-temp` on every CLI command. Call it after the model has beenreleased, or at exit. Folders of other processes are never touched; a leftover from a crashed run has to be removedby hand (the folder name carries the pid and date).
## Tokenizer
The Swift `GemmaTokenizer` re-implements the Hugging Face `tokenizers` pipeline described by `tokenizer.json`(special-token extraction, `Replace " " → "▁"` normalizer, BPE with byte fallback, rank-ordered merges withposition tie-break). It works on Unicode scalars and exact UTF-8 bytes: no grapheme clusters, nocanonical-equivalence string comparisons, and `tokenizer.json` is read with an exact JSON reader because Foundation's`JSONSerialization` strips a leading U+FEFF from string values (which silently corrupts three vocabulary entries).
Proof: `tests/tokenizer-cases.json` holds 3,758 fictional texts (608,225 ids) covering CRLF and mixed line endings,NFD / NFKD / mixed-normalization accents, emoji ZWJ sequences, skin tones, flags and keycaps, Arabic and Persianwith diacritics, Hebrew, Thai, Hindi, Bengali, Tamil, Chinese (simplified and traditional), Japanese, Korean,Georgian, Armenian, Amharic and 18 more languages, whitespace runs of 31–500 spaces / newlines / tabs / NBSP /ideographic spaces, control characters and invisible code points, special-token look-alikes (`<bos>`, `<|tool>`,`<start_of_turn>`, `< bos >`, `▁<bos>▁`, …), empty and 1-character inputs, random code points from 23 Unicode blocksincluding unassigned, private-use and non-characters, and inputs of 5,000–80,000 characters. `eg2-cli tokcheck`reports **3758 / 3758 identical** to `tokenizers` 0.23.3, in 0.15 s. The Python runner uses `tokenizers` directly.
Regenerate the fixture with `tests/make_tokenizer_cases.py` (needs `tokenizers`) and the reference vectors with`tests/make_smoke_reference.py` (needs sentence-transformers and the original checkpoint).
## Known issues and limitations
- **2048-token cap.** Longer inputs are truncated by the runners. Buckets of 4096/8192 can be exported (`export.py --buckets …,4096,8192`) but were not validated here and would run on the GPU.- **One text per call.** The functions are batch-1. Throughput for a corpus comes from calling them back to back; running two or four calls concurrently did not increase throughput in our tests (the Neural Engine serializes).- **Short inputs are not faster than ~4.8 ms.** There is a per-call floor in this Core AI build; 32- and 64-token inputs cost the same.- **CPU-only route is less precise** (cosine ≥ 0.9991) — use it only as a fallback.- **Latency is not shape-stable across bucket sets.** Core AI specializes the whole function set together; adding or removing buckets changed the 512-token latency by half (22 vs 34 ms) in our experiments. Measure the exact bucket set you ship.- **Specialization cache is per executable name.** Renaming your binary costs one first load of up to ~140 s.- **Temp files** as described above.- **fp16 output.** The embedding is fp16 (relative precision ~5e-4); convert to float32 before accumulating dot products over many vectors, as the runners do.- Validated on one machine and one OS build (macOS 27.0 26A5406e). Core AI is new in macOS 27 and its runtime behaviour (first-load time, placement, temp files) may change between OS releases.
## Reproducing the export
```# a separate environment with Python 3.12 (torch 2.9.0); exact versions in python/requirements.lockpython3.12 -m venv .venv-export && source .venv-export/bin/activatepip install coreai-core==1.0.0b3 torch==2.9.0 coreai-torch==0.4.3 coreai-opt==0.3.0 torchao==0.17.0 \ safetensors numpy tokenizers huggingface-hub# the original checkpoint: config.json, model.safetensors, tokenizer.json, tokenizer_config.jsonhf download google/embeddinggemma-2 --local-dir ./embeddinggemma-2cd exportpython3 -W ignore export.py --model-dir ../embeddinggemma-2 --out ../model-rebuilt --name embeddinggemma-2-text \ --mode ane --buckets 32,64,128,256,512,1024,2048 --date 2026-10-12```
`eg2_text.py` is the re-authored encoder (plain PyTorch, `PRECISION_MODE = "ane"`: all fp16, max-abs-rescalednorms, no divides in the model code; `GLOBAL_ATTN_MODE = "mqa4d"`). `export.py` does one dynamic-length `torch.export`, converts with`coreai_torch.TorchConverter`, clones the entry point per bucket with `set_static_shape_config`, and writes thebundle (~20 s). torchao must be 0.17.0 with torch 2.9 (0.18 breaks `import coreai_opt`).
The rebuilt bundle is **functionally identical, not byte-identical**: its smoke-test cosines match the shipped bundleto all printed digits (0.999948 / 0.999979 / 0.999976), but the converter names internal graph resources at random,so `main.mlirb` and `main.hash` differ by a few KB, and the runtime writes `metadata.json` with the export time andits own key order. `config.json`, `MODEL-ATTRIBUTION.json` (with `--date 2026-10-12`) and the tokenizer files come outbyte-identical, so those four entries of `CHECKSUMS.sha256` match. Verify a rebuild with `smoke`, not withchecksums. A rebuilt bundle has a new content hash, so its first load takes the full ~140 s again.
To check Neural Engine eligibility without running: `xcrun coreai-build compile model/embeddinggemma-2-text.aimodel--output ./compiled --preferred-compute neural-engine --architecture h17g 2>&1 | grep -c ane_validation_message`prints 0 for this bundle (no op rejected; the output folder is about 700 MB, delete it afterwards).
## Credits and license
- Model: EmbeddingGemma 2 by the Gemma Team, Google DeepMind — Apache-2.0. Deployments must adhere to the [Gemma Prohibited Use Policy](https://ai.google.dev/gemma/prohibited_use_policy).- Architecture reference: Hugging Face transformers (Apache-2.0).- The max-abs RMSNorm rescaling idea comes from the FluidInference Core ML port of this model (Apache-2.0); no code was copied.- Port, exporter, runners, tokenizer and tests: Copyright 2026 Everpaper, Apache-2.0.- Gemma and EmbeddingGemma are trademarks of Google LLC; Apple, Core AI, Apple silicon and Neural Engine are trademarks of Apple Inc. This port is not affiliated with, sponsored by or endorsed by Google or Apple.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE).## Re-authoring of the EmbeddingGemma 2 text encoder architecture as described by# modeling_embedding_gemma2.py / configuration_embedding_gemma2.py in Hugging Face transformers 5.19# (Copyright 2026 The HuggingFace Team, Apache-2.0). Model weights: Google DeepMind (Gemma Team), Apache-2.0,# https://huggingface.co/google/embeddinggemma-2 — see ../NOTICE for the full attribution and modification list."""EmbeddingGemma 2 text encoder re-authored for Core AI export (standard [B, S, D] layout, batch 1).
Everything the sentence-transformers pipeline does is inside one graph: input_ids [1, S] int32 + attention_mask [1, S] (1 = token, 0 = right padding) -> scaled token embedding (sqrt(512) folded into the fp16 table) -> projection-only per-layer embeddings (PLE) -> 24 bidirectional encoder layers (5 sliding-window-512 : 1 full, GQA 4 heads / 2 kv (sliding, hd 256) or 1 kv (full, hd 512)) -> final RMSNorm -> 512->768 projection -> masked mean pooling -> L2 normalization -> embedding [1, 768]
Precision modes (PRECISION_MODE, set before construction): "ane" every op fp16 (Neural Engine eligible). RMSNorm and the final L2 norm divide the row by its max-abs value before squaring (the model's activations reach ~2000, whose square overflows fp16; this rescaling idea comes from the README of the FluidInference Core ML port of this model and was re-derived here). Softmax in fp16 (scores are bounded because q/k are RMS-normalized with scale 1.0). "gpu" fp16 weights/matmuls, fp32 islands for every RMSNorm, the softmax, the pooling and the L2 norm (the HF reference computes the norms and the softmax in fp32). "fp32" the whole model in fp32 (correctness gate for the re-authoring itself; never exported)."""from __future__ import annotations
import jsonimport mathfrom pathlib import Path
import torchimport torch.nn as nnimport torch.nn.functional as F
PRECISION_MODE = "ane" # "ane" | "gpu" | "fp32"GQA_MODE = "grouped5d" # "expand" (HF repeat_kv: expand+reshape) | "grouped5d" (no repeat, 5-D batched matmul) | "repeat_interleave"# Global (full-attention, MQA: 1 kv head, head_dim 512) layers 5/11/17/23. "grouped5d" = the GQA_MODE path.# "mqa4d" (the released bundle) = plain 4-D matmul with k/v broadcast over the heads. With grouped5d the Neural# Engine compiler rejects the MQA matmul ("Unsupported mps.matmul op for this ANE architecture"), every global layer# becomes a GPU region, and MPSGraph then places the short buckets entirely on the GPU; mqa4d keeps one GPU region# (the token gather) and everything else on the ANE. Mathematically identical (fp32 max|diff| 3e-7).GLOBAL_ATTN_MODE = __import__("os").environ.get("EG2_GLOBAL_ATTN", "mqa4d")# repeat_interleave made the Neural Engine compiler's fused SDPA fail on the MQA layers (1 kv head, 4 q heads):# "'mps_spi.sdpa' op failed: grouping for the value tensor does not match the one available on the key tensor".MASK_VALUE = -10000.0 # finite (ANE softmax mishandles -inf); fp16-exactNORM_FLOOR = 1e-3 # floor for the row max-abs in "ane" RMSNorm (sqrt(eps) = 1e-3 for eps 1e-6)
class Cfg: def __init__(self, model_dir: str): c = json.loads((Path(model_dir) / "config.json").read_text())["text_config"] self.hidden = c["hidden_size"] # 512 self.inter = c["intermediate_size"] # 2048 self.n_layers = c["num_hidden_layers"] # 24 self.n_heads = c["num_attention_heads"] # 4 self.layer_types = c["layer_types"] self.sliding_window = c["sliding_window"] # 512 (inclusive distance) self.eps = c["rms_norm_eps"] self.vocab = c["vocab_size"] self.embed_dim = c["embedding_dim"] # 768 self.ple_dim = c["hidden_size_per_layer_input"] # 512 self.act = c["hidden_activation"] assert self.act == "gelu_pytorch_tanh", self.act self.rope_theta = {k: v["rope_theta"] for k, v in c["rope_parameters"].items()} per = c.get("per_layer_config", {}) self.head_dim = [per.get(f"{i:02d}", {}).get("head_dim", c["head_dim"]) for i in range(self.n_layers)] self.kv_heads = [per.get(f"{i:02d}", {}).get("num_key_value_heads", c["num_key_value_heads"]) for i in range(self.n_layers)]
def _f16(x: float) -> torch.Tensor: return torch.tensor(x, dtype=torch.float16)
def _recip(m: torch.Tensor) -> torch.Tensor: """1/m for m > 0 without a divide: the Neural Engine compiler rejects `mps.divide` (falls back to the GPU); rsqrt is native. rsqrt(m)*rsqrt(m) stays in fp16 range for m in [6e-5, 65504].""" r = torch.rsqrt(m) return r * r
class RMSNorm(nn.Module): """Gemma RMSNorm: x * rsqrt(mean(x^2) + eps) * w (w stored as the raw weight, no +1)."""
def __init__(self, dim: int, eps: float, with_scale: bool = True): super().__init__() self.with_scale = with_scale if with_scale: self.weight = nn.Parameter(torch.ones(dim), requires_grad=False) self.eps = eps self.register_buffer("eps16", _f16(eps), persistent=False) self.register_buffer("sqrt_eps16", _f16(math.sqrt(eps)), persistent=False) self.register_buffer("floor16", _f16(NORM_FLOOR), persistent=False)
def forward(self, x: torch.Tensor) -> torch.Tensor: if PRECISION_MODE == "ane": inv_m = _recip(torch.maximum(x.abs().amax(-1, keepdim=True), self.floor16)) u = x * inv_m r = self.sqrt_eps16 * inv_m inv = torch.rsqrt((u * u).mean(-1, keepdim=True) + r * r) y = u * inv return y * self.weight if self.with_scale else y xf = x.float() y = xf * torch.rsqrt(xf.pow(2).mean(-1, keepdim=True) + self.eps) if self.with_scale: y = y * self.weight.float() return y.to(x.dtype)
def rotate_half(x: torch.Tensor) -> torch.Tensor: h = x.shape[-1] // 2 return torch.cat((-x[..., h:], x[..., :h]), dim=-1)
class Attention(nn.Module): def __init__(self, cfg: Cfg, i: int): super().__init__() self.hd = cfg.head_dim[i] self.n_heads = cfg.n_heads self.n_kv = cfg.kv_heads[i] self.groups = self.n_heads // self.n_kv self.q_proj = nn.Linear(cfg.hidden, cfg.n_heads * self.hd, bias=False) self.k_proj = nn.Linear(cfg.hidden, self.n_kv * self.hd, bias=False) self.v_proj = nn.Linear(cfg.hidden, self.n_kv * self.hd, bias=False) self.o_proj = nn.Linear(cfg.n_heads * self.hd, cfg.hidden, bias=False) self.q_norm = RMSNorm(self.hd, cfg.eps) self.k_norm = RMSNorm(self.hd, cfg.eps) self.v_norm = RMSNorm(self.hd, cfg.eps, with_scale=False)
def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, bias: torch.Tensor) -> torch.Tensor: B, S, _ = x.shape q = self.q_proj(x).view(B, S, self.n_heads, self.hd).transpose(1, 2) k = self.k_proj(x).view(B, S, self.n_kv, self.hd).transpose(1, 2) v = self.v_proj(x).view(B, S, self.n_kv, self.hd).transpose(1, 2) q, k, v = self.q_norm(q), self.k_norm(k), self.v_norm(v) q = q * cos + rotate_half(q) * sin k = k * cos + rotate_half(k) * sin if self.n_kv == 1 and GLOBAL_ATTN_MODE == "mqa4d": # MQA (global layers, 1 kv head): plain 4-D matmul, k/v broadcast over the 4 query heads, no repeat scores = torch.matmul(q, k.transpose(-1, -2)) + bias p = torch.softmax(scores, dim=-1) if PRECISION_MODE == "ane" else torch.softmax(scores.float(), dim=-1).to(q.dtype) o = torch.matmul(p, v) elif GQA_MODE == "grouped5d" and self.groups > 1: qg = q.view(B, self.n_kv, self.groups, S, self.hd) scores = torch.matmul(qg, k.transpose(-1, -2)[:, :, None]) + bias[:, None] # [B, n_kv, groups, S, S] p = torch.softmax(scores, dim=-1) if PRECISION_MODE == "ane" else torch.softmax(scores.float(), dim=-1).to(q.dtype) o = torch.matmul(p, v[:, :, None]).view(B, self.n_heads, S, self.hd) else: if self.groups > 1: if GQA_MODE == "repeat_interleave": k = k.repeat_interleave(self.groups, dim=1) v = v.repeat_interleave(self.groups, dim=1) else: k = k[:, :, None].expand(B, self.n_kv, self.groups, S, self.hd).reshape(B, self.n_heads, S, self.hd) v = v[:, :, None].expand(B, self.n_kv, self.groups, S, self.hd).reshape(B, self.n_heads, S, self.hd) scores = torch.matmul(q, k.transpose(-1, -2)) + bias # scale 1.0 (the model's q/k norms carry it) p = torch.softmax(scores, dim=-1) if PRECISION_MODE == "ane" else torch.softmax(scores.float(), dim=-1).to(q.dtype) o = torch.matmul(p, v) o = o.transpose(1, 2).reshape(B, S, self.n_heads * self.hd) return self.o_proj(o)
class MLP(nn.Module): def __init__(self, cfg: Cfg): super().__init__() self.gate_proj = nn.Linear(cfg.hidden, cfg.inter, bias=False) self.up_proj = nn.Linear(cfg.hidden, cfg.inter, bias=False) self.down_proj = nn.Linear(cfg.inter, cfg.hidden, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.down_proj(F.gelu(self.gate_proj(x), approximate="tanh") * self.up_proj(x))
class PLEBlock(nn.Module): def __init__(self, cfg: Cfg): super().__init__() self.per_layer_input_gate = nn.Linear(cfg.hidden, cfg.ple_dim, bias=False) self.per_layer_projection = nn.Linear(cfg.ple_dim, cfg.hidden, bias=False) self.post_per_layer_input_norm = RMSNorm(cfg.hidden, cfg.eps)
def forward(self, x: torch.Tensor, ple: torch.Tensor) -> torch.Tensor: h = F.gelu(self.per_layer_input_gate(x), approximate="tanh") * ple return x + self.post_per_layer_input_norm(self.per_layer_projection(h))
class Layer(nn.Module): def __init__(self, cfg: Cfg, i: int): super().__init__() self.self_attn = Attention(cfg, i) self.mlp = MLP(cfg) self.input_layernorm = RMSNorm(cfg.hidden, cfg.eps) self.post_attention_layernorm = RMSNorm(cfg.hidden, cfg.eps) self.pre_feedforward_layernorm = RMSNorm(cfg.hidden, cfg.eps) self.post_feedforward_layernorm = RMSNorm(cfg.hidden, cfg.eps) self.ple_block = PLEBlock(cfg) self.register_buffer("layer_scalar", torch.ones(1))
def forward(self, x, ple, cos, sin, bias): x = x + self.post_attention_layernorm(self.self_attn(self.input_layernorm(x), cos, sin, bias)) x = x + self.post_feedforward_layernorm(self.mlp(self.pre_feedforward_layernorm(x))) x = self.ple_block(x, ple) return x * self.layer_scalar
class EmbeddingGemma2Text(nn.Module): def __init__(self, cfg: Cfg, max_seq: int = 8192): super().__init__() self.cfg = cfg self.max_seq = max_seq self.embed_tokens = nn.Embedding(cfg.vocab, cfg.hidden) # sqrt(hidden) folded in at load self.per_layer_model_projection = nn.Linear(cfg.hidden, cfg.n_layers * cfg.ple_dim, bias=False) # scale folded self.per_layer_projection_norm = RMSNorm(cfg.ple_dim, cfg.eps) self.layers = nn.ModuleList([Layer(cfg, i) for i in range(cfg.n_layers)]) self.norm = RMSNorm(cfg.hidden, cfg.eps) self.embedding_projection = nn.Linear(cfg.hidden, cfg.embed_dim, bias=False) # RoPE tables [max_seq, hd] per (layer type); head dims differ: sliding 256 @ theta 1e4, full 512 @ theta 1e6 for lt in ("sliding_attention", "full_attention"): hd = cfg.head_dim[cfg.layer_types.index(lt)] inv = 1.0 / (cfg.rope_theta[lt] ** (torch.arange(0, hd, 2, dtype=torch.float32) / hd)) fr = torch.arange(max_seq, dtype=torch.float32)[:, None] * inv[None] emb = torch.cat((fr, fr), dim=-1) self.register_buffer(f"cos_{lt}", emb.cos(), persistent=False) self.register_buffer(f"sin_{lt}", emb.sin(), persistent=False) self.register_buffer("mask_value", _f16(MASK_VALUE), persistent=False) self.register_buffer("one", _f16(1.0), persistent=False) self.register_buffer("zero", _f16(0.0), persistent=False) self.register_buffer("floor16", _f16(NORM_FLOOR), persistent=False) self.register_buffer("window", torch.tensor(cfg.sliding_window, dtype=torch.int32), persistent=False)
def _biases(self, attention_mask: torch.Tensor, S: int): pad = (self.one - attention_mask) * self.mask_value # [1, S] 0 for tokens, -1e4 for padding keys full = pad[:, None, None, :] # [1, 1, 1, S] broadcast over heads and queries pos = torch.arange(S, device=attention_mask.device, dtype=torch.int32) dist = (pos[:, None] - pos[None, :]).abs() band = torch.where(dist <= self.window, self.zero, self.mask_value) # [S, S] sliding = full + band[None, None] return full, sliding
def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor: S = input_ids.shape[1] x = self.embed_tokens(input_ids) # [1, S, 512], already scaled ple = self.per_layer_projection_norm( self.per_layer_model_projection(x).view(1, S, self.cfg.n_layers, self.cfg.ple_dim)) full, sliding = self._biases(attention_mask, S) cs = {} for lt in ("sliding_attention", "full_attention"): cs[lt] = (getattr(self, f"cos_{lt}")[:S].to(x.dtype)[None, None], getattr(self, f"sin_{lt}")[:S].to(x.dtype)[None, None]) for i, layer in enumerate(self.layers): lt = self.cfg.layer_types[i] x = layer(x, ple[:, :, i, :], cs[lt][0], cs[lt][1], sliding if lt == "sliding_attention" else full) x = self.embedding_projection(self.norm(x)) # [1, S, 768] # masked mean pooling: weights sum to 1, so no large partial sums in fp16 if PRECISION_MODE == "ane": w = attention_mask * _recip(attention_mask.sum(-1, keepdim=True)) # [1, S] (count exact in fp16 up to 2048) pooled = torch.matmul(w[:, None, :], x)[:, 0, :] # [1, 768] u = pooled * _recip(torch.maximum(pooled.abs().amax(-1, keepdim=True), self.floor16)) return u * torch.rsqrt((u * u).sum(-1, keepdim=True)) xf = x.float() wf = attention_mask.float() pooled = (xf * wf[:, :, None]).sum(1) / wf.sum(-1, keepdim=True) return (pooled / pooled.norm(dim=-1, keepdim=True)).to(x.dtype)
def load_model(model_dir: str, dtype: torch.dtype, max_seq: int = 8192) -> EmbeddingGemma2Text: from safetensors import safe_open
cfg = Cfg(model_dir) m = EmbeddingGemma2Text(cfg, max_seq) sd = {} with safe_open(str(Path(model_dir) / "model.safetensors"), "pt") as f: for k in f.keys(): if not k.startswith("language_model."): continue t = f.get_tensor(k).float() k = k[len("language_model."):] if k == "embed_tokens.weight": t = t * math.sqrt(cfg.hidden) elif k == "ple.per_layer_model_projection.weight": k = "per_layer_model_projection.weight" t = t * cfg.hidden ** -0.5 elif k == "ple.per_layer_projection_norm.weight": k = "per_layer_projection_norm.weight" sd[k] = t.to(dtype) missing, unexpected = m.load_state_dict(sd, strict=False) missing = [k for k in missing if not any(s in k for s in ("cos_", "sin_", "eps16", "sqrt_eps16", "floor16", "mask_value", "one", "zero", "window"))] assert not missing and not unexpected, (missing, unexpected) return m.to(dtype).eval()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE)."""Export the EmbeddingGemma 2 text encoder to a Core AI `.aimodel` with one static entrypoint per sequence bucket.
# 1. download the original checkpoint: hf download google/embeddinggemma-2 --local-dir ./embeddinggemma-2 # 2. python3 -W ignore export.py --model-dir ./embeddinggemma-2 --out <bundle folder> --name embeddinggemma-2-text \ # --mode ane --buckets 32,64,128,256,512,1024,2048 (the released bundle; ~20 s on an M-series Mac)
The result is functionally identical to the released bundle (same outputs), not byte-identical: the converter givesinternal graph resources random names, so main.mlirb / main.hash differ, and the runtime writes metadata.json with theexport time and its own key order. config.json, MODEL-ATTRIBUTION.json (with --date 2026-10-12) and the tokenizerfiles come out byte-identical to the released ones.
One dynamic-S torch.export -> TorchConverter -> AIProgram.set_static_shape_config("main", {...}) which replaces`main` by `main_<S>` clones (static shapes are what keeps the graph on the Neural Engine).
Bundle layout: <out>/<name>.aimodel functions main_<S>: inputs input_ids [1,S] int32, attention_mask [1,S] fp16 -> embedding [1,768] fp16 <out>/tokenizer/ tokenizer.json + tokenizer_config.json (unchanged Gemma tokenizer) <out>/config.json buckets, prompts, special tokens, matryoshka dims, precision mode <out>/MODEL-ATTRIBUTION.json base model, checkpoint sha256, exporter versions, modification list
Requirements (see ../python/requirements.lock): Python 3.12, torch 2.9.0, coreai-torch 0.4.3, coreai-opt 0.3.0,torchao 0.17.0, coreai-core 1.0.0b3, safetensors."""from __future__ import annotations
import argparseimport hashlibimport jsonimport osimport shutilimport subprocessimport sysimport timefrom pathlib import Path
import torch
LAB = Path(__file__).resolve().parentsys.path.insert(0, str(LAB))import eg2_text # noqa: E402
T0 = time.time()PROMPTS = { "document": "title: none | text: ", "search_query": "task: search result | query: ", "question_answering": "task: question answering | query: ", "fact_checking": "task: fact checking | query: ", "code_retrieval": "task: code retrieval | query: ", "classification": "task: classification | query: ", "clustering": "task: clustering | query: ", "sentence_similarity": "task: sentence similarity | query: ",}
def log(msg: str) -> None: print(f"[{time.time() - T0:7.1f}s] {msg}", flush=True)
def sha256_file(p: Path) -> str: h = hashlib.sha256() with open(p, "rb") as f: for chunk in iter(lambda: f.read(1 << 24), b""): h.update(chunk) return h.hexdigest()
def dir_bytes(p: Path) -> int: return sum(f.stat().st_size for f in p.rglob("*") if f.is_file()) if p.is_dir() else p.stat().st_size
def main() -> None: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--model-dir", required=True, help="folder with the original google/embeddinggemma-2 checkpoint " "(config.json, model.safetensors, tokenizer.json, tokenizer_config.json)") ap.add_argument("--hf-id", default="google/embeddinggemma-2") ap.add_argument("--out", required=True) ap.add_argument("--name", default=None) ap.add_argument("--mode", default="ane", choices=["ane", "gpu"]) ap.add_argument("--buckets", default="32,64,128,256,512,1024,2048") ap.add_argument("--num-layers", type=int, default=None, help="smoke export with the first N layers") ap.add_argument("--debug-info", action="store_true") ap.add_argument("--no-static", action="store_true", help="keep the single dynamic-S `main` (GPU only)") ap.add_argument("--date", default=None, help="port date written to MODEL-ATTRIBUTION.json (default: today)") args = ap.parse_args() buckets = sorted(int(b) for b in args.buckets.split(",")) max_seq = max(buckets) out = Path(args.out) name = args.name or out.name eg2_text.PRECISION_MODE = args.mode log(f"args {vars(args)}")
model = eg2_text.load_model(args.model_dir, torch.float16, max_seq=max_seq) if args.num_layers: model.layers = model.layers[: args.num_layers] model.cfg.layer_types = model.cfg.layer_types[: args.num_layers] log(f"model loaded: {sum(p.numel() for p in model.parameters()) / 1e6:.1f} M params, mode={args.mode}")
import coreai_torch from torch.export import Dim
S0 = buckets[min(1, len(buckets) - 1)] ref = {"input_ids": torch.zeros(1, S0, dtype=torch.int32), "attention_mask": torch.ones(1, S0, dtype=torch.float16)} ref["input_ids"][0, :8] = torch.tensor([2, 8071, 236787, 3927, 1354, 1109, 7609, 1], dtype=torch.int32) ref["attention_mask"][0, 8:] = 0 S = Dim("S", min=min(8, buckets[0]), max=max_seq) dyn = {"input_ids": {1: S}, "attention_mask": {1: S}} with torch.no_grad(): ep = torch.export.export(model, args=(), kwargs=ref, dynamic_shapes=dyn) log("torch.export done") ep = ep.run_decompositions(coreai_torch.get_decomp_table()) log("decomposed") mode = coreai_torch.TorchConverter.Mode.DEBUG if args.debug_info else coreai_torch.TorchConverter.Mode.RELEASE conv = coreai_torch.TorchConverter(mode=mode) conv.add_exported_program(ep, input_names=["input_ids", "attention_mask"], output_names=["embedding"], entrypoint_name="main") prog = conv.to_coreai() log("converted to AIProgram") functions = ["main"] if not args.no_static: cfg = {f'"{b}"': {"input_ids": (1, b), "attention_mask": (1, b)} for b in buckets} prog.set_static_shape_config("main", cfg) functions = [f"main_{b}" for b in buckets] log(f"static shape specializations: {functions}")
from coreai.runtime import AIModelAssetMetadata
out.mkdir(parents=True, exist_ok=True) aimodel = out / f"{name}.aimodel" shutil.rmtree(aimodel, ignore_errors=True) meta = AIModelAssetMetadata() meta.author = "Gemma Team (Google); Core AI port" meta.license = "Apache-2.0" meta.model_description = (f"EmbeddingGemma 2 text encoder ({args.hf_id}) with mean pooling, 768-d projection and L2 " f"normalization in-graph; precision mode {args.mode}; sequence buckets {buckets}.") meta.creation_date = int(time.time()) prog.save_asset(aimodel, meta) log(f"saved {aimodel} ({dir_bytes(aimodel) / 2**20:.1f} MiB)")
# ---- bundle --------------------------------------------------------------------- tok_dir = out / "tokenizer" tok_dir.mkdir(exist_ok=True) for f in ("tokenizer.json", "tokenizer_config.json"): shutil.copy(Path(args.model_dir) / f, tok_dir / f) cfg_json = { "base_model": args.hf_id, "format": "coreai", "asset": aimodel.name, "functions": functions, "precision_mode": args.mode, "buckets": buckets, "max_tokens": max_seq, "inputs": {"input_ids": "[1, S] int32 (bos=2 ... eos=1, right-padded with pad=0)", "attention_mask": "[1, S] float16 (1 = token, 0 = padding)"}, "output": {"embedding": "[1, 768] float16, L2-normalized; Matryoshka: keep the first 512/256/128 and re-normalize"}, "hidden_size": 512, "embedding_dim": 768, "matryoshka_dims": [768, 512, 256, 128], "bos_token_id": 2, "eos_token_id": 1, "pad_token_id": 0, "prompts": PROMPTS, "tokenizer": "tokenizer/tokenizer.json (unchanged Gemma tokenizer; the runner adds <bos> and <eos>)", } (out / "config.json").write_text(json.dumps(cfg_json, indent=2) + "\n") import coreai_torch as ct from importlib.metadata import version as pkg_version
rescale = ("fp16 everywhere (Neural Engine): every RMSNorm and the final L2 normalization divide the row by its " "max-abs value before squaring, so activations (~2000) never overflow fp16; in the model code every " "division is expressed as rsqrt(x)*rsqrt(x) (the Neural Engine compiler rejects divide)." if args.mode == "ane" else "fp16 weights and matmuls; RMSNorm, softmax, pooling and L2 normalization in fp32.") attribution = { "name": "EmbeddingGemma-2-CoreAI-text", "version": "1.0", "base_model": args.hf_id, "base_model_author": "Gemma Team, Google DeepMind", "base_model_source": f"https://huggingface.co/{args.hf_id}", "base_model_license": "Apache-2.0", "base_model_terms": "Deployments must adhere to the Gemma Prohibited Use Policy: https://ai.google.dev/gemma/prohibited_use_policy", "base_model_file_sha256": {"model.safetensors": sha256_file(Path(args.model_dir) / "model.safetensors")}, "port": { "copyright": "2026 Everpaper", "license": "Apache-2.0 (see LICENSE); attribution and modification notice in NOTICE", "date": args.date or time.strftime("%Y-%m-%d"), "format": "Apple Core AI .aimodel (macOS 27), fp16, static sequence buckets", "exporter": { "coreai_torch": ct.__version__, "coreai_core": pkg_version("coreai-core"), "torch": torch.__version__.split("+")[0], "recipe": f"export/export.py --mode {args.mode} --buckets {','.join(map(str, buckets))}", "global_attention": eg2_text.GLOBAL_ATTN_MODE, "gqa": eg2_text.GQA_MODE, }, }, "modifications": [ "Text encoder only: the vision and audio encoders of the multimodal checkpoint are not included.", "Architecture re-authored in plain PyTorch for Core AI export; weights are the original ones, rounded from bf16 to fp16.", "The sqrt(512) token-embedding scale and the 512^-0.5 per-layer-embedding scale are folded into the tables.", "The four global (full-attention, single-kv-head) layers compute attention as a plain 4-D matmul with k/v " "broadcast over the heads; the sliding-window layers use a 5-D grouped matmul. Mathematically identical to " "the reference (fp32 max abs diff 3e-7).", rescale, "Bidirectional attention masks (padding + sliding window 512) are built inside the graph from attention_mask; " "the additive mask value is -10000 (finite).", "Masked mean pooling, the 512->768 embedding projection and the L2 normalization run inside the graph; the " "output is the final sentence embedding.", f"Fixed sequence buckets {', '.join(map(str, buckets))} as functions main_<S>; input longer than {max_seq} " "tokens must be truncated or chunked by the caller (the original model accepts 8192).", ], } if args.num_layers: attribution["port"]["exporter"]["num_layers"] = args.num_layers (out / "MODEL-ATTRIBUTION.json").write_text(json.dumps(attribution, indent=2) + "\n") log(f"bundle written: {out} ({dir_bytes(out) / 2**20:.1f} MiB)") try: print(subprocess.run(["xcrun", "coreai-build", "inspect", str(aimodel), "--no-metadata"], capture_output=True, text=True, timeout=120).stdout[:3000]) except Exception as e: # noqa: BLE001 log(f"inspect failed: {e}") log("DONE")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536{ "name": "EmbeddingGemma-2-CoreAI-text", "version": "1.0", "base_model": "google/embeddinggemma-2", "base_model_author": "Gemma Team, Google DeepMind", "base_model_source": "https://huggingface.co/google/embeddinggemma-2", "base_model_license": "Apache-2.0", "base_model_terms": "Deployments must adhere to the Gemma Prohibited Use Policy: https://ai.google.dev/gemma/prohibited_use_policy", "base_model_file_sha256": { "model.safetensors": "197a32965d4b1105faf060417baa899e193fb73cd401f42ec9295234d5553d79" }, "port": { "copyright": "2026 Everpaper", "license": "Apache-2.0 (see LICENSE); attribution and modification notice in NOTICE", "date": "2026-10-12", "format": "Apple Core AI .aimodel (macOS 27), fp16, static sequence buckets", "exporter": { "coreai_torch": "0.4.3", "coreai_core": "1.0.0b3", "torch": "2.9.0", "recipe": "export/export.py --mode ane --buckets 32,64,128,256,512,1024,2048", "global_attention": "mqa4d", "gqa": "grouped5d" } }, "modifications": [ "Text encoder only: the vision and audio encoders of the multimodal checkpoint are not included.", "Architecture re-authored in plain PyTorch for Core AI export; weights are the original ones, rounded from bf16 to fp16.", "The sqrt(512) token-embedding scale and the 512^-0.5 per-layer-embedding scale are folded into the tables.", "The four global (full-attention, single-kv-head) layers compute attention as a plain 4-D matmul with k/v broadcast over the heads; the sliding-window layers use a 5-D grouped matmul. Mathematically identical to the reference (fp32 max abs diff 3e-7).", "fp16 everywhere (Neural Engine): every RMSNorm and the final L2 normalization divide the row by its max-abs value before squaring, so activations (~2000) never overflow fp16; in the model code every division is expressed as rsqrt(x)*rsqrt(x) (the Neural Engine compiler rejects divide).", "Bidirectional attention masks (padding + sliding window 512) are built inside the graph from attention_mask; the additive mask value is -10000 (finite).", "Masked mean pooling, the 512->768 embedding projection and the L2 normalization run inside the graph; the output is the final sentence embedding.", "Fixed sequence buckets 32, 64, 128, 256, 512, 1024, 2048 as functions main_<S>; input longer than 2048 tokens must be truncated or chunked by the caller (the original model accepts 8192)." ]}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354{ "base_model": "google/embeddinggemma-2", "format": "coreai", "asset": "embeddinggemma-2-text.aimodel", "functions": [ "main_32", "main_64", "main_128", "main_256", "main_512", "main_1024", "main_2048" ], "precision_mode": "ane", "buckets": [ 32, 64, 128, 256, 512, 1024, 2048 ], "max_tokens": 2048, "inputs": { "input_ids": "[1, S] int32 (bos=2 ... eos=1, right-padded with pad=0)", "attention_mask": "[1, S] float16 (1 = token, 0 = padding)" }, "output": { "embedding": "[1, 768] float16, L2-normalized; Matryoshka: keep the first 512/256/128 and re-normalize" }, "hidden_size": 512, "embedding_dim": 768, "matryoshka_dims": [ 768, 512, 256, 128 ], "bos_token_id": 2, "eos_token_id": 1, "pad_token_id": 0, "prompts": { "document": "title: none | text: ", "search_query": "task: search result | query: ", "question_answering": "task: question answering | query: ", "fact_checking": "task: fact checking | query: ", "code_retrieval": "task: code retrieval | query: ", "classification": "task: classification | query: ", "clustering": "task: clustering | query: ", "sentence_similarity": "task: sentence similarity | query: " }, "tokenizer": "tokenizer/tokenizer.json (unchanged Gemma tokenizer; the runner adds <bos> and <eos>)"}1uӄ1��$q�T�3Wq
0\�hr���υ�欝F�123version https://git-lfs.github.com/spec/v1oid sha256:75d38431abcd2471b0540c863357710d305cb1687295f5b0cf859de6ac9d46ebsize 63152996012345678{ "assetVersion" : "2.0", "producer" : "coreai-core 1.0.0b3", "license" : "Apache-2.0", "author" : "Gemma Team (Google); Core AI port", "creationDate" : "20261012T021653Z", "description" : "EmbeddingGemma 2 text encoder (google\/embeddinggemma-2) with mean pooling, 768-d projection and L2 normalization in-graph; precision mode ane; sequence buckets [32, 64, 128, 256, 512, 1024, 2048]."}123version https://git-lfs.github.com/spec/v1oid sha256:4d777ef5bdc1aa36227abdfb77c3e49e7b9c892d16e1b6bda41c393504828be4size 32170510123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657{ "audio_token": "<|audio|>", "backend": "tokenizers", "boa_token": "<|audio>", "boi_token": "<|image>", "bos_token": "<bos>", "eoa_token": "<audio|>", "eoc_token": "<channel|>", "eoi_token": "<image|>", "eos_token": "<eos>", "eot_token": "<turn|>", "escape_token": "<|\"|>", "etc_token": "<tool_call|>", "etd_token": "<tool|>", "etr_token": "<tool_response|>", "extra_special_tokens": [ "<|video|>" ], "image_token": "<|image|>", "is_local": true, "local_files_only": false, "mask_token": "<mask>", "model_max_length": 1000000000000000019884624838656, "model_specific_special_tokens": { "audio_token": "<|audio|>", "boa_token": "<|audio>", "boi_token": "<|image>", "eoa_token": "<audio|>", "eoc_token": "<channel|>", "eoi_token": "<image|>", "eot_token": "<turn|>", "escape_token": "<|\"|>", "etc_token": "<tool_call|>", "etd_token": "<tool|>", "etr_token": "<tool_response|>", "image_token": "<|image|>", "soc_token": "<|channel>", "sot_token": "<|turn>", "stc_token": "<|tool_call>", "std_token": "<|tool>", "str_token": "<|tool_response>", "think_token": "<|think|>", "video_token": "<|video|>" }, "pad_token": "<pad>", "padding_side": "right", "processor_class": "EmbeddingGemma2Processor", "soc_token": "<|channel>", "sot_token": "<|turn>", "stc_token": "<|tool_call>", "std_token": "<|tool>", "str_token": "<|tool_response>", "think_token": "<|think|>", "tokenizer_class": "GemmaTokenizer", "unk_token": "<unk>", "video_token": "<|video|>"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278#!/usr/bin/env python3# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE)."""EmbeddingGemma 2 (text) on Apple Core AI, macOS 27 — Python runner (library + CLI).
Library:
from embeddinggemma2_coreai import EmbeddingGemma2CoreAI, remove_own_mpsgraph_temp_files m = EmbeddingGemma2CoreAI("../model", compute="ane") # ane | gpu | cpu | default m.prewarm() # optional: one call per bucket q = m.embed("When do the cloud credits expire?", kind="search_query") # 768-d, L2-normalized d = m.embed("The credits expire on the last day of March.", kind="document", dim=256) del m remove_own_mpsgraph_temp_files() # see "Temp files" in the README
CLI:
python3 embeddinggemma2_coreai.py embed ../model [--compute ane] [--kind document] [--dim 768] "text" ... python3 embeddinggemma2_coreai.py smoke ../model ../tests/smoke-reference.json [--compute ane] [--cleanup-temp] python3 embeddinggemma2_coreai.py tokcheck ../model ../tests/tokenizer-cases.json python3 embeddinggemma2_coreai.py latency ../model [--compute ane] [--trials 50] python3 embeddinggemma2_coreai.py prewarm ../model [--compute ane]
Tokenization uses the Hugging Face `tokenizers` library on the bundle's tokenizer.json (the reference tokenizer)."""from __future__ import annotations
import argparseimport asyncioimport ctypesimport jsonimport osimport sysimport timefrom pathlib import Path
import numpy as np
_PROCESS_START = time.time()
class EmbeddingGemma2CoreAI: """Loads the bundle folder (config.json, tokenizer/, the .aimodel) and embeds texts."""
def __init__(self, bundle: str | os.PathLike, compute: str = "ane", quiet: bool = True): import coreai.runtime as rt from tokenizers import Tokenizer
self._rt = rt self.bundle = Path(bundle) self.config = json.loads((self.bundle / "config.json").read_text(encoding="utf-8")) t = time.perf_counter() self.tokenizer = Tokenizer.from_file(str(self.bundle / "tokenizer" / "tokenizer.json")) self.tokenizer_load_s = time.perf_counter() - t self.compute = compute if compute == "cpu": opts = rt.SpecializationOptions.cpu_only() elif compute == "gpu": opts = rt.SpecializationOptions.from_preferred_compute_unit_kind(rt.ComputeUnitKind.gpu()) elif compute == "ane": opts = rt.SpecializationOptions.from_preferred_compute_unit_kind(rt.ComputeUnitKind.neural_engine()) elif compute == "default": opts = rt.SpecializationOptions.default() else: raise ValueError("compute must be ane | gpu | cpu | default") t = time.perf_counter() self.model = asyncio.run(rt.AIModel.load(self.bundle / self.config["asset"], opts)) self.functions: dict[int, object] = {} for name in self.config["functions"]: self.functions[int(name.rsplit("_", 1)[1])] = self.model.load_function(name) self.buckets = sorted(self.functions) self.load_s = time.perf_counter() - t if not quiet: print(f"loaded {self.bundle.name} compute={compute} buckets={self.buckets} in {self.load_s:.1f}s", flush=True)
# ---- tokenization ------------------------------------------------------------------------------------- def tokenize(self, text: str, kind: str | None = "document") -> list[int]: """<bos> + prompt prefix + text + <eos>, truncated to the largest bucket (longer input is cut).""" prefix = self.config["prompts"].get(kind, "") if kind else "" ids = self.tokenizer.encode(prefix + text, add_special_tokens=False).ids max_body = self.buckets[-1] - 2 if len(ids) > max_body: ids = ids[:max_body] return [self.config["bos_token_id"]] + ids + [self.config["eos_token_id"]]
def bucket(self, n_tokens: int) -> int: for b in self.buckets: if b >= n_tokens: return b raise ValueError(f"{n_tokens} tokens exceeds the largest bucket {self.buckets[-1]}")
# ---- inference ---------------------------------------------------------------------------------------- def embed_ids(self, ids: list[int], bucket: int | None = None) -> np.ndarray: """Runs main_<bucket> on ids (with <bos>/<eos>), right-padded with <pad>. Returns the raw 768-d output.""" S = bucket or self.bucket(len(ids)) if len(ids) > S: raise ValueError(f"{len(ids)} ids do not fit bucket {S}") x = np.full((1, S), self.config["pad_token_id"], dtype=np.int32) x[0, : len(ids)] = ids m = np.zeros((1, S), dtype=np.float16) m[0, : len(ids)] = 1 out = asyncio.run(self.functions[S](inputs={"input_ids": self._rt.NDArray(x), "attention_mask": self._rt.NDArray(m)})) return np.array(out["embedding"].numpy(), dtype=np.float32).reshape(-1)
def embed(self, text: str, kind: str | None = "document", dim: int = 768) -> np.ndarray: """L2-normalized embedding. dim 768, or a Matryoshka size (512/256/128): leading dims, re-normalized.""" if dim not in self.config["matryoshka_dims"]: raise ValueError(f"dim must be one of {self.config['matryoshka_dims']}") v = self.embed_ids(self.tokenize(text, kind))[:dim] return v / max(float(np.linalg.norm(v)), 1e-12)
def prewarm(self) -> float: """One call per bucket so the first real call pays no specialization/first-call cost. Returns seconds.""" t = time.perf_counter() for S in self.buckets: self.embed_ids([self.config["bos_token_id"], self.config["eos_token_id"]], S) return time.perf_counter() - t
def close(self) -> None: self.functions.clear() self.model = None
# ---- temp-file hygiene ----------------------------------------------------------------------------------def mpsgraph_temp_root() -> Path: import tempfile
return Path(os.environ.get("TMPDIR") or tempfile.gettempdir()) / "com.apple.MetalPerformanceShadersGraph"
def remove_own_mpsgraph_temp_files(pid: int | None = None, since: float | None = None) -> tuple[int, int]: """Each Neural Engine load of this model makes MPSGraph write ~358 MB of compiled-graph copies into $TMPDIR/com.apple.MetalPerformanceShadersGraph/mpsgraph-<pid>-<date>-<n>/, and the Python runtime (coreai.runtime, macOS 27.0) does NOT delete them when the model is released or the interpreter exits. This removes the folders of the CURRENT process only (its pid, created after it started). Call it after the model object has been released, or at exit. Returns (folders, bytes).""" import shutil
pid = pid or os.getpid() since = _PROCESS_START - 2 if since is None else since root = mpsgraph_temp_root() folders = nbytes = 0 if not root.is_dir(): return 0, 0 for d in root.iterdir(): if not d.name.startswith(f"mpsgraph-{pid}-") or not d.is_dir(): continue try: if d.stat().st_birthtime < since: continue # left by an older process that happened to have the same pid nbytes += sum(f.stat().st_size for f in d.rglob("*") if f.is_file()) shutil.rmtree(d) folders += 1 except OSError: pass return folders, nbytes
# ---- process accounting helpers (macOS) ------------------------------------------------------------------def _rusage_v6(): class RU6(ctypes.Structure): _fields_ = [("uuid", ctypes.c_uint8 * 16)] + [(f"f{i}", ctypes.c_uint64) for i in range(47)] + [("reserved", ctypes.c_uint64 * 9)]
libc = ctypes.CDLL("/usr/lib/libSystem.B.dylib") r = RU6() return r if libc.proc_pid_rusage(os.getpid(), 6, ctypes.byref(r)) == 0 else None
def footprints_mb() -> tuple[float, float]: """(lifetime max physical footprint, Neural Engine footprint) in MB; the latter is > 0 only if the ANE ran.""" r = _rusage_v6() if r is None: return -1.0, -1.0 return r.f28 / 2**20, r.f44 / 2**20 # ri_lifetime_max_phys_footprint, ri_neural_footprint
def cosine(a, b) -> float: a = np.asarray(a, np.float64) b = np.asarray(b, np.float64) return float(a @ b / (np.linalg.norm(a) * np.linalg.norm(b)))
# ---- CLI -------------------------------------------------------------------------------------------------def cmd_embed(a) -> int: m = EmbeddingGemma2CoreAI(a.bundle, a.compute) for t in a.args: v = m.embed(t, None if a.kind == "none" else a.kind, a.dim) print(json.dumps({"text": t, "kind": a.kind, "dim": int(v.shape[0]), "embedding": [round(float(x), 6) for x in v]}, ensure_ascii=False)) m.close() return 0
def cmd_smoke(a) -> int: ref = json.loads(Path(a.args[0]).read_text(encoding="utf-8")) m = EmbeddingGemma2CoreAI(a.bundle, a.compute) print(f"loaded in {m.load_s:.1f} s (tokenizer {m.tokenizer_load_s:.2f} s), compute={a.compute}, buckets={m.buckets}") ok = True for c in ref["cases"]: v = m.embed(c["text"], c["kind"]) cos = cosine(v, c["embedding"]) passed = cos >= ref["threshold_cosine"] and bool(np.all(np.isfinite(v))) ok = ok and passed print(f"{'PASS' if passed else 'FAIL'} cosine {cos:.6f} (threshold {ref['threshold_cosine']}) {c['kind']}: {c['text'][:60]}") fp, nf = footprints_mb() print(f"peak footprint {fp:.0f} MB, neural footprint {nf:.0f} MB") print("SMOKE PASS" if ok else "SMOKE FAIL") m.close() return 0 if ok else 1
def cmd_tokcheck(a) -> int: from tokenizers import Tokenizer
cases = json.loads(Path(a.args[0]).read_text(encoding="utf-8")) tok = Tokenizer.from_file(str(Path(a.bundle) / "tokenizer" / "tokenizer.json")) prefixes = {"q": "task: search result | query: ", "d": "title: none | text: ", "raw": ""} same = 0 t = time.perf_counter() for c in cases: ids = [2] + tok.encode(prefixes[c["kind"]] + c["text"], add_special_tokens=False).ids + [1] same += ids == c["ids"] print(f"TOKCHECK {same}/{len(cases)} identical (tokenizers {__import__('tokenizers').__version__}) in {time.perf_counter() - t:.2f} s") return 0 if same == len(cases) else 1
def cmd_latency(a) -> int: m = EmbeddingGemma2CoreAI(a.bundle, a.compute) print(f"loaded in {m.load_s:.1f} s, compute={a.compute}; prewarm {m.prewarm():.2f} s") print("bucket p50 ms min ms p90 ms CPU ms/call tok/s") for S in m.buckets: ids = [m.config["bos_token_id"]] + [1000 + (i * 7919) % 200000 for i in range(1, S - 1)] + [m.config["eos_token_id"]] for _ in range(3): m.embed_ids(ids, S) ts = [] c0 = time.process_time() for _ in range(a.trials): t = time.perf_counter() m.embed_ids(ids, S) ts.append((time.perf_counter() - t) * 1000) cpu = (time.process_time() - c0) / a.trials * 1000 p50 = float(np.median(ts)) print(f"{S:5d} {p50:7.2f} {min(ts):7.2f} {float(np.percentile(ts, 90)):7.2f} {cpu:11.2f} {S / p50 * 1000:6.0f}") fp, nf = footprints_mb() print(f"peak footprint {fp:.0f} MB, neural footprint {nf:.0f} MB ({'Neural Engine in use' if nf > 0 else 'Neural Engine NOT used'})") m.close() return 0
def cmd_prewarm(a) -> int: t0 = time.perf_counter() m = EmbeddingGemma2CoreAI(a.bundle, a.compute) w = m.prewarm() print(f"load {m.load_s:.1f} s + prewarm {w:.2f} s = ready in {time.perf_counter() - t0:.1f} s") m.close() return 0
def main() -> None: ap = argparse.ArgumentParser(description="EmbeddingGemma 2 (text) on Apple Core AI") ap.add_argument("cmd", choices=["embed", "smoke", "tokcheck", "latency", "prewarm"]) ap.add_argument("bundle", help="the model folder (config.json, tokenizer/, *.aimodel)") ap.add_argument("args", nargs="*", help="texts (embed) or a json file (smoke, tokcheck)") ap.add_argument("--compute", default="ane", choices=["ane", "gpu", "cpu", "default"]) ap.add_argument("--kind", default="document", help="prompt kind from config.json (document, search_query, ...) or none") ap.add_argument("--dim", type=int, default=768) ap.add_argument("--trials", type=int, default=50) ap.add_argument("--cleanup-temp", action="store_true", help="remove this process's MPSGraph temp folders at exit") a = ap.parse_args() code = {"embed": cmd_embed, "smoke": cmd_smoke, "tokcheck": cmd_tokcheck, "latency": cmd_latency, "prewarm": cmd_prewarm}[a.cmd](a) if a.cleanup_temp: import gc
gc.collect() n, b = remove_own_mpsgraph_temp_files() print(f"cleanup-temp: removed {n} MPSGraph temp folder(s), {b / 2**20:.1f} MB") sys.exit(code)
if __name__ == "__main__": main()12345678910111213141516171819# Exact versions of the environments this release was validated in (macOS 27.0, Apple silicon).# Runtime (python/), Python 3.12 or 3.14:coreai-core==1.0.0b3numpy==2.4.6tokenizers==0.23.3huggingface-hub==2.2.0# Exporter (export/), on top of the runtime, Python 3.12:coreai-torch==0.4.3coreai-opt==0.3.0torch==2.9.0torchao==0.17.0safetensors==0.8.0ml-dtypes==0.6.0networkx==3.7scipy==1.18.1packaging==26.3# Reference vectors (tests/make_smoke_reference.py), a separate environment:# sentence-transformers==6.1.0 transformers==5.19.0 torch==2.14.1 torchvision==0.29.1 pillow tokenizers==0.23.3 numpy==2.5.3# (pillow and torchvision are needed because the checkpoint's processor is multimodal, even for text-only use)1234567# Runtime requirements for python/ (macOS 27, Apple silicon; validated with Python 3.12 and 3.14).# coreai-core provides the `coreai.runtime` module (Apple's Core AI Python runtime); install it from the same# distribution that provides coreai-torch. The exact versions this release was validated with are in# requirements.lock.coreai-core==1.0.0b3numpy>=2.0,<2.5tokenizers>=0.22,<0.241234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162#!/usr/bin/env python3# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE)."""Smoke test: load the bundle, embed three fictional sentences, compare with the shipped fp32 reference vectors.
python3 smoke_test.py [--bundle ../model] [--reference ../tests/smoke-reference.json] [--compute ane] [--cleanup-temp]
Exit code 0 = every cosine >= threshold (0.999) and all values finite. The first run of a new executable (here: yourpython binary) specializes the model for the Neural Engine: expect up to ~145 s (139-144 s measured). Later runs loadin 0.4-1.5 s."""from __future__ import annotations
import argparseimport sysfrom pathlib import Path
HERE = Path(__file__).resolve().parentsys.path.insert(0, str(HERE))from embeddinggemma2_coreai import EmbeddingGemma2CoreAI, cosine, footprints_mb, remove_own_mpsgraph_temp_files # noqa: E402
def main() -> int: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--bundle", default=str(HERE.parent / "model")) ap.add_argument("--reference", default=str(HERE.parent / "tests" / "smoke-reference.json")) ap.add_argument("--compute", default="ane", choices=["ane", "gpu", "cpu", "default"]) ap.add_argument("--cleanup-temp", action="store_true", help="remove this process's MPSGraph temp folders at exit") a = ap.parse_args() import json
ref = json.loads(Path(a.reference).read_text(encoding="utf-8")) m = EmbeddingGemma2CoreAI(a.bundle, a.compute) print(f"loaded in {m.load_s:.1f} s (tokenizer {m.tokenizer_load_s:.2f} s), compute={a.compute}, buckets={m.buckets}") ok = True for c in ref["cases"]: v = m.embed(c["text"], c["kind"]) cos = cosine(v, c["embedding"]) passed = cos >= ref["threshold_cosine"] and all(map(lambda x: x == x and abs(x) != float("inf"), v.tolist())) ok = ok and passed print(f"{'PASS' if passed else 'FAIL'} cosine {cos:.6f} (threshold {ref['threshold_cosine']}) {c['kind']}: {c['text'][:60]}") # Matryoshka prefixes must stay close to the truncated+renormalized reference too for dim in (512, 256, 128): c = ref["cases"][1] v = m.embed(c["text"], c["kind"], dim=dim) r = c["embedding"][:dim] print(f"{'PASS' if cosine(v, r) >= ref['threshold_cosine'] else 'FAIL'} cosine {cosine(v, r):.6f} at dim {dim}") fp, nf = footprints_mb() print(f"peak footprint {fp:.0f} MB, neural footprint {nf:.0f} MB ({'Neural Engine in use' if nf > 0 else 'Neural Engine not used'})") print("SMOKE PASS" if ok else "SMOKE FAIL") m.close() del m if a.cleanup_temp: import gc
gc.collect() n, b = remove_own_mpsgraph_temp_files() print(f"cleanup-temp: removed {n} MPSGraph temp folder(s), {b / 2**20:.1f} MB") return 0 if ok else 1
if __name__ == "__main__": sys.exit(main())1234567891011121314151617181920212223242526// swift-tools-version: 6.0// Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE).import PackageDescription
let package = Package( name: "EmbeddingGemma2CoreAI", platforms: [.macOS("27.0")], products: [ .library(name: "EmbeddingGemma2CoreAI", targets: ["EmbeddingGemma2CoreAI"]), .executable(name: "eg2-cli", targets: ["eg2-cli"]), ], targets: [ // Library: exact Gemma tokenizer + Core AI embedder. No third-party dependencies. .target( name: "EmbeddingGemma2CoreAI", path: "Sources/EmbeddingGemma2CoreAI", linkerSettings: [.linkedFramework("CoreAI")] ), // Command-line tool: embed | smoke | tokcheck | latency | prewarm | cleanup-temp .executableTarget( name: "eg2-cli", dependencies: ["EmbeddingGemma2CoreAI"], path: "Sources/eg2-cli" ), ])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197// Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../../../LICENSE).//// EmbeddingGemma 2 (text) on Apple Core AI, macOS 27. Loads the bundle folder (config.json, tokenizer/, the// .aimodel), tokenizes exactly like Hugging Face `tokenizers`, pads to the smallest sequence bucket, runs the// matching `main_<S>` function and returns the L2-normalized 768-d embedding (or a Matryoshka prefix).
import CoreAIimport Foundation
public struct BundleConfig: Decodable, Sendable { public let asset: String public let functions: [String] public let buckets: [Int] public let max_tokens: Int public let embedding_dim: Int public let matryoshka_dims: [Int] public let prompts: [String: String] public let bos_token_id: Int32 public let eos_token_id: Int32 public let pad_token_id: Int32}
public enum ComputePreference: String, Sendable { case neuralEngine = "ane" case gpu case cpu case `default`}
public struct EmbedderError: Error, CustomStringConvertible { public let description: String}
public final class EmbeddingGemma2 { public let bundleURL: URL public let config: BundleConfig public let tokenizer: GemmaTokenizer public let compute: ComputePreference /// Wall-clock seconds spent in `AIModel(contentsOf:)` + `loadFunction` (specialization). 0.4–1.5 s warm; /// the first load by an executable that has not loaded this bundle before takes up to ~145 s (see README). public let loadSeconds: Double public let tokenizerLoadSeconds: Double
private let model: AIModel private let functions: [Int: InferenceFunction] // bucket -> main_<S> public var buckets: [Int] { functions.keys.sorted() }
public init(bundle: URL, compute: ComputePreference = .neuralEngine) async throws { bundleURL = bundle self.compute = compute config = try JSONDecoder().decode(BundleConfig.self, from: Data(contentsOf: bundle.appendingPathComponent("config.json"))) var t0 = Date() tokenizer = try GemmaTokenizer(contentsOf: bundle.appendingPathComponent("tokenizer/tokenizer.json")) tokenizerLoadSeconds = Date().timeIntervalSince(t0) guard tokenizer.bosID == config.bos_token_id, tokenizer.eosID == config.eos_token_id, tokenizer.padID == config.pad_token_id else { throw EmbedderError(description: "tokenizer special ids do not match config.json") } t0 = Date() let options: SpecializationOptions switch compute { case .gpu: options = SpecializationOptions(preferredComputeUnitKind: .gpu) case .cpu: options = SpecializationOptions.cpuOnly case .neuralEngine: options = SpecializationOptions(preferredComputeUnitKind: .neuralEngine) case .default: options = SpecializationOptions.default } model = try await AIModel(contentsOf: bundle.appendingPathComponent(config.asset), options: options) var fns: [Int: InferenceFunction] = [:] for name in config.functions { guard let s = Int(name.split(separator: "_").last ?? ""), let fn = try model.loadFunction(named: name) else { throw EmbedderError(description: "function \(name) missing from \(config.asset)") } fns[s] = fn } functions = fns loadSeconds = Date().timeIntervalSince(t0) }
// MARK: - Tokenization
/// `<bos>` + prompt prefix + text + `<eos>`, truncated to the largest bucket (2048 tokens: longer input is cut). /// `kind` is a key of `config.prompts` ("document", "search_query", ...); nil = no prefix. public func tokenize(_ text: String, kind: String? = "document") -> [Int32] { let prefix = kind.flatMap { config.prompts[$0] } ?? "" var ids = tokenizer.encode(prefix + text) let maxBody = (buckets.last ?? 32) - 2 if ids.count > maxBody { ids = Array(ids.prefix(maxBody)) } return [config.bos_token_id] + ids + [config.eos_token_id] }
public func bucket(forTokenCount n: Int) throws -> Int { guard let b = buckets.first(where: { $0 >= n }) else { throw EmbedderError(description: "\(n) tokens exceeds the largest bucket \(buckets.last ?? 0)") } return b }
// MARK: - Inference
/// Runs `main_<bucket>` on already-tokenized ids (with `<bos>`/`<eos>`), right-padded with `<pad>`. /// Returns the raw 768-d output (already L2-normalized by the graph), as Float. public func embed(ids: [Int32], bucket: Int? = nil) async throws -> [Float] { let S = try bucket ?? self.bucket(forTokenCount: ids.count) guard let fn = functions[S] else { throw EmbedderError(description: "no function for bucket \(S)") } guard ids.count <= S else { throw EmbedderError(description: "\(ids.count) ids do not fit bucket \(S)") } var inputIDs = NDArray(shape: [1, S], scalarType: .int32) var mask = NDArray(shape: [1, S], scalarType: .float16) do { var v = inputIDs.mutableView(as: Int32.self) var m = mask.mutableView(as: Float16.self) let pad = config.pad_token_id v.withUnsafeMutablePointer { p, _, _ in for i in 0..<S { p[i] = i < ids.count ? ids[i] : pad } } m.withUnsafeMutablePointer { p, _, _ in for i in 0..<S { p[i] = i < ids.count ? 1 : 0 } } } var out = try await fn.run(inputs: ["input_ids": inputIDs, "attention_mask": mask]) guard let emb = out.remove("embedding")?.ndArray else { throw EmbedderError(description: "output 'embedding' missing") } let dim = config.embedding_dim var result = [Float](repeating: 0, count: dim) let view = emb.view(as: Float16.self) view.withUnsafePointer { p, _, _ in for i in 0..<dim { result[i] = Float(p[i]) } } return result }
/// Embeds one text. `dim` 768 (full) or a Matryoshka size 512/256/128: the leading dims, re-normalized. public func embed(_ text: String, kind: String? = "document", dim: Int = 768) async throws -> [Float] { guard config.matryoshka_dims.contains(dim) else { throw EmbedderError(description: "dim must be one of \(config.matryoshka_dims)") } var v = Array(try await embed(ids: tokenize(text, kind: kind)).prefix(dim)) let norm = max(sqrt(v.reduce(0) { $0 + $1 * $1 }), 1e-12) for i in v.indices { v[i] /= norm } return v }
/// Runs every bucket once so the first real call pays no specialization/first-call cost. Call it right after /// init, off the main thread. Returns the seconds it took. @discardableResult public func prewarm() async throws -> Double { let t0 = Date() for S in buckets { _ = try await embed(ids: [config.bos_token_id, config.eos_token_id], bucket: S) } return Date().timeIntervalSince(t0) }
// MARK: - Temp-file hygiene
/// Each Neural Engine load of this model makes MPSGraph write ~358 MB of compiled graph copies into /// `$TMPDIR/com.apple.MetalPerformanceShadersGraph/mpsgraph-<pid>-<date>-<n>/`. With the Swift `CoreAI` /// framework (macOS 27.0) the runtime deletes that folder itself when the model is released, so this usually /// finds nothing; it is a safety net for a folder that survives (e.g. the process exits or the model is still /// alive). It removes only the folders that belong to the CURRENT process (its pid, created after it started). /// Call it after the `EmbeddingGemma2` instance has been released, or at exit. Returns the number of folders /// removed and their total bytes. @discardableResult public static func removeOwnMPSGraphTempFiles() -> (folders: Int, bytes: Int64) { let fm = FileManager.default let root = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("com.apple.MetalPerformanceShadersGraph") let prefix = "mpsgraph-\(getpid())-" let started = processStartDate() var folders = 0 var bytes: Int64 = 0 guard let entries = try? fm.contentsOfDirectory(at: root, includingPropertiesForKeys: [.creationDateKey]) else { return (0, 0) } for url in entries where url.lastPathComponent.hasPrefix(prefix) { if let started, let created = (try? url.resourceValues(forKeys: [.creationDateKey]))?.creationDate, created < started - 2 { continue // a folder left by an older process that happened to have the same pid } if let e = fm.enumerator(at: url, includingPropertiesForKeys: [.fileSizeKey]) { for case let f as URL in e { bytes += Int64((try? f.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 0) } } if (try? fm.removeItem(at: url)) != nil { folders += 1 } } return (folders, bytes) }
private static func processStartDate() -> Date? { var info = kinfo_proc() var size = MemoryLayout<kinfo_proc>.stride var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()] guard sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0) == 0 else { return nil } let tv = info.kp_proc.p_starttime return Date(timeIntervalSince1970: Double(tv.tv_sec) + Double(tv.tv_usec) / 1e6) }}
// MARK: - Small helpers for callers and tests
public func cosineSimilarity(_ a: [Float], _ b: [Float]) -> Double { var dot = 0.0, na = 0.0, nb = 0.0 for i in 0..<min(a.count, b.count) { dot += Double(a[i]) * Double(b[i]); na += Double(a[i]) * Double(a[i]); nb += Double(b[i]) * Double(b[i]) } return na > 0 && nb > 0 ? dot / (sqrt(na) * sqrt(nb)) : 0}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202// Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../../../LICENSE).//// A small, exact JSON reader, written for this package and used to read tokenizer.json. Strings are returned as// the exact UTF-8 bytes they encode (every \uXXXX escape, including surrogate pairs, is decoded to UTF-8; nothing// is normalized or stripped). It exists because Foundation's JSONSerialization (macOS 27.0) drops a leading U+FEFF// (byte-order mark) from every string VALUE, which turns the vocabulary entries "", "", ...// into "", "", ... and breaks the tokenizer. (JSONDecoder does not have the bug, but a 32 MB vocabulary// decoded through Codable is slow, and this reader keeps the bytes exact by construction.)//// Supported: the full JSON grammar (RFC 8259). Numbers are returned as Double. Object keys keep their order and// duplicates (the vocabulary is read as an ordered list of pairs).
import Foundation
public indirect enum ExactJSON { case object([(key: [UInt8], value: ExactJSON)]) case array([ExactJSON]) case string([UInt8]) case number(Double) case bool(Bool) case null
public struct ParseError: Error, CustomStringConvertible { public let description: String }
public static func parse(_ data: Data) throws -> ExactJSON { try data.withUnsafeBytes { raw -> ExactJSON in var p = Parser(bytes: raw.bindMemory(to: UInt8.self)) p.skipWS() let v = try p.value() p.skipWS() guard p.i == p.bytes.count else { throw ParseError(description: "trailing characters at byte \(p.i)") } return v } }
public static func parse(contentsOf url: URL) throws -> ExactJSON { try parse(Data(contentsOf: url)) }
// MARK: accessors public subscript(key: String) -> ExactJSON? { guard case .object(let pairs) = self else { return nil } let k = Array(key.utf8) for p in pairs where p.key == k { return p.value } return nil } public var stringBytes: [UInt8]? { if case .string(let b) = self { return b }; return nil } public var string: String? { stringBytes.map { String(decoding: $0, as: UTF8.self) } } public var double: Double? { if case .number(let d) = self { return d }; return nil } public var int: Int? { double.flatMap { $0 == $0.rounded() && abs($0) < 9e15 ? Int($0) : nil } } public var bool: Bool? { if case .bool(let b) = self { return b }; return nil } public var isNull: Bool { if case .null = self { return true }; return false } public var array: [ExactJSON]? { if case .array(let a) = self { return a }; return nil } public var pairs: [(key: [UInt8], value: ExactJSON)]? { if case .object(let p) = self { return p }; return nil }
// MARK: parser private struct Parser { let bytes: UnsafeBufferPointer<UInt8> var i = 0
init(bytes: UnsafeBufferPointer<UInt8>) { self.bytes = bytes }
mutating func skipWS() { while i < bytes.count, bytes[i] == 0x20 || bytes[i] == 0x0A || bytes[i] == 0x0D || bytes[i] == 0x09 { i += 1 } }
func fail(_ what: String) -> ParseError { ParseError(description: "\(what) at byte \(i)") }
mutating func value() throws -> ExactJSON { guard i < bytes.count else { throw fail("unexpected end") } switch bytes[i] { case 0x7B: return try object() case 0x5B: return try array() case 0x22: return .string(try string()) case 0x74: try literal("true"); return .bool(true) case 0x66: try literal("false"); return .bool(false) case 0x6E: try literal("null"); return .null default: return .number(try number()) } }
mutating func literal(_ s: StaticString) throws { let n = s.utf8CodeUnitCount guard i + n <= bytes.count else { throw fail("bad literal") } for k in 0..<n where bytes[i + k] != s.utf8Start[k] { throw fail("bad literal") } i += n }
mutating func number() throws -> Double { let start = i while i < bytes.count { let c = bytes[i] if (c >= 0x30 && c <= 0x39) || c == 0x2D || c == 0x2B || c == 0x2E || c == 0x65 || c == 0x45 { i += 1 } else { break } } guard i > start, let d = Double(String(decoding: bytes[start..<i], as: UTF8.self)) else { throw fail("bad number") } return d }
mutating func object() throws -> ExactJSON { i += 1 var pairs: [(key: [UInt8], value: ExactJSON)] = [] skipWS() if i < bytes.count, bytes[i] == 0x7D { i += 1; return .object(pairs) } while true { skipWS() guard i < bytes.count, bytes[i] == 0x22 else { throw fail("expected key") } let k = try string() skipWS() guard i < bytes.count, bytes[i] == 0x3A else { throw fail("expected ':'") } i += 1 skipWS() pairs.append((k, try value())) skipWS() guard i < bytes.count else { throw fail("unterminated object") } if bytes[i] == 0x2C { i += 1; continue } if bytes[i] == 0x7D { i += 1; return .object(pairs) } throw fail("expected ',' or '}'") } }
mutating func array() throws -> ExactJSON { i += 1 var items: [ExactJSON] = [] skipWS() if i < bytes.count, bytes[i] == 0x5D { i += 1; return .array(items) } while true { skipWS() items.append(try value()) skipWS() guard i < bytes.count else { throw fail("unterminated array") } if bytes[i] == 0x2C { i += 1; continue } if bytes[i] == 0x5D { i += 1; return .array(items) } throw fail("expected ',' or ']'") } }
mutating func hex4() throws -> UInt32 { guard i + 4 <= bytes.count else { throw fail("bad \\u escape") } var v: UInt32 = 0 for _ in 0..<4 { let c = bytes[i] let d: UInt32 switch c { case 0x30...0x39: d = UInt32(c - 0x30) case 0x41...0x46: d = UInt32(c - 0x41 + 10) case 0x61...0x66: d = UInt32(c - 0x61 + 10) default: throw fail("bad hex digit") } v = v << 4 | d i += 1 } return v }
/// Decodes a JSON string starting at the opening quote into exact UTF-8 bytes. mutating func string() throws -> [UInt8] { i += 1 var out: [UInt8] = [] while true { guard i < bytes.count else { throw fail("unterminated string") } let c = bytes[i] if c == 0x22 { i += 1; return out } if c != 0x5C { out.append(c); i += 1; continue } i += 1 guard i < bytes.count else { throw fail("unterminated escape") } let e = bytes[i] i += 1 switch e { case 0x22: out.append(0x22) case 0x5C: out.append(0x5C) case 0x2F: out.append(0x2F) case 0x62: out.append(0x08) case 0x66: out.append(0x0C) case 0x6E: out.append(0x0A) case 0x72: out.append(0x0D) case 0x74: out.append(0x09) case 0x75: var cp = try hex4() if cp >= 0xD800, cp <= 0xDBFF, i + 6 <= bytes.count, bytes[i] == 0x5C, bytes[i + 1] == 0x75 { let save = i i += 2 let lo = try hex4() if lo >= 0xDC00, lo <= 0xDFFF { cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00) } else { i = save // not a pair: keep the lone surrogate (encoded below as 3 bytes, like Rust would reject; never occurs in our files) } } appendUTF8(cp, to: &out) default: throw fail("bad escape") } } }
func appendUTF8(_ cp: UInt32, to out: inout [UInt8]) { switch cp { case 0..<0x80: out.append(UInt8(cp)) case 0x80..<0x800: out.append(UInt8(0xC0 | cp >> 6)); out.append(UInt8(0x80 | cp & 0x3F)) case 0x800..<0x10000: out.append(UInt8(0xE0 | cp >> 12)); out.append(UInt8(0x80 | (cp >> 6) & 0x3F)); out.append(UInt8(0x80 | cp & 0x3F)) default: out.append(UInt8(0xF0 | cp >> 18)); out.append(UInt8(0x80 | (cp >> 12) & 0x3F)); out.append(UInt8(0x80 | (cp >> 6) & 0x3F)); out.append(UInt8(0x80 | cp & 0x3F)) } } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281// Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../../../LICENSE).//// Exact re-implementation of the Hugging Face `tokenizers` encode pipeline for the Gemma `tokenizer.json`// shipped in this bundle://// added tokens leftmost-longest match of the 24 special tokens on the RAW text (they are `normalized: false`)// normalizer Replace " " -> "\u{2581}" on every remaining piece// pre_tokenizer Split(" ") — a no-op after the normalizer, so each piece is ONE BPE word// model BPE, byte_fallback (a scalar missing from the vocab becomes its UTF-8 bytes as `<0xNN>` tokens),// merges applied by rank, ties broken by position (the `tokenizers` heap algorithm)//// Everything operates on Unicode scalars and UTF-8 bytes: no grapheme-cluster (`Character`) iteration and no// String equality (which is canonical equivalence in Swift). The vocabulary is keyed by exact UTF-8 bytes, so// "é" (U+00E9) and "e" + U+0301 are different keys, as they are for `tokenizers`. tokenizer.json is read with// ExactJSON, not Foundation (which strips a leading U+FEFF from string values and corrupts three vocab entries).// Verified byte-identical to `tokenizers` 0.23 on tests/tokenizer-cases.json (3,758 texts, 608,225 ids).
import Foundation
public final class GemmaTokenizer: Sendable { public struct LoadError: Error, CustomStringConvertible { public let description: String }
public let bosID: Int32 public let eosID: Int32 public let padID: Int32 public let unkID: Int32 public let vocabSize: Int
private let vocab: [[UInt8]: Int32] private let tokenBytes: [[UInt8]] // id -> bytes (for decode) private let merges: [UInt64: MergeTarget] // (left id << 32 | right id) -> rank, merged id private let byteIDs: [Int32] // 256 entries: id of "<0xNN>" private let asciiIDs: [Int32] // 128 entries: id of the 1-byte token, or -1 private let added: [AddedToken] // longest content first private let addedFirstScalars: Set<UInt32> private let specialIDs: Set<Int32>
private struct MergeTarget { let rank: Int32; let id: Int32 } private struct AddedToken { let scalars: [Unicode.Scalar]; let id: Int32 }
/// Loads `tokenizer.json`. Throws if the file is not the Gemma BPE configuration this class implements. public convenience init(contentsOf url: URL) throws { try self.init(data: try Data(contentsOf: url)) }
public init(data: Data) throws { let root = try ExactJSON.parse(data) // --- configuration guards: refuse anything this implementation does not reproduce exactly --- guard let norm = root["normalizer"], norm["type"]?.string == "Replace", norm["pattern"]?["String"]?.string == " ", norm["content"]?.stringBytes == Array("\u{2581}".utf8) else { throw LoadError(description: "unsupported normalizer (expected Replace \" \" -> U+2581)") } guard let pre = root["pre_tokenizer"], pre["type"]?.string == "Split", pre["pattern"]?["String"]?.string == " " else { throw LoadError(description: "unsupported pre_tokenizer (expected Split \" \")") } guard let model = root["model"], model["type"]?.string == "BPE", model["byte_fallback"]?.bool == true, model["continuing_subword_prefix"]?.isNull ?? true, model["end_of_word_suffix"]?.isNull ?? true, model["dropout"]?.isNull ?? true, (model["ignore_merges"]?.bool ?? false) == false else { throw LoadError(description: "unsupported BPE model configuration") } guard let vocabPairs = model["vocab"]?.pairs, let mergeList = model["merges"]?.array else { throw LoadError(description: "tokenizer.json: missing vocab/merges") }
// --- vocab: exact UTF-8 bytes -> id --- var vocab = [[UInt8]: Int32](minimumCapacity: vocabPairs.count) var maxID: Int32 = -1 for (key, value) in vocabPairs { guard let id = value.int else { throw LoadError(description: "vocab value is not an integer") } vocab[key] = Int32(id) if Int32(id) > maxID { maxID = Int32(id) } } guard vocab.count == vocabPairs.count else { throw LoadError(description: "duplicate vocab keys (\(vocab.count) != \(vocabPairs.count))") } var tokenBytes = [[UInt8]](repeating: [], count: Int(maxID) + 1) for (bytes, id) in vocab { tokenBytes[Int(id)] = bytes }
// --- merges: [a, b] (or "a b") -> rank, id of a+b --- var merges = [UInt64: MergeTarget](minimumCapacity: mergeList.count) for (rank, m) in mergeList.enumerated() { let a: [UInt8], b: [UInt8] if let pair = m.array, pair.count == 2, let x = pair[0].stringBytes, let y = pair[1].stringBytes { (a, b) = (x, y) } else if let s = m.stringBytes, let sp = s.firstIndex(of: 0x20) { (a, b) = (Array(s[..<sp]), Array(s[(sp + 1)...])) } else { throw LoadError(description: "merge #\(rank) has an unexpected shape") } guard let ia = vocab[a], let ib = vocab[b], let iab = vocab[a + b] else { throw LoadError(description: "merge #\(rank) references a token missing from the vocab") } merges[GemmaTokenizer.pairKey(ia, ib)] = MergeTarget(rank: Int32(rank), id: iab) }
// --- byte fallback tokens and the ASCII fast path --- var byteIDs = [Int32](repeating: -1, count: 256) for b in 0..<256 { guard let id = vocab[Array(String(format: "<0x%02X>", b).utf8)] else { throw LoadError(description: "byte fallback token <0x\(String(format: "%02X", b))> missing") } byteIDs[b] = id } var asciiIDs = [Int32](repeating: -1, count: 128) for c in 0..<128 { asciiIDs[c] = vocab[[UInt8(c)]] ?? -1 }
// --- added (special) tokens: only the exact-match flavour is supported --- var added: [AddedToken] = [] for t in root["added_tokens"]?.array ?? [] { guard let content = t["content"]?.stringBytes, let id = t["id"]?.int else { continue } let flags = ["normalized", "lstrip", "rstrip", "single_word"].map { t[$0]?.bool ?? false } guard !flags.contains(true) else { throw LoadError(description: "added token \(String(decoding: content, as: UTF8.self)) uses normalized/lstrip/rstrip/single_word, which is not supported") } added.append(AddedToken(scalars: Array(String(decoding: content, as: UTF8.self).unicodeScalars), id: Int32(id))) } added.sort { $0.scalars.count > $1.scalars.count }
func special(_ name: String) throws -> Int32 { guard let id = vocab[Array(name.utf8)] else { throw LoadError(description: "\(name) missing from vocab") } return id } self.vocab = vocab self.tokenBytes = tokenBytes self.merges = merges self.byteIDs = byteIDs self.asciiIDs = asciiIDs self.added = added self.addedFirstScalars = Set(added.compactMap { $0.scalars.first?.value }) self.specialIDs = Set(added.map { $0.id }) self.vocabSize = vocab.count self.bosID = try special("<bos>") self.eosID = try special("<eos>") self.padID = try special("<pad>") self.unkID = try special("<unk>") }
@inline(__always) private static func pairKey(_ a: Int32, _ b: Int32) -> UInt64 { (UInt64(UInt32(bitPattern: a)) << 32) | UInt64(UInt32(bitPattern: b)) }
/// Token ids of `text` without `<bos>`/`<eos>` (the same as `tokenizers` `encode(text, add_special_tokens=False)`). /// Special tokens written literally in the text (e.g. "<eos>") are recognised, exactly as `tokenizers` does. public func encode(_ text: String) -> [Int32] { let scalars = Array(text.unicodeScalars) var ids: [Int32] = [] ids.reserveCapacity(scalars.count / 3 + 4) var i = 0, pieceStart = 0 while i < scalars.count { if addedFirstScalars.contains(scalars[i].value), let hit = matchAdded(scalars, at: i) { if i > pieceStart { bpe(scalars[pieceStart..<i], into: &ids) } ids.append(hit.id) i += hit.scalars.count pieceStart = i } else { i += 1 } } if pieceStart < scalars.count { bpe(scalars[pieceStart...], into: &ids) } return ids }
/// `<bos>` + encode(text) + `<eos>`. public func encodeWithSpecials(_ text: String) -> [Int32] { [bosID] + encode(text) + [eosID] }
/// Text for a sequence of ids (the bundle's decoder: "\u{2581}" -> " ", byte-fallback tokens fused into UTF-8). public func decode(_ ids: [Int32], skipSpecialTokens: Bool = true) -> String { var bytes: [UInt8] = [] for id in ids { guard id >= 0, Int(id) < tokenBytes.count else { continue } if skipSpecialTokens, specialIDs.contains(id) { continue } if let b = byteIDs.firstIndex(of: id) { bytes.append(UInt8(b)); continue } let t = tokenBytes[Int(id)] var j = 0 while j < t.count { // replace every U+2581 (E2 96 81) by a space if j + 2 < t.count, t[j] == 0xE2, t[j + 1] == 0x96, t[j + 2] == 0x81 { bytes.append(0x20); j += 3 } else { bytes.append(t[j]); j += 1 } } } return String(decoding: bytes, as: UTF8.self) }
private func matchAdded(_ s: [Unicode.Scalar], at i: Int) -> AddedToken? { for t in added where i + t.scalars.count <= s.count { // sorted longest first => first hit is the longest var k = 0 while k < t.scalars.count, s[i + k] == t.scalars[k] { k += 1 } if k == t.scalars.count { return t } } return nil }
// MARK: - BPE on one word (the `tokenizers` Word::merge_all algorithm)
private struct HeapEntry { let rank: Int32; let pos: Int32; let id: Int32 }
private func bpe(_ piece: ArraySlice<Unicode.Scalar>, into out: inout [Int32]) { // 1. initial symbols: one per scalar (space -> U+2581), byte fallback for scalars missing from the vocab var syms: [Int32] = [] syms.reserveCapacity(piece.count) var utf8 = [UInt8](repeating: 0, count: 4) for sc in piece { let v = sc.value if v < 128, v != 0x20 { let id = asciiIDs[Int(v)] syms.append(id >= 0 ? id : byteIDs[Int(v)]) continue } let scalar = v == 0x20 ? Unicode.Scalar(0x2581)! : sc var n = 0 for b in String(scalar).utf8 { utf8[n] = b; n += 1 } // the UTF-8 of ONE scalar: nothing is normalized let key = Array(utf8[0..<n]) if let id = vocab[key] { syms.append(id) } else { for b in key { syms.append(byteIDs[Int(b)]) } } } let n = syms.count if n < 2 { out.append(contentsOf: syms); return }
// 2. merge by rank, leftmost first among equal ranks (binary heap on (rank, pos)) var next = [Int32](repeating: 0, count: n), prev = [Int32](repeating: 0, count: n) var alive = [Bool](repeating: true, count: n) for i in 0..<n { next[i] = Int32(i + 1); prev[i] = Int32(i - 1) } var heap: [HeapEntry] = [] heap.reserveCapacity(n) @inline(__always) func less(_ a: HeapEntry, _ b: HeapEntry) -> Bool { a.rank != b.rank ? a.rank < b.rank : a.pos < b.pos } func push(_ e: HeapEntry) { heap.append(e) var c = heap.count - 1 while c > 0 { let p = (c - 1) >> 1 if less(heap[c], heap[p]) { heap.swapAt(c, p); c = p } else { break } } } func pop() -> HeapEntry { let top = heap[0] let last = heap.removeLast() if !heap.isEmpty { heap[0] = last var p = 0 while true { let l = 2 * p + 1, r = l + 1 var m = p if l < heap.count, less(heap[l], heap[m]) { m = l } if r < heap.count, less(heap[r], heap[m]) { m = r } if m == p { break } heap.swapAt(p, m); p = m } } return top } for i in 0..<(n - 1) { if let m = merges[GemmaTokenizer.pairKey(syms[i], syms[i + 1])] { push(HeapEntry(rank: m.rank, pos: Int32(i), id: m.id)) } } while !heap.isEmpty { let e = pop() let i = Int(e.pos) if !alive[i] { continue } let j = Int(next[i]) if j >= n || !alive[j] { continue } guard let m = merges[GemmaTokenizer.pairKey(syms[i], syms[j])], m.rank == e.rank else { continue } syms[i] = m.id alive[j] = false next[i] = next[j] if Int(next[i]) < n { prev[Int(next[i])] = Int32(i) } if prev[i] >= 0, let m2 = merges[GemmaTokenizer.pairKey(syms[Int(prev[i])], syms[i])] { push(HeapEntry(rank: m2.rank, pos: prev[i], id: m2.id)) } if Int(next[i]) < n, let m2 = merges[GemmaTokenizer.pairKey(syms[i], syms[Int(next[i])])] { push(HeapEntry(rank: m2.rank, pos: Int32(i), id: m2.id)) } } for i in 0..<n where alive[i] { out.append(syms[i]) } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187// Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../../../LICENSE).//// eg2-cli — command-line front end for the EmbeddingGemma2CoreAI library.//// eg2-cli embed <bundle> [--compute ane|gpu|cpu] [--kind document|search_query|...] [--dim 768] "text" ...// eg2-cli smoke <bundle> <smoke-reference.json> [--compute ane|gpu|cpu] [--cleanup-temp]// eg2-cli tokcheck <bundle> <tokenizer-cases.json>// eg2-cli latency <bundle> [--compute ane|gpu|cpu] [--trials 50]// eg2-cli prewarm <bundle> [--compute ane|gpu|cpu]// eg2-cli cleanup-temp (removes this process's MPSGraph temp folders; mostly useful after the others)//// Every command accepts --cleanup-temp to delete the MPSGraph temp folders this process created, after the// model is released (see EmbeddingGemma2.removeOwnMPSGraphTempFiles).
import EmbeddingGemma2CoreAIimport Foundation
var args = Array(CommandLine.arguments.dropFirst())
@MainActor func usage() -> Never { print(""" usage: eg2-cli <embed|smoke|tokcheck|latency|prewarm|cleanup-temp> <bundle> [...] [--compute ane|gpu|cpu] [--cleanup-temp] """) exit(2)}
@MainActor func takeOption(_ name: String) -> String? { guard let i = args.firstIndex(of: name), i + 1 < args.count else { return nil } let v = args[i + 1] args.removeSubrange(i...(i + 1)) return v}
@MainActor func takeFlag(_ name: String) -> Bool { guard let i = args.firstIndex(of: name) else { return false } args.remove(at: i) return true}
func fmt(_ x: Double, _ digits: Int = 2) -> String { String(format: "%.\(digits)f", x) }
func cpuSeconds() -> Double { var u = rusage() getrusage(RUSAGE_SELF, &u) return Double(u.ru_utime.tv_sec) + Double(u.ru_utime.tv_usec) / 1e6 + Double(u.ru_stime.tv_sec) + Double(u.ru_stime.tv_usec) / 1e6}
/// Peak physical footprint (MB) and the Neural Engine footprint (MB) of this process (proc_pid_rusage, RUSAGE_INFO_V6).func footprints() -> (physMB: Double, neuralMB: Double) { var r = rusage_info_v6() let kr = withUnsafeMutablePointer(to: &r) { p -> Int32 in p.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(getpid(), RUSAGE_INFO_V6, $0) } } guard kr == 0 else { return (-1, -1) } return (Double(r.ri_lifetime_max_phys_footprint) / 1048576, Double(r.ri_neural_footprint) / 1048576)}
guard !args.isEmpty else { usage() }let cmd = args.removeFirst()let cleanup = takeFlag("--cleanup-temp")let compute = ComputePreference(rawValue: takeOption("--compute") ?? "ane") ?? .neuralEngine
@MainActor func finish(_ code: Int32) -> Never { if cleanup { let r = EmbeddingGemma2.removeOwnMPSGraphTempFiles() print("cleanup-temp: removed \(r.folders) MPSGraph temp folder(s), \(fmt(Double(r.bytes) / 1048576, 1)) MB") } exit(code)}
if cmd == "cleanup-temp" { finish(0) }guard let bundlePath = args.first else { usage() }args.removeFirst()let bundle = URL(fileURLWithPath: bundlePath)
do { switch cmd { case "embed": let kind = takeOption("--kind") ?? "document" let dim = Int(takeOption("--dim") ?? "768") ?? 768 let eg2 = try await EmbeddingGemma2(bundle: bundle, compute: compute) for text in args { let v = try await eg2.embed(text, kind: kind == "none" ? nil : kind, dim: dim) let json: [String: Any] = ["text": text, "kind": kind, "dim": dim, "embedding": v.map { Double($0) }] print(String(decoding: try JSONSerialization.data(withJSONObject: json), as: UTF8.self)) } _ = consume eg2 finish(0)
case "smoke": guard let refPath = args.first else { usage() } struct Ref: Decodable { let text: String; let kind: String; let embedding: [Float] } struct RefFile: Decodable { let threshold_cosine: Double; let cases: [Ref] } let ref = try JSONDecoder().decode(RefFile.self, from: Data(contentsOf: URL(fileURLWithPath: refPath))) let eg2 = try await EmbeddingGemma2(bundle: bundle, compute: compute) print("loaded in \(fmt(eg2.loadSeconds, 1)) s (tokenizer \(fmt(eg2.tokenizerLoadSeconds, 2)) s), compute=\(compute.rawValue), buckets=\(eg2.buckets)") var ok = true for c in ref.cases { let v = try await eg2.embed(c.text, kind: c.kind) let cos = cosineSimilarity(v, c.embedding) let pass = cos >= ref.threshold_cosine && v.allSatisfy { $0.isFinite } ok = ok && pass print("\(pass ? "PASS" : "FAIL") cosine \(fmt(cos, 6)) (threshold \(ref.threshold_cosine)) \(c.kind): \(c.text.prefix(60))") } let fp = footprints() print("peak footprint \(fmt(fp.physMB, 0)) MB, neural footprint \(fmt(fp.neuralMB, 0)) MB") print(ok ? "SMOKE PASS" : "SMOKE FAIL") _ = consume eg2 finish(ok ? 0 : 1)
case "tokcheck": guard let casesPath = args.first else { usage() } struct Case: Decodable { let cat: String; let kind: String; let text: String; let ids: [Int32] } let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: URL(fileURLWithPath: casesPath))) let config = try JSONDecoder().decode(BundleConfig.self, from: Data(contentsOf: bundle.appendingPathComponent("config.json"))) let t0 = Date() let tok = try GemmaTokenizer(contentsOf: bundle.appendingPathComponent("tokenizer/tokenizer.json")) print("tokenizer loaded in \(fmt(Date().timeIntervalSince(t0), 2)) s, vocab \(tok.vocabSize)") let prefixes = ["q": "task: search result | query: ", "d": "title: none | text: ", "raw": ""] var same = 0, totalIDs = 0, shown = 0 var perCat: [String: (Int, Int)] = [:] let t1 = Date() let cpu0 = cpuSeconds() for c in cases { let ids = tok.encodeWithSpecials(prefixes[c.kind]! + c.text) let ok = ids == c.ids totalIDs += c.ids.count var e = perCat[c.cat] ?? (0, 0) e.1 += 1 if ok { same += 1; e.0 += 1 } else if shown < 10 { shown += 1 print("DIFFERENT [\(c.cat)] \(c.text.prefix(60).debugDescription): got \(ids.prefix(12)) expected \(c.ids.prefix(12))") } perCat[c.cat] = e } let secs = Date().timeIntervalSince(t1), cpu = cpuSeconds() - cpu0 for (cat, e) in perCat.sorted(by: { $0.key < $1.key }) { print(" \(cat): \(e.0)/\(e.1)") } print("TOKCHECK \(same)/\(cases.count) texts identical to Hugging Face tokenizers (\(totalIDs) ids) in \(fmt(secs, 2)) s wall / \(fmt(cpu, 2)) s CPU") print("config bos/eos/pad \(config.bos_token_id)/\(config.eos_token_id)/\(config.pad_token_id) == tokenizer \(tok.bosID)/\(tok.eosID)/\(tok.padID)") finish(same == cases.count ? 0 : 1)
case "latency": let trials = Int(takeOption("--trials") ?? "50") ?? 50 let eg2 = try await EmbeddingGemma2(bundle: bundle, compute: compute) print("loaded in \(fmt(eg2.loadSeconds, 1)) s, compute=\(compute.rawValue)") let warm = try await eg2.prewarm() print("prewarm (one call per bucket) \(fmt(warm, 2)) s") print("bucket p50 ms min ms p90 ms CPU ms/call tok/s") for S in eg2.buckets { var ids = [Int32](repeating: 0, count: S) ids[0] = eg2.config.bos_token_id; ids[S - 1] = eg2.config.eos_token_id for i in 1..<(S - 1) { ids[i] = Int32(1000 + (i * 7919) % 200000) } for _ in 0..<3 { _ = try await eg2.embed(ids: ids, bucket: S) } var ts: [Double] = [] let cpu0 = cpuSeconds() for _ in 0..<trials { let t = Date() _ = try await eg2.embed(ids: ids, bucket: S) ts.append(Date().timeIntervalSince(t) * 1000) } let cpu = (cpuSeconds() - cpu0) / Double(trials) * 1000 ts.sort() let p50 = ts[ts.count / 2], p90 = ts[min(ts.count - 1, Int(Double(ts.count) * 0.9))] print(String(format: "%5d %7.2f %7.2f %7.2f %11.2f %6.0f", S, p50, ts[0], p90, cpu, Double(S) / p50 * 1000)) } let fp = footprints() print("peak footprint \(fmt(fp.physMB, 0)) MB, neural footprint \(fmt(fp.neuralMB, 0)) MB (\(fp.neuralMB > 0 ? "Neural Engine in use" : "Neural Engine NOT used"))") _ = consume eg2 finish(0)
case "prewarm": let t0 = Date() let eg2 = try await EmbeddingGemma2(bundle: bundle, compute: compute) let warm = try await eg2.prewarm() print("load \(fmt(eg2.loadSeconds, 1)) s + prewarm \(fmt(warm, 2)) s = ready in \(fmt(Date().timeIntervalSince(t0), 1)) s") _ = consume eg2 finish(0)
default: usage() }} catch { print("error: \(error)") finish(1)}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#!/usr/bin/env python3# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE)."""Regenerate `smoke-reference.json`: fp32 reference embeddings for three fictional sentences, computed withsentence-transformers on the ORIGINAL google/embeddinggemma-2 checkpoint (text only, CPU, float32).
python3 make_smoke_reference.py --model google/embeddinggemma-2 --out smoke-reference.json
Needs: sentence-transformers >= 6.1, transformers >= 5.19, torch, torchvision and pillow (the checkpoint's processoris multimodal and imports them even for text). The shipped file was produced with sentence-transformers 6.1.0 /transformers 5.19.0 / torch 2.14.1; torchvision 0.29.1 is the matching version (see ../python/requirements.lock)."""from __future__ import annotations
import argparseimport jsonfrom pathlib import Path
CASES = [ {"kind": "search_query", "text": "When does the warehouse lease in Brindlemoor expire?"}, {"kind": "document", "text": "The warehouse lease in Brindlemoor runs until the last day of March; the landlord agreed to fix the window before then."}, {"kind": "document", "text": "Lista della spesa: latte d'avena, due limoni, riso basmati, coriandolo e croccantini per il cane."},]PROMPT_NAME = {"search_query": "query", "document": "document"}
def main() -> None: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--model", default="google/embeddinggemma-2", help="HF id or local checkpoint folder") ap.add_argument("--out", default="smoke-reference.json") ap.add_argument("--threshold", type=float, default=0.999) args = ap.parse_args() import sentence_transformers import torch import transformers from sentence_transformers import SentenceTransformer
model = SentenceTransformer(args.model, device="cpu", model_kwargs={"torch_dtype": torch.float32}) out = { "reference": "sentence-transformers fp32 on CPU, original checkpoint, text only", "versions": {"sentence_transformers": sentence_transformers.__version__, "transformers": transformers.__version__, "torch": torch.__version__}, "threshold_cosine": args.threshold, "cases": [], } for c in CASES: v = model.encode(c["text"], prompt_name=PROMPT_NAME[c["kind"]], normalize_embeddings=True, convert_to_numpy=True) out["cases"].append({**c, "embedding": [round(float(x), 7) for x in v.tolist()]}) print(c["kind"], len(v), c["text"][:50]) Path(args.out).write_text(json.dumps(out, ensure_ascii=False, indent=1), encoding="utf-8") print("wrote", args.out)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298#!/usr/bin/env python3# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE)."""Generate the tokenizer conformance fixture `tokenizer-cases.json`: >= 3,000 deterministic, FICTIONAL texts thatstress the Gemma tokenizer (CRLF, NFD accents, emoji ZWJ / skin tones / flags / keycaps, Arabic with diacritics, Thai,Hindi, CJK, whitespace runs > 31, tabs, control characters, special-token look-alikes, very long inputs), with the idsproduced by the Hugging Face `tokenizers` library for `<bos> + encode(prefix + text) + <eos>` (no truncation).
python3 make_tokenizer_cases.py --tokenizer ../model/tokenizer/tokenizer.json --out tokenizer-cases.json
The people, companies, events and numbers below are invented. Most place names are invented too; a few realplace names and words appear in the accent and script stress strings (e.g. Łódź, Đà Nẵng, Ångström)."""from __future__ import annotations
import argparseimport jsonimport randomimport unicodedatafrom pathlib import Path
PREFIX = {"q": "task: search result | query: ", "d": "title: none | text: ", "raw": ""}
FIRST = ["Orla", "Teodor", "Mirela", "Kasimir", "Ysolde", "Bram", "Fenna", "Ruaridh", "Ottilie", "Severin", "Jolan", "Liesl", "Corvin", "Tamsin", "Ilse", "Quillon", "Saskia", "Elowen", "Thaddeus", "Perpetua"]LAST = ["Vantrell", "Dunholm", "Brecklow", "Szarvas", "Penhallow", "Lindqvar", "Castelmoro", "Ravensby", "Quarterman", "Halloran-Bey", "Møllergaard", "Åkerlund", "Nowakówna", "Çelikkaya", "Đặng", "O'Rourke-Vale"]ORGS = ["Lumenvale", "Brightwater Mills", "Okkam Freight", "Velmora Labs", "Quillfeather", "Saltmarsh & Co", "Norrow Analytics", "Cobaltine", "Haverly Bikes", "Studio Morello", "Tavola Verde", "Pinecrest Dental"]TOWNS = ["Brindlemoor", "Castel Ferrino", "Ostwick", "Valdorna", "Pellham Cross", "Marrowgate", "San Lucetto", "Hadleigh Fen", "Rocca Bruna", "Thistleby", "Søndervik", "Łęczyca Nowa", "Çamlıdere", "Žďár Horní"]THINGS = ["the quarterly invoice", "the boiler service", "the onboarding checklist", "the API rate limits", "the warehouse lease", "the beta feedback", "the dentist appointment", "the hiring plan", "the tax return", "the server migration", "the book club", "the roof estimate", "the ferry timetable"]COSE = ["la fattura trimestrale", "la revisione della caldaia", "il contratto d'affitto", "il preventivo del tetto", "la riunione di condominio", "il volo per Valdorna", "la visita dal dentista", "il piano ferie", "il trasloco di sabato", "il corso di ceramica"]DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]MONTHS = ["January", "March", "May", "July", "September", "October", "November", "December"]
EN_TPL = [ "{p} from {o} moved {t} to {d} because the team in {town} is short-staffed this month.", "The budget for {t} is {n},{m:03d} and {p} wants a second quote before {d}.", "Remember to call {p} about {t}; the office in {town} closes at {h}:30.", "{o} signed the renewal: {n} seats at {m} per seat, billed every {mo}.", "On {d} the deploy failed twice because a config flag was read as seconds instead of milliseconds.", "Minutes: {p} presented the roadmap, {p2} raised concerns about {t}, and the decision was postponed to {mo}.", "Our p99 latency rose to {m} ms after the {d} release; rollback is ready if it passes {h}0 ms.", "Grocery list: oat milk, two lemons, basmati rice, coriander, and dog treats for Biscuit.", "Flight {o2}{n} to {town} departs at {h}:{m2:02d}; seat {m2}C, one checked bag.", "Draft reply to {p}: thanks for the notes on {t}, we will send the revised version by {d}.", "The landlord in {town} agreed to fix the window before {mo}; the rent stays at {n},{m:03d} a year.",]IT_TPL = [ "{p} di {o} ha spostato {c} a {d} perché l'ufficio di {town} è sotto organico.", "Il preventivo per {c} è di {n}.{m:03d} euro e {p} vuole un secondo parere entro {d}.", "Ricordati di chiamare {p} per {c}: lo studio di {town} chiude alle {h}:30.", "Verbale: {p} ha presentato il piano, {p2} ha sollevato dubbi su {c} e la decisione slitta a {mo}.", "La caldaia perde ancora; il tecnico dice che la valvola costa {m} euro e serve una settimana.", "Lista della spesa: latte d'avena, due limoni, riso basmati, coriandolo e croccantini per il cane.", "Il volo per {town} parte alle {h}:{m2:02d}; posto {m2}C, un bagaglio in stiva. Perché così presto?",]OTHER = { "de": ["Die Heizung in {town} fällt seit {d} ständig aus; {p} ruft morgen den Installateur an.", "Größere Änderungen am Vertrag mit {o} müssen bis {mo} unterschrieben werden."], "fr": ["{p} a déplacé la réunion à {h} h parce que le train pour {town} était en retard.", "Le devis de {o} s'élève à {n} {m:03d} € ; l'échéance est fixée à {mo}."], "es": ["¿Quién se encarga de {t} en {o}? {p} dijo que el plazo termina el {d}.", "El alquiler en {town} sube a {n}.{m:03d} al año; ¡hay que renegociar antes de {mo}!"], "pt": ["{p} avisou que a entrega de {o} chega a {town} na {d}, por volta das {h}h.", "O orçamento para {t} ficou em {n}.{m:03d}; a decisão foi adiada para {mo}."], "nl": ["{p} belt {d} met {o} over {t}; het kantoor in {town} sluit om {h}:30.", "De huur in {town} blijft {n}.{m:03d} per jaar als het raam vóór {mo} gemaakt is."], "pl": ["{p} przeniósł spotkanie na {d}, bo zespół w {town} ma za mało ludzi.", "Faktura od {o} opiewa na {n} {m:03d} zł; termin płatności mija w {mo}."], "tr": ["{p}, {o} ile yapılan sözleşmeyi {mo} ayına kadar imzalamalı; ofis {h}:30'da kapanıyor.", "{town}'daki kira yılda {n}.{m:03d} olarak kalıyor; pencere {mo} öncesi tamir edilecek."], "vi": ["{p} đã dời cuộc họp sang {d} vì đội ở {town} đang thiếu người.", "Hóa đơn của {o} là {n}.{m:03d}; hạn thanh toán là tháng {m2}."], "ru": ["{p} перенёс встречу на {d}, потому что в {town} не хватает людей.", "Счёт от {o} на {n} {m:03d}; оплатить нужно до {mo}."], "el": ["Ο {p} μετέφερε τη συνάντηση την {d} επειδή η ομάδα στο {town} είναι λίγη.", "Το τιμολόγιο της {o} είναι {n}.{m:03d}· πληρωμή μέχρι {mo}."], "he": ["{p} הזיז את הפגישה ל-{d} כי הצוות ב-{town} קטן מדי.", "החשבונית מ-{o} היא {n},{m:03d}; לשלם עד {mo}."], "ar": ["نَقَلَ {p} الاجْتِماعَ إلى يَوْمِ {d} لِأَنَّ الفَريقَ في {town} قَليلُ العَدَدِ هذا الشَّهْرَ.", "الفاتُورَةُ مِنْ {o} بِقيمَةِ {n}٬{m:03d}؛ يَجِبُ الدَّفْعُ قَبْلَ {mo}.", "تَذَكَّرْ أَنْ تَتَّصِلَ بِـ{p} بِخُصُوصِ {t}؛ المَكْتَبُ يُغْلِقُ السّاعَةَ {h}:30."], "fa": ["{p} جلسه را به {d} موکول کرد چون تیم {town} این ماه کمنیرو است.", "صورتحساب {o} به مبلغ {n}٬{m:03d} است؛ پرداخت تا {mo}."], "th": ["{p} เลื่อนการประชุมไปวัน{d} เพราะทีมที่{town}มีคนไม่พอในเดือนนี้", "ใบแจ้งหนี้จาก {o} เป็นเงิน {n},{m:03d} บาท ต้องชำระก่อน{mo}", "อย่าลืมโทรหา{p}เรื่อง{t} สำนักงานปิดเวลา {h}:30 น."], "hi": ["{p} ने बैठक {d} को टाल दी क्योंकि {town} की टीम में इस महीने लोग कम हैं।", "{o} का चालान {n},{m:03d} रुपये का है; भुगतान {mo} से पहले करना है।", "{p} को {t} के बारे में फ़ोन करना याद रखें; दफ़्तर {h}:30 बजे बंद होता है।"], "bn": ["{p} সভাটি {d}-এ পিছিয়ে দিয়েছেন কারণ {town}-এর দলে এই মাসে লোক কম।", "{o}-এর চালান {n},{m:03d} টাকার; {mo}-এর আগে পরিশোধ করতে হবে।"], "ta": ["{p} கூட்டத்தை {d} அன்று ஒத்திவைத்தார்; {town} குழுவில் இந்த மாதம் ஆட்கள் குறைவு.", "{o} விலைப்பட்டியல் {n},{m:03d}; {mo}க்கு முன் செலுத்த வேண்டும்."], "zh": ["{p}把会议改到了{d},因为{town}的团队这个月人手不够。", "{o}的发票金额是{n},{m:03d}元,必须在{mo}之前付款。", "记得给{p}打电话谈{t};办公室{h}点半关门。"], "zh_tw": ["{p}把會議改到了{d},因為{town}的團隊這個月人手不夠。", "{o}的發票金額是{n},{m:03d}元,必須在{mo}之前付款。"], "ja": ["{p}さんは{town}のチームが今月人手不足なので、会議を{d}に延期しました。", "{o}からの請求書は{n},{m:03d}円で、{mo}までに支払う必要があります。", "{t}について{p}さんに電話するのを忘れないで。オフィスは{h}時半に閉まります。"], "ko": ["{p}은(는) {town} 팀이 이번 달 인력이 부족해서 회의를 {d}로 미뤘습니다.", "{o}의 청구서는 {n},{m:03d}원이며 {mo} 전에 결제해야 합니다.", "{t} 건으로 {p}에게 전화하는 것 잊지 마세요. 사무실은 {h}시 30분에 닫습니다."], "ka": ["{p}-მ��� შეხვედრა {d}-ზე გადადო, რადგან {town}-ის გუნდი ამ თვეში მცირეა."], "hy": ["{p}-ը հանդիպումը տեղափոխեց {d}, քանի որ {town}-ի թիմը այս ամիս փոքր է։"], "am": ["{p} ስብሰባውን ወደ {d} አዛወረው፣ ምክንያቱም በ{town} ያለው ቡድን በዚህ ወር ትንሽ ነው።"], "uk": ["{p} переніс зустріч на {d}, бо команді в {town} бракує людей."], "cs": ["{p} přesunul schůzku na {d}, protože tým v {town} má tento měsíc málo lidí."], "fi": ["{p} siirsi kokouksen {d}, koska {town}n tiimi on tässä kuussa alimiehitetty."], "hu": ["{p} áthelyezte a megbeszélést {d}-ra, mert a {town}-i csapat ebben a hónapban kevés."], "sw": ["{p} alihamisha mkutano hadi {d} kwa sababu timu ya {town} ina watu wachache mwezi huu."], "id": ["{p} memindahkan rapat ke {d} karena tim di {town} kekurangan orang bulan ini."],}QUERIES = ["When is {t} due?", "Who handles {t} at {o}?", "How much did {o} quote for {t}?", "What did {p} decide about the {town} office?", "Which flight goes to {town} on {d}?", "Why was the deploy rolled back?", "What is the rent in {town}?", "Quando scade {c}?", "Chi si occupa di {c} a {town}?", "¿Cuánto cuesta {t}?", "Wann endet die Testphase von {o}?", "{town} 사무실 임대료는 얼마인가요?", "{o}の請求書はいつまで?", "{p}是谁?", "ما هو إيجار {town}؟", "{town}में किराया कितना है?", "ค่าเช่าใน{town}เท่าไหร่", "p99 latency", "SOC 2", "INC-{m}", "wifi password"]CODE = [ "def backoff(attempt: int, base: float = 0.5, cap: float = {m}.0) -> float:\n return min(cap, base * 2 ** attempt)\n", "func retryDelay(_ attempt: Int) -> Duration {{\n\t.milliseconds(min({m}_000, 500 << attempt))\n}}\n", "SELECT customer_id, SUM(amount) AS total\nFROM invoices\nWHERE issued_at >= '2026-{m2:02d}-01'\nGROUP BY customer_id\nHAVING SUM(amount) > {n}{m};\n", "const queue = new Map();\nexport function enqueue(id, payload) {{\n if (queue.size > {m}) throw new Error('queue full');\n queue.set(id, {{ payload, tries: 0 }});\n}}\n", "#!/bin/sh\nset -eu\nfor f in logs/*.gz; do\n zcat \"$f\" | grep -c 'status={h}0' || true\ndone\n", "{{\n \"service\": \"{o2}-webhooks\",\n \"region\": \"ams-1\",\n \"retries\": {h},\n \"timeout_ms\": {m}0\n}}\n", "class DeliveryQueue:\n def __init__(self, limit={m}):\n self.items = []\n\n def push(self, item):\n if len(self.items) >= self.limit:\n raise OverflowError(item)\n", "fn parse_port(s: &str) -> Result<u16, String> {{\n s.trim().parse::<u16>().map_err(|e| format!(\"bad port {{s}}: {{e}}\"))\n}}\n", "<div class=\"card\">\r\n\t<span id=\"x{m}\"> </span>\r\n</div>\r\n", "x = [i**2 for i in range({m}) if i % {h} == 0] # noqa\n\n\n\n",]EMOJI = ["👩💻", "👨👩👧👦", "🏳️🌈", "🏴☠️", "👍🏽", "🧑🏿🚀", "🇮🇹", "🇯🇵", "🇺🇳", "1️⃣", "#️⃣", "*️⃣", "❤️", "❤", "🫶🏼", "👩🏻❤️💋👨🏾", "🏃♀️", "🧘🏽♂️", "🫠", "🐈⬛", "🐻❄️", "😶🌫️", "🇿🇿", "🏴", "✌🏻", "☝🏿", "🅰️", "™", "©️", "⚠️"]LOOKALIKES = ["<bos>", "<eos>", "<pad>", "<unk>", "<mask>", "<|tool>", "<tool|>", "<|tool_call>", "<tool_call|>", "<|tool_response>", "<tool_response|>", "<|\"|>", "<|think|>", "<|channel>", "<channel|>", "<|turn>", "<turn|>", "<|image>", "<|audio>", "<|image|>", "<|audio|>", "<image|>", "<audio|>", "<|video|>", "<start_of_turn>", "<end_of_turn>", "<bos", "bos>", "< bos >", "<BOS>", "<Bos>", "<eos><bos>", "<eos><eos>", "<|tool><tool|>", "<|tool", "<|tool>>", "<<bos>>", "<bos>text<eos>", "<bos>", "<0x0A>", "<0xFF>", "<unused0>", "[multimodal]", "<|im_start|>", "<s>", "</s>", "<|endoftext|>", "▁", "▁▁", "▁<bos>▁", "<▁bos>"]CONTROLS = ["\x00", "\x01", "\x07", "\x08", "\x0b", "\x0c", "\x1b", "\x1f", "\x7f", "\x85", "\xa0", " ", " ", " ", "", "", "", "", "", "
", "
", "", "", "", "", "", " ", "", "", "", "�", "", "\U000e0001", "\U0010ffff", "\U0001d11e", "\U00020000", "\U0002a6d6", "\U0003134a"]ACCENTED = ["crème brûlée à la façon de {town}", "Ñandú y pingüino en el Río Zañartú", "Søren Ångström, Þórður Ægir", "Łódź, Žilina, Ștefan, Țara", "ẞtraße, naïve, coöperate, résumé, exposé", "Việt Nam, Hồ Chí Minh? Đà Nẵng!", "Džemal, Ljubljana, Njegoš", "Ḥamid, Ṣalāḥ, Ṭāriq, Ẓāhir", "Ễ ô̂ ạ̈ ṓ ṩ ǟ", "Ά έ ή ί ό ύ ώ ϊ ϋ ΐ ΰ"]WS = [" ", "\n", "\t", "\r\n", "\r", " \t", " ", "\xa0", "\n\n"]
def fill(tpl: str, r: random.Random) -> str: o = r.choice(ORGS) return tpl.format(p=f"{r.choice(FIRST)} {r.choice(LAST)}", p2=r.choice(FIRST), o=o, o2=o.split()[0].lower()[:6], t=r.choice(THINGS), c=r.choice(COSE), d=r.choice(DAYS), mo=r.choice(MONTHS), town=r.choice(TOWNS), n=r.randint(2, 98), m=r.randint(10, 999), m2=r.randint(1, 59), h=r.randint(7, 21))
def sentence(r: random.Random, langs=None) -> str: langs = langs or ["en", "en", "it"] + list(OTHER) lang = r.choice(langs) pool = {"en": EN_TPL, "it": IT_TPL}.get(lang) or OTHER[lang] return fill(r.choice(pool), r)
def rand_scalar(r: random.Random) -> str: """A random assigned-or-not scalar from a spread of blocks (never a surrogate).""" ranges = [(0x20, 0x7e), (0xa0, 0x24f), (0x370, 0x3ff), (0x400, 0x4ff), (0x590, 0x6ff), (0x900, 0x97f), (0xe00, 0xe7f), (0x1100, 0x11ff), (0x2000, 0x206f), (0x2190, 0x23ff), (0x2500, 0x27bf), (0x3000, 0x30ff), (0x4e00, 0x9fff), (0xac00, 0xd7a3), (0xe000, 0xf8ff), (0xfb00, 0xfeff), (0x10000, 0x1049f), (0x1f300, 0x1faff), (0x20000, 0x2a6df), (0xe0000, 0xe007f), (0x30000, 0x3134f), (0xf0000, 0xffffd), (0x100000, 0x10ffff)] lo, hi = r.choice(ranges) return chr(r.randint(lo, hi))
def build(r: random.Random) -> list[dict]: out: list[dict] = []
def add(cat, text, kind=None): out.append({"cat": cat, "kind": kind or r.choice(["d", "q", "raw"]), "text": text})
for _ in range(420): add("en", " ".join(sentence(r, ["en"]) for _ in range(r.randint(1, 4))), "d") for _ in range(220): add("it", " ".join(sentence(r, ["it"]) for _ in range(r.randint(1, 3))), "d") for lang in OTHER: for _ in range(14 if lang in ("ar", "th", "hi", "zh", "ja", "ko") else 7): add(lang, " ".join(sentence(r, [lang]) for _ in range(r.randint(1, 3)))) for _ in range(300): add("query", fill(r.choice(QUERIES), r), "q") for _ in range(200): add("code", "".join(fill(r.choice(CODE), r) for _ in range(r.randint(1, 3))), "d") # CRLF and mixed line endings for _ in range(160): s = "\r\n".join(sentence(r) for _ in range(r.randint(1, 5))) s = r.choice([s, s + "\r\n", "\r\n" + s, s.replace("\r\n", "\r", 1), s.replace("\r\n", "\n\r", 1), s + "\r"]) add("crlf", s) # NFD / NFKC / mixed normalization forms for _ in range(160): base = fill(r.choice(ACCENTED), r) + " " + sentence(r, ["it", "fr", "pl", "vi", "tr", "cs", "hu", "el", "ru"]) form = r.choice(["NFD", "NFD", "NFKD", "NFKC", "NFC"]) s = unicodedata.normalize(form, base) if r.random() < 0.4: # mix NFC and NFD halves s = unicodedata.normalize("NFC", base[: len(base) // 2]) + unicodedata.normalize("NFD", base[len(base) // 2:]) add("nfd", s) # emoji sequences for _ in range(220): parts = [r.choice(EMOJI) for _ in range(r.randint(1, 8))] words = sentence(r, ["en"]).split() s = " ".join(w + (r.choice(parts) if r.random() < 0.3 else "") for w in words) if r.random() < 0.5 else "".join(parts) add("emoji", s) # scripts with combining marks and no spaces for lang, n in (("ar", 100), ("fa", 40), ("he", 30), ("th", 110), ("hi", 100), ("bn", 40), ("ta", 40), ("zh", 70), ("zh_tw", 30), ("ja", 70), ("ko", 70), ("ka", 20), ("hy", 20), ("am", 20)): for _ in range(n): s = sentence(r, [lang]) if lang in ("ar", "fa") and r.random() < 0.5: s = "".join(c + (r.choice("ًٌٍَُِّْ") if r.random() < 0.3 and "ء" <= c <= "ي" else "") for c in s) if r.random() < 0.3: s = s + " " + sentence(r, ["en"]) add(lang + "_x", s) # whitespace runs > 31, tabs, newline runs, mixed for _ in range(180): ws = r.choice(WS) k = r.choice([31, 32, 33, 40, 63, 64, 65, 100, 127, 128, 129, 200, 500]) a, b = sentence(r, ["en", "it"]), sentence(r, ["en"]) s = r.choice([a + ws * k + b, ws * k + a, a + ws * k, ws * k, a + " " * k + "\t" * (k // 3) + b, (ws * k + a) * 2]) add("whitespace", s) for _ in range(100): s = "\t".join(sentence(r).split(" ")[: r.randint(1, 12)]) s = r.choice([s, "\t" + s, s + "\t\t", s.replace("\t", "\t \t", 1), s.replace("\t", " \t ", 2)]) add("tabs", s) # control characters and invisible code points for _ in range(120): words = sentence(r).split(" ") s = " ".join(w + (r.choice(CONTROLS) if r.random() < 0.35 else "") for w in words) s = r.choice([s, r.choice(CONTROLS) + s, s + r.choice(CONTROLS), "".join(r.choice(CONTROLS) for _ in range(r.randint(1, 12)))]) add("control", s) # special-token look-alikes, alone, embedded, adjacent, repeated for _ in range(170): toks = [r.choice(LOOKALIKES) for _ in range(r.randint(1, 5))] words = sentence(r, ["en"]).split(" ") pos = r.randint(0, len(words)) s = r.choice([" ".join(words[:pos] + toks + words[pos:]), "".join(toks), " ".join(toks), "".join(words[:pos]) + "".join(toks) + "".join(words[pos:]), toks[0] * r.randint(2, 6)]) add("lookalike", s) # very short / empty for s in ["", " ", " ", "\n", "a", "é", "é", "▁", "▁a", "🙂", "<", ">", "|", "title:", "task:", "none", "x" * 1, "\u0000", "", "", "\U0010ffff", "0", "٠١٢٣", "१२३", "一", "あ", "한"]: add("short", s, "raw") add("short", s, "d") # random scalar soup (assigned, unassigned, private use, non-characters) for _ in range(220): s = "".join(rand_scalar(r) for _ in range(r.randint(1, 60))) add("soup", s) # mixed everything for _ in range(200): bits = [sentence(r), r.choice(EMOJI), r.choice(LOOKALIKES), r.choice(CONTROLS), r.choice(WS) * r.randint(1, 40), fill(r.choice(CODE), r), unicodedata.normalize("NFD", fill(r.choice(ACCENTED), r)), "".join(rand_scalar(r) for _ in range(5))] r.shuffle(bits) add("mixed", "".join(bits[: r.randint(2, 8)])) # very long inputs (5k .. 80k characters) for i in range(22): n = [5000, 8000, 12000, 20000, 30000, 50000, 80000][i % 7] parts = [] while sum(len(p) for p in parts) < n: parts.append(r.choice([sentence(r), fill(r.choice(CODE), r), r.choice(EMOJI), r.choice(WS) * r.randint(1, 50), "".join(rand_scalar(r) for _ in range(20)), r.choice(LOOKALIKES)])) add("long", " ".join(parts), "d") return out
def main() -> None: ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--tokenizer", required=True, help="path to tokenizer.json") ap.add_argument("--out", required=True) ap.add_argument("--seed", type=int, default=20261012) args = ap.parse_args() from tokenizers import Tokenizer
tok = Tokenizer.from_file(args.tokenizer) cases = build(random.Random(args.seed)) for c in cases: c["ids"] = [2] + tok.encode(PREFIX[c["kind"]] + c["text"], add_special_tokens=False).ids + [1] Path(args.out).write_text(json.dumps(cases, ensure_ascii=False), encoding="utf-8") from collections import Counter print(f"{len(cases)} cases, {sum(len(c['ids']) for c in cases)} ids ->", args.out) print(dict(Counter(c["cat"] for c in cases)))
if __name__ == "__main__": main()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333{ "reference": "sentence-transformers fp32 on CPU, original checkpoint, text only", "versions": { "sentence_transformers": "6.1.0", "transformers": "5.19.0", "torch": "2.14.1" }, "threshold_cosine": 0.999, "cases": [ { "kind": "search_query", "text": "When does the warehouse lease in Brindlemoor expire?", "embedding": [ 0.0140514, 0.0200849, 0.0201675, -0.0119595, -0.035274, 0.0034859, -0.0322359, 0.0260318, -0.0253003, -0.1322919, 0.0172573, -0.0929851, -0.0345029, 0.0034794, 0.0074408, -0.0231558, -0.0121178, 0.0041102, -0.026006, -0.0039767, -0.0500095, -0.0474389, 0.0046259, -0.0144053, 0.0104898, -0.0104195, -0.0212026, -0.011046, -0.0204922, 0.1905717, -0.0039958, -0.0352325, -0.0434769, -0.0078017, 0.0146712, -0.0420867, -0.0191855, 0.0736523, 0.0112718, 0.0299183, -0.0180284, 0.0087481, -0.0032612, -0.0007796, 0.0054152, 0.0057414, 0.0320906, 0.0198112, -0.0058027, 0.0055605, -0.011155, 0.0018056, -0.0036217, 0.0599205, -0.0395265, 0.0071835, 0.0045348, 0.0249366, 0.0060176, -0.0363902, -0.0180425, -0.0019947, 0.013761, 0.0338091, -0.0665499, -0.0047037, 0.0470355, -0.0196299, 0.0428396, 0.0634703, 0.0617316, 0.0512214, 0.0480056, 0.0396131, -0.0358877, -0.0718305, -0.102156, 0.0629865, -0.0244289, -0.0405096, 0.0616369, 0.0004009, -0.0722917, -0.0258843, -0.037575, -0.0455716, -0.0343866, 0.0075406, 0.035479, 0.0328424, -0.008169, 0.0508599, -0.0598774, -0.0104931, -0.067505, 0.0006392, -0.0198671, 0.0011432, 0.0123858, -0.0174422, 0.0056807, -0.0079437, -0.0044856, 0.0179937, 0.0756269, 0.0363916, -0.0265536, 0.0695819, 0.040324, -0.0622708, -0.064421, -0.2657686, -0.0183439, 0.0499475, 0.0405476, 0.2633377, -0.0433234, -0.0144355, -0.0109114, 0.0286458, 0.0041431, 0.0063956, -0.0419718, 0.0039493, -0.057251, -0.0082195, 0.0578189, 0.0176498, 0.0127398, 0.0349538, 0.0112667, -0.0230163, 0.0316641, 0.0500062, -0.0358909, -0.0121331, -0.0285246, -0.0099984, -0.0417251, -0.015272, -0.0617455, -0.0135424, 0.015151, 0.0473345, -0.0048397, 0.0277582, -0.0336748, -0.032164, 0.0029421, 0.0061318, 0.0053797, -0.0440732, -0.0239873, 0.0194475, 0.0319975, 0.0183903, 0.0482337, -0.007429, -0.0113159, -0.0271933, -0.0209385, 0.002265, 0.017666, 0.0208267, 0.0063856, 0.0297219, -0.0184708, 0.025398, 0.0026902, 0.0512845, -0.0034231, -0.0198501, -0.000227, -0.0331328, 0.0207864, 0.0023767, 0.0558418, -0.0101271, -0.0198393, 0.0030498, -0.0618708, -0.0085252, -0.0102823, -0.0304731, 0.0326227, 0.0539232, -0.0254712, 0.003854, -0.0184922, -0.0071012, 0.0024879, 0.0039352, 0.0036627, 0.0092597, -0.0218288, 0.0062251, 0.0374328, -0.012431, -0.0118201, 0.0184596, 0.0124813, -0.0153233, 0.0424144, -0.0017183, -0.0481435, 0.0003325, -0.0335241, 0.0318867, 0.0052809, -0.0104564, 0.0022734, -0.0028741, 0.0161565, -0.0500812, 0.0328645, 0.0240628, -0.0186112, 0.0400808, 0.0222267, -0.025151, -0.006919, 0.0431995, 0.0789402, 0.0139404, -0.0049106, -0.0538642, 0.0063854, -0.0147278, -0.0038739, 0.056846, -0.0180968, 0.0210576, -0.0370839, -0.088229, 0.0906495, -0.0048958, -0.0581601, 0.0112045, -0.0083936, 0.0677319, -0.0318846, -0.0155604, 0.0173322, 0.061947, -0.0243701, -0.0212206, -0.0025019, 0.0102068, 0.0122189, -0.0119761, 0.0699356, -0.0088522, -0.0546333, -0.0722661, -0.015771, -0.0367368, 0.0324103, -0.0347873, 0.0060024, 0.0400301, -0.0096935, -0.0621471, -0.0305385, 0.024493, 0.004069, 0.0410262, -0.0622035, 0.0020669, 0.0152834, -0.0156231, 0.0165897, 0.0094615, 0.0054113, 0.0058183, 0.0291583, 0.0337521, -0.0329887, 0.044419, 0.0099162, -0.0130374, 0.0149398, -0.0300086, -0.0443118, 0.0060803, -0.0163578, 0.0493293, -0.0172757, 0.0178506, 0.0246122, 0.0469199, 0.0338066, -0.035952, 0.0218767, 0.0120018, -0.028764, 0.032157, -0.0097965, 0.0423428, 0.0040818, 0.0187548, -0.0104548, 0.0285643, -0.0246368, -0.032603, 0.0659568, -0.0442716, -0.0681514, -0.022396, -0.0112234, -0.0442733, -0.0107434, -0.0283538, -0.0115352, -0.02458, 0.0270112, 0.0093793, -0.0185534, 0.0137447, 0.0031274, -0.079896, -0.0106351, 0.0440398, -0.0201902, -0.0351441, 0.0031468, 0.0466262, -0.0217505, -0.0193922, 0.0082847, -0.033297, -0.0407191, 0.0030198, -0.0266054, 0.0127826, 0.0090996, 0.0228752, 0.0433155, 0.0605334, 0.0233076, -0.0209052, -0.021734, -0.1376118, -0.062436, 0.0284235, 0.021142, -0.0097345, 0.003243, 0.0095441, -0.0628982, 0.0300183, -0.0005559, 0.0380125, 0.0286191, -0.0209423, -0.0044779, 0.0035791, 0.0474121, 0.0384367, 0.00689, 0.0448739, -0.0003337, 0.0272092, -0.0048142, 0.0293282, -0.0084391, -0.0278422, -0.0488862, 0.0090513, -0.0323157, 0.0215345, -0.0074275, 0.013417, -0.0262015, 0.0041043, 0.0173136, 0.0766509, 0.0014924, 0.0104271, -0.0027695, -0.0264011, 0.0729229, 0.0227126, -0.0357576, 0.0224196, 0.0429215, -0.0572116, 0.030736, -0.0222465, 0.0291479, 0.0198262, 0.0098066, 0.2381135, 0.0009308, -0.0170637, 0.0399403, 0.0244772, -0.0218695, 0.013202, -0.0195465, -0.009753, 0.0249087, 0.0040551, -0.0182575, 0.0128712, -0.0166567, -0.0183231, -0.0034079, -0.0336708, -0.0235771, 0.0139053, -0.0131362, 0.0313214, 0.0032543, -0.0036488, -0.0122029, -0.0135203, -0.0332577, -0.0270707, -0.0539252, -0.0376571, 0.0039163, 0.0268505, -0.009067, -0.0237761, 0.0446752, 0.015811, -0.0068362, 0.0052214, -0.004454, 0.0399815, 0.023899, -0.031928, -0.0199918, 0.0048249, 0.0395325, 0.0104519, 0.0263888, -0.0090891, -0.0206043, -0.0156751, 0.0082406, -0.0057344, -0.0070633, 0.0328125, 0.0125726, -0.0467525, -0.0043885, -0.0173589, -0.0055486, 0.0196809, -0.0101459, -0.0079408, 0.0167622, 0.0093635, -0.0186476, -0.0031106, -0.0500078, -0.0372527, 0.0440114, 0.008821, -0.0034126, -0.0175557, -0.005297, -0.0138077, 0.0122492, 0.0161653, -0.0163164, -0.0130119, -0.0301623, -0.0263716, -0.0042851, 0.0190618, -0.0150112, 0.0062657, -0.0044136, -0.0191775, 0.0154332, 0.0233396, 0.0147409, -0.0016429, 0.0397183, -0.0267775, -0.0249655, -0.0084888, -0.0365686, 0.0376978, -0.0176997, 0.0466568, -0.0013101, 0.0168162, 0.0361352, 0.0076559, -0.038352, 0.0192798, -0.0334455, -0.0148572, 0.0290095, -0.0101722, -0.00072, 0.0198838, -0.0442767, 0.0014474, -0.024855, 0.0167283, -0.0336968, -0.0121257, -0.0358068, -0.1305676, 0.0021763, 0.0454077, 0.0026204, 0.040022, 0.0299537, 0.0282371, 0.0095981, -0.0063128, 0.0010373, 0.0163293, -0.0015985, 0.0251838, 0.0284173, -0.028472, -0.009489, -0.0275945, -0.0364336, -0.0283635, 0.0044131, 0.0035401, 0.0103233, -0.0083207, 0.0274399, 0.0147917, -0.0156048, -0.0215956, -0.0052218, -0.0390134, -0.0142176, -0.0004916, 0.0027648, -0.0229333, 0.0540044, -0.0389055, -0.003413, 0.0124384, -0.0203941, 0.0389145, 0.0146834, 0.0001552, -0.0018129, 0.0674664, 0.0412302, 0.0415348, -0.0023984, -0.0284114, -0.2059626, -0.0090965, -0.0156103, 0.0002269, -0.0365953, -0.0244425, 0.0057417, 0.048451, 0.0383219, -0.0023854, -0.0357098, 0.0177072, -0.021637, 0.0201444, -0.0428248, 0.0057723, -0.0342198, 0.0354257, -0.0277822, 0.0002803, 0.0401719, -0.0344315, 0.0256965, 0.0327575, 0.0532876, 0.0113293, -0.0113227, 0.0346123, -0.0359698, -0.0094106, 0.0203509, -0.0320784, -0.0114563, -0.0019832, -0.0448561, 0.0305796, 0.0081664, -0.0176064, 0.0073882, -0.015993, -0.0134097, -0.031895, 0.0357883, -0.0018839, -0.0136349, 0.0152895, -0.0087174, -0.013247, 0.0096113, 0.0168226, -0.0005419, -0.0159698, 0.0096558, -0.0327691, -0.0433982, -0.0066851, 0.0044284, 0.0213365, 0.0320568, 0.0022381, 0.0257285, 0.0143513, 0.0022292, -0.0166571, -0.0146441, -0.0404922, -0.0189938, -0.0101463, 0.0187055, 0.0037433, 0.0022404, -0.0062486, -0.0070731, 0.0032853, 0.0411538, -0.0119422, -0.0371468, 0.0130075, 0.0291758, -0.0188306, -0.0089265, 0.0263609, 0.0156603, -0.004102, -0.0210846, 0.0158581, -5.31e-05, 0.0059412, 0.0064431, -0.0194571, 0.0351863, 0.0506362, 0.0143477, 0.0433141, -0.0236757, 0.0323491, 0.018349, 0.0509639, 0.0147666, -0.0070182, -0.002872, -0.0116239, -0.0142457, 0.0087374, -0.0020458, 0.0024226, 0.0514395, -0.0161209, 0.0376469, 0.013314, -0.0078508, 0.0112804, -0.0185661, 0.0004649, -0.0206176, 0.0290994, -0.0127025, -0.0008931, -0.0125401, -0.011069, -0.01393, -0.0161357, -0.0261728, -0.0188071, 0.0273512, 0.0359035, -0.0228979, -0.0147417, 0.0048217, 0.0288668, 0.0073423, 0.0100393, 0.041307, 0.0198599, 0.0067079, -0.0030872, -0.0128413, 0.0158811, 0.0128299, 0.0302011, 0.009724, -0.0548747, 0.0092055, -0.0272114, -0.024287, 0.0015032, 0.0542283, 0.0184035, 0.02366, 0.0377193, 0.0221289, 0.0158905, 0.0122361, -0.0181141, -0.0064896, 0.0225127, 0.0133703, -0.0149417, -0.028168, 0.0244618, -0.0035144, -0.0209218, 0.0446331, 0.0127702, -0.0186521, 0.0005753, -0.0012075, -0.0183204, 0.0400032, 0.0197334, 0.0030626, -0.0455221, 0.0269828, -0.0142443, -0.0115392, 0.0330398, 0.0007724, 0.0340609, 0.2108322, 0.0039355, -0.0055149, 0.0161179, -0.0071948, -0.0103065, 0.0139283, 0.0374079, -0.0069936, 0.0435351, 0.0173975, 0.0105077, 0.0359319, -0.0259694, -0.0158281, -0.0211381, 0.029089, -0.0099361, 0.022188, 0.0078775, 0.0127904, -0.0391305, -0.0249713, 0.0108958, -0.0368597, 0.0160098, -0.0362015, 0.0072925, -0.0076356, 0.0512759, -0.0074541, -0.0033625, -0.0056429, 0.0345776, -0.016772, 0.016576 ] }, { "kind": "document", "text": "The warehouse lease in Brindlemoor runs until the last day of March; the landlord agreed to fix the window before then.", "embedding": [ 0.0264644, -0.0242984, 0.0116434, -0.0094456, -0.0538755, -0.0034127, 0.01068, 0.0363724, -0.0184347, -0.1608366, 0.0270172, -0.0312475, 0.0268842, 0.0346971, -0.0035566, -0.0575296, -0.0185978, -0.0473271, -0.0191439, -0.006971, -0.0709102, -0.0006216, -0.0111378, -0.0304192, 0.0180717, -0.0026377, -0.0228687, -0.0221057, -0.0491751, 0.173117, -0.0081147, -0.0180295, -0.0083443, -0.0130251, 0.0615742, -0.0135436, -0.0290864, 0.0527058, -0.0001754, 0.0241153, -0.0342266, 0.0070813, -0.0127815, 0.004976, 0.0269828, 0.0039893, 0.0317817, 0.0179321, -0.0148058, 0.0165759, 0.0083154, 0.0053715, -0.000684, 0.0518343, -0.0072164, 0.0328747, 0.0133487, 0.0612634, -0.0099483, -0.0133693, 0.0050172, -0.0203206, 0.0005791, 0.0136871, -0.0553643, 0.0312551, 0.0846288, -0.0247101, 0.0219906, 0.0775483, 0.0239536, -6.89e-05, 0.0347904, 0.0412152, -0.0377688, -0.0377716, -0.107596, 0.0521024, -0.0165523, -0.0078278, 0.0289683, 0.0188598, -0.0292757, 0.0088558, 0.0036091, -0.052127, 0.0031963, 0.012254, -0.0349788, 0.0253031, 0.0281118, 0.0375488, -0.0521213, 0.0173015, -0.0504028, 0.0084912, 0.0133818, 0.0270663, -0.0064787, 0.0219093, 0.0043792, 0.0296521, -0.0094517, 0.0395419, 0.0519162, 0.0129672, -0.0317267, 0.0651949, 0.0392844, -0.0268617, -0.0067156, -0.2840129, -0.0304729, 0.0118219, 0.044337, 0.3071545, -0.0170552, -0.0233961, 0.0009735, 0.0657148, -0.0084815, 0.0454244, -0.0533229, 0.0050689, -0.0576392, -0.0074852, 0.0658127, -0.0100089, -0.008696, 0.0184354, -0.0248318, -0.0052167, 0.0300652, 0.0693946, -0.0166476, -0.0157638, -0.031577, -0.0206295, -0.0082261, -0.0169719, -0.0148738, 0.0184738, 0.0035616, 0.0650747, -0.0467764, 0.0229762, -0.019042, 0.0011456, 0.0141589, -0.0006831, 0.0390894, -0.0008999, -0.0218775, -0.0199317, 0.0193458, 0.0177277, 0.0344862, 0.025085, -0.0311837, -0.0123519, -0.030943, 0.0107378, 0.0289433, 0.008099, -0.0235138, 0.0252559, -0.06868, 0.017455, 0.0419686, 0.0408476, 0.0254791, -0.0242629, -0.0216531, -0.0102706, 0.0281387, -0.0112468, 0.0444572, -0.0275173, 0.0002826, 0.0086426, -0.0212904, -0.0579055, 0.0215879, -0.0140405, 0.0252201, 0.0211788, -0.0142696, -0.0234745, -0.0227191, 0.0323166, 0.0026156, 0.0010938, 0.0220431, -0.0217016, -0.0015277, -0.0262872, 0.0427692, 0.0093728, 0.0139956, 0.0309493, 0.0226167, -0.0272248, 0.0383349, 0.0175825, -0.0447357, -0.001296, -0.0477331, -0.0072662, -0.0054068, -0.019633, 0.0323781, -0.0285428, 0.0375163, 0.000403, -0.0015404, 0.0526022, 0.0112267, 0.0903424, -0.0030999, -0.0105379, 0.0019585, 0.0412905, 0.0871157, 0.0094184, -0.023252, -0.0291806, 0.0024612, -0.0049254, -0.0248866, 0.0566803, 0.0235696, 0.0388389, -0.0158421, -0.0421797, 0.0584066, 0.0114919, -0.0320118, -0.010658, -0.0164602, 0.0407591, -0.0285565, 0.0040359, -0.0325882, 0.0494892, -0.0049442, -0.0034794, 0.0059856, -0.0019577, 0.0305299, -0.0129538, 0.0508744, 0.0091949, -0.0272065, -0.0722709, 0.0146036, -0.0279869, 0.042139, -0.0165828, -0.0148761, 0.0239786, -0.0248741, -0.037085, -0.0145703, 0.0046245, -0.0166312, 0.0376871, -0.0232947, -0.0080422, -0.0138715, 0.0056189, 0.0084751, -0.0106014, -0.0011364, -0.004992, 0.0041779, 0.0412414, -0.0332662, 0.0296032, -0.0010485, -0.0027399, 0.0034219, -0.0054038, -0.0211052, 0.0092755, -0.0005159, 0.0146269, -0.0139352, 0.0105331, 0.0190807, 0.0334958, 0.0569536, -0.0163442, 0.0040735, 0.0267959, -0.020799, 0.0280741, -0.0202714, 0.037818, 0.0111139, 0.0095929, -0.0142725, -0.0020434, -0.0200119, 0.0226144, 0.0670394, -0.010802, -0.0725678, -0.0035054, -0.0368582, -0.0428894, -0.0163187, -0.0220368, -0.02653, -0.0019634, 0.0407205, 0.0271887, -0.0069772, -0.0075532, -0.0117692, -0.0747322, -0.0010772, 0.0234839, -0.0173247, -0.025903, 0.00921, 0.0320551, -0.0262158, 0.0017413, -0.0307316, -0.0400242, -0.0027447, -0.0017768, -0.0218839, 0.0246199, 0.0165034, -0.0065996, 0.0269161, 0.0112707, 0.0273772, -0.0098787, -0.0179007, -0.1398847, -0.010778, 3.89e-05, 0.0183281, 0.0210552, 0.0155068, 0.0270709, -0.0392508, 0.0251768, 0.0052068, 0.0513872, 0.0119341, -0.0019493, -0.0304656, 0.0209176, 0.0601201, 0.0198429, 0.0126918, 0.0168446, -0.0221235, 0.0020831, 0.0003669, 0.0489466, -0.0236555, -0.0086362, -0.0242799, 0.016296, -0.0046918, 0.0240075, 0.0036074, 0.0125185, 0.0037332, 0.0026463, 0.0287793, 0.0442534, -0.0008671, -0.0068142, -0.0095245, 0.0267455, 0.0584806, 0.0285134, 0.0102658, 0.0434944, 0.0367759, -0.0005722, 0.0451684, 0.0049044, 0.0289554, 0.0256482, -0.0042619, 0.286271, 0.0199974, 0.0035183, 0.0286582, 0.0405934, -0.0167135, 0.0291236, -0.0018019, -0.0219017, 0.003888, 0.0021741, 0.0179549, -0.0181287, -0.003402, 0.0241312, -0.0023668, -0.0459792, 0.0055813, -0.005714, -0.0212274, 0.0166356, 0.0252439, 0.0101637, 0.0045283, -0.0259132, -0.0325882, -0.0176896, -0.0400428, 0.0077737, -0.0154222, 0.0193038, -0.0162069, 0.0035849, 0.0416583, 0.009458, -0.0236569, 0.0048055, -0.0021436, 0.0053086, 0.0072111, 0.0059566, -0.0258894, 0.0079106, 0.0285769, 0.0098245, 0.036957, 0.0063089, -0.0089957, 0.0008741, 0.0068467, -0.0078629, -0.0002527, -0.0080054, -0.0158927, -0.0252407, -0.0111276, -0.028465, -0.0106257, 0.0402076, 0.0120167, 0.0064657, 0.0467545, -0.0183397, -0.000893, -0.0129492, -0.0484937, -0.0369712, 0.0168339, 0.0526176, -0.0011955, -0.0661879, -0.0166612, -0.0033391, 0.0409913, -0.0211322, 0.003656, -0.0014469, -0.0320232, -0.0187179, -0.0282663, 0.0106685, 0.0107463, -0.0215654, -0.0187622, -0.0152856, 0.0209789, 0.0112883, 0.0126507, -0.0026165, 0.0558417, -0.0469727, -0.0207666, -0.0208326, -0.0426097, -0.00665, 0.0050596, 0.0485706, -0.0045574, 0.023139, 0.0228496, -0.001325, -0.0225157, 0.0214948, -0.0123868, -0.0100371, 0.0241164, -0.0104662, -0.0054405, 0.0180619, -0.0388385, 0.005823, 0.0250867, 0.0443838, 0.0081462, -0.0252075, 0.0075606, -0.134257, 0.0238103, 0.0403092, -0.033447, 0.0609221, 0.0102637, 0.031284, -0.0042293, 0.0090502, 0.0206708, 0.0153386, -0.0074572, 0.0034939, -0.0141006, -0.0052234, 0.0150197, -0.016243, -0.0435209, -0.0013051, 0.0232986, -0.0497696, -0.0017224, -0.0076917, 0.0083871, -0.0113315, -0.0265682, -0.0089959, -0.0379045, -0.0246575, -0.0098334, -0.004501, 0.0203684, -0.0072931, 0.0307912, 0.0030027, -0.0012177, 0.0056174, -0.0066306, 0.017854, -0.0046366, -0.0017239, 0.0141466, 0.0568046, 0.0336952, 0.0252416, 0.0015724, -0.0107673, -0.2303559, -0.0640194, 0.002422, 0.0279597, -4.16e-05, 0.0091563, -0.0084589, 0.0591685, 0.006867, 0.0021346, -0.0314672, 0.0068378, -0.0169414, 0.011671, -0.0496234, 0.0066084, -0.022313, 0.025086, -0.0206278, -0.0088476, 0.0645719, -0.0277695, 0.033116, 0.0418436, 0.0363094, 0.0308525, -0.0173255, -0.0032406, 0.0002146, -0.0165661, -0.0121446, -0.031358, -0.0283029, 0.0191103, -0.0352197, 0.0368655, 0.025689, -0.0201068, 0.0135953, -0.0169755, -0.0089089, -0.0187155, 0.0421347, 0.0302731, 0.0062562, -0.009368, -0.0101766, -0.0238247, 0.0058758, -0.0011821, 0.0071059, -0.0118324, 0.0148747, -0.0112144, -0.0351373, -0.0065663, -0.0335906, 0.013, 0.0116598, 0.0019158, -0.0010235, 0.0066092, -0.0084019, 0.0062142, -0.0070109, -0.0236729, -0.0142321, -1.57e-05, 0.0091445, 0.0293063, 0.0035479, 0.0109397, -0.0091237, 0.0246063, 0.0384086, -0.0161373, -0.0127944, 0.0076104, 0.0206074, 0.0328081, 0.0080331, 0.0056978, -0.0059247, 0.008972, -0.014289, 0.0126287, -0.0137993, 0.0074459, 0.0032179, -0.0166674, 0.0199677, 0.025108, -0.0031548, 0.0136954, -0.0395122, 0.041827, 0.002465, 0.050934, 0.0119671, -0.0135284, 0.0051843, -0.0149521, 0.0001085, -0.0003902, -0.0051798, 0.0069398, 0.0121671, -0.0026059, 0.0065326, -0.0162301, -0.0196698, 0.0183328, -0.0255839, 0.0067643, -0.023012, 0.0014238, -0.0116004, -0.0156751, -0.0077851, -0.0268469, -0.0117132, -0.0267632, -0.012754, -0.0475532, 0.009542, 0.0140083, -0.0277571, -0.0022977, 0.0071351, 0.0189572, -0.0126652, 0.0310627, 0.0322423, -0.0091997, 0.0089104, -0.0056425, -0.024342, 0.0111729, 0.0005422, 0.0169579, 0.0189936, -0.0415399, 0.021819, -0.0405018, 0.0107911, -0.0077887, 0.048318, 0.0013121, 0.017062, 0.0367175, 0.0018443, -0.0108327, 0.0287105, -0.024023, -0.0027745, 0.0187717, 0.0018345, -0.0108974, -0.0224133, 0.0256002, -0.0080634, -0.0265149, 0.0517693, 0.0269079, 0.0251603, 0.0053892, -0.0136671, 0.0394292, 0.0463492, 0.0071633, -0.0029102, -0.0272801, 0.0164657, -0.0011746, 0.0025788, 0.0265455, -0.0069759, 0.0037236, 0.2317612, 0.0251917, -0.0107766, -0.017, -0.0060105, -0.0122113, 0.0047453, 0.0309908, -0.0087099, 0.0553553, 0.012144, 0.0458252, 0.046074, -0.0357232, -0.0233153, 0.0017353, -0.0060374, -0.0035919, 0.020817, 0.0342276, 0.0057531, -0.0323938, -0.0293829, 0.0287774, -0.0663916, 0.0148558, -0.0343938, 0.0079967, -0.0117679, 0.0250412, 0.0329488, -0.0064652, -0.0493818, 0.0227841, -0.0245425, 0.0195178 ] }, { "kind": "document", "text": "Lista della spesa: latte d'avena, due limoni, riso basmati, coriandolo e croccantini per il cane.", "embedding": [ 0.0170374, -0.044635, -0.0047019, -0.0229727, 0.0358784, 0.0070688, 0.0636706, -0.0095134, 0.0034211, -0.0978533, 0.02883, 0.0047634, -0.045708, -0.0344993, 0.0595915, -0.0239481, 0.0599293, -0.0019344, 0.0037584, 0.0237904, -0.0470828, 0.0045741, -0.0460006, 0.0576605, 0.0114495, -0.012251, 0.0123977, -0.0349354, -0.018569, 0.1733741, -0.0089963, -0.023553, -0.0083854, -0.0460702, 0.0420627, -0.0377996, -0.0119087, -0.0360215, -0.0097449, -0.0203342, -0.0063737, -0.0413225, 0.0101058, 0.0178437, 0.0132315, 0.0265682, 0.0069234, 0.026995, 0.003121, 0.0125665, 0.1006458, -0.0385414, 0.0088992, 0.1112718, 0.0027232, 0.0248341, 0.0098803, 0.0200804, 0.0097934, 0.0446817, 0.0187769, 0.0145933, -0.0395547, -0.021266, -0.0572196, 0.0402977, 0.0587767, 0.0088406, -0.0020986, -0.042525, -0.0556863, 0.0168584, 0.0026634, 0.0761056, -0.0228929, -0.0022126, -0.1013387, -0.0081393, -0.0288168, 0.0312328, -0.0225599, -0.0985526, 0.0337848, 0.0062871, 0.0147591, -0.0307018, -0.0281429, -0.0074711, -0.0092093, 0.0195643, -0.0058784, 0.0078961, -0.0018558, -0.0332802, -0.0126549, 0.0003527, -0.0325215, 0.0357396, -0.0350979, 0.0203608, 0.0657294, -0.0410924, -0.0014359, 0.0259345, -0.027136, 0.0457944, -0.0290958, 0.0276919, -0.0201393, -0.0269499, -0.0059097, -0.238396, -0.012558, 0.0606131, -0.0038405, 0.3050316, 0.0053073, -0.0133485, 0.0361453, 0.0484401, -0.0515617, 0.0310277, -0.0136832, -0.0556593, -0.0321827, -0.0284951, 0.0439218, -0.0254445, -0.0127552, 0.0721373, 0.0206097, 0.0132098, -0.0089761, 0.0079829, -0.0075321, 0.0093124, -0.0187054, -0.0464838, 0.057731, 0.0051577, 0.0629364, 0.0032504, 0.0047574, 0.0042688, -0.0160958, 0.0298165, -0.0073184, -0.0608206, 0.0457134, 0.0437723, -0.0025557, -0.0156474, -0.0127475, 0.0342485, -0.0275448, -0.001125, 0.0481392, -0.0004659, 0.015135, 0.0579678, -0.0078953, 0.0246101, -0.0220383, 0.026813, 0.0231499, -0.0199915, 0.00961, -0.0206561, 0.0142155, 0.0067647, 0.0095087, 0.0523173, -0.0174483, 0.0398959, 0.0364616, 0.0350139, -0.0067026, -0.0409621, 0.0211005, -0.0012647, -0.0170568, -0.0003618, -0.0184823, -0.0114733, 0.0172884, 0.0080164, -0.0111308, 0.0265054, -0.0213923, -0.0088149, 0.0117311, -0.0181282, 0.0290931, 0.007055, -0.0008388, 0.0112384, 0.0431553, 0.0231443, 0.0070247, 0.0454419, 0.0255751, 0.0369707, -0.003059, -0.0032789, -0.0741629, -0.0015363, 0.0230501, 0.0400231, -0.0153894, -0.0068579, 0.0070991, -0.0205951, -0.0253457, 0.0338322, 0.0225516, 0.0175246, -0.0288264, 0.0823566, 0.0081965, -0.0106182, 0.0156874, 0.0001789, 0.0148504, 0.0117717, 0.0237754, -0.0086117, 0.0141754, 0.040231, -0.0331057, 0.0583299, 0.0457594, 0.0117246, 0.0342566, -0.0196156, 0.0901752, -0.0388455, -0.0651512, 0.0140004, 0.0339629, 0.042578, -0.0354294, 0.0235349, -0.0210134, 0.0416971, 0.0437426, -0.0424866, -0.0298579, 0.0389892, 0.0169898, -0.0264389, 0.034758, 0.0391694, 0.0372253, -0.0370816, -0.009757, -0.0709113, 0.0470219, 0.0118337, -0.0328658, -0.0113846, -0.0286826, 0.0172676, 0.0031711, -0.0201598, 0.0341731, 0.023543, 0.0063189, 0.0039108, 0.0083759, 0.0220321, -0.0339149, 0.0470052, 0.0067079, -0.0155743, 0.0610002, 0.0137803, -0.0104527, 0.0080595, -0.0055056, -0.0366666, 0.0390951, 0.0451955, 6.55e-05, -0.0554373, 0.0325824, 0.0034411, -0.0094541, 0.0053737, -0.0002473, -0.0061789, -0.007296, -0.0111808, -0.0176897, 0.0280049, 0.0177174, -0.0380043, -0.0497624, -0.0087323, 0.0110018, -0.0265812, 0.0269822, 0.0264606, 0.009159, -0.0071128, 0.0436609, 0.0021422, 0.0072964, 0.0140107, -0.0042789, 0.0438331, -0.0138949, -0.0173498, -0.0026295, -0.025416, 0.0098757, 0.0498686, 0.006806, -0.0143414, 0.0148724, -0.0585902, -0.0366992, 0.0212392, -0.0159478, -0.0019213, 0.0226183, 0.0002265, 0.0102534, 0.0026059, 0.043171, 0.0120144, -0.0175667, 0.0179332, -0.0314321, 0.0135192, 0.0274326, -0.0141854, -0.0160875, 0.0610543, 0.0045262, -0.0099408, -0.0283346, -0.1284437, 0.0084628, 0.0090422, -0.0450438, -0.0010222, -0.0016807, 0.0036075, -0.0028395, 0.0069319, 0.0187971, 0.0372987, -0.0047398, -0.0177076, -0.0372152, -0.0088774, 0.0449237, -0.031268, -0.0450276, 0.0441433, -0.0191669, 0.0403419, 0.0248082, -0.0168166, -0.0134252, -0.0117406, -0.0309384, 0.0038555, 0.0036268, -0.0663664, -0.0120338, 0.0290708, -0.0015897, 0.0098999, 0.0051596, 0.0095467, 0.0021275, 0.0100098, 0.0147187, 0.0123174, -0.0022185, -0.0318748, 0.0186827, -0.0021616, 0.0121651, -0.0037288, 0.003544, -0.0247473, -0.0347024, 0.0048945, -0.0175091, 0.2937374, 0.00277, -0.0044884, -0.0576349, -0.0149706, -0.0010073, -0.0486672, 0.0168386, -0.0088559, 0.0072639, -0.0239114, 0.0257517, -0.0026901, 0.018372, 0.0177089, 0.0407887, 0.002236, -0.019927, 0.0092718, -0.0381663, -0.031039, 0.0386571, 0.0191666, 0.0158292, 0.0121963, 0.0132033, 0.0107741, -0.0330748, -0.0244974, -0.0072312, -0.0226028, -0.0474324, 0.018386, 0.0055725, 0.0053387, -0.0067288, -0.0069327, 0.0388352, -0.0092138, 0.0017214, 0.0108645, -0.007365, -0.0308875, 0.0276345, 0.0030492, 0.0597437, 0.0149274, 0.0108632, 0.0063664, -0.0472365, -0.0659725, 0.0116199, -0.0311121, -0.0084875, 0.021202, -0.0144852, -0.0276581, 0.0346197, 0.0190425, 0.0021423, -0.0214823, 0.0042432, 0.0367772, -0.0352249, -0.0175849, -0.0085295, -0.0160579, -0.0221073, 0.0715203, -0.022603, 0.0121224, -0.0373269, 0.0218277, 0.0170323, -0.0286245, -0.0078001, 0.0256803, -0.0148012, 0.0099331, -0.0136366, 0.0587975, -0.0419719, -0.0031683, 0.0040288, -0.0167476, 0.0084212, 0.0180768, -0.0465007, -0.0154712, 0.0111867, -0.0109722, -0.0218118, -0.0080581, 0.0726468, -0.0298194, 0.0206009, -0.0122841, 0.0012644, -0.0214854, 0.020771, 0.0373861, -0.0202912, 0.0086824, 0.0104239, -0.0177915, -0.0195416, -0.0051046, 0.00611, 0.0131852, -0.0365019, 0.0227813, 0.0248264, -0.0207044, -0.0116764, 0.0259135, 0.0267124, -0.1291034, 0.0239644, 0.0625211, 0.0244442, 0.0325541, 0.0351543, 0.0217503, 0.0121765, 0.0449031, 0.0567871, 0.0054928, 0.0165023, -0.0382681, 0.0124143, 0.037944, 0.0284664, -0.0216116, 0.0124165, 0.0038941, -0.0059169, -0.0400646, -0.0100921, -0.0168236, 0.0026536, 0.0008066, 0.0205489, 0.0077813, -0.0524134, -0.0034027, -0.005347, -0.0321935, 0.0125179, -0.0294378, 0.0317069, 0.010248, -0.0071578, -0.0060076, -0.0125832, -0.0200377, 0.0289566, -0.0045058, -0.0214601, 0.0466605, -0.0174458, -0.037283, 0.0125868, -0.0232067, -0.2290279, 0.0035629, -0.0056789, 0.0374352, -0.0616575, -0.0058487, 0.0131172, 0.0539453, -0.0240027, -0.0390158, 0.0107827, 0.0038621, -0.0261863, 0.0437467, -0.0608736, -0.0023836, 0.0005415, 0.0291586, 0.0035115, -0.000339, 0.0223815, -0.0281816, 0.0592663, -0.007491, 0.0226631, 0.0274635, -0.0112232, -0.0176956, -0.021663, -0.0004892, -0.0213772, -0.0075747, -0.0309256, -0.0192184, 0.0389742, 0.0291572, 0.0186152, -0.0461713, 0.020704, -0.0140283, -0.0298755, -0.0019759, 0.0245542, -0.017876, -0.000494, -0.0161591, 0.0552246, 0.0046217, 0.0244851, -0.0494911, 0.0113249, -0.0002698, 0.022198, 0.0076108, 0.0061694, 0.0151072, 9.93e-05, 0.0397179, 0.0182748, -0.0288742, -0.0532453, 0.0270052, 0.038392, -0.001379, -0.0237011, 0.0293171, 0.004786, 0.0394925, 0.0042305, 0.0089678, 0.0208281, 0.0092142, -0.0111049, 0.0429073, -0.0086474, 0.004501, -0.042835, -0.0161324, 0.0090948, -0.0291586, -0.0044904, -0.0001088, 0.0094551, -0.0286622, -0.0575663, 0.0294203, -0.0009848, -0.0122502, -0.0377292, 0.0191204, 0.0062761, 0.0521794, -0.0036846, 0.0403789, -0.0335401, 0.0054602, -0.0295457, 0.0062646, -0.0318549, -0.0200798, -0.0350457, -0.0211371, -0.0045081, -0.0536032, 0.0180079, 0.0292347, -0.0373302, 0.0102212, -0.0161656, 0.0030769, -0.0042317, 0.0422946, -0.0132999, 0.0639726, 0.0063302, 0.0127261, 0.0608007, 0.0217346, 0.0141945, -0.0055504, 0.0164727, 0.0310062, 0.0074169, 0.0124027, 0.0103245, -0.0233224, -0.0102762, 0.0091786, 0.0063271, -0.0013132, 0.0032481, 0.0331999, 0.0064559, -0.0219145, 0.0096493, 0.0178552, 0.030541, 0.0063936, 0.0139802, -0.0252062, 0.0227818, -0.0051253, -0.0292184, -0.0409617, 0.022986, -0.0172845, 0.0212696, -0.021213, -0.0138542, -0.0126601, -0.0104976, -0.020326, 0.0113664, -0.0096861, 0.0007517, 0.0258834, -0.0228422, -0.0242639, -0.0164893, -0.0155036, -0.0012227, -0.0143082, -0.0010508, 0.0134848, -0.0181285, -0.0109053, -0.0121414, -0.0269855, -0.0168856, 0.0062585, 0.0075284, -0.0223102, 0.0206764, -0.0381524, -0.0254449, 0.0046192, 0.0094333, -0.0023871, 0.1939883, -0.010786, -0.0109085, -0.0034179, -0.0061638, -0.0228264, 0.0092627, 0.0038894, -0.0105737, 0.0254012, -0.0387676, 0.0177953, 0.0114002, -0.0082272, -0.0021445, 0.0136881, -0.0476875, 0.0161518, -0.0005517, 0.0211178, 0.0158259, 0.0371481, 0.0041266, 0.0420225, 0.0057804, 0.0119182, 0.00443, -0.0013008, -0.001863, 0.0110961, 0.0062314, -0.0355586, -0.0639177, 0.0010688, 0.0185759, 0.0168337 ] } ]}.gitattributes+2
CHANGELOG.mdAdded+32
CHECKSUMS.sha256Added+26
LICENSEAdded+202
NOTICEAdded+85
README.mdAdded+346
export/eg2_text.pyAdded+288
export/export.pyAdded+215
model/MODEL-ATTRIBUTION.jsonAdded+36
model/config.jsonAdded+54
model/embeddinggemma-2-text.aimodel/main.hashAdded+1
model/embeddinggemma-2-text.aimodel/main.mlirbAdded+3
model/embeddinggemma-2-text.aimodel/metadata.jsonAdded+8
model/tokenizer/tokenizer.jsonAdded+3
model/tokenizer/tokenizer_config.jsonAdded+57
python/embeddinggemma2_coreai.pyAdded+278
python/requirements.lockAdded+19
python/requirements.txtAdded+7
python/smoke_test.pyAdded+62
swift/Package.swiftAdded+26
swift/Sources/EmbeddingGemma2CoreAI/Embedder.swiftAdded+197
swift/Sources/EmbeddingGemma2CoreAI/ExactJSON.swiftAdded+202
swift/Sources/EmbeddingGemma2CoreAI/GemmaTokenizer.swiftAdded+281
swift/Sources/eg2-cli/main.swiftAdded+187
tests/make_smoke_reference.pyAdded+54
tests/make_tokenizer_cases.pyAdded+298
tests/smoke-reference.jsonAdded+2333
tests/tokenizer-cases.jsonAdded