Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

examples/potion_e2e.rs

//! **End-to-end proof for the static-table backend: fetch → load → index → search.**
//!
//! `Backend::StaticTable` and `StaticEmbedder` shipped with unit tests only. Unit tests
//! prove the safetensors reader refuses malformed input and that pooling skips
//! out-of-vocab ids; they prove NOTHING about whether the real published weights load,
//! whether 256-dim vectors survive the warehouse round trip, or whether a search over
//! them returns anything a human would call correct. This closes that gap.
//!
//! It drives the SHIPPED path, not a private one: `select::load()` (so the model→backend
//! routing is exercised, not bypassed), then `store::index_repo` and `store::search` —
//! the same two functions `nornir vector index` and `nornir vector search` call.
//!
//! ## What a PASS actually establishes
//!
//! 1. `$NORNIR_EMBED_MODEL=potion-base-8m` routes to [`Backend::StaticTable`] — that
//!    `model_backend` outranks the hardware probe in the real selector, not just in a
//!    unit test with an injected kind.
//! 2. The published `model.safetensors` parses and its shape matches the registry's
//!    declared `dim` (the mismatch guard in `StaticEmbedder::load` is the one thing
//!    standing between a wrong table and vectors the warehouse would reject far away
//!    from the cause).
//! 3. 256-dim vectors round-trip through Iceberg and rebuild into a searchable index —
//!    the claim "the engine is fully dim-parametric" tested rather than asserted.
//! 4. Retrieval is not noise: a natural-language query about a known file ranks that
//!    file first. A static table has no attention, so this is exactly where it could
//!    fail while everything else stayed green.
//!
//! ## What a pass does NOT establish
//!
//! That it is good enough to REPLACE jina. Ranking here is scale-free; the duplicates
//! lens gates on an ABSOLUTE `cosine >= 0.92` calibrated for jina, and this says nothing
//! about that constant. Treat a pass as "the backend works", not "ship it".
//!
//! ```text
//! NORNIR_EMBED_MODEL=potion-base-8m cargo run --release --example potion_e2e
//! ```

#[cfg(not(all(feature = "embed-tract", feature = "embed-ort")))]
fn main() {
    eprintln!("potion_e2e needs the default backends: --features embed-tract,embed-ort");
    std::process::exit(2);
}

#[cfg(all(feature = "embed-tract", feature = "embed-ort"))]
fn main() -> anyhow::Result<()> {
    use nornir::vector::chunk::ChunkOptions;
    use nornir::vector::embed_registry as reg;
    use nornir::vector::embed_support as es;
    use nornir::vector::select::{self, Backend};
    use nornir::vector::store::{self, RepoRef};
    use nornir::warehouse::iceberg::IcebergWarehouse;
    use std::time::Instant;

    let model = es::active_model();
    println!(
        "model      : {} ({:?}, {} dims)",
        model.id, model.kind, model.dim
    );
    anyhow::ensure!(
        model.kind == reg::ModelKind::StaticTable,
        "this example must run with a STATIC model — set NORNIR_EMBED_MODEL=potion-base-8m \
         (selected: `{}`, which is {:?})",
        model.id,
        model.kind
    );

    // (1) The SHIPPED routing decides, not the example.
    let backend = select::chosen_backend();
    println!("backend    : {}", backend.label());
    anyhow::ensure!(
        backend == Backend::StaticTable,
        "model kind did not outrank the hardware probe — got {backend:?}"
    );

    // (2) Real published weights.
    let t = Instant::now();
    let embedder = select::load()?;
    let load_ms = t.elapsed().as_secs_f64() * 1e3;
    let profile = embedder.profile();
    println!(
        "loaded     : {load_ms:.0} ms · dim {} · profile {}",
        profile.dim,
        &profile.id()[..16]
    );
    anyhow::ensure!(profile.dim == model.dim, "profile dim != registry dim");

    // ── corpus: this repo's own vector module, real code with real doc comments ──
    let mut files: Vec<(String, String)> = Vec::new();
    for e in std::fs::read_dir("src/vector")?.flatten() {
        let p = e.path();
        if p.extension().is_some_and(|x| x == "rs") {
            let name = p.to_string_lossy().into_owned();
            if let Ok(txt) = std::fs::read_to_string(&p) {
                files.push((name, txt));
            }
        }
    }
    files.sort();
    anyhow::ensure!(!files.is_empty(), "no sources found under src/vector");
    let bytes: usize = files.iter().map(|(_, t)| t.len()).sum();
    println!("corpus     : {} files / {} KiB", files.len(), bytes / 1024);

    // (3) Index into a scratch warehouse through the SHIPPED write path.
    let dir = tempfile::tempdir()?;
    let wh = IcebergWarehouse::open(&dir.path().join("wh"))?;
    let r = RepoRef {
        workspace: "potion-e2e",
        repo: "nornir",
        git_sha: "0000000000000000000000000000000000000000",
        branch: "main",
        complete: true,
    };
    let t = Instant::now();
    let snap = store::index_repo(&wh, &r, &files, &ChunkOptions::default(), embedder.as_ref())?;
    let index_s = t.elapsed().as_secs_f64();
    println!(
        "indexed    : {index_s:.2}s · {:.1} KiB/s · snapshot {}",
        (bytes as f64 / 1024.0) / index_s,
        &format!("{snap:?}").chars().take(48).collect::<String>()
    );

    // (4) Retrieval that a static table could plausibly fail.
    let queries: &[(&str, &str)] = &[
        (
            "reciprocal rank fusion is not here, but: a static token table with no transformer",
            "embed_static.rs",
        ),
        (
            "discover the AMD ROCm runtime libraries and probe the GPU",
            "rocm.rs",
        ),
        (
            "split a source file into overlapping line windows",
            "chunk.rs",
        ),
        (
            "say out loud why embedding fell back to the CPU",
            "fallback.rs",
        ),
    ];
    let mut top1 = 0usize;
    println!("\nquery → top hit                                    rank1  cos");
    for (q, want) in queries {
        let qv = embedder.embed(&[q])?;
        let hits = store::search(&wh, r.repo, Some(r.git_sha), &profile.id(), &qv[0], 5)?;
        let best = hits.first();
        let hit_file = best.map(|(_, o)| o.file.clone()).unwrap_or_default();
        let ok = hit_file.ends_with(want);
        if ok {
            top1 += 1;
        }
        println!(
            "  {:<46}  {:<5}  {:.3}  → {}",
            &q[..q.len().min(46)],
            if ok { "HIT" } else { "miss" },
            best.map(|(s, _)| *s).unwrap_or(0.0),
            hit_file.rsplit('/').next().unwrap_or("-"),
        );
    }
    println!(
        "\nrecall@1   : {top1}/{} on the labelled probes",
        queries.len()
    );

    // A static table with no context is EXPECTED to be weaker than jina, so this floor
    // is deliberately low — it is a smoke test for "retrieval works at all", not a
    // quality gate. Zero, though, means the vectors carry no usable signal.
    anyhow::ensure!(
        top1 > 0,
        "NO labelled query ranked its own file first — the static vectors carry no \
         usable signal through the warehouse round trip"
    );
    println!("\nPASS — static-table backend works end to end (fetch → load → index → search).");
    println!("NOTE  ranking is scale-free; this says NOTHING about the duplicates lens's");
    println!("      absolute cosine >= 0.92 gate, which is still jina-calibrated.");
    Ok(())
}