Files
OBLITERATUS/docs/gsq-quantization.md
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

681 lines
41 KiB
Markdown

# Portable 2-bit GSQ quantization workflow
This workflow produces 2-bit GSQ-quantized releases of abliterated models
from a plain environment. It uses a pinned external GSQ source, a recorded
commit and prepared-tree hash, a coverage validator for target projections,
fail-closed numerical checks, and memory-safe activation capture. It needs
no deployment-specific services.
Pinned source: `https://github.com/IST-DASLab/GSQ.git` at revision
`03fc16484c369e3127225615d5e03e8d3a6043e3`. The machine-readable record
lives in `ci/gsq-source-v1.json` (repository, revision, prepared Python
tree SHA-256, pinned OpenThoughts dataset revision);
`tests/test_gsq_workflow.py` asserts the code and the file agree. GSQ runs
in its own environment created from its own pinned `uv.lock`.
## Choosing a memory policy
Immediately before spawning the trainer, `run` checks fresh device and host
headroom through the shared phase-admission API. The demand includes base
weights at the configured run dtype, RAM activation buffers and their
next-layer copies, layer Hessians, and quantization/optimizer workspace.
It reads local safetensors headers without loading tensors. These trainer
overlap estimates have not been calibrated against measured peaks, so they
are labelled **estimated**, not guaranteed requirements or GPU qualification.
Use the default policy for an initial exploratory run: calibrated shortfalls
reject, while estimated shortfalls or unavailable estimates warn before launch.
Choose `OBLITERATUS_PHASE_ADMISSION=strict` to refuse estimated shortfalls
and unknown demand too. `fail-closed` requires calibrated components and
therefore rejects this uncalibrated trainer estimate. An independently
verified run can explicitly use `off`, which bypasses the preflight and
still reports that admission is disabled. Admission observes memory; it
does not reserve it or coordinate a concurrent UI process.
If a warning identifies host capture buffers, reduce `data.num_samples`,
`data.val_samples`, `gptq.nsamples`, or `data.max_length`. For a device
shortfall, choose a smaller base model or a device with more free VRAM.
Changing the dtype requires a matching base checkpoint under the workflow's
existing dtype contract. Recheck model quality after reducing calibration
samples. Passing admission is not evidence that quantization quality passed.
## Commands
All commands go through `scripts/gsq_workflow.py` (or
`python -m obliteratus.gsq`):
```bash
scripts/gsq_workflow.py fetch /path/to/GSQ
uv sync --locked # inside /path/to/GSQ
scripts/gsq_workflow.py prepare /path/to/GSQ
scripts/gsq_workflow.py config --model /path/to/model \
--out gsq.yaml --checkpoint-dir /path/to/checkpoints
# Hub ids resolve at config time instead:
# scripts/gsq_workflow.py config --model Qwen/Qwen3.5-0.8B \
# --model-revision <revision> --out gsq.yaml \
# --checkpoint-dir /path/to/checkpoints
scripts/gsq_workflow.py run /path/to/GSQ --config gsq.yaml
scripts/gsq_workflow.py validate --model-config /path/to/model/config.json \
--checkpoint-dir /path/to/checkpoints/<run-id> \
--gsq-checkout /path/to/GSQ --gsq-config gsq.yaml \
--model <model-id> --model-revision <revision>
scripts/gsq_workflow.py assemble /path/to/GSQ --config gsq.yaml \
--run-id <run-id> --out-dir /path/to/assembled
```
`fetch` clones the pinned revision. `uv sync --locked` inside the
checkout builds GSQ's own interpreter (`.venv`) before `run`. `prepare`
verifies the pin, computes the prospective prepared-tree digest from the
in-memory staged contents (plus unchanged files and the guard file)
before promoting anything, and fails without changing any file (no temp
or backup files) unless that digest matches the recorded value
(override with `--allow-unrecorded-digest` only to record a new value),
then applies the adapter and prints the digest (relative paths hash in
canonical POSIX form, so Windows and POSIX checkouts of the same tree
share the recorded digest); every patch is
classified as pending or fully applied in memory first, so a
half-applied tree (replacement present while its old fragment remains,
or old and new side by side) raises before any file is written; the
write itself is atomic (staged temp files plus `os.replace` promotion
with intent recorded before each rename and rollback from backups, so
even a post-rename interrupt rolls back), so a mid-prepare failure
leaves the tree byte-identical for an idempotent retry. Permission bits
survive the round trip: staged replacements are chmodded to the original
file's mode before promotion (new files such as `gsq_numerics_guard.py`
get `0o644 & ~umask`), and backups/restores carry the original mode, so
a `0755` checkout file never comes back as `0600`. Modes are not part of
the prepared-tree digest (paths and bytes only), so preserving them
cannot change the recorded digest.
`config` renders
a GSQ YAML with `num_workers: 0` and a very high
`act_cache_mmap_threshold_gb` (see below); `--dataset` selects the
calibration dataset (default `open_thoughts`; one of `c4`, `fineweb_edu`,
`open_thoughts`, `mixed`, matching what `create_dataloader` resolves at
the pinned revision). The rendered `model.dtype` (and matching
`quantization.logits_dtype`) is derived from the resolved model
directory's actual float weight dtype, read from the top-level
safetensors headers -- never from `config.json` `torch_dtype`, which the
pinned loader does not consult: `src/models/base.py` `_set_tensors`
preserves each checkpoint tensor's own dtype (`dtype=t.dtype`) while
`main.py` `create_dict` allocates the activation buffers in the single
`model.dtype` run dtype, so a hardcoded `bfloat16` with FP16/FP32
weights feeds mismatched activations into those layers and fails
calibration. An explicit `--dtype` override must match the detected
base dtype when one exists. Uncertainty policy: only unanimous float
2-D+ weight dtypes render (mixed float dtypes, F64/F8 weights, and
unreadable headers fail closed at render time); a directory with no
detectable float weights renders `bfloat16` (byte-identical to the
historical rendering for bf16 bases), and `run`, `validate
--gsq-config`, and `assemble` re-check the config dtypes against the
current base headers, so a stale or hand-edited config bound to another
base dtype refuses before launch, certification, or assembly.
`checkpoint-dir`, `log-dir`, and `act-cache-dir`
are resolved to absolute paths against the caller working directory at
config time, and so is `--model` when it names a local path that exists
on disk (for example `./models/abliterated`): training and assembly run
with `cwd` set to the GSQ checkout, so a relative model path would
otherwise resolve against the wrong directory. The workflow is
local-snapshot based: a Hub repo id (`org/name` that does not exist
locally) requires `--model-revision` and is resolved with
`huggingface_hub.snapshot_download` at config time, rendering the
snapshot path -- so later steps bind the exact validated snapshot
instead of letting GSQ re-resolve (and possibly move) the Hub pointer.
`run` resolves the checkout, config, and
interpreter paths before launching, then refuses a config whose
`training.checkpoint_dir` is relative (training runs with `cwd` set to
the checkout, so a relative dir would write the run inside the checkout
while assembly looks in the caller directory), refuses unexpanded `$`
placeholders in `model.name` / `training.checkpoint_dir` / `log_dir` /
`act_cache_dir` (GSQ would expand them from the process environment),
refuses a `model.name` that is not an existing absolute local directory,
verifies pin and digest,
and executes `python main.py --config ...` inside the checkout with the
checkout's own
`.venv` interpreter (or `--python`), so relative paths never resolve
inside the
checkout itself. `run` is single-GPU only: `--world-size` must be `1`
(anything larger is refused before anything launches; see Known
limitations). There is no resume path: `run --resume` (and the
`resume` parameter behind it) is refused with a start-a-fresh-run
error, because the pinned trainer would reuse the previous run's
completed layers while training the rest from the current config's
model, silently mixing two base models (see Known limitations).
Before launching, `run` enforces the portable method contract on
the GSQ YAML -- 2-bit `quantization.gsq_bits` from `gptq` init with
`gsq_enabled` true at symmetric 2-bit `gptq.wbits`, matching
`quantization.groupsize` / `gptq.groupsize`, explicit
`data.num_workers: 0`, and an explicit
`training.act_cache_mmap_threshold_gb` at least 1024 (every key must
be present: omitted keys refuse instead of falling back to GSQ
defaults such as 8 workers or a 2 GB mmap threshold) -- and requires
the base weight index to agree
with the top-level shards when `model.safetensors.index.json` exists
beside the base model: every `weight_map` file must be a top-level
file in the dir (no nested paths, no missing files), the referenced
set must equal the top-level `*.safetensors` set, each mapped key
must actually live in its mapped file (headers-only check), and the
`weight_map` key set must equal exactly the union of tensor keys in
the top-level shards (an omitted norm or projection stays on `meta`
during training). GSQ
training follows the index (including nested shard paths), so without
this gate a run could train against nested snapshot B while top-level
snapshot A stayed unchanged. Before launching, `run` additionally
sizes the requested activation capture the way the pinned `main.py`
`create_dict` allocates it -- one `num_samples x seqlen x hidden_size`
buffer of the run dtype per loader, with sample counts from
`data.num_samples` / `data.val_samples` / `gptq.nsamples`, `seqlen`
from `data.max_length`, `hidden_size` from the base model config
(`text_config`-aware), and the item size from `model.dtype`: any
buffer at or above `training.act_cache_mmap_threshold_gb` refuses, so
the disk-backed mmap branch can never be taken and capture stays in
RAM. `run` then writes a launch binding -- a
JSON record in the absolute `training.checkpoint_dir` named after a
fresh launch id, holding the GSQ config SHA-256 and the per-file
base-model hashes (including the index, so changing or removing the
index after launch is drift) plus the training-source identity
(checkout revision, computed prepared-tree digest, whether it matched
the recorded digest, and whether `--allow-unrecorded-digest` was
used) -- and after the subprocess exits it
re-hashes both inputs against those preserved pre-launch values: any
change during training (replaced base snapshot, edited YAML, or
edited/removed index) refuses to bind with a clear error, so the run
can never certify against replacement inputs. Recording the binding
hashes the config and every base-model file, which can take a while,
so `run` re-verifies the revision and digest immediately before
spawning and refuses to launch when the source moved (rewriting the
launch record unverified first) -- changed code can never execute
while recording `prepared_tree_verified=True`. The whole
snapshot-launch-bind sequence holds an exclusive launch lock
(`gsq_launch.lock`, created with `O_CREAT|O_EXCL` and holding
`<pid>:<launch id>`) in the absolute checkpoint dir: a second workflow
launch there refuses instead of creating run dirs concurrently, and the
lock always releases in a `finally` block, including on Ctrl-C. The run-dir
`gsq_launch.json` always carries the preserved pre-launch hashes --
never recomputed values -- plus the exit status and completed layer.
The training-source fields alone are re-verified after the child exits
(on success, non-zero exit, and interruption alike): a moved source
still binds, but with the recomputed values and
`prepared_tree_verified=False`, so `validate` records an unverified
training source and `assemble` refuses the run without
`--allow-unverified-training-source` (a successful run with a moved
source raises instead of returning success).
On success `run` diffs the run-dir set (exactly one new run dir is
required); the single new dir must also be newer than the launch start
(a dir predating it belongs to another launch and refuses), and a run
dir already holding another launch's `gsq_launch.json` is never
overwritten -- and writes that binding into it as `gsq_launch.json`; on a
non-zero exit it still binds the single new run dir holding a
`progress.json` (recording the exit status) so
`validate --allow-partial --gsq-config` can certify the valid prefix,
while still returning the non-zero exit code. A `KeyboardInterrupt`
from the training subprocess (Ctrl-C) binds the same unambiguous
partial run with the interrupted exit status (`-2`) before re-raising,
so completed layers still validate with `--allow-partial` instead of
being lost. `validate` with `--gsq-config` compares the current
config/base hashes against the preserved pre-launch values and refuses
any run that was not launched with the current config and base model
(including an index changed or removed after launch), and re-checks
index/path agreement even for runs validated without `--gsq-config`.
Both `run` and `assemble` launch GSQ with a sanitized
environment that strips every variable GSQ's `load_config` consults
(`GSQ_MODEL_NAME`, `GSQ_CHECKPOINT_DIR`, `GSQ_LOG_DIR`,
`GSQ_ACT_CACHE_DIR`, plus `WANDB_PROJECT`/`WANDB_ENTITY` for
determinism), so an inherited override cannot redirect the run away from
the validated config, and that pins the single-GPU distributed
identity (`WORLD_SIZE=1`, `RANK=0`, `LOCAL_RANK=0`,
`LOCAL_WORLD_SIZE=1`) while dropping rendezvous/launcher records
(`MASTER_ADDR`, `MASTER_PORT`, `GROUP_*`, `ROLE_*`,
`TORCHELASTIC_*`), so an inherited `WORLD_SIZE=2` cannot push the
pinned `main.py` into NCCL rendezvous. `validate` detects the tensor prefix from the
base-model weights next to the model config (index `weight_map` or
single-file keys); when no weights are available it fails closed and
`--module-prefix` (`model` or `model.language_model`) must be passed
explicitly instead of guessing from config nesting. When
`--base-model-dir` is given, `--model-config` must be that base model's
own `config.json` by same resolved path. It requires complete coverage
unless `--allow-partial` records an explicit partial (a genuinely
interrupted run with `progress.json` `last_completed_layer` below
`num_layers-1` -- e.g. a 24-layer model stopped after layer 0 via
`--max-layers 1` -- validates expected projections only for layers
`0..last_completed_layer`, refusing shards for layers beyond it, and
records `partial_layers`; without the flag such runs still require
completion) and checks every expected projection's packed tensors against
the base-model `[out, in]` shapes (base weights are the truth whenever
they are available: `--base-model-dir` defaults to the model config's
directory whenever base weights exist there -- on every path, including
the CLI and assembly revalidation -- and an explicit shape table is
cross-checked entry by entry with any mismatch refusing, so it cannot
override actual base-weight dimensions; explicit shapes are usable only
for weightless runs with no base weights anywhere; `--group-size`
default 128 tunes the
`weight_scale` extent check, while the pack width is fixed at the
assembler's 4-bit container -- any other `pack_bits` is refused):
`weight_packed` must be int32 with shape `[out, in*4/32]`, `weight_scale`
floating with shape `[out, ceil(in/group_size)]` and finite (no NaN/Inf),
and `weight_shape` int64 with shape `(2,)` holding positive base-shape
extents; `in_features` must be divisible by `group_size` (group
quantization has no padding path, so a remainder such as in 16 with
group 128 could never load); empty tensors fail, and without base
weights or explicit shapes
validation fails closed instead of skipping the checks. Finite scales are
additionally certified to decode to finite weights as the real
pack-quantized decompressor computes them: the container ints unpacked
per the 4-bit width times the per-group scale in the scale dtype, checked
per group (an fp32 scale of `finfo(float32).max` times unpacked `-8`
decodes to `-inf`) in bounded row chunks, so large models never
materialize the decoded tensor -- and in the bound model load dtype too
(the config's run dtype when `--gsq-config` is given, else the dtype
derived from the base weights): the loader casts scales to the load
dtype before decoding, so each scale must also be finite after casting
(an fp32 `1e10` scale is `+inf` in fp16) with `max|q| * |scale|` finite
in that dtype. GSQ here is
symmetric (`gptq` `sym: True`): for targeted projections only the trio is
accepted -- any `weight_zero_point`, `weight_g_idx`, or other `weight_*`
tensor for an expected projection raises, since the symmetric
pack-quantized container must not carry them. The config-derived topology
is cross-checked against the base weights when available: every expected
projection's base weight must exist and the base must not contain
additional layer indices beyond the derived list, so a shortened config
with fewer layers than the base refuses instead of certifying a subset.
Only the shard set the assembler consumes (`*.safetensors` matching
`layers_<N>`) is inspected: any other safetensors file, a filename/key
layer-index mismatch, or a duplicate key raises. Every tensor key must
additionally belong to its file's layer (`<prefix>.layers.<N>.` with the
filename's index), and any key carrying a quantization suffix
(`weight_packed`, `weight_scale`, `weight_shape`, `weight_zero_point`,
`weight_g_idx`) must name an expected projection: unknown quantization
metadata such as `lm_head.weight_scale` would make the assembler drop a
base weight with no packed replacement. Plain layer keys without such a
suffix (layernorm weights, `conv1d`, `A_log`, `dt_bias`, `norm`,
`q_norm`/`k_norm`) are accepted only when they overwrite honestly:
every non-projection tensor in a consumed shard overwrites the base
tensor with the same key during assembly (`save_model.py` merges by
exact key), so each must exist in the base model with the same dtype
and shape and hold finite, non-empty contents (streamed per tensor);
without `--base-model-dir` such runs fail closed. When a load dtype is
bound, every floating overwrite must additionally stay finite after
casting to it in bounded chunks: the loader casts retained weights to
the load dtype too, so a stored-finite overwrite (an fp32 `1e10`) that
overflows it would load as non-finite.
When `--gsq-config` is given, the validation settings are cross-checked
against that YAML first: `quantization.self_attn` must equal the
attention flag (`--no-attention` against a config with `self_attn: true`
is refused), MLP quantization is mandatory (GSQ always quantizes MLP, so
`--no-mlp` is refused), and the portable method contract must hold:
`quantization.gsq_bits == 2`, `gptq.wbits == 2` with `gptq.sym` true,
`quantization.init_method == "gptq"` with `gsq_enabled` true,
`quantization.groupsize` and `gptq.groupsize` equal to `--group-size`,
explicit `data.num_workers == 0`, and an explicit
`training.act_cache_mmap_threshold_gb` of at least 1024 (omitted keys
refuse instead of falling back to GSQ defaults; a `gsq_bits=4` /
`wbits=4` config shares the two-bit container shapes, so the widths are
certified here rather than by shape checks), and every activation
buffer the pinned allocator would capture (`data.num_samples` /
`data.val_samples` / `gptq.nsamples` x `data.max_length` x base hidden
size x run dtype) must sit below that threshold, so the disk-backed
mmap branch can never be taken. `assemble` re-checks the
same contract (including the RAM-resident capture sizing) against the recorded validation settings before invoking
`save_model.py`. The pack width must be the assembler's 4-bit
container, unexpanded `$` placeholders are refused, and the config's
`model.name` must be an existing absolute local directory equal to the
validated base model dir (or the model config's directory with explicit
shapes) -- an unresolved Hub id, or a directory that is not the
validated one, is refused. The run must additionally hold the
`gsq_launch.json` binding `run` wrote after launching it, recording
the same config SHA-256 and base-model hashes: a run without a binding
(or launched from another snapshot, even a same-shaped one) is refused
instead of being certified for this config. The binding also records
the training-source identity (checkout revision, computed prepared-tree
digest, whether it matched the recorded digest, and whether
`--allow-unrecorded-digest` was used), which `validate` preserves into
the manifest. Runs validated without
`--gsq-config` skip this binding. `--model` / `--model-revision` only record
provenance in the manifest; binding is through the directories. On success `validate`
writes `gsq_run_manifest.json` (schema 2) next to the checkpoint
recording the GSQ repo, commit, prepared-tree digest and verification
flag, the launch-time training-source identity, model id/revision, model config path, absolute config path and
its SHA-256, the full validation settings (including the explicit
`allow_partial` flag and the recorded `partial_layers` set), the bound
model load dtype the scales were certified to decode finitely under
(`run_dtype`), the base
model dir or explicit shapes, a per-file SHA-256 of every consumed
shard plus `progress.json`, a per-file SHA-256 of every base-model file
the assembler reads (every top-level `*.safetensors` plus
`model.safetensors.index.json`, `config.json`, the non-weight files
`save_model.py` copies including `chat_template.jinja`,
`chat_template.json`, `added_tokens.json`, `processor_config.json`,
`video_preprocessor_config.json`, and `chat_templates/*.jinja`), run id,
absolute checkpoint dir, and coverage
result (including the validated packed module names for the
post-assembly exact-set check).
`prepared_tree_verified` is true only when the commit pin
matches, the computed digest equals the recorded value, AND the
launch-time training source was verified (launch revision matches the
pin and launch digest equals the recorded digest); a checkout
with a mismatching digest fails closed instead of recording a verified
claim. A run trained with `--allow-unrecorded-digest` from experimental
code therefore validates to an unverified manifest even after the
checkout is restored to the approved state. `assemble` verifies pin and digest, resolves the run location
from the same config training used (its checkpoint dir must be
absolute), and binds to the validated run identity: the run dir must
contain the `validate`-written manifest (schema 2, with a config
binding — a manifest without one is refused) whose run id, checkpoint
dir, config path and SHA-256 match the config being assembled with,
whose recorded launch-time training source was verified (otherwise only
an explicit `--allow-unverified-training-source` assembles it),
whose recorded settings agree with that config, whose recorded shard
hashes match the current checkpoint bytes (any added, removed, or
changed shard file fails closed), whose recorded base-model hashes
match the current base-model bytes (any added, removed, or changed
base file fails closed before `save_model.py` runs), and whose coverage
passed for full runs (including the recorded validated packed module
names); otherwise assembly fails closed and never selects an unrelated
run. `--allow-partial` requires a manifest that records an explicit
partial validation with a matching flag and a recorded partial layer
set (from `validate --allow-partial`); a full assembly refuses a partial
manifest and vice versa. It then re-runs the full checkpoint validation
with the recorded settings (including the partial flag, with the partial
layer set re-checked, always using base-derived shapes when base weights
are available so an explicit shape override cannot survive revalidation)
immediately before invoking `save_model.py`, so a shard or base file
deleted or changed after validation refuses to assemble. The output
directory must be new or empty: an existing non-empty `--out-dir` fails
closed before `save_model.py` runs, so a stale `model.safetensors`
cannot sit beside the new index (Transformers would prefer the stale
file over the assembled shards). `assemble` then runs
`save_model.py --config <yaml> --run-id <run_id> --run-dir <run>
--out-dir <out>` inside
the checkout to merge the per-layer packed shards with the base model
into a loadable checkpoint; the resolved run directory always reaches
`save_model.py` as `--run-dir`, so a `--checkpoint-dir` override cannot
assemble a different same-named run from the YAML directory. On success
`assemble` certifies the output with the post-assembly verifier
(`obliteratus.gsq.coverage.verify_assembled_checkpoint`, with the
validated projection set): the injected `quantization_config` must be
exactly what the pinned `save_model.py` writes (`compressed-tensors` /
`pack-quantized` / `compressed` status, a single `Linear` group holding
exactly `input_activations` / `output_activations` (null), `targets`, and
`weights`, with `weights` holding exactly 4-bit grouped symmetric int
`num_bits` / `strategy` / `group_size` / `symmetric` / `type` in exact
JSON types -- no group `format` override, no `block_structure`, no
`dynamic` / `actorder`, no `kv_cache_scheme` / `global_compression_ratio` /
`run_compressed`, and no other key at any level; unknown keys fail closed
because the loader ignores them silently), the layout must be loadable by Transformers
-- either a single `model.safetensors` (no index, and no other weight
files) or `model.safetensors.index.json` whose `weight_map` covers
exactly the shard files present, so numbered shards without an index
fail -- every index reference must be a plain top-level assembler shard
name (`model-<NNNNN>-of-<MMMMM>.safetensors`, resolving inside the
output dir, so traversal/absolute/subdirectory/symlink escapes fail),
every module holding any of `weight_packed` / `weight_scale` /
`weight_shape` must hold all three (orphan metadata fails, including
zero packed modules with orphan scales/shapes), the output packed set
must equal exactly the validated set, and every tensor key inside a
referenced shard must be listed in the index `weight_map` for that file
(an unindexed tensor in a shard -- such as a stray `weight_zero_point`
-- fails, since loaders read shard contents; without an index the
single `model.safetensors` file's own keys are the full set), competing
checkpoint files fail (with an index, `model.safetensors` is always
refused -- even when the index references it, since Transformers prefers
the standalone file over indexed shards -- and every other top-level
`*.safetensors` / `*.bin` / `*.pt` / `pytorch_model*` file must be listed
in its `weight_map`; without an index, any `*.bin` /
`*.pt` / `pytorch_model*` file fails), every module holding a dense 2-D
`<module>.weight` tensor must be covered by the assembled
`quantization_config` ignore list -- matching compressed-tensors
`is_match` semantics (plain entries compare exactly and `re:` patterns
apply with `re.match`, anchored at the start, against module names, so
unanchored `re:visual` does not cover `model.visual.*` while
`re:.*visual.*` does -- PLUS plain entries matched against module class
names, so `ignore: ["Linear"]` or `["Module"]` skips every packed
projection while `re:.*Linear` matches nothing) -- every
`<module>.weight_packed` module
must be described (not ignored, with scale and shape), finite after
decoding (same per-group unpacked-int-times-scale check as run-shard
validation, in the stored dtype and in every bound load dtype: the
manifest's `run_dtype`, the bound base dtype, and the assembled
config's own `torch_dtype`/`dtype` declarations, since the loader casts
scales before decoding), no
`weight_zero_point` / `weight_g_idx` / other `weight_*` tensor besides
the symmetric trio may appear, and when a base model dir is given each
packed module's `weight_shape` must equal its base `<module>.weight`
shape (packed metadata is never trusted alone), and every
retained/overwritten base tensor
in the output must match the base dtype and shape with finite, non-empty
contents (streamed per tensor, with only complete trios exempting their
base weight so orphan metadata cannot silently drop it) -- and every
floating one must stay finite after casting to each bound load dtype
(the manifest's `run_dtype`, the bound base dtype, and the assembled
config's own `torch_dtype`/`dtype`) in bounded chunks, since the loader
casts retained weights too -- and every
chat/inference template file present in the base dir must be present in
the output with identical bytes -- otherwise
assembly fails closed instead of shipping metadata that misdescribes the
tensors. The checkout revision and prepared-tree digest are re-verified
immediately before spawning `save_model.py` (a source moved during the
lengthy revalidation refuses without running anything) and again after
it exits on every path, before the output is verified: output from a
source that moved during assembly is never certified. After the output
verification, `assemble` re-verifies every
bound input hash (consumed shards, `progress.json`, base-model files,
GSQ config, model config): an input changed while `save_model.py` ran
fails closed instead of being certified. Without `--allow-partial`,
partial layer runs are refused before any output is written.
Run-directory resolution never guesses by modification time: with a single
completed run under the checkpoint directory (a subdirectory holding
`progress.json`), `--run-id` may be omitted; with several runs, or none,
`assemble` fails closed until an explicit `--run-id` names a completed
run. Checkpoints land in `<checkpoint_dir>/<run_id>/` with
`progress.json` there.
## What the adapter changes and why
`obliteratus/gsq/source.py` applies a textual, fail-closed adapter: every
expected upstream fragment must match exactly before anything is changed,
and re-running a prepared tree changes nothing.
- Remote model code is never trusted: every `trust_remote_code=True`
becomes `False`.
- `main.py` inspects the declared model type with `AutoConfig`
(`trust_remote_code=False`) for every model path, so a checkpoint for a
certified dense family under any directory name (including a neutral
path such as `/models/export`) dispatches from `model_type`
(top-level or `text_config`): `llama` selects the LLaMA branch, `qwen3`
selects the Qwen3 branch, and `qwen3_5` / `qwen3_5_text` selects the
Qwen3.5 branch (MoE configs still route to the MoE wrappers via the
existing config-based `_is_moe` check: `hasattr(_tc, 'num_experts')` or
`hasattr(_tc, 'num_local_experts')`); a config that cannot load without
remote code, an unknown `model_type`, or any load error falls back to
the existing name-based dispatch instead of crashing.
- `qwen35.py` detects the tensor prefix (`model` vs
`model.language_model`) from the checkpoint layout, including
single-shard checkpoints.
- Coverage: linear attention selects **all** `nn.Linear` projections
(`in_proj_qkv`, `in_proj_z`, `in_proj_a`, `in_proj_b`, `out_proj`) and
the compressed layer list includes `in_proj_a` / `in_proj_b`; the
packing calls in `qwen35.py` and dense `qwen3.py` use the current
compressed-tensors API (`self._compress_weight`, as `base.py` does).
`llama.py` inherits the fixed base-class saver; `gemma4.py` is not a
certified family and is left unpatched.
- Coverage only certifies dense llama/qwen3-style stacks and
Qwen3.5/3.8 hybrid dense topologies: Qwen3.5 configs omitting
`layer_types` derive it from `full_attention_interval` (default 4,
every Nth layer full attention, as transformers does), while configs
declaring experts (`num_experts` / `n_routed_experts` /
`num_local_experts` > 0), non-empty `mlp_only_layers`, or a set
`decoder_sparse_step` are rejected, as are malformed topologies
(non-list or empty `layer_types`, non-positive layer counts).
- Fail-closed Hessian: `src/prior/gptq.py` damps and factorizes through
`gsq_numerics_guard.py` (a verbatim copy of
`obliteratus/gsq/numerics_guard.py`), which raises on non-finite
Hessians, invalid damping, non-finite damped Hessians (damping
overflow), non-finite Cholesky factors, and Cholesky failure, with
diagonal statistics in each message.
- Calibration data is reproducible: both
`open-thoughts/OpenThoughts-114k` loads in `src/data/dataset.py` are
pinned to dataset revision `bd093c3994fd54d2390985b66988ddf282a55eb6`
(recorded in `ci/gsq-source-v1.json`), and the hard-coded
"You are Kimi, an AI assistant created by Moonshot AI." system message
is removed from OpenThoughts calibration conversations, since
calibrating non-Kimi models with a Kimi identity prompt is wrong.
- `num_workers: 0`: each rank allocates large host-side activation
buffers, and forked loader workers would inherit that address space.
- Activation buffers use `torch.empty` (callers fill every sample) with a
high mmap threshold so capture stays in RAM, plus a completeness guard
that raises unless every batch is captured.
- `save_model.py` no longer ignores quantized attention when
`quantization.self_attn` is on; it accepts a resolved `--run-dir`
(which overrides `--run-id` and the config checkpoint directory) and
refuses partial assembly unless `--allow-partial` is passed, with the
refusal checked right after collecting entries and before creating or
modifying any output.
- `save_model.py` additionally ignores Qwen3.5-style dense vision and
multi-token-prediction modules (`re:.*visual.*` for `model.visual.*`,
`re:.*mtp.*` for `mtp.*`): upstream only excluded `vision_tower` /
`mm_projector`, leaving dense vision Linears behind while the exported
metadata targets all `Linear` modules.
- `save_model.py` copies chat/inference metadata the upstream assembler
dropped: `chat_template.jinja`, `chat_template.json`,
`added_tokens.json`, `processor_config.json`,
`video_preprocessor_config.json`, and `chat_templates/*.jinja`
(preserving the subdirectory). Those files are hash-bound like the
other base-model inputs and verified byte-identical in the assembled
output.
## Assembled checkpoint format
Assembled checkpoints store the 2-bit GSQ codes in GSQ's
compressed-tensors pack-quantized container: `save_model.py` merges the
per-layer `weight_packed` / `weight_scale` / `weight_shape` tensors with
the base model into sharded safetensors, copies the config, tokenizer,
and chat/inference template files (`chat_template.jinja`,
`chat_template.json`, `added_tokens.json`, `processor_config.json`,
`video_preprocessor_config.json`, `chat_templates/*.jinja`), and injects
a compressed-tensors `quantization_config` (`format:
pack-quantized`, status `compressed`, `Linear` targets, null activations,
grouped symmetric int weights, no dynamic/reordered decoding). Note
the injected metadata declares a 4-bit int weight container:
`inject_quantization_config` defaults to `wbits=4` and assembly does not
override it, even though the packed codes it describes are the 2-bit GSQ
weights. That default is GSQ's own `save_model.py` behavior at the pinned
revision, unchanged by the adapter; the end-to-end gate reloads the
assembled output and checks finite logits rather than trusting the
declared width.
## Qwen3.8 496-projection contract
64 layers (48 linear-attention + 16 full-attention) resolve to 496 target
modules: 240 linear-attention, 64 full-attention, 192 MLP. Enforce it with
`expected_count=496` (see `qwen38_layer_types()` and
`QWEN38_MODULE_COUNT` in `obliteratus/gsq/coverage.py`). The validator
rejects any checkpoint missing a targeted projection, any dense fallback,
duplicate tensor keys, incomplete `progress.json`, packed tensors
missing `weight_scale` / `weight_shape`, safetensors files outside the
assembler's `layers_<N>` shard set, filename/key layer-index mismatches,
tensor keys outside their file's layer namespace, unknown quantization
metadata (any `weight_packed` / `weight_scale` / `weight_shape` /
`weight_zero_point` / `weight_g_idx` key that is not an expected
projection), non-finite scales, non-positive recorded shapes,
and any packed tensor whose dtype, extents, or recorded `[out, in]`
disagree with the base-model weights (2-bit codes in GSQ's 4-bit pack
container, group size 128).
## End-to-end gate
`tests/conditional/test_gsq_runtime.py` (markers `gpu`, `download`,
`slow`) snapshot-downloads `Qwen/Qwen3.5-0.8B` at pinned revision
`2fc06364715b967f1860aea9cf38778875588b17` and passes the local snapshot
path (not the hub id) as the model name, then quantizes with tiny
calibration sizes (24 layers: 18 linear + 6 full, 186 projections),
validates coverage with `expected_count=186` (packed dtypes/shapes
checked against the snapshot weights), asserts the manifest binds the
run id, checkpoint dir, config path and SHA-256, validation settings,
base model dir, and per-file shard hashes alongside the verified tree
state, assembles the run via the run-dir helper (which re-verifies that
binding, re-checks the shard hashes, and re-runs validation before
invoking `save_model.py`), and
runs the post-assembly verifier (186 packed modules, every dense 2-D
weight ignored), and reloads the assembled output in a fresh subprocess
with the GSQ interpreter to assert finite logits for a
short prompt: the full multimodal class named by the assembled config's
`architectures` (via `AutoModelForImageTextToText`, or the named
transformers class as fallback, `trust_remote_code=False`, bf16, CUDA)
when the config carries a `vision_config` -- asserting no
missing/unexpected keys (besides the tied `lm_head` when
`tie_word_embeddings` is set) -- and `AutoModelForCausalLM` otherwise.
Run it on a CUDA runner with a prepared checkout:
```bash
OBLITERATUS_GSQ_E2E=1 OBLITERATUS_GSQ_CHECKOUT=/path/to/GSQ \
python -m pytest tests/conditional/test_gsq_runtime.py --no-cov
```
`--no-cov` opts out of the repository-wide 75% coverage gate in
`pyproject.toml` (`addopts` enforces `--cov-fail-under=75`), which a
single end-to-end test cannot meet on its own.
The GSQ interpreter defaults to the checkout's `.venv` (override with
`OBLITERATUS_GSQ_PYTHON`). The test is skipped unless
`OBLITERATUS_GSQ_E2E=1`; once opted in, a missing
`OBLITERATUS_GSQ_CHECKOUT` (or any other unmet prerequisite) fails
instead of skipping, so a misconfigured opt-in can never pass without
exercising quantization. The Hub snapshot download runs in a fresh
subprocess under the GSQ interpreter (which has `huggingface_hub`),
returning the resolved path from the last stdout line with exit 0
required: the HF client leaves unclosed `ssl.SSLSocket` objects behind,
and with `filterwarnings = ["error"]` those `ResourceWarning`s would
otherwise surface after the test body as a
`PytestUnraisableExceptionWarning` failure -- the subprocess keeps those
sockets out of the pytest process deterministically, with no
warning filters touched.
## Known limitations
- Single-GPU only: the workflow has only been validated with
`--world-size 1`, and the pinned distributed Qwen3.5 lookup resolves a
`model.language_model` prefix against a text-only model under
multi-GPU launch (the first attention projection raises
`AttributeError`). `run` (and the `build_run_command` path behind the
CLI) therefore refuses `--world-size` above 1 with a clear error
instead of launching a run that cannot succeed; the parameter is kept
for future multi-GPU support.
- No resume: `run --resume` (and the `resume` parameter behind it) is
refused with a start-a-fresh-run error. Resuming would reuse the
previous run's completed layers (trained from the previous base
model) while training the remaining layers from the current config's
model, silently mixing two base models with no snapshot check. Each
fresh launch instead records a launch binding (the GSQ config
SHA-256 and the per-file base-model hashes, written to the checkpoint
dir before launch and bound into the run dir as `gsq_launch.json`
afterwards with the preserved pre-launch values plus exit status and
completed layer; inputs are re-hashed after training and any drift
refuses to bind, while a failed launch still binds its unambiguous
partial run for `validate --allow-partial`), and `validate` with
`--gsq-config` requires that binding to match -- so a run launched
from one snapshot can never be certified with a config naming
another.
- Model dispatch is config-based for the certified dense families
(`llama`, `qwen3`, `qwen3_5` / `qwen3_5_text`, top-level or
`text_config`): the declared `model_type` selects the wrapper
regardless of the directory name, so neutral paths such as
`/models/export` work and a Qwen3.5 checkpoint under a path containing
"llama" still selects the Qwen3.5 wrapper. Unknown `model_type` values
and configs that require remote code keep the upstream name-based
dispatch.
- Only dense llama/qwen3-style stacks and Qwen3.5/3.8 hybrid dense
topologies are certified by the validator; MoE, sparse, and other
topologies are rejected (see above).
- Concurrent launches serialize per checkpoint dir: `run` holds an
exclusive `gsq_launch.lock` for the whole snapshot-launch-bind
sequence, so a second workflow launch against the same absolute
checkpoint directory refuses instead of racing run-dir creation;
binding additionally refuses a run dir that predates the launch start
or already carries another launch's `gsq_launch.json`, so a launch
that creates no dir of its own can never bind another launch's run.
- Activation capture is RAM-only by construction: any capture buffer at
or above `training.act_cache_mmap_threshold_gb` refuses at `run`,
`validate --gsq-config`, and `assemble` time, so the pinned
allocator's disk-backed mmap branch can never be taken.