/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. 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 http://www.apache.org/licenses/LICENSE-2.0 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. */ #include "paddle/fluid/operators/controlflow/conditional_block_op.h" #include "paddle/fluid/operators/assign_op.h" namespace paddle { namespace operators { 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"; 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) {} private: void RunImpl(const framework::Scope &scope, const platform::Place &dev_place) const override { bool need_run; if (Attr("is_scalar_condition")) { // 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). auto xs = InputTensors(scope, ConditionalOp::kCondition); need_run = ScalarCondition(xs); } else { // 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). auto xs = InputTensors(scope, ConditionalOp::kInputs); need_run = std::all_of( xs.begin(), xs.end(), [](const framework::LoDTensor *t) { return t->numel() != 0; }); } if (need_run) { auto *scope_var = scope.FindVar(Output(ConditionalOp::kScope)); PADDLE_ENFORCE(scope_var != nullptr, "Must set scope"); auto *scopes = scope_var->GetMutable>(); scopes->resize(1); scopes->front() = &scope.NewScope(); auto &cur_scope = *scopes->front(); framework::Executor exec(dev_place); auto *block = Attr("sub_block"); VLOG(3) << "Conditional block.idx = " << block->ID() << ", scope = " << &cur_scope; auto &skip_vars = Attr>(ConditionalOp::kSkipEagerDeletionVars); exec.Run(*block->Program(), &cur_scope, block->ID(), false, true, skip_vars, /* force_disable_gc */ false, /* keep_kid_scopes */ true); } } }; 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) {} private: void RunImpl(const framework::Scope &scope, const platform::Place &dev_place) const override { bool need_run; if (Attr("is_scalar_condition")) { auto xs = this->InputTensors(scope, ConditionalOp::kCondition); need_run = ScalarCondition(xs); } else { auto xs = this->InputTensors(scope, ConditionalOp::kInputs); need_run = std::all_of( xs.begin(), xs.end(), [](const framework::LoDTensor *t) { return t->numel() != 0; }); } if (need_run) { const auto &inputs = Inputs(ConditionalOp::kInputs); const auto &outside_grads = Outputs(framework::GradVarName(ConditionalOp::kInputs)); std::vector inside_grads; inside_grads.reserve(inputs.size()); for (auto &in : inputs) { inside_grads.emplace_back(framework::GradVarName(in)); } auto *scope_var = scope.FindVar(Input(ConditionalOp::kScope)); PADDLE_ENFORCE_NE(scope_var, nullptr, platform::errors::InvalidArgument( "Scope must be set in conditional block op")); auto &scopes = scope_var->Get>(); PADDLE_ENFORCE_GT(scopes.size(), 0, platform::errors::InvalidArgument( "Scope must be set in conditional block op")); framework::Scope &cur_scope = *scopes[0]; framework::Executor exec(dev_place); auto *block = Attr("sub_block"); VLOG(3) << "Conditional Grad block.idx = " << block->ID() << ", scope = " << &cur_scope; exec.Run(*block->Program(), &cur_scope, block->ID(), false, true, inside_grads, /* force_disable_gc */ false, /* keep_kid_scopes */ false); AssignLocalGradientToParentScope(dev_place, cur_scope, scope, inside_grads, outside_grads); } } private: void AssignLocalGradientToParentScope( const platform::Place &place, const framework::Scope &cur_scope, const framework::Scope &parent_scope, const std::vector &inside_grads, const std::vector &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) { continue; } 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)); } } }; class ConditionalBlockGradInferShape : public framework::InferShapeBase { public: void operator()(framework::InferShapeContext *context) const override { PADDLE_ENFORCE(context->HasInputs(ConditionalOp::kCondition)); if (context->HasInputs(ConditionalOp::kInputs) && context->HasOutputs(framework::GradVarName(ConditionalOp::kInputs))) { context->SetOutputsDim(framework::GradVarName(ConditionalOp::kInputs), context->GetInputsDim(ConditionalOp::kInputs)); } } }; template class ConditionalBlockGradMaker : public framework::SingleGradOpMaker { public: using framework::SingleGradOpMaker::SingleGradOpMaker; protected: std::unique_ptr Apply() const override { auto grad_op = new T(); grad_op->SetType("conditional_block_grad"); grad_op->SetInput(ConditionalOp::kCondition, this->Input(ConditionalOp::kCondition)); grad_op->SetInput(ConditionalOp::kInputs, this->Input(ConditionalOp::kInputs)); grad_op->SetInput(ConditionalOp::kOutputs, this->Output(ConditionalOp::kOutputs)); grad_op->SetInput(framework::GradVarName(ConditionalOp::kOutputs), this->OutputGrad(ConditionalOp::kOutputs)); grad_op->SetInput(ConditionalOp::kScope, this->Output(ConditionalOp::kScope)); grad_op->SetOutput(framework::GradVarName(ConditionalOp::kInputs), this->InputGrad(ConditionalOp::kInputs, false)); grad_op->SetBlockAttr("sub_block", this->grad_block_[0]); grad_op->SetAttr("is_scalar_condition", this->GetAttr("is_scalar_condition")); return std::unique_ptr(grad_op); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OPERATOR(conditional_block, ops::ConditionalBlockOp, ops::ConditionalBlockOpProtoMaker, ops::ConditionalBlockGradMaker); REGISTER_OPERATOR(conditional_block_grad, ops::ConditionalBlockGradOp, ops::ConditionalBlockGradInferShape);