Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
27f77c5
fix(gemm): derive MoE tuning shapes from the observed dispatch tuple
Aug 19, 2026
2509f28
fix(gemm): preserve integrate faults and add fused-MoE coverage
Aug 19, 2026
372e076
fix(gemm): retry forge E2E integrate within fault budget
Aug 20, 2026
b438a85
fix(warm-replay): apply recipe patches via nogit when git HEAD is absent
Aug 20, 2026
4264201
fix(breakdown): show forge GEMM e2e gain instead of micro speedup
Aug 20, 2026
9d04cb9
fix(forge-gemm): persist all aiter tuned CSV env keys durably
Aug 20, 2026
353ec09
fix(model-path): resolve serving model path at bootstrap and E2E inte…
Aug 20, 2026
9578316
fix(model-path): wire unified resolver through session executors
Aug 20, 2026
8d88075
fix(nogit-patch): tolerate placeholder git index headers
Aug 20, 2026
0c4866f
Merge origin/main into feature/leixin/forge-fmoe-key-from-log
Aug 20, 2026
a978aee
Merge origin/main into feature/leixin/forge-fmoe-key-from-log
Aug 20, 2026
14bf84a
fix(forge-gemm): stop diagnostics from blocking the run they describe
Aug 20, 2026
2cca948
fix(gemm): name the adopted artifact, and resolve the framework bench…
Aug 20, 2026
c358858
fix(forge-gemm): keep the durable CSV out of aiter's auto-merge scan
Aug 20, 2026
31f6cd3
feat(forge-gemm): tell forge the workload's input sequence length
Aug 20, 2026
660973d
feat(forge-gemm): hand forge the trace shape manifest when one exists
Aug 20, 2026
ab85fc8
Revert "feat(forge-gemm): hand forge the trace shape manifest when on…
Aug 20, 2026
3591e62
Revert "feat(forge-gemm): tell forge the workload's input sequence le…
Aug 20, 2026
2f3e05c
fix(gemm): name the adopted artifact from the candidate, not the stack
Aug 20, 2026
2700241
test(gemm): cover the MoE runtime key handoff
Aug 21, 2026
6be8871
fix(forge-gemm): give every forge wording a verdict, and lift the reason
Aug 21, 2026
1cfc763
fix(forge-gemm): keep the reason lift from breaking the run it describes
Aug 21, 2026
4851c8a
fix(forge-gemm): take tokens as sent, and stop token counts vetoing a…
Aug 21, 2026
7112a92
fix(gemm): neutralise the envelope when E2E validation raises, drop d…
Aug 21, 2026
019b262
style: cut the comments on today's fixes down to the constraint
Aug 21, 2026
0b356ad
fix(warm-replay): refuse a required timeline without a git HEAD, as b…
Aug 21, 2026
e806fca
test(warm-replay): cover the nogit revert the teardown depends on
Aug 21, 2026
95e2e34
Merge origin/main into feature/leixin/forge-fmoe-key-from-log
Aug 21, 2026
9a0292f
fix(test): read _apply_warm_patches' legacy return as the list it is
Aug 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hyperloom/agents/kernel/tools/forge_gemm_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _build_cmd(args: dict[str, Any]) -> list[str]:
_add_opt(cmd, args, "global_timeout", "--global-timeout")
_add_opt(cmd, args, "tuner", "--tuner")
_add_opt(cmd, args, "untuned_csv", "--untuned-csv")
_add_opt(cmd, args, "moe_untuned_csv", "--moe-untuned-csv")
_add_opt(cmd, args, "shapes_json", "--shapes-json")
_add_opt(cmd, args, "tunableop_input", "--tunableop-input")
_add_opt(cmd, args, "kernel_signature_log", "--kernel-signature-log")
Expand Down
59 changes: 59 additions & 0 deletions src/hyperloom/common/model_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

from __future__ import annotations

import os
from pathlib import Path
from typing import Any


def _identity_leaf(seg: str) -> str:
Expand Down Expand Up @@ -128,3 +130,60 @@
if isinstance(hit, str) and Path(hit).is_file():
return Path(hit).parent
return None


def resolve_serving_model_path(raw: str) -> str:
"""Resolve a session model identity to a path suitable for launching servers.

Precedence mirrors ``run_hyperloom.sbatch``: an existing directory wins,
then ``HL_MODEL_BASE/<repo-tail>``, then the HuggingFace hub cache via
:func:`resolve_local_model_dir`. When nothing resolves, the original
string is returned unchanged.
"""
text = str(raw or "").strip()
if not text:
return ""
try:
direct = Path(text).expanduser()
if direct.is_dir():
return str(direct)
except OSError:
pass
base = os.environ.get("HL_MODEL_BASE", "").strip()
if base:
leaf = text.rstrip("/").split("/")[-1]
if leaf:
candidate = Path(base) / leaf
try:
if candidate.is_dir():
return str(candidate)
except OSError:
pass
resolved = resolve_local_model_dir(text)
if resolved is not None:
return str(resolved)
return text


