-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensorops.cpp
More file actions
150 lines (126 loc) · 4.75 KB
/
Copy pathtensorops.cpp
File metadata and controls
150 lines (126 loc) · 4.75 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
#include <iostream>
#include <vector>
#include "tensor.h"
namespace tensor{
Tensor* Tensor::concat(Tensor* other, int dim) {
for (int i = 0; i < shape.size(); ++i) {
if (i != dim && shape[i] != other->getShape()[i]) {
throw std::invalid_argument("Incompatible shapes for concatenation");
}
}
std::vector<int> newShape = shape;
newShape[dim] += other->getShape()[dim];
Tensor* result = new Tensor(newShape, isGrad);
int numElementsBeforeDim = 1;
for (int i = 0; i < dim; ++i) {
numElementsBeforeDim *= shape[i];
}
int numElementsAfterDim = 1;
for (int i = dim + 1; i < shape.size(); ++i) {
numElementsAfterDim *= shape[i];
}
int thisDimSize = shape[dim];
int otherDimSize = other->getShape()[dim];
// Copy data from this tensor and the other tensor
for (int i = 0; i < numElementsBeforeDim; ++i) {
for (int j = 0; j < thisDimSize; ++j) {
int srcIndex = i * thisDimSize * numElementsAfterDim + j * numElementsAfterDim;
int destIndex = i * (thisDimSize + otherDimSize) * numElementsAfterDim + j * numElementsAfterDim;
std::copy(data.begin() + srcIndex, data.begin() + srcIndex + numElementsAfterDim,
result->data.begin() + destIndex);
}
for (int j = 0; j < otherDimSize; ++j) {
int srcIndex = i * otherDimSize * numElementsAfterDim + j * numElementsAfterDim;
int destIndex = i * (thisDimSize + otherDimSize) * numElementsAfterDim + (thisDimSize + j) * numElementsAfterDim;
std::copy(other->data.begin() + srcIndex, other->data.begin() + srcIndex + numElementsAfterDim,
result->data.begin() + destIndex);
}
}
return result;
}
Tensor* Tensor::squeeze(int dim) {
if (dim >= shape.size()) {
std::cout << "Dimension out of bounds" << std::endl;
}
if (shape[dim] != 1) {
std::cout << "Dimension is not 1" << std::endl;
}
std::vector<int> newDims = shape;
newDims.erase(newDims.begin() + dim);
Tensor* result = new Tensor(data, newDims, isGrad);
if(isGrad)
{
result->addToPrevious(this);
result->gradFn = [result, this, dim]() mutable {
for (int i = 0; i < this->data.size(); i++) {
this->grad[i] += result->grad[i];
}
};
}
return result;
}
Tensor* Tensor::unsqueeze(int dim) {
if (dim > shape.size()) {
std::cout << "Dimension out of bounds" << std::endl;
}
std::vector<int> newDims = shape;
newDims.insert(newDims.begin() + dim, 1);
Tensor* result = new Tensor(data, newDims, isGrad);
if(isGrad)
{
result->addToPrevious(this);
result->gradFn = [result, this, dim]() mutable {
for (int i = 0; i < this->data.size(); i++) {
this->grad[i] += result->grad[i];
}
};
}
return result;
}
int Tensor::numel() const {
int result = 1;
for (int i = 0; i < shape.size(); i++) {
result *= shape[i];
}
return result;
}
void Tensor::resize(const std::vector<int>& newDims) {
shape = newDims;
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]);
}
float Tensor::get(const std::vector<int>& indices) const {
int index = 0;
for (int i = 0; i < indices.size(); i++) {
index += indices[i] * strides[i];
}
return data[index];
}
void Tensor::set(const std::vector<int>& indices, float value) {
int index = 0;
for (int i = 0; i < indices.size(); i++) {
index += indices[i] * strides[i];
}
data[index] = value;
}
std::vector<int> Tensor::getShape() const {
return shape;
}
float Tensor::at(const std::vector<int>& indices) const {
if (indices.size() != shape.size()) {
throw std::runtime_error("Number of indices doesn't match tensor dimensions");
}
int index = 0;
for (size_t i = 0; i < indices.size(); i++) {
if (indices[i] >= shape[i]) {
throw std::runtime_error("Index out of bounds");
}
index += indices[i] * strides[i];
}
return data[index];
}
}