var_type_inference_test.cc 3.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Y
Yu Yang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/var_type_inference.h"
16
#include <string>
Y
Yu Yang 已提交
17
#include "gtest/gtest.h"
Y
Yi Wang 已提交
18 19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/program_desc.h"
Y
Yu Yang 已提交
21 22 23 24

namespace paddle {
namespace framework {

Y
yuyang18 已提交
25 26 27 28 29 30 31 32 33 34 35
class NOP : public OperatorBase {
 public:
  NOP(const std::string &type, const VariableNameMap &inputs,
      const VariableNameMap &outputs, const AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}

 private:
  void RunImpl(const Scope &scope,
               const platform::Place &place) const override {}
};

Y
Yu Yang 已提交
36 37
class SumOpMaker : public OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
38
  void Make() {
Y
Yu Yang 已提交
39 40 41 42 43 44 45 46
    AddInput("X", "").AsDuplicable();
    AddOutput("Out", "");
    AddComment("");
  }
};

class SumOpVarTypeInference : public VarTypeInference {
 public:
M
minqiyang 已提交
47 48
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto &inputs = ctx->Input("X");
49
    auto default_var_type = proto::VarType::SELECTED_ROWS;
Y
Yu Yang 已提交
50 51

    bool any_input_is_lod_tensor = std::any_of(
M
minqiyang 已提交
52
        inputs.begin(), inputs.end(), [&ctx](const std::string &name) {
M
minqiyang 已提交
53
          return ctx->GetType(name) == proto::VarType::LOD_TENSOR;
Y
Yu Yang 已提交
54 55
        });
    if (any_input_is_lod_tensor) {
56
      default_var_type = proto::VarType::LOD_TENSOR;
Y
Yu Yang 已提交
57
    }
Y
Yu Yang 已提交
58

M
minqiyang 已提交
59 60
    auto out_var_name = ctx->Output("Out").front();
    ctx->SetType(out_var_name, default_var_type);
Y
Yu Yang 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }
};
}  // namespace framework
}  // namespace paddle

REGISTER_OPERATOR(sum, paddle::framework::NOP, paddle::framework::SumOpMaker,
                  paddle::framework::SumOpVarTypeInference);
REGISTER_OPERATOR(sum_without_infer_var_type, paddle::framework::NOP,
                  paddle::framework::SumOpMaker);

namespace paddle {
namespace framework {

TEST(InferVarType, sum_op) {
Y
Yu Yang 已提交
75
  ProgramDesc prog;
76
  auto *op = prog.MutableBlock(0)->AppendOp();
Y
Yu Yang 已提交
77 78 79 80
  op->SetType("sum");
  op->SetInput("X", {"test_a", "test_b", "test_c"});
  op->SetOutput("Out", {"test_out"});

81 82 83
  prog.MutableBlock(0)->Var("test_a")->SetType(proto::VarType::SELECTED_ROWS);
  prog.MutableBlock(0)->Var("test_b")->SetType(proto::VarType::SELECTED_ROWS);
  prog.MutableBlock(0)->Var("test_c")->SetType(proto::VarType::SELECTED_ROWS);
84
  prog.MutableBlock(0)->Var("test_out");
Y
Yu Yang 已提交
85

86
  op->InferVarType(prog.MutableBlock(0));
Y
Yu Yang 已提交
87

88
  ASSERT_EQ(proto::VarType::SELECTED_ROWS,
89
            prog.MutableBlock(0)->Var("test_out")->GetType());
Y
Yu Yang 已提交
90

91
  prog.MutableBlock(0)->Var("test_b")->SetType(proto::VarType::LOD_TENSOR);
92
  op->InferVarType(prog.MutableBlock(0));
93
  ASSERT_EQ(proto::VarType::LOD_TENSOR,
94
            prog.MutableBlock(0)->Var("test_out")->GetType());
Y
Yu Yang 已提交
95 96 97
}

TEST(InferVarType, sum_op_without_infer_var_type) {
Y
Yu Yang 已提交
98
  ProgramDesc prog;
99
  auto *op = prog.MutableBlock(0)->AppendOp();
Y
Yu Yang 已提交
100 101 102 103
  op->SetType("sum_without_infer_var_type");
  op->SetInput("X", {"test2_a", "test2_b", "test2_c"});
  op->SetOutput("Out", {"test2_out"});

104 105 106
  prog.MutableBlock(0)->Var("test2_a")->SetType(proto::VarType::SELECTED_ROWS);
  prog.MutableBlock(0)->Var("test2_b")->SetType(proto::VarType::SELECTED_ROWS);
  prog.MutableBlock(0)->Var("test2_c")->SetType(proto::VarType::SELECTED_ROWS);
107
  prog.MutableBlock(0)->Var("test2_out");
Y
Yu Yang 已提交
108

109
  op->InferVarType(prog.MutableBlock(0));
Y
Yu Yang 已提交
110

S
sneaxiy 已提交
111
  ASSERT_EQ(proto::VarType::LOD_TENSOR,
112
            prog.MutableBlock(0)->Var("test2_out")->GetType());
Y
Yu Yang 已提交
113 114 115
}

}  // namespace framework
116
}  // namespace paddle