File size: 6,675 Bytes
2560811 aaf5eb0 96f5a46 aaf5eb0 96f5a46 aaf5eb0 96f5a46 aaf5eb0 96f5a46 aaf5eb0 2560811 aaf5eb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | ---
language: en
license: other
license_name: gpt-2-modified-mit
license_link: LICENSE
library_name: coreai
pipeline_tag: text-generation
base_model:
- openai-community/gpt2
- openai-community/gpt2-medium
- openai-community/gpt2-large
- openai-community/gpt2-xl
tags:
- apple
- coreai
- ane
- apple-neural-engine
- tinkerbox
- gpt2
- fp16
- int8
- lut8
---
# GPT-2 on Apple Neural Engine
All four original GPT-2 sizes ported to Core AI framework. FP16, INT8 and LUT8 weights in one repository.
Requires Tinkerbox 2.3.0 or later, Apple Silicon M1+ and macOS 27 or later.
The transformer runs on ANE. Tokenization, embeddings, FP32 vocabulary projection and sampling run on CPU. Context is 1,024 tokens, with independent batches of 1, 4 or 8 and automatic selection for partial groups. Prefill width is 64. FP16 is the default; INT8 and LUT8 are optional compressed weights. These are base text-completion models with no chat template or instruction tuning.
## Choose a bundle
```text
<model>/<weights>/macos27/<architecture>/
```
Models: `gpt2`, `gpt2-medium`, `gpt2-large`, `gpt2-xl`. Weights: `fp16`, `int8`, `lut8`. Each directory is self-contained: `model.json`, CPU checkpoint/tokenizer files, and its compiled `.aimodelc` stages.
Download sizes below are for the M5 Pro bundle, including its CPU checkpoint:
| Model | Parameters | FP16 MiB | INT8 MiB | LUT8 MiB |
| --- | ---: | ---: | ---: | ---: |
| Small (`gpt2`) | 124,439,808 | 253.0 | 175.6 | 198.9 |
| Medium (`gpt2-medium`) | 354,823,168 | 709.8 | 431.3 | 494.0 |
| Large (`gpt2-large`) | 774,030,080 | 1,530.6 | 873.3 | 992.4 |
| XL (`gpt2-xl`) | 1,557,611,200 | 3,051.5 | 1,674.7 | 1,878.0 |
## Run
Requires `tinkerbox-ane>=2.3.0` and Python 3.12 or 3.13:
```sh
python -m pip install "tinkerbox-ane>=2.3.0"
```
Download only the chosen model, weights and chip target:
```python
from pathlib import Path
from huggingface_hub import HfApi, snapshot_download
import tinkerbox_ane
from tinkerbox_ane.host import device_architecture
repo = "wegotomars/GPT2-CoreAI-ANE"
revision = HfApi().model_info(repo).sha # Record this immutable revision for reuse.
model = "gpt2"
weights = "fp16" # fp16, int8 or lut8
folder = f"{model}/{weights}/macos27/{device_architecture()}"
root = snapshot_download(
repo_id=repo,
revision=revision,
local_dir=Path("models/GPT2-CoreAI-ANE") / revision,
allow_patterns=["config.json", "LICENSE", "NOTICE", f"{folder}/**"],
)
with tinkerbox_ane.load(model, Path(root) / folder) as engine:
reply = engine.generate("Once upon a time,", max_new_tokens=48)
print(reply["text"])
samples = engine.generate_batch(
["The scientist discovered", "The musician played"],
max_new_tokens=32, temperature=1.0, top_k=40, seed=42,
)
```
Temperature zero selects greedy decoding. Seeded sampling preserves standalone replay. Batches accept unrelated prefixes and different lengths, and return input order. Prompt plus requested output tokens must fit the 1,024-token context.
## Performance
Measured on **Apple M5 Pro, 24 GB, macOS 27.0.1 (`h17s`)**. Median of three warm 24-token completions, four CPU threads, with CPU processing and host dispatch included. Batch 8 uses independent requests with the same 12-token prompt. The reference backend is Transformers FP16 on MPS.
| Model | Weights | Single tokens/s | Batch 8 tokens/s | MPS FP16 batch 8 tokens/s |
| --- | --- | ---: | ---: | ---: |
| Small | FP16 | 327.6 | 1,309.0 | 1,225.5 |
| Small | INT8 | 396.2 | 1,444.6 | 1,225.5 |
| Small | LUT8 | 394.4 | 1,443.6 | 1,225.5 |
| Medium | FP16 | 135.8 | 667.3 | 781.7 |
| Medium | INT8 | 178.6 | 778.0 | 781.7 |
| Medium | LUT8 | 177.8 | 770.4 | 781.7 |
| Large | FP16 | 69.9 | 395.9 | 575.3 |
| Large | INT8 | 98.0 | 491.4 | 575.3 |
| Large | LUT8 | 97.6 | 491.7 | 575.3 |
| XL | FP16 | 37.8 | 228.7 | 421.2 |
| XL | INT8 | 55.1 | 305.3 | 421.2 |
| XL | LUT8 | 52.0 | 299.6 | 421.2 |
## Conversion results
All 252 bundles passed full ANE placement verification. The 12 M5 Pro bundles passed **36 public API tests and 60 numerical checks** through the full context. FP32 MPS supplied the reference scores and continuations.
[WikiText-2 test](https://huggingface.co/datasets/Salesforce/wikitext): **287,363 predictions per variant**, with 1,024-token windows. Reference: FP32 MPS transformer with CPU readout. Lower perplexity/KL and higher agreement are better. **Bold** compares INT8 with LUT8.
### Small
FP32 reference perplexity: **29.941**
| Weights | Perplexity | ΔPPL | Mean KL (10⁻³ nats) | P95 KL (10⁻³ nats) | Top-1 agreement |
| --- | ---: | ---: | ---: | ---: | ---: |
| FP16 | 29.950 | +0.009 | 0.092 | 0.239 | 99.35% |
| INT8 | 29.977 | +0.037 | 1.287 | 3.482 | 97.62% |
| LUT8 | 29.968 | +0.027 | **0.469** | **1.267** | **98.58%** |
### Medium
FP32 reference perplexity: **21.706**
| Weights | Perplexity | ΔPPL | Mean KL (10⁻³ nats) | P95 KL (10⁻³ nats) | Top-1 agreement |
| --- | ---: | ---: | ---: | ---: | ---: |
| FP16 | 21.709 | +0.003 | 0.065 | 0.166 | 99.50% |
| INT8 | 21.705 | -0.001 | 0.740 | 2.042 | 98.28% |
| LUT8 | 21.698 | -0.008 | **0.294** | **0.784** | **98.91%** |
### Large
FP32 reference perplexity: **19.436**
| Weights | Perplexity | ΔPPL | Mean KL (10⁻³ nats) | P95 KL (10⁻³ nats) | Top-1 agreement |
| --- | ---: | ---: | ---: | ---: | ---: |
| FP16 | 19.424 | -0.012 | 0.051 | 0.109 | 99.62% |
| INT8 | 19.437 | +0.001 | 0.309 | 0.880 | 98.96% |
| LUT8 | 19.426 | -0.010 | **0.158** | **0.453** | **99.26%** |
### XL
FP32 reference perplexity: **17.400**
| Weights | Perplexity | ΔPPL | Mean KL (10⁻³ nats) | P95 KL (10⁻³ nats) | Top-1 agreement |
| --- | ---: | ---: | ---: | ---: | ---: |
| FP16 | 17.394 | -0.006 | 0.028 | 0.084 | 99.68% |
| INT8 | 17.397 | -0.003 | 0.235 | 0.719 | 99.13% |
| LUT8 | 17.392 | -0.008 | **0.124** | **0.374** | **99.33%** |
## Supported architectures
| Architecture | SoC name |
| --- | --- |
| `h13g` | Apple M1 |
| `h13s` | Apple M1 Pro |
| `h13c` | Apple M1 Max |
| `h13d` | Apple M1 Ultra |
| `h14g` | Apple M2 |
| `h14s` | Apple M2 Pro |
| `h14c` | Apple M2 Max |
| `h14d` | Apple M2 Ultra |
| `h15g` | Apple M3 |
| `h15s` | Apple M3 Pro |
| `h15m` | Apple M3 Max (14-core CPU) |
| `h15c` | Apple M3 Max (16-core CPU) |
| `h15d` | Apple M3 Ultra |
| `h16g` | Apple M4 |
| `h16s` | Apple M4 Pro |
| `h16c` | Apple M4 Max |
| `h17g` | Apple M5 |
| `h17s` | Apple M5 Pro |
| `h17c` | Apple M5 Max |
| `h17d` | Apple M5 Ultra |
| `h18g` | Apple M6 |
## License
Original GPT-2 Software Copyright © 2019 OpenAI, under the original [Modified MIT License](LICENSE). Conversion details are in [NOTICE](NOTICE).
|