Commit eea8244
committed
Move the macOS export graph contract onto the model classes
`export/macos.py` hardcoded the standard text-LLM forward -- two inputs, two KV cache
states, one logits output -- so a model needing anything else cannot use the pipeline at
all. A model threading extra conv or recurrent state alongside the KV cache is stuck, and
one mixing sliding and full attention hits the same wall, because the two layer types need
separate cache tensors with different head counts and head dims.
Adds overridable hooks to `BaseForCausalLM`, keyed by graph name:
export_input_names() -> {graph: (name, ...)}
export_state_names() -> {graph: (name, ...)}
export_output_names() -> {graph: (name, ...)}
build_reference_inputs(config, target_dtype, spec) -> {graph: {param: tensor}}
build_dynamic_shapes(config, spec) -> {graph: shapes}
A macOS model has one graph, `main`. The defaults are exactly what `macos.py` hardcoded,
so `export_macos_model` and the pipeline's quantization step now just call them.
Two ordering rules, because the two are consumed differently. Reference inputs bind to
the traced signature, so they must be in its exact order, interleaved where the signature
interleaves inputs and states. The name lists are looked up by name at runtime, so each
carries only the relative order of its own kind.
Also in here:
* `quantize_for_export` builds the calibration trace from the hooks, so the pipeline and
any standalone recipe share it. `quantize_pytorch_model` now requires `cache_seq_len`
and `state_indices` rather than guessing them from input positions.
* `KVCache.create_cache_tensors` takes an explicit `seq_len`, replacing a
mutate-`config.max_position_embeddings`-and-restore hack. `cache_scatter`'s copy gets
it too, since its docstring promises the two are drop-in interchangeable.
* `export/_constants.py` moves to `coreai_models/_constants.py`. These are graph/runner
contract constants that `models/` now needs, and importing them from `export/` would
reverse the package dependency direction.
* Fixes a crash for contexts at or below the trace cache length: the cache dim was
declared `Dim(min=TRACE_KV_CACHE_SEQ_LEN, max=max_context_length)` unconditionally, so
`--max-context-length 2048` raised "Cannot create Dim with inconsistent min/max" from
inside torch.export. `TraceSpec` now requires `cache_seq_len <= max_context_length` --
a cache longer than the context it serves is meaningless, so callers cap it at the
context they are exporting -- and pins the cache dims when the two are equal, since a
cache traced at the full context has nowhere to grow.
No behavior change for anything that exports today: all 8 registered macOS model classes
produce bit-identical reference inputs, dynamic-shape bounds, and graph names.1 parent 1677713 commit eea8244
11 files changed
Lines changed: 829 additions & 170 deletions
File tree
- python
- src/coreai_models
- export
- models
- primitives/macos
- tests
- _runner_infra
- test_model_conversion
- test_model_units/test_export
Lines changed: 10 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
7 | 14 | | |
8 | 15 | | |
9 | 16 | | |
10 | 17 | | |
11 | 18 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 19 | + | |
| 20 | + | |
15 | 21 | | |
16 | 22 | | |
17 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
22 | 24 | | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| |||
100 | 103 | | |
101 | 104 | | |
102 | 105 | | |
| 106 | + | |
| 107 | + | |
103 | 108 | | |
104 | 109 | | |
105 | 110 | | |
| |||
119 | 124 | | |
120 | 125 | | |
121 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
122 | 131 | | |
123 | 132 | | |
124 | 133 | | |
| |||
142 | 151 | | |
143 | 152 | | |
144 | 153 | | |
145 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
146 | 159 | | |
147 | 160 | | |
148 | 161 | | |
| |||
151 | 164 | | |
152 | 165 | | |
153 | 166 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
157 | 179 | | |
158 | 180 | | |
159 | 181 | | |
| |||
162 | 184 | | |
163 | 185 | | |
164 | 186 | | |
165 | | - | |
166 | | - | |
167 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
168 | 191 | | |
169 | 192 | | |
170 | 193 | | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
175 | 201 | | |
176 | 202 | | |
177 | 203 | | |
| |||
203 | 229 | | |
204 | 230 | | |
205 | 231 | | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
206 | 292 | | |
207 | 293 | | |
208 | 294 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 21 | + | |
27 | 22 | | |
28 | 23 | | |
29 | 24 | | |
30 | 25 | | |
31 | | - | |
| 26 | + | |
32 | 27 | | |
33 | 28 | | |
34 | 29 | | |
| |||
64 | 59 | | |
65 | 60 | | |
66 | 61 | | |
67 | | - | |
| 62 | + | |
68 | 63 | | |
69 | 64 | | |
70 | 65 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
| 66 | + | |
| 67 | + | |
79 | 68 | | |
80 | | - | |
81 | | - | |
| 69 | + | |
| 70 | + | |
82 | 71 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
91 | 76 | | |
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 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
125 | 82 | | |
126 | 83 | | |
127 | 84 | | |
128 | 85 | | |
129 | | - | |
| 86 | + | |
130 | 87 | | |
131 | 88 | | |
132 | 89 | | |
| |||
197 | 154 | | |
198 | 155 | | |
199 | 156 | | |
200 | | - | |
| 157 | + | |
201 | 158 | | |
202 | 159 | | |
203 | 160 | | |
| |||
209 | 166 | | |
210 | 167 | | |
211 | 168 | | |
212 | | - | |
| 169 | + | |
| 170 | + | |
213 | 171 | | |
214 | 172 | | |
215 | 173 | | |
| |||
231 | 189 | | |
232 | 190 | | |
233 | 191 | | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | 192 | | |
239 | 193 | | |
240 | 194 | | |
241 | 195 | | |
242 | 196 | | |
243 | | - | |
244 | | - | |
245 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
246 | 200 | | |
247 | 201 | | |
248 | 202 | | |
| |||
0 commit comments