open-standards/joule-code/examples/l3-jouleclaw/tests/common/mod.rs
Transaction Science f521fb17be
Some checks failed
joule-code conformance gate / Build + round-trip C1-C4 vectors (pull_request) Has been cancelled
jouleclaw public surface must stay documented / Public-surface audit (jouleclaw-rs) (pull_request) Has been cancelled
no-vanity-metrics / Scan committed Markdown for vanity metrics (pull_request) Has been cancelled
standards workspaces build + test / jouleclaw-rs (pull_request) Has been cancelled
standards workspaces build + test / aac-rs (pull_request) Has been cancelled
standards workspaces build + test / arl-rs (pull_request) Has been cancelled
standards workspaces build + test / eoc-rs (pull_request) Has been cancelled
standards workspaces build + test / jcp-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-code-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-ui-rs (pull_request) Has been cancelled
standards workspaces build + test / joulecontract-rs (pull_request) Has been cancelled
standards workspaces build + test / jouledesk-rs (pull_request) Has been cancelled
standards workspaces build + test / joulehook-rs (pull_request) Has been cancelled
standards workspaces build + test / map-rs (pull_request) Has been cancelled
standards workspaces build + test / mesh-rs (pull_request) Has been cancelled
standards workspaces build + test / openpay (pull_request) Has been cancelled
standards workspaces build + test / proof-rs (pull_request) Has been cancelled
standards workspaces build + test / sandbox-rs (pull_request) Has been cancelled
standards workspaces build + test / sense-rs (pull_request) Has been cancelled
standards workspaces build + test / smart-byte-rs (pull_request) Has been cancelled
standards workspaces build + test / macOS energy backend (jcp-energy) (pull_request) Has been cancelled
l3-jouleclaw: a fabric-priced projection is graded by its weakest input
The L3 stage tagged every per-call figure a backend reported as ModelBased, on
the premise that it was "priced by THIS machine's IOReport calibration". It is
priced by whatever matmul figure each fabric member loaded: a calibration row at
the grade its writer declared, or a seeded figure, and the seeded GPU figures are
`Estimated` (a coarse model). A projection priced from an estimate was sealed as
a calibrated per-operation model. Joule-code SPEC §6 keeps those apart:
ModelBased is "computed from a calibrated per-operation model", Estimator "a
coarse constant estimator".

The fix needs each input's grade, which the ledger did not carry.

- jouleclaw-loader-gguf: `LedgerEntry` gains `projected_grade`, the
  `EnergyProvenance` of the cost row that priced `projected_uj`, taken from the
  same row, so the two are present or absent together. Additive; nothing else
  constructs the struct.
- l3-jouleclaw: `projection_grade(window)` grades a projection by its weakest
  input. ModelBased only when every member that ran was priced by a `Measured`
  or `Benchmarked` figure; Estimator when any was priced by an `Estimated`
  figure, when any ran unpriced (its work is missing from the sum), or when the
  window is empty (a figure no priced input accounts for). A projection is never
  a reading, so it is never HwShunt. The stage now opens the attribution window
  around the call itself (reset before, snapshot after), so a figure is graded
  by this call's inputs and not by entries an earlier call left behind.

Tests. The grading table covers Measured, Benchmarked, both, Estimated alone and
beside each calibrated grade, an unpriced member, and no input. In the stage, a
backend that runs a real matmul through the dispatcher with the fabric off, and
one that runs nothing, both report 0.25 J and get Estimator (main: ModelBased).
Two integration tests, each its own process, switch the fabric on with
`JOULECLAW_FABRIC=1` and point `JOULECLAW_FABRIC_CALIBRATION` at a file they
write, pricing `local/cpu-neon`'s matmul at 2.5e8 µJ/GFLOP: declared `Measured`,
the window carries `Measured` and the receipt says ModelBased, 1024 µJ;
declared `Estimated`, the window carries `Estimated` and the receipt says
Estimator, 1024 µJ. The machine's own ~/.jouleclaw/fabric-calibration.json is
neither read nor written by these tests.

