candle_mi/error.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Error types for candle-mi.
4
5/// Errors that can occur during MI operations.
6///
7/// This enum is `#[non_exhaustive]`: new variants will be added in future
8/// releases as new backends and capabilities are added.
9#[non_exhaustive]
10#[derive(Debug, thiserror::Error)]
11pub enum MIError {
12 /// Model loading or forward pass error (wraps candle).
13 #[error("model error: {0}")]
14 Model(#[from] candle_core::Error),
15
16 /// Hook capture or lookup error.
17 #[error("hook error: {0}")]
18 Hook(String),
19
20 /// Intervention validation or application error.
21 #[error("intervention error: {0}")]
22 Intervention(String),
23
24 /// Model configuration parsing error.
25 #[error("config error: {0}")]
26 Config(String),
27
28 /// Tokenizer error.
29 #[error("tokenizer error: {0}")]
30 Tokenizer(String),
31
32 /// I/O error.
33 #[error(transparent)]
34 Io(#[from] std::io::Error),
35
36 /// Model download error.
37 ///
38 /// Returned when downloading a model from the `HuggingFace` Hub fails.
39 #[error("download error: {0}")]
40 Download(String),
41
42 /// Memory measurement error.
43 ///
44 /// Returned when a platform API for RAM or VRAM measurement fails.
45 #[error("memory error: {0}")]
46 Memory(String),
47}
48
49/// Bridge anamnesis errors into [`MIError`] when the `sae` feature is enabled.
50///
51/// [`AnamnesisError`](anamnesis::AnamnesisError) is `#[non_exhaustive]`, so the
52/// catch-all arm ensures forward compatibility with future variants.
53#[cfg(feature = "sae")]
54impl From<anamnesis::AnamnesisError> for MIError {
55 fn from(e: anamnesis::AnamnesisError) -> Self {
56 match e {
57 anamnesis::AnamnesisError::Parse { reason } => Self::Config(reason),
58 anamnesis::AnamnesisError::Unsupported { format, detail } => {
59 Self::Config(format!("unsupported {format}: {detail}"))
60 }
61 anamnesis::AnamnesisError::Io(io_err) => Self::Io(io_err),
62 // AnamnesisError is #[non_exhaustive] — forward-compatible catch-all
63 _ => Self::Config(e.to_string()),
64 }
65 }
66}
67
68/// Result type alias for candle-mi operations.
69pub type Result<T> = std::result::Result<T, MIError>;