use leto::LetoError;
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CrossEntropyOperand {
Logits,
Probabilities,
Loss,
LogitGradient,
OutputGradient,
}
#[derive(Debug, Error, Clone, PartialEq)]
#[non_exhaustive]
pub enum CrossEntropyError {
#[error("cross-entropy {operand:?} shape mismatch: expected {expected:?}, actual {actual:?}")]
Shape {
operand: CrossEntropyOperand,
expected: Vec<usize>,
actual: Vec<usize>,
},
#[error("cross-entropy batch must contain at least one sample")]
EmptyBatch,
#[error("cross-entropy class dimension must contain at least one class")]
EmptyClasses,
#[error("cross-entropy target count mismatch: expected {expected}, actual {actual}")]
TargetCount {
expected: usize,
actual: usize,
},
#[error("cross-entropy target {target} at batch {batch} is outside class range 0..{classes}")]
TargetOutOfRange {
batch: usize,
target: usize,
classes: usize,
},
#[error("cross-entropy {operand:?} layout is invalid: {source}")]
Layout {
operand: CrossEntropyOperand,
#[source]
source: LetoError,
},
#[error("cross-entropy {dimension} extent {extent} is not finite in the selected scalar")]
ScalarExtent {
dimension: &'static str,
extent: usize,
},
#[error(
"cross-entropy class extent {classes} exceeds the selected scalar's probability-resolution bound"
)]
ProbabilityResolution {
classes: usize,
},
#[error("cross-entropy {operand:?} contains a non-finite value")]
NonFinite {
operand: CrossEntropyOperand,
},
#[error("cross-entropy arithmetic is not finite for batch row {batch}")]
ArithmeticNonFinite {
batch: usize,
},
#[error("cross-entropy probabilities at batch row {batch} do not form a valid row")]
InvalidProbabilities {
batch: usize,
},
}
pub type CrossEntropyResult<T> = core::result::Result<T, CrossEntropyError>;