Skip to content

Commit c6579ff

Browse files
committed
feat: add support for backward stage indexing and refine path-c ABI output management and ELK UI snake layout logic
1 parent 80c7d57 commit c6579ff

10 files changed

Lines changed: 599 additions & 57 deletions

File tree

cppmega_mlx/runtime/path_c_fusion_schedules.py

Lines changed: 300 additions & 12 deletions
Large diffs are not rendered by default.

cppmega_mlx/training/compiled.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"path_c_run_backward": 1,
5757
"path_c_row_chunk_index": 0,
5858
"path_c_row_subchunk_index": 0,
59+
"path_c_backward_stage_index": 0,
5960
}
6061

6162
REGIONAL_COMPILE_TARGETS: Mapping[CompileTarget, bool] = {
@@ -148,6 +149,8 @@ def __init__(
148149
row_subchunk_count: int | None = None,
149150
row_subchunk_index_param: str | None = None,
150151
rows_per_kernel_launch: int | None = None,
152+
backward_stage_count: int | None = None,
153+
backward_stage_index_param: str | None = None,
151154
) -> None:
152155
if not callable(kernel):
153156
raise TypeError("fused train-block kernel must be callable")
@@ -200,12 +203,24 @@ def __init__(
200203
if rows_per_kernel_launch is not None
201204
else None
202205
)
206+
self.backward_stage_count = (
207+
max(1, int(backward_stage_count))
208+
if backward_stage_count is not None
209+
else None
210+
)
211+
self.backward_stage_index_param = (
212+
str(backward_stage_index_param) if backward_stage_index_param else None
213+
)
203214
self._cppmega_path_c_backward_gate_param = self.backward_gate_param
204215
self._cppmega_path_c_row_chunk_count = self.row_chunk_count
205216
self._cppmega_path_c_row_chunk_index_param = self.row_chunk_index_param
206217
self._cppmega_path_c_row_subchunk_count = self.row_subchunk_count
207218
self._cppmega_path_c_row_subchunk_index_param = self.row_subchunk_index_param
208219
self._cppmega_path_c_rows_per_kernel_launch = self.rows_per_kernel_launch
220+
self._cppmega_path_c_backward_stage_count = self.backward_stage_count
221+
self._cppmega_path_c_backward_stage_index_param = (
222+
self.backward_stage_index_param
223+
)
209224
self._logical_gradient_names = frozenset(
210225
name for name in self.physical_abi_map if name.endswith("_grad")
211226
)
@@ -486,6 +501,8 @@ def value_and_grad_contract(self) -> Mapping[str, Any]:
486501
"row_subchunk_count": self.row_subchunk_count,
487502
"row_subchunk_index_param": self.row_subchunk_index_param,
488503
"rows_per_kernel_launch": self.rows_per_kernel_launch,
504+
"backward_stage_count": self.backward_stage_count,
505+
"backward_stage_index_param": self.backward_stage_index_param,
489506
}
490507

491508

