载入中...
搜索中...
未找到
Tensor.h
浏览该文件的文档.
1#ifndef EVE_TENSOR_TENSOR_H
2#define EVE_TENSOR_TENSOR_H
3
4#include <cstdint>
5#include <string>
6#include <vector>
7
8namespace eve::tensor {
9
10class Graph;
11class Func;
12class TF;
13
22enum class DType : uint8_t {
23 Float32 = 0,
24 Int32 = 1,
25 Fp16 = 2, // IEEE half, 2 bytes/elem, weight-only
26 Fp8E4M3 = 3, // 1 byte/elem, weight-only
27 Fp4E2M1 = 4, // 4-bit e2m1, two per byte, weight-only
28 Int8 = 5, // 1 byte/elem + per-group scale, weight-only
29 Int4 = 6, // 4-bit + per-group scale, weight-only
30};
31
32namespace q {
33bool isQuantDType(DType dt);
34}
35
36const char *dtypeName(DType dtype);
37bool parseDType(const std::string &name, DType &out);
38
43class Tensor {
44public:
45 static constexpr int kMaxRank = 6;
46
47 Tensor() = default;
49 explicit Tensor(const int *dims, int rank);
50 explicit Tensor(DType dtype, const int *dims, int rank);
52 Tensor(int d0);
53 Tensor(int d0, int d1);
54 Tensor(int d0, int d1, int d2);
55 Tensor(int d0, int d1, int d2, int d3);
56 Tensor(int d0, int d1, int d2, int d3, int d4);
57 Tensor(int d0, int d1, int d2, int d3, int d4, int d5);
58
60 static Tensor *makeSymbolic(Graph *graph, int nodeId, const int *dims, int rank);
61
62 bool isSymbolic() const { return kind_ == Kind::Symbolic; }
63 bool isEager() const { return kind_ == Kind::Eager; }
64 Graph *graph() const { return graph_; }
65 int nodeId() const { return nodeId_; }
66
67 int getRank() const { return rank_; }
68 int getSize() const { return size_; }
69 int getDim(int axis) const;
70 int getDim0() const { return dims_[0]; }
71 int getDim1() const { return dims_[1]; }
72 int getDim2() const { return dims_[2]; }
73 int getDim3() const { return dims_[3]; }
74 int getDim4() const { return dims_[4]; }
75 int getDim5() const { return dims_[5]; }
76 std::string getDevice() const { return device_; }
77 std::string getDtype() const { return dtypeName(dtype_); }
78 DType dtype() const { return dtype_; }
79 void setDtype(DType dtype) { dtype_ = dtype; }
80
82 bool isQuantized() const { return q::isQuantDType(dtype_); }
83
85 std::vector<float> dequantized() const;
86
88 const std::vector<float> &qScales() const { return qScales_; }
89 int qGroup() const { return qGroup_; }
90 const std::vector<uint8_t> &qBytes() const { return bytes_; }
91
92 float get(int flatIndex) const;
93 void set(int flatIndex, float value);
94 float get1(int i0) const;
95 void set1(int i0, float value);
96 float get2(int i0, int i1) const;
97 void set2(int i0, int i1, float value);
98 float get3(int i0, int i1, int i2) const;
99 void set3(int i0, int i1, int i2, float value);
100 float get4(int i0, int i1, int i2, int i3) const;
101 void set4(int i0, int i1, int i2, int i3, float value);
102 float get5(int i0, int i1, int i2, int i3, int i4) const;
103 void set5(int i0, int i1, int i2, int i3, int i4, float value);
104 float get6(int i0, int i1, int i2, int i3, int i4, int i5) const;
105 void set6(int i0, int i1, int i2, int i3, int i4, int i5, float value);
106
107 void fill(float value);
108 void copyFrom(const Tensor *other);
109 Tensor *clone() const;
110
112 Tensor *add(const Tensor *other) const;
113 Tensor *sub(const Tensor *other) const;
114 Tensor *multiply(const Tensor *other) const;
115 Tensor *div(const Tensor *other) const;
116 Tensor *addScalar(float s) const;
117 Tensor *subScalar(float s) const;
118 Tensor *mulScalar(float s) const;
119 Tensor *divScalar(float s) const;
120 Tensor *neg() const;
121 Tensor *abs() const;
122 Tensor *sqrt() const;
123 Tensor *exp() const;
124 Tensor *log() const;
125 Tensor *sin() const;
126 Tensor *cos() const;
127 Tensor *tanh() const;
128 Tensor *relu() const;
129 Tensor *sigmoid() const;
130 Tensor *gelu() const;
131 Tensor *silu() const;
132 Tensor *powScalar(float exp) const;
133 Tensor *clamp(float lo, float hi) const;
134 Tensor *maximumScalar(float s) const;
135 Tensor *minimumScalar(float s) const;
136
138 void addInPlace(const Tensor *other);
139 void multiplyInPlace(const Tensor *other);
140 void addScalarInPlace(float s);
141 void mulScalarInPlace(float s);
142 void reluInPlace();
143
145 float reduceSum() const;
146 float reduceMean() const;
147 float reduceMin() const;
148 float reduceMax() const;
149 float dot(const Tensor *other) const;
150
152 Tensor *matmul(const Tensor *other) const;
153 Tensor *transpose() const;
154 Tensor *permute(const int *order, int rank) const;
155 Tensor *reshape1(int d0) const;
156 Tensor *reshape2(int d0, int d1) const;
157 Tensor *reshape3(int d0, int d1, int d2) const;
158 Tensor *reshape4(int d0, int d1, int d2, int d3) const;
159 Tensor *reshape5(int d0, int d1, int d2, int d3, int d4) const;
160 Tensor *reshape6(int d0, int d1, int d2, int d3, int d4, int d5) const;
161 Tensor *flatten() const;
162
164 float *data();
165 const float *data() const;
166
167 void ensureEager(const char *op) const;
168
169 static int product(const int *dims, int rank);
170
171private:
172 friend class TF;
173 friend class Func;
174 friend class CompiledFunction;
175 friend class Graph;
176
177 enum class Kind { Eager, Symbolic };
178
179 void initDims(DType dtype, const int *dims, int rank);
180 void checkSameShape(const Tensor *other, const char *op) const;
181 int offset2(int i0, int i1) const;
182 int offset3(int i0, int i1, int i2) const;
183 int offset4(int i0, int i1, int i2, int i3) const;
184 int offset5(int i0, int i1, int i2, int i3, int i4) const;
185 int offset6(int i0, int i1, int i2, int i3, int i4, int i5) const;
186
187 Kind kind_ = Kind::Eager;
188 Graph *graph_ = nullptr;
189 int nodeId_ = -1;
190 int rank_ = 0;
191 int dims_[kMaxRank] = {0, 0, 0, 0, 0, 0};
192 int size_ = 0;
193 DType dtype_ = DType::Float32;
194 std::vector<float> data_;
195 std::vector<uint8_t> bytes_; // packed payload for quantized dtypes
196 std::vector<float> qScales_; // per-group scales (int8/int4)
197 int qGroup_ = 0; // elements per scale group
198 std::string device_ = "cpu";
199};
200
201} // namespace eve::tensor
202
203#endif // EVE_TENSOR_TENSOR_H
std::string value
uint32_t i1
Definition Grass.cpp:62
uint32_t i2
Definition Grass.cpp:62
uint32_t i0
Definition Grass.cpp:62
const char * name
Definition RockMesh.cpp:21
uint32_t s
Definition Weather.cpp:28
Optimized / scheduled graph ready to run with feeds.
Definition Graph.h:191
Trace builder — TF2 tf.function analogue (tf.func in scripts). While active, TF ops record into this ...
Definition Graph.h:125
TF2-like namespace module. Script: tf <- eve.TF(); Default eager; tf.func() traces a graph for compil...
Definition TF.h:18
float32 / int32 tensor (rank 1–6), row-major. Eager: owns a buffer. Symbolic: node in a Func graph (n...
Definition Tensor.h:43
Tensor * tanh() const
Tensor * mulScalar(float s) const
Definition Tensor.cpp:338
const std::vector< float > & qScales() const
Definition Tensor.h:88
Tensor * reshape5(int d0, int d1, int d2, int d3, int d4) const
Definition Tensor.cpp:596
Tensor * sub(const Tensor *other) const
Definition Tensor.cpp:320
std::string getDtype() const
Definition Tensor.h:77
float reduceSum() const
归约:求和 / 均值 / 最小 / 最大。
Definition Tensor.cpp:437
Tensor * sin() const
int getDim4() const
Definition Tensor.h:74
int qGroup() const
Definition Tensor.h:89
bool isQuantized() const
Definition Tensor.h:82
void fill(float value)
Definition Tensor.cpp:267
void ensureEager(const char *op) const
Definition Tensor.cpp:136
Tensor * add(const Tensor *other) const
Eager 逐元素运算(符号张量会抛异常)。
Definition Tensor.cpp:319
void copyFrom(const Tensor *other)
Definition Tensor.cpp:272
void set3(int i0, int i1, int i2, float value)
Definition Tensor.cpp:238
float reduceMax() const
Definition Tensor.cpp:456
Tensor * log() const
void addScalarInPlace(float s)
Definition Tensor.cpp:418
static constexpr int kMaxRank
Definition Tensor.h:45
Tensor * reshape4(int d0, int d1, int d2, int d3) const
Definition Tensor.cpp:587
float get3(int i0, int i1, int i2) const
Definition Tensor.cpp:234
int getDim2() const
Definition Tensor.h:72
float get1(int i0) const
Definition Tensor.cpp:218
float get4(int i0, int i1, int i2, int i3) const
Definition Tensor.cpp:242
Tensor * sigmoid() const
int getDim3() const
Definition Tensor.h:73
std::vector< float > dequantized() const
Definition Tensor.cpp:210
Tensor * abs() const
const std::vector< uint8_t > & qBytes() const
Definition Tensor.h:90
std::string getDevice() const
Definition Tensor.h:76
void mulScalarInPlace(float s)
Definition Tensor.cpp:424
int getSize() const
Definition Tensor.h:68
float get5(int i0, int i1, int i2, int i3, int i4) const
Definition Tensor.cpp:250
Tensor * reshape6(int d0, int d1, int d2, int d3, int d4, int d5) const
Definition Tensor.cpp:605
Tensor * neg() const
Tensor * reshape2(int d0, int d1) const
Definition Tensor.cpp:569
float * data()
原始数据指针(eager)。
Definition Tensor.cpp:185
Tensor * relu() const
Tensor * clamp(float lo, float hi) const
Definition Tensor.cpp:392
Tensor * reshape1(int d0) const
Definition Tensor.cpp:561
float dot(const Tensor *other) const
Definition Tensor.cpp:464
int nodeId() const
Definition Tensor.h:65
Tensor * exp() const
Tensor * gelu() const
float get6(int i0, int i1, int i2, int i3, int i4, int i5) const
Definition Tensor.cpp:258
Tensor * divScalar(float s) const
Definition Tensor.cpp:344
Tensor * multiply(const Tensor *other) const
Definition Tensor.cpp:321
float get2(int i0, int i1) const
Definition Tensor.cpp:226
bool isSymbolic() const
Definition Tensor.h:62
int getDim1() const
Definition Tensor.h:71
int getDim0() const
Definition Tensor.h:70
Graph * graph() const
Definition Tensor.h:64
Tensor * powScalar(float exp) const
Definition Tensor.cpp:350
Tensor * subScalar(float s) const
Definition Tensor.cpp:332
int getRank() const
Definition Tensor.h:67
void set1(int i0, float value)
Definition Tensor.cpp:222
int getDim(int axis) const
Definition Tensor.cpp:141
Tensor * transpose() const
Definition Tensor.cpp:528
void set(int flatIndex, float value)
Definition Tensor.cpp:203
Tensor * clone() const
Definition Tensor.cpp:281
void set4(int i0, int i1, int i2, int i3, float value)
Definition Tensor.cpp:246
float get(int flatIndex) const
Definition Tensor.cpp:196
Tensor * maximumScalar(float s) const
Definition Tensor.cpp:356
Tensor * sqrt() const
Tensor * div(const Tensor *other) const
Definition Tensor.cpp:324
static Tensor * makeSymbolic(Graph *graph, int nodeId, const int *dims, int rank)
Symbolic handle into a graph node.
Definition Tensor.cpp:122
void set2(int i0, int i1, float value)
Definition Tensor.cpp:230
Tensor * reshape3(int d0, int d1, int d2) const
Definition Tensor.cpp:578
Tensor * silu() const
bool isEager() const
Definition Tensor.h:63
void multiplyInPlace(const Tensor *other)
Definition Tensor.cpp:409
DType dtype() const
Definition Tensor.h:78
Tensor * flatten() const
Definition Tensor.cpp:615
void setDtype(DType dtype)
Definition Tensor.h:79
Tensor * matmul(const Tensor *other) const
矩阵乘法 / 转置 / 变形。
Definition Tensor.cpp:475
Tensor * minimumScalar(float s) const
Definition Tensor.cpp:362
int getDim5() const
Definition Tensor.h:75
Tensor * addScalar(float s) const
Definition Tensor.cpp:326
float reduceMean() const
Definition Tensor.cpp:444
Tensor * permute(const int *order, int rank) const
Definition Tensor.cpp:545
static int product(const int *dims, int rank)
Definition Tensor.cpp:71
void addInPlace(const Tensor *other)
Eager 原地运算。
Definition Tensor.cpp:400
void set6(int i0, int i1, int i2, int i3, int i4, int i5, float value)
Definition Tensor.cpp:262
void set5(int i0, int i1, int i2, int i3, int i4, float value)
Definition Tensor.cpp:254
float reduceMin() const
Definition Tensor.cpp:448
Tensor * cos() const
bool isQuantDType(DType dt)
Definition Quant.h:16
bool parseDType(const std::string &name, DType &out)
Definition Tensor.cpp:39
DType
Tensor element types.
Definition Tensor.h:22
const char * dtypeName(DType dtype)
Definition Tensor.cpp:26