Files
leleka/docs/compressed-runtime.md
T

8.1 KiB

Compressed model runtime contract

Artifact pipeline

strata-next model fetch downloads the pinned source shards to an external volume. model verify checks their size and SHA-256 before any conversion. The planned model prepare --format kar2 step will build a KAR-2 archive and a small manifest containing the source digest, archive digest, codec version, per-class rate policy, tokenizer identity, and completion state. It must write to a staging path and publish the finished archive atomically. The final one-command serve flow should run missing fetch/verify/prepare stages automatically, reuse a verified prepared archive on subsequent starts, and never treat an incomplete stage as usable. Neither source shards nor the archive belong in Git or CI artifacts.

The first source profile is Qwen3.8-Flash-Next IQ3_S, which is already quantized. KAR transcoding of it is a lossy experiment until full-model quality is measured. A higher-fidelity source may be needed if the IQ3_S → KAR path exceeds the error budget. Keep the verified native IQ3_S pack as the control arm and recovery path; never infer model quality from archive size or isolated tensor error alone.

KAR-2, as specified by the local koference project at commit 806d2398ae1daaceb2917242911e5680cbfb3608 (r08/docs/format-kar-v2.md, r08/code/libkar.h), is one archive per model with a source digest, directory digest, payload checks, and per-tensor precision levels. The Rust command should call a validated native encoder or a stable libkar API; Python may be used for offline research, calibration, and differential tests, but not by production serving. The decoder must reject an archive whose identity, bounds, checksums, or tensor layout do not match the expected model.

The current model inspect-kar command performs bounded structural checks, header CRC-32, directory SHA-256, source identity reporting, and tensor section checks. model verify-kar additionally scans every tensor section and checks its payload CRC-32 with bounded memory. With --manifest SPEC --source-dir DIR, it verifies each source shard's size and SHA-256, then hashes the shards in filename order and compares the concatenated digest and byte count with the archive header. Padding semantics and decode accuracy are still unchecked, so these commands are early rejection tools rather than the final loader gate.

The Rust kar_writer module now emits a KAR-2 container from already coded RAW/NESTED tensor slices. A synthetic two-tensor output matches the pinned koference reference writer byte for byte, including header, directory, aligned payload, CRCs, and digest. The writer verifies its temporary file before atomic no-replace publication and rejects non-finite f16 scales/books. It is an internal building block: it does not encode GGUF weights, choose compression levels, or stream a whole model tensor by tensor yet.

strata-next model index FILE.gguf [--hash] prints one JSON record per tensor with checked absolute offset, exact source byte count, type, and dimensions. --hash adds a SHA-256 computed with a fixed 1 MiB buffer. The index uses Strata's pinned GGUF block geometry, including IQ3_S, and rejects unknown types, overlapping spans, truncated payloads, and rows that do not align to quantization blocks. The KAR writer can stream both RAW tensor bytes and pre-encoded NESTED sections from files through a 1 MiB buffer. It checks the RAW source digest or NESTED section digest; the latter also checks finite f16 scales/books and zero alignment padding. This is input preparation for a streamed converter; quantized tensors still need a native encoder.

An experimental native kar_codec::encode_with_boundaries converts one in-memory f32 tensor to group scales and MSB-first bit planes using nested boundaries. kar_codec::fit_topdown fits those boundaries and f16 books to an already selected normalized sample; pinned synthetic fixtures match koference's Python reference. It rounds scales to IEEE f16 before assigning cells and uses the same bit-within-u32 order as koference's CUDA decoder. kar_codec::decode_range reconstructs a selected prefix into a caller-sized CPU buffer, giving a small numerical oracle for later CUDA checks. The encoder does not decode IQ3_S source blocks, choose a model rate, select a representative sample, or enable a lossy path for serving. The full converter still needs bounded per-tensor encoding and calibration provenance.

strata-next model prepare-f32-preview FILE.gguf --output FILE.kar --levels N connects the GGUF index, native calibration, encoder, CPU reconstruction check, and verified KAR writer. It accepts only a single F32 GGUF no larger than 64 MiB, at most 1000 tensors, with at most 1 million elements per tensor. Each encoded tensor is written to a private staging section and released from memory before the next tensor; the archive writer reads those sections with a 1 MiB buffer. Staging is cleaned after success or failure. It fits each tensor from all normalized values, records the rate in the KAR builder identity, and prints source/archive SHA-256 plus the maximum tensor relative L2. This command is an explicitly lossy integration preview. IQ3_S input is rejected, and no output from this command is eligible for serving until the full-model quality gate passes.

The Rust iq3_s module now decodes one GGUF IQ3_S block (110 bytes, 256 F32 outputs) or a caller-sized sequence of complete blocks. Its 512-entry grid is copied from the pinned Strata/ggml tree, checked by SHA-256, and carries the ggml MIT notice in the binary through --ggml-license. Synthetic block output matches an independent Python calculation. Real tensor parity still needs the upstream CPU oracle and real-model fixtures.

KAR-2 records source_dtype only as F32, F16, or BF16. An IQ3_S GGUF cannot be identified directly as a KAR-2 tensor source without violating that field. The IQ3_S → F32 derivation must therefore have its own digest and provenance record, with the original GGUF shard digests linked separately, or the archive format must be versioned to represent quantized source types. The preview continues to reject IQ3_S until that identity contract is implemented; it must never label IQ3_S bytes as F32 source bytes.

GPU path

For the target compressed runtime, copy the selected KAR data to VRAM in compressed form. Keep at least the working layer and selected experts decoded or decode them into a bounded scratch buffer on a CUDA stream before multiply. Use kar_decode_rows() for sparse per-layer embedding lookups. The scheduler must account for archive residency, precision level, transfers, decode, and multiply, and measure cold misses separately from warm expert-cache hits. The archive format and decoded numerical values must be compared with the koference CPU oracle before an end-to-end speed claim.

The user-facing Linux command checks the installed NVIDIA driver and starts the native engine. It does not carry the driver. The single-file GPU packaging goal still needs a real Linux CUDA build and loader test.

Cache profiles

Profile Attention state Status
with-key K and V cache, using the selected baseline KV format Control profile for quality and performance
without-key Value-Only cache with a validated Wq_keyless transform; separate QSA indexer keys remain Experimental until the Keyless quality gate passes

Record the model digest, archive digest, cache profile, KV format, context length, slot count, and hardware with every comparison. Test without-key on held-out activation traces and full 256K/512K agent workloads; report memory savings, quality, and throughput separately. A cache profile cannot silently change when an archive is selected.

Test-runtime switch

The testing runtime can first receive shadow traffic after a real Linux GPU smoke test proves model loading and a complete text response. Replacing the current test endpoint requires live Responses streaming, cancellation and tool-call integrity, sanitized Codex replay, a verified compressed GPU path, both cache profiles labeled accurately, and a one-step rollback to the current runtime. without-key can be tested separately before it is eligible as a default.