Commit 20860b7
committed
perf(mamba3-bwd): replace lane-disjoint global atomic_add with non-atomic RMW
PROFILE (M4 Max, full-scale local_gb10_quarter, hidden=3584 S=4096):
- GPU-vs-CPU split (Metal System Trace, metal-gpu-intervals): the 0.58-0.64s
per reverse time-step is ~100% PURE GPU compute. Each launch = ONE Compute
Command; CPU->GPU latency ~0.2-1.0ms (<0.2%). Not a CPU/eval/sync problem.
- Dispatch: T.Kernel(1, threads=1024) -> a SINGLE threadgroup => ~1/40 of the
40 GPU cores. Confirmed from the generated PrimFunc source.
- Replay fraction: checkpoint-interval sweep {8,4,2,1} -> steady 0.64/0.54/
0.52/0.510s. Even with ZERO replay (interval=1) it is 0.510s/step, so the
per-step backward math (not the replay) dominates the floor; each extra
replay row adds only ~0.039s.
- Limiter: NOT ALU. Diagnostic (atomic->scratch) showed the in_proj_weight_grad
scatter loop's entire 0.198s/step cost IS the two relaxed global atomic_add
ops -- removing the atomics but keeping the 18784xH loop+MAC was free. Metal
serializes relaxed global atomics; the kernel is atomic/memory-bound.
FIX: every grad-scatter whose global address is uniquely owned by its thread
lane within a launch (in/out-proj weight, conv weight/bias, dt_bias, B/C norm
& bias) is converted from T.atomic_add to a plain read-modify-write. The time
loop is serial within the single threadgroup, so cross-step accumulation stays
correct; no two lanes ever touch the same address in one step. hidden_grad and
the shared-scratch reductions (D, a/dt_grad, b/c_group_grad, project_grad angle)
genuinely collide across lanes and KEEP their atomics.
RESULT (full scale):
- no-replay floor: 0.510 -> 0.348s/step (1.47x, -32%)
- production interval=8 block average: ~0.595 -> ~0.466s/step (~1.28x, -22%)
- window=2 steady: ~1.19s -> ~0.94s (~0.47s/step)
GRADIENT PARITY: bitwise-identical. tiny-smoke fwd+bwd, 15 non-zero mamba3 grad
buffers, worst max|baseline-fixed| = 0.000e+00.
Adds isolation probes: _bwd_shape_introspect (analytic per-step cost),
_bwd_dump_source (kernel launch + loop structure), _bwd_ckpt_sweep (replay
fraction). The dominant remaining inefficiency is the single-threadgroup
dispatch (~1/40 GPU); see report for the grid-parallel feasibility plan.1 parent b18f415 commit 20860b7
4 files changed
Lines changed: 421 additions & 39 deletions
File tree
- cppmega_mlx/runtime
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11581 | 11581 | | |
11582 | 11582 | | |
11583 | 11583 | | |
| 11584 | + | |
| 11585 | + | |
| 11586 | + | |
11584 | 11587 | | |
11585 | | - | |
11586 | | - | |
11587 | | - | |
| 11588 | + | |
| 11589 | + | |
11588 | 11590 | | |
11589 | 11591 | | |
11590 | 11592 | | |
| |||
11845 | 11847 | | |
11846 | 11848 | | |
11847 | 11849 | | |
| 11850 | + | |
| 11851 | + | |
11848 | 11852 | | |
11849 | | - | |
11850 | | - | |
11851 | | - | |
11852 | | - | |
| 11853 | + | |
| 11854 | + | |
| 11855 | + | |
11853 | 11856 | | |
11854 | 11857 | | |
11855 | 11858 | | |
11856 | 11859 | | |
11857 | | - | |
11858 | | - | |
| 11860 | + | |
11859 | 11861 | | |
11860 | 11862 | | |
11861 | 11863 | | |
| |||
11876 | 11878 | | |
11877 | 11879 | | |
11878 | 11880 | | |
| 11881 | + | |
11879 | 11882 | | |
11880 | | - | |
11881 | | - | |
11882 | | - | |
11883 | | - | |
| 11883 | + | |
| 11884 | + | |
| 11885 | + | |
11884 | 11886 | | |
11885 | 11887 | | |
11886 | 11888 | | |
11887 | 11889 | | |
11888 | | - | |
11889 | | - | |
| 11890 | + | |
11890 | 11891 | | |
11891 | 11892 | | |
11892 | 11893 | | |
| |||
11935 | 11936 | | |
11936 | 11937 | | |
11937 | 11938 | | |
| 11939 | + | |
11938 | 11940 | | |
11939 | | - | |
11940 | | - | |
| 11941 | + | |
11941 | 11942 | | |
11942 | 11943 | | |
11943 | 11944 | | |
| |||
11951 | 11952 | | |
11952 | 11953 | | |
11953 | 11954 | | |
| 11955 | + | |
| 11956 | + | |
| 11957 | + | |
11954 | 11958 | | |
11955 | | - | |
11956 | | - | |
11957 | | - | |
| 11959 | + | |
| 11960 | + | |
11958 | 11961 | | |
11959 | 11962 | | |
11960 | | - | |
11961 | | - | |
11962 | | - | |
| 11963 | + | |
| 11964 | + | |
11963 | 11965 | | |
11964 | 11966 | | |
11965 | 11967 | | |
| |||
12012 | 12014 | | |
12013 | 12015 | | |
12014 | 12016 | | |
| 12017 | + | |
| 12018 | + | |
12015 | 12019 | | |
12016 | | - | |
12017 | | - | |
| 12020 | + | |
| 12021 | + | |
12018 | 12022 | | |
12019 | 12023 | | |
12020 | 12024 | | |
| |||
12038 | 12042 | | |
12039 | 12043 | | |
12040 | 12044 | | |
| 12045 | + | |
12041 | 12046 | | |
12042 | | - | |
12043 | | - | |
| 12047 | + | |
12044 | 12048 | | |
12045 | 12049 | | |
12046 | 12050 | | |
| |||
12072 | 12076 | | |
12073 | 12077 | | |
12074 | 12078 | | |
| 12079 | + | |
| 12080 | + | |
12075 | 12081 | | |
12076 | | - | |
12077 | | - | |
| 12082 | + | |
| 12083 | + | |
12078 | 12084 | | |
12079 | 12085 | | |
12080 | 12086 | | |
| |||
12084 | 12090 | | |
12085 | 12091 | | |
12086 | 12092 | | |
| 12093 | + | |
| 12094 | + | |
| 12095 | + | |
12087 | 12096 | | |
12088 | | - | |
12089 | | - | |
12090 | | - | |
12091 | | - | |
| 12097 | + | |
| 12098 | + | |
| 12099 | + | |
12092 | 12100 | | |
12093 | 12101 | | |
12094 | 12102 | | |
| |||
12108 | 12116 | | |
12109 | 12117 | | |
12110 | 12118 | | |
| 12119 | + | |
12111 | 12120 | | |
12112 | | - | |
12113 | | - | |
12114 | | - | |
| 12121 | + | |
| 12122 | + | |
| 12123 | + | |
12115 | 12124 | | |
12116 | 12125 | | |
12117 | 12126 | | |
| |||
12130 | 12139 | | |
12131 | 12140 | | |
12132 | 12141 | | |
| 12142 | + | |
| 12143 | + | |
| 12144 | + | |
| 12145 | + | |
| 12146 | + | |
12133 | 12147 | | |
12134 | | - | |
12135 | | - | |
12136 | | - | |
| 12148 | + | |
| 12149 | + | |
12137 | 12150 | | |
12138 | 12151 | | |
| 12152 | + | |
| 12153 | + | |
12139 | 12154 | | |
12140 | 12155 | | |
12141 | 12156 | | |
| |||
| 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 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 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 | + | |
| 125 | + | |
0 commit comments