1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Error types for the ruvector-attention crate.
//!
//! This module defines all error types that can occur during attention computation,
//! configuration, and training operations.
use thiserror::Error;
/// Errors that can occur during attention operations.
#[derive(Error, Debug, Clone)]
pub enum AttentionError {
/// Dimension mismatch between query, key, or value tensors.
#[error("Dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch {
/// Expected dimension size
expected: usize,
/// Actual dimension size
actual: usize,
},
/// Invalid configuration parameter.
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
/// Error during attention computation.
#[error("Computation error: {0}")]
ComputationError(String),
/// Memory allocation failure.
#[error("Memory allocation failed: {0}")]
MemoryError(String),
/// Invalid head configuration for multi-head attention.
#[error("Invalid head count: dimension {dim} not divisible by {num_heads} heads")]
InvalidHeadCount {
/// Model dimension
dim: usize,
/// Number of attention heads
num_heads: usize,
},
/// Empty input provided.
#[error("Empty input: {0}")]
EmptyInput(String),
/// Invalid edge configuration for graph attention.
#[error("Invalid edge configuration: {0}")]
InvalidEdges(String),
/// Numerical instability detected.
#[error("Numerical instability: {0}")]
NumericalInstability(String),
/// Invalid mask dimensions.
#[error("Invalid mask dimensions: expected {expected}, got {actual}")]
InvalidMask {
/// Expected mask dimensions
expected: String,
/// Actual mask dimensions
actual: String,
},
}
/// Result type for attention operations.
pub type AttentionResult<T> = Result<T, AttentionError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = AttentionError::DimensionMismatch {
expected: 512,
actual: 256,
};
assert_eq!(err.to_string(), "Dimension mismatch: expected 512, got 256");
let err = AttentionError::InvalidConfig("dropout must be in [0, 1]".to_string());
assert_eq!(
err.to_string(),
"Invalid configuration: dropout must be in [0, 1]"
);
}
#[test]
fn test_error_clone() {
let err = AttentionError::ComputationError("test".to_string());
let cloned = err.clone();
assert_eq!(err.to_string(), cloned.to_string());
}
}