--- license: apache-2.0 library_name: coreml pipeline_tag: text-classification base_model: helmo/DecidaBERT-large-v2 tags: [coreml, on-device, decision-model, system-one, typed-decisions, modernbert, calibration] datasets: [LocalLLaMA/typed-decisions] --- # DecidaBERT-large-v2 for Core ML [helmo/DecidaBERT-large-v2](https://huggingface.co/helmo/DecidaBERT-large-v2) (revision `c6879f2`) converted to Core ML, to run on iPhone, iPad and Mac. Send a state and typed questions (`noul` yes/no, `choice`, `score`); every question gets a probability distribution and a calibrated confidence from **one forward pass**, with no generated text and nothing sent off the device. ## What is in the package - `DecidaBERT-large-v2.mlpackage`: one **multifunction** ML Program, float16, iOS 18 / macOS 15 or later. One function per fixed length: `len_512`, `len_1024`, `len_2048`, `len_4096`, `len_8192`. The weights are stored once: 810 MB for all five lengths. - Inputs: `input_ids` and `attention_mask`, both `[1, L]` int32. Pad with `[PAD]` (50283) and mask 0. - Output: `logits` `[1, 3, L]` float32: the yes/no, choice and score head at **every** position. Read them at the `[MASK]` markers of the input layout (see the source model card and `decidabert_v2/model.py` there). - `decida.json`: lengths, token budgets (`max_len` 8192, `head_max_tokens` 256, `option_max_tokens` 96), confidence temperatures, special token ids, source revision. - `tokenizer.json`: the source model's tokenizer, unchanged. - `manifest.json`: every file with its size and SHA-256. - `conversion/`: the scripts that made the package (PyTorch 2.7.0, transformers 5.24, coremltools 9.0). ## How it was converted - The trained encoder and heads are reused unchanged; the three heads are folded into the model. - Full-attention layers use Core ML's native `scaled_dot_product_attention` with a key-padding mask. - The sliding-window layers (window ±64) attend block-wise: each 128-token block sees its previous, own and next block. That is exact, and it avoids an L × L band mask. - In PyTorch, padding to 512, 1024 or 4096 tokens leaves the answers unchanged (largest probability difference 2.4e-6). ## Checked against the PyTorch model Typed Decisions `test` split (400 cases, 2,000 questions), each case at the smallest length that fits it, compute units CPU + GPU, on an Apple M3 Ultra: | | PyTorch (float32) | Core ML (float16) | |---|---|---| | accuracy vs gold label | 0.7880 | 0.7885 | | top answer same as PyTorch | | 1,997 / 2,000 (99.85%) | | largest probability difference | | 0.0025 | On an **iPhone 17 Pro Max** (iOS 27.2, CPU + GPU, in the Decida app): accuracy 0.788 (choice 0.748, score 0.759, yes/no 0.867), top answer the same as PyTorch for 1,998 / 2,000 (99.90%), largest probability difference 0.0027. Long inputs (the source Space's demos, and its incident log stretched up to 7,684 tokens): 52 of 52 answers the same as PyTorch, largest probability difference 0.0011. 8-bit weights were tried (405 MB). They give 99.40% the same top answer and a largest difference of 0.023, which fails these bars, so they are not published. ## Speed and memory (M3 Ultra, CPU + GPU) | Length | Time per request | Peak memory | |---|---|---| | 512 | 25 ms | 0.69 GB | | 1024 | 43 ms | 0.81 GB | | 2048 | 83 ms | 1.17 GB | | 4096 | 169 ms | 1.73 GB | | 8192 | 392 ms | 2.84 GB | - **Load it with `.cpuAndGPU`.** Core ML refuses this multifunction package on the Neural Engine: `.cpuAndNeuralEngine` fails to load every function, and `.all` fails to load `len_4096` and `len_8192`. The error it gives is misleading ("`.functionName` property must be `nil` unless the model type is ML Program"). - On this Mac the Neural Engine is slower anyway: a single-function test took 69 ms at 512 tokens, against 25 ms on the GPU. It has not been tried on an iPhone yet. ## Speed and memory (iPhone 17 Pro Max, CPU + GPU) Measured in the Decida app: median of 5 forward passes per length; "first use" includes loading that length; memory is the app's footprint after that length (not the peak). The app ran every length up to 8,192 without being closed for memory. | Length | Time per request | First use | Memory after | |---|---|---|---| | 512 | 62 ms | 1.9 s | 0.14 GB | | 1024 | 133 ms | 1.9 s | 0.26 GB | | 2048 | 282 ms | 2.2 s | 0.26 GB | | 4096 | 629 ms | 2.2 s | 0.39 GB | | 8192 | 1,532 ms | 3.2 s | 0.66 GB | ## Licence Apache-2.0, as the source model.