Skip to content

Commit b269b61

Browse files
committed
refactor: set path_b as the default backward route for Mamba3 in Path C training and add support for explicit full-Path-C backward overrides.
1 parent e2d3a0b commit b269b61

8 files changed

Lines changed: 151 additions & 11 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"template_name": "v4_smoke_h1024_L12",
3+
"hidden_size": 1024,
4+
"layers": 12,
5+
"seq_len": 256,
6+
"batch_size": 1,
7+
"steps": 3,
8+
"parquet_path": "/Volumes/external/sources/cppmega.mlx/data/parquet_samples/gb10/clang_semantic_4k_v10/val_00000.parquet",
9+
"fwd_median_ms": 59.37258299672976,
10+
"bwd_median_ms": 27.799417002825066,
11+
"total_median_ms": 90.63329198397696,
12+
"tokens_per_sec": 2824.569144473514,
13+
"per_step": [
14+
{
15+
"step": 0,
16+
"fwd_ms": 96.77291702246293,
17+
"bwd_ms": 7.3932919767685235,
18+
"total_ms": 104.16620899923146,
19+
"loss": 11.183396339416504
20+
},
21+
{
22+
"step": 1,
23+
"fwd_ms": 56.313292006962,
24+
"bwd_ms": 34.31999997701496,
25+
"total_ms": 90.63329198397696,
26+
"loss": 11.018841743469238
27+
},
28+
{
29+
"step": 2,
30+
"fwd_ms": 59.37258299672976,
31+
"bwd_ms": 27.799417002825066,
32+
"total_ms": 87.17199999955483,
33+
"loss": 10.956659317016602
34+
}
35+
]
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"template_name": "v4_smoke_h1024_L8",
3+
"hidden_size": 1024,
4+
"layers": 8,
5+
"seq_len": 256,
6+
"batch_size": 1,
7+
"steps": 3,
8+
"parquet_path": "/Volumes/external/sources/cppmega.mlx/data/parquet_samples/gb10/clang_semantic_4k_v10/val_00000.parquet",
9+
"fwd_median_ms": 6.824791984399781,
10+
"bwd_median_ms": 0.4123759863432497,
11+
"total_median_ms": 7.097541994880885,
12+
"tokens_per_sec": 36068.82497978039,
13+
"per_step": [
14+
{
15+
"step": 0,
16+
"fwd_ms": 34.21054198406637,
17+
"bwd_ms": 31.339375040261075,
18+
"total_ms": 65.54991702432744,
19+
"loss": 11.423054695129395
20+
},
21+
{
22+
"step": 1,
23+
"fwd_ms": 6.824791984399781,
24+
"bwd_ms": 0.1975830236915499,
25+
"total_ms": 7.0223750080913305,
26+
"loss": 11.195006370544434
27+
},
28+
{
29+
"step": 2,
30+
"fwd_ms": 6.685166008537635,
31+
"bwd_ms": 0.4123759863432497,
32+
"total_ms": 7.097541994880885,
33+
"loss": 11.354619026184082
34+
}
35+
]
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"template_name": "v4_smoke_h2048_L12",
3+
"hidden_size": 2048,
4+
"layers": 12,
5+
"seq_len": 256,
6+
"batch_size": 1,
7+
"steps": 2,
8+
"parquet_path": "/Volumes/external/sources/cppmega.mlx/data/parquet_samples/gb10/clang_semantic_4k_v10/val_00000.parquet",
9+
"fwd_median_ms": 222.840249989531,
10+
"bwd_median_ms": 28.563021012814716,
11+
"total_median_ms": 154.23662500688806,
12+
"tokens_per_sec": 1659.7873558797548,
13+
"per_step": [
14+
{
15+
"step": 0,
16+
"fwd_ms": 352.65866699046455,
17+
"bwd_ms": 0.0,
18+
"total_ms": 158.32537499954924,
19+
"loss": 11.305686950683594
20+
},
21+
{
22+
"step": 1,
23+
"fwd_ms": 93.02183298859745,
24+
"bwd_ms": 57.12604202562943,
25+
"total_ms": 150.14787501422688,
26+
"loss": 11.046894073486328
27+
}
28+
]
29+
}

