Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
5.0 KiB
Python
109 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""A/A guard for the spd_ab_job.sh arms (2026-11-11).
|
|
|
|
Since pkg-s14 the engine_env of the built-in profile overrides the shell env of the engine, so an A/B whose
|
|
variable key is also in the profile compares the same engine twice. Two modes:
|
|
|
|
make < profile.json > arm-profile.json
|
|
Drop every guarded switch key from the profile's engine_env and set the ones this arm exports (the arm's
|
|
shell env, LELEKA_* keys). Use with `leleka up --profile FILE` (SPD_ARM_PROFILE=1).
|
|
env UP_FOREGROUND (at ready: the engine log has no switch line yet, it appears with the first request)
|
|
check ENGINE_LOG UP_FOREGROUND (after the arm has run)
|
|
Compare what the engine really got with what this arm exports; exit 1 on any mismatch:
|
|
1. the "requested Sm120Switches" line of the engine log: an exported switch must be true, an unexported one
|
|
false (unless it is in SPD_SWITCH_DEFAULT_ON, comma separated field names);
|
|
2. the "engine environment" line of `leleka up`: a guarded key there must equal this arm's export; one the
|
|
arm does not export is an override from the profile (unless in SPD_PROFILE_KEYS_OK, comma separated; EXPERT_PER_LAYER is always allowed).
|
|
"""
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
SM120 = { # env key suffix -> field of the "requested Sm120Switches" struct
|
|
"ATTN_DAILY": "attn_daily", "GU_SWIGLU": "gu_swiglu", "SIDE_MIXER": "side_mixer", "SIDE_QSA": "side_qsa",
|
|
"SIDE_SHARED": "side_shared", "ACCEPT_ON_DEVICE": "accept_on_device", "FUSED_QSA_PRELUDE": "fused_qsa_prelude",
|
|
"FUSED_GDN": "fused_gdn", "GDN_AB_UNROLL": "gdn_ab_unroll", "COMBINE_VEC4": "combine_vec4",
|
|
"HEAD_MIX_MULTI": "head_mix_multi", "GR_DOWN_FUSED": "gr_down_fused", "DOWN_SUB16": "down_sub16",
|
|
"HITS_IN_COMBINE": "hits_in_combine", "WAIT_M_ROWS": "wait_m_rows",
|
|
}
|
|
GUARDED = re.compile(
|
|
r"^(LELEKA_SM120_(?!ATTN_DAILY_MTP$)|LELEKA_DRAFT_FUSED_COMMIT$|LELEKA_PROMPT_EMBED_ALIAS$|LELEKA_ADAPT_|"
|
|
r"LELEKA_ORIGINAL_SERVE_(PCIE_|ADAPT_|DECODE_RESERVE_MIB$|VRAM_RESERVE_MIB$|EXPERT_PER_LAYER$)|LELEKA_SPEC(_|$))")
|
|
TRUE = {"1", "on", "true", "yes"}
|
|
|
|
|
|
def arm_env():
|
|
return {k: v for k, v in os.environ.items() if GUARDED.match(k)}
|
|
|
|
|
|
def make():
|
|
profile = json.load(sys.stdin)
|
|
env = profile["engine_env"]
|
|
for key in [k for k in env if GUARDED.match(k)]:
|
|
env.pop(key)
|
|
env.update(arm_env())
|
|
json.dump(profile, sys.stdout, indent=1)
|
|
|
|
|
|
def requested_fields(engine_log):
|
|
for line in open(engine_log, errors="replace"):
|
|
if "requested Sm120Switches" in line:
|
|
return dict(re.findall(r"\b([a-z0-9_]+): (true|false)\b", line.split("requested Sm120Switches", 1)[1]))
|
|
return None
|
|
|
|
|
|
def check_switches(engine_log, errors):
|
|
fields = requested_fields(engine_log)
|
|
if fields is None:
|
|
errors.append(f"{engine_log}: no 'requested Sm120Switches' line")
|
|
return
|
|
allowed_on = set(filter(None, os.environ.get("SPD_SWITCH_DEFAULT_ON", "").split(",")))
|
|
env = arm_env()
|
|
for suffix, field in SM120.items():
|
|
want = env.get("LELEKA_SM120_" + suffix, "").lower() in TRUE
|
|
got = fields.get(field) == "true"
|
|
if want and not got:
|
|
errors.append(f"{field}: exported but the engine did not get it")
|
|
if got and not want and field not in allowed_on:
|
|
errors.append(f"{field}: the engine has it ON but this arm does not export it (profile override?)")
|
|
|
|
|
|
def check_environment(up_foreground, errors):
|
|
line = next((l for l in open(up_foreground, errors="replace") if "engine environment:" in l), "")
|
|
got = dict(kv.split("=", 1) for kv in line.split("engine environment:", 1)[-1].split() if "=" in kv)
|
|
env = arm_env()
|
|
allowed = {"LELEKA_ORIGINAL_SERVE_EXPERT_PER_LAYER"} # constant in every arm of the 5090 profile (placement, not a variable)
|
|
allowed |= set(filter(None, os.environ.get("SPD_PROFILE_KEYS_OK", "").split(",")))
|
|
for key, value in got.items():
|
|
if not GUARDED.match(key):
|
|
continue
|
|
if key not in env:
|
|
if key in allowed:
|
|
continue
|
|
errors.append(f"{key}={value} comes from the profile, this arm does not export it")
|
|
elif env[key] != value:
|
|
errors.append(f"{key}: arm exports {env[key]}, the profile engine_env has {value}")
|
|
|
|
|
|
def check(engine_log, up_foreground):
|
|
errors = []
|
|
if engine_log:
|
|
check_switches(engine_log, errors)
|
|
check_environment(up_foreground, errors)
|
|
for e in errors:
|
|
print("A/A GUARD:", e)
|
|
print("A/A guard: " + ("FAILED" if errors else "ok"), f"({len(arm_env())} guarded keys exported)")
|
|
sys.exit(1 if errors else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if sys.argv[1:2] == ["make"]:
|
|
make()
|
|
elif sys.argv[1:2] == ["check"] and len(sys.argv) == 4:
|
|
check(sys.argv[2], sys.argv[3])
|
|
elif sys.argv[1:2] == ["env"] and len(sys.argv) == 3:
|
|
check(None, sys.argv[2])
|
|
else:
|
|
sys.exit(__doc__)
|