+
Skip to content

Adding ConstDim and ConstShape for tensor creation #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/nn/npz_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<
#[cfg(test)]
mod tests {
use crate::{
shapes::{Dtype, Rank1, Rank3, Shape},
shapes::*,
tensor::{AsArray, SampleTensor, Tensor},
tensor_ops::Device,
tests::TestDevice,
Expand All @@ -344,7 +344,7 @@ mod tests {
use tempfile::NamedTempFile;

fn test_save_load<
S: Shape + Default,
S: ConstShape,
E: Dtype,
D: Device<E>,
M: ResetParams<D, E> + Module<Tensor<S, E, D>> + SaveToNpz + LoadFromNpz,
Expand Down
5 changes: 3 additions & 2 deletions src/shapes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) use replace_dim::{RemoveDimTo, ReplaceDimTo};
pub(crate) use same_numel::HasSameNumelAs;

pub use axes::{Axes2, Axes3, Axes4, Axes5, Axes6, Axis, HasAxes};
pub use shape::{Const, Dim};
pub use shape::{Const, ConstDim, Dim};
pub use shape::{ConstShape, HasShape, Shape};
pub use shape::{Dtype, HasDtype, HasUnitType, Unit};
pub use shape::{HasShape, Rank0, Rank1, Rank2, Rank3, Rank4, Rank5, Rank6, Shape};
pub use shape::{Rank0, Rank1, Rank2, Rank3, Rank4, Rank5, Rank6};
11 changes: 11 additions & 0 deletions src/shapes/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub trait Dim: 'static + Copy + Clone + std::fmt::Debug + Send + Sync + Eq + Par
fn from_size(size: usize) -> Option<Self>;
}

/// Represents a single dimension where all
/// instances are guarunteed to be the same size at compile time.
pub trait ConstDim: Default + Dim {}

impl Dim for usize {
#[inline(always)]
fn size(&self) -> usize {
Expand Down Expand Up @@ -72,6 +76,8 @@ impl<const M: usize> Dim for Const<M> {
}
}

impl<const M: usize> ConstDim for Const<M> {}

/// A collection of dimensions ([Dim]) that change how a multi-dimensional
/// array is interacted with.
pub trait Shape:
Expand Down Expand Up @@ -132,6 +138,9 @@ pub trait Shape:
}
}

/// Represents a [Shape] that has all [ConstDim]s
pub trait ConstShape: Default + Shape {}

/// Represents something that has a [Shape].
pub trait HasShape {
type WithShape<New: Shape>: HasShape<Shape = New>;
Expand Down Expand Up @@ -182,6 +191,7 @@ impl<$($D: Dim, )*> Shape for ($($D, )*) {
Some(($(Dim::from_size(concrete[$Idx])?, )*))
}
}
impl<$($D: ConstDim, )*> ConstShape for ($($D, )*) { }
};
}

Expand All @@ -203,6 +213,7 @@ impl Shape for () {
Some(())
}
}
impl ConstShape for () {}

shape!((D1 0), rank=1, all=Axis);
shape!((D1 0, D2 1), rank=2, all=Axes2);
Expand Down
18 changes: 9 additions & 9 deletions src/tensor/storage_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rand::distributions::Distribution;
use rand_distr::{Standard, StandardNormal};

use crate::{
shapes::{Dtype, HasDtype, HasShape, HasUnitType, Shape, Unit},
shapes::{ConstShape, Dtype, HasDtype, HasShape, HasUnitType, Shape, Unit},
unique_id::unique_id,
};

Expand Down Expand Up @@ -98,12 +98,12 @@ pub trait ZerosTensor<E: Unit>: DeviceStorage {
/// # let dev: Cpu = Default::default();
/// let a: Tensor<Rank2<2, 3>> = dev.zeros();
/// ```
fn zeros<S: Shape + Default>(&self) -> Tensor<S, E, Self> {
fn zeros<S: ConstShape>(&self) -> Tensor<S, E, Self> {
self.try_zeros_like::<S>(&Default::default()).unwrap()
}

/// Fallible version of [ZerosTensor::zeros]
fn try_zeros<S: Shape + Default>(&self) -> Result<Tensor<S, E, Self>, Self::Err> {
fn try_zeros<S: ConstShape>(&self) -> Result<Tensor<S, E, Self>, Self::Err> {
self.try_zeros_like::<S>(&Default::default())
}

Expand Down Expand Up @@ -146,12 +146,12 @@ pub trait OnesTensor<E: Unit>: DeviceStorage {
/// # let dev: Cpu = Default::default();
/// let a: Tensor<Rank2<2, 3>> = dev.ones();
/// ```
fn ones<S: Shape + Default>(&self) -> Tensor<S, E, Self> {
fn ones<S: ConstShape>(&self) -> Tensor<S, E, Self> {
self.try_ones_like::<S>(&Default::default()).unwrap()
}

/// Fallible version of [OnesTensor::ones]
fn try_ones<S: Shape + Default>(&self) -> Result<Tensor<S, E, Self>, Self::Err> {
fn try_ones<S: ConstShape>(&self) -> Result<Tensor<S, E, Self>, Self::Err> {
self.try_ones_like::<S>(&Default::default())
}

Expand Down Expand Up @@ -188,25 +188,25 @@ pub trait OneFillStorage<E: Unit>: DeviceStorage {

/// Constructs tensors filled with random values from a given distribution.
pub trait SampleTensor<E: Unit>: DeviceStorage {
fn sample_uniform<S: Shape + Default>(&self) -> Tensor<S, E, Self>
fn sample_uniform<S: ConstShape>(&self) -> Tensor<S, E, Self>
where
Standard: Distribution<E>,
{
self.sample::<S, _>(Standard)
}

fn sample_normal<S: Shape + Default>(&self) -> Tensor<S, E, Self>
fn sample_normal<S: ConstShape>(&self) -> Tensor<S, E, Self>
where
StandardNormal: Distribution<E>,
{
self.sample::<S, _>(StandardNormal)
}

fn sample<S: Shape + Default, D: Distribution<E>>(&self, distr: D) -> Tensor<S, E, Self> {
fn sample<S: ConstShape, D: Distribution<E>>(&self, distr: D) -> Tensor<S, E, Self> {
self.try_sample_like::<S, D>(&Default::default(), distr)
.unwrap()
}
fn try_sample<S: Shape + Default, D: Distribution<E>>(
fn try_sample<S: ConstShape, D: Distribution<E>>(
&self,
distr: D,
) -> Result<Tensor<S, E, Self>, Self::Err> {
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载