grad_op_builder_test.cc 8.8 KB
Newer Older
F
fengjiayi 已提交
1
#include "paddle/framework/grad_op_builder.h"
2 3 4 5
#include <gtest/gtest.h>
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"

6
USE_OP(sum);
7 8 9 10

namespace paddle {
namespace framework {

F
fengjiayi 已提交
11 12 13 14 15
class MutiInOutOpMaker : public OpProtoAndCheckerMaker {
 public:
  MutiInOutOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("In1", "a single input");
Y
Yu Yang 已提交
16
    AddInput("In2_mult", "a multiple input").AsDuplicable();
F
fengjiayi 已提交
17 18
    AddInput("In3", "another single input");
    AddOutput("Out1", "a single output");
Y
Yu Yang 已提交
19
    AddOutput("Out2_mult", "a multiple output").AsDuplicable();
F
fengjiayi 已提交
20 21 22 23 24 25 26 27 28
    AddComment("test op with multiple inputs and outputs");
  }
};

class IOIgnoredOpMaker : public OpProtoAndCheckerMaker {
 public:
  IOIgnoredOpMaker(OpProto *proto, OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("In1", "a single input");
29
    AddInput("In2_mult", "a multiple input").AsDuplicable().NotInGradient();
Y
Yu Yang 已提交
30 31
    AddInput("In3_mult", "another multiple input").AsDuplicable();
    AddOutput("Out1_mult", "a multiple output").AsDuplicable();
32
    AddOutput("Out2", "a single output").NotInGradient();
F
fengjiayi 已提交
33 34 35 36 37 38 39 40 41
    AddComment("op with inputs and outputs ignored in gradient calculating");
  }
};

}  // namespace framework
}  // namespace paddle

namespace f = paddle::framework;

F
fengjiayi 已提交
42
TEST(GradOpBuilder, AddTwo) {
Y
Yu Yang 已提交
43
  std::shared_ptr<f::OperatorBase> add_op(f::OpRegistry::CreateOp(
44
      "sum", {{"X", {"x", "y"}}}, {{"Out", {"out"}}}, {}));
F
fengjiayi 已提交
45 46
  std::shared_ptr<f::OperatorBase> grad_add_op =
      f::OpRegistry::CreateGradOp(*add_op);
47 48 49

  EXPECT_EQ(grad_add_op->Inputs().size(), 1UL);
  EXPECT_EQ(grad_add_op->Outputs().size(), 1UL);
50
  EXPECT_EQ(grad_add_op->Input(f::GradVarName("Out")), f::GradVarName("out"));
51 52 53 54 55 56 57 58 59 60 61
  auto &outputs = grad_add_op->Outputs(f::GradVarName("X"));
  EXPECT_EQ(2UL, outputs.size());
  auto in_output = [&outputs](const std::string &name) {
    for (auto &output_name : outputs) {
      if (output_name == name) return true;
    }
    return false;
  };

  EXPECT_TRUE(in_output(f::GradVarName("x")));
  EXPECT_TRUE(in_output(f::GradVarName("y")));
62 63
}

64 65
REGISTER_OP(mult_io, f::NOP, f::MutiInOutOpMaker, mult_io_grad, f::NOP);
REGISTER_OP(io_ignored, f::NOP, f::IOIgnoredOpMaker, io_ignored_grad, f::NOP);
F
fengjiayi 已提交
66 67 68