def resolve_session_model_path(
*,
params: dict[str, Any] | None = None,
state_model_path: str = "",
for_serving: bool = False,
) -> str:
"""Unified session model-path precedence for executors and handlers.

Order: ``params['model_path']`` → ``$MODEL_PATH`` → ``state.model_path``.
When ``for_serving`` is true, :func:`resolve_serving_model_path` is applied
to the chosen raw value.
"""
raw = (
str((params or {}).get("model_path") or "").strip()
or os.environ.get("MODEL_PATH", "").strip()
or str(state_model_path or "").strip()
)
if not raw:
return ""
if for_serving:
return resolve_serving_model_path(raw)
return raw
Original file line number Diff line number Diff line change
Expand Up @@ -1297,10 +1297,15 @@ def collect_gemm_tuning(state: dict[str, Any]) -> dict[str, Any]:
if not isinstance(raw, dict):
continue
engine = _resolve_gemm_engine(raw)
e2e_gain_pct = _to_float(raw.get("e2e_gain_pct"))
speedup = _to_float(raw.get("best_speedup"))
gain_pct: float | None = None
tuned_tput: float | None = None
if speedup is not None:
if e2e_gain_pct is not None:
gain_pct = e2e_gain_pct
if baseline_tput is not None:
tuned_tput = baseline_tput * (1.0 + e2e_gain_pct / 100.0)
elif speedup is not None:
gain_pct = (speedup - 1.0) * 100.0
if baseline_tput is not None:
tuned_tput = baseline_tput * speedup
Expand Down Expand Up @@ -1331,6 +1336,7 @@ def collect_gemm_tuning(state: dict[str, Any]) -> dict[str, Any]:
"gpu_type": str(raw.get("gpu_type") or gpu_type),
"baseline_tput": baseline_tput,
"best_speedup": speedup,
"e2e_gain_pct": e2e_gain_pct,
"gain_pct": gain_pct,
"tuned_tput": tuned_tput,
"tuned_file": tuned_file,
Expand Down
4 changes: 3 additions & 1 deletion src/hyperloom/inference_optimizer/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,9 @@ async def _run_optimize(args: argparse.Namespace) -> int:
)
sys.exit(2)
# Re-export so subprocess executors inject the resolved model into the Magpie YAML, not its hardcoded model.
os.environ["MODEL_PATH"] = str(args.model)
from hyperloom.common.model_paths import resolve_serving_model_path

os.environ["MODEL_PATH"] = resolve_serving_model_path(str(args.model)) or str(args.model)

# Quantization prelude (one-shot, before any session/baseline work):
# if --quantize was passed, quantize the source model now and rewrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,112 @@ async def test_does_not_e2e_validate_missing_aiter_candidate(
"candidate_artifact_missing"
)

@pytest.mark.asyncio
async def test_integrate_bench_fault_not_recorded_as_zero_gain_revert(
self, tmp_path, monkeypatch
):
"""A server that never booted is an integrate fault, not a 0% REVERT."""
coord = _coord(tmp_path, baseline_tput=100.0, framework="sglang")
phase = KernelPhase(coord)
fmoe_candidate = tmp_path / "fmoe.csv"
dense_candidate = tmp_path / "dense.csv"
fmoe_candidate.write_text("token,model_dim\n1,2\n", encoding="utf-8")
dense_candidate.write_text("M,N,K\n1,2,3\n", encoding="utf-8")
calls: list[dict] = []

async def _fake_integrate(payload, *, session_dir):
calls.append(payload)
if payload["kernel_id"] == "gemm_tune_fmoe_ck":
return {
"status": "failed",
"error_class": "bench_exception",
"decision": "REVERT",
"error": "re-baseline did not succeed",
}
return {"status": "ok", "decision": "KEEP", "new_tput": 120.0, "gain_pct": 9.09}

monkeypatch.setattr(krh_mod, "integrate_handler", _fake_integrate)
monkeypatch.setattr(explore_mod, "_compute_explore_variant_timeout", lambda **_k: 61)
monkeypatch.setattr(
phase,
"_merge_gemm_candidate_with_runtime",
lambda _env_var, env_value: env_value,
)

