-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensor.h
More file actions
197 lines (176 loc) · 4.87 KB
/
Tensor.h
File metadata and controls
197 lines (176 loc) · 4.87 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
#pragma once
#include "Log.h"
#include "Object.h"
#include "Util.h"
namespace mvm {
enum TypeCode {
TInt,
TFloat,
};
inline std::string type2Str(TypeCode code) {
static std::map<TypeCode, std::string> s = {
{TInt, "int"},
{TFloat, "float"},
};
return s[code];
}
class TensorNode : public ObjectNode {
};
class Tensor : public Object {
public:
Tensor() {
}
MVM_OBJECT_METHODS(Tensor, Object, TensorNode)
typedef TensorNode NodeType;
Tensor(int i) : mType(TInt) {
mV.i = i;
}
Tensor(float f) : mType(TFloat) {
mV.f = f;
}
#define TENSOR_SCALAR_UPCAST(Type) \
if (mType <= Type) {\
if (Type == TInt) { \
return mV.i; \
} else if (Type == TFloat) {\
switch(mType) { \
case TInt : \
return (int)mV.i; \
case TFloat: \
return mV.f; \
} \
} \
} else {\
MVM_CHECK(false, "cannot cast from %s to %s\n", type2Str(mType).c_str(), type2Str(Type).c_str()); \
}
operator int() const {
checkScalar();
TENSOR_SCALAR_UPCAST(TInt);
return mV.i;
}
operator float() const {
checkScalar();
TENSOR_SCALAR_UPCAST(TFloat);
return mV.f;
}
inline bool isScalar() const {
return mShape.empty();
}
inline void checkScalar() const {
MVM_CHECK(isScalar(), "This is not a scalar");
}
std::vector<int> shape() const {
return mShape;
}
TypeCode type() const {
return mType;
}
private:
union V {
char c;
int i;
float f;
double d;
} mV;
std::vector<int> mShape;
TypeCode mType;
};
#define DEFINE_TENSOR_BINARY_OPERATOR(OP) \
inline Tensor OP(int i, const Tensor& t) { \
return OP(Tensor(i), t); \
} \
\
inline Tensor OP(const Tensor& t, int i) { \
return OP(Tensor(i), t); \
}\
inline Tensor OP(float i, const Tensor& t) { \
return OP(Tensor(i), t); \
} \
\
inline Tensor OP(const Tensor& t, float i) { \
return OP(Tensor(i), t); \
}\
\
//MVM_CHECK(t1.type() == t2.type(), "Upcast not support now, t1:%s, t2:%s", type2Str(t1.type()).c_str(), type2Str(t2.type()).c_str());
#define DEFINE_TENSOR_BINARY_OPERATOR_T(OP) \
inline Tensor operator OP(const Tensor& t1, const Tensor& t2) { \
MVM_CHECK((t1.isScalar() == t2.isScalar()), "Broadcast not support now"); \
if (t1.isScalar()) { \
TypeCode maxType = t1.type() < t2.type() ? t2.type() : t1.type(); /*Upcast*/\
switch(maxType) { \
case TInt: \
printf("t1 "#OP" t2 : %d %d\n", (int)t1, (int)t2); \
return Tensor((int)t1 OP (int)t2); \
case TFloat: \
printf("t1 "#OP" t2 : %f %f\n", (float)t1, (float)t2); \
return Tensor((float)t1 OP (float)t2); \
default:\
return Tensor(-1); \
} \
} else { \
MVM_CHECK(t1.type() == t2.type(), "type should be same for array"); \
} \
} \
DEFINE_TENSOR_BINARY_OPERATOR_T(+);
DEFINE_TENSOR_BINARY_OPERATOR_T(-);
DEFINE_TENSOR_BINARY_OPERATOR_T(*);
DEFINE_TENSOR_BINARY_OPERATOR_T(/);
DEFINE_TENSOR_BINARY_OPERATOR(operator+);
DEFINE_TENSOR_BINARY_OPERATOR(operator-);
DEFINE_TENSOR_BINARY_OPERATOR(operator*);
DEFINE_TENSOR_BINARY_OPERATOR(operator/);
inline Tensor operator-(const Tensor& t) {
switch(t.type()) {
case TInt:
return Tensor(-(int)t);
case TFloat:
return Tensor(-(float)t);
default:
return Tensor(-1);
}
}
#define DEFINE_TENSOR_UNARY_OPERATOR(OP) \
inline Tensor OP(const Tensor& t) { \
if (t.isScalar()) { \
switch(t.type()) { \
case TInt: \
printf("t1 "#OP" : %d %f\n", (int)t, std::OP((int)t)); \
return Tensor((float)std::OP((int)t)); \
case TFloat: \
return Tensor(std::OP((float)t)); \
default:\
return Tensor(-1); \
} \
} \
return Tensor(-1); \
}\
DEFINE_TENSOR_UNARY_OPERATOR(log);
DEFINE_TENSOR_UNARY_OPERATOR(exp);
//class Buffer {
//public:
// Buffer() {
// }
// void resize(const std::vector<int>& shape) {
// int size = 0;
// if (!shapeSame(mShape, shape) && (size = shapeSize(shape))) {
// if (mBuffer != nullptr) {
// delete[] mBuffer;
// }
// mBuffer = new char[size];
// mShape = shape;
// }
//
// }
// const char* data() {
// MVM_CHECK(mBuffer, "Buffer is NULL");
// return mBuffer;
// }
// char* mutableData() {
// MVM_CHECK(mBuffer, "Buffer is NULL");
// return mBuffer;
// }
//private:
// std::vector<int> mShape;
// char* mBuffer{nullptr};
//};
} // namespace mvm