TEST(GradOpBuilder, MutiInOut) {
  std::shared_ptr<f::OperatorBase> test_op(f::OpRegistry::CreateOp(
Y
Yu Yang 已提交
69 70 71
      "mult_io", {{"In1", {"in1"}},
                  {"In2_mult", {"in2_1", "in2_2", "in2_3"}},
                  {"In3", {"in3"}}},
72
      {{"Out1", {"out1"}}, {"Out2_mult", {"out2_1", "out2_2"}}}, {}));
F
fengjiayi 已提交
73 74 75
  std::shared_ptr<f::OperatorBase> grad_test_op =
      f::OpRegistry::CreateGradOp(*test_op);

Q
qiaolongfei 已提交
76
  ASSERT_EQ(grad_test_op->Inputs().size(), 3UL + 2UL + 2UL);
F
fengjiayi 已提交
77 78 79 80 81 82 83
  EXPECT_EQ(grad_test_op->Input("In1"), "in1");
  EXPECT_EQ(grad_test_op->Inputs("In2_mult"),
            std::vector<std::string>({"in2_1", "in2_2", "in2_3"}));
  EXPECT_EQ(grad_test_op->Input("In3"), "in3");
  EXPECT_EQ(grad_test_op->Input("Out1"), "out1");
  EXPECT_EQ(grad_test_op->Inputs("Out2_mult"),
            std::vector<std::string>({"out2_1", "out2_2"}));
84 85 86
  EXPECT_EQ(grad_test_op->Input(f::GradVarName("Out1")),
            f::GradVarName("out1"));
  EXPECT_EQ(grad_test_op->Inputs(f::GradVarName("Out2_mult")),
Y
Yi Wang 已提交
87
            std::vector<std::string>(
88
                {f::GradVarName("out2_1"), f::GradVarName("out2_2")}));
F
fengjiayi 已提交
89

Q
qiaolongfei 已提交
90
  ASSERT_EQ(grad_test_op->Outputs().size(), 3UL);
91 92 93 94 95 96
  EXPECT_EQ(grad_test_op->Output(f::GradVarName("In1")), f::GradVarName("in1"));
  EXPECT_EQ(grad_test_op->Outputs(f::GradVarName("In2_mult")),
            std::vector<std::string>({f::GradVarName("in2_1"),
                                      f::GradVarName("in2_2"),
                                      f::GradVarName("in2_3")}));
  EXPECT_EQ(grad_test_op->Output(f::GradVarName("In3")), f::GradVarName("in3"));
F
fengjiayi 已提交
97 98 99 100
}

TEST(GradOpBuilder, IOIgnoredInGradient) {
  std::shared_ptr<f::OperatorBase> test_op(f::OpRegistry::CreateOp(
Y
Yu Yang 已提交
101 102 103
      "io_ignored", {{"In1", {"in1"}},
                     {"In2_mult", {"in2_1", "in2_2"}},
                     {"In3_mult", {"in3_1", "in3_2"}}},
104
      {{"Out1_mult", {"out1_1", "out1_2"}}, {"Out2", {"out2"}}}, {}));
F
fengjiayi 已提交
105 106 107 108
  std::shared_ptr<f::OperatorBase> grad_test_op =
      f::OpRegistry::CreateGradOp(*test_op);

  // 'In2' and 'Out2' are ignored in gradient calculating
Q
qiaolongfei 已提交
109
  ASSERT_EQ(grad_test_op->Inputs().size(), 2UL + 1UL + 2UL);
F
fengjiayi 已提交
110 111 112 113 114
  EXPECT_EQ(grad_test_op->Input("In1"), "in1");
  EXPECT_EQ(grad_test_op->Inputs("In3_mult"),
            std::vector<std::string>({"in3_1", "in3_2"}));
  EXPECT_EQ(grad_test_op->Inputs("Out1_mult"),
            std::vector<std::string>({"out1_1", "out1_2"}));
115
  EXPECT_EQ(grad_test_op->Inputs(f::GradVarName("Out1_mult")),
Y
Yi Wang 已提交
116
            std::vector<std::string>(
117 118 119
                {f::GradVarName("out1_1"), f::GradVarName("out1_2")}));
  EXPECT_EQ(grad_test_op->Input(f::GradVarName("Out2")),
            f::GradVarName("out2"));
F
fengjiayi 已提交
120

Q
qiaolongfei 已提交
121
  ASSERT_EQ(grad_test_op->Outputs().size(), 3UL);
122 123
  EXPECT_EQ(grad_test_op->Output(f::GradVarName("In1")), f::GradVarName("in1"));
  EXPECT_EQ(grad_test_op->Outputs(f::GradVarName("In2_mult")),
Y
Yi Wang 已提交
124
            std::vector<std::string>(
125 126
                {f::GradVarName("in2_1"), f::GradVarName("in2_2")}));
  EXPECT_EQ(grad_test_op->Outputs(f::GradVarName("In3_mult")),
Y
Yi Wang 已提交
127
            std::vector<std::string>(
128
                {f::GradVarName("in3_1"), f::GradVarName("in3_2")}));
F
fengjiayi 已提交
129
}
F
fengjiayi 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142