result = {
"backend": "forge",
"tuners_run": [
{
"status": "ok",
"tuner": "fmoe_ck",
"improved_shapes": 2,
"env_var": "AITER_CONFIG_FMOE",
"env_value": str(fmoe_candidate),
},
{
"status": "ok",
"tuner": "dense_bf16",
"improved_shapes": 1,
"env_var": "AITER_CONFIG_DENSE",
"env_value": str(dense_candidate),
},
],
}

await phase._validate_gemm_tuning_e2e(result)

assert len(calls) == 3
assert result["e2e_results"]["faults"][0]["reason"] == "integrate_fault:bench_exception"
assert result["e2e_results"]["faults"][0]["fault_attempts"] == 2
assert result["e2e_results"]["reverted"] == []
assert result["e2e_results"]["kept"][0]["tuner"] == "dense_bf16"
assert result["decision"] == "KEEP"

@pytest.mark.asyncio
async def test_integrate_fault_retries_once_before_verdict(self, tmp_path, monkeypatch):
coord = _coord(tmp_path, baseline_tput=100.0, framework="sglang")
phase = KernelPhase(coord)
dense_candidate = tmp_path / "dense.csv"
dense_candidate.write_text("M,N,K\n1,2,3\n", encoding="utf-8")
calls: list[dict] = []

async def _fake_integrate(payload, *, session_dir):
calls.append(payload)
if len(calls) == 1:
return {
"status": "failed",
"error_class": "bench_exception",
"decision": "REVERT",
"error": "re-baseline did not succeed",
}
return {"status": "ok", "decision": "KEEP", "new_tput": 110.0, "gain_pct": 10.0}

monkeypatch.setattr(krh_mod, "integrate_handler", _fake_integrate)
monkeypatch.setattr(
phase,
"_merge_gemm_candidate_with_runtime",
lambda _env_var, env_value: env_value,
)
result = {
"backend": "forge",
"tuners_run": [
{
"status": "ok",
"tuner": "dense_bf16",
"improved_shapes": 1,
"env_var": "AITER_CONFIG_DENSE",
"env_value": str(dense_candidate),
},
],
}

await phase._validate_gemm_tuning_e2e(result)

assert len(calls) == 2
assert result["e2e_results"]["faults"] == []
assert result["e2e_results"]["kept"][0]["tuner"] == "dense_bf16"
assert result["decision"] == "KEEP"

