Commit 68eb7ce
committed
NNX: QK-Clip on NNX + NNX-format checkpoint utilities
Closes the QK-Clip TODO and migrates the remaining checkpoint utilities
to NNX, plus convert-on-load and serve-mode paths for AQT in MaxEngine.
Linen paths preserved byte-for-byte (every NNX edit is gated on
config.pure_nnx or runtime state-shape detection).
QK-Clip:
- qk_clip_utils.apply_qk_clip_nnx mutates state.model in place via
nnx.split -> pure-dict tree_map -> nnx.replace_by_pure_dict ->
nnx.update. Accepts both the production NNX intermediate shape
(self_attention.attention_op.max_logits) and the synthetic-fixture
shape from the existing Linen tests (self_attention.max_logits).
- train.py train_step dispatches to apply_qk_clip_nnx for NNX, removing
the prior TODO at the QK-Clip call site.
Checkpoint utilities (NNX paths added):
- standalone_checkpointer.checkpoint_loop builds an NNX init_state_fn
under pure_nnx; add_entropy_to_checkpoint dispatches across Linen
TrainState, NNX TrainStateNNX Module, and post-split nnx.State shapes.
- generate_param_only_checkpoint: NNX init_state_fn under pure_nnx;
_possibly_unroll_params_nnx slices scanned NNX layers via dict-style
state mutation; _save_decode_checkpoint_nnx writes a bf16 pure-dict
tree to orbax. Parallel LoRA decode flow operates on the single-nested
LoRA delta tree from PR8's get_lora_abstract_state_nnx.
- convert_gpt3_ckpt_from_paxml: parallel NNX state_map keystr
translation (.params['params']<rest> -> .model<rest>.value, etc.).
End-to-end paxml -> NNX conversion is wired but not yet validated on
hardware.
NNX + AQT in MaxEngine:
- model_creation_utils threads quant_mode_str ("train" | "convert" |
"serve") through from_config / create_model / get_nnx_create_model_fn
/ create_nnx_abstract_model / from_pretrained. Default "train"
preserves existing behavior; "serve" propagates to
configure_quantization so AQT layers don't materialize the
full-precision kernel when the on-disk checkpoint already carries
qrhs scale factors.
- maxengine.__init__ selects the quant mode from
config.checkpoint_is_quantized; _load_params_nnx drops the
NotImplementedError. Two paths: pre-quantized
(checkpoint_is_quantized=True) loads via quant_mode_str="serve";
full-precision + quantization=int8 loads in TRAIN mode and AQT
layers quantize per-forward (same numerical result for absmax
calibration).
- layerwise_quantization._load_and_quantize_nnx: whole-model NNX
convert path. Loads full-precision in TRAIN mode, transfers kernels
into a CONVERT-mode model, runs forward to populate qrhs.frozen via
the ToNNX(AqtDotGeneral) bridge, strips kernels at quantized paths,
saves serve-mode-shaped state.
Sharding helpers / from_pretrained QTensor handling:
- maxtext_utils.get_nnx_named_sharding_with_scan_axis emits a
parallel-tree of replicated NamedSharding leaves when a Variable's
value is a composite pytree (AQT serve-mode QTensor with a qvalue
int8 leaf and a list of bf16 scale leaves).
- model_creation_utils.from_pretrained: dropped a redundant
jax.set_mesh wrap in create_nnx_abstract_model that broke serve-mode
AQT under Flax 0.12.6 (NamedSharding(mesh=AbstractMesh, spec=None)
rejected). _build_value_target / _free_device_memory /
_unwrap_for_align use Variable.get_value() instead of v[...]
indexing for QTensor leaves (QTensor.__getitem__ trips on the
LogicallyPartitioned wrapper around qvalue). Also widens the restore
filter beyond nnx.Param to cover the aqt-typed qrhs.frozen Variable
type, and skips QTensor leaves in the per-axis shape-alignment
dispatch (their saved shape already matches the model). Carries
in-progress PR9.5 debugging fixes for the serve-mode reload path
(see nnx_migration.md).
Tests:
- qk_clip_test: 7 new NNX cases covering attention-type guard, MLA
wq_b/wkv_b math, both intermediate shapes, no-clip-below-threshold,
missing-stats resilience, Linen<->NNX numeric parity.
- standalone_checkpointer_nnx_test (new): 3 cases for adam mu/nu
overwrite on TrainStateNNX Module shape, no mutation of state.model
params, post-split nnx.State shape from setup_training_state.
- generate_param_only_checkpoint_nnx_test (new): 3 cases for scanned
layer slicing (Llama-style; DeepSeek-style dense+moe split; LoRA
delta unroll on the single-nested NNX shape).
- layerwise_quantization_nnx_test (new): 3 cases for
_strip_kernels_at_quantized_paths covering quantized removal,
non-quantized preservation, mixed-shape trees.
- maxengine_test: replaced test_quantize_raises_for_nnx with
test_quantize_passes_gate_for_nnx; added
test_load_pre_quantized_nnx_passes_quant_gate and
test_quantized_prefill_nnx_train_mode (real numerical verification
with quantization=int8 + random params + TRAIN mode).
End-to-end: convert-mode forward + qrhs.frozen extraction +
serve-mode-shape save validated on TPU. Reload in serve mode is the
remaining piece; details and status in nnx_migration.md PR9.5 entry.1 parent 10bfe3f commit 68eb7ce
16 files changed
Lines changed: 1544 additions & 218 deletions
File tree
- src/maxtext
- checkpoint_conversion/standalone_scripts
- inference/maxengine
- models
- trainers/pre_train
- utils
- tests/unit
Lines changed: 61 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
| |||
87 | 92 | | |
88 | 93 | | |
89 | 94 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
98 | 112 | | |
99 | 113 | | |
100 | 114 | | |
| |||
103 | 117 | | |
104 | 118 | | |
105 | 119 | | |
106 | | - | |
107 | 120 | | |
108 | 121 | | |
109 | 122 | | |
| |||
188 | 201 | | |
189 | 202 | | |
190 | 203 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
195 | 219 | | |
196 | 220 | | |
197 | 221 | | |
| |||
203 | 227 | | |
204 | 228 | | |
205 | 229 | | |
206 | | - | |
207 | | - | |
208 | 230 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
219 | 251 | | |
220 | 252 | | |
221 | 253 | | |
| |||
267 | 299 | | |
268 | 300 | | |
269 | 301 | | |
270 | | - | |
271 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
272 | 305 | | |
273 | | - | |
| 306 | + | |
274 | 307 | | |
275 | 308 | | |
276 | 309 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
123 | 129 | | |
124 | 130 | | |
125 | | - | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
126 | 134 | | |
127 | | - | |
| 135 | + | |
128 | 136 | | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
| |||
371 | 380 | | |
372 | 381 | | |
373 | 382 | | |
374 | | - | |
375 | | - | |
376 | | - | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
377 | 392 | | |
378 | 393 | | |
379 | 394 | | |
| |||
396 | 411 | | |
397 | 412 | | |
398 | 413 | | |
399 | | - | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
400 | 418 | | |
401 | | - | |
402 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
403 | 449 | | |
404 | 450 | | |
405 | | - | |
| 451 | + | |
406 | 452 | | |
407 | 453 | | |
408 | 454 | | |
| |||
485 | 531 | | |
486 | 532 | | |
487 | 533 | | |
488 | | - | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
489 | 544 | | |
490 | 545 | | |
491 | 546 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
235 | 236 | | |
236 | 237 | | |
237 | 238 | | |
| 239 | + | |
238 | 240 | | |
239 | 241 | | |
240 | 242 | | |
| |||
252 | 254 | | |
253 | 255 | | |
254 | 256 | | |
| 257 | + | |
255 | 258 | | |
256 | 259 | | |
257 | 260 | | |
| |||
260 | 263 | | |
261 | 264 | | |
262 | 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 | + | |
263 | 290 | | |
264 | 291 | | |
265 | 292 | | |
| |||
328 | 355 | | |
329 | 356 | | |
330 | 357 | | |
331 | | - | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
332 | 370 | | |
333 | 371 | | |
334 | 372 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
526 | 526 | | |
527 | 527 | | |
528 | 528 | | |
529 | | - | |
530 | 529 | | |
531 | 530 | | |
| 531 | + | |
| 532 | + | |
532 | 533 | | |
533 | | - | |
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
| |||
0 commit comments