Skip to content

Commit 54161c3

Browse files
committed
refactor: replace boolean Path C fusion gate with a detailed audit dataclass and add matrix profile receipt validation logic
1 parent 9261f75 commit 54161c3

4 files changed

Lines changed: 375 additions & 26 deletions

File tree

cppmega_mlx/runtime/path_c_fusion.py

Lines changed: 142 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
"PathCFusionRegionBuilder",
4040
"PathCModelBrick",
4141
"PathCModelShapeEnv",
42+
"PathCPlanDefaultEligibilityAudit",
4243
"SparseMLAFp8PrepareSpec",
4344
"WarmCacheAudit",
4445
"Z3SyncSpec",
4546
"audit_fusion_cache_key",
47+
"audit_fused_path_c_plan_default_eligibility",
4648
"audit_warm_cache_reuse",
4749
"build_path_c_aot_autograd_region",
4850
"build_path_c_fusion_region",
@@ -2966,6 +2968,16 @@ def c_over_b(self) -> float:
29662968
return self.path_c_warm_tok_sec / self.path_b_tok_sec
29672969

29682970

2971+
@dataclass(frozen=True)
2972+
class PathCPlanDefaultEligibilityAudit:
2973+
"""Structured result for the fused Path C default-eligibility gate."""
2974+
2975+
eligible: bool
2976+
status: str
2977+
reason: str
2978+
schedule_id: str = ""
2979+
2980+
29692981
def path_b_baseline_is_clean(
29702982
row: BenchmarkAcceptanceRow,
29712983
*,
@@ -2995,34 +3007,149 @@ def fused_path_c_default_eligible(
29953007
return row.path_c_peak_delta_gib <= max_peak_delta_gib
29963008

29973009

2998-
def fused_path_c_plan_default_eligible(
3010+
def audit_fused_path_c_plan_default_eligibility(
29993011
plan: FusionCompilePlan,
30003012
row: BenchmarkAcceptanceRow,
30013013
*,
30023014
min_c_over_b: float = DEFAULT_MIN_C_OVER_B,
30033015
max_peak_delta_gib: float = DEFAULT_MAX_PEAK_DELTA_GIB,
3004-
) -> bool:
3005-
"""Gate default Path C fusion on both the plan and clean benchmark row."""
3016+
trusted_schedule_ids: Sequence[str] | None = None,
3017+
) -> PathCPlanDefaultEligibilityAudit:
3018+
"""Explain whether a fused Path C plan can be enabled by default."""
30063019

30073020
if not plan.single_kernel_fused:
3008-
return False
3021+
return PathCPlanDefaultEligibilityAudit(
3022+
eligible=False,
3023+
status="not_single_kernel_fused",
3024+
reason="plan did not lower to one fused kernel",
3025+
)
30093026
if plan.schedule_status != "ready":
3010-
return False
3027+
return PathCPlanDefaultEligibilityAudit(
3028+
eligible=False,
3029+
status="schedule_not_ready",
3030+
reason=f"schedule status is {plan.schedule_status!r}, not 'ready'",
3031+
)
30113032
if plan.autograd_status != "ready" or plan.autograd_missing_backward_nodes:
3012-
return False
3033+
return PathCPlanDefaultEligibilityAudit(
3034+
eligible=False,
3035+
status="autograd_not_ready",
3036+
reason="AOT autograd backward coverage is not complete",
3037+
)
30133038
if plan.semantic_blockers:
3014-
return False
3015-
if plan.schedule_contract is None or plan.schedule_contract.status != "verified":
3016-
return False
3017-
if not plan.schedule_contract.declared_required_real_abi_inputs:
3018-
return False
3019-
if plan.schedule_contract.missing_real_abi_inputs:
3020-
return False
3021-
return fused_path_c_default_eligible(
3039+
return PathCPlanDefaultEligibilityAudit(
3040+
eligible=False,
3041+
status="semantic_blockers",
3042+
reason="semantic fusion blockers remain",
3043+
)
3044+
if plan.schedule_contract is None:
3045+
return PathCPlanDefaultEligibilityAudit(
3046+
eligible=False,
3047+
status="missing_schedule_contract",
3048+
reason="plan has no verified schedule contract",
3049+
)
3050+
contract = plan.schedule_contract
3051+
if contract.status != "verified":
3052+
return PathCPlanDefaultEligibilityAudit(
3053+
eligible=False,
3054+
status="unverified_schedule_contract",
3055+
reason=contract.reason,
3056+
schedule_id=contract.declared_schedule_id,
3057+
)
3058+
if contract.declared_implementation_kind != "production":
3059+
return PathCPlanDefaultEligibilityAudit(
3060+
eligible=False,
3061+
status="non_production_schedule",
3062+
reason=(
3063+
"declared implementation kind is "
3064+
f"{contract.declared_implementation_kind!r}, not 'production'"
3065+
),
3066+
schedule_id=contract.declared_schedule_id,
3067+
)
3068+
if not contract.declared_schedule_id:
3069+
return PathCPlanDefaultEligibilityAudit(
3070+
eligible=False,
3071+
status="missing_schedule_id",
3072+
reason="verified production contract did not declare a schedule ID",
3073+
)
3074+
trusted_ids = (
3075+
trusted_path_c_production_schedule_ids()
3076+
if trusted_schedule_ids is None
3077+
else frozenset(str(schedule_id) for schedule_id in trusted_schedule_ids)
3078+
)
3079+
if contract.declared_schedule_id not in trusted_ids:
3080+
return PathCPlanDefaultEligibilityAudit(
3081+
eligible=False,
3082+
status="untrusted_schedule_id",
3083+
reason=(
3084+
f"production schedule ID {contract.declared_schedule_id!r} "
3085+
"is not trusted by this build"
3086+
),
3087+
schedule_id=contract.declared_schedule_id,
3088+
)
3089+
if not contract.declared_required_real_abi_inputs:
3090+
return PathCPlanDefaultEligibilityAudit(
3091+
eligible=False,
3092+
status="undeclared_real_abi",
3093+
reason="verified production contract declared no real ABI inputs",
3094+
schedule_id=contract.declared_schedule_id,
3095+
)
3096+
if contract.missing_real_abi_inputs:
3097+
missing = ", ".join(contract.missing_real_abi_inputs)
3098+
return PathCPlanDefaultEligibilityAudit(
3099+
eligible=False,
3100+
status="incomplete_real_abi",
3101+
reason=f"verified production contract is missing real ABI inputs: {missing}",
3102+
schedule_id=contract.declared_schedule_id,
3103+
)
3104+
if not path_b_baseline_is_clean(row):
3105+
return PathCPlanDefaultEligibilityAudit(
3106+
eligible=False,
3107+
status="unclean_path_b_baseline",
3108+
reason="Path B baseline row is not clean enough for default gating",
3109+
schedule_id=contract.declared_schedule_id,
3110+
)
3111+
if row.c_over_b < min_c_over_b:
3112+
return PathCPlanDefaultEligibilityAudit(
3113+
eligible=False,
3114+
status="insufficient_path_c_speedup",
3115+
reason=f"Path C/B ratio {row.c_over_b:.6g} is below {min_c_over_b:.6g}",
3116+
schedule_id=contract.declared_schedule_id,
3117+
)
3118+
if row.path_c_peak_delta_gib > max_peak_delta_gib:
3119+
return PathCPlanDefaultEligibilityAudit(
3120+
eligible=False,
3121+
status="path_c_peak_delta_too_high",
3122+
reason=(
3123+
f"Path C peak delta {row.path_c_peak_delta_gib:.6g} GiB "
3124+
f"exceeds {max_peak_delta_gib:.6g} GiB"
3125+
),
3126+
schedule_id=contract.declared_schedule_id,
3127+
)
3128+
return PathCPlanDefaultEligibilityAudit(
3129+
eligible=True,
3130+
status="eligible",
3131+
reason="plan and benchmark row satisfy fused Path C default gating",
3132+
schedule_id=contract.declared_schedule_id,
3133+
)
3134+
3135+
3136+
def fused_path_c_plan_default_eligible(
3137+
plan: FusionCompilePlan,
3138+
row: BenchmarkAcceptanceRow,
3139+
*,
3140+
min_c_over_b: float = DEFAULT_MIN_C_OVER_B,
3141+
max_peak_delta_gib: float = DEFAULT_MAX_PEAK_DELTA_GIB,
3142+
trusted_schedule_ids: Sequence[str] | None = None,
3143+
) -> bool:
3144+
"""Gate default Path C fusion on both the plan and clean benchmark row."""
3145+
3146+
return audit_fused_path_c_plan_default_eligibility(
3147+
plan,
30223148
row,
30233149
min_c_over_b=min_c_over_b,
30243150
max_peak_delta_gib=max_peak_delta_gib,
3025-
)
3151+
trusted_schedule_ids=trusted_schedule_ids,
3152+
).eligible
30263153

30273154

30283155
@dataclass(frozen=True)

scripts/m04_train_step.py

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@
228228
PATH_C_FUSION_COMPILE_RECEIPT_PATH = (
229229
ROOT / "reports" / "path_c_fusion_compile_receipt.json"
230230
)
231+
PATH_C_FUSION_MATRIX_PROFILE_RECEIPT_ENV = (
232+
"CPPMEGA_PATH_C_FUSION_MATRIX_PROFILE_RECEIPT"
233+
)
234+
PATH_C_FUSION_MATRIX_PROFILE_RECEIPT_PATH = (
235+
ROOT / "reports" / "path_c_fusion_matrix_profile_receipt.json"
236+
)
231237
FP8_PATH_C_BRIDGE_TARGET = "native_mlx_tvm_ffi_graph_bridge"
232238
FP8_PATH_C_BRIDGE_STATUS = "m04_wired_for_native_tvm_ffi_graph_outputs"
233239
FP8_PATH_C_CARRIER_DTYPE = "bfloat16"
@@ -5767,10 +5773,114 @@ def _path_c_fusion_compile_receipt_payload(
57675773
}
57685774

57695775

5776+
def _path_c_fusion_matrix_profile_receipt_path() -> Path:
5777+
override = os.environ.get(PATH_C_FUSION_MATRIX_PROFILE_RECEIPT_ENV)
5778+
if override:
5779+
return Path(override).expanduser()
5780+
return PATH_C_FUSION_MATRIX_PROFILE_RECEIPT_PATH
5781+
5782+
5783+
def _path_c_fusion_matrix_profile_payload(
5784+
*,
5785+
schedule_spec: Any,
5786+
receipt_path: str | Path | None = None,
5787+
) -> dict[str, Any]:
5788+
"""Validate 1B matrix/profile/cache evidence for a production schedule."""
5789+
5790+
receipt_path = (
5791+
Path(receipt_path).expanduser()
5792+
if receipt_path is not None
5793+
else _path_c_fusion_matrix_profile_receipt_path()
5794+
)
5795+
expected_schedule_id = str(schedule_spec.schedule_id)
5796+
expected_schedule_name = str(schedule_spec.schedule_name)
5797+
base: dict[str, Any] = {
5798+
"path": str(receipt_path),
5799+
"status": "missing",
5800+
"verified": False,
5801+
"schedule_id": None,
5802+
"schedule_name": None,
5803+
"failed_checks": [],
5804+
"reason": "production fused schedule 1B matrix/profile receipt is missing",
5805+
}
5806+
if not receipt_path.exists():
5807+
return base
5808+
try:
5809+
receipt = json.loads(receipt_path.read_text(encoding="utf-8"))
5810+
except (OSError, json.JSONDecodeError) as exc:
5811+
return {
5812+
**base,
5813+
"status": "unreadable",
5814+
"reason": f"{type(exc).__name__}: {exc}",
5815+
}
5816+
5817+
matrix_rows = receipt.get("matrix_rows", ())
5818+
checks = {
5819+
"receipt_kind_ok": (
5820+
receipt.get("kind")
5821+
== "cppmega_path_c_fusion_matrix_profile_receipt"
5822+
),
5823+
"receipt_status_ok": receipt.get("status") == "ok",
5824+
"model_profile_matches": (
5825+
receipt.get("model_profile") == REQUIRED_MODEL_PROFILE
5826+
),
5827+
"schedule_id_matches": (
5828+
str(receipt.get("schedule_id", "")) == expected_schedule_id
5829+
),
5830+
"schedule_name_matches": (
5831+
str(receipt.get("schedule_name", "")) == expected_schedule_name
5832+
),
5833+
"full_1b_matrix_captured": (
5834+
receipt.get("full_1b_matrix_captured") is True
5835+
),
5836+
"profiling_traces_captured": (
5837+
receipt.get("profiling_traces_captured") is True
5838+
),
5839+
"memory_non_regression_ok": (
5840+
receipt.get("memory_non_regression_ok") is True
5841+
),
5842+
"cache_receipts_captured": (
5843+
receipt.get("cache_receipts_captured") is True
5844+
),
5845+
"path_b_baselines_clean": (
5846+
receipt.get("path_b_baselines_clean") is True
5847+
),
5848+
"path_c_default_gate_rows_passed": (
5849+
receipt.get("path_c_default_gate_rows_passed") is True
5850+
),
5851+
"path_c_peak_memory_non_regression": (
5852+
receipt.get("path_c_peak_memory_non_regression") is True
5853+
),
5854+
"path_c_warm_cache_hit_observed": (
5855+
receipt.get("path_c_warm_cache_hit_observed") is True
5856+
),
5857+
"matrix_rows_present": isinstance(matrix_rows, list) and bool(matrix_rows),
5858+
}
5859+
failed_checks = [name for name, ok in checks.items() if not ok]
5860+
verified = not failed_checks
5861+
return {
5862+
**base,
5863+
"status": "verified" if verified else "mismatch",
5864+
"verified": verified,
5865+
"schedule_id": receipt.get("schedule_id"),
5866+
"schedule_name": receipt.get("schedule_name"),
5867+
"checks": checks,
5868+
"failed_checks": failed_checks,
5869+
"matrix_row_count": len(matrix_rows) if isinstance(matrix_rows, list) else 0,
5870+
"reason": (
5871+
"production fused schedule has matching full 1B matrix, profiling, "
5872+
"memory non-regression, and cache receipt evidence"
5873+
if verified
5874+
else "production fused schedule matrix/profile receipt does not match this route"
5875+
),
5876+
}
5877+
5878+
57705879
def path_c_fusion_payload(
57715880
*,
57725881
model: Any | None = None,
57735882
compile_receipt_path: str | Path | None = None,
5883+
matrix_profile_receipt_path: str | Path | None = None,
57745884
bank_buffers: Mapping[str, Any] | None = None,
57755885
bank_buffer_owner: str | None = None,
57765886
bank_owner: Any | None = None,
@@ -5825,6 +5935,13 @@ def path_c_fusion_payload(
58255935
receipt_path=compile_receipt_path,
58265936
)
58275937
production_compile_verified = bool(compile_receipt.get("verified"))
5938+
matrix_profile_receipt = _path_c_fusion_matrix_profile_payload(
5939+
schedule_spec=schedule_spec,
5940+
receipt_path=matrix_profile_receipt_path,
5941+
)
5942+
production_matrix_profile_verified = bool(
5943+
matrix_profile_receipt.get("verified")
5944+
)
58285945
expected_bank_buffer_owner = (
58295946
bank_buffer_owner
58305947
if bank_buffer_owner is not None
@@ -6109,6 +6226,7 @@ def path_c_fusion_payload(
61096226
fused_train_block_training_contract
61106227
),
61116228
"production_compile_receipt": compile_receipt,
6229+
"production_matrix_profile_receipt": matrix_profile_receipt,
61126230
"direct_chained_fusion": {
61136231
**_path_c_direct_chain_plan_payload(direct_chain),
61146232
"construction": direct_chain_construction,
@@ -6264,16 +6382,31 @@ def path_c_fusion_payload(
62646382
if not runtime_fused_route_bound
62656383
else []
62666384
),
6267-
{
6268-
"kind": "production_1b_matrix_profile_missing",
6269-
"schedule_id": schedule_spec.schedule_id,
6270-
"schedule_name": schedule_spec.schedule_name,
6271-
"reason": (
6272-
"the full 1B Path B/Path C matrix, profiling traces, memory "
6273-
"non-regression evidence, and cache receipts have not been "
6274-
"captured for this production schedule"
6275-
),
6276-
},
6385+
*(
6386+
[
6387+
{
6388+
"kind": "production_1b_matrix_profile_missing",
6389+
"schedule_id": schedule_spec.schedule_id,
6390+
"schedule_name": schedule_spec.schedule_name,
6391+
"matrix_profile_receipt_status": (
6392+
matrix_profile_receipt.get("status")
6393+
),
6394+
"matrix_profile_receipt_path": (
6395+
matrix_profile_receipt.get("path")
6396+
),
6397+
"failed_checks": list(
6398+
matrix_profile_receipt.get("failed_checks", ())
6399+
),
6400+
"reason": (
6401+
"the full 1B Path B/Path C matrix, profiling traces, "
6402+
"memory non-regression evidence, and cache receipts "
6403+
"have not been captured for this production schedule"
6404+
),
6405+
}
6406+
]
6407+
if not production_matrix_profile_verified
6408+
else []
6409+
),
62776410
*(
62786411
[
62796412
{
@@ -6352,6 +6485,9 @@ def path_c_fusion_payload(
63526485
"requires_ready_fusion_plan": True,
63536486
"requires_compile_verified_single_kernel": True,
63546487
"current_compile_receipt_verified": production_compile_verified,
6488+
"current_matrix_profile_verified": (
6489+
production_matrix_profile_verified
6490+
),
63556491
"requires_verified_schedule_contract": True,
63566492
"requires_complete_real_abi_contract": True,
63576493
"current_plan_default_eligible": False,

0 commit comments

Comments
 (0)