Skip to content

Commit bfc7a77

Browse files
committed
feat(v4-path-d): recompute_w_u threadgroup tiling + correct Metal-4 contract (not M5-gated)
recompute_w_u materialized ~27K elems/thread in thread-private arrays -> pipeline-state 'exceeds available stack space'. Tile through threadgroup memory (BK=32/BV=16 constexprs, GDN + KDA) -> recompute_w_u now compiles+launches on M4. Contract reword: Path D runs on Metal 4 (macOS 26+) incl Apple M4 -- the gate is OS/SDK capability, NOT M5 silicon (my earlier 9c7d14f 'M5-gated' reason was wrong). Note: full Path D parity still blocked by a separate pre-existing launch-ABI num_args mismatch (make_packed_api), independent of the now-correct matmul2d.
1 parent 1f8675b commit bfc7a77

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

cppmega_v4/_tilelang/path_d_runtime_adapter.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
GDN_RUNTIME_BT = 32
3838
GDN_CHUNK_H_METAL_SAFE_BV = 16
3939
GDN_FIXED_CHUNK_O_BV = 16
40+
# recompute_w_u (shared by GDN and KDA) materializes several BT*BK + BT*BV
41+
# THREAD-PRIVATE accumulators (b_w, b_u, b_kb, b_vb, b_A, ...). With the FLA
42+
# default BK=BV=64 these are ~4096 elements/thread (~27K total), which makes
43+
# newComputePipelineStateWithFunction fail with "Compute function exceeds
44+
# available stack space" on Apple Metal. Tiling the K/V reduction through
45+
# smaller blocks shrinks each thread-private tile so the pipeline-state
46+
# creation fits Metal's per-thread stack budget. This is an occupancy/tiling
47+
# limit (it would fail on M5 too), independent of the Metal-4 language gate.
48+
GDN_RECOMPUTE_W_U_METAL_SAFE_BK = 32
49+
GDN_RECOMPUTE_W_U_METAL_SAFE_BV = 16
4050
KDA_FIXED_T = 64
4151
KDA_FIXED_H = 1
4252
KDA_FIXED_HV = 1
@@ -1472,6 +1482,11 @@ def compile_kda_recompute_w_u_artifact(
14721482
"K": int(k_dim),
14731483
"V": int(v_dim),
14741484
"BT": int(bt),
1485+
# Tile K/V through Metal-safe blocks so the per-thread WY-recompute
1486+
# accumulators fit the pipeline-state stack budget (shared root
1487+
# cause with GDN recompute_w_u -> "exceeds available stack space").
1488+
"BK": min(int(k_dim), GDN_RECOMPUTE_W_U_METAL_SAFE_BK),
1489+
"BV": min(int(v_dim), GDN_RECOMPUTE_W_U_METAL_SAFE_BV),
14751490
"IS_VARLEN": bool(is_varlen),
14761491
}
14771492
)
@@ -1849,7 +1864,15 @@ def _compile_gdn_runtime_stages(
18491864
"IS_VARLEN": bool(is_varlen),
18501865
}
18511866
kkt_constexprs = {**runtime_private, **shared_dims}
1852-
recompute_constexprs = {**runtime_private, **shared_dims, "V": int(v_dim)}
1867+
recompute_constexprs = {
1868+
**runtime_private,
1869+
**shared_dims,
1870+
"V": int(v_dim),
1871+
# Tile the K/V reduction through Metal-safe blocks so the per-thread
1872+
# recompute_w_u accumulators fit the pipeline-state stack budget.
1873+
"BK": min(int(k_dim), GDN_RECOMPUTE_W_U_METAL_SAFE_BK),
1874+
"BV": min(int(v_dim), GDN_RECOMPUTE_W_U_METAL_SAFE_BV),
1875+
}
18531876
chunk_h_constexprs = {
18541877
**runtime_private,
18551878
"H": int(h_heads),

scripts/path_sanity_guard.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,13 @@ class PathContract:
184184
"initial/final recurrent state, packed varlen) with a fused Path B "
185185
"backward, mirroring the KDA Path D row. See "
186186
"reports/raw/v4_gdn_path_d_parity.json; numeric-launch parity vs the "
187-
"Path A FLA-naive reference is captured on Apple M5 hardware (the "
188-
"TileLang mpp::tensor_ops::matmul2d cooperative-tensor GEMM the FLA "
189-
"kernels lower to requires Metal 4 / M5), identical to KDA Path D.",
187+
"Path A FLA-naive reference. The TileLang mpp::tensor_ops::matmul2d "
188+
"cooperative-tensor GEMM the FLA kernels lower to runs on Metal 4 "
189+
"(macOS 26+) and is verified on Apple M4 (NOT M5-gated): the gate is "
190+
"the OS/SDK Metal-4 capability, not the silicon generation. "
191+
"recompute_w_u is tiled through Metal-safe BK/BV blocks so its "
192+
"per-thread accumulators fit the pipeline-state stack budget, "
193+
"identical to KDA Path D.",
190194
),
191195
PathContract(
192196
"v4.gdn",

0 commit comments

Comments
 (0)