scripts/bench_1b_training_matrix.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ def build_parser() -> argparse.ArgumentParser:
178178
parser.add_argument("--dtypes", default=",".join(DTYPE_CHOICES))
179179
parser.add_argument("--optimizers", default=",".join(OPTIMIZER_CHOICES))
180180
parser.add_argument("--paths", default=",".join(PATH_CHOICES))
181+
parser.add_argument(
182+
"--mamba3-bwd",
183+
choices=("path_b", "path_c"),
184+
default="path_b",
185+
help=(
186+
"Backward route for Mamba3 inside Path C cells. The default keeps "
187+
"the performance-safe Path C forward + Path B backward route; use "
188+
"--mamba3-bwd path_c only for an explicit full-Path-C experiment."
189+
),
190+
)
181191
parser.add_argument("--fresh-process", action="store_true", default=False)
182192
parser.add_argument("--out", type=Path, default=DEFAULT_OUT)
183193
parser.add_argument("--csv", type=Path, default=DEFAULT_CSV)
@@ -231,6 +241,7 @@ def path_env_and_support(
231241
dtype: str,
232242
path: str,
233243
cache_dir: Path,
244+
mamba3_bwd: str,
234245
) -> tuple[dict[str, str], bool, str | None, str, Path | None]:
235246
if dtype == "fp8" and path == "path_b":
236247
return (
@@ -265,7 +276,7 @@ def path_env_and_support(
265276
"CPPMEGA_KERNEL_PATH__MAMBA3_MIMO": "path_c",
266277
"CPPMEGA_KERNEL_PATH__M2RNN": "path_c",
267278
"CPPMEGA_KERNEL_PATH__SPARSE_MLA": "path_c",
268-
MAMBA3_PATH_C_BWD_ENV: "path_c",
279+
MAMBA3_PATH_C_BWD_ENV: mamba3_bwd,
269280
}
270281
if dtype == "fp8":
271282
env[SPARSE_MLA_FP8_ROUTE_ENV] = "path_c"
@@ -276,7 +287,7 @@ def path_env_and_support(
276287
"CPPMEGA_KERNEL_PATH__MAMBA3_MIMO": "path_c",
277288
"CPPMEGA_KERNEL_PATH__M2RNN": "path_c",
278289
"CPPMEGA_KERNEL_PATH__SPARSE_MLA": "path_c",
279-
MAMBA3_PATH_C_BWD_ENV: "path_c",
290+
MAMBA3_PATH_C_BWD_ENV: mamba3_bwd,
280291
}
281292
if dtype == "fp8":
282293
env[SPARSE_MLA_FP8_ROUTE_ENV] = "path_c"
@@ -301,6 +312,7 @@ def build_cell(
301312
dtype=dtype,
302313
path=path,
303314
cache_dir=cache_dir,
315+
mamba3_bwd=args.mamba3_bwd,
304316
)
305317
if not path_supported:
306318
supported = False
@@ -858,6 +870,7 @@ def main(argv: list[str] | None = None) -> int:
858870
"dtypes": list(parse_csv_list(args.dtypes, DTYPE_CHOICES)),
859871
"optimizers": list(parse_csv_list(args.optimizers, OPTIMIZER_CHOICES)),
860872
"paths": list(parse_csv_list(args.paths, PATH_CHOICES)),
873+
"mamba3_bwd": args.mamba3_bwd,
861874
"fresh_process": bool(args.fresh_process),
862875
"dry_run": bool(args.dry_run),
863876
"reuse_existing_ok": bool(args.reuse_existing_ok),

scripts/m04_train_step.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
MAMBA3_PATH_C_BWD_ENV = "CPPMEGA_MAMBA3_PATH_C_BWD"
162162
FP8_PATH_C_RUNTIME_ENV: dict[str, str] = {
163163
SPARSE_MLA_FP8_ROUTE_ENV: "path_c",
164-
MAMBA3_PATH_C_BWD_ENV: "path_c",
164+
MAMBA3_PATH_C_BWD_ENV: "path_b",
165165
}
166166
FP8_PATH_B_RUNTIME_ENV: dict[str, str] = {SPARSE_MLA_FP8_ROUTE_ENV: "path_b"}
167167
FP8_PATH_C_SPLIT_GRAD_UPDATE_EVAL_REASON = (
@@ -1061,10 +1061,13 @@ def fp8_path_c_training_route_payload(
10611061
"fp8_route_auto_selected": True,
10621062
"fp8_route_reason": (
10631063
"Path C now uses direct tvm-ffi owner-output for contiguous "
1064-
"selective-scan buffers, including Path C backward, and "
1065-
"matches Path B dispatch geometry on that explicit boundary"
1064+
"selective-scan forward buffers and keeps the performance-safe "
1065+
"Path B backward route until full Path C backward wins the "
1066+
"1B training matrix"
10661067
),
1067-
"training_surface": True,
1068+
"training_surface": False,
1069+
"full_path_c_backward_available": True,
1070+
"default_backward_route": "path_b",
10681071
},
10691072
{
10701073
"name": "m2rnn_path_c",

scripts/render_1b_training_matrix_html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,15 +550,15 @@ def render_route_legend() -> str:
550550
</div>
551551
<div class="route-card">
552552
<h3>BF16 Path C</h3>
553-
<p>Runs <code>--dtype bfloat16</code> with <code>CPPMEGA_KERNEL_PATH=path_c</code> and <code>CPPMEGA_MAMBA3_PATH_C_BWD=path_c</code>. Mamba3, M2RNN, and Sparse-MLA are all requested through Path C; no Path B backward fallback is allowed in these cells.</p>
553+
<p>Runs <code>--dtype bfloat16</code> with <code>CPPMEGA_KERNEL_PATH=path_c</code>. The default matrix keeps Mamba3 on Path C forward plus Path B backward via <code>--mamba3-bwd path_b</code>; pass <code>--mamba3-bwd path_c</code> only for an explicit full-Path-C experiment.</p>
554554
</div>
555555
<div class="route-card">
556556
<h3>FP8 Path B</h3>
557557
<p>Runs <code>--dtype fp8_path_b</code> and forces <code>CPPMEGA_KERNEL_PATH__SPARSE_MLA=path_b</code> plus <code>CPPMEGA_SPARSE_MLA_FP8_ROUTE=path_b</code>. The DSA Sparse-MLA baseline dispatch is recorded as <code>sparse_mla_fp8_reference_path_b</code>; it is an honest non-Path-C FP8 training baseline, not a Path C fallback.</p>
558558
</div>
559559
<div class="route-card">
560560
<h3>FP8 Path C</h3>
561-
<p>Runs <code>--dtype fp8_path_c</code>, <code>CPPMEGA_SPARSE_MLA_FP8_ROUTE=path_c</code>, and the same Mamba3 Path C forward + Path B backward training policy. Sparse-MLA consumes prepared <code>q_fp8/q_scale/kv_fp8/kv_scale</code> buffers through the TileLang/tvm-ffi route.</p>
561+
<p>Runs <code>--dtype fp8_path_c</code>, <code>CPPMEGA_SPARSE_MLA_FP8_ROUTE=path_c</code>, and the selected Mamba3 backward policy from the matrix command. Sparse-MLA consumes prepared <code>q_fp8/q_scale/kv_fp8/kv_scale</code> buffers through the TileLang/tvm-ffi route.</p>
562562
</div>
563563
</div>
564564
</section>

tests/test_bench_1b_training_matrix.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_bench_1b_matrix_plan_covers_dtype_optimizer_path_cells(
4949
assert by_case["bf16_adamw_path_c_cold"].env["CPPMEGA_KERNEL_PATH"] == "path_c"
5050
assert by_case["bf16_adamw_path_c_cold"].env["CPPMEGA_KERNEL_PATH__MAMBA3_MIMO"] == "path_c"
5151
assert by_case["bf16_adamw_path_c_cold"].env["CPPMEGA_KERNEL_PATH__M2RNN"] == "path_c"
52-
assert by_case["bf16_adamw_path_c_cold"].env["CPPMEGA_MAMBA3_PATH_C_BWD"] == "path_c"
52+
assert by_case["bf16_adamw_path_c_cold"].env["CPPMEGA_MAMBA3_PATH_C_BWD"] == "path_b"
5353
assert by_case["bf16_adamw_path_c_cold"].cache_mode == "cold"
5454
assert by_case["bf16_adamw_path_c_warm"].cache_mode == "warm"
5555
assert "--seq-len" in by_case["bf16_adamw_path_b"].command
@@ -67,6 +67,26 @@ def test_bench_1b_matrix_plan_covers_dtype_optimizer_path_cells(
6767
assert by_case["fp8_adamw_path_c_warm"].env["CPPMEGA_SPARSE_MLA_FP8_ROUTE"] == "path_c"
6868

6969

70+
def test_bench_1b_matrix_can_explicitly_force_full_mamba3_path_c_bwd(
71+
tmp_path: Path,
72+
) -> None:
73+
args = _args(
74+
tmp_path,
75+
"--dtypes",
76+
"bf16",
77+
"--optimizers",
78+
"adamw",
79+
"--paths",
80+
"path_c_warm",
81+
"--mamba3-bwd",
82+
"path_c",
83+
)
84+
cell = matrix.build_cells(args)[0]
85+
86+
assert cell.env["CPPMEGA_KERNEL_PATH__MAMBA3_MIMO"] == "path_c"
87+
assert cell.env["CPPMEGA_MAMBA3_PATH_C_BWD"] == "path_c"
88+
89+
7090
def test_bench_1b_matrix_dry_run_writes_markdown_csv_and_json(
7191
tmp_path: Path,
7292
) -> None:
@@ -106,6 +126,7 @@ def test_bench_1b_matrix_dry_run_writes_markdown_csv_and_json(
106126
payload = json.loads((tmp_path / "matrix.json").read_text(encoding="utf-8"))
107127
assert payload["scope"] == "cppmega_1b_path_matrix"
108128
assert payload["config"]["block_size"] == 2048
129+
assert payload["config"]["mamba3_bwd"] == "path_b"
109130

110131

111132
def test_bench_1b_matrix_extracts_m04_receipt_metrics(

tests/test_m04_train_step.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_fp8_path_policies_set_explicit_runtime_routes(
114114
with m04_train_step.fp8_path_c_kernel_policy(path_c_args):
115115
assert os.environ["CPPMEGA_KERNEL_PATH__SPARSE_MLA"] == "path_c"
116116
assert os.environ["CPPMEGA_SPARSE_MLA_FP8_ROUTE"] == "path_c"
117-
assert os.environ["CPPMEGA_MAMBA3_PATH_C_BWD"] == "path_c"
117+
assert os.environ["CPPMEGA_MAMBA3_PATH_C_BWD"] == "path_b"
118118
assert "CPPMEGA_SPARSE_MLA_FP8_ROUTE" not in os.environ
119119
assert "CPPMEGA_MAMBA3_PATH_C_BWD" not in os.environ
120120

@@ -1191,7 +1191,9 @@ def test_fp8_path_c_training_dtype_route_blocks_missing_sparse_mla_producer(
11911191
surface["name"]: surface for surface in route["available_path_c_surfaces"]
11921192
}
11931193
assert surfaces["matmul_tl_fp8_scaled_matmul"]["kernel_surface_available"] is True
1194-
assert surfaces["mamba3_mimo_path_c"]["training_surface"] is True
1194+
assert surfaces["mamba3_mimo_path_c"]["training_surface"] is False
1195+
assert surfaces["mamba3_mimo_path_c"]["full_path_c_backward_available"] is True
1196+
assert surfaces["mamba3_mimo_path_c"]["default_backward_route"] == "path_b"
11951197
assert surfaces["mamba3_mimo_path_c"]["fp8_route_auto_selected"] is True
11961198
assert surfaces["m2rnn_path_c"]["training_surface"] is True
11971199
assert surfaces["m2rnn_path_c"]["fallback_to_path_b_allowed"] is False

0 commit comments

Comments
 (0)