cppmega_mlx/training/path_c_fused_replay.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def build_fused_replay_boundary_custom_function(
125125
row_chunk_index_param: str | None = None,
126126
row_subchunk_count: int | None = None,
127127
row_subchunk_index_param: str | None = None,
128+
backward_stage_count: int | None = None,
129+
backward_stage_index_param: str | None = None,
128130
backward_gate_param: str = "path_c_run_backward",
129131
) -> FusedReplayBoundaryCallable:
130132
"""Return ``f(hidden_entry, *params) -> boundary outputs``.
@@ -160,6 +162,8 @@ def build_fused_replay_boundary_custom_function(
160162
max(1, int(row_subchunk_count or 1)) if subchunk_param else 1
161163
)
162164
gate_param = str(backward_gate_param or "path_c_run_backward")
165+
stage_param = str(backward_stage_index_param or "")
166+
stage_count = max(1, int(backward_stage_count or 1)) if stage_param else 1
163167
backward_sync_outputs = (
164168
f"{hidden_entry_logical_name}_grad",
165169
*(
@@ -192,10 +196,21 @@ def _launch_all(*, run_backward: bool) -> None:
192196
row_chunk_index_param=chunk_param,
193197
row_subchunk_index_param=subchunk_param,
194198
):
195-
artifact.forward(
196-
bank_owner=bank_owner,
197-
kernel_scalar_params=scalar_params,
198-
)
199+
stage_range = range(stage_count) if run_backward else range(1)
200+
for stage_index in stage_range:
201+
stage_scalar_params = dict(scalar_params)
202+
if run_backward and stage_param:
203+
stage_scalar_params[stage_param] = int(stage_index)
204+
try:
205+
artifact.forward(
206+
bank_owner=bank_owner,
207+
kernel_scalar_params=stage_scalar_params,
208+
)
209+
except Exception as exc:
210+
raise RuntimeError(
211+
"fused replay artifact launch failed with "
212+
f"kernel_scalar_params={stage_scalar_params!r}"
213+
) from exc
199214
sync_outputs = backward_sync_outputs if run_backward else boundary_outputs
200215
mx.eval(*_logical_views(abi_map, bank_buffers, sync_outputs))
201216

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
bundle: http://127.0.0.1:8765/assets/index-B0DK3qBx.js
1+
bundle: http://127.0.0.1:8765/assets/index-DoLZxAnf.js
22
step 1 action: load preset deepseek_v4_flash
33
step 2 action: Auto Align Graph (folded)
44
[folded] 9 edges, crossings (interior) = 0
55
step 3 action: Unpack All, then Auto Align Graph
66
[unpacked] 62 edges, crossings (interior) = 6
7-
X engram_unpacked_r0_1779964444764_2_add -> dsv4_attentio × dsv4_attention_unpacked_r0_1779964444764_3 -> dsv4_atte
8-
X engram_unpacked_r0_1779964444764_2_add -> dsv4_attentio × engram_unpacked_r0_1779964444764_2_add -> dsv4_attentio
9-
X csa_hca_unpacked_r1_1779964444764_1_add -> engram_unpac × engram_unpacked_r1_1779964444764_2 -> engram_unpacked_r
10-
X csa_hca_unpacked_r1_1779964444764_1_add -> engram_unpac × csa_hca_unpacked_r1_1779964444764_1_add -> engram_unpac
11-
X lightning_indexer_unpacked_r2_1779964444764_0_add -> cs × csa_hca_unpacked_r2_1779964444764_1 -> csa_hca_unpacked
12-
X lightning_indexer_unpacked_r2_1779964444764_0_add -> cs × lightning_indexer_unpacked_r2_1779964444764_0_add -> cs
7+
X engram_unpacked_r0_1779964683031_2_add -> dsv4_attentio × dsv4_attention_unpacked_r0_1779964683031_3 -> dsv4_atte
8+
X engram_unpacked_r0_1779964683031_2_add -> dsv4_attentio × engram_unpacked_r0_1779964683031_2_add -> dsv4_attentio
9+
X csa_hca_unpacked_r1_1779964683031_1_add -> engram_unpac × engram_unpacked_r1_1779964683031_2 -> engram_unpacked_r
10+
X csa_hca_unpacked_r1_1779964683031_1_add -> engram_unpac × csa_hca_unpacked_r1_1779964683031_1_add -> engram_unpac
11+
X lightning_indexer_unpacked_r2_1779964683031_0_add -> cs × csa_hca_unpacked_r2_1779964683031_1 -> csa_hca_unpacked
12+
X lightning_indexer_unpacked_r2_1779964683031_0_add -> cs × lightning_indexer_unpacked_r2_1779964683031_0_add -> cs
1313
RESULT folded crossings = 0
1414
RESULT unpacked crossings = 6
-25 Bytes
Loading
1.28 KB
Loading

0 commit comments

Comments
 (0)