forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_c10wrapper.h
253 lines (224 loc) · 9.06 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
#pragma once
#include <ATen/core/dispatch/Dispatcher.h>
#include "caffe2/core/operator.h"
#include <c10/util/ArrayRef.h>
#include <c10/util/Metaprogramming.h>
#include <ATen/core/ivalue.h>
namespace caffe2 {
namespace details {
template <size_t...>
struct true_t : std::true_type {};
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_state_argument =
!std::is_same<void, State>::value;
C10OperatorWrapper(const OperatorDef& operator_def, Workspace* ws)
: Operator<Context>(operator_def, ws),
state_(make_intrusive<Blob>()),
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_state_argument ? 1 : 0);
}
static constexpr size_t num_parameters() {
return std::tuple_size<ParameterDefTuple>::value;
}
static constexpr size_t num_outputs() {
return Schema::signature::num_outputs;
}
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_state_argument && !use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
state_->GetMutable<State>(); // initialize state if not initialized yet
c10::Dispatcher<OpSchemaDef>::call(
ArrayRef<IValue>{
IValue(at::Tensor(C10Tensor(Input(InputIndex))))...,
IValue(at::Tensor(C10Tensor(*Output(OutputIndex))))...,
IValue(std::get<ParameterIndex>(parameters_))...,
IValue(state_)
});
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value &&
!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(
// TODO Make outputs be returned, not passed in
ArrayRef<IValue>{
IValue(at::Tensor(C10Tensor(Input(InputIndex))))...,
IValue(at::Tensor(C10Tensor(*Output(OutputIndex))))...,
IValue(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_state_argument && use_array_input,
void>
RunOnDevice_(
c10::guts::index_sequence<InputIndex...>,
c10::guts::index_sequence<OutputIndex...>,
c10::guts::index_sequence<ParameterIndex...>) {
state_->GetMutable<State>(); // initialize state if not initialized yet
c10::Dispatcher<OpSchemaDef>::call(
// TODO Make outputs be returned, not passed in
ArrayRef<IValue>{
IValue(at::ArrayRef<at::Tensor>(array_inputs_())),
IValue(at::Tensor(C10Tensor(*Output(OutputIndex))))...,
IValue(std::get<ParameterIndex>(parameters_))...,
IValue(state_)
});
}
template <
size_t... InputIndex,
size_t... OutputIndex,
size_t... ParameterIndex>
c10::guts::enable_if_t<
details::true_t<InputIndex...>::value &&
!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(
ArrayRef<IValue>{
IValue(ivalue::TensorList(array_inputs_())),
IValue(at::Tensor(C10Tensor(*Output(OutputIndex))))...,
IValue(std::get<ParameterIndex>(parameters_))...
});
}
std::vector<at::Tensor> array_inputs_() {
std::vector<at::Tensor> result;
result.reserve(InputSize());
for (size_t i = 0; i < InputSize(); ++i) {
result.push_back(at::Tensor(c10::C10Tensor(Input(i))));
}
return result;
}
intrusive_ptr<Blob> 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