1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use super::{axes::*, ReduceShape, ReduceShapeTo};
#[cfg(not(feature = "cuda"))]
pub trait SafeZeros {}
#[cfg(feature = "cuda")]
pub trait SafeZeros: cudarc::driver::ValidAsZeroBits + cudarc::driver::DeviceRepr {}
pub trait Unit:
'static
+ Copy
+ Clone
+ Default
+ std::fmt::Debug
+ PartialEq
+ PartialOrd
+ Send
+ Sync
+ std::marker::Unpin
+ SafeZeros
{
const ONE: Self;
}
macro_rules! unit {
($type:ty, $one:expr) => {
impl SafeZeros for $type {}
impl Unit for $type {
const ONE: Self = $one;
}
};
}
unit!(f32, 1.0);
unit!(f64, 1.0);
unit!(usize, 1);
unit!(isize, 1);
unit!(u8, 1);
unit!(i8, 1);
unit!(u16, 1);
unit!(i16, 1);
unit!(u32, 1);
unit!(i32, 1);
unit!(u64, 1);
unit!(i64, 1);
unit!(u128, 1);
unit!(i128, 1);
unit!(bool, true);
pub trait HasUnitType {
type Unit: Unit;
}
pub trait Dtype:
Unit
+ std::ops::Add<Self, Output = Self>
+ std::ops::Sub<Self, Output = Self>
+ std::ops::Mul<Self, Output = Self>
+ std::ops::Div<Self, Output = Self>
+ std::ops::AddAssign
+ std::ops::SubAssign
+ std::ops::MulAssign
+ std::ops::DivAssign
+ num_traits::FromPrimitive
+ num_traits::Float
+ rand_distr::uniform::SampleUniform
{
}
impl Dtype for f32 {}
impl Dtype for f64 {}
pub trait HasDtype {
type Dtype: Dtype;
}
pub trait Dim: 'static + Copy + Clone + std::fmt::Debug + Send + Sync + Eq + PartialEq {
fn size(&self) -> usize;
fn from_size(size: usize) -> Option<Self>;
}
pub trait ConstDim: Default + Dim {
const SIZE: usize;
}
impl Dim for usize {
#[inline(always)]
fn size(&self) -> usize {
*self
}
#[inline(always)]
fn from_size(size: usize) -> Option<Self> {
Some(size)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Const<const M: usize>;
impl<const M: usize> Dim for Const<M> {
#[inline(always)]
fn size(&self) -> usize {
M
}
#[inline(always)]
fn from_size(size: usize) -> Option<Self> {
if size == M {
Some(Const)
} else {
None
}
}
}
impl<const M: usize> ConstDim for Const<M> {
const SIZE: usize = M;
}
impl<const N: usize> core::ops::Add<Const<N>> for usize {
type Output = usize;
fn add(self, rhs: Const<N>) -> Self::Output {
self.size() + rhs.size()
}
}
impl<const N: usize> core::ops::Add<usize> for Const<N> {
type Output = usize;
fn add(self, rhs: usize) -> Self::Output {
self.size() + rhs.size()
}
}
#[cfg(feature = "nightly")]
impl<const N: usize, const M: usize> core::ops::Add<Const<N>> for Const<M>
where
Const<{ N + M }>: Sized,
{
type Output = Const<{ N + M }>;
fn add(self, _: Const<N>) -> Self::Output {
Const
}
}
pub trait Array<T>: IntoIterator<Item = T> {
type Dim: Dim;
fn dim(&self) -> Self::Dim;
}
impl<T, const N: usize> Array<T> for [T; N] {
type Dim = Const<N>;
fn dim(&self) -> Self::Dim {
Const
}
}
impl<T> Array<T> for std::vec::Vec<T> {
type Dim = usize;
fn dim(&self) -> Self::Dim {
self.len()
}
}
pub trait Shape:
'static
+ std::fmt::Debug
+ Clone
+ Copy
+ Send
+ Sync
+ Eq
+ PartialEq
+ HasAxes<Self::AllAxes>
+ HasAxes<Self::LastAxis>
+ ReduceShapeTo<(), Self::AllAxes>
+ ReduceShape<Self::LastAxis>
{
const NUM_DIMS: usize;
type Concrete: std::fmt::Debug
+ Clone
+ Copy
+ Default
+ Eq
+ PartialEq
+ std::ops::Index<usize, Output = usize>
+ std::ops::IndexMut<usize>
+ Send
+ Sync
+ IntoIterator<Item = usize>
+ Into<std::vec::Vec<usize>>;
type AllAxes: Axes;
type LastAxis: Axes;
fn concrete(&self) -> Self::Concrete;
fn from_concrete(concrete: &Self::Concrete) -> Option<Self>;
#[inline(always)]
fn num_elements(&self) -> usize {
self.concrete().into_iter().product()
}
#[inline(always)]
fn strides(&self) -> Self::Concrete {
let sizes = self.concrete();
let mut strides: Self::Concrete = Default::default();
strides[Self::NUM_DIMS - 1] = 1;
for i in (0..(Self::NUM_DIMS - 1)).rev() {
strides[i] = strides[i + 1] * sizes[i + 1];
}
strides
}
}
pub trait ConstShape: Default + Shape {
const NUMEL: usize;
}
pub trait HasShape {
type WithShape<New: Shape>: HasShape<Shape = New>;
type Shape: Shape;
fn shape(&self) -> &Self::Shape;
}
impl<S: Shape> HasShape for S {
type WithShape<New: Shape> = New;
type Shape = Self;
fn shape(&self) -> &Self::Shape {
self
}
}
pub type Rank0 = ();
pub type Rank1<const M: usize> = (Const<M>,);
pub type Rank2<const M: usize, const N: usize> = (Const<M>, Const<N>);
pub type Rank3<const M: usize, const N: usize, const O: usize> = (Const<M>, Const<N>, Const<O>);
pub type Rank4<const M: usize, const N: usize, const O: usize, const P: usize> =
(Const<M>, Const<N>, Const<O>, Const<P>);
pub type Rank5<const M: usize, const N: usize, const O: usize, const P: usize, const Q: usize> =
(Const<M>, Const<N>, Const<O>, Const<P>, Const<Q>);
#[rustfmt::skip]
pub type Rank6<const M: usize, const N: usize, const O: usize, const P: usize, const Q: usize, const R: usize> =
(Const<M>, Const<N>, Const<O>, Const<P>, Const<Q>, Const<R>);
macro_rules! shape {
(($($D:tt $Idx:tt),*), rank=$Num:expr, all=$All:tt) => {
impl<$($D: Dim, )*> Shape for ($($D, )*) {
const NUM_DIMS: usize = $Num;
type Concrete = [usize; $Num];
type AllAxes = $All<$($Idx,)*>;
type LastAxis = Axis<{$Num - 1}>;
#[inline(always)]
fn concrete(&self) -> Self::Concrete {
[$(self.$Idx.size(), )*]
}
#[inline(always)]
fn from_concrete(concrete: &Self::Concrete) -> Option<Self> {
Some(($(Dim::from_size(concrete[$Idx])?, )*))
}
}
impl<$($D: ConstDim, )*> ConstShape for ($($D, )*) {
const NUMEL: usize = $($D::SIZE * )* 1;
}
impl Shape for [usize; $Num] {
const NUM_DIMS: usize = $Num;
type Concrete = Self;
type AllAxes = $All<$($Idx,)*>;
type LastAxis = Axis<{$Num - 1}>;
fn concrete(&self) -> Self::Concrete {
*self
}
fn from_concrete(concrete: &Self::Concrete) -> Option<Self> {
Some(*concrete)
}
}
};
}
impl Shape for () {
const NUM_DIMS: usize = 0;
type Concrete = [usize; 0];
type AllAxes = Axis<0>;
type LastAxis = Axis<0>;
#[inline(always)]
fn concrete(&self) -> Self::Concrete {
[]
}
#[inline(always)]
fn strides(&self) -> Self::Concrete {
[]
}
#[inline(always)]
fn from_concrete(_: &Self::Concrete) -> Option<Self> {
Some(())
}
}
impl ConstShape for () {
const NUMEL: usize = 1;
}
shape!((D1 0), rank=1, all=Axis);
shape!((D1 0, D2 1), rank=2, all=Axes2);
shape!((D1 0, D2 1, D3 2), rank=3, all=Axes3);
shape!((D1 0, D2 1, D3 2, D4 3), rank=4, all=Axes4);
shape!((D1 0, D2 1, D3 2, D4 3, D5 4), rank=5, all=Axes5);
shape!((D1 0, D2 1, D3 2, D4 3, D5 4, D6 5), rank=6, all=Axes6);