Files
leleka/scripts/gpu-bringup/reseal_own.py
T

61 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Re-seal one of OUR OWN already-sealed PTX assets (prefill_stage, qsa_late, ...) after its .cu changed.
seal_pins.py fills the pins of an unsealed placeholder and refuses an asset whose PTX is already pinned ("expected nowhere"), which is
the right default for the Original's assets. The assets that this repository compiles itself (extra-seal-plan.json, encoding
concat_include_str, a `PtxPin { source_sha256, ptx_sha256 }` in the Rust owner) are re-sealed by this tool: it takes the PTX that
`compile_assets.py --group G --out DIR` produced for the CURRENT .cu, writes it over the committed PTX, and patches the two pin fields
the plan names (`rust_fields_to_fill`) with the SHA-256 of the .cu bytes and of the PTX body. Nothing else changes.
reseal_own.py --ptx-dir DIR ASSET_ID [ASSET_ID ...] # dry run: prints what would change
reseal_own.py --ptx-dir DIR --write ASSET_ID [ASSET_ID ...]
The sm_86 companions and the generated table are rebuilt afterwards with `scripts/gpu-bringup/sm86_assets.py build` (docs/sm86-assets.md).
"""
import argparse
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import bringup_lib as L # noqa: E402
import rustpins as R # noqa: E402
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--ptx-dir", required=True, type=Path)
ap.add_argument("--write", action="store_true")
ap.add_argument("ids", nargs="+")
args = ap.parse_args()
root = L.REPO
plan = json.loads((root / "scripts/gpu-bringup/extra-seal-plan.json").read_text())
by_id = {Path(a["cu"]).stem: a for a in plan["assets"]}
manifest = json.loads((args.ptx_dir / "build-manifest.json").read_text())
for asset_id in args.ids:
a = by_id[asset_id]
rec = next(r for r in manifest["assets"] if r["id"] == asset_id)
if rec["status"] != "ok" or rec.get("fake"):
sys.exit(f"{asset_id}: manifest status {rec['status']}")
raw = (args.ptx_dir / rec["ptx_file"]).read_bytes()
cu = (root / a["cu"]).read_bytes()
if L.sha256_hex(cu) != rec["cu_sha256"]:
sys.exit(f"{asset_id}: the .cu changed after the compile; compile again")
v = R.PinValues(len(raw), L.sha256_digest(raw), L.sha256_digest(cu))
for consumer, specs in a["rust_fields_to_fill"].items():
path = next(root.glob(f"crates/*/src/{consumer}"))
text = path.read_text(encoding="utf-8", newline="")
new = text
for spec in specs:
new = R.apply_field(new, spec, v)
print(f"{asset_id}: {path.relative_to(root)} {'patched' if new != text else 'unchanged'} (ptx {len(raw)} B sha {v.ptx_sha256.hex()[:16]}, cu sha {v.source_sha256.hex()[:16]})")
if args.write:
path.write_text(new, encoding="utf-8", newline="")
if args.write:
(root / a["ptx"]).write_bytes(raw)
print(f"{asset_id}: wrote {a['ptx']}")
if __name__ == "__main__":
main()