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
use ndarray;
use std::fmt;
use num_traits;
pub struct GenTensor<T> {
d: Vec<T>,
dim: Vec<u32>,
}
impl<T> GenTensor<T> where T: num_traits::Float {
fn new() -> GenTensor<T> {
GenTensor { d: Vec::<T>::new(), dim: Vec::new() }
}
pub fn new_raw(data: &Vec<T>, shape: &Vec<u32>) -> GenTensor<T> {
let mut new_data = data.to_vec();
let mut new_dim = shape.to_vec();
GenTensor {
d: new_data,
dim: new_dim,
}
}
pub fn new_val(d: T, shape: &Vec<u32>) -> GenTensor<T> {
let mut dsize = 0;
for i in shape {
dsize += (*i) as usize;
}
GenTensor {
d: vec![d; dsize],
dim: shape.to_vec(),
}
}
pub fn stride(&self) -> Vec<u32> {
let mut ret = vec![0; self.dim.len()];
let dsize = ret.len();
for i in 0..dsize {
if i == 0 {
ret[dsize-1] = 1;
} else {
ret[dsize-i-1] = ret[dsize-i]*self.dim[dsize-i];
}
}
ret
}
pub fn get(&self, o: &Vec<u32>) -> T {
let stride = self.stride();
let dsize = o.len();
let mut index = 0;
for i in 0..dsize {
index += (stride[i]*o[i]) as usize;
}
self.d[index]
}
pub fn add(&self, o: &GenTensor<T>) -> GenTensor<T> {
let mut ret = GenTensor {
d: Vec::with_capacity(self.d.len()),
dim: self.dim.clone(),
};
for item in self.d.iter().zip(o.d.iter()) {
let (v1, v2) = item;
ret.d.push(*v1 + *v2);
}
ret
}
pub fn sub(&self, o: &GenTensor<T>) -> GenTensor<T> {
let mut ret = GenTensor {
d: Vec::with_capacity(self.d.len()),
dim: self.dim.clone(),
};
for item in self.d.iter().zip(o.d.iter()) {
let (v1, v2) = item;
ret.d.push(*v1 - *v2);
}
ret
}
pub fn mul(&self, o: &GenTensor<T>) -> GenTensor<T> {
let mut ret = GenTensor {
d: Vec::with_capacity(self.d.len()),
dim: self.dim.clone(),
};
for item in self.d.iter().zip(o.d.iter()) {
let (v1, v2) = item;
ret.d.push(*v1 * *v2);
}
ret
}
pub fn div(&self, o: &GenTensor<T>) -> GenTensor<T> {
let mut ret = GenTensor {
d: Vec::with_capacity(self.d.len()),
dim: self.dim.clone(),
};
for item in self.d.iter().zip(o.d.iter()) {
let (v1, v2) = item;
ret.d.push(*v1 / *v2);
}
ret
}
pub fn mm(&self, o: GenTensor<T>) {
}
}
impl<T> fmt::Display for GenTensor<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0")
}
}
macro_rules! typed_tensor_method {
($a:ident) => {
fn $a(&self, o: &TypedTensor) -> TypedTensor {
match (&self, o) {
(TypedTensor::Typef32(v1), TypedTensor::Typef32(v2)) => {TypedTensor::Typef32(v1.$a(v2))},
(TypedTensor::Typef64(v1), TypedTensor::Typef64(v2)) => {TypedTensor::Typef64(v1.$a(v2))},
_ => {panic!("should have same tensor type!");},
}
}
}
}
enum TypedTensor {
Typef32(GenTensor<f32>),
Typef64(GenTensor<f64>),
}
impl TypedTensor {
fn new() -> TypedTensor {
TypedTensor::Typef32(GenTensor::new())
}
fn to_f32(i: TypedTensor) {}
fn to_f64(i: TypedTensor) {}
typed_tensor_method!(add);
typed_tensor_method!(sub);
typed_tensor_method!(mul);
typed_tensor_method!(div);
}
impl fmt::Display for TypedTensor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TypedTensor::Typef32(v) => write!(f, "({}, )", v),
TypedTensor::Typef64(v) => write!(f, "({}, )", v),
}
}
}
pub struct Tensor {
v: TypedTensor,
}
impl Tensor {
pub fn new() -> Tensor {
Tensor {
v: TypedTensor::new(),
}
}
pub fn from_vec_f32(input: &Vec<f32>, dim: &Vec<u32>) -> Tensor {
let mut data = input.to_vec();
let mut idim = dim.to_vec();
Tensor {
v: TypedTensor::Typef32(GenTensor { d: data, dim: idim }),
}
}
pub fn to_vec_f32(&mut self) -> Vec<f32> {
let mut data = Vec::<f32>::new();
if let TypedTensor::Typef32(gt) = &self.v {
for item in >.d {
data.push(item.clone())
}
} else {
()
}
data
}
pub fn from_vec_f64(i: &Vec<f64>) -> Tensor {
Tensor::new()
}
pub fn full() -> Tensor {
Tensor::new()
}
pub fn full_like() -> Tensor {
Tensor::new()
}
pub fn empty() -> Tensor {
Tensor::new()
}
pub fn new_ones(dim: &Vec<u32>) -> Tensor {
Tensor::new()
}
pub fn new_zeros(dim: &Vec<u32>) -> Tensor {
Tensor::new()
}
pub fn zeros_like(o: &Tensor) -> Tensor {
Tensor::new()
}
pub fn ones_like(o: &Tensor) -> Tensor {
Tensor::new()
}
pub fn range(start: f64, step: f64) -> Tensor {
Tensor::new()
}
pub fn linespace(start: f64, end: f64, steps: u32) -> Tensor {
Tensor::new()
}
pub fn logspace(start: f64, end: f64, steps: u32, base: f64) -> Tensor {
Tensor::new()
}
pub fn eye(n: u32, m: u32) -> Tensor {
Tensor::new()
}
pub fn cat() {}
pub fn chunk() {}
pub fn gather() {}
pub fn index_select() {}
pub fn masked_select() {}
pub fn narrow() {}
pub fn nonzero() {}
pub fn reshape() {}
pub fn split() {}
pub fn squeeze() {}
pub fn stack() {}
pub fn t() {}
pub fn take() {}
pub fn transpose() {}
pub fn unbind() {}
pub fn unsqueeze() {}
pub fn condition() {}
pub fn to_f64(&mut self) {}
pub fn to_f32(&mut self) {}
pub fn add(&self, o: &Tensor) -> Tensor {
Tensor {
v: self.v.add(&o.v),
}
}
pub fn sub(&self, o: &Tensor) -> Tensor {
Tensor {
v: self.v.sub(&o.v),
}
}
pub fn mul(&self, o: &Tensor) -> Tensor {
Tensor {
v: self.v.mul(&o.v),
}
}
pub fn div(&self, o: &Tensor) -> Tensor {
Tensor {
v: self.v.div(&o.v),
}
}
}
impl fmt::Display for Tensor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, )", self.v)
}
}