-
Notifications
You must be signed in to change notification settings - Fork 217
/
tensor_ops.hpp
206 lines (179 loc) · 7.23 KB
/
tensor_ops.hpp
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
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2017 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#ifndef GUARD_MIOPEN_TENSOR_OPPS_HPP_
#define GUARD_MIOPEN_TENSOR_OPPS_HPP_
#include <miopen/common.hpp>
#include <miopen/miopen.h>
#include <miopen/object.hpp>
#include <miopen/tensor.hpp>
#include <miopen/functional.hpp>
#include <vector>
#include <boost/range/combine.hpp>
#include <boost/range/adaptor/filtered.hpp>
namespace miopen {
struct Handle;
struct f_length_is_not_1_t
{
template <typename T>
bool operator()(T&& v)
{
return boost::get<0>(v) > 1;
}
};
TensorDescriptor GetFlattenedTensorDescriptor(const TensorDescriptor& desc);
template <typename... TDescriptors>
std::tuple<TDescriptors...>
GetConsistentFlattenedTensorDescriptors(const TDescriptors&... real_descriptor_pack)
{
constexpr std::size_t NTensor = sizeof...(TDescriptors);
std::integral_constant<std::size_t, NTensor> NTensorConstant;
std::array<const TensorDescriptor*, NTensor> real_descriptors{{(&real_descriptor_pack)...}};
#ifndef NDEBUG
// sanity check: all input TensorDescriptors should have the same GetLengths()
const auto& real_desc_0_lens = real_descriptors[0]->GetLengths();
for(std::size_t itensor = 1; itensor < NTensor; ++itensor)
{
if(real_desc_0_lens != real_descriptors[itensor]->GetLengths())
MIOPEN_THROW(miopenStatusBadParm, "Lengths of Tensors are different.");
}
#endif
// if tensors are all packed
bool is_all_packed = true;
for(std::size_t itensor = 0; itensor < NTensor; ++itensor)
is_all_packed &= real_descriptors[itensor]->IsPacked();
if(is_all_packed)
{
auto sz = real_descriptors[0]->GetElementSize();
return create_tuple<NTensor>([&](auto itensor) {
return TensorDescriptor{real_descriptors[itensor]->GetType(), {sz}, {1}};
});
}
// start flattening tensors
std::array<std::vector<std::size_t>, NTensor> array_of_flat_lengths;
std::array<std::vector<std::size_t>, NTensor> array_of_flat_strides;
auto non1_length_strides =
boost::combine(real_descriptors[0]->GetLengths(), real_descriptor_pack.GetStrides()...) |
boost::adaptors::filtered(f_length_is_not_1_t());
auto i = non1_length_strides.begin();
std::size_t flat_len = boost::get<0>(*i);
auto i_previous = i++;
// the 0-th dimension full-length doesn't matter
for(; i != non1_length_strides.end(); ++i)
{
std::size_t len = boost::get<0>(*i);
bool is_all_full_length = true;
repeat_n(
[&](auto itensor) {
std::size_t stride = boost::get<itensor + 1>(*i);
std::size_t previous_stride = boost::get<itensor + 1>(*i_previous);
std::size_t full_len = previous_stride / stride;
is_all_full_length &= (len == full_len);
},
NTensorConstant);
if(is_all_full_length)
{
flat_len *= len;
}
else
{
array_of_flat_lengths[0].push_back(flat_len);
repeat_n(
[&](auto itensor) {
std::size_t previous_stride = boost::get<itensor + 1>(*i_previous);
array_of_flat_strides[itensor].push_back(previous_stride);
},
NTensorConstant);
flat_len = len;
}
i_previous = i;
}
// lengths of all flattend tensors are the same
array_of_flat_lengths[0].push_back(flat_len);
// strides of all flattend tensors are different
repeat_n(
[&](auto itensor) {
std::size_t previous_stride = boost::get<itensor + 1>(*i_previous);
array_of_flat_strides[itensor].push_back(previous_stride);
},
NTensorConstant);
for(std::size_t itensor = 1; itensor < NTensor; ++itensor)
array_of_flat_lengths[itensor] = array_of_flat_lengths[0];
return create_tuple<NTensor>([&](auto itensor) {
return TensorDescriptor{real_descriptors[itensor]->GetType(),
std::move(array_of_flat_lengths[itensor]),
std::move(array_of_flat_strides[itensor])};
});
}
void ScaleTensor(const Handle& handle,
const TensorDescriptor& yDesc,
Data_t y,
const void* alpha,
int offset = 0);
void SetTensor(const Handle& handle,
const TensorDescriptor& yDesc,
Data_t y,
const void* alpha,
int offset = 0);
void OpTensor(const Handle& handle,
miopenTensorOp_t tensorOp,
const void* alpha0,
const TensorDescriptor& aTensorDesc,
ConstData_t ATensor,
const void* alpha1,
const TensorDescriptor& bTensorDesc,
ConstData_t BTensor,
const void* beta,
const TensorDescriptor& cTensorDesc,
Data_t CTensor,
size_t Aoffset = 0,
size_t Boffset = 0,
size_t Coffset = 0);
void CopyTensor(const Handle& handle,
const TensorDescriptor& srcDesc,
ConstData_t src,
const TensorDescriptor& dstDesc,
Data_t dst,
int srcOffset = 0,
int dstOffset = 0);
void CastTensor(const Handle& handle,
const void* alpha,
const TensorDescriptor& srcDesc,
ConstData_t src,
const TensorDescriptor& dstDesc,
Data_t dst,
int srcOffset = 0,
int dstOffset = 0);
void TransformTensor(const Handle& handle,
const void* alpha,
const TensorDescriptor& xDesc,
ConstData_t x,
const void* beta,
const TensorDescriptor& yDesc,
Data_t y,
size_t Xoffset = 0,
size_t Yoffset = 0);
} // namespace miopen
#endif // GUARD_MIOPEN_TENSOR_OPPS_HPP_