McBopomofoLM v3.1.2 models: decoder with next-token functions (n16-n96), dec_next_first.tsv
Browse files- README.md +12 -5
- mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Data/com.apple.CoreML/model.mlmodel +2 -2
- mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Data/com.apple.CoreML/weights/weight.bin +2 -2
- mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Manifest.json +8 -8
- runtime/MANIFEST.txt +2 -1
- runtime/dec60m.mlmodelc/analytics/coremldata.bin +1 -1
- runtime/dec60m.mlmodelc/coremldata.bin +2 -2
- runtime/dec60m.mlmodelc/metadata.json +288 -0
- runtime/dec60m.mlmodelc/model.mil +0 -0
- runtime/dec60m.mlmodelc/weights/weight.bin +2 -2
- runtime/dec_next_first.tsv +0 -0
- runtime/runtime-manifest.txt +6 -5
- scripts/dec/build_dec_tn.py +83 -0
- scripts/dec/build_next.py +73 -0
README.md
CHANGED
|
@@ -15,19 +15,26 @@ base_model: Luigi/sloth-ime-models
|
|
| 15 |
|
| 16 |
Core ML conversions of the SlothE models used by
|
| 17 |
[McBopomofoLM](https://github.com/workfunction/McBopomofoLM) (branch `lm`), a fork of
|
| 18 |
-
McBopomofo that uses the models to improve candidate selection.
|
| 19 |
Both models run on the Apple Neural Engine only (`computeUnits = CPU_AND_NE`).
|
| 20 |
|
|
|
|
|
|
|
| 21 |
## Contents
|
| 22 |
|
| 23 |
| Path | What |
|
| 24 |
|---|---|
|
| 25 |
-
| `runtime/` | The exact runtime bundle shipped in McBopomofoLM
|
| 26 |
| `mlpackage/enc25m_multi_pal2_emb.mlpackage` | SlothE-T 25M encoder, 2-bit palettized. The ternary weights make this lossless. Multifunction package with functions L8/L16/L32/L64/L256. The embedding lookup runs in the caller, from `runtime/enc25m_embed_f16.bin`. |
|
| 27 |
-
| `mlpackage/
|
| 28 |
-
| `scripts/` | Conversion and bundling scripts, research-grade. They import helpers from the author's research workspace, which is not included, so they document the conversion and do not run on their own. `MCBPMF_LM_WORK` sets the workspace root. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
## Sources and licenses
|
| 33 |
|
|
|
|
| 15 |
|
| 16 |
Core ML conversions of the SlothE models used by
|
| 17 |
[McBopomofoLM](https://github.com/workfunction/McBopomofoLM) (branch `lm`), a fork of
|
| 18 |
+
McBopomofo that uses the models to improve candidate selection and to suggest corrections for mistyped syllables.
|
| 19 |
Both models run on the Apple Neural Engine only (`computeUnits = CPU_AND_NE`).
|
| 20 |
|
| 21 |
+
**Revisions:** `main` and tag `v3.1.2` match McBopomofoLM v3.1.x. Tag `v2.1.1` is the runtime for McBopomofoLM v2.1.1, which has no next-token functions.
|
| 22 |
+
|
| 23 |
## Contents
|
| 24 |
|
| 25 |
| Path | What |
|
| 26 |
|---|---|
|
| 27 |
+
| `runtime/` | The exact runtime bundle shipped in McBopomofoLM v3.1.2 (`Contents/Resources/SlothE`): compiled `.mlmodelc`, embedding table, vocabularies, the next-token first-char table `dec_next_first.tsv`, and `runtime-manifest.txt`. The app checks the size and SHA-256 of each file against the manifest before it loads that file. |
|
| 28 |
| `mlpackage/enc25m_multi_pal2_emb.mlpackage` | SlothE-T 25M encoder, 2-bit palettized. The ternary weights make this lossless. Multifunction package with functions L8/L16/L32/L64/L256. The embedding lookup runs in the caller, from `runtime/enc25m_embed_f16.bin`. |
|
| 29 |
+
| `mlpackage/dec_mf_tn_fp16.mlpackage` | SlothE decoder `pred_q35_60m` (Qwen3.5 architecture), fp16, one weight file shared by two function families. **t16/t32/t64/t96** (batch 3) return per-token log-probabilities of given texts; they are bit-identical to v2.1.1's decoder. **n16/n32/n64/n96** (batch 1; input `ids` right-padded and `last` = index of the last real token) return the full next-token log-softmax over the 16k vocabulary. Gated DeltaNet runs in fixed 16-token chunks. |
|
| 30 |
+
| `scripts/` | Conversion and bundling scripts, research-grade. They import helpers from the author's research workspace, which is not included, so they document the conversion and do not run on their own. `MCBPMF_LM_WORK` sets the workspace root. `build_next.py` builds the n* functions; `build_dec_tn.py` merges them with the t* functions. |
|
| 31 |
+
|
| 32 |
+
Measured on an M2 (ANE):
|
| 33 |
+
- encoder: 100% of ops on the ANE, about 0.8 ms per call;
|
| 34 |
+
- decoder t*: 86–95% on the ANE, 2.6–7.9 ms per call depending on length;
|
| 35 |
+
- decoder n*: 83–94% on the ANE, 2.3–3.1 ms per call.
|
| 36 |
|
| 37 |
+
Against HF fp32 on CPU, the n* functions agree on the top-1 token for 95.5% of 288 prefixes; every mismatch is a near-tie. The largest log-prob difference over the top-50 tokens is 0.31 nats at the median and 0.73 at P95, from fp16 on the ANE. The first compile of all functions takes about 3 minutes.
|
| 38 |
|
| 39 |
## Sources and licenses
|
| 40 |
|
mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Data/com.apple.CoreML/model.mlmodel
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3e8c0989eaa1433d2c66d2d55cbdc5f333fb6b05d74a1c6cc0860931bdb89249
|
| 3 |
+
size 4082104
|
mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Data/com.apple.CoreML/weights/weight.bin
RENAMED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:862afa757bdc1957b8087b190f6f1136737b5967430c77bb1dc8edaf36b13fb1
|
| 3 |
+
size 119707840
|
mlpackage/{dec_mf_fp16.mlpackage → dec_mf_tn_fp16.mlpackage}/Manifest.json
RENAMED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
{
|
| 2 |
"fileFormatVersion": "1.0.0",
|
| 3 |
"itemInfoEntries": {
|
| 4 |
-
"
|
| 5 |
-
"author": "com.apple.CoreML",
|
| 6 |
-
"description": "CoreML Model Weights",
|
| 7 |
-
"name": "weights",
|
| 8 |
-
"path": "com.apple.CoreML/weights"
|
| 9 |
-
},
|
| 10 |
-
"8B383AB4-0DCB-4C46-AEB1-F0C29DADE0A3": {
|
| 11 |
"author": "com.apple.CoreML",
|
| 12 |
"description": "CoreML Model Specification",
|
| 13 |
"name": "model.mlmodel",
|
| 14 |
"path": "com.apple.CoreML/model.mlmodel"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
},
|
| 17 |
-
"rootModelIdentifier": "
|
| 18 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"fileFormatVersion": "1.0.0",
|
| 3 |
"itemInfoEntries": {
|
| 4 |
+
"6DB0F534-ADD1-4D4C-87C9-F5034442482B": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"author": "com.apple.CoreML",
|
| 6 |
"description": "CoreML Model Specification",
|
| 7 |
"name": "model.mlmodel",
|
| 8 |
"path": "com.apple.CoreML/model.mlmodel"
|
| 9 |
+
},
|
| 10 |
+
"6DDF96CB-AB76-4677-8191-CA14A9E2AE30": {
|
| 11 |
+
"author": "com.apple.CoreML",
|
| 12 |
+
"description": "CoreML Model Weights",
|
| 13 |
+
"name": "weights",
|
| 14 |
+
"path": "com.apple.CoreML/weights"
|
| 15 |
}
|
| 16 |
},
|
| 17 |
+
"rootModelIdentifier": "6DB0F534-ADD1-4D4C-87C9-F5034442482B"
|
| 18 |
}
|
runtime/MANIFEST.txt
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
SlothE runtime files for McBopomofoLM v2 (Core ML, ANE only)
|
| 2 |
encoder source: ane/enc/models/enc25m_multi_pal2_emb.mlpackage tree sha256 1ebd9c64baf81bd94456ce37e70f27063ad027826d7232239d5f7dd0970046d5
|
| 3 |
-
decoder source:
|
| 4 |
embedding: ane/enc/work/embed_f16_25m.bin sha256 b23e59ecd93a0b6c95ac3e93d83e7cb77657adaa365a0d8f7dab31ccafc89583
|
| 5 |
tokenizer: HF pred_q35_60m/tokenizer.json sha256 3f81807dc89667acebf50f878df8c64bed8254c926a25a182a3e1e80ec8b4bfc
|
|
|
|
| 6 |
compiled with: xcrun coremlcompiler compile
|
| 7 |
config A' (walk2/frozen_final2.json): 25M, beta 0.3, gamma 0, pen -15, variant guard; decoder lambda 2, tau 0.5, top-3
|
| 8 |
|
|
|
|
| 1 |
SlothE runtime files for McBopomofoLM v2 (Core ML, ANE only)
|
| 2 |
encoder source: ane/enc/models/enc25m_multi_pal2_emb.mlpackage tree sha256 1ebd9c64baf81bd94456ce37e70f27063ad027826d7232239d5f7dd0970046d5
|
| 3 |
+
decoder source: McBopomofoLM/build/v31b/dec_mf_tn_fp16.mlpackage tree sha256 e9e7516339fe081a577ef6afde09a0c6384317c21efd8d55b3a21ed208a74f3c
|
| 4 |
embedding: ane/enc/work/embed_f16_25m.bin sha256 b23e59ecd93a0b6c95ac3e93d83e7cb77657adaa365a0d8f7dab31ccafc89583
|
| 5 |
tokenizer: HF pred_q35_60m/tokenizer.json sha256 3f81807dc89667acebf50f878df8c64bed8254c926a25a182a3e1e80ec8b4bfc
|
| 6 |
+
next-token first-char table: SlothE/v31b/dec_next_first.tsv sha256 60da0fdd6c4572cd300ffd915d897bfa3271f7e8544e469b9a1b7517ac710b52
|
| 7 |
compiled with: xcrun coremlcompiler compile
|
| 8 |
config A' (walk2/frozen_final2.json): 25M, beta 0.3, gamma 0, pen -15, variant guard; decoder lambda 2, tau 0.5, top-3
|
| 9 |
|
runtime/dec60m.mlmodelc/analytics/coremldata.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 243
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a9b3f77b7a9011b243b5053019ed9d8a5720b124ac4ecc8d60995a6aa877da0e
|
| 3 |
size 243
|
runtime/dec60m.mlmodelc/coremldata.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:987064983d22ae039f5bf23f4e1a8c30fba6eaee5a0c9b00114312e451eccda0
|
| 3 |
+
size 660
|
runtime/dec60m.mlmodelc/metadata.json
CHANGED
|
@@ -262,6 +262,294 @@
|
|
| 262 |
"Ios18.sub" : 32,
|
| 263 |
"Ios18.sliceByIndex" : 291
|
| 264 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
}
|
| 266 |
],
|
| 267 |
"mlProgramOperationTypeHistogram" : {
|
|
|
|
| 262 |
"Ios18.sub" : 32,
|
| 263 |
"Ios18.sliceByIndex" : 291
|
| 264 |
}
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
"inputSchema" : [
|
| 268 |
+
{
|
| 269 |
+
"hasShapeFlexibility" : "0",
|
| 270 |
+
"isOptional" : "0",
|
| 271 |
+
"dataType" : "Int32",
|
| 272 |
+
"formattedType" : "MultiArray (Int32 1 × 16)",
|
| 273 |
+
"shortDescription" : "",
|
| 274 |
+
"shape" : "[1, 16]",
|
| 275 |
+
"name" : "ids",
|
| 276 |
+
"type" : "MultiArray"
|
| 277 |
+
},
|
| 278 |
+
{
|
| 279 |
+
"hasShapeFlexibility" : "0",
|
| 280 |
+
"isOptional" : "0",
|
| 281 |
+
"dataType" : "Int32",
|
| 282 |
+
"formattedType" : "MultiArray (Int32 1)",
|
| 283 |
+
"shortDescription" : "",
|
| 284 |
+
"shape" : "[1]",
|
| 285 |
+
"name" : "last",
|
| 286 |
+
"type" : "MultiArray"
|
| 287 |
+
}
|
| 288 |
+
],
|
| 289 |
+
"computePrecision" : "Mixed (Float16, Float32, Int16, Int32)",
|
| 290 |
+
"storagePrecision" : "Mixed (Float16, Int32)",
|
| 291 |
+
"stateSchema" : [
|
| 292 |
+
|
| 293 |
+
],
|
| 294 |
+
"outputSchema" : [
|
| 295 |
+
{
|
| 296 |
+
"hasShapeFlexibility" : "0",
|
| 297 |
+
"isOptional" : "0",
|
| 298 |
+
"dataType" : "Float32",
|
| 299 |
+
"formattedType" : "MultiArray (Float32 1 × 16000)",
|
| 300 |
+
"shortDescription" : "",
|
| 301 |
+
"shape" : "[1, 16000]",
|
| 302 |
+
"name" : "lp",
|
| 303 |
+
"type" : "MultiArray"
|
| 304 |
+
}
|
| 305 |
+
],
|
| 306 |
+
"name" : "n16",
|
| 307 |
+
"mlProgramOperationTypeHistogram" : {
|
| 308 |
+
"Ios18.expandDims" : 24,
|
| 309 |
+
"Ios18.mul" : 250,
|
| 310 |
+
"Ios18.softmax" : 2,
|
| 311 |
+
"Ios18.rsqrt" : 39,
|
| 312 |
+
"Ios18.matmul" : 76,
|
| 313 |
+
"Ios16.reduceMean" : 39,
|
| 314 |
+
"Ios18.exp" : 13,
|
| 315 |
+
"Ios18.sigmoid" : 8,
|
| 316 |
+
"Ios18.equal" : 1,
|
| 317 |
+
"Ios18.greaterEqual" : 1,
|
| 318 |
+
"Select" : 1,
|
| 319 |
+
"Ios16.reduceMax" : 1,
|
| 320 |
+
"Tile" : 16,
|
| 321 |
+
"Ios18.gather" : 1,
|
| 322 |
+
"Ios18.add" : 123,
|
| 323 |
+
"Ios16.reduceSum" : 2,
|
| 324 |
+
"Ios18.reshape" : 68,
|
| 325 |
+
"Ios18.linear" : 39,
|
| 326 |
+
"Ios18.relu" : 6,
|
| 327 |
+
"Ios18.concat" : 14,
|
| 328 |
+
"Ios18.silu" : 20,
|
| 329 |
+
"Ios18.pow" : 21,
|
| 330 |
+
"Ios18.cast" : 5,
|
| 331 |
+
"Ios18.transpose" : 44,
|
| 332 |
+
"Ios18.abs" : 6,
|
| 333 |
+
"Ios18.log" : 7,
|
| 334 |
+
"Ios18.sub" : 2,
|
| 335 |
+
"Ios18.sliceByIndex" : 108
|
| 336 |
+
}
|
| 337 |
+
},
|
| 338 |
+
{
|
| 339 |
+
"inputSchema" : [
|
| 340 |
+
{
|
| 341 |
+
"hasShapeFlexibility" : "0",
|
| 342 |
+
"isOptional" : "0",
|
| 343 |
+
"dataType" : "Int32",
|
| 344 |
+
"formattedType" : "MultiArray (Int32 1 × 32)",
|
| 345 |
+
"shortDescription" : "",
|
| 346 |
+
"shape" : "[1, 32]",
|
| 347 |
+
"name" : "ids",
|
| 348 |
+
"type" : "MultiArray"
|
| 349 |
+
},
|
| 350 |
+
{
|
| 351 |
+
"hasShapeFlexibility" : "0",
|
| 352 |
+
"isOptional" : "0",
|
| 353 |
+
"dataType" : "Int32",
|
| 354 |
+
"formattedType" : "MultiArray (Int32 1)",
|
| 355 |
+
"shortDescription" : "",
|
| 356 |
+
"shape" : "[1]",
|
| 357 |
+
"name" : "last",
|
| 358 |
+
"type" : "MultiArray"
|
| 359 |
+
}
|
| 360 |
+
],
|
| 361 |
+
"computePrecision" : "Mixed (Float16, Float32, Int16, Int32)",
|
| 362 |
+
"storagePrecision" : "Mixed (Float16, Int32)",
|
| 363 |
+
"stateSchema" : [
|
| 364 |
+
|
| 365 |
+
],
|
| 366 |
+
"outputSchema" : [
|
| 367 |
+
{
|
| 368 |
+
"hasShapeFlexibility" : "0",
|
| 369 |
+
"isOptional" : "0",
|
| 370 |
+
"dataType" : "Float32",
|
| 371 |
+
"formattedType" : "MultiArray (Float32 1 × 16000)",
|
| 372 |
+
"shortDescription" : "",
|
| 373 |
+
"shape" : "[1, 16000]",
|
| 374 |
+
"name" : "lp",
|
| 375 |
+
"type" : "MultiArray"
|
| 376 |
+
}
|
| 377 |
+
],
|
| 378 |
+
"name" : "n32",
|
| 379 |
+
"mlProgramOperationTypeHistogram" : {
|
| 380 |
+
"Ios18.expandDims" : 36,
|
| 381 |
+
"Ios18.mul" : 310,
|
| 382 |
+
"Ios18.softmax" : 2,
|
| 383 |
+
"Ios18.rsqrt" : 39,
|
| 384 |
+
"Ios18.matmul" : 172,
|
| 385 |
+
"Ios16.reduceMean" : 39,
|
| 386 |
+
"Ios18.exp" : 31,
|
| 387 |
+
"Ios18.sigmoid" : 8,
|
| 388 |
+
"Ios18.equal" : 1,
|
| 389 |
+
"Ios18.greaterEqual" : 1,
|
| 390 |
+
"Select" : 1,
|
| 391 |
+
"Ios16.reduceMax" : 1,
|
| 392 |
+
"Tile" : 16,
|
| 393 |
+
"Ios18.gather" : 1,
|
| 394 |
+
"Ios18.add" : 153,
|
| 395 |
+
"Ios16.reduceSum" : 2,
|
| 396 |
+
"Ios18.reshape" : 80,
|
| 397 |
+
"Ios18.linear" : 57,
|
| 398 |
+
"Ios18.relu" : 6,
|
| 399 |
+
"Ios18.concat" : 20,
|
| 400 |
+
"Ios18.silu" : 20,
|
| 401 |
+
"Ios18.pow" : 21,
|
| 402 |
+
"Ios18.cast" : 5,
|
| 403 |
+
"Ios18.transpose" : 44,
|
| 404 |
+
"Ios18.abs" : 6,
|
| 405 |
+
"Ios18.log" : 7,
|
| 406 |
+
"Ios18.sub" : 8,
|
| 407 |
+
"Ios18.sliceByIndex" : 168
|
| 408 |
+
}
|
| 409 |
+
},
|
| 410 |
+
{
|
| 411 |
+
"inputSchema" : [
|
| 412 |
+
{
|
| 413 |
+
"hasShapeFlexibility" : "0",
|
| 414 |
+
"isOptional" : "0",
|
| 415 |
+
"dataType" : "Int32",
|
| 416 |
+
"formattedType" : "MultiArray (Int32 1 × 64)",
|
| 417 |
+
"shortDescription" : "",
|
| 418 |
+
"shape" : "[1, 64]",
|
| 419 |
+
"name" : "ids",
|
| 420 |
+
"type" : "MultiArray"
|
| 421 |
+
},
|
| 422 |
+
{
|
| 423 |
+
"hasShapeFlexibility" : "0",
|
| 424 |
+
"isOptional" : "0",
|
| 425 |
+
"dataType" : "Int32",
|
| 426 |
+
"formattedType" : "MultiArray (Int32 1)",
|
| 427 |
+
"shortDescription" : "",
|
| 428 |
+
"shape" : "[1]",
|
| 429 |
+
"name" : "last",
|
| 430 |
+
"type" : "MultiArray"
|
| 431 |
+
}
|
| 432 |
+
],
|
| 433 |
+
"computePrecision" : "Mixed (Float16, Float32, Int16, Int32)",
|
| 434 |
+
"storagePrecision" : "Mixed (Float16, Int32)",
|
| 435 |
+
"stateSchema" : [
|
| 436 |
+
|
| 437 |
+
],
|
| 438 |
+
"outputSchema" : [
|
| 439 |
+
{
|
| 440 |
+
"hasShapeFlexibility" : "0",
|
| 441 |
+
"isOptional" : "0",
|
| 442 |
+
"dataType" : "Float32",
|
| 443 |
+
"formattedType" : "MultiArray (Float32 1 × 16000)",
|
| 444 |
+
"shortDescription" : "",
|
| 445 |
+
"shape" : "[1, 16000]",
|
| 446 |
+
"name" : "lp",
|
| 447 |
+
"type" : "MultiArray"
|
| 448 |
+
}
|
| 449 |
+
],
|
| 450 |
+
"name" : "n64",
|
| 451 |
+
"mlProgramOperationTypeHistogram" : {
|
| 452 |
+
"Ios18.expandDims" : 72,
|
| 453 |
+
"Ios18.mul" : 442,
|
| 454 |
+
"Ios18.softmax" : 2,
|
| 455 |
+
"Ios18.rsqrt" : 39,
|
| 456 |
+
"Ios18.matmul" : 364,
|
| 457 |
+
"Ios16.reduceMean" : 39,
|
| 458 |
+
"Ios18.exp" : 79,
|
| 459 |
+
"Ios18.sigmoid" : 8,
|
| 460 |
+
"Ios18.equal" : 1,
|
| 461 |
+
"Ios18.greaterEqual" : 1,
|
| 462 |
+
"Select" : 1,
|
| 463 |
+
"Ios16.reduceMax" : 1,
|
| 464 |
+
"Tile" : 16,
|
| 465 |
+
"Ios18.gather" : 1,
|
| 466 |
+
"Ios18.add" : 225,
|
| 467 |
+
"Ios16.reduceSum" : 2,
|
| 468 |
+
"Ios18.reshape" : 104,
|
| 469 |
+
"Ios18.linear" : 105,
|
| 470 |
+
"Ios18.relu" : 6,
|
| 471 |
+
"Ios18.concat" : 20,
|
| 472 |
+
"Ios18.silu" : 20,
|
| 473 |
+
"Ios18.pow" : 21,
|
| 474 |
+
"Ios18.cast" : 5,
|
| 475 |
+
"Ios18.transpose" : 44,
|
| 476 |
+
"Ios18.abs" : 6,
|
| 477 |
+
"Ios18.log" : 7,
|
| 478 |
+
"Ios18.sub" : 20,
|
| 479 |
+
"Ios18.sliceByIndex" : 228
|
| 480 |
+
}
|
| 481 |
+
},
|
| 482 |
+
{
|
| 483 |
+
"inputSchema" : [
|
| 484 |
+
{
|
| 485 |
+
"hasShapeFlexibility" : "0",
|
| 486 |
+
"isOptional" : "0",
|
| 487 |
+
"dataType" : "Int32",
|
| 488 |
+
"formattedType" : "MultiArray (Int32 1 × 96)",
|
| 489 |
+
"shortDescription" : "",
|
| 490 |
+
"shape" : "[1, 96]",
|
| 491 |
+
"name" : "ids",
|
| 492 |
+
"type" : "MultiArray"
|
| 493 |
+
},
|
| 494 |
+
{
|
| 495 |
+
"hasShapeFlexibility" : "0",
|
| 496 |
+
"isOptional" : "0",
|
| 497 |
+
"dataType" : "Int32",
|
| 498 |
+
"formattedType" : "MultiArray (Int32 1)",
|
| 499 |
+
"shortDescription" : "",
|
| 500 |
+
"shape" : "[1]",
|
| 501 |
+
"name" : "last",
|
| 502 |
+
"type" : "MultiArray"
|
| 503 |
+
}
|
| 504 |
+
],
|
| 505 |
+
"computePrecision" : "Mixed (Float16, Float32, Int16, Int32)",
|
| 506 |
+
"storagePrecision" : "Mixed (Float16, Int32)",
|
| 507 |
+
"stateSchema" : [
|
| 508 |
+
|
| 509 |
+
],
|
| 510 |
+
"outputSchema" : [
|
| 511 |
+
{
|
| 512 |
+
"hasShapeFlexibility" : "0",
|
| 513 |
+
"isOptional" : "0",
|
| 514 |
+
"dataType" : "Float32",
|
| 515 |
+
"formattedType" : "MultiArray (Float32 1 × 16000)",
|
| 516 |
+
"shortDescription" : "",
|
| 517 |
+
"shape" : "[1, 16000]",
|
| 518 |
+
"name" : "lp",
|
| 519 |
+
"type" : "MultiArray"
|
| 520 |
+
}
|
| 521 |
+
],
|
| 522 |
+
"name" : "n96",
|
| 523 |
+
"mlProgramOperationTypeHistogram" : {
|
| 524 |
+
"Ios18.expandDims" : 108,
|
| 525 |
+
"Ios18.mul" : 574,
|
| 526 |
+
"Ios18.softmax" : 2,
|
| 527 |
+
"Ios18.rsqrt" : 39,
|
| 528 |
+
"Ios18.matmul" : 556,
|
| 529 |
+
"Ios16.reduceMean" : 39,
|
| 530 |
+
"Ios18.exp" : 127,
|
| 531 |
+
"Ios18.sigmoid" : 8,
|
| 532 |
+
"Ios18.equal" : 1,
|
| 533 |
+
"Ios18.greaterEqual" : 1,
|
| 534 |
+
"Select" : 1,
|
| 535 |
+
"Ios16.reduceMax" : 1,
|
| 536 |
+
"Tile" : 16,
|
| 537 |
+
"Ios18.gather" : 1,
|
| 538 |
+
"Ios18.add" : 297,
|
| 539 |
+
"Ios16.reduceSum" : 2,
|
| 540 |
+
"Ios18.reshape" : 128,
|
| 541 |
+
"Ios18.linear" : 153,
|
| 542 |
+
"Ios18.relu" : 6,
|
| 543 |
+
"Ios18.concat" : 20,
|
| 544 |
+
"Ios18.silu" : 20,
|
| 545 |
+
"Ios18.pow" : 21,
|
| 546 |
+
"Ios18.cast" : 5,
|
| 547 |
+
"Ios18.transpose" : 44,
|
| 548 |
+
"Ios18.abs" : 6,
|
| 549 |
+
"Ios18.log" : 7,
|
| 550 |
+
"Ios18.sub" : 32,
|
| 551 |
+
"Ios18.sliceByIndex" : 288
|
| 552 |
+
}
|
| 553 |
}
|
| 554 |
],
|
| 555 |
"mlProgramOperationTypeHistogram" : {
|
runtime/dec60m.mlmodelc/model.mil
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
runtime/dec60m.mlmodelc/weights/weight.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:862afa757bdc1957b8087b190f6f1136737b5967430c77bb1dc8edaf36b13fb1
|
| 3 |
+
size 119707840
|
runtime/dec_next_first.tsv
ADDED
|
Binary file (96.4 kB). View file
|
|
|
runtime/runtime-manifest.txt
CHANGED
|
@@ -2,11 +2,12 @@
|
|
| 2 |
# Any mismatch or missing file = that model is not loaded (encoder -> stock McBopomofo,
|
| 3 |
# decoder -> encoder-only in-walk). Format: <sha256> <bytes> <path>
|
| 4 |
58ccc20614291fc39b7fc6cc2ce4b536a5b787cf62f89bd13fba85bd232569d4 132104 char2id.tsv
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
3f81807dc89667acebf50f878df8c64bed8254c926a25a182a3e1e80ec8b4bfc 1260362 dec_tokenizer.json
|
| 11 |
e3a4791a3fda6d7f0f7a4c322691c960cfe928f951d224e14e60748917ef6486 243 enc25m.mlmodelc/analytics/coremldata.bin
|
| 12 |
b5b297a9d490bba32eacdf888e4d5dd45114f07131425f82569508164e3eb3b0 540 enc25m.mlmodelc/coremldata.bin
|
|
|
|
| 2 |
# Any mismatch or missing file = that model is not loaded (encoder -> stock McBopomofo,
|
| 3 |
# decoder -> encoder-only in-walk). Format: <sha256> <bytes> <path>
|
| 4 |
58ccc20614291fc39b7fc6cc2ce4b536a5b787cf62f89bd13fba85bd232569d4 132104 char2id.tsv
|
| 5 |
+
a9b3f77b7a9011b243b5053019ed9d8a5720b124ac4ecc8d60995a6aa877da0e 243 dec60m.mlmodelc/analytics/coremldata.bin
|
| 6 |
+
987064983d22ae039f5bf23f4e1a8c30fba6eaee5a0c9b00114312e451eccda0 660 dec60m.mlmodelc/coremldata.bin
|
| 7 |
+
214b529e733d18d677e9c1c5e3e939a9048292ba2497f2b2329f68f93060e5c9 17883 dec60m.mlmodelc/metadata.json
|
| 8 |
+
61c054450a0cb565dcdeea08d6b18bd7a0c939e166f87aa2581474e4833feefc 4105774 dec60m.mlmodelc/model.mil
|
| 9 |
+
862afa757bdc1957b8087b190f6f1136737b5967430c77bb1dc8edaf36b13fb1 119707840 dec60m.mlmodelc/weights/weight.bin
|
| 10 |
+
60da0fdd6c4572cd300ffd915d897bfa3271f7e8544e469b9a1b7517ac710b52 96396 dec_next_first.tsv
|
| 11 |
3f81807dc89667acebf50f878df8c64bed8254c926a25a182a3e1e80ec8b4bfc 1260362 dec_tokenizer.json
|
| 12 |
e3a4791a3fda6d7f0f7a4c322691c960cfe928f951d224e14e60748917ef6486 243 enc25m.mlmodelc/analytics/coremldata.bin
|
| 13 |
b5b297a9d490bba32eacdf888e4d5dd45114f07131425f82569508164e3eb3b0 540 enc25m.mlmodelc/coremldata.bin
|
scripts/dec/build_dec_tn.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""v3.1b decoder package: the shipped t16/t32/t64/t96 (B=3 scorers) + the next-token n16/n32/n64/n96 (B=1,
|
| 2 |
+
reports/ane-dec.md "next-token function") in ONE multifunction package with shared weights, from the same parts.
|
| 3 |
+
1. the parts are the shipped ones: a t-only merge of ane/dec/mf/parts/*_fp16 has the shipped
|
| 4 |
+
ane/dec/mf/dec_mf_fp16.mlpackage's weight.bin byte for byte and the same t16..t96 program functions;
|
| 5 |
+
2. t + n merge -> McBopomofoLM/build/v31b/dec_mf_tn_fp16.mlpackage (default function t16, as shipped): its t16..t96
|
| 6 |
+
program functions equal the shipped ones and its weight.bin holds the shipped blobs at the same offsets (only
|
| 7 |
+
the header's blob count differs), so every weight a t function reads is the shipped bytes;
|
| 8 |
+
3. SlothE/v31b/dec_next_first.tsv: char \t token ids whose decoded text starts with that char (partial-byte and
|
| 9 |
+
whitespace-initial tokens dropped) = typo/t2.py _dinit / typo/t2c.py, the study's first-char grouping.
|
| 10 |
+
usage: nice -n 15 $W/ane/dec/.venv/bin/python SlothE/v31b/build_dec_tn.py
|
| 11 |
+
"""
|
| 12 |
+
import hashlib
|
| 13 |
+
import shutil
|
| 14 |
+
import sys
|
| 15 |
+
import tempfile
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
HERE = Path(__file__).resolve().parent
|
| 19 |
+
W = HERE.parents[2]
|
| 20 |
+
DEC = W / "ane" / "dec"
|
| 21 |
+
sys.path.insert(0, str(DEC))
|
| 22 |
+
import coremltools as ct # noqa: E402
|
| 23 |
+
from coremltools.models.utils import MultiFunctionDescriptor, save_multifunction # noqa: E402
|
| 24 |
+
from tokenizers import decoders # noqa: E402
|
| 25 |
+
from common import TOK # noqa: E402
|
| 26 |
+
|
| 27 |
+
TS = (16, 32, 64, 96)
|
| 28 |
+
OUT = W / "McBopomofoLM" / "build" / "v31b"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def sha(p):
|
| 32 |
+
return hashlib.sha256(Path(p).read_bytes()).hexdigest()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def merge(fns, dst):
|
| 36 |
+
d = MultiFunctionDescriptor()
|
| 37 |
+
for name, p in fns.items():
|
| 38 |
+
d.add_function(str(p), src_function_name="main", target_function_name=name)
|
| 39 |
+
d.default_function_name = "t16"
|
| 40 |
+
shutil.rmtree(dst, ignore_errors=True)
|
| 41 |
+
save_multifunction(d, str(dst))
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
tparts = {f"t{T}": DEC / "mf" / "parts" / f"dec_b3_t{T}_fp16.mlpackage" for T in TS}
|
| 45 |
+
nparts = {f"n{T}": DEC / "mf_next" / "parts" / f"dec_b1_n{T}_fp16.mlpackage" for T in TS}
|
| 46 |
+
SHIPPED = DEC / "mf" / "dec_mf_fp16.mlpackage"
|
| 47 |
+
shipped_spec = ct.utils.load_spec(str(SHIPPED))
|
| 48 |
+
shipped_w = (SHIPPED / "Data" / "com.apple.CoreML" / "weights" / "weight.bin").read_bytes()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def same_t(pkg):
|
| 52 |
+
spec = ct.utils.load_spec(str(pkg))
|
| 53 |
+
w = (Path(pkg) / "Data" / "com.apple.CoreML" / "weights" / "weight.bin").read_bytes()
|
| 54 |
+
fns = all(spec.mlProgram.functions[f"t{T}"] == shipped_spec.mlProgram.functions[f"t{T}"] for T in TS)
|
| 55 |
+
# the 64-byte storage header starts with the blob count; every blob after it must be the shipped bytes
|
| 56 |
+
return fns, w[64:len(shipped_w)] == shipped_w[64:], len(w)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 60 |
+
t_only = Path(tmp) / "t_only.mlpackage"
|
| 61 |
+
merge(tparts, t_only)
|
| 62 |
+
fns, prefix, n = same_t(t_only)
|
| 63 |
+
print(f"t-only merge vs shipped: t functions equal {fns}, weight blobs equal {prefix and n == len(shipped_w)}")
|
| 64 |
+
assert fns and prefix and n == len(shipped_w), "the t parts are not the shipped ones"
|
| 65 |
+
OUT.mkdir(parents=True, exist_ok=True)
|
| 66 |
+
dst = OUT / "dec_mf_tn_fp16.mlpackage"
|
| 67 |
+
merge(tparts | nparts, dst)
|
| 68 |
+
fns, prefix, n = same_t(dst)
|
| 69 |
+
size = sum(f.stat().st_size for f in dst.rglob("*") if f.is_file()) / 1e6
|
| 70 |
+
print(f"merged t16..t96 + n16..n96 -> {dst} {size:.1f} MB; t functions equal shipped {fns}; "
|
| 71 |
+
f"weight.bin = the shipped blobs at the same offsets {prefix} (+{n - len(shipped_w)} B of n-only blobs appended)")
|
| 72 |
+
assert fns and prefix
|
| 73 |
+
if TOK.decoder is None:
|
| 74 |
+
TOK.decoder = decoders.ByteLevel()
|
| 75 |
+
first = {}
|
| 76 |
+
for t in range(TOK.get_vocab_size()):
|
| 77 |
+
s = TOK.decode([t])
|
| 78 |
+
if s and s[0] != "�" and not s[0].isspace():
|
| 79 |
+
first.setdefault(s[0], []).append(t)
|
| 80 |
+
with open(HERE / "dec_next_first.tsv", "w", encoding="utf-8") as f:
|
| 81 |
+
for c in sorted(first):
|
| 82 |
+
f.write(c + "\t" + " ".join(map(str, first[c])) + "\n")
|
| 83 |
+
print(f"first-char table: {len(first)} chars, {sum(map(len, first.values()))} tokens")
|
scripts/dec/build_next.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""v3.1b: next-token decoder functions n16/n32/n64/n96 -> mf_next/dec_mf_next_fp16.mlpackage (multifunction, shared weights).
|
| 2 |
+
Input ids int32 [1,T] = BOS + left text, RIGHT-padded with anything (causal model: pads never reach position `last`)
|
| 3 |
+
last int32 [1] = index of the last real token (= len(BOS + left ids) - 1)
|
| 4 |
+
Output lp fp32 [1,V] = log-softmax over the 16k vocab of the NEXT token after ids[0, last] (max-shifted logsumexp).
|
| 5 |
+
Same QDec body as the shipped t{T} functions (fp16, ane_kwargs, t16 single-block GDN, t>=32 chunked c16).
|
| 6 |
+
usage: build_next.py [--merge-test] (--merge-test: also merge with the shipped mf/parts/*fp16 into a throwaway package
|
| 7 |
+
to measure whether weights de-duplicate; prints sizes, then deletes it)"""
|
| 8 |
+
import sys, time, shutil, torch, numpy as np
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from qdec import QDec, ane_kwargs, FF
|
| 12 |
+
|
| 13 |
+
TS = (16, 32, 64, 96)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class QNext(QDec):
|
| 17 |
+
def __init__(self, T, **kw):
|
| 18 |
+
super().__init__(T, B=1, **kw)
|
| 19 |
+
self.register_buffer("pos", torch.arange(T, dtype=torch.int32)[None, :]) # [1,T]
|
| 20 |
+
|
| 21 |
+
def forward(self, ids, last):
|
| 22 |
+
x = F.embedding(ids, self.emb)
|
| 23 |
+
for m in self.layers:
|
| 24 |
+
h = m.n1(x)
|
| 25 |
+
x = x + (self.gdn(m, h) if m.kind == "linear_attention" else self.attn(m, h))
|
| 26 |
+
gu = m.gate_up(m.n2(x))
|
| 27 |
+
x = x + m.down(F.silu(gu[..., :FF]) * gu[..., FF:])
|
| 28 |
+
# pick row `last` BEFORE the final RMSNorm (row-wise, so exact): one-hot mask * x, sum over T
|
| 29 |
+
sel = (self.pos == last.to(torch.int32)[:, None]).to(x.dtype) # [1,T]
|
| 30 |
+
h = self.norm((sel[:, :, None] * x).sum(1)) # [1,640]
|
| 31 |
+
logits = h @ self.emb.t() # tied head [1,V]
|
| 32 |
+
mx = logits.max(-1, keepdim=True).values # max-shifted lse (ANE fp16, qdec.py)
|
| 33 |
+
return logits - (mx + torch.log(torch.exp(logits - mx).sum(-1, keepdim=True)))
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
import coremltools as ct
|
| 39 |
+
from coremltools.models.utils import MultiFunctionDescriptor, save_multifunction
|
| 40 |
+
out = Path("mf_next"); (out / "parts").mkdir(parents=True, exist_ok=True)
|
| 41 |
+
size = lambda p: sum(f.stat().st_size for f in Path(p).rglob("*") if f.is_file()) / 1e6
|
| 42 |
+
torch.set_num_threads(1)
|
| 43 |
+
parts = {}
|
| 44 |
+
for T in TS:
|
| 45 |
+
m = QNext(T, chunk=16 if T > 16 else None, **ane_kwargs()).eval()
|
| 46 |
+
ex = (torch.zeros((1, T), dtype=torch.long), torch.tensor([T - 1], dtype=torch.int32))
|
| 47 |
+
with torch.no_grad():
|
| 48 |
+
tr = torch.jit.trace(m, ex)
|
| 49 |
+
t0 = time.time()
|
| 50 |
+
mm = ct.convert(tr, inputs=[ct.TensorType("ids", shape=(1, T), dtype=np.int32), ct.TensorType("last", shape=(1,), dtype=np.int32)],
|
| 51 |
+
outputs=[ct.TensorType("lp", dtype=np.float32)], compute_precision=ct.precision.FLOAT16,
|
| 52 |
+
minimum_deployment_target=ct.target.macOS15, convert_to="mlprogram")
|
| 53 |
+
p = out / "parts" / f"dec_b1_n{T}_fp16.mlpackage"; shutil.rmtree(p, ignore_errors=True); mm.save(str(p)); parts[T] = p
|
| 54 |
+
print(f"OK part n{T} {time.time()-t0:.1f}s {size(p):.1f} MB", flush=True)
|
| 55 |
+
|
| 56 |
+
def merge(fns, dst, default):
|
| 57 |
+
d = MultiFunctionDescriptor()
|
| 58 |
+
for name, p in fns.items():
|
| 59 |
+
d.add_function(str(p), src_function_name="main", target_function_name=name)
|
| 60 |
+
d.default_function_name = default
|
| 61 |
+
shutil.rmtree(dst, ignore_errors=True); save_multifunction(d, str(dst))
|
| 62 |
+
return size(dst)
|
| 63 |
+
|
| 64 |
+
dst = out / "dec_mf_next_fp16.mlpackage"
|
| 65 |
+
print(f"OK multifunction n16..n96: {merge({f'n{T}': p for T, p in parts.items()}, dst, 'n16'):.1f} MB "
|
| 66 |
+
f"(parts total {sum(size(p) for p in parts.values()):.1f} MB) -> {dst}")
|
| 67 |
+
if "--merge-test" in sys.argv: # shipped t* parts + new n* parts in one package: do the weights de-duplicate?
|
| 68 |
+
fns = {f"t{T}": Path(f"mf/parts/dec_b3_t{T}_fp16.mlpackage") for T in TS} | {f"n{T}": p for T, p in parts.items()}
|
| 69 |
+
mt = out / "merge_test_fp16.mlpackage"
|
| 70 |
+
print(f"MERGE-TEST t16..t96 + n16..n96: {merge(fns, mt, 't16'):.1f} MB (shipped mf/dec_mf_fp16 {size('mf/dec_mf_fp16.mlpackage'):.1f} MB)")
|
| 71 |
+
for w in mt.rglob("*.bin"):
|
| 72 |
+
print(" weight file", w.relative_to(mt), f"{w.stat().st_size/1e6:.1f} MB")
|
| 73 |
+
shutil.rmtree(mt)
|