Adds `ig_diag` binary under ml-explainability/src/bin/ that runs
Integrated Gradients on a trained DQN safetensors checkpoint and
writes a per-feature attribution report as JSON. Designed for offline
model inspection, not the inference hot path.
Forward target: mean(Q[direction, 0..4]), which simplifies to V(s)
under the dueling identity (mean of centered advantages is zero by
the identifiability constraint). Smooth, no argmax discontinuity,
well-posed for IG.
Regime-head handling: loads only the `trending__`-prefixed weights
(matches RegimeConditionalDQN::load_from_merged_safetensors). Full
multi-head attribution is a future extension.
CLI args:
--checkpoint PATH safetensors file
--states auto|PATH `auto` samples from test_data/feature-cache/
*.fxcache; otherwise a JSON file containing
{ "states": [[f32; STATE_DIM], ...] }
--feature-names PATH JSON array of STATE_DIM names (optional)
--num-steps N IG Riemann steps (default 50)
--output PATH output JSON (default ig_report.json)
--auto-samples N fxcache sample count (default 16)
--seed N LCG seed for reproducibility (default 42)
Output JSON schema:
{
"schema_version": 1,
"checkpoint", "num_steps", "state_dim", "num_states",
"forward_target", "regime_head",
"features": [ { "name", "mean_abs", "stddev", "mean_signed" } ],
"top_10_by_mean_abs": [...],
"completeness": {
"worst_relative_error": f64,
"per_state": [ { "state_idx", "sum_attributions",
"f_input_minus_f_baseline", "relative_error" } ]
}
}
NoisyNet is disabled on both the Q-network and target network before
IG runs so attributions are deterministic (same checkpoint + states +
num_steps = bit-identical attributions).
Gated behind the `ig-diag-cli` Cargo feature (optional Cargo binary
feature, not a runtime flag) because the binary depends on ml-dqn.
Cannot depend on `ml` due to a cyclic dependency (ml already depends
on ml-explainability). To avoid pulling in the heavy `ml` crate, the
fxcache header parser is reimplemented inline in the binary (~80 LOC,
matches ml/src/fxcache.rs byte-for-byte for v4 files).
Tests:
- tests/ig_diag_cli_integration.rs: end-to-end test that builds a
DQN, saves a checkpoint, writes a states JSON, invokes the binary
via std::process::Command, parses the output, asserts schema +
completeness axiom (worst relative error < 5%). Gated with
#[cfg(all(feature = "cuda", feature = "ig-diag-cli"))] + #[ignore]
because it needs CUDA + the binary pre-built.
Build/run:
cargo build --release -p ml-explainability \
--features ig-diag-cli --bin ig_diag
cargo test -p ml-explainability --features ig-diag-cli \
--test ig_diag_cli_integration -- --ignored --nocapture
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
63 lines
2.1 KiB
TOML
63 lines
2.1 KiB
TOML
[package]
|
|
name = "ml-explainability"
|
|
version.workspace = true
|
|
edition.workspace = true
|
|
rust-version.workspace = true
|
|
authors.workspace = true
|
|
license.workspace = true
|
|
repository.workspace = true
|
|
homepage.workspace = true
|
|
documentation.workspace = true
|
|
publish.workspace = true
|
|
keywords.workspace = true
|
|
categories.workspace = true
|
|
description = "Model explainability (integrated gradients) for Foxhunt ML"
|
|
|
|
[features]
|
|
default = ["cuda"]
|
|
cuda = ["ml-core/cuda", "cudarc"]
|
|
# Standalone diagnostic binary. Gated with an optional Cargo feature so the
|
|
# library target stays minimal; enabling `ig-diag-cli` pulls in ml-dqn +
|
|
# argument parsing + JSON serialization for the `ig_diag` bin target.
|
|
ig-diag-cli = [
|
|
"cuda",
|
|
"dep:ml-dqn",
|
|
"dep:clap",
|
|
"dep:serde_json",
|
|
"dep:anyhow",
|
|
"dep:tracing",
|
|
"dep:tracing-subscriber",
|
|
]
|
|
|
|
[dependencies]
|
|
ml-core = { path = "../ml-core", default-features = false }
|
|
cudarc = { version = "0.19", optional = true, default-features = false, features = ["driver", "dynamic-linking", "std", "cuda-version-from-build-system"] }
|
|
|
|
# ig_diag binary deps (optional — only compiled with `--features ig-diag-cli`).
|
|
# NOTE: Cannot depend on `ml` here — it would create a cyclic dependency
|
|
# (ml already depends on ml-explainability). The CLI reads fxcache files
|
|
# directly (header parsing is trivial, see src/bin/ig_diag.rs).
|
|
ml-dqn = { path = "../ml-dqn", optional = true }
|
|
clap = { workspace = true, optional = true }
|
|
serde_json = { workspace = true, optional = true }
|
|
anyhow = { workspace = true, optional = true }
|
|
tracing = { workspace = true, optional = true }
|
|
tracing-subscriber = { workspace = true, optional = true }
|
|
|
|
[dev-dependencies]
|
|
tempfile = "3"
|
|
# The ig_diag CLI integration test drives the binary end-to-end: it needs
|
|
# ml-dqn + safetensors + serde_json to build a checkpoint and parse the
|
|
# report. These are only used by tests (no library impact).
|
|
ml-dqn = { path = "../ml-dqn" }
|
|
safetensors = "0.7"
|
|
serde_json = { workspace = true }
|
|
|
|
[[bin]]
|
|
name = "ig_diag"
|
|
path = "src/bin/ig_diag.rs"
|
|
required-features = ["ig-diag-cli"]
|
|
|
|
[lints]
|
|
workspace = true
|