Performance Benchmarks / Run Benchmarks (push) Canceled after 0s
CI / Format Check (push) Canceled after 0s
CI / Clippy Check (push) Canceled after 0s
CI / Build (macos-latest) (push) Canceled after 0s
CI / Build (ubuntu-latest) (push) Canceled after 0s
CI / Test (macos-latest) (push) Canceled after 0s
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Build CPU-Only (Explicit) (push) Canceled after 0s
CI / Python Bindings (maturin) (macos-latest) (push) Canceled after 0s
CI / Python Bindings (maturin) (ubuntu-latest) (push) Canceled after 0s
CI / WASM Build + Size Check (push) Canceled after 0s
CI / Distributed Training Tests (push) Canceled after 0s
CI / CI Success (push) Canceled after 0s
Documentation / Build API Documentation (push) Canceled after 0s
Documentation / Build User Guide (push) Canceled after 0s
SparseAutoencoder::new_seeded draws encoder/decoder weights via randn_seeded with per-tensor SplitMix64-derived seeds (same derivation as MambaBlock::new_seeded), so identical (config, seed) gives bit-exact SAEs — without it, cross-instance loss comparisons are noise (measured downstream: 0.09 vs 0.65 starts on identical data). SAETrainer::reinitialize_neuron couples the encoder-unit weight reinit with zeroing that unit's optimizer moment rows (encoder row, bias slot, decoder column), so external generate-and-test callers can't reset weights while leaving optimizer state stale — previously only the trainer's internal dead-neuron resampling did both. Note train_step's update rule is plain SGD today, so the moment reset is inert until the Adam path is switched on; the coupling is the contract either way, and a doctored-checkpoint test pins the row/column semantics. Also drops a vacuous assert!(true) smoke test that failed clippy's assertions_on_constants. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B1feFAQxjbCRHePUdxuNra
97 lines
3.4 KiB
Rust
97 lines
3.4 KiB
Rust
//! # RTX Interpret - Model Interpretability Library
|
|
//!
|
|
//! GPU-accelerated model interpretability and explainability toolkit for RustyTorch++.
|
|
//! This crate provides PyTorch Captum-equivalent functionality for understanding
|
|
//! and interpreting neural network predictions.
|
|
//!
|
|
//! ## Features
|
|
//!
|
|
//! - **Gradient-based Attribution**: Saliency maps, Integrated Gradients, GradCAM
|
|
//! - **Perturbation-based Attribution**: Occlusion, Feature Ablation, LIME
|
|
//! - **Layer Attribution**: Layer Conductance, Internal Influence
|
|
//! - **Neuron Attribution**: Neuron activation analysis, TCAV (Testing with Concept Activation Vectors)
|
|
//! - **Attention Analysis**: Attention Rollout, Attention Flow, Head Importance
|
|
//!
|
|
//! ## Architecture
|
|
//!
|
|
//! The library is organized around core traits:
|
|
//! - `Attribution`: Methods that compute feature importance scores
|
|
//! - `Perturbation`: Methods based on input modifications
|
|
//! - `Neuron`: Methods for neuron-level interpretability including TCAV
|
|
//! - `Attention`: Methods for analyzing transformer attention mechanisms
|
|
//!
|
|
//! ## Example
|
|
//!
|
|
//! ```rust
|
|
//! use rtx_interpret::{Attribution, Saliency, SaliencyConfig};
|
|
//! use rtx_tensor::{Tensor, Device};
|
|
//!
|
|
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
//! let device = Device::cpu();
|
|
//! let input = Tensor::randn(&[1, 3, 224, 224], &device)?;
|
|
//!
|
|
//! // Define your model's forward function
|
|
//! let forward_fn = |x: &Tensor| -> rtx_tensor::Result<Tensor> {
|
|
//! // Your model forward pass here
|
|
//! Ok(x.clone()) // Placeholder
|
|
//! };
|
|
//!
|
|
//! // Compute saliency map
|
|
//! let config = SaliencyConfig::default();
|
|
//! let saliency = Saliency::new(config);
|
|
//! let attributions = saliency.attribute(&forward_fn, &input, Some(0))?;
|
|
//!
|
|
//! println!("Attribution shape: {:?}", attributions.attributions().shape());
|
|
//! # Ok(())
|
|
//! # }
|
|
//! ```
|
|
|
|
#![allow(clippy::module_name_repetitions)]
|
|
|
|
pub mod attention;
|
|
pub mod attribution;
|
|
pub mod error;
|
|
pub mod neuron;
|
|
pub mod perturbation;
|
|
pub mod sae;
|
|
pub mod types;
|
|
|
|
// Re-export main types
|
|
pub use attention::{
|
|
AttentionFlow, AttentionFlowConfig, AttentionRollout, AttentionRolloutConfig, HeadFusion,
|
|
HeadImportance, HeadImportanceConfig, HeadImportanceMethod,
|
|
};
|
|
pub use attribution::{
|
|
Attribution, DeepLIFT, DeepLIFTConfig, GradCAM, GradCAMConfig, IntegratedGradients,
|
|
IntegratedGradientsConfig, LRP, LRPConfig, LRPRule, Saliency, SaliencyConfig,
|
|
};
|
|
pub use error::{InterpretError, Result};
|
|
pub use neuron::{ChannelActivation, ConceptActivationVector, MaxActivation, TCAV, TCAVConfig};
|
|
pub use perturbation::{
|
|
FeatureAblation, FeatureAblationConfig, FeatureSelection, LIME, LIMEConfig, Occlusion,
|
|
OcclusionConfig, Perturbation, SHAP, SHAPConfig,
|
|
};
|
|
pub use sae::{
|
|
ActivationHook, ActivationHookConfig, FeatureAnalyzer, FeatureImportance, FeatureStats,
|
|
HookHandle, ImportanceMethod, LayerHooks, SAEConfig, SAEStats, SAETrainer, SAETrainerConfig,
|
|
SparseAutoencoder, SparsityStats, SparsityType, TopActivation, TrainingHistory,
|
|
};
|
|
pub use types::{AttributionMetadata, AttributionOutput, Baseline, PerturbationOutput};
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_error_types_accessible() {
|
|
let err = InterpretError::tensor("test");
|
|
assert!(err.to_string().contains("test"));
|
|
}
|
|
|
|
#[test]
|
|
fn test_baseline_enum_accessible() {
|
|
let baseline = Baseline::Zero;
|
|
assert!(matches!(baseline, Baseline::Zero));
|
|
}
|
|
}
|