use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MuonError {
InvalidConfig(&'static str),
ExpectedMatrix {
rank: usize,
},
EmptyMatrix,
ShapeMismatch(&'static str),
DTypeMismatch(&'static str),
DeviceMismatch(&'static str),
EmptyMuonGroup,
DuplicateParameter(u64),
UnknownParameter(u64),
FrozenParameter(u64),
ModelChanged,
UnusedGradients,
IncompatibleRecord,
UnsupportedDistributed,
}
impl fmt::Display for MuonError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConfig(message) => write!(f, "invalid Muon configuration: {message}"),
Self::ExpectedMatrix { rank } => write!(f, "Newton-Schulz iteration requires 2D tensors, got {rank}D"),
Self::EmptyMatrix => f.write_str("Muon requires a nonempty matrix"),
Self::ShapeMismatch(what) => write!(f, "Muon {what} shape does not match its parameter"),
Self::DTypeMismatch(what) => write!(f, "Muon {what} dtype does not match its parameter"),
Self::DeviceMismatch(what) => write!(f, "Muon {what} device does not match its parameter"),
Self::EmptyMuonGroup => f.write_str("select at least one hidden matrix explicitly for Muon"),
Self::DuplicateParameter(id) => write!(f, "duplicate Muon parameter id {id}"),
Self::UnknownParameter(id) => write!(f, "unknown Muon parameter id {id}"),
Self::FrozenParameter(id) => write!(f, "Muon parameter {id} does not require gradients"),
Self::ModelChanged => f.write_str("model parameter ids/shapes changed or tied aliases disagree"),
Self::UnusedGradients => f.write_str("gradients include an unknown or frozen parameter"),
Self::IncompatibleRecord => f.write_str("Muon/AdamW record configuration, grouping, geometry or version mismatch"),
Self::UnsupportedDistributed => f.write_str("Muon requires complete synchronized matrices; implicit sharded/step_multi updates are not supported"),
}
}
}
impl core::error::Error for MuonError {}