forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_schema_test.cc
279 lines (257 loc) · 9.12 KB
/
operator_schema_test.cc
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
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/operator_schema.h"
#include "caffe2/utils/proto_utils.h"
#include <gtest/gtest.h>
namespace caffe2 {
OPERATOR_SCHEMA(OpSchemaTestOp)
.NumInputs(1).NumOutputs(1)
.SetDoc(R"DOC(Test Documentation)DOC")
.Input(0, "in0", "dummy input.")
.Output(0, "out0", "dummy output.");
TEST(OperatorSchemaTest, BasicSchema) {
const OpSchema* schema = OpSchemaRegistry::Schema("OpSchemaTestOp");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
EXPECT_TRUE(schema != nullptr);
EXPECT_TRUE(schema->doc() != nullptr);
OperatorDef def1 = CreateOperatorDef(
"OpSchemaTestOp", "",
vector<string>{"in"}, vector<string>{"out"});
EXPECT_TRUE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaTestOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out"});
EXPECT_FALSE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaTestOp", "",
vector<string>{"in"}, vector<string>{"out1", "out2"});
EXPECT_FALSE(schema->Verify(def3));
}
OPERATOR_SCHEMA(OpSchemaSpecifiedInputOutputOp)
.NumInputs({2, 4}).NumOutputs({1, 3});
TEST(OperatorSchemaTest, SpecifiedInputOutput) {
const OpSchema* schema
= OpSchemaRegistry::Schema("OpSchemaSpecifiedInputOutputOp");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
EXPECT_TRUE(schema != nullptr);
OperatorDef def1 = CreateOperatorDef(
"OpSchemaSpecifiedInputOutputOp", "",
vector<string>{"in"}, vector<string>{"out"});
EXPECT_FALSE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaSpecifiedInputOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out"});
EXPECT_TRUE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaSpecifiedInputOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2"});
EXPECT_FALSE(schema->Verify(def3));
}
OPERATOR_SCHEMA(OpSchemaInputOutputRelationOp)
.NumInputsOutputs([](int in, int out) {
return out == in || out == in * 2;
});
TEST(OperatorSchemaTest, InputOutputRelation) {
const OpSchema* schema
= OpSchemaRegistry::Schema("OpSchemaInputOutputRelationOp");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
EXPECT_TRUE(schema != nullptr);
OperatorDef def1 = CreateOperatorDef(
"OpSchemaInputOutputRelationOp", "",
vector<string>{"in"}, vector<string>{"out"});
EXPECT_TRUE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaInputOutputRelationOp", "",
vector<string>{"in"}, vector<string>{"out1", "out2"});
EXPECT_TRUE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaInputOutputRelationOp", "",
vector<string>{"in1", "in2", "in3"}, vector<string>{"out1", "out2"});
EXPECT_FALSE(schema->Verify(def3));
}
OPERATOR_SCHEMA(OpSchemaSameInputOutputOp)
.SameNumberOfOutput();
TEST(OperatorSchemaTest, SameInputOutput) {
const OpSchema* schema =
OpSchemaRegistry::Schema("OpSchemaSameInputOutputOp");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
OperatorDef def1 = CreateOperatorDef(
"OpSchemaSameInputOutputOp", "",
vector<string>{"in"}, vector<string>{"out"});
EXPECT_TRUE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaSameInputOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2"});
EXPECT_TRUE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaSameInputOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2", "out3"});
EXPECT_FALSE(schema->Verify(def3));
}
OPERATOR_SCHEMA(OpSchemaCalculateOutputOp)
.NumInputs(1, 5).NumOutputs(2, 6)
.OutputCalculator([](int n) { return n + 1; });
TEST(OperatorSchemaTest, CalculateOutput) {
const OpSchema* schema =
OpSchemaRegistry::Schema("OpSchemaCalculateOutputOp");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
OperatorDef def1 = CreateOperatorDef(
"OpSchemaCalculateOutputOp", "",
vector<string>{"in"}, vector<string>{"out"});
EXPECT_FALSE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaCalculateOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2"});
EXPECT_FALSE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaCalculateOutputOp", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2", "out3"});
EXPECT_TRUE(schema->Verify(def3));
}
OPERATOR_SCHEMA(OpSchemaInplace)
.NumInputs(2).NumOutputs(2)
.AllowInplace({{0, 0}})
.EnforceInplace({{1, 1}});
TEST(OperatorSchemaTest, Inplace) {
const OpSchema* schema =
OpSchemaRegistry::Schema("OpSchemaInplace");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
OperatorDef def1 = CreateOperatorDef(
"OpSchemaInplace", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "in2"});
EXPECT_TRUE(schema->Verify(def1));
OperatorDef def2 = CreateOperatorDef(
"OpSchemaInplace", "",
vector<string>{"in1", "in2"}, vector<string>{"in1", "in2"});
EXPECT_TRUE(schema->Verify(def2));
OperatorDef def3 = CreateOperatorDef(
"OpSchemaInplace", "",
vector<string>{"in1", "in2"}, vector<string>{"in1", "out2"});
EXPECT_FALSE(schema->Verify(def3));
OperatorDef def4 = CreateOperatorDef(
"OpSchemaInplace", "",
vector<string>{"in1", "in2"}, vector<string>{"out1", "out2"});
EXPECT_FALSE(schema->Verify(def4));
}
OPERATOR_SCHEMA(OpSchemaSameInputOutputTensorInference).IdenticalTypeAndShape();
TEST(OperatorSchemaTest, TensorInferenceIdentical) {
const OpSchema* schema =
OpSchemaRegistry::Schema("OpSchemaSameInputOutputTensorInference");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
OperatorDef def = CreateOperatorDef(
"OpSchemaSameInputOutputTensorInference",
"",
vector<string>{"in"},
vector<string>{"out"});
vector<TensorShape> shapes(1);
shapes[0].set_data_type(TensorProto::FLOAT);
shapes[0].add_dims(1);
shapes[0].add_dims(2);
shapes[0].add_dims(3);
vector<TensorShape> out = schema->InferTensor(def, shapes);
EXPECT_EQ(out.size(), 1);
EXPECT_EQ(out[0].SerializeAsString(), shapes[0].SerializeAsString());
}
OPERATOR_SCHEMA(OpSchemaArbitraryTensorInference)
.TensorInferenceFunction(
[](const OperatorDef&, const vector<TensorShape>&) {
vector<TensorShape> shapes(1);
shapes[0].set_data_type(TensorProto::FLOAT);
shapes[0].add_dims(1701);
return shapes;
});
TEST(OperatorSchemaTest, TensorInferenceArbitrary) {
const OpSchema* schema =
OpSchemaRegistry::Schema("OpSchemaArbitraryTensorInference");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
OperatorDef def = CreateOperatorDef(
"OpSchemaArbitraryTensorInference",
"",
vector<string>{"in"},
vector<string>{"out"});
vector<TensorShape> out = schema->InferTensor(def, vector<TensorShape>());
EXPECT_EQ(out.size(), 1);
EXPECT_EQ(out[0].data_type(), TensorProto::FLOAT);
EXPECT_EQ(out[0].dims_size(), 1);
EXPECT_EQ(out[0].dims(0), 1701);
}
TEST(OperatorSchemaTest, TestCastSchema) {
// This tests a use case of the schema: the Cast op takes in the def and
// deduces the
// schema from the "to" argument.
const OpSchema* schema = OpSchemaRegistry::Schema("Cast");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
if (!schema) {
// Compiled without the Cast op.
return;
}
OperatorDef def = CreateOperatorDef(
"Cast",
"",
vector<string>{"in"},
vector<string>{"out"},
vector<Argument>{MakeArgument<int64_t>("to", TensorProto::UINT8)});
vector<TensorShape> out = schema->InferTensor(def, vector<TensorShape>(1));
EXPECT_EQ(out.size(), 1);
// Data type should be inferred.
EXPECT_EQ(out[0].data_type(), TensorProto::UINT8);
// Dim should not be set (same as input);
EXPECT_EQ(out[0].dims_size(), 0);
}
OPERATOR_SCHEMA(OpSchemaCostInference)
.NumInputs(2)
.NumOutputs(2)
.CostInferenceFunction([](const OperatorDef& /*def*/,
const vector<TensorShape>& inputs) {
struct OpSchema::Cost c;
c.flops = 2 * inputs[0].dims(0) * inputs[0].dims(1) * inputs[1].dims(1);
return c;
});
TEST(OperatorSchemaTest, TestCostInference) {
const OpSchema* schema = OpSchemaRegistry::Schema("OpSchemaCostInference");
#ifdef CAFFE2_NO_OPERATOR_SCHEMA
EXPECT_TRUE(schema == nullptr);
return;
#endif
if (!schema) {
return;
}
OperatorDef def = CreateOperatorDef(
"OpSchemaCostInference", "", vector<string>{"in"}, vector<string>{"out"});
vector<TensorShape> shapes(2);
shapes[0].set_data_type(TensorProto::FLOAT);
shapes[0].add_dims(10);
shapes[0].add_dims(10);
shapes[1].set_data_type(TensorProto::FLOAT);
shapes[1].add_dims(10);
shapes[1].add_dims(10);
EXPECT_EQ(2000, schema->InferCost(def, shapes).flops);
}
} // namespace caffe2