mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
- Render model.dtype and quantization.logits_dtype from the base checkpoint's unanimous float weight dtype (safetensors headers), with a validated --dtype override; run, validate, and assemble refuse a config whose dtype disagrees with the base. A bf16 base renders exactly as before. - Preserve original permission bits across atomic prepare, backups, and rollback; new files honor the umask. - Download the e2e snapshot in a GSQ-interpreter subprocess so no HF client sockets outlive the call inside pytest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
460 lines
18 KiB
Python
460 lines
18 KiB
Python
"""Conditional end-to-end portable GSQ workflow on a small public model.
|
|
|
|
Runs only when ``OBLITERATUS_GSQ_E2E=1`` and ``OBLITERATUS_GSQ_CHECKOUT``
|
|
point at a GSQ checkout (prepared in place), on a CUDA runner. It
|
|
snapshot-downloads ``Qwen/Qwen3.5-0.8B`` at the pinned revision, quantizes
|
|
the local snapshot path with tiny calibration sizes, asserts the launch
|
|
binding (preserved pre-launch config SHA-256 plus base-model hashes
|
|
with exit status, required by the ``--gsq-config`` validation),
|
|
validates full
|
|
186-projection coverage, assembles a loadable checkpoint, certifies the
|
|
assembled metadata against the assembled tensors, and reloads the
|
|
assembled output in a fresh process -- with the full multimodal class
|
|
when the config carries a ``vision_config`` -- to check clean loading
|
|
(no missing/unexpected keys) and finite logits.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.gsq import coverage as gsq_coverage
|
|
from obliteratus.gsq import source, workflow
|
|
|
|
|
|
# Only the end-to-end test needs CUDA/network; the prerequisite-decision
|
|
# tests below are CPU-only and run in the default gate.
|
|
|
|
MODEL = workflow.DEFAULT_E2E_MODEL
|
|
MODEL_REVISION = workflow.DEFAULT_E2E_MODEL_REVISION
|
|
EXPECTED_PROJECTIONS = 186
|
|
|
|
|
|
def check_e2e_prerequisites(env=None, run=None):
|
|
"""Resolve the e2e gate: skip when not opted in, fail when opted in.
|
|
|
|
Skips only when ``OBLITERATUS_GSQ_E2E`` is not ``"1"``. Once opted
|
|
in, every prerequisite failure -- a missing
|
|
``OBLITERATUS_GSQ_CHECKOUT``, a missing checkout, a
|
|
missing/unrunnable interpreter, a failing torch import, or a failing
|
|
CUDA probe in the GSQ interpreter -- fails (with the probe's
|
|
stdout/stderr) instead of skipping, so a misconfigured opt-in can
|
|
never pass without exercising quantization. ``env`` and ``run`` are
|
|
injectable for CPU tests.
|
|
"""
|
|
mapping = os.environ if env is None else env
|
|
runner = subprocess.run if run is None else run
|
|
if mapping.get("OBLITERATUS_GSQ_E2E") != "1":
|
|
pytest.skip("set OBLITERATUS_GSQ_E2E=1 to run the GSQ end-to-end gate")
|
|
checkout = mapping.get("OBLITERATUS_GSQ_CHECKOUT")
|
|
if not checkout:
|
|
pytest.fail(
|
|
"OBLITERATUS_GSQ_E2E=1 is set but OBLITERATUS_GSQ_CHECKOUT"
|
|
" is missing: set it to a GSQ checkout path"
|
|
)
|
|
if not Path(checkout).is_dir():
|
|
pytest.fail(f"GSQ checkout not found: {checkout}")
|
|
interpreter = mapping.get("OBLITERATUS_GSQ_PYTHON") or (
|
|
workflow.resolve_python(checkout)
|
|
)
|
|
# GSQ runs in its own environment; the OBLITERATUS test environment may
|
|
# use CPU-only torch, so probe CUDA with the GSQ interpreter instead.
|
|
try:
|
|
probe = runner(
|
|
[interpreter, "-c", "import torch; assert torch.cuda.is_available()"],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
except OSError as exc:
|
|
pytest.fail(f"cannot run GSQ interpreter {interpreter!r}: {exc}")
|
|
if probe.returncode != 0:
|
|
output = f"{probe.stdout or ''}{probe.stderr or ''}"[-3000:]
|
|
pytest.fail(
|
|
"GSQ end-to-end prerequisites failed in the GSQ env"
|
|
f" (interpreter={interpreter!r}, exit={probe.returncode}):\n"
|
|
f"{output}"
|
|
)
|
|
return Path(checkout), interpreter
|
|
|
|
|
|
def _require_environment():
|
|
return check_e2e_prerequisites()
|
|
|
|
|
|
def download_snapshot_dir(interpreter, repo_id, revision, *, run=None):
|
|
"""Download a Hub snapshot in a fresh subprocess; return its path.
|
|
|
|
Runs ``huggingface_hub.snapshot_download`` under ``interpreter``
|
|
(the GSQ interpreter, which has ``huggingface_hub`` installed) and
|
|
returns the resolved snapshot path printed as the last stdout line,
|
|
requiring exit 0. The download runs outside the pytest process on
|
|
purpose: the HF client leaves unclosed ``ssl.SSLSocket`` objects
|
|
behind, and with ``filterwarnings = ["error"]`` those
|
|
``ResourceWarning``s surface after the test body as a
|
|
``PytestUnraisableExceptionWarning`` failure -- a subprocess keeps
|
|
those sockets (and warnings) out of this process entirely.
|
|
``run`` is injectable for CPU tests.
|
|
"""
|
|
runner = subprocess.run if run is None else run
|
|
script = (
|
|
"from huggingface_hub import snapshot_download\n"
|
|
f"print(snapshot_download(repo_id={repo_id!r},"
|
|
f" revision={revision!r}))\n"
|
|
)
|
|
try:
|
|
completed = runner(
|
|
[interpreter, "-c", script],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
except OSError as exc:
|
|
pytest.fail(f"cannot run snapshot download ({interpreter!r}): {exc}")
|
|
if completed.returncode != 0:
|
|
output = f"{completed.stdout or ''}{completed.stderr or ''}"[-3000:]
|
|
pytest.fail(
|
|
f"snapshot download of {repo_id}@{revision} failed"
|
|
f" (interpreter={interpreter!r},"
|
|
f" exit={completed.returncode}):\n{output}"
|
|
)
|
|
lines = [
|
|
line.strip()
|
|
for line in (completed.stdout or "").splitlines()
|
|
if line.strip()
|
|
]
|
|
if not lines:
|
|
pytest.fail(
|
|
f"snapshot download of {repo_id}@{revision} printed no path"
|
|
f" (interpreter={interpreter!r})"
|
|
)
|
|
return Path(lines[-1])
|
|
|
|
|
|
def _run_fresh(interpreter: str, lines: list[str]) -> None:
|
|
"""Run a snippet in a fresh process and require success."""
|
|
completed = subprocess.run(
|
|
[interpreter, "-c", "\n".join(lines)],
|
|
check=False,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
output = (completed.stdout + completed.stderr)[-3000:]
|
|
assert completed.returncode == 0, output
|
|
assert "logits" in completed.stdout
|
|
|
|
|
|
def _reload_and_check_logits(interpreter: str, assembled_dir: Path) -> None:
|
|
"""Reload the assembled checkpoint fresh and require finite logits.
|
|
|
|
Multimodal outputs (``vision_config`` present) load through the full
|
|
class named by the config's ``architectures`` -- so dense vision
|
|
weights the quantization metadata does not describe would fail here
|
|
-- while text-only outputs keep the causal-LM loader. Loading must
|
|
report no missing/unexpected keys (besides the tied ``lm_head``,
|
|
which is absent from the shards by design when
|
|
``tie_word_embeddings`` is set) and give finite logits on a
|
|
text-only prompt.
|
|
"""
|
|
config = json.loads((assembled_dir / "config.json").read_text())
|
|
if "vision_config" not in config:
|
|
_run_fresh(
|
|
interpreter,
|
|
[
|
|
"import torch",
|
|
"from transformers import AutoModelForCausalLM, AutoTokenizer",
|
|
f"model_dir = {str(assembled_dir)!r}",
|
|
"model = AutoModelForCausalLM.from_pretrained(",
|
|
" model_dir, trust_remote_code=False,",
|
|
" dtype=torch.bfloat16, device_map='cuda')",
|
|
"tokenizer = AutoTokenizer.from_pretrained(",
|
|
" model_dir, trust_remote_code=False)",
|
|
"inputs = tokenizer('Hello, world', return_tensors='pt')",
|
|
"inputs = {key: value.to('cuda') for key, value in"
|
|
" inputs.items()}",
|
|
"with torch.no_grad():",
|
|
" logits = model(**inputs).logits",
|
|
"assert torch.isfinite(logits).all(), 'non-finite logits'",
|
|
"print('logits', tuple(logits.shape), logits.dtype)",
|
|
],
|
|
)
|
|
return
|
|
_run_fresh(
|
|
interpreter,
|
|
[
|
|
"import torch",
|
|
"from transformers import AutoConfig, AutoTokenizer",
|
|
f"model_dir = {str(assembled_dir)!r}",
|
|
"config = AutoConfig.from_pretrained(",
|
|
" model_dir, trust_remote_code=False)",
|
|
"arch = (config.architectures",
|
|
" or ['Qwen3_5ForConditionalGeneration'])[0]",
|
|
"try:",
|
|
" from transformers import AutoModelForImageTextToText as _MM",
|
|
" loader = _MM",
|
|
"except ImportError:",
|
|
" import transformers as _tf",
|
|
" loader = getattr(_tf, arch)",
|
|
"model, loading = loader.from_pretrained(",
|
|
" model_dir, trust_remote_code=False,",
|
|
" dtype=torch.bfloat16, device_map='cuda',",
|
|
" output_loading_info=True)",
|
|
"missing = list(loading.get('missing_keys') or [])",
|
|
"unexpected = list(loading.get('unexpected_keys') or [])",
|
|
"# tie_word_embeddings ties lm_head to embed_tokens: the tied",
|
|
"# head is absent from the shards by design, so it is the only",
|
|
"# tolerated missing key, and only when tying is enabled.",
|
|
"if getattr(config, 'tie_word_embeddings', False):",
|
|
" missing = [key for key in missing",
|
|
" if key != 'lm_head.weight']",
|
|
"assert not missing, missing",
|
|
"assert not unexpected, unexpected",
|
|
"tokenizer = AutoTokenizer.from_pretrained(",
|
|
" model_dir, trust_remote_code=False)",
|
|
"inputs = tokenizer('Hello, world', return_tensors='pt')",
|
|
"inputs = {key: value.to('cuda') for key, value in"
|
|
" inputs.items()}",
|
|
"with torch.no_grad():",
|
|
" logits = model(**inputs).logits",
|
|
"assert torch.isfinite(logits).all(), 'non-finite logits'",
|
|
"print('logits', tuple(logits.shape), logits.dtype)",
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.mark.gpu
|
|
@pytest.mark.download
|
|
@pytest.mark.slow
|
|
def test_gsq_quantization_end_to_end(tmp_path):
|
|
checkout, interpreter = _require_environment()
|
|
|
|
# The snapshot download runs in a fresh subprocess under the GSQ
|
|
# interpreter (which has huggingface_hub), so no HF network sockets
|
|
# -- and their unclosed-SSLSocket ResourceWarnings, which
|
|
# filterwarnings=error would turn into a post-body failure -- ever
|
|
# live in this pytest process.
|
|
model_dir = download_snapshot_dir(interpreter, MODEL, MODEL_REVISION)
|
|
|
|
changed, digest = workflow.prepare_checkout(checkout)
|
|
assert digest == source.EXPECTED_PREPARED_TREE_SHA256
|
|
|
|
workdir = tmp_path / "gsq-e2e"
|
|
checkpoint_dir = workdir / "checkpoints"
|
|
checkpoint_dir.mkdir(parents=True)
|
|
config_path = workdir / "gsq-e2e.yaml"
|
|
config_path.write_text(
|
|
workflow.render_gsq_config(
|
|
str(model_dir),
|
|
checkpoint_dir=str(checkpoint_dir),
|
|
log_dir=str(workdir / "logs"),
|
|
act_cache_dir=str(workdir / "act-cache"),
|
|
num_samples=8,
|
|
val_samples=2,
|
|
gptq_nsamples=4,
|
|
max_length=256,
|
|
batch_size=2,
|
|
num_epochs=1,
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
code = workflow.run_quantization(
|
|
checkout,
|
|
config_path,
|
|
python=interpreter,
|
|
world_size=1,
|
|
)
|
|
assert code == 0
|
|
|
|
config = json.loads(
|
|
(model_dir / "config.json").read_text(encoding="utf-8")
|
|
)
|
|
expected = gsq_coverage.expected_gsq_modules(config, expected_count=186)
|
|
assert len(expected) == EXPECTED_PROJECTIONS
|
|
|
|
run_dir = workflow.resolve_run_dir(checkpoint_dir)
|
|
# The launch binding flow: run records the launch (config SHA-256
|
|
# plus base-model hashes) in the checkpoint dir and binds it into
|
|
# the single new run dir; validate with --gsq-config below requires
|
|
# that binding before certifying the run.
|
|
launch_records = list(
|
|
checkpoint_dir.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*")
|
|
)
|
|
assert len(launch_records) == 1
|
|
binding = json.loads((run_dir / workflow.LAUNCH_FILENAME).read_text())
|
|
assert binding["launch_id"] == json.loads(
|
|
launch_records[0].read_text(encoding="utf-8")
|
|
)["launch_id"]
|
|
assert binding["run_id"] == run_dir.name
|
|
assert binding["gsq_config_sha256"] == workflow._sha256_file(
|
|
config_path.resolve()
|
|
)
|
|
assert binding["base_model_sha256"] == workflow._base_model_file_hashes(
|
|
model_dir
|
|
)
|
|
assert binding["exit_code"] == 0
|
|
result = workflow.validate_checkpoint(
|
|
model_config_path=model_dir / "config.json",
|
|
checkpoint_dir=run_dir,
|
|
gsq_checkout=checkout,
|
|
model=MODEL,
|
|
model_revision=MODEL_REVISION,
|
|
gsq_config_path=config_path,
|
|
)
|
|
assert result.passed
|
|
manifest_path = run_dir / workflow.MANIFEST_FILENAME
|
|
assert manifest_path.is_file()
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
assert manifest["run_id"] == run_dir.name
|
|
assert manifest["checkpoint_dir"] == str(run_dir.resolve())
|
|
assert manifest["gsq_config_path"] == str(config_path.resolve())
|
|
assert manifest["gsq_config_sha256"] == workflow._sha256_file(
|
|
config_path.resolve()
|
|
)
|
|
assert manifest["validation"]["quantize_attention"] is True
|
|
assert manifest["validation"]["quantize_mlp"] is True
|
|
assert manifest["validation"]["group_size"] == 128
|
|
assert manifest["validation"]["expected_count"] is None
|
|
assert manifest["base_model_dir"] == str(model_dir.resolve())
|
|
assert set(manifest["shard_sha256"]) == {
|
|
path.name
|
|
for path in run_dir.glob("*.safetensors")
|
|
} | {"progress.json"}
|
|
assert manifest["base_model_sha256"] == workflow._base_model_file_hashes(
|
|
model_dir
|
|
)
|
|
assert "config.json" in manifest["base_model_sha256"]
|
|
assert any(
|
|
name.endswith(".safetensors")
|
|
for name in manifest["base_model_sha256"]
|
|
)
|
|
assert manifest["coverage"]["passed"] is True
|
|
assert manifest["prepared_tree_verified"] is True
|
|
|
|
assembled_dir = workdir / "assembled"
|
|
assemble_code = workflow.assemble_checkpoint(
|
|
checkout,
|
|
config_path,
|
|
out_dir=assembled_dir,
|
|
python=interpreter,
|
|
)
|
|
assert assemble_code == 0
|
|
verification = gsq_coverage.verify_assembled_checkpoint(assembled_dir)
|
|
assert len(verification["packed_modules"]) == EXPECTED_PROJECTIONS
|
|
_reload_and_check_logits(interpreter, assembled_dir)
|
|
|
|
|
|
def _e2e_env(tmp_path, **overrides):
|
|
env = {
|
|
"OBLITERATUS_GSQ_E2E": "1",
|
|
"OBLITERATUS_GSQ_CHECKOUT": str(tmp_path),
|
|
}
|
|
env.update(overrides)
|
|
return env
|
|
|
|
|
|
def test_e2e_gate_skips_without_opt_in(tmp_path):
|
|
with pytest.raises(pytest.skip.Exception):
|
|
check_e2e_prerequisites(env={})
|
|
with pytest.raises(pytest.skip.Exception):
|
|
check_e2e_prerequisites(env={"OBLITERATUS_GSQ_E2E": "0"})
|
|
|
|
|
|
def test_e2e_gate_fails_when_opted_in_without_checkout(tmp_path):
|
|
"""An explicit opt-in without a checkout fails instead of skipping."""
|
|
with pytest.raises(pytest.fail.Exception, match="OBLITERATUS_GSQ_CHECKOUT"):
|
|
check_e2e_prerequisites(env={"OBLITERATUS_GSQ_E2E": "1"})
|
|
|
|
|
|
def test_e2e_gate_fails_when_probe_fails(tmp_path):
|
|
"""An opted-in CUDA probe failure fails with probe output, not skip."""
|
|
|
|
def _failing_probe(*args, **kwargs):
|
|
return subprocess.CompletedProcess(
|
|
args[0], 1, stdout="probe-out", stderr="torch-cuda-missing"
|
|
)
|
|
|
|
with pytest.raises(pytest.fail.Exception, match="torch-cuda-missing"):
|
|
check_e2e_prerequisites(env=_e2e_env(tmp_path), run=_failing_probe)
|
|
|
|
|
|
def test_e2e_gate_fails_when_checkout_missing(tmp_path):
|
|
missing = tmp_path / "no-checkout"
|
|
with pytest.raises(pytest.fail.Exception, match="not found"):
|
|
check_e2e_prerequisites(
|
|
env=_e2e_env(tmp_path, OBLITERATUS_GSQ_CHECKOUT=str(missing)),
|
|
run=lambda *a, **k: subprocess.CompletedProcess(a[0], 0),
|
|
)
|
|
|
|
|
|
def test_e2e_gate_fails_when_interpreter_unrunnable(tmp_path):
|
|
def _boom(*args, **kwargs):
|
|
raise FileNotFoundError("no such interpreter")
|
|
|
|
with pytest.raises(pytest.fail.Exception, match="cannot run"):
|
|
check_e2e_prerequisites(env=_e2e_env(tmp_path), run=_boom)
|
|
|
|
|
|
def test_e2e_gate_passes_when_probe_succeeds(tmp_path):
|
|
def _ok_probe(*args, **kwargs):
|
|
return subprocess.CompletedProcess(args[0], 0, stdout="", stderr="")
|
|
|
|
checkout, _interpreter = check_e2e_prerequisites(
|
|
env=_e2e_env(tmp_path), run=_ok_probe
|
|
)
|
|
assert checkout == tmp_path
|
|
|
|
|
|
def test_snapshot_download_runs_in_subprocess_and_returns_last_line(tmp_path):
|
|
"""The helper shells out and takes the resolved path from stdout."""
|
|
seen: dict = {}
|
|
|
|
def _fake_run(command, **kwargs):
|
|
seen["command"] = list(command)
|
|
seen["kwargs"] = kwargs
|
|
return subprocess.CompletedProcess(
|
|
command, 0, stdout="progress line\n/snap/dir\n", stderr=""
|
|
)
|
|
|
|
result = download_snapshot_dir(
|
|
"gsq-python", MODEL, MODEL_REVISION, run=_fake_run
|
|
)
|
|
assert result == Path("/snap/dir")
|
|
assert seen["command"][0] == "gsq-python"
|
|
assert seen["command"][1] == "-c"
|
|
assert "snapshot_download" in seen["command"][2]
|
|
assert MODEL in seen["command"][2]
|
|
assert MODEL_REVISION in seen["command"][2]
|
|
assert seen["kwargs"]["capture_output"] is True
|
|
|
|
|
|
def test_snapshot_download_fails_on_nonzero_exit():
|
|
def _failing_run(*args, **kwargs):
|
|
return subprocess.CompletedProcess(
|
|
args[0], 1, stdout="", stderr="network unreachable"
|
|
)
|
|
|
|
with pytest.raises(pytest.fail.Exception, match="network unreachable"):
|
|
download_snapshot_dir("gsq-python", MODEL, MODEL_REVISION, run=_failing_run)
|
|
|
|
|
|
def test_snapshot_download_fails_when_interpreter_unrunnable():
|
|
def _boom(*args, **kwargs):
|
|
raise FileNotFoundError("no such interpreter")
|
|
|
|
with pytest.raises(pytest.fail.Exception, match="cannot run"):
|
|
download_snapshot_dir("gsq-python", MODEL, MODEL_REVISION, run=_boom)
|
|
|
|
|
|
def test_snapshot_download_fails_without_printed_path():
|
|
def _quiet_run(*args, **kwargs):
|
|
return subprocess.CompletedProcess(args[0], 0, stdout="\n", stderr="")
|
|
|
|
with pytest.raises(pytest.fail.Exception, match="printed no path"):
|
|
download_snapshot_dir("gsq-python", MODEL, MODEL_REVISION, run=_quiet_run)
|