conditional_block_op.cc 8.5 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. */
14 15

#include "paddle/fluid/operators/controlflow/conditional_block_op.h"
16
#include "paddle/fluid/operators/assign_op.h"
Y
Yu Yang 已提交
17 18 19 20

namespace paddle {
namespace operators {

Z
Zeng Jinle 已提交
21 22 23 24 25 26
const char ConditionalOp::kInputs[] = "Input";
const char ConditionalOp::kOutputs[] = "Out";
const char ConditionalOp::kCondition[] = "Cond";
const char ConditionalOp::kScope[] = "Scope";
const char ConditionalOp::kSkipEagerDeletionVars[] = "skip_eager_deletion_vars";

Y
Yu Yang 已提交
27 28 29 30 31 32 33
class ConditionalBlockOp : public ConditionalOp {
 public:
  ConditionalBlockOp(const std::string &type,
                     const framework::VariableNameMap &inputs,
                     const framework::VariableNameMap &outputs,
                     const framework::AttributeMap &attrs)
      : ConditionalOp(type, inputs, outputs, attrs) {}
34 35 36 37

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &dev_place) const override {
38 39
    bool need_run;
    if (Attr<bool>("is_scalar_condition")) {
40 41 42
      // When is_scalar_condition is True, the conditional variable is a scalar,
      // whether need to execute the operators in sub-block depends on the
      // conditional variable (Cond).
Z
Zeng Jinle 已提交
43
      auto xs = InputTensors(scope, ConditionalOp::kCondition);
44 45
      need_run = ScalarCondition(xs);
    } else {
46 47 48
      // When is_scalar_condition is False, the conditional variable maybe a
      // vector or tensor, whether need to execute the operators in sub-block
      // depends on the input variables (Input).
Z
Zeng Jinle 已提交
49
      auto xs = InputTensors(scope, ConditionalOp::kInputs);
50 51 52 53
      need_run = std::all_of(
          xs.begin(), xs.end(),
          [](const framework::LoDTensor *t) { return t->numel() != 0; });
    }
Y
Yu Yang 已提交
54 55

    if (need_run) {
Z
Zeng Jinle 已提交
56
      auto *scope_var = scope.FindVar(Output(ConditionalOp::kScope));
Y
Yu Yang 已提交
57 58 59 60 61
      PADDLE_ENFORCE(scope_var != nullptr, "Must set scope");
      auto *scopes = scope_var->GetMutable<std::vector<framework::Scope *>>();
      scopes->resize(1);
      scopes->front() = &scope.NewScope();
      auto &cur_scope = *scopes->front();
D
dzhwinter 已提交
62
      framework::Executor exec(dev_place);
Y
Yu Yang 已提交
63
      auto *block = Attr<framework::BlockDesc *>("sub_block");
64 65
      VLOG(3) << "Conditional block.idx = " << block->ID()
              << ", scope = " << &cur_scope;
Z
Zeng Jinle 已提交
66 67 68
      auto &skip_vars =
          Attr<std::vector<std::string>>(ConditionalOp::kSkipEagerDeletionVars);
      exec.Run(*block->Program(), &cur_scope, block->ID(), false, true,
69 70
               skip_vars, /* force_disable_gc */ false,
               /* keep_kid_scopes */ true);
Y
Yu Yang 已提交
71 72 73 74 75 76 77 78 79 80 81
    }
  }
};

class ConditionalBlockGradOp : public ConditionalOp {
 public:
  ConditionalBlockGradOp(const std::string &type,
                         const framework::VariableNameMap &inputs,
                         const framework::VariableNameMap &outputs,
                         const framework::AttributeMap &attrs)
      : ConditionalOp(type, inputs, outputs, attrs) {}
82 83 84 85

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &dev_place) const override {
86 87
    bool need_run;
    if (Attr<bool>("is_scalar_condition")) {
Z
Zeng Jinle 已提交
88
      auto xs = this->InputTensors(scope, ConditionalOp::kCondition);
89 90
      need_run = ScalarCondition(xs);
    } else {
Z
Zeng Jinle 已提交
91
      auto xs = this->InputTensors(scope, ConditionalOp::kInputs);
92 93 94 95
      need_run = std::all_of(
          xs.begin(), xs.end(),
          [](const framework::LoDTensor *t) { return t->numel() != 0; });
    }
Y
Yu Yang 已提交
96 97

    if (need_run) {
98 99 100 101 102 103 104 105 106 107
      const auto &inputs = Inputs(ConditionalOp::kInputs);
      const auto &outside_grads =
          Outputs(framework::GradVarName(ConditionalOp::kInputs));

      std::vector<std::string> inside_grads;
      inside_grads.reserve(inputs.size());
      for (auto &in : inputs) {
        inside_grads.emplace_back(framework::GradVarName(in));
      }

Z
Zeng Jinle 已提交
108
      auto *scope_var = scope.FindVar(Input(ConditionalOp::kScope));
109 110 111
      PADDLE_ENFORCE_NE(scope_var, nullptr,
                        platform::errors::InvalidArgument(
                            "Scope must be set in conditional block op"));
Y
Yu Yang 已提交
112
      auto &scopes = scope_var->Get<std::vector<framework::Scope *>>();
113 114 115
      PADDLE_ENFORCE_GT(scopes.size(), 0,
                        platform::errors::InvalidArgument(
                            "Scope must be set in conditional block op"));
Y
Yu Yang 已提交
116 117
      framework::Scope &cur_scope = *scopes[0];

D
dzhwinter 已提交
118
      framework::Executor exec(dev_place);
Y
Yu Yang 已提交
119
      auto *block = Attr<framework::BlockDesc *>("sub_block");
Y
Yu Yang 已提交
120

121 122
      VLOG(3) << "Conditional Grad block.idx = " << block->ID()
              << ", scope = " << &cur_scope;
123
      exec.Run(*block->Program(), &cur_scope, block->ID(), false, true,
124 125
               inside_grads, /* force_disable_gc */ false,
               /* keep_kid_scopes */ false);
Y
Yu Yang 已提交
126

127 128
      AssignLocalGradientToParentScope(dev_place, cur_scope, scope,
                                       inside_grads, outside_grads);
Y
Yu Yang 已提交
129 130 131 132
    }
  }

