113113PATH_C_DESCRIPTOR_SCHEDULE_GENERATOR = "dynamic_brick_descriptor_generator"
114114DESCRIPTOR_DEFAULT_BUFFER_EXTENT = 4
115115DESCRIPTOR_DEFAULT_THREADS = 256
116- DESCRIPTOR_ROW_PHASED_THREADS = 128
116+ DESCRIPTOR_ROW_PHASED_THREADS = 1024
117117DESCRIPTOR_INTERNAL_BUFFER_POLICY_SCALAR_LOCAL = "scalar_local"
118118DESCRIPTOR_INTERNAL_BUFFER_POLICY_ROW_LOCAL_HIDDEN = "row_local_hidden"
119119DESCRIPTOR_LOOP_POLICY_FLAT = "flat"
123123DESCRIPTOR_PHYSICAL_ABI_POLICY_BANKED_BY_ROLE = "banked_by_role"
124124DESCRIPTOR_PORTABLE_KERNEL_BUFFER_LIMIT = 31
125125DESCRIPTOR_SHARED_SCRATCH_SPILL_THRESHOLD_BYTES = 1024
126- DESCRIPTOR_SHARED_SCRATCH_BUDGET_BYTES = 12 * 1024
126+ DESCRIPTOR_SHARED_SCRATCH_BUDGET_BYTES = 8 * 1024
127127MAMBA3_BWD_REPLAY_CHECKPOINT_INTERVAL = 8
128128M2RNN_BWD_REPLAY_CHECKPOINT_INTERVAL = 1
129129_DESCRIPTOR_ROOT_READS_MARKER = "cppmega_path_c_root_reads"
@@ -1780,6 +1780,7 @@ def build_path_c_descriptor_prim_func(
17801780 internal_buffers=internal_buffers,
17811781 internal_buffer_shapes=internal_buffer_shapes,
17821782 physical_abi_plan=physical_abi_plan,
1783+ physical_abi_policy=validated_physical_abi_policy,
17831784 internal_buffer_policy=validated_internal_buffer_policy,
17841785 loop_policy=validated_loop_policy,
17851786 max_rows_per_launch=validated_max_rows_per_launch,
@@ -2408,6 +2409,7 @@ def _descriptor_prim_func_source(
24082409 internal_buffers: Sequence[str],
24092410 internal_buffer_shapes: Mapping[str, tuple[int, ...]],
24102411 physical_abi_plan: _PhysicalAbiPlan,
2412+ physical_abi_policy: str,
24112413 internal_buffer_policy: str,
24122414 loop_policy: str,
24132415 max_rows_per_launch: int | None,
@@ -2667,6 +2669,9 @@ def _descriptor_prim_func_source(
26672669 existing_parameter_count=len(param_lines),
26682670 internal_buffer_names=frozenset(active_internal_buffers),
26692671 force_spill_names=frozenset(force_spilled_internal_buffers),
2672+ force_builtin_spill_names=(
2673+ physical_abi_policy != DESCRIPTOR_PHYSICAL_ABI_POLICY_DIRECT
2674+ ),
26702675 )
26712676
26722677
@@ -3861,6 +3866,7 @@ def _spill_large_shared_scratch_to_abi(
38613866 existing_parameter_count: int,
38623867 internal_buffer_names: frozenset[str] = frozenset(),
38633868 force_spill_names: frozenset[str] = frozenset(),
3869+ force_builtin_spill_names: bool = True,
38643870) -> tuple[str, Mapping[str, Mapping[str, Any]]]:
38653871 candidates: list[dict[str, Any]] = []
38663872 total_shared_bytes = 0
@@ -3881,7 +3887,10 @@ def _spill_large_shared_scratch_to_abi(
38813887 force_scratch_abi = (
38823888 scratch_name in force_spill_names
38833889 or _is_row_phased_bwd_scratch_abi_buffer(scratch_name)
3884- or _force_spill_shared_scratch_name(scratch_name)
3890+ or (
3891+ force_builtin_spill_names
3892+ and _force_spill_shared_scratch_name(scratch_name)
3893+ )
38853894 )
38863895 if (
38873896 byte_count >= DESCRIPTOR_SHARED_SCRATCH_SPILL_THRESHOLD_BYTES
@@ -6363,6 +6372,7 @@ def _append_row_phased_mamba3_body(
63636372 f"({out_inner}[{feature}] + ({d_skip} * {x_val})) * "
63646373 f"{z_val} * (1.0 / (1.0 + T.exp(-{z_val})))"
63656374 )
6375+ body.append(f"{indent * 3}T.sync_threads()")
63666376 body.append(f"{indent * 3}if ((row + 1) % {checkpoint_interval}) == 0:")
63676377 body.append(f"{indent * 4}{checkpoint_idx} = (row + 1) // {checkpoint_interval}")
63686378 body.append(
@@ -9447,14 +9457,17 @@ def _append_row_phased_m2rnn_bwd_body(
94479457 )
94489458 body.append(f"{indent * 5}for {state_idx} in T.serial(0, {full_state_extent}):")
94499459 body.append(f"{indent * 6}{dh}[{state_idx}] = 0.0")
9450- body.append(f"{indent * 3}if lane == 0:")
9451- body.append(f"{indent * 4}for {time_rev} in T.serial({time_rev_range}):")
9452- body.append(f"{indent * 6}{time_idx} = {sequence_length - 1} - {time_rev}")
94539460 if hidden_grad is not None:
9454- body.append(f"{indent * 6}for {hidden_loop} in T.serial(0, {hidden_size}):")
94559461 body.append(
9456- f"{indent * 7}{_indexed_buffer_ref(hidden_grad, access_by_buffer, f'{time_idx} * {hidden_size} + {hidden_loop}')} = 0.0"
9462+ f"{indent * 5}for {grad_flat} in T.serial(0, "
9463+ f"{sequence_length * hidden_size}):"
94579464 )
9465+ body.append(
9466+ f"{indent * 6}{_indexed_buffer_ref(hidden_grad, access_by_buffer, grad_flat)} = 0.0"
9467+ )
9468+ body.append(f"{indent * 3}if lane == 0:")
9469+ body.append(f"{indent * 4}for {time_rev} in T.serial({time_rev_range}):")
9470+ body.append(f"{indent * 6}{time_idx} = {sequence_length - 1} - {time_rev}")
94589471 body.append(f"{indent * 6}{checkpoint_idx} = {time_idx} // {checkpoint_interval}")
94599472 body.append(f"{indent * 6}{checkpoint_start} = {checkpoint_idx} * {checkpoint_interval}")
94609473 body.append(f"{indent * 6}for {state_idx} in T.serial(0, {full_state_extent}):")
@@ -9734,10 +9747,7 @@ def _append_row_phased_m2rnn_bwd_body(
97349747 body.append(f"{indent * 6}for {conv_ch} in T.serial(0, {conv_dim}):")
97359748 body.append(f"{indent * 7}{conv_grad}[{conv_ch}] = 0.0")
97369749 body.append(f"{indent * 6}T.sync_threads()")
9737- body.append(
9738- f"{indent * 6}for {proj_dim} in T.serial(lane, {in_proj_dim}, "
9739- f"step={thread_count}):"
9740- )
9750+ body.append(f"{indent * 6}for {proj_dim} in T.serial(0, {in_proj_dim}):")
97419751 body.append(f"{indent * 7}{project_grad}[{proj_dim}] = 0.0")
97429752 body.append(f"{indent * 6}for {feature} in T.serial(0, {features}):")
97439753 body.append(f"{indent * 7}{head} = {feature} // {v_dim}")
@@ -9838,13 +9848,7 @@ def _append_row_phased_m2rnn_bwd_body(
98389848 f"({h_prev}[{head} * {k_dim * v_dim} + {kk} * {v_dim} + {vv_inner}] * "
98399849 f"{state_weight_expr_inner})"
98409850 )
9841- body.append(
9842- f"{indent * 9}{tanh_val}[0] = "
9843- f"({h_next}[{head} * {k_dim * v_dim} + {kk} * {v_dim} + {vv}] - "
9844- f"({decay}[0] * "
9845- f"{h_prev}[{head} * {k_dim * v_dim} + {kk} * {v_dim} + {vv}])) / "
9846- f"(1.0 - {decay}[0])"
9847- )
9851+ body.append(f"{indent * 9}{tanh_val}[0] = T.tanh({accum}[0] + ({k_val} * {v_val}))")
98489852 body.append(
98499853 f"{indent * 9}{scalar0}[0] = {scalar0}[0] + "
98509854 f"({dh}[{head} * {k_dim * v_dim} + {kk} * {v_dim} + {vv}] * "
@@ -10048,10 +10052,7 @@ def _append_row_phased_m2rnn_bwd_body(
1004810052 f"{project_grad}[{conv_ch}] + ({scalar1}[0] * {current_conv_weight_expr})"
1004910053 )
1005010054 body.append(f"{indent * 6}T.sync_threads()")
10051- body.append(
10052- f"{indent * 6}for {proj_dim} in T.serial(lane, {in_proj_dim}, "
10053- f"step={thread_count}):"
10054- )
10055+ body.append(f"{indent * 6}for {proj_dim} in T.serial(0, {in_proj_dim}):")
1005510056 body.append(f"{indent * 7}for {hidden_loop} in T.serial(0, {hidden_size}):")
1005610057 hidden_expr = _node_indexed_canonical_or_positional_input_expr(
1005710058 node,
@@ -10766,7 +10767,6 @@ def _mamba3_emit_recompute_row(
1076610767 )
1076710768 if (
1076810769 hidden_grad is not None
10769- and not launcher_chunked_rows
1077010770 and not _is_full_sequence_bank_slot(
1077110771 hidden_grad,
1077210772 access_by_buffer,
@@ -10819,14 +10819,6 @@ def _mamba3_emit_recompute_row(
1081910819 body.append(f"{indent * 5}T.sync_threads()")
1082010820 body.append(f"{indent * 5}for {time_rev} in T.serial({time_rev_range}):")
1082110821 body.append(f"{indent * 6}{time_idx} = {sequence_length - 1} - {time_rev}")
10822- if launcher_chunked_rows and hidden_grad is not None:
10823- body.append(
10824- f"{indent * 6}for {hidden_loop} in T.serial(lane, {hidden_size}, "
10825- f"step={thread_count}):"
10826- )
10827- body.append(
10828- f"{indent * 7}{_indexed_buffer_ref(hidden_grad, access_by_buffer, f'{time_idx} * {hidden_size} + {hidden_loop}')} = 0.0"
10829- )
1083010822 body.append(f"{indent * 6}{checkpoint_idx} = {time_idx} // {checkpoint_interval}")
1083110823 body.append(
1083210824 f"{indent * 6}{checkpoint_start} = {checkpoint_idx} * {checkpoint_interval}"
@@ -13728,6 +13720,12 @@ def _physical_abi_policy_for_region(
1372813720 external_buffer_count += len(_TRAIN_STEP_SCALAR_OUTPUT_ABI_NAMES)
1372913721 external_buffer_count += len(_TRAIN_STEP_SUFFIX_LOSS_INPUT_ABI_NAMES)
1373013722 external_buffer_count += len(_TRAIN_STEP_SUFFIX_LOSS_PARAMETER_GRAD_ABI_NAMES)
13723+ if (
13724+ shape_env is not None
13725+ and internal_buffer_policy == DESCRIPTOR_INTERNAL_BUFFER_POLICY_ROW_LOCAL_HIDDEN
13726+ and loop_policy == DESCRIPTOR_LOOP_POLICY_ROW_PHASED_HIDDEN
13727+ ):
13728+ return DESCRIPTOR_PHYSICAL_ABI_POLICY_BANKED_BY_ROLE
1373113729 if external_buffer_count > DESCRIPTOR_PORTABLE_KERNEL_BUFFER_LIMIT:
1373213730 return DESCRIPTOR_PHYSICAL_ABI_POLICY_BANKED_BY_ROLE
1373313731 return DESCRIPTOR_PHYSICAL_ABI_POLICY_DIRECT
0 commit comments