File size: 3,309 Bytes
83894cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# LFM2 / LFM2.5-MoE export defs for coreai-models (North Star, 2026-07-14)

The missing LiquidAI model definitions for Apple's CoreAI exporter, written by us and
**verified bit-exact vs the HF reference** (`verify_lfm2_def.py`: prefill Ξ”=2.6e-5,
decode-with-state Ξ”=1.0e-5 on logit scale ~30, fp32, 4-layer truncation covering conv +
attention + MLP + tied embeddings + state continuation).

## What's in here
- `lfm2.py` β†’ `python/src/coreai_models/models/macos/lfm2.py` (NEW) β€” dense LFM2/2.5
  hybrid: gated short-conv as L explicit taps (no aten.conv1d β€” converter-safe), conv
  state as a mutable state tensor (`conv_state`, zeros = sequence start so one graph
  serves prefill+decode), GQA attention with fused qkv + fused per-head qk RMSNorm,
  KV cache sized to attention layers only (6 not 16).
- `lfm2_moe.py` β†’ `python/src/coreai_models/models/macos/lfm2_moe.py` (NEW) β€” the 8B-A1B:
  same hybrid + 32-expert top-4 SwiGLU via **SwitchGLU/GatherMM (the routed kernel β€”
  only the 4 routed experts' slabs are read per token)**. Router matches the MLX-Swift
  reference: softmax(fp32) β†’ +expert_bias β†’ top-4 β†’ renorm. First `num_dense_layers`
  are dense MLP. Expert stacking in `_mutate_state_dict` (experts.{e}.w1/w3/w2 β†’
  switch_mlp.gate/up/down (1,E,out,in)).
- `registry.py` β†’ `python/src/coreai_models/models/registry.py` β€” adds "lfm2" + "lfm2_moe".
- `macos.py` β†’ `python/src/coreai_models/export/macos.py` β€” export hook: a model class may
  provide `build_reference_inputs(...)` + `state_names()` to declare extra mutable state
  (the conv state) beyond k_cache/v_cache.
- `verify_lfm2_def.py` β†’ repo root β€” the numerical vet harness. Run it after ANY change.

## Apply to a fresh clone of john-rocky/coreai-models
1. `git clone --depth 1 https://github.com/john-rocky/coreai-models && cd coreai-models`
2. Bump pins in `python/pyproject.toml`: `coreai-core==1.0.0b2`, `coreai-torch==0.4.1`
   (b1 artifacts are rejected by the macOS 27 beta3+ strict loader β€” FB23666783), then
   `uv lock --upgrade-package coreai-core --upgrade-package coreai-torch`.
3. Copy the four .py files to the paths above (+ verify_lfm2_def.py to the root).
4. Vet: `uv run python verify_lfm2_def.py` β†’ must print MATCH / MATCH.
5. Export 1.2B: `uv run coreai.llm.export LiquidAI/LFM2.5-1.2B-Instruct --experimental
   --compute-precision float16 --max-context-length 8192 --output-dir ~/models/lfm25-1.2b-coreai-b2`
   ⚠️ `--max-context-length 8192` is MANDATORY on an 8GB box. Without it the export bakes in the
   model's full 128000 context; the CoreAI engine's load/specialization then spikes RAM and CRASHED
   the 8GB Mac (2026-07-14). Reference bundles all use 8192. (lesson #42)
6. Bench: `Tools/CoreAIBench/.build/release/CoreAIBench <bundle-dir> 600`
   (build CoreAIBench with `DEVELOPER_DIR=/Library/Developer/CommandLineTools swift build
   -c release` + coreai-kit's Package.resolved copied in).

## 8B-A1B (lfm2_moe) status
Def is written and registered; NOT yet exported: (a) torch.export of 8.3B fp16 needs
~17GB+ RAM β€” impossible on the 8GB box, run it on a β‰₯32GB machine or HF Jobs;
(b) `AutoConfig` for model_type=lfm2_moe needs transformers β‰₯5.x (venv has 4.57 β€” bump
only for the 8B export, or construct the config by hand from config.json).