pythia-160m-stateful-coreml
ζ₯ζ¬θͺηγ―γγ‘γ (README.ja.md)
Model Summary
This is an unofficial Core ML conversion of EleutherAI/pythia-160m
(GPT-NeoX architecture, one of EleutherAI's Pythia research model suite),
using Core ML's stateful-model support (coremltools.StateType) for a real
KV-cache implementation, rather than a KV-cache-free full-recompute design.
Different from earlier conversions
Earlier ASR/OCR conversions had shallow decoders, so a KV-cache-free design
that recomputes the whole sequence every step was practical enough. But
pythia-160m has a standard 12-layer autoregressive decoder, where the
same design would be inefficient. This model instead uses coremltools'
StateType (ct.StateType) to keep each layer's Key/Value cache as Core
ML "state", decoding one token per step and updating that state in place.
GPT-NeoX-specific details (the parallel residual structure, partial
rotary position embeddings with rotary_pct=0.25) are hand-reimplemented
directly against the original weights, without going through
transformers' Cache classes.
Usage (Python / coremltools)
import numpy as np
import coremltools as ct
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pythia-160m")
mlmodel = ct.models.MLModel("pythia-160m-stateful_fp16.mlpackage")
prompt = "The history of artificial intelligence began"
input_ids = tokenizer(prompt, return_tensors="pt").input_ids[0].tolist()
state = mlmodel.make_state() # the KV-cache state object
pos = 0
logits = None
for tok in input_ids: # prefill: feed the prompt one token at a time
out = mlmodel.predict({
"input_ids": np.array([[tok]], dtype=np.int32),
"position": np.array([pos], dtype=np.int32),
}, state=state)
logits = out["logits"]
pos += 1
generated = list(input_ids)
for _ in range(50): # greedy decoding
next_id = int(np.argmax(logits[0]))
generated.append(next_id)
out = mlmodel.predict({
"input_ids": np.array([[next_id]], dtype=np.int32),
"position": np.array([pos], dtype=np.int32),
}, state=state)
logits = out["logits"]
pos += 1
print(tokenizer.decode(generated))
Traced with max_seq_len=256, so before position reaches 256 you need
to create a fresh state (or provide a separate bucket for longer
sequences).
Accuracy (an honest report)
The output token sequence does not exactly match the PyTorch fp32
reference (model.generate()). Isolating this to the first-step logits
comparison: the cosine similarity between PyTorch fp32 and Core ML fp16
logits was 0.999999 (effectively identical). The divergence comes
from the top-1 and top-2 candidates being extremely close (841.006 vs
840.990), so rounding to fp16 precision nearly ties them, and the
argmax selection flips. Running the PyTorch model itself in fp16 makes
the same choice, so this is not a Core ML conversion bug -- it's a
known consequence of fp16 precision.
| Item | Result |
|---|---|
| Logits cosine similarity (PyTorch fp32 vs Core ML fp16) | 0.999999 |
| Logits cosine similarity (PyTorch fp16 vs Core ML fp16) | 0.9999998 |
| Exact token-sequence match | β (path diverges at a near-tie) |
For use cases that need strict reproducibility, consider re-converting
with compute_precision=ct.precision.FLOAT32 (this release is fp16 only).
Specs
| Item | Value |
|---|---|
| Base model | EleutherAI/pythia-160m (12 layers, 160M params) |
| Precision | float16 |
| Max sequence length | 256 |
| Framework | Core ML (stateful KV-cache via coremltools.StateType) |
Notes
- This is a community conversion, not an official release from EleutherAI.
Security
Audited against its upstream with model-audit-lite: weight format, bundled code, and a machine-readable lineage (ML-BOM). Details, checksums and how to reproduce: SECURITY.md.
- Downloads last month
- 8
Model tree for masahiroid/pythia-160m-stateful-coreml
Base model
EleutherAI/pythia-160m