Commit 30dca7a
fix(metal): Phi-3.5 Q4_K_M garbage output under default build
Root cause: tq_matmul_gguf_cpu() helper unconditionally reset
tq_matmul_force_cpu to 0 after its own matmul, clobbering the
Phi-3 forward-pass invariant that tq_forward sets. The invariant
says: for has_fused_qkv models, ALL matmuls in this forward must
stay on CPU because Metal kernels produce garbage for Phi-3.5's
dims. The helper ran once for fused QKV/FFN (which it correctly
forced to CPU), then reset the flag to 0, so subsequent wo/w_down/
lm_head matmuls dispatched to Metal and corrupted intermediate
state. Output under default build: "etandideti hypothesis Rot
Rothrivial...". Phi-3.5 Q8_0 was spared because Q8 Metal kernel
happens to match CPU output; Q4_K does not.
Fix:
1. tq_matmul_gguf_cpu: save prev flag, restore (not hard-reset).
2. tq_matmul_force_cpu: drop _Thread_local. Worker threads in
the matmul thread pool must see the flag value set by the
main thread; with _Thread_local, they saw 0 (default) and
independently dispatched to Metal.
Verified:
- Phi-3.5 Q4_K_M + Metal → "in the land of Eldoria, there lived
an old sage named Alaric" (was: "etandideti hypothesis Rot...")
- Phi-3.5 Q8_0, Qwen3.5-4B Q4_K_M + Metal: no regression
Test hardening: scripts/test_models.sh gains a Metal-ON tier
that runs without TQ_NO_METAL=1. The existing tests set that env
var universally, which hid this bug for an unknown duration.
11/11 PASS with default build.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 361c40f commit 30dca7a
3 files changed
Lines changed: 23 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
95 | 105 | | |
96 | 106 | | |
97 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2364 | 2364 | | |
2365 | 2365 | | |
2366 | 2366 | | |
2367 | | - | |
| 2367 | + | |
| 2368 | + | |
| 2369 | + | |
| 2370 | + | |
| 2371 | + | |
2368 | 2372 | | |
2369 | 2373 | | |
2370 | 2374 | | |
| |||
2663 | 2667 | | |
2664 | 2668 | | |
2665 | 2669 | | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
| 2674 | + | |
2666 | 2675 | | |
2667 | 2676 | | |
2668 | | - | |
| 2677 | + | |
2669 | 2678 | | |
2670 | 2679 | | |
2671 | 2680 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2281 | 2281 | | |
2282 | 2282 | | |
2283 | 2283 | | |
2284 | | - | |
| 2284 | + | |
2285 | 2285 | | |
2286 | 2286 | | |
2287 | 2287 | | |
| |||
3008 | 3008 | | |
3009 | 3009 | | |
3010 | 3010 | | |
3011 | | - | |
| 3011 | + | |
3012 | 3012 | | |
3013 | 3013 | | |
3014 | 3014 | | |
| |||
0 commit comments