Files
leleka/engine/strata/serve/cpu_affinity.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

69 lines
2.6 KiB
Python

"""Split a launcher's allowed physical cores between independent Linux instances."""
from __future__ import annotations
import argparse
import os
from pathlib import Path
import sys
def partition_cpu_sets(count: int, allowed: set[int], reserved: set[int] | None = None,
sys_root: Path = Path("/sys")) -> list[list[int]]:
if count < 1:
raise ValueError("the instance count must be positive")
if not allowed:
raise ValueError("no CPUs are allowed for this launcher")
if count == 1 and not reserved:
return [sorted(allowed)]
cores: dict[tuple[int, int], list[int]] = {}
occupied: set[tuple[int, int]] = set()
for cpu in sorted(allowed | (reserved or set())):
topology = sys_root / "devices/system/cpu" / f"cpu{cpu}" / "topology"
try:
key = (int((topology / "physical_package_id").read_text()),
int((topology / "core_id").read_text()))
except (OSError, ValueError) as error:
raise ValueError(f"cannot read physical-core topology for CPU {cpu}: {error}") from error
if cpu in allowed:
cores.setdefault(key, []).append(cpu)
if reserved and cpu in reserved:
occupied.add(key)
available = sorted((cpus for key, cpus in cores.items() if key not in occupied), key=min)
if len(available) < count:
raise ValueError(
f"{count} instances need at least {count} free physical cores, but only {len(available)} "
"are available; restart the managed instances together or expand the launcher's CPU set"
)
size, extra = divmod(len(available), count)
result, start = [], 0
for index in range(count):
end = start + size + (index < extra)
result.append(sorted(cpu for siblings in available[start:end] for cpu in siblings))
start = end
return result
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("instances", type=int)
parser.add_argument("--exclude-pid", type=int, action="append", default=[])
options = parser.parse_args()
try:
reserved: set[int] = set()
for pid in options.exclude_pid:
try:
reserved.update(os.sched_getaffinity(pid))
except ProcessLookupError:
continue
groups = partition_cpu_sets(options.instances, os.sched_getaffinity(0), reserved)
except (OSError, ValueError) as error:
print(f"CPU affinity: {error}", file=sys.stderr)
return 1
for group in groups:
print(",".join(str(cpu) for cpu in group))
return 0
if __name__ == "__main__":
raise SystemExit(main())