6#include <gtest/gtest.h>
11 .NumInputs(1).NumOutputs(1)
12 .SetDoc(R
"DOC(Test Documentation)DOC")
13 .Input(0, "in0",
"dummy input.")
14 .Output(0,
"out0",
"dummy output.");
16TEST(OperatorSchemaTest, BasicSchema) {
18#ifdef CAFFE2_NO_OPERATOR_SCHEMA
19 EXPECT_TRUE(
schema ==
nullptr);
22 EXPECT_TRUE(
schema !=
nullptr);
23 EXPECT_TRUE(
schema->doc() !=
nullptr);
26 vector<string>{
"in"}, vector<string>{
"out"});
27 EXPECT_TRUE(
schema->Verify(def1));
30 vector<string>{
"in1",
"in2"}, vector<string>{
"out"});
31 EXPECT_FALSE(
schema->Verify(def2));
34 vector<string>{
"in"}, vector<string>{
"out1",
"out2"});
35 EXPECT_FALSE(
schema->Verify(def3));
41TEST(OperatorSchemaTest, SpecifiedInputOutput) {
44#ifdef CAFFE2_NO_OPERATOR_SCHEMA
45 EXPECT_TRUE(
schema ==
nullptr);
48 EXPECT_TRUE(
schema !=
nullptr);
50 "OpSchemaSpecifiedInputOutputOp",
"",
51 vector<string>{
"in"}, vector<string>{
"out"});
52 EXPECT_FALSE(
schema->Verify(def1));
54 "OpSchemaSpecifiedInputOutputOp",
"",
55 vector<string>{
"in1",
"in2"}, vector<string>{
"out"});
56 EXPECT_TRUE(
schema->Verify(def2));
58 "OpSchemaSpecifiedInputOutputOp",
"",
59 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2"});
60 EXPECT_FALSE(
schema->Verify(def3));
64 .NumInputsOutputs([](
int in,
int out) {
68TEST(OperatorSchemaTest, InputOutputRelation) {
71#ifdef CAFFE2_NO_OPERATOR_SCHEMA
72 EXPECT_TRUE(
schema ==
nullptr);
75 EXPECT_TRUE(
schema !=
nullptr);
77 "OpSchemaInputOutputRelationOp",
"",
78 vector<string>{
"in"}, vector<string>{
"out"});
79 EXPECT_TRUE(
schema->Verify(def1));
81 "OpSchemaInputOutputRelationOp",
"",
82 vector<string>{
"in"}, vector<string>{
"out1",
"out2"});
83 EXPECT_TRUE(
schema->Verify(def2));
85 "OpSchemaInputOutputRelationOp",
"",
86 vector<string>{
"in1",
"in2",
"in3"}, vector<string>{
"out1",
"out2"});
87 EXPECT_FALSE(
schema->Verify(def3));
91 .SameNumberOfOutput();
93TEST(OperatorSchemaTest, SameInputOutput) {
96#ifdef CAFFE2_NO_OPERATOR_SCHEMA
97 EXPECT_TRUE(
schema ==
nullptr);
101 "OpSchemaSameInputOutputOp",
"",
102 vector<string>{
"in"}, vector<string>{
"out"});
103 EXPECT_TRUE(
schema->Verify(def1));
105 "OpSchemaSameInputOutputOp",
"",
106 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2"});
107 EXPECT_TRUE(
schema->Verify(def2));
109 "OpSchemaSameInputOutputOp",
"",
110 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2",
"out3"});
111 EXPECT_FALSE(
schema->Verify(def3));
115 .NumInputs(1, 5).NumOutputs(2, 6)
116 .OutputCalculator([](
int n) {
return n + 1; });
118TEST(OperatorSchemaTest, CalculateOutput) {
121#ifdef CAFFE2_NO_OPERATOR_SCHEMA
122 EXPECT_TRUE(
schema ==
nullptr);
126 "OpSchemaCalculateOutputOp",
"",
127 vector<string>{
"in"}, vector<string>{
"out"});
128 EXPECT_FALSE(
schema->Verify(def1));
130 "OpSchemaCalculateOutputOp",
"",
131 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2"});
132 EXPECT_FALSE(
schema->Verify(def2));
134 "OpSchemaCalculateOutputOp",
"",
135 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2",
"out3"});
136 EXPECT_TRUE(
schema->Verify(def3));
140 .NumInputs(2).NumOutputs(2)
141 .AllowInplace({{0, 0}})
144TEST(OperatorSchemaTest, Inplace) {
147#ifdef CAFFE2_NO_OPERATOR_SCHEMA
148 EXPECT_TRUE(
schema ==
nullptr);
152 "OpSchemaInplace",
"",
153 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"in2"});
154 EXPECT_TRUE(
schema->Verify(def1));
156 "OpSchemaInplace",
"",
157 vector<string>{
"in1",
"in2"}, vector<string>{
"in1",
"in2"});
158 EXPECT_TRUE(
schema->Verify(def2));
160 "OpSchemaInplace",
"",
161 vector<string>{
"in1",
"in2"}, vector<string>{
"in1",
"out2"});
162 EXPECT_FALSE(
schema->Verify(def3));
164 "OpSchemaInplace",
"",
165 vector<string>{
"in1",
"in2"}, vector<string>{
"out1",
"out2"});
166 EXPECT_FALSE(
schema->Verify(def4));
171TEST(OperatorSchemaTest, TensorInferenceIdentical) {
174#ifdef CAFFE2_NO_OPERATOR_SCHEMA
175 EXPECT_TRUE(
schema ==
nullptr);
179 "OpSchemaSameInputOutputTensorInference",
181 vector<string>{
"in"},
182 vector<string>{
"out"});
185 shapes[0].add_dims(1);
186 shapes[0].add_dims(2);
187 shapes[0].add_dims(3);
189 EXPECT_EQ(
out.size(), 1);
190 EXPECT_EQ(
out[0].SerializeAsString(), shapes[0].SerializeAsString());
194 .TensorInferenceFunction(
198 shapes[0].add_dims(1701);
202TEST(OperatorSchemaTest, TensorInferenceArbitrary) {
205#ifdef CAFFE2_NO_OPERATOR_SCHEMA
206 EXPECT_TRUE(
schema ==
nullptr);
210 "OpSchemaArbitraryTensorInference",
212 vector<string>{
"in"},
213 vector<string>{
"out"});
215 EXPECT_EQ(
out.size(), 1);
217 EXPECT_EQ(
out[0].dims_size(), 1);
218 EXPECT_EQ(
out[0].
dims(0), 1701);
221TEST(OperatorSchemaTest, TestCastSchema) {
226#ifdef CAFFE2_NO_OPERATOR_SCHEMA
227 EXPECT_TRUE(
schema ==
nullptr);
237 vector<string>{
"in"},
238 vector<string>{
"out"},
241 EXPECT_EQ(
out.size(), 1);
245 EXPECT_EQ(
out[0].dims_size(), 0);
251 .CostInferenceFunction([](
const OperatorDef& ,
258TEST(OperatorSchemaTest, TestCostInference) {
260#ifdef CAFFE2_NO_OPERATOR_SCHEMA
261 EXPECT_TRUE(
schema ==
nullptr);
268 "OpSchemaCostInference",
"", vector<string>{
"in"}, vector<string>{
"out"});
271 shapes[0].add_dims(10);
272 shapes[0].add_dims(10);
274 shapes[1].add_dims(10);
275 shapes[1].add_dims(10);
276 EXPECT_EQ(2000,
schema->InferCost(def, shapes).flops);
static const OpSchema * Schema(const string &key)
A class to record the schema of an op.
Copyright (c) 2016-present, Facebook, Inc.
const vector< TensorShape > & inputs
return vector< TensorShape >
const vector< TensorShape > & in
INT_MAX NumOutputs(1, INT_MAX)
*and produces a single output tensor *expanded *The op also takes an argument *dims *with a list of dimensions for where to add the single dimensional entries If the same blob is provided as input and the operation is copy free This is the exact inverse operation of *Squeeze *Github dims
OperatorDef CreateOperatorDef(const string &type, const string &name, const IterableInputs &inputs, const IterableOutputs &outputs, const IterableArgs &args, const DeviceOption &device_option=DeviceOption(), const string &engine="")
TEST(CommonTest, TestStoi)
Module caffe2.python.schema.