Repository navigation
Conversation
…l past 255 AGX flow states, which the backend tests with 8-bit immediates, so mp3 took wrong branches on an M1 Pro
|
Thanks @totaland, the write-up is very thorough. I tested 626c841 merged on main 03c3fd8, on an M4 Pro and on the gate's M4 minis. Gates: repo 54/54, ping 48/48, test 1596/1596, same as main. I could not reproduce the bug on M4. I built your mulk job (with a 500-element list, since 2048 overflows the lane stack on both trees) next to four renamed copies of demos/pure_hvm5_mini, all called under a It does cost time on M4. terrain PAR-GPU is slower in all 4 perf runs: 0.337 to 0.363 s against main's 0.306 to 0.310 s (median +12%), and one run fails the gate at 1.16x. Locally on the M4 Pro it is 0.167 s against 0.157 s. The cost is heap_alloc_miss: making only that helper noinline gives the whole slowdown, while making only rfc_wrap and blk_copy noinline costs nothing. heap_alloc_miss is not cold on the device, because a device lane starts every kernel with empty lists, so it takes the miss path often. The other GPU cells are within noise. As you say in the description, this lowers the state count but does not bound it. A kernel about twice the size of yours would cross 255 again on an M1, so the fault stays reachable, it just needs a bigger program. There is no test, and the gate hardware does not show the fault, so nothing would catch it coming back. comp.ts goes from 63,905 to 63,975 tokens, which leaves 25 under the 64,000 cap. 62 of the 70 are the comment, and it states a fact about Apple's backend that we cannot check from our side. So as it stands this trades a measured slowdown on the gate's hardware for a partial workaround of an Apple compiler bug on M1 that we can't reproduce, and we can't merge it like this. |
|
Thanks I will check again and let you know |
|
Could not reproduce, closing because it is stale |
Why
On an M1 Pro, a small
!job gives a wrong answer on Metal when a large, unrelated function is compiled into the same program. The job is correct on the CPU, and correct on Metal in a small program.The repro is a 46-line driver (below) that runs
mk!(xs(2048n, 0)): a tail-recursivemulkover 2048F32s, then a sum. Its other branch would call one Qwen3.5 decode step (M.step, from a TensorFold model outside this repo). That branch never runs, butM.step's device code is compiled into the same kernel.--gpu off--gpu 8GB60fa05dc85.3929785.3929785.39297(3/3 runs)Depending on the build, the wrong branch shows up as a hang (the host sums a garbage list), as
frontier drained without a result, or asa function the device does not hold.How it failed
The AGX backend miscompiles the kernel once its control flow needs more than 256 states.
FID_ENTER → FID_MK, and the inlinedmulk.gospin runs all 2049 iterations. On the last one,xsisNil, yet neitherelsebranch runs. Control falls into MK's tail with the spin's output unwritten (0), orwork_loopreturns 0 and the task is lost. The front-end AIR (xcrun metal -O2 -S -emit-llvm) for that spin is correct._agc.mainfrom the.gpuarchive and disassembled it with dougallj/applegpu. The backend's structurizer keeps one 16-bit flow-state register per function (r4h) and numbers states across the whole function. It sets a state with a 16-bitmov_immor withicmpsel. The dispatch compares (if_icmp,else_icmp,while_icmp) only hold an 8-bit immediate, so a state of 256 or above is tested asstate - 256.mov_imm r4h, 319at0x3158dc, thenif_icmp r0l, ueq, r4h, 63at0x3158f2;326is tested as70,332as76, and335as79.OUTLINEwas plainstatic, so the backend inlined every cold runtime helper (heap_alloc_miss,rfc_wrap,blk_copy,span_fade,heap_hand, …) at each call site inside the singlework_loopkernel. Each inlined copy of therfc_wrap→heap_alloc→heap_alloc_misschain adds about 4 states, and mp3's kernel holds about 51 of them. That is why the failure needs enough unrelated code. When I made device segments host-only one at a time, the threshold was cumulative, with no single culprit. Filler code of the same size that adds no helper call sites does not reproduce it.Ruled out on the way:
WL_RESW > 16(a 22-word return withoutM.steppasses);maxTotalThreadsPerThreadgroup,maxCallStackDepthand threadgroup memory;volatilelocal ina32_load, and uninitializedWL_BANKregisters;coherent(device), theblk_locarguments and theerr_spunpoll.Change
On Metal,
OUTLINEis nowstatic __attribute__((noinline)), as it already is on the host and on CUDA. A comment above the dialect block says why. The helpers become real device calls, so their flow states stay in their own functions instead of piling into_agc.main.Verification
Everything below was run on this branch (
60fa05dcplus this commit) and on60fa05dc, on an Apple M1 Pro (Darwin 25.6.0, Metal compiler32023.850), with GPU jobs one at a time.mp3 repro: see the table above.
Flow states in
_agc.main(r4h):V.mulkThe 28 runnable tests in
tests/that contain a!: built-o tand run as./t, asgates/test.tsdoes. All 56 runs (upstream and this PR) print their#|lines.printer/offload_bang_gand_hcan't be built, because theirmainis a function; the gate only checks and interprets them.An rms repro (
mulkwithoutM.step):591.4099on GPU and CPU, upstream and this PR.mandelbrot (big):
3101455856in every run. Over 8 alternating pairs after a warm-up, both builds took 0.187–0.222 s.tree-matmul (big):
3797651056in every completed run. Over 10 alternating pairs:One upstream run was killed by macOS with
kIOGPUCommandBufferCallbackErrorImpactingInteractivity(the machine was not idle), so that median is over 9 runs.ttok -m gpt-3.5-turbo < bend2/comp.ts(the encoding that gives upstream's 63,905): 63,905 → 63,975 (+70), still under the 64,000 cap. Without the comment it would be +8.mp3 repro driver (needs TensorFold's
bend/model)Notes / open questions
return 0exit in the kernel, so a kernel about twice mp3's size could reach 256 again. A single-exit spin shape (do { … } while (again)) halves mp3's count again (67, measured on hand-edited generated C). It rewrites the spin macros, spin functions and call sites, so it's left as a follow-up with its own timing pass. Runningcounton the extracted_agc.mainis the check.!over a two-leaf fork tree (a 365-fork spine beside a 3-fork spine) faults; each leaf alone, a balanced 365+365, and--gpu offall pass #1394 lane-stack fix. With TensorFold's current non-tailV.mulk, the same repro also needs that fix, because 2048 frames overflow the 2048-word lane stack. The driver above uses a tail loop to isolate this bug. On a tree with both fixes,deep,forksandlsumfrom those issues still pass on GPU and CPU.