Ran: `cargo test` in l3-jouleclaw (the model-backed tests stay #[ignore], no
model was loaded), `cargo test -p jouleclaw-loader-gguf --features fabric-meter
--lib` (304 passed), loader-gguf examples checked; clippy reports nothing new.

Not done: a projection under 1 µJ still truncates to a 0 µJ receipt figure, as
before; real hybrid-model calls are far above that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-30 20:48:47 -04:00

65 lines
3 KiB
Rust

//! Shared by the fabric-grade tests: run the L3 stage, with the fabric ON in this
//! test process and priced from a calibration file the test writes (never the
//! machine's own `~/.jouleclaw/fabric-calibration.json`), over a backend that
//! dispatches one real matmul through the fabric and reports the ledger's
//! projection, as the hybrid backend does.
use joule_code_cascade::{Request, Stage};
use joule_code_receipt::Energy;
use jouleclaw_fabric::EnergyProvenance;
use jouleclaw_llm::{FinishReason, LlmBackend, LlmError, LlmRequest, LlmResponse};
use jouleclaw_loader_gguf::fabric_dispatch::{ledger_reset, ledger_snapshot, matmul_bf16_auto, LedgerEntry};
/// One decode-shaped matmul (m = 1, a 4 KiB weight): the fabric places it on the
/// CPU SIMD member, `local/cpu-neon`.
struct OneMatmul;
impl LlmBackend for OneMatmul {
fn model_name(&self) -> &str {
"one-matmul"
}
fn complete(&self, _request: &LlmRequest) -> Result<LlmResponse, LlmError> {
let (m, n, k) = (1usize, 32usize, 64usize);
let a = vec![0.5f32; m * k];
let w = vec![0u8; 2 * n * k];
let mut out = vec![0f32; m * n];
ledger_reset();
matmul_bf16_auto(&a, &w, &[], m, n, k, &mut out);
let projected_j: f64 = ledger_snapshot().iter().filter_map(|e| e.projected_uj).sum::<f64>() / 1e6;
Ok(LlmResponse {
text: "fn hello() {}".into(),
finish_reason: FinishReason::Stop,
input_tokens: 1,
output_tokens: 1,
energy_joules: (projected_j > 0.0).then_some(projected_j),
})
}
fn typical_joules_per_call(&self) -> f64 {
1.0
}
}
/// Write a calibration file pricing `local/cpu-neon`'s matmul at `grade` (or
/// declaring none), switch the fabric on for this process, resolve one L3
/// request, and return its receipt energy with the ledger window it was priced
/// from. Call once per test process: the fabric is discovered once.
pub fn resolve_priced_at(tag: &str, grade: Option<EnergyProvenance>) -> (Energy, Vec<LedgerEntry>) {
let path = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("l3-fabric-grade-{tag}.json"));
let declared = grade.map(|g| format!(r#","provenance":"{g:?}""#)).unwrap_or_default();
// Priced high enough that the one 4.1e-6 GFLOP matmul projects to ~1 mJ, a
// whole number of µJ on the receipt.
let row = format!(
r#"[{{"member_id":"local/cpu-neon","op":"matmul","uj_per_gflop":250000000.0,"method":"test row"{declared}}}]"#
);
std::fs::write(&path, row).expect("write the test calibration file");
std::env::set_var("JOULECLAW_FABRIC_CALIBRATION", &path);
std::env::set_var("JOULECLAW_FABRIC", "1");
let stage = l3_jouleclaw_binding::jouleclaw_l3_stage(Box::new(OneMatmul));
let res = stage
.try_resolve(&Request { prior: "b3:AAAA".into(), intent: "write a hello function".into() })
.expect("L3 resolved");
let window = ledger_snapshot();
let _ = std::fs::remove_file(&path);
(res.receipt.energy, window)
}