ruvector_attention/
error.rs1use thiserror::Error;
7
8#[derive(Error, Debug, Clone)]
10pub enum AttentionError {
11 #[error("Dimension mismatch: expected {expected}, got {actual}")]
13 DimensionMismatch {
14 expected: usize,
16 actual: usize,
18 },
19
20 #[error("Invalid configuration: {0}")]
22 InvalidConfig(String),
23
24 #[error("Computation error: {0}")]
26 ComputationError(String),
27
28 #[error("Memory allocation failed: {0}")]
30 MemoryError(String),
31
32 #[error("Invalid head count: dimension {dim} not divisible by {num_heads} heads")]
34 InvalidHeadCount {
35 dim: usize,
37 num_heads: usize,
39 },
40
41 #[error("Empty input: {0}")]
43 EmptyInput(String),
44
45 #[error("Invalid edge configuration: {0}")]
47 InvalidEdges(String),
48
49 #[error("Numerical instability: {0}")]
51 NumericalInstability(String),
52
53 #[error("Invalid mask dimensions: expected {expected}, got {actual}")]
55 InvalidMask {
56 expected: String,
58 actual: String,
60 },
61}
62
63pub type AttentionResult<T> = Result<T, AttentionError>;
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_error_display() {
72 let err = AttentionError::DimensionMismatch {
73 expected: 512,
74 actual: 256,
75 };
76 assert_eq!(err.to_string(), "Dimension mismatch: expected 512, got 256");
77
78 let err = AttentionError::InvalidConfig("dropout must be in [0, 1]".to_string());
79 assert_eq!(
80 err.to_string(),
81 "Invalid configuration: dropout must be in [0, 1]"
82 );
83 }
84
85 #[test]
86 fn test_error_clone() {
87 let err = AttentionError::ComputationError("test".to_string());
88 let cloned = err.clone();
89 assert_eq!(err.to_string(), cloned.to_string());
90 }
91}