use leto::LetoError;
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttentionOperand {
Query,
Key,
Value,
Mask,
Output,
Weights,
OutputGradient,
QueryGradient,
KeyGradient,
ValueGradient,
Scale,
}
#[derive(Debug, Error, Clone, PartialEq)]
#[non_exhaustive]
pub enum AttentionError {
#[error("attention {operand:?} shape mismatch: expected {expected:?}, actual {actual:?}")]
Shape {
operand: AttentionOperand,
expected: [usize; 3],
actual: [usize; 3],
},
#[error("attention mask shape {actual:?} cannot broadcast to {target:?}")]
MaskShape {
actual: [usize; 3],
target: [usize; 3],
},
#[error("attention key sequence must contain at least one element")]
EmptyKeySequence,
#[error("attention backward requires at least one gradient target")]
NoGradientTargets,
#[error("attention {operand:?} contains a non-finite value")]
NonFinite {
operand: AttentionOperand,
},
#[error("attention arithmetic produces a non-finite {operand:?} value")]
ArithmeticNonFinite {
operand: AttentionOperand,
},
#[error("attention weights row [{batch}, {query}] is not a probability row")]
InvalidWeights {
batch: usize,
query: usize,
},
#[error("attention {operand:?} layout is invalid: {source}")]
Layout {
operand: AttentionOperand,
#[source]
source: LetoError,
},
#[error("attention workspace size overflow")]
WorkspaceOverflow,
}
pub type AttentionResult<T> = core::result::Result<T, AttentionError>;