@pytest.mark.asyncio
async def test_a_stopped_run_leaves_its_tuners_unjudged(self, tmp_path, monkeypatch):
"""A clock that ran out is not a verdict on the tuners it interrupted."""
Expand Down Expand Up @@ -2182,7 +2288,7 @@ async def test_handles_no_candidates_without_rewriting_raw_result(self, tmp_path
assert coord.shared_state.optimization_stack == []

@pytest.mark.asyncio
async def test_records_integrate_exception_as_revert(self, tmp_path, monkeypatch):
async def test_records_integrate_exception_as_fault(self, tmp_path, monkeypatch):
coord = _coord(tmp_path, baseline_tput=100.0, framework="sglang")
phase = KernelPhase(coord)
dense_candidate = tmp_path / "dense.csv"
Expand All @@ -2191,7 +2297,13 @@ async def test_records_integrate_exception_as_revert(self, tmp_path, monkeypatch
async def _raise_integrate(*_args, **_kwargs):
raise RuntimeError("integrate failed")

monkeypatch.setattr(krh_mod, "integrate_handler", _raise_integrate)
calls: list[str] = []

async def _counting_raise(*_args, **_kwargs):
calls.append("boom")
raise RuntimeError("integrate failed")

monkeypatch.setattr(krh_mod, "integrate_handler", _counting_raise)
monkeypatch.setattr(
phase,
"_merge_gemm_candidate_with_runtime",
Expand All @@ -2213,9 +2325,15 @@ async def _raise_integrate(*_args, **_kwargs):

await phase._validate_gemm_tuning_e2e(result)

assert result["decision"] == "REVERT"
assert result["micro_decision"] == "candidate_no_e2e_gain"
assert "integrate failed" in result["e2e_results"]["reverted"][0]["reason"]
assert result["status"] == "failed"
assert result["micro_decision"] == "integrate_fault"
assert result["e2e_gain_pct"] is None
fault = result["e2e_results"]["faults"][0]
assert fault["reason"] == "integrate_fault:handler_exception"
assert fault["fault"] is True
assert fault["fault_attempts"] == 2
assert len(calls) == 2
assert result["e2e_results"]["reverted"] == []


class TestBf16DenseFallback:
Expand Down Expand Up @@ -3103,7 +3221,7 @@ async def test_all_revert_resets_and_marks_no_gain(self, tmp_path, monkeypatch):
assert result["requires_e2e_validation"] is False

@pytest.mark.asyncio
async def test_integrate_exception_reverts_tuner(self, tmp_path, monkeypatch):
async def test_integrate_exception_records_fault_not_revert(self, tmp_path, monkeypatch):
coord = _coord(tmp_path, baseline_tput=100.0, framework="sglang")

async def _boom(payload, *, session_dir):
Expand All @@ -3128,10 +3246,13 @@ async def _boom(payload, *, session_dir):
}
await coord._validate_gemm_tuning_e2e(result)

assert result["decision"] == "REVERT"
reverted = result["e2e_results"]["reverted"]
assert len(reverted) == 1
assert reverted[0]["reason"].startswith("RuntimeError")
assert result["status"] == "failed"
assert result["micro_decision"] == "integrate_fault"
assert result["e2e_gain_pct"] is None
faults = result["e2e_results"]["faults"]
assert len(faults) == 1
assert faults[0]["reason"] == "integrate_fault:handler_exception"
assert result["e2e_results"]["reverted"] == []

@pytest.mark.asyncio
async def test_timeout_fallback_when_explore_helper_raises(self, tmp_path, monkeypatch):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,45 @@
assert dst.is_file() # copy committed despite the snapshot failure
assert out["AITER_CONFIG_GEMM_A8W8_BLOCKSCALE"] == str(dst) # repoint SURVIVES
assert snap == "" # snapshot dir empty (it failed), but durability is kept


def test_persist_fmoe_csv_uses_tuned_fmoe_stem(tmp_path, monkeypatch):
aiter_pkg = _fake_aiter(monkeypatch, tmp_path)
ws = tmp_path / "ws"
ws.mkdir()
src = ws / "tuned_fmoe.csv"
src.write_text("cu_num,token,model_dim,inter_dim,quantType\n304,16,4096,512,14\n", encoding="utf-8")

extra = {"AITER_CONFIG_FMOE": str(src)}
out, snap = rh._persist_forge_gemm_csv_durably(
extra, model_path="/models/DeepSeek-V4-Flash", session_dir=ws
)

dst = aiter_pkg / "configs" / "model_configs" / "tuned_fmoe_deepseek-v4-flash.csv"
assert dst.is_file()
assert out["AITER_CONFIG_FMOE"] == str(dst)
assert snap and Path(snap).is_dir()


def test_persist_copies_dense_and_fmoe_together(tmp_path, monkeypatch):
aiter_pkg = _fake_aiter(monkeypatch, tmp_path)
ws = tmp_path / "ws"
ws.mkdir()
dense = ws / "dense.csv"
dense.write_text("gfx,M,N,K,splitK\ngfx950,64,5120,5120,2\n", encoding="utf-8")
fmoe = ws / "fmoe.csv"
fmoe.write_text("cu_num,token\n304,16\n", encoding="utf-8")

extra = {
"AITER_CONFIG_GEMM_A8W8_BLOCKSCALE": str(dense),
"AITER_CONFIG_FMOE": str(fmoe),
}
out, snap = rh._persist_forge_gemm_csv_durably(
extra, model_path="/models/Qwen3-14B-FP8", session_dir=ws
)

assert out["AITER_CONFIG_GEMM_A8W8_BLOCKSCALE"].endswith(
"a8w8_blockscale_tuned_gemm_qwen3-14b-fp8.csv"
)
assert out["AITER_CONFIG_FMOE"].endswith("tuned_fmoe_qwen3-14b-fp8.csv")
assert snap and (Path(snap) / "manifest.json").is_file()
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,29 @@ async def test_sweep_via_geak_requires_existing_bench_script(tmp_path: Path) ->

assert result["status"] == "failed"
assert result["error_class"] == "missing_bench_script"


def test_collect_gemm_tuning_prefers_e2e_gain_over_micro_speedup() -> None:
from hyperloom.inference_optimizer.breakdown.collectors.kernels import collect_gemm_tuning

out = collect_gemm_tuning(
{
"baseline_tput": 1000.0,
"gemm_tuning_attempts": [
{
"engine": "forge",
"status": "complete",
"decision": "KEEP",
"best_speedup": 1.5,
"e2e_gain_pct": 9.26,
"e2e_validated": True,
"tuned_file": "/tmp/tuned.csv",
}
],
}
)

run = out["runs"][0]
assert run["gain_pct"] == pytest.approx(9.26)
assert run["tuned_tput"] == pytest.approx(1092.6)
assert run["best_speedup"] == pytest.approx(1.5)
Loading
Loading