TEST(GradOpDescBuilder, MutiInOut) {
  f::OpDescBind *forw_op = new f::OpDescBind();
  forw_op->SetType("mult_io");
  forw_op->SetInput("In1", {"in1"});
  forw_op->SetInput("In2_mult", {"in2_1", "in2_2", "in2_3"});
  forw_op->SetInput("In3", {"in3"});
  forw_op->SetOutput("Out1", {"out1"});
  forw_op->SetOutput("Out2_mult", {"out2_1", "out2_2"});

  f::OpDescBind *grad_op = new f::OpDescBind();
  f::CompleteGradOpDesc(forw_op, grad_op);

F
fengjiayi 已提交
143
  EXPECT_EQ(grad_op->Type(), "mult_io_grad");
F
fengjiayi 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  ASSERT_EQ(grad_op->InputNames().size(), 3UL + 2UL + 2UL);
  EXPECT_EQ(grad_op->Input("In1"), std::vector<std::string>({"in1"}));
  EXPECT_EQ(grad_op->Input("In2_mult"),
            std::vector<std::string>({"in2_1", "in2_2", "in2_3"}));
  EXPECT_EQ(grad_op->Input("In3"), std::vector<std::string>({"in3"}));
  EXPECT_EQ(grad_op->Input("Out1"), std::vector<std::string>({"out1"}));
  EXPECT_EQ(grad_op->Input("Out2_mult"),
            std::vector<std::string>({"out2_1", "out2_2"}));
  EXPECT_EQ(grad_op->Input(f::GradVarName("Out1")),
            std::vector<std::string>({f::GradVarName("out1")}));
  EXPECT_EQ(grad_op->Input(f::GradVarName("Out2_mult")),
            std::vector<std::string>(
                {f::GradVarName("out2_1"), f::GradVarName("out2_2")}));

  ASSERT_EQ(grad_op->OutputNames().size(), 3UL);
  EXPECT_EQ(grad_op->Output(f::GradVarName("In1")),
            std::vector<std::string>({f::GradVarName("in1")}));
  EXPECT_EQ(grad_op->Output(f::GradVarName("In2_mult")),
            std::vector<std::string>({f::GradVarName("in2_1"),
                                      f::GradVarName("in2_2"),
                                      f::GradVarName("in2_3")}));
  EXPECT_EQ(grad_op->Output(f::GradVarName("In3")),
            std::vector<std::string>({f::GradVarName("in3")}));
F
fengjiayi 已提交
167 168
  delete forw_op;
  delete grad_op;
F
fengjiayi 已提交
169
}
F
fengjiayi 已提交
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

TEST(GradOpDescBuilder, IOIgnoredInGradient) {
  f::OpDescBind *forw_op = new f::OpDescBind();
  forw_op->SetType("io_ignored");
  forw_op->SetInput("In1", {"in1"});
  forw_op->SetInput("In2_mult", {"in2_1", "in2_2"});
  forw_op->SetInput("In3_mult", {"in3_1", "in3_2"});
  forw_op->SetOutput("Out1_mult", {"out1_1", "out1_2"});
  forw_op->SetOutput("Out2", {"out2"});

  f::OpDescBind *grad_op = new f::OpDescBind();
  f::CompleteGradOpDesc(forw_op, grad_op);

  EXPECT_EQ(grad_op->Type(), "io_ignored_grad");
  // 'In2' and 'Out2' are ignored in gradient calculating
  ASSERT_EQ(grad_op->InputNames().size(), 2UL + 1UL + 2UL);
  EXPECT_EQ(grad_op->Input("In1"), std::vector<std::string>({"in1"}));
  EXPECT_EQ(grad_op->Input("In3_mult"),
            std::vector<std::string>({"in3_1", "in3_2"}));
  EXPECT_EQ(grad_op->Input("Out1_mult"),
            std::vector<std::string>({"out1_1", "out1_2"}));
  EXPECT_EQ(grad_op->Input(f::GradVarName("Out1_mult")),
            std::vector<std::string>(
                {f::GradVarName("out1_1"), f::GradVarName("out1_2")}));
  EXPECT_EQ(grad_op->Input(f::GradVarName("Out2")),
            std::vector<std::string>({f::GradVarName("out2")}));

  ASSERT_EQ(grad_op->OutputNames().size(), 3UL);
  EXPECT_EQ(grad_op->Output(f::GradVarName("In1")),
            std::vector<std::string>({f::GradVarName("in1")}));
  EXPECT_EQ(grad_op->Output(f::GradVarName("In2_mult")),
            std::vector<std::string>(
                {f::GradVarName("in2_1"), f::GradVarName("in2_2")}));
  EXPECT_EQ(grad_op->Output(f::GradVarName("In3_mult")),
            std::vector<std::string>(
                {f::GradVarName("in3_1"), f::GradVarName("in3_2")}));
  delete forw_op;
  delete grad_op;
}