Files
Strata/tools/docker/fetch-engines.py
T
Niko1221 68a6b5a612 ling32: Docker build and fetch know the ling32 shape; the register lists the engine's STRATA_L32_* settings
- Dockerfile.src: BUILD_LING32 (off by default) builds strata-ling32 for the sm_80+ architectures and packages it
  as shapes/ling32; fetch-engines.py fetches the ling32 shape package of a feed (a sixth column), with a test.
- settings register: the 43 settings the Ling engine reads (adaptive tier, prefetch, MTP controller, MLA kernel,
  traces) with status and a note taken from the code comments; the engine-reads-nothing-unregistered test passes
  again on the integration tree.
2026-12-02 12:30:56 +01:00

191 lines
10 KiB
Python

#!/usr/bin/env python3
"""Fetch the engine packages for the Docker images from a release, checked against the signed update feed.
python3 tools/docker/fetch-engines.py --release-tag v0.2.7 --flavors cuda13,cuda12,rocm-gfx120x --out ./pkgs
python3 tools/docker/fetch-engines.py --source /path/to/release-folder --flavors cuda13 --out ./pkgs
python3 tools/docker/fetch-engines.py --release-tag v0.2.7 --list
What it does, in the same order and with the same code as `strata update`:
1. reads strata-feed.json and strata-feed.json.sig from the release (GitHub release assets, or a folder / URL given with --source)
and checks the Ed25519 signature against the public key in terminal/strata_control/update_keys.json;
2. for each flavor picks the linux-x86_64 engine package the feed lists for it (see FLAVORS);
3. downloads the archive and checks its size and SHA-256 against the signed feed (a mismatch deletes the file and stops);
4. unpacks it (the archive's names and links are checked, its size may not exceed what the feed declares), checks that the
manifest is the one the feed names and says what the feed says, then the package signature and every file's hash.
Stdout gets one line per flavor, tab separated: `<flavor> <build.sh --flavor value> <package folder> [<hy4 folder> [<inkling2 folder> [<ling32 folder>]]]` (the folders of the
model-shape engines the feed has for that flavor, an empty column for one it has not; a line stops after the last one it has); everything else goes to stderr. Nothing is installed anywhere and nothing is pushed. Needs only the Python standard library and this repository.
"""
from __future__ import annotations
import argparse
import shutil
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO_ROOT / "terminal"))
from strata_control import engine_component as ec, feed as feedmod, shape_engine, signing, transfer, updater # noqa: E402
#: image flavor (the tag suffix) -> (feed backend, feed variant or None for any, tools/docker/build.sh --flavor)
FLAVORS = {
"cuda13": ("cuda", "cuda13", "cuda"),
"cuda12": ("cuda", "cuda12", "cuda"),
"rocm-gfx120x": ("hip", "rx9070", "hip"), # RX 9000, Radeon AI PRO R9700 (gfx1200 / gfx1201)
"rocm-gfx110x": ("hip", "rx7900", "hip"), # RX 7000 (gfx1100 / gfx1101 / gfx1102)
"rocm-gfx1151": ("hip", "halo", "hip"), # Strix Halo (gfx1151)
"rocm-igpu": ("hip", "igpu", "hip"), # Radeon 780M (gfx1103) & Strix Point 890M/880M (gfx1150)
"sycl": ("sycl", None, "sycl"), # Intel Arc; only when the release has a Linux SYCL package
}
PLATFORM = "linux-x86_64"
def log(message: str) -> None:
print(message, file=sys.stderr, flush=True)
def pick(feed: feedmod.Feed, flavor: str) -> feedmod.EnginePackage | None:
backend, variant, _ = FLAVORS[flavor]
found = [p for p in feed.engine if p.platform == PLATFORM and p.backend == backend and (variant is None or p.variant == variant)]
if not found:
return None
return max(found, key=lambda p: feedmod.version_key(p.version))
def pick_shape(feed: feedmod.Feed, shape: str, flavor: str, main_pkg: feedmod.EnginePackage | None = None) -> feedmod.EnginePackage | None:
backend, variant, _ = FLAVORS[flavor]
pool = [p for p in feed.shape_engines if p.shape == shape and p.platform == PLATFORM and p.backend == backend]
if not pool:
return None
if variant is not None:
exact = [p for p in pool if p.variant == variant]
if exact:
return max(exact, key=lambda p: feedmod.version_key(p.version))
if main_pkg is not None and main_pkg.archs:
want = set(main_pkg.archs)
overlap = [p for p in pool if set(p.archs) & want]
if overlap:
return max(overlap, key=lambda p: feedmod.version_key(p.version))
return None
return max(pool, key=lambda p: feedmod.version_key(p.version))
def fetch_one(feed: feedmod.Feed, pkg: feedmod.EnginePackage, out: Path, trusted: dict, *, subdir: str = "") -> Path:
base = (out / subdir) if subdir else out
base.mkdir(parents=True, exist_ok=True)
folder = base / updater.folder_name(pkg)
archive = out / ".partial" / pkg.url.rsplit("/", 1)[-1]
if folder.is_dir() and (folder / ec.MANIFEST).is_file() and transfer.sha256_file(folder / ec.MANIFEST) == pkg.manifest_sha256:
log(f" {folder.name}: already unpacked, manifest matches the feed")
else:
log(f" downloading {pkg.url} ({pkg.size / (1 << 20):.0f} MiB)")
try:
transfer.download(feed.url(pkg.url), archive, size=pkg.size, sha256=pkg.sha256)
except transfer.TransferError as exc:
raise SystemExit(f"fetch-engines: {exc}")
log(" size and SHA-256 match the signed feed; unpacking")
try:
updater.extract(archive, folder, pkg.format, pkg.installed_bytes)
except updater.UpdateError as exc:
raise SystemExit(f"fetch-engines: {exc}")
finally:
archive.unlink(missing_ok=True)
manifest = folder / ec.MANIFEST
if transfer.sha256_file(manifest) != pkg.manifest_sha256:
raise SystemExit(f"fetch-engines: {folder.name} has a manifest the signed feed does not name")
expected_id = shape_engine.component_id(pkg.shape) if pkg.shape else ec.COMPONENT_ID
try:
component = ec.read_manifest(folder, component_id=expected_id)
except ec.ComponentProblem as exc:
raise SystemExit(f"fetch-engines: {folder.name}: the package manifest is invalid: {exc}")
for name, a, b in (("version", component.version, pkg.version), ("platform", component.platform, pkg.platform),
("backend", component.backend, pkg.backend), ("variant", component.variant, pkg.variant),
("shape", component.shape, pkg.shape),
("build_id", component.build_id, pkg.build_id), ("archs", set(component.archs), set(pkg.archs))):
if a != b:
raise SystemExit(f"fetch-engines: {folder.name}: the manifest says a different {name} than the signed feed")
outer = ec._verifier
signing.install_component_verifier(trusted)
try:
ec.verify_signature(component, {ec.ENV_REQUIRE_SIGNED: "1"})
ec.verify(component, full=True)
except ec.ComponentProblem as exc:
raise SystemExit(f"fetch-engines: {folder.name} failed its check: {exc}")
finally:
ec.register_signature_verifier(outer)
log(f" {folder.name}: signature and every file verified (build {pkg.build_id})")
return folder
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("--release-tag", help="the GitHub release tag, for example v0.2.7")
ap.add_argument("--repo", default="Niko1221/Strata", help="owner/name of the GitHub repository (default %(default)s)")
ap.add_argument("--source", help="a release folder or base URL holding strata-feed.json(.sig) and the archives (instead of --release-tag)")
ap.add_argument("--flavors", default=",".join(FLAVORS), help="comma separated; default: all (%s)" % ", ".join(FLAVORS))
ap.add_argument("--out", type=Path, help="where the unpacked package folders go")
ap.add_argument("--skip-missing", action="store_true", help="a flavor the feed has no package for is skipped (default: an error)")
ap.add_argument("--list", action="store_true", help="only print which package each flavor would use")
a = ap.parse_args()
if bool(a.release_tag) == bool(a.source):
ap.error("give exactly one of --release-tag and --source")
if not a.list and not a.out:
ap.error("--out is required")
flavors = [f.strip() for f in a.flavors.split(",") if f.strip()]
unknown = [f for f in flavors if f not in FLAVORS]
if unknown:
ap.error(f"unknown flavor {', '.join(unknown)} (known: {', '.join(FLAVORS)})")
source = a.source or f"https://github.com/{a.repo}/releases/download/{a.release_tag}"
trusted = signing.load_trusted_keys()
log(f"feed: {source}")
try:
feed = feedmod.fetch(source, trusted)
except feedmod.FeedError as exc:
raise SystemExit(f"fetch-engines: {exc}")
log(f"feed signature OK (key {feed.key_id}), channel {feed.channel}, sequence {feed.sequence}")
status = 0
for flavor in flavors:
pkg = pick(feed, flavor)
if pkg is None:
log(f"{flavor}: the feed has no {PLATFORM} package for it")
if not a.skip_missing:
status = 3
else:
print(f"{flavor}\t{FLAVORS[flavor][2]}\tSKIP")
continue
hy4_pkg = pick_shape(feed, "hy4", flavor, pkg)
ink_pkg = pick_shape(feed, "inkling2", flavor, pkg)
ling_pkg = pick_shape(feed, "ling32", flavor, pkg)
log(f"{flavor}: {pkg.label} ({pkg.url})")
if hy4_pkg is not None:
log(f"{flavor} [hy4]: {hy4_pkg.label} ({hy4_pkg.url})")
if ink_pkg is not None:
log(f"{flavor} [inkling2]: {ink_pkg.label} ({ink_pkg.url})")
if ling_pkg is not None:
log(f"{flavor} [ling32]: {ling_pkg.label} ({ling_pkg.url})")
if a.list:
print(f"{flavor}\t{FLAVORS[flavor][2]}\t{pkg.url}\t{pkg.size}\t{pkg.sha256}")
continue
a.out.mkdir(parents=True, exist_ok=True)
folder = fetch_one(feed, pkg, a.out, trusted)
hy4_folder = fetch_one(feed, hy4_pkg, a.out, trusted, subdir="shapes/hy4") if hy4_pkg is not None else ""
ink_folder = fetch_one(feed, ink_pkg, a.out, trusted, subdir="shapes/inkling2") if ink_pkg is not None else ""
ling_folder = fetch_one(feed, ling_pkg, a.out, trusted, subdir="shapes/ling32") if ling_pkg is not None else ""
cols = [flavor, FLAVORS[flavor][2], str(folder)]
if hy4_folder or ink_folder or ling_folder:
cols.append(str(hy4_folder))
if ink_folder or ling_folder:
cols.append(str(ink_folder))
if ling_folder:
cols.append(str(ling_folder))
print("\t".join(cols), flush=True)
if a.out and (a.out / ".partial").is_dir():
shutil.rmtree(a.out / ".partial", ignore_errors=True)
return status
if __name__ == "__main__":
sys.exit(main())