diff --git a/control-plane/src/main.rs b/control-plane/src/main.rs index e540f73a..99c6f2e0 100644 --- a/control-plane/src/main.rs +++ b/control-plane/src/main.rs @@ -69,6 +69,41 @@ async fn main() -> Result<(), Box> { println!("not eligible for serving without a full-model quality gate"); Ok(()) } + [model, action, manifest_spec, source_flag, source_dir, output_flag, destination, levels_flag, levels] + if model == "model" + && action == "ensure-sharded-streamed" + && source_flag == "--source-dir" + && output_flag == "--output" + && levels_flag == "--levels" => + { + let manifest = model::Manifest::from_spec(manifest_spec).await?; + let outcome = model_pipeline::ensure_sharded_streamed( + &manifest, Path::new(source_dir), Path::new(destination), levels.parse()?, + ).await?; + let source_bytes = manifest.files.iter().try_fold(0u64, |sum, file| sum.checked_add(file.size)) + .ok_or("sharded source byte count overflow")?; + match outcome { + model_pipeline::PreviewOutcome::Created(result) => { + println!("created and verified experimental streamed KAR-2: {destination}"); + println!("archive bytes: {}", result.archive.archive_bytes); + println!("source GGUF bytes: {source_bytes}"); + println!("archive/source ratio: {:.6}", result.archive.archive_bytes as f64 / source_bytes as f64); + println!("accounted bits per weight: {:.6}", result.archive.accounted_bpw); + println!("archive SHA-256: {}", hex::encode(result.archive_digest)); + println!("maximum tensor relative L2: {:.8}", result.maximum_relative_l2); + } + model_pipeline::PreviewOutcome::Reused(summary) => { + println!("reused verified experimental streamed KAR-2: {destination}"); + println!("archive bytes: {}", summary.archive_bytes); + println!("source GGUF bytes: {source_bytes}"); + println!("archive/source ratio: {:.6}", summary.archive_bytes as f64 / source_bytes as f64); + println!("accounted bits per weight: {:.6}", summary.accounted_bpw); + println!("tensors: {}", summary.tensor_count); + } + } + println!("not eligible for serving without a full-model quality gate"); + Ok(()) + } [model, action, path] if model == "model" && action == "inspect" => { let summary = gguf::inspect(Path::new(path))?; println!("GGUF v3: {path}"); @@ -248,9 +283,13 @@ async fn main() -> Result<(), Box> { let result = prepare_preview::prepare_sharded_streamed( &manifest, Path::new(source_dir), Path::new(destination), levels.parse()?, )?; + let source_bytes = manifest.files.iter().try_fold(0u64, |sum, file| sum.checked_add(file.size)) + .ok_or("sharded source byte count overflow")?; println!("EXPERIMENTAL streamed sharded GGUF-derived F32 KAR-2: {destination}"); println!("tensors: {}", result.archive.tensor_count); println!("archive bytes: {}", result.archive.archive_bytes); + println!("source GGUF bytes: {source_bytes}"); + println!("archive/source ratio: {:.6}", result.archive.archive_bytes as f64 / source_bytes as f64); println!("accounted bits per weight: {:.6}", result.archive.accounted_bpw); println!("derived F32 SHA-256: {}", hex::encode(result.archive.source_digest)); println!("archive SHA-256: {}", hex::encode(result.archive_digest)); @@ -418,7 +457,7 @@ async fn main() -> Result<(), Box> { print!("{}", include_str!("../third_party/ggml/LICENSE")); Ok(()) } - _ => Err("usage: strata-next [serve|doctor|--ggml-license|model smoke-fixture --output FILE.kar|model fetch MANIFEST --output DIR|model verify MANIFEST --output DIR|model ensure-sharded-preview MANIFEST --source-dir DIR --output FILE.kar --levels N|model inspect FILE.gguf|model index FILE.gguf [--hash]|model derive-iq3-identity FILE.gguf|model derive-mixed-identity FILE.gguf|model derive-sharded-identity MANIFEST --source-dir DIR|model prepare-f32-preview FILE.gguf --output FILE.kar --levels N|model prepare-iq3-preview FILE.gguf --output FILE.kar --levels N|model prepare-mixed-preview FILE.gguf --output FILE.kar --levels N|model prepare-sharded-preview MANIFEST --source-dir DIR --output FILE.kar --levels N|model prepare-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N|model inspect-kar FILE.kar|model checksum-kar-preview FILE.kar TENSOR LEVEL|model verify-kar FILE.kar [--manifest SPEC --source-dir DIR]|model verify-iq3-preview FILE.kar --source FILE.gguf|model verify-mixed-preview FILE.kar --source FILE.gguf|model verify-sharded-preview FILE.kar --manifest SPEC --source-dir DIR|model verify-sharded-streamed FILE.kar --manifest SPEC --source-dir DIR --levels N|model extract-tokenizer FILE.gguf --output DIR|model tokenize TOKENIZER_DIR TEXT [--special]|model tokenize TOKENIZER_DIR --stdin [--special]|model detokenize TOKENIZER_DIR IDS]".into()), + _ => Err("usage: strata-next [serve|doctor|--ggml-license|model smoke-fixture --output FILE.kar|model fetch MANIFEST --output DIR|model verify MANIFEST --output DIR|model ensure-sharded-preview MANIFEST --source-dir DIR --output FILE.kar --levels N|model ensure-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N|model inspect FILE.gguf|model index FILE.gguf [--hash]|model derive-iq3-identity FILE.gguf|model derive-mixed-identity FILE.gguf|model derive-sharded-identity MANIFEST --source-dir DIR|model prepare-f32-preview FILE.gguf --output FILE.kar --levels N|model prepare-iq3-preview FILE.gguf --output FILE.kar --levels N|model prepare-mixed-preview FILE.gguf --output FILE.kar --levels N|model prepare-sharded-preview MANIFEST --source-dir DIR --output FILE.kar --levels N|model prepare-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N|model inspect-kar FILE.kar|model checksum-kar-preview FILE.kar TENSOR LEVEL|model verify-kar FILE.kar [--manifest SPEC --source-dir DIR]|model verify-iq3-preview FILE.kar --source FILE.gguf|model verify-mixed-preview FILE.kar --source FILE.gguf|model verify-sharded-preview FILE.kar --manifest SPEC --source-dir DIR|model verify-sharded-streamed FILE.kar --manifest SPEC --source-dir DIR --levels N|model extract-tokenizer FILE.gguf --output DIR|model tokenize TOKENIZER_DIR TEXT [--special]|model tokenize TOKENIZER_DIR --stdin [--special]|model detokenize TOKENIZER_DIR IDS]".into()), } } diff --git a/control-plane/src/model_pipeline.rs b/control-plane/src/model_pipeline.rs index 63331809..3f014f44 100644 --- a/control-plane/src/model_pipeline.rs +++ b/control-plane/src/model_pipeline.rs @@ -1,5 +1,5 @@ -//! A fail-closed acquisition and preparation path for small sharded previews. -//! The full target model needs a bounded-memory encoder and a quality gate. +//! Fail-closed acquisition and preparation paths. The full-size stream is +//! experimental until real-model quality and Linux GPU loading are verified. use std::{error::Error, fs, path::Path}; @@ -55,6 +55,36 @@ pub async fn ensure_sharded_preview( } } +pub async fn ensure_sharded_streamed( + manifest: &model::Manifest, + source_dir: &Path, + archive: &Path, + levels: u8, +) -> Result> { + if !(1..=8).contains(&levels) { + return Err("streamed sharded preparation requires 1..=8 KAR levels".into()); + } + model::fetch(manifest, source_dir).await?; + model::verify(manifest, source_dir).await?; + match fs::symlink_metadata(archive) { + Ok(metadata) => { + if !metadata.is_file() || metadata.file_type().is_symlink() { + return Err("existing KAR path must be a regular file".into()); + } + let summary = + prepare_preview::verify_sharded_streamed(manifest, source_dir, archive, levels)?; + Ok(PreviewOutcome::Reused(summary)) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + let created = + prepare_preview::prepare_sharded_streamed(manifest, source_dir, archive, levels)?; + prepare_preview::verify_sharded_streamed(manifest, source_dir, archive, levels)?; + Ok(PreviewOutcome::Created(created)) + } + Err(error) => Err(error.into()), + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/docs/build-validation.md b/docs/build-validation.md index 9bd10566..f0b821cb 100644 --- a/docs/build-validation.md +++ b/docs/build-validation.md @@ -99,3 +99,4 @@ The test toolchain was installed into ignored `artifacts/tool-venv/` for this ru - Added `model prepare-sharded-preview` and `model verify-sharded-preview` for two or more manifest-pinned GGUF files under the existing 64 MiB preview limit. A two-shard synthetic Rust test passed archive preparation, provenance verification, and source-record checks; changing a pinned manifest digest caused rejection. A separate CLI smoke built and verified an archive with derived F32 SHA-256 `c407838a4718093d8f2699209c7cac8b998a47b937e5afe59e4c5a0af09033a7` and archive SHA-256 `49206cc99935dad8ddc1cb342dfc263548285e04767237c2c377c23d33a88479`. All 47 Rust tests, Clippy with warnings denied, and Linux/arm64 musl cross-check passed. No full-model conversion, CUDA decode, or throughput comparison was performed. - Added `model prepare-sharded-streamed` and `model verify-sharded-streamed`: the experimental path samples at most 65,536 normalized values per tensor, writes nested or exact RAW F32 sections from bounded GGUF chunks, checks per-tensor physical/virtual digests, rehashes each shard, assembles a KAR-2 archive, and binds a separate versioned sidecar. The two-shard F32/IQ3_S synthetic test verifies the archive, reconstructs a dense tensor on CPU, rejects a changed preparation level and pinned shard digest, and refuses to overwrite the archive. All 56 Rust tests and Clippy with warnings denied passed locally. The CLI reports actual archive bytes and accounted bits per weight. Real Qwen shards have not been converted, and no GPU quality or speed result exists. - Extended the existing static-CLI smoke script with two manifest-pinned synthetic shards. The local macOS CLI prepared and independently verified their streamed archive; its SHA-256 is pinned as `51688d265860172feca39e296f9142b1ed16800496bae4b06b4b8d8fa11d806e`. This script is wired into the Linux musl workflow, but hosted GitHub Actions has not run because this checkout has no remote repository. The fixture is synthetic and says nothing about full-model size or quality. +- Added opt-in `model ensure-sharded-streamed`: fetches pinned GGUF shards, verifies source bytes, creates and verifies the streamed archive, then rehashes and checks source/provenance/payload on reuse. A local CLI smoke created and reused the synthetic two-shard archive and rejected a rerun with a changed level without modifying the existing file. Full 83.6 GB model preparation has not run; the local checkout had about 121 GiB free at this step, insufficient for source plus staged and final archive. No full-model size is reported yet. diff --git a/docs/compressed-runtime.md b/docs/compressed-runtime.md index ec417480..845f8fb4 100644 --- a/docs/compressed-runtime.md +++ b/docs/compressed-runtime.md @@ -36,6 +36,8 @@ The `kar_stream_encode` primitive writes one canonical nested section directly f `model prepare-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N` now connects the bounded visitor, deterministic 65,536-value calibration sample, file-backed section encoder, and KAR-2 archive writer across manifest-pinned GGUF shards. It has no 64 MiB preview cap or per-tensor allocation. If a tensor cannot supply a valid nested calibration, it is copied as virtual F32 into a RAW section instead of being discarded. Every streamed tensor's physical and virtual digest is checked against an independently derived identity, every shard is hashed again after preparation, and the archive and separate preparation-policy provenance are published without replacing existing files. `model verify-sharded-streamed FILE.kar --manifest SPEC --source-dir DIR --levels N` independently verifies the shards, virtual tensor source records, archive payload CRCs, and sidecar binding. A small two-shard F32/IQ3_S test also reconstructs one tensor from the archive on CPU. Real target shards, rate choice, full-model quality, and the Linux GPU loader still need validation. This command is experimental and its output is not eligible for serving. +`model ensure-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N` adds the opt-in fetch → source verify → streamed prepare → archive verify sequence. On rerun it only reuses an archive after fresh shard, payload, and provenance checks; a damaged archive or changed level fails closed. The real-model size remains unmeasured. Use an external model volume or the Linux GPU host for the first full preparation. + ## 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. diff --git a/docs/model-assets.md b/docs/model-assets.md index 5c476bd5..5b82ab71 100644 --- a/docs/model-assets.md +++ b/docs/model-assets.md @@ -17,6 +17,8 @@ The executable carries its HTTPS trust roots, so `model fetch` does not need a C For small GGUF fixtures, `model ensure-sharded-preview MANIFEST --source-dir DIR --output FILE.kar --levels N` runs fetch, source verification, experimental KAR-2 preparation, and archive verification in one command. On rerun it hashes the source shards and verifies the archive payload, provenance, and level count before reusing the file. A damaged or mismatched archive fails closed and is never overwritten automatically. This preview is limited to 64 MiB of GGUF input; the embedded `qwen-iq3-s` manifest is deliberately rejected before download. It does not provide full-model conversion or an approved serving artifact. +`model ensure-sharded-streamed MANIFEST --source-dir DIR --output FILE.kar --levels N` is the opt-in, uncapped experimental path. It fetches and verifies source shards, prepares a bounded-memory KAR-2 archive if absent, independently verifies the new archive and provenance, and rehashes all sources plus the archive before reuse. It prints actual archive bytes and accounted bits per weight. Use a model volume with enough capacity for the GGUF shards, temporary sections, and final archive. The real `qwen-iq3-s` shards have not yet been processed, so no full-model output size, quality result, or serving approval is known. + `model checksum-kar-preview FILE.kar TENSOR LEVEL` independently decodes one bounded nested tensor from the archive and prints an FNV-1a checksum of the F32 output. On Linux, compare `output_fnv1a64` with the same field from `strata-kar-gpu-smoke FILE.kar TENSOR LEVEL ITERATIONS`. This checks CPU and CUDA reconstruction byte for byte on a fixture; it is not a full-model quality gate. `model smoke-fixture --output FILE.kar` writes a deterministic 32-element KAR-2 archive named `smoke.expert` and prints its archive digest and two-level CPU decode checksum. It refuses to overwrite an existing file. This model-free artifact lets the Linux CUDA decoder be checked before the target model is available. diff --git a/docs/roadmap.md b/docs/roadmap.md index 034dab79..4fcec092 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -72,6 +72,7 @@ The target interface is one Linux command, `strata-next`, with `serve`, `model f - [x] Compute a versioned virtual F32 identity across manifest-verified GGUF shards in filename order, with global tensor offsets. - [x] Encode and independently verify a small, manifest-pinned sharded KAR preview with provenance. - [x] Add an experimental sharded preparation command with bounded calibration and encoding and no preview size cap; verify its archive and provenance on synthetic F32/IQ3_S shards. +- [x] Add an opt-in full-size fetch → verify → prepare → verify CLI path that reuses only a reverified streamed archive. - [ ] Inspect real target-model tensor types, add any missing decoders, and run streamed preparation and independent verification on real shards. - [ ] Verify IQ3_S values against upstream CPU dequantization on representative tensors. - [ ] Promote the bounded GGUF/KAR path to `model prepare` after real-shard validation, per-class rate policy, and quality checks. diff --git a/scripts/smoke-static-cli.py b/scripts/smoke-static-cli.py index 2ea4aa63..4674f5cf 100644 --- a/scripts/smoke-static-cli.py +++ b/scripts/smoke-static-cli.py @@ -126,6 +126,8 @@ def main() -> None: ) assert "EXPERIMENTAL" in prepare_streamed.stdout assert "archive bytes:" in prepare_streamed.stdout + assert "source GGUF bytes:" in prepare_streamed.stdout + assert "archive/source ratio:" in prepare_streamed.stdout assert "accounted bits per weight:" in prepare_streamed.stdout assert "not eligible for serving" in prepare_streamed.stdout streamed_provenance = json.loads( @@ -144,6 +146,23 @@ def main() -> None: capture_output=True, text=True, check=True, ) assert "payload CRCs match" in verify_streamed.stdout + ensured = root / "ensured.kar" + ensure_args = [ + binary, "model", "ensure-sharded-streamed", manifest, + "--source-dir", root, "--output", ensured, "--levels", "2", + ] + first = subprocess.run(ensure_args, capture_output=True, text=True, check=True) + assert "created and verified" in first.stdout + assert "archive/source ratio:" in first.stdout + assert ensured.read_bytes() == streamed.read_bytes() + second = subprocess.run(ensure_args, capture_output=True, text=True, check=True) + assert "reused verified" in second.stdout + assert "archive/source ratio:" in second.stdout + wrong_level = subprocess.run( + ensure_args[:-1] + ["3"], capture_output=True, text=True, + ) + assert wrong_level.returncode != 0 + assert ensured.read_bytes() == streamed.read_bytes() print("static CLI mixed and streamed sharded GGUF/KAR smokes passed")