-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
355 lines (299 loc) · 9.62 KB
/
Copy pathtensor.cpp
File metadata and controls
355 lines (299 loc) · 9.62 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include "tensor.h"
namespace tensor {
//static methods
Tensor* Tensor::fromVector(const std::vector<int>& dims, bool isGrad) {
Tensor* result = new Tensor(dims);
result->isGrad = isGrad;
return result;
}
Tensor* Tensor::fromVector(const std::vector<float>& data, const std::vector<int>& dims, bool isGrad) {
Tensor* result= new Tensor(data, dims);
result->isGrad = isGrad;
if(isGrad)
{
result->grad.resize(data.size(), 0.0f);
}
return result;
}
Tensor* Tensor::fromVector(const Tensor& t, const std::vector<int>& dims, bool isGrad) {
Tensor* result= new Tensor(dims);
result->data = t.data;
result->isGrad = isGrad;
return result;
}
//Constructor and Destructor
Tensor::Tensor() {
isGrad = false;
}
Tensor::Tensor(const std::vector<int>& dims, bool isGrad) {
shape = dims;
strides.resize(shape.size());
strides[shape.size() - 1] = 1;
for (int i = shape.size() - 2; i >= 0; i--) {
strides[i] = strides[i + 1] * shape[i + 1];
}
data.resize(strides[0] * shape[0]);
this->isGrad = isGrad;
if(isGrad)
{
grad.resize(data.size(), 0.0f);
}
}
Tensor::Tensor(Tensor& other) {
shape = other.shape;
strides = other.strides;
data = other.data;
isGrad = other.isGrad;
grad = other.grad;
previous = other.previous;
gradFn = other.gradFn;
isLeafNode(other.isLeaf);
name = other.name;
}
Tensor::Tensor(const std::vector<float>& data, const std::vector<int>& dims, const std::vector<int>& strides, bool isGrad) {
this->data = data;
this->shape = dims;
this->strides = strides;
this->isGrad = isGrad;
if(isGrad)
{
grad.resize(data.size(), 0.0f);
}
}
Tensor::Tensor(const std::vector<float>& data, const std::vector<int>& dims,bool isGrad) {
shape = dims;
strides.resize(shape.size());
strides[shape.size() - 1] = 1;
for (int i = shape.size() - 2; i >= 0; i--) {
strides[i] = strides[i + 1] * shape[i + 1];
}
this->data = data;
this->isGrad = isGrad;
if(isGrad)
{
grad.resize(data.size(), 0.0f);
}
}
Tensor::Tensor(const std::vector<int>& dims, Tensor* other) : Tensor(dims)
{
addToPrevious(other);
}
Tensor::~Tensor() {
}
//Normal Distribution
Tensor* Tensor::normal(float mean, float std, const std::vector<int>& dims, bool isGrad) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::normal_distribution<float> distribution(mean, std);
Tensor* result = new Tensor(dims, isGrad);
for (int i = 0; i < result->data.size(); i++) {
result->data[i] = distribution(gen);
}
return result;
}
//MatMul
Tensor* Tensor::matmul2D_(Tensor* other)
{
std::vector<int> otherShape = other->getShape();
std::vector<int> newDims = { shape[0], otherShape[1] };
Tensor* result = new Tensor(newDims, isGrad);
if(isGrad)
{
result->addToPrevious(this);
result->addToPrevious(other);
result->gradFn = [result, this, other]() mutable {
// Initialize gradients to zero
std::fill(this->grad.begin(), this->grad.end(), 0.0);
std::fill(other->grad.begin(), other->grad.end(), 0.0);
for (int i = 0; i < shape[0]; i++) {
for (int j = 0; j < other->getShape()[1]; j++) {
for (int k = 0; k < shape[1]; k++) {
this->grad[i * strides[0] + k] += result->grad[i * result->strides[0] + j] * other->data[k * other->strides[0] + j];
other->grad[k * other->strides[0] + j] += result->grad[i * result->strides[0] + j] * data[i * strides[0] + k];
}
}
}
};
}
for(int i =0 ;i< shape[0];i++)
{
for(int j=0;j<otherShape[1];j++)
{
float sum = 0;
for(int k =0 ;k< shape[1];k++)
{
sum+= data[i* strides[0] + k] * other->data[k* other->strides[0] + j];
}
result->data[i* result->strides[0] + j] = sum;
}
}
return result;
}
Tensor* Tensor::matmul3D_(Tensor* other)
{
std::vector<int> otherShape = other->getShape();
std::vector<int> newDims = { shape[0], shape[1], otherShape[2] };
Tensor* result = new Tensor(newDims);
for(int t=0 ; t< shape[0]; t++)
{
for(int i =0 ;i< shape[1];i++)
{
for(int j=0;j<otherShape[2];j++)
{
float sum = 0;
for(int k =0 ;k< shape[2];k++)
{
sum+= data[t * strides[0] + i* strides[1] + k] * other->data[t * strides[0] + k* other->strides[2] + j];
}
result->data[t * result->strides[0] + i* result->strides[1] + j] = sum;
}
}
}
return result;
}
Tensor* Tensor::matmul( Tensor* other) {
std::vector<int> dims = other->getShape();
if(other->shape.size()==2)
return this->matmul2D_(other);
else
return this->matmul3D_(other);
}
//Transpose
Tensor* Tensor::transpose()
{
std::vector<int> newDims = {shape.rbegin(), shape.rend()};
std::vector<int> newStrides = {strides.rbegin(), strides.rend()};
Tensor* result = new Tensor(data, newDims, newStrides);
return result;
}
//slice
Tensor* Tensor::slice(int index, int dim) {
if (dim >= shape.size()) {
std::cout << "Dimension out of bounds" << std::endl;
}
if (index >= shape[dim]) {
std::cout << "Index out of bounds" << std::endl;
}
std::vector<int> newShape = {shape.begin() + dim +1, shape.end()};
Tensor* result = new Tensor(newShape);
for(int i =0 ; i < strides[dim];i++)
{
result->data[i] = data[i + index * strides[dim]];
}
return result;
}
std::vector<int> Tensor::unravelIndex(int index) const {
std::vector<int> result(shape.size());
for (int i = shape.size() - 1; i >= 0; i--) {
result[i] = index % shape[i];
index /= shape[i];
}
return result;
}
int Tensor::ravelIndex(const std::vector<int>& indices, const std::vector<int>& strides) const {
int result = 0;
for (int i = 0; i < indices.size(); i++) {
result += indices[i] * strides[i];
}
return result;
}
//backward
void Tensor::setGrad(bool val)
{
this->isGrad = val;
}
void Tensor::setGradFn(std::function<void()> fn)
{
this->gradFn = fn;
}
void Tensor::setGrad(const std::vector<float>& grad)
{
this->grad = grad;
}
void Tensor::zeroGrad()
{
this->grad = std::vector<float>(data.size(), 0.0f);
}
std::vector<float>& Tensor::getGrad()
{
return this->grad;
}
std::vector<int> Tensor::getStrides()
{
return this->strides;
}
bool Tensor::isGradNode(){
return this->isGrad;
}
//backward
std::vector<Tensor*> Tensor::getPrevious()
{
return this->previous;
}
void topologySort(std::vector<Tensor*>& visited, std::vector<Tensor*>& topoList, Tensor* t)
{
if (std::find(visited.begin(), visited.end(), t) == visited.end())
{
visited.push_back(t);
for (Tensor* t1 : t->getPrevious())
{
topologySort(visited, topoList, t1);
}
topoList.push_back(t);
}
}
void Tensor::backward()
{
this->grad[0] = 1.0;
std::vector<Tensor*> visited;
std::vector<Tensor*> topoList;
topologySort(visited, topoList, this);
for (int i = topoList.size() - 1; i >= 0; i--)
{
Tensor* t = topoList[i];
if (t->gradFn != nullptr)
{
t->gradFn();
}
//if(!t->isLeafNode() && t->isGrad)
// {
// delete t;
//}
}
}
void Tensor::addToPrevious(Tensor* t)
{
this->previous.push_back(t);
}
//other Ops
void Tensor::isLeafNode(bool val)
{
this->isLeaf = val;
}
bool Tensor::isLeafNode(){
return this->isLeaf;
}
Tensor* Tensor::zeros(const std::vector<int>& dims, bool isGrad) {
Tensor* result = new Tensor(dims, isGrad);
return result;
}
Tensor* Tensor::ones(const std::vector<int>& dims, bool isGrad) {
Tensor* result = new Tensor(dims, isGrad);
for (int i = 0; i < result->data.size(); i++) {
result->data[i] = 1.0f;
}
return result;
}
void Tensor::setName(std::string name)
{
this->name = name;
}
std::string Tensor::getName()
{
return this->name;
}
}