|
53 | 53 | "tl.fusion.physical_abi.physical_buffer_shapes", |
54 | 54 | ) |
55 | 55 | _ROW_DISPATCH_GRID_CHUNKS = "grid_chunks" |
| 56 | +_FORWARD_ONLY_GATE = 0 |
| 57 | +_BACKWARD_GATE = 1 |
| 58 | +_BACKWARD_FORWARD_PREPASS_GATE = 2 |
56 | 59 |
|
57 | 60 |
|
58 | 61 | def _decode_attr(prim_func: Any, key: str) -> Any: |
@@ -98,6 +101,7 @@ class PathCAbiManifest: |
98 | 101 | bank_shapes: Mapping[str, tuple[int, ...]] |
99 | 102 | bank_dtypes: Mapping[str, str] |
100 | 103 | internal_scratch_buffers: tuple[str, ...] |
| 104 | + internal_scratch_aliases: Mapping[str, Mapping[str, Any]] |
101 | 105 | cotangent_seed_buffers: tuple[str, ...] |
102 | 106 | top_level_scratch_shapes: Mapping[str, tuple[int, ...]] |
103 | 107 | top_level_scratch_dtypes: Mapping[str, str] |
@@ -152,6 +156,10 @@ def load_path_c_abi_manifest(prim_func: Any) -> PathCAbiManifest: |
152 | 156 | scratch_raw = ( |
153 | 157 | json.loads(str(scratch_attr)) if scratch_attr is not None else [] |
154 | 158 | ) |
| 159 | + scratch_alias_attr = prim_func.attrs.get("tl.fusion.internal_scratch_abi_aliases") |
| 160 | + scratch_aliases = ( |
| 161 | + json.loads(str(scratch_alias_attr)) if scratch_alias_attr is not None else {} |
| 162 | + ) |
155 | 163 |
|
156 | 164 | cotangent_raw = prim_func.attrs.get("tl.fusion.train_step_loss_cotangent_abi") |
157 | 165 | cotangent_buffers: tuple[str, ...] |
@@ -206,6 +214,7 @@ def load_path_c_abi_manifest(prim_func: Any) -> PathCAbiManifest: |
206 | 214 | bank_shapes=bank_shapes, |
207 | 215 | bank_dtypes=bank_dtypes, |
208 | 216 | internal_scratch_buffers=tuple(scratch_raw), |
| 217 | + internal_scratch_aliases=scratch_aliases, |
209 | 218 | cotangent_seed_buffers=cotangent_buffers, |
210 | 219 | top_level_scratch_shapes=top_level_shapes, |
211 | 220 | top_level_scratch_dtypes=top_level_dtypes, |
@@ -274,8 +283,8 @@ def _flat_into_bank( |
274 | 283 | f"expected {placement.dtype}, got {value.dtype}" |
275 | 284 | ) |
276 | 285 | flat = value.reshape((placement.size,)) |
277 | | - indices = mx.arange(placement.offset, placement.offset + placement.size, dtype=mx.int32) |
278 | | - return bank.at[indices].add(flat - bank[indices]) |
| 286 | + slot = slice(placement.offset, placement.offset + placement.size) |
| 287 | + return bank.at[slot].add(flat - bank[slot]) |
279 | 288 |
|
280 | 289 |
|
281 | 290 | def _bank_slot_view( |
@@ -489,6 +498,8 @@ def __call__( |
489 | 498 | # (uses top_level_scratch_shapes). |
490 | 499 | scratch_buffers: dict[str, mx.array] = {} |
491 | 500 | for name in manifest.internal_scratch_buffers: |
| 501 | + if name in manifest.internal_scratch_aliases: |
| 502 | + continue |
492 | 503 | if name in manifest.logical_to_physical: |
493 | 504 | placement = manifest.logical_to_physical[name] |
494 | 505 | scratch_buffers[name] = mx.zeros( |
@@ -545,7 +556,7 @@ def __call__( |
545 | 556 | positional.append(0) |
546 | 557 | elif logical_name == manifest.backward_gate_param: |
547 | 558 | backward_gate_param_index = len(positional) |
548 | | - positional.append(1 if run_backward else 0) |
| 559 | + positional.append(_BACKWARD_GATE if run_backward else _FORWARD_ONLY_GATE) |
549 | 560 | else: |
550 | 561 | raise ValueError( |
551 | 562 | f"compiled PrimFunc has an unexpected parameter {param_name!r} " |
@@ -676,19 +687,16 @@ def launch_chunk(chunk_index: int, subchunk_index: int) -> None: |
676 | 687 | if row_subchunk_param_index is not None: |
677 | 688 | positional[row_subchunk_param_index] = int(subchunk_index) |
678 | 689 | apply_returned_outputs(self._kernel(*positional)) |
679 | | - eval_positionals() |
680 | 690 |
|
681 | 691 | if run_backward and backward_gate_param_index is not None: |
682 | | - positional[backward_gate_param_index] = 0 |
683 | | - forward_subchunk_count = int(manifest.row_subchunk_count or 1) |
| 692 | + positional[backward_gate_param_index] = _BACKWARD_FORWARD_PREPASS_GATE |
684 | 693 | for chunk_index in range(chunk_count): |
685 | | - for subchunk_index in range(forward_subchunk_count): |
686 | | - launch_chunk(chunk_index, subchunk_index) |
| 694 | + launch_chunk(chunk_index, 0) |
687 | 695 | final_forward_states = snapshot_logical_buffers( |
688 | 696 | _STATE_AFTER_LOGICAL_BUFFERS |
689 | 697 | ) |
690 | 698 | restore_logical_buffers(initial_backward_states) |
691 | | - positional[backward_gate_param_index] = 1 |
| 699 | + positional[backward_gate_param_index] = _BACKWARD_GATE |
692 | 700 | launch_chunk(0, 0) |
693 | 701 | restore_logical_buffers(final_forward_states) |
694 | 702 | else: |
|
0 commit comments