mirror of
https://github.com/huggingface/candle.git
synced 2025-06-18 11:37:11 +00:00
Cleanup the main crate error and add a couple dedicated ones (#142)
* Cosmetic cleanups to the error enum. * More error cleanup. * Proper error handling rather than panicing. * Add some conv1d dedicated error.
This commit is contained in:
@ -194,7 +194,7 @@ impl Dim for usize {
|
||||
if dim >= shape.dims().len() {
|
||||
Err(Error::DimOutOfRange {
|
||||
shape: shape.clone(),
|
||||
dim,
|
||||
dim: dim as i32,
|
||||
op,
|
||||
})?
|
||||
} else {
|
||||
@ -207,7 +207,7 @@ impl Dim for usize {
|
||||
if dim > shape.dims().len() {
|
||||
Err(Error::DimOutOfRange {
|
||||
shape: shape.clone(),
|
||||
dim,
|
||||
dim: dim as i32,
|
||||
op,
|
||||
})?
|
||||
} else {
|
||||
@ -221,30 +221,36 @@ pub enum D {
|
||||
Minus2,
|
||||
}
|
||||
|
||||
impl D {
|
||||
fn out_of_range(&self, shape: &Shape, op: &'static str) -> Error {
|
||||
let dim = match self {
|
||||
Self::Minus1 => -1,
|
||||
Self::Minus2 => -2,
|
||||
};
|
||||
Error::DimOutOfRange {
|
||||
shape: shape.clone(),
|
||||
dim,
|
||||
op,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dim for D {
|
||||
fn to_index(&self, shape: &Shape, op: &'static str) -> Result<usize> {
|
||||
let rank = shape.rank();
|
||||
match self {
|
||||
Self::Minus1 if rank >= 1 => Ok(rank - 1),
|
||||
Self::Minus2 if rank >= 2 => Ok(rank - 2),
|
||||
_ => Err(Error::DimOutOfRange {
|
||||
shape: shape.clone(),
|
||||
dim: 42, // TODO: Have an adequate error
|
||||
op,
|
||||
}),
|
||||
_ => Err(self.out_of_range(shape, op)),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_index_plus_one(&self, shape: &Shape, op: &'static str) -> Result<usize> {
|
||||
let rank = shape.rank();
|
||||
match self {
|
||||
Self::Minus1 if rank >= 1 => Ok(rank),
|
||||
Self::Minus2 if rank >= 2 => Ok(rank - 1),
|
||||
_ => Err(Error::DimOutOfRange {
|
||||
shape: shape.clone(),
|
||||
dim: 42, // TODO: Have an adequate error
|
||||
op,
|
||||
}),
|
||||
Self::Minus1 => Ok(rank),
|
||||
Self::Minus2 if rank >= 1 => Ok(rank - 1),
|
||||
_ => Err(self.out_of_range(shape, op)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user