-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor_operation.cpp
More file actions
143 lines (131 loc) · 4.94 KB
/
Copy pathtensor_operation.cpp
File metadata and controls
143 lines (131 loc) · 4.94 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
#include <numeric>
#include "tensor.h"
namespace ts {
Tensor cat(std::vector<ts::Tensor> Tensors, int dim) {
// 检查输入列表是否为空
if (Tensors.empty()) {
throw std::invalid_argument("Input tensor list is empty");
}
// 获取输入张量的形状
std::vector<size_t> shape = Tensors[0].size();
// 增加指定维度的大小
shape[dim] = 0;
int total_size = 0;
for (const auto& tensor : Tensors) {
shape[dim] += tensor.size()[dim];
total_size += tensor.size()[0]*tensor.get_stride()[0];
}
// 创建输出张量的数据容器
std::vector<double> new_data;
if (dim == 0) {
for (const auto& tensor : Tensors) {
for (int i = 0; i < tensor.size()[0]*tensor.get_stride()[0]; ++i) {
new_data.push_back(tensor.data_ptr()[i]);
}
}
} else {
int a = Tensors[0].size()[0]*Tensors[0].get_stride()[0]/Tensors[0].get_stride()[dim-1];
for (int j = 0; j < a; ++j) {
for (const auto& tensor : Tensors) {
for (int i = 0; i < tensor.get_stride()[dim-1]; ++i) {
new_data.push_back(tensor.data_ptr()[i + j * tensor.get_stride()[dim-1]]);
}
}
}
}
return Tensor(shape, "double", new_data);
}
Tensor tile(Tensor tensor, std::vector<int> dims) {
if (dims.size() != tensor.size().size()) {
throw std::invalid_argument("the dimension of dims is wrong");
}
Tensor t = tensor;
for (int i = 0; i < dims.size(); ++i) {
int n = dims[i];
if (n != 1) {
std::vector<Tensor> a(n, t);
t = cat(a, i);
}
}
return t;
}
Tensor transpose(Tensor tensor, int dim1, int dim2) {
std::vector<size_t> new_shape;
std::vector<size_t> new_stride;
for (int i = 0; i < tensor.size().size(); ++i) {
if (i == dim1) {
new_shape.push_back(tensor.size()[dim2]);
} else if (i == dim2) {
new_shape.push_back(tensor.size()[dim1]);
} else {
new_shape.push_back(tensor.size()[i]);
}
}
for (int i = new_shape.size() - 1; i >= 0; --i) {
int a = 1;
for (int j = 0; j < i; ++j) {
a = a * new_shape[new_shape.size() - j - 1];
}
new_stride.push_back(a);
}
std::vector<double> new_data;
for (int i = 0; i < tensor.size()[0]*tensor.get_stride()[0]; ++i) {
std::vector<int> pos;
int a = i;
for (int j = 0; j < tensor.size().size(); ++j) {
int b = a/tensor.get_stride()[j];
pos.push_back(b);
a = a - b*tensor.get_stride()[j];
}
int fin = 0;
for (int j = 0; j < tensor.size().size(); ++j) {
if (j == dim1) {
fin += pos[dim2]*new_stride[dim1];
} else if (j == dim2) {
fin += pos[dim1]*new_stride[dim2];
} else {
fin += pos[j]*new_stride[j];
}
}
new_data.push_back(tensor.data_ptr()[fin]);
}
return Tensor(new_shape, "double", new_data);
}
Tensor permute(Tensor tensor, std::vector<int> dims) {
if (dims.size() != tensor.size().size()) {
throw std::invalid_argument("the dimension of dims is wrong");
}
std::vector<size_t> new_shape;
std::vector<size_t> new_stride;
for (int i = 0; i < tensor.size().size(); ++i) {
new_shape.push_back(tensor.size()[dims[i]]);
}
for (int i = new_shape.size() - 1; i >= 0; --i) {
int a = 1;
for (int j = 0; j < i; ++j) {
a = a * new_shape[new_shape.size() - j - 1];
}
new_stride.push_back(a);
}
std::vector<double> new_data;
for (int i = 0; i < tensor.size()[0]*tensor.get_stride()[0]; ++i) {
std::vector<int> pos;
int a = i;
for (int j = 0; j < tensor.size().size(); ++j) {
int b = a/tensor.get_stride()[j];
pos.push_back(b);
a = a - b*tensor.get_stride()[j];
}
int fin = 0;
for (int j = 0; j < tensor.size().size(); ++j) {
fin += pos[dims[j]]*new_stride[j];
}
new_data.push_back(tensor.data_ptr()[fin]);
}
return Tensor(new_shape, "double", new_data);
}
Tensor view(Tensor tensor, std::vector<size_t> shape) {
std::vector<double> a(tensor.data_ptr(), tensor.data_ptr() + tensor.size()[0]*tensor.get_stride()[0]);
return Tensor(shape, tensor.type(), a);
}
}