Files
OBLITERATUS/scripts/build_rank_local_reference.py
Joseph Magly 54a190e78a fix(distributed): bind E4 greedy steps to the replayed count and tighten admission
The E4 receipt accepted greedy_steps 1 to 8 and the builder used the
draft's value, but the worker always replays three greedy steps and
compares them exactly, so any other value passed admission and was
guaranteed to fail after the physical run. Share REFERENCE_GREEDY_STEPS = 3
between admission, the reference builder and the worker, and refuse any
other value before building.

Also compare the live PROBE config and tokenization digests with the job in
the E4 worker, record a workload/job mismatch in the Gloo agreement so both
ranks refuse together, chain the cause of a local admission failure, and
require a plain int workload.
2026-11-27 13:51:14 -05:00

81 lines
4.0 KiB
Python

"""Build the bounded LMS-E4 streaming reference offline from an immutable local source.
Run once on one trusted host. Then copy ``reference.safetensors`` and its receipt
into each rank's local job directory; the job binds the reference digest. The
script never downloads, launches ranks or approves anything.
"""
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path
def build(source: Path, draft: dict, output: Path, *, host_budget_bytes: int, device: str) -> dict:
import torch
from safetensors.torch import save_file
from transformers import PreTrainedTokenizerFast
from obliteratus.distributed.model import model_config_digest
from obliteratus.distributed.model_manifest import read_bounded_json
from obliteratus.distributed.preflight import inspect_source
from obliteratus.distributed.probe_contracts import prepare_probe_corpus
from obliteratus.distributed.rank_local_e4 import (
REFERENCE_GREEDY_STEPS, StreamingLlamaReference, e4_samples, e4_stage_configs, reference_receipt,
)
inputs = draft.get("reference_inputs")
if not isinstance(inputs, dict) or set(inputs) != {"tokens", "greedy_steps"}:
raise ValueError("draft requires exactly reference_inputs.tokens and greedy_steps")
steps = inputs["greedy_steps"]
if type(steps) is not int or steps != REFERENCE_GREEDY_STEPS:
# The worker replays exactly this count; any other reference fails after the paid run.
raise ValueError(f"reference_inputs.greedy_steps must be {REFERENCE_GREEDY_STEPS}")
probe_config = e4_stage_configs(draft)[0]
harmful, harmless, _verification, _refusal = e4_samples(draft)
identity = inspect_source(source, timeout_seconds=3600)
tokenizer = PreTrainedTokenizerFast.from_pretrained(str(source), local_files_only=True)
corpus = prepare_probe_corpus(tokenizer, harmful, harmless, source_tokenizer_digest=identity.tokenizer_digest,
config=probe_config)
builder = StreamingLlamaReference(source, host_budget_bytes=host_budget_bytes, device=device)
tensors = builder.build(
torch.tensor(inputs["tokens"], dtype=torch.int64), greedy_steps=REFERENCE_GREEDY_STEPS,
probe_ids=torch.tensor(corpus.input_ids, dtype=torch.int64),
probe_mask=torch.tensor(corpus.attention_mask, dtype=torch.int64),
harmful_count=len(harmful), harmless_count=len(harmless), probe_config=probe_config,
)
if output.exists():
raise FileExistsError("reference output already exists; references are never overwritten")
save_file(tensors, str(output))
output.chmod(0o444)
return reference_receipt(
output, source_digest=identity.source_digest,
config_digest=model_config_digest(read_bounded_json(source / "config.json")),
builder=builder, greedy_steps=REFERENCE_GREEDY_STEPS, probe_config_digest=probe_config.digest,
probe_tokenization_digest=corpus.tokenization_digest,
)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--source", type=Path, required=True)
parser.add_argument("--draft", type=Path, required=True, help="job draft with stage_configs, samples and reference_inputs")
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--receipt", type=Path, required=True)
parser.add_argument("--host-budget-bytes", type=int, required=True)
parser.add_argument("--device", choices=("cpu", "cuda"), default="cpu")
args = parser.parse_args()
draft = json.loads(args.draft.read_text(encoding="utf-8"))
receipt = build(args.source, draft, args.output, host_budget_bytes=args.host_budget_bytes, device=args.device)
descriptor = os.open(args.receipt, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o444)
with os.fdopen(descriptor, "w", encoding="utf-8") as stream:
json.dump(receipt, stream, sort_keys=True, separators=(",", ":"))
print(json.dumps(receipt, sort_keys=True))
return 0
if __name__ == "__main__":
raise SystemExit(main())