Skip to content

Commit aaa13fa

Browse files
committed
perf(tilelang): use k-parallel M2RNN Path C for small K
1 parent e4cacdd commit aaa13fa

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"schema_version": 1,
3+
"scope": "local_only",
4+
"kernel": "m2rnn_full_hybrid_r_train",
5+
"hardware_label": "M4 Max local",
6+
"workload": {
7+
"profile": "hybrid-smoke",
8+
"route": "m2rnn",
9+
"model_route": "hybrid-r",
10+
"batch_size": 2,
11+
"seq_len": 64,
12+
"dtype": "bfloat16",
13+
"compile": true,
14+
"warmup_steps": 2,
15+
"measured_steps": 20,
16+
"tokens_per_step": 128,
17+
"data_contract": "synthetic_tokens"
18+
},
19+
"results": [
20+
{
21+
"path": "path_b",
22+
"kernel_used": "metal_kernel_fwd_v1",
23+
"tokens_per_second": 129382.75128422497,
24+
"mean_step_ms": 0.9893127076793461,
25+
"median_step_ms": 0.9652710286900401,
26+
"compile_time_s": 0.1551910419948399
27+
},
28+
{
29+
"path": "path_c",
30+
"kernel_used": "path_c_tilelang_dsl_packed",
31+
"scheduler": "serial_packed_before_k_parallel_threshold_fix",
32+
"tokens_per_second": 25961.985865299168,
33+
"mean_step_ms": 4.930285405134782,
34+
"median_step_ms": 4.864290996920317,
35+
"compile_time_s": 4.640729499980807
36+
},
37+
{
38+
"path": "path_c",
39+
"kernel_used": "path_c_tilelang_dsl_packed",
40+
"scheduler": "k_parallel_k4",
41+
"tokens_per_second": 126674.93,
42+
"mean_step_ms": 1.0105,
43+
"median_step_ms": 1.0372,
44+
"compile_time_s": 3.9039
45+
}
46+
],
47+
"speed": {
48+
"path_c_k_parallel_vs_path_b_tokens_per_second_ratio": 0.9790712347313265,
49+
"path_c_k_parallel_mean_step_slowdown_percent": 2.141615218708076
50+
},
51+
"matched_run_guard": "Compare only rows with identical workload, software stack, hardware label, warmup, and measured step counts."
52+
}

cppmega_mlx/nn/_tilelang/m2rnn_path_c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def _path_c_inputs_eligible(
9898
"h_steps_scratch",
9999
)
100100
_PACKED_BWD_OUTPUT_IDX = (6, 7, 8, 9, 10)
101-
_PACKED_FWD_K_PARALLEL_MIN_K = 16
102-
_PACKED_BWD_K_PARALLEL_MIN_K = 16
101+
_PACKED_FWD_K_PARALLEL_MIN_K = 4
102+
_PACKED_BWD_K_PARALLEL_MIN_K = 4
103103

104104
M2RNNFwdOwnerOutputs = tuple[mx.array, mx.array, mx.array]
105105
M2RNNBwdOwnerOutputs = tuple[

tests/test_tilelang_m2rnn_path_c.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _np(x: mx.array) -> np.ndarray:
3838
if x.dtype == mx.bfloat16:
3939
x = x.astype(mx.float32)
4040
mx.eval(x)
41-
return np.asarray(x)
41+
return np.array(x, copy=True)
4242

4343

4444
def _make_m2rnn_inputs(
@@ -110,6 +110,24 @@ def test_m2rnn_packed_path_c_large_k_uses_k_parallel_lowering() -> None:
110110
assert _msl_transform.metal_grid_for_lowering(bwd_lowering) == (32, 1, 1)
111111

112112

113+
def test_m2rnn_packed_path_c_small_k_uses_k_parallel_lowering() -> None:
114+
_require_m2rnn_path_c()
115+
116+
_fwd_kernel, fwd_lowering = m2rnn_path_c._packed_fwd_kernel_for(
117+
1, 4, 2, 4, 4, "float32"
118+
)
119+
assert fwd_lowering.grid == (2, 1, 1)
120+
assert fwd_lowering.threadgroup == (4, 1, 1)
121+
assert _msl_transform.metal_grid_for_lowering(fwd_lowering) == (8, 1, 1)
122+
123+
_bwd_kernel, bwd_lowering = m2rnn_path_c._packed_bwd_kernel_for(
124+
1, 4, 2, 4, 4, "float32"
125+
)
126+
assert bwd_lowering.grid == (2, 1, 1)
127+
assert bwd_lowering.threadgroup == (4, 1, 1)
128+
assert _msl_transform.metal_grid_for_lowering(bwd_lowering) == (8, 1, 1)
129+
130+
113131
def test_m2rnn_apply_path_c_fails_closed_instead_of_path_b(
114132
monkeypatch: pytest.MonkeyPatch,
115133
) -> None:

0 commit comments

Comments
 (0)