mirror of
https://github.com/huggingface/candle.git
synced 2025-06-18 11:37:11 +00:00
Start refactoring the stride.
This commit is contained in:
47
candle-core/src/layout.rs
Normal file
47
candle-core/src/layout.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use crate::Shape;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Layout {
|
||||
shape: Shape,
|
||||
// The strides are given in number of elements and not in bytes.
|
||||
stride: Vec<usize>,
|
||||
start_offset: usize,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
pub fn contiguous<S: Into<Shape>>(shape: S) -> Self {
|
||||
let shape = shape.into();
|
||||
let stride = shape.stride_contiguous();
|
||||
Self {
|
||||
shape,
|
||||
stride,
|
||||
start_offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dims(&self) -> &[usize] {
|
||||
self.shape.dims()
|
||||
}
|
||||
|
||||
pub fn shape(&self) -> &Shape {
|
||||
&self.shape
|
||||
}
|
||||
|
||||
pub fn stride(&self) -> &[usize] {
|
||||
&self.stride
|
||||
}
|
||||
|
||||
pub fn start_offset(&self) -> usize {
|
||||
self.start_offset
|
||||
}
|
||||
|
||||
/// Returns true if the data is stored in a C contiguous (aka row major) way.
|
||||
pub fn is_contiguous(&self) -> bool {
|
||||
self.shape.is_contiguous(&self.stride)
|
||||
}
|
||||
|
||||
/// Returns true if the data is stored in a Fortran contiguous (aka column major) way.
|
||||
pub fn is_fortran_contiguous(&self) -> bool {
|
||||
self.shape.is_fortran_contiguous(&self.stride)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user