Commit 238cbf4
committed
feat(v4): bwd for GDN/KDA Path B + wire Path E training=True (PR #1217 VJP)
Three landings, all enabling actual training (fwd+bwd):
1. cppmega_v4/_tilelang/linear_attention_path_b_bwd.py
- gdn_apply_path_b(q, k, v, beta, g) wrapped as @mx.custom_function.
- Forward = fast Path B Metal kernel (3-40× over A from earlier bench).
- VJP = mx.grad through Path A reference (algebraically identical),
guarantees correct grads. Pattern named "path_b_fwd_path_a_bwd"
mirroring mamba3's mamba3_mimo_apply_with_state_path_c_fwd_path_b_bwd.
- Future revision swaps the VJP for a hand-MSL backward kernel; this
wrapper already unlocks fwd+bwd training today.
2. cppmega_v4/_tilelang/kda_path_b_bwd.py — same pattern for KDA.
3. Path E training=True wiring:
- Vendored mlx-lm PR #1217's gated_delta_vjp.py (179 LoC, Python
chunked-VJP reference) and gated_delta_vjp_metal.py (772 LoC, Metal
backward kernel — 8-11× over Python).
- Patched _mlx_lm_gated_delta_vendored.py imports to point at the
vendored module names (was `from .gated_delta_vjp_metal`).
- Added `training=True` flag to our Path E adapter
(mlx_lm_gated_delta_update.py); when set, dispatches through the
chunked Metal VJP path for finite-memory training at T≥2048.
Tests (10 new):
- test_path_b_bwd.py (4): fwd parity vs Path A; bwd grad parity for
(dq, dk, dv, dbeta, dg); fwd+bwd produces finite grads on larger
shape; chain through tanh.sum() works.
- test_kda_path_b_bwd.py (3): same for KDA.
- test_path_e_training.py (3): training=True produces finite output;
matches inference within chunk tolerance (atol 1e-2); grads
propagate through mx.grad.
v4 suite: 273 passed / 2 skipped.1 parent cedf2f3 commit 238cbf4
9 files changed
Lines changed: 1243 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
311 | | - | |
312 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
313 | 314 | | |
314 | 315 | | |
315 | 316 | | |
316 | 317 | | |
317 | 318 | | |
318 | | - | |
319 | | - | |
| 319 | + | |
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| |||
0 commit comments