 private:
133
  void AssignLocalGradientToParentScope(
D
dzhwinter 已提交
134
      const platform::Place &place, const framework::Scope &cur_scope,
135 136 137 138 139 140 141 142 143 144 145
      const framework::Scope &parent_scope,
      const std::vector<std::string> &inside_grads,
      const std::vector<std::string> &outside_grads) const {
    for (size_t i = 0; i < outside_grads.size(); ++i) {
      const std::string &outside_grad_name = outside_grads[i];
      const std::string &inside_grad_name = inside_grads[i];
      VLOG(4) << "inside_grad_name = " << inside_grad_name
              << ", outside_grad_name = " << outside_grad_name;
      framework::Variable *inside_var =
          cur_scope.FindLocalVar(inside_grad_name);
      if (inside_var == nullptr) {
Y
Yu Yang 已提交
146 147
        continue;
      }
148 149 150 151 152 153 154 155 156
      framework::Variable *outside_var =
          parent_scope.FindVar(outside_grad_name);
      if (outside_var == nullptr) {
        continue;
      }
      platform::DeviceContext *dev_ctx =
          platform::DeviceContextPool::Instance().Get(place);
      framework::VisitVarType(*inside_var,
                              AssignFunctor(outside_var, *dev_ctx));
Y
Yu Yang 已提交
157 158 159 160 161 162 163
    }
  }
};

class ConditionalBlockGradInferShape : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext *context) const override {
Z
Zeng Jinle 已提交
164
    PADDLE_ENFORCE(context->HasInputs(ConditionalOp::kCondition));
165 166
    if (context->HasInputs(ConditionalOp::kInputs) &&
        context->HasOutputs(framework::GradVarName(ConditionalOp::kInputs))) {
Z
Zeng Jinle 已提交
167 168
      context->SetOutputsDim(framework::GradVarName(ConditionalOp::kInputs),
                             context->GetInputsDim(ConditionalOp::kInputs));
Y
Yu Yang 已提交
169 170 171 172
    }
  }
};

H
hong 已提交
173 174
template <typename T>
class ConditionalBlockGradMaker : public framework::SingleGradOpMaker<T> {
Y
Yu Yang 已提交
175
 public:
H
hong 已提交
176
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
Y
Yu Yang 已提交
177 178

 protected:
H
hong 已提交
179 180
  std::unique_ptr<T> Apply() const override {
    auto grad_op = new T();
Y
Yu Yang 已提交
181
    grad_op->SetType("conditional_block_grad");
Z
Zeng Jinle 已提交
182
    grad_op->SetInput(ConditionalOp::kCondition,
H
hong 已提交
183 184 185 186 187
                      this->Input(ConditionalOp::kCondition));
    grad_op->SetInput(ConditionalOp::kInputs,
                      this->Input(ConditionalOp::kInputs));
    grad_op->SetInput(ConditionalOp::kOutputs,
                      this->Output(ConditionalOp::kOutputs));
Z
Zeng Jinle 已提交
188
    grad_op->SetInput(framework::GradVarName(ConditionalOp::kOutputs),
H
hong 已提交
189 190 191
                      this->OutputGrad(ConditionalOp::kOutputs));
    grad_op->SetInput(ConditionalOp::kScope,
                      this->Output(ConditionalOp::kScope));
Z
Zeng Jinle 已提交
192
    grad_op->SetOutput(framework::GradVarName(ConditionalOp::kInputs),
H
hong 已提交
193
                       this->InputGrad(ConditionalOp::kInputs, false));
A
Abhinav Arora 已提交
194
    grad_op->SetBlockAttr("sub_block", this->grad_block_[0]);
H
hong 已提交
195 196 197
    grad_op->SetAttr("is_scalar_condition",
                     this->GetAttr("is_scalar_condition"));
    return std::unique_ptr<T>(grad_op);
Y
Yu Yang 已提交
198 199 200 201 202 203 204 205 206
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(conditional_block, ops::ConditionalBlockOp,
                  ops::ConditionalBlockOpProtoMaker,
H
hong 已提交
207
                  ops::ConditionalBlockGradMaker<paddle::framework::OpDesc>);
Y
Yu Yang 已提交
208 209
REGISTER_OPERATOR(conditional_block_grad, ops::ConditionalBlockGradOp,
                  ops::ConditionalBlockGradInferShape);