length 64, verified at 0.9964-1.0000; the 128 build was not faithful and is documented
Browse filesFiles changed (4)
123version https://git-lfs.github.com/spec/v1oid sha256:e1679a30c3db14fad41dcb49883c5517bb0e64cbb09c304f0c0ca1aa03901c4dsize 256356123version https://git-lfs.github.com/spec/v1oid sha256:88b4bb9fef8e786c3ea37969a07bf13d302570f702cf4b96aafb889c9b826a2csize 299358592123456789101112131415161718{ "fileFormatVersion": "1.0.0", "itemInfoEntries": { "46F15BA9-E755-4DE3-B0A6-4C3D9F77A9A8": { "author": "com.apple.CoreML", "description": "CoreML Model Specification", "name": "model.mlmodel", "path": "com.apple.CoreML/model.mlmodel" }, "5933553D-41D2-45B5-8C2B-00A396A141BB": { "author": "com.apple.CoreML", "description": "CoreML Model Weights", "name": "weights", "path": "com.apple.CoreML/weights" } }, "rootModelIdentifier": "46F15BA9-E755-4DE3-B0A6-4C3D9F77A9A8"}14 unmodified lines1516171819202122232433 unmodified lines58596061626364656667686 unmodified lines75767778798013 unmodified lines9495969798995 unmodified lines10510610710810911011111214 unmodified lines# ModernBERT-base in Core ML[ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base) convertedto Core ML with its masked-LM head, at a **fixed sequence length of 128**.Verified against the PyTorch model rather than assumed: correlation of thelogits at a masked position is **0.997 to 0.99999**. `verify.py` reproduces it.```The capital of Ireland is [MASK].33 unmodified linesfrom transformers import AutoTokenizertok = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base")model = ct.models.MLModel("ModernBERT-base-128.mlpackage")text = "The capital of Ireland is " + tok.mask_token + "."encoded = tok(text, return_tensors="np")ids = np.full((1, 128), tok.pad_token_id, np.int32)ids[0, : encoded.input_ids.shape[1]] = encoded.input_idslogits = model.predict({"input_ids": ids})["logits"][0]6 unmodified lines## Two things to know before you rely on it**The length is fixed, and padding is attended.** The attention mask is`ones_like(input_ids)` inside the graph, so padding tokens are read as text. Onone sentence-boundary task this cost nothing measurable, but that is a property13 unmodified lines| int8 | 152 MB | 23 ms | indistinguishable || int4 | 81 MB | 21 ms | shifted, and worse where it matters |int8 halves the size and doubles the latency, because the weights aredequantised on every pass. int4 changes the distribution enough to matter.5 unmodified lines```shpip install torch==2.7.0 transformers==4.48.3 coremltools==9.0python convert.py --length 128python verify.py ModernBERT-base-128.mlpackage --length 128```## Licence14 unmodified lines151617181920212223242533 unmodified lines59606162636465666768696 unmodified lines767778798081828384858687888990919213 unmodified lines1061071081091101111121131141151165 unmodified lines12212312412512612712812914 unmodified lines# ModernBERT-base in Core ML[ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base) convertedto Core ML with its masked-LM head, at a **fixed sequence length of 64**.Verified against the PyTorch model rather than assumed: correlation of thelogits at a masked position is **0.9964 to 1.0000** over four sentences.`verify.py` reproduces it.```The capital of Ireland is [MASK].33 unmodified linesfrom transformers import AutoTokenizertok = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base")model = ct.models.MLModel("ModernBERT-base-64.mlpackage")text = "The capital of Ireland is " + tok.mask_token + "."encoded = tok(text, return_tensors="np")ids = np.full((1, 64), tok.pad_token_id, np.int32)ids[0, : encoded.input_ids.shape[1]] = encoded.input_idslogits = model.predict({"input_ids": ids})["logits"][0]6 unmodified lines## Two things to know before you rely on it**Fidelity falls off with length, and 64 is what is verified here.** The samerecipe converted at 128 correlates at only **0.90 to 0.99** across the same foursentences, against 0.9964 to 1.0000 at 64. The conversion emits an `overflowencountered in cast` warning while building the attention mask, and the longerthe window the more it seems to cost. I published the 128 build before checkingit and had to take it down — which is the same mistake this repository exists towarn about, so it is recorded here rather than quietly fixed.**Verify at the length you convert.** Do not assume a recipe that is faithful atone length is faithful at another. `verify.py` takes `--length`.**The length is fixed, and padding is attended.** The attention mask is`ones_like(input_ids)` inside the graph, so padding tokens are read as text. Onone sentence-boundary task this cost nothing measurable, but that is a property13 unmodified lines| int8 | 152 MB | 23 ms | indistinguishable || int4 | 81 MB | 21 ms | shifted, and worse where it matters |Those three were measured on the 128 build, before its fidelity problem wasfound. The ranking is about quantisation and should carry over, but the numberswere taken on a build that is not faithful — treat them as a direction, not ameasurement.int8 halves the size and doubles the latency, because the weights aredequantised on every pass. int4 changes the distribution enough to matter.5 unmodified lines```shpip install torch==2.7.0 transformers==4.48.3 coremltools==9.0python convert.py --length 64python verify.py ModernBERT-base-64.mlpackage --length 64```## Licence
ModernBERT-base-64.mlpackage/Data/com.apple.CoreML/model.mlmodelAdded+3
ModernBERT-base-64.mlpackage/Data/com.apple.CoreML/weights/weight.binAdded+3
ModernBERT-base-64.mlpackage/Manifest.jsonAdded+18
README.md+23-6