forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator_c10wrapper.h
332 lines (299 loc) · 11.6 KB
/
operator_c10wrapper.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#pragma once
#include <c10/core/dispatch/Dispatcher.h>
#include "caffe2/core/operator.h"
#include <c10/util/ArrayRef.h>
#include <c10/util/Metaprogramming.h>
namespace caffe2 {
namespace details {
template <size_t...>
struct true_t : std::true_type {};
template <class State>
inline std::shared_ptr<State> init_state() {
return std::make_shared<State>();
}
template <>
inline std::shared_ptr<void> init_state<void>() {
return std::shared_ptr<void>();
}
template <class T>
using is_output_arg = std::is_same<Tensor*, T>;
template <class ParameterDef>
using extract_type_t = typename ParameterDef::type;
} // namespace details
/**
* To make a c10 operator "C10Add" callable from caffe2 as "C2MyAddOpName", just
* write
*
* REGISTER_C10_OPERATOR_FOR_CAFFE2_DISPATCH(C10Add, C2MyAddOpName)
*
* Note: This wrapper currently only supports C10 ops that have exactly one
* output and take that in the last parameter as "Tensor* output".
* TODO: Figure out a better way to handle output parameters
*/
template <
class OpSchemaDef,
class Context,
class State,
bool use_array_input,
class ParameterDefTuple>
class C10OperatorWrapper final : public Operator<Context> {
using Schema = c10::OpSchema<OpSchemaDef>;
public:
static_assert(
c10::guts::is_instantiation_of<std::tuple, ParameterDefTuple>::value,
"");
using ParameterTuple =
c10::guts::typelist::to_tuple_t<c10::guts::typelist::map_t<
details::extract_type_t,
c10::guts::typelist::from_tuple_t<ParameterDefTuple>>>;
USE_OPERATOR_CONTEXT_FUNCTIONS;
static constexpr bool op_has_context_argument = std::is_same<
BaseContext*,
c10::guts::typelist::last_t<
typename Schema::signature::parameter_types>>::value;
static constexpr bool op_has_state_argument =
!std::is_same<void, State>::value;
C10OperatorWrapper(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws),
state_(details::init_state<State>()),
parameters_(parse_parameters_(
operator_def,
c10::guts::make_index_sequence<num_parameters()>())) {}
static constexpr size_t num_inputs() {
return Schema::signature::num_args - num_outputs() - num_parameters() -
(op_has_context_argument ? 1 : 0) - (op_has_state_argument ? 1 : 0);
}
static constexpr size_t num_parameters() {
return std::tuple_size<ParameterDefTuple>::value;
}
static constexpr size_t num_outputs() {
return c10::guts::typelist::count_if<
details::is_output_arg,
typename Schema::signature::parameter_types>::value;
}
bool RunOnDevice() override {
RunOnDevice_(
c10::guts::make_index_sequence<num_inputs()>(),
c10::guts::make_index_sequence<num_outputs()>(),
c10::guts::make_index_sequence<num_parameters()>());
return true;
}
private:
template <size_t... ParameterIndex>
ParameterTuple parse_parameters_(
const OperatorDef& operator_def,
c10::guts::index_sequence<ParameterIndex...>) {
return ParameterTuple{Parameter<ParameterIndex>(operator_def)...};
}
template <size_t Index>
details::extract_type_t<
typename std::tuple_element<Index, ParameterDefTuple>::type>
Parameter(const OperatorDef& operator_def) {
using Parameter =
typename std::tuple_element<Index, ParameterDefTuple>::type;
return Parameter::parse(ArgumentHelper(operator_def));
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && op_has_context_argument &&
op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
Input(InputIndex)...,
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
state_.get(),
static_cast<BaseContext*>(&context_));
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && op_has_context_argument &&
!op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
Input(InputIndex)...,
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
static_cast<BaseContext*>(&context_));
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && !op_has_context_argument &&
op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
Input(InputIndex)...,
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
state_.get());
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && !op_has_context_argument &&
!op_has_state_argument && !use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
Input(InputIndex)...,
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...);
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && op_has_context_argument &&
op_has_state_argument && use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
at::ArrayRef<const Tensor*>(array_inputs_()),
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
state_.get(),
static_cast<BaseContext*>(&context_));
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && op_has_context_argument &&
!op_has_state_argument && use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
at::ArrayRef<const Tensor*>(array_inputs_()),
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
static_cast<BaseContext*>(&context_));
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && !op_has_context_argument &&
op_has_state_argument && use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
at::ArrayRef<const Tensor*>(array_inputs_()),
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...,
state_.get());
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value && !op_has_context_argument &&
!op_has_state_argument && use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
c10::Dispatcher<OpSchemaDef>::call(
at::ArrayRef<const Tensor*>(array_inputs_()),
Output(OutputIndex)...,
std::get<ParameterIndex>(parameters_)...);
}
std::vector<const Tensor*> array_inputs_() {
std::vector<const Tensor*> result;
result.reserve(InputSize());
for (size_t i = 0; i < InputSize(); ++i) {
result.push_back(&Input(i));
}
return result;
}
std::shared_ptr<State> state_;
ParameterTuple parameters_;
};
template <class ParameterDef>
struct ParameterHelper final {
using type = typename ParameterDef::type;
static typename ParameterDef::type parse(const ArgumentHelper& helper) {
return helper.GetSingleArgument<typename ParameterDef::type>(
ParameterDef::name(), ParameterDef::default_value());
}
};
C10_DECLARE_REGISTRY(
C10OperatorRegistry,
OperatorBase,
const OperatorDef&,
Workspace*);
// TODO Currently we only register the CPU variant. This is going to be fixed
// once the tensor detemplatization lands.
#define REGISTER_C10_OPERATOR_FOR_CAFFE2_DISPATCH(OpSchemaDef, State, Name) \
C10_REGISTER_CLASS( \
C10OperatorRegistry, \
Name, \
C10OperatorWrapper<OpSchemaDef, CPUContext, State, false, std::tuple<>>)
#define REGISTER_C10_OPERATOR_FOR_CAFFE2_DISPATCH_WITH_PARAMETERS( \
OpSchemaDef, State, Name, ...) \
C10_REGISTER_CLASS( \
C10OperatorRegistry, \
Name, \
C10OperatorWrapper< \
OpSchemaDef, \
CPUContext, \
State, \
false, \
std::tuple<__VA_ARGS__>>)
#define REGISTER_C10_OPERATOR_FOR_CAFFE2_DISPATCH_WITH_ARRAY_INPUT( \
OpSchemaDef, State, Name) \
C10_REGISTER_CLASS( \
C10OperatorRegistry, \
Name, \
C10OperatorWrapper<OpSchemaDef, CPUContext, State, true, std::tuple<>>)
#define REGISTER_C10_OPERATOR_FOR_CAFFE2_DISPATCH_WITH_ARRAY_INPUT_AND_PARAMETERS( \
OpSchemaDef, State, Name, ...) \
C10_REGISTER_CLASS( \
C10OperatorRegistry, \
Name, \
C10OperatorWrapper< \
OpSchemaDef, \
CPUContext, \
State, \
true, \
std::tuple<__VA_ARGS__>>)
} // namespace caffe2