open-standards/joule-code/examples/l3-jouleclaw/tests/hybrid_l3.rs
Transaction Science a6ee726b2f joule-code L3: receipts priced by the fabric — ModelBased, end to end
The fabric arc reaches the standard's receipts. The hybrid
backend wraps every complete() in an attribution-ledger
window: GFLOPs recorded per fabric member that ACTUALLY ran
each matmul, priced by the machine's IOReport calibration.
The summed figure rides LlmResponse.energy_joules, and the
L3 stage now grades it correctly per SPEC §6:

  backend reports a figure  →  estimated_uj, ModelBased
                               (a calibrated per-operation
                               model — precisely what §6
                               defines ModelBased to mean)
  no figure                 →  catalog constant, Estimator
                               (the honest old behavior,
                               unchanged for Echo/llama.cpp)

Live, real checkpoint (Mellum2, fabric ON):

  "The capital of France is" → " Paris.  \nParis is"
  fabric-priced energy: 113.0 J (ModelBased)

A coding-cascade L3 receipt now carries energy from the
silicon that actually executed the call — attributed per
member, priced by a calibration metered on this machine —
instead of a 1.0 J placeholder. The provenance ladder is
exercised end to end: Estimator (catalog) → ModelBased
(fabric ledger × calibration) → HwShunt (a meter wrapping
the whole call, as the joule-fix harness already does).

Test updated to the new contract: Some(>0) with the fabric
on, honest None without — both branches printed.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-06-11 12:50:15 -04:00

85 lines
3 KiB
Rust

//! L3 on the **hybrid safetensors families** — NemotronH / Granite-4.0-H /
//! Falcon-H1 behind one auto-detecting backend.
//!
//! Proves the wave-57/58 surface end to end: a real Mamba2/attention hybrid,
//! loaded from its BF16 safetensors checkpoint and driven through
//! `hybrid_lm::generate`, closes a Joule Code cascade at tier L3 with a
//! content-addressed `CodingOp` and a joules-class receipt.
//!
//! `#[ignore]` because it needs a real checkpoint on disk. Run with:
//!
//! JOULE_HYBRID=/path/to/snapshot-dir \
//! cargo test -p l3-jouleclaw-binding --release --test hybrid_l3 -- --ignored --nocapture
//!
//! (defaults to the Falcon-H1-0.5B-Instruct snapshot on the external drive —
//! the smallest of the hybrid set.)
use joule_code_cascade::{Cascade, L0Cache, Request, Stage};
use joule_code_schema::Tier;
use jouleclaw_llm::{LlmBackend, LlmRequest};
use l3_jouleclaw_binding::hybrid_backend::HybridBackend;
use l3_jouleclaw_binding::jouleclaw_l3_stage;
fn snapshot_dir() -> String {
std::env::var("JOULE_HYBRID").unwrap_or_else(|_| {
"/Volumes/macos_4TB_external_1/hf_hub/falcon-h1-0.5b-instruct".to_string()
})
}
#[test]
#[ignore]
fn hybrid_backend_completes_in_process() {
let backend = HybridBackend::from_dir(snapshot_dir()).expect("load hybrid checkpoint");
eprintln!("model: {}", backend.model_name());
let mut req = LlmRequest::from_prompt("The capital of France is", 6);
req.temperature = 0.0; // greedy — deterministic
let resp = backend.complete(&req).expect("complete");
eprintln!(
" in={} out={} text={:?}",
resp.input_tokens, resp.output_tokens, resp.text
);
assert!(resp.input_tokens > 0, "prompt tokenized");
assert!(resp.output_tokens > 0, "tokens generated");
assert!(
resp.text.contains("Paris"),
"smoke answer should mention Paris, got {:?}",
resp.text
);
// Energy contract: with the fabric on and calibrated,
// the call reports a ModelBased figure from the
// attribution ledger; otherwise honest None.
match resp.energy_joules {
Some(j) => {
assert!(j > 0.0);
eprintln!(" fabric-priced energy: {j:.1} J (ModelBased)");
}
None => eprintln!(" energy: None (fabric off — honest)"),
}
}
#[test]
#[ignore]
fn hybrid_backend_closes_the_cascade_at_l3() {
let backend = HybridBackend::from_dir(snapshot_dir()).expect("load hybrid checkpoint");
let cascade = Cascade::new([
Box::new(L0Cache::new()) as Box<dyn Stage>,
Box::new(jouleclaw_l3_stage(Box::new(backend))),
]);
let req = Request {
prior: "tree-hash".into(),
intent: "Summarize: the capital of France is".into(),
};
let res = cascade.resolve(&req).expect("L3 closes");
assert_eq!(res.receipt.tier, Tier::L3);
let op = res.op.expect("coding op produced");
assert_eq!(op.tier, Tier::L3);
assert!(!op.edits.is_empty());
eprintln!(
" op_id={} estimated_uj={}",
op.op_id().unwrap(),
res.receipt.energy.estimated_uj
);
}