|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | +#include <algorithm> |
| 15 | +#include "paddle/framework/executor.h" |
| 16 | +#include "paddle/framework/op_registry.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +class ConditionalOp : public framework::OperatorBase { |
| 22 | + public: |
| 23 | + ConditionalOp(const std::string &type, |
| 24 | + const framework::VariableNameMap &inputs, |
| 25 | + const framework::VariableNameMap &outputs, |
| 26 | + const framework::AttributeMap &attrs) |
| 27 | + : OperatorBase(type, inputs, outputs, attrs) {} |
| 28 | + |
| 29 | + protected: |
| 30 | + std::vector<const framework::LoDTensor *> InputTensors( |
| 31 | + const framework::Scope &scope) const { |
| 32 | + std::vector<const framework::LoDTensor *> retv; |
| 33 | + auto xs = Inputs("X"); |
| 34 | + retv.resize(xs.size(), nullptr); |
| 35 | + std::transform( |
| 36 | + xs.begin(), xs.end(), retv.begin(), |
| 37 | + [&scope](const std::string &var_name) -> const framework::LoDTensor * { |
| 38 | + auto *var = scope.FindVar(var_name); |
| 39 | + PADDLE_ENFORCE(var != nullptr, "Cannot find variable %s", var_name); |
| 40 | + return &var->Get<framework::LoDTensor>(); |
| 41 | + }); |
| 42 | + return retv; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +class ConditionalBlockOp : public ConditionalOp { |
| 47 | + public: |
| 48 | + ConditionalBlockOp(const std::string &type, |
| 49 | + const framework::VariableNameMap &inputs, |
| 50 | + const framework::VariableNameMap &outputs, |
| 51 | + const framework::AttributeMap &attrs) |
| 52 | + : ConditionalOp(type, inputs, outputs, attrs) {} |
| 53 | + void Run(const framework::Scope &scope, |
| 54 | + const platform::DeviceContext &dev_ctx) const override { |
| 55 | + auto xs = InputTensors(scope); |
| 56 | + bool need_run = std::all_of( |
| 57 | + xs.begin(), xs.end(), |
| 58 | + [](const framework::LoDTensor *t) { return t->numel() != 0; }); |
| 59 | + |
| 60 | + if (need_run) { |
| 61 | + auto *scope_var = scope.FindVar(Output("Scope")); |
| 62 | + PADDLE_ENFORCE(scope_var != nullptr, "Must set scope"); |
| 63 | + auto *scopes = scope_var->GetMutable<std::vector<framework::Scope *>>(); |
| 64 | + scopes->resize(1); |
| 65 | + scopes->front() = &scope.NewScope(); |
| 66 | + auto &cur_scope = *scopes->front(); |
| 67 | + |
| 68 | + auto *block = Attr<framework::BlockDescBind *>("block"); |
| 69 | + framework::Executor exec(dev_ctx); |
| 70 | + exec.Run(*block->Program(), &cur_scope, block->ID(), false); |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +class ConditionalBlockOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 76 | + public: |
| 77 | + ConditionalBlockOpProtoMaker(framework::OpProto *proto, |
| 78 | + framework::OpAttrChecker *op_checker) |
| 79 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 80 | + AddInput("X", |
| 81 | + "The conditional variable of this operator. If X is empty, the " |
| 82 | + "whole sub-block will not be executed.") |
| 83 | + .AsDuplicable(); |
| 84 | + AddInput("Params", "The input variables of the sub-block.").AsDuplicable(); |
| 85 | + AddOutput("Out", "The output variables of the sub-block.").AsDuplicable(); |
| 86 | + AddOutput("Scope", |
| 87 | + "(std::vector<Scope*>) The step scope of conditional block. To " |
| 88 | + "unify the conditional block, rnn and while op, the type of " |
| 89 | + "scope is std::vector<Scope*>"); |
| 90 | + AddAttr<framework::BlockDescBind *>( |
| 91 | + "block", "The step block of conditional block operator"); |
| 92 | + AddComment(R"DOC(Conditional block operator |
| 93 | +
|
| 94 | +Run the sub-block if X is not empty. Params is the other inputs and Out is the |
| 95 | +outputs of the sub-block. |
| 96 | +)DOC"); |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +class ConditionalBlockGradOp : public ConditionalOp { |
| 101 | + public: |
| 102 | + ConditionalBlockGradOp(const std::string &type, |
| 103 | + const framework::VariableNameMap &inputs, |
| 104 | + const framework::VariableNameMap &outputs, |
| 105 | + const framework::AttributeMap &attrs) |
| 106 | + : ConditionalOp(type, inputs, outputs, attrs) {} |
| 107 | + void Run(const framework::Scope &scope, |
| 108 | + const platform::DeviceContext &dev_ctx) const override { |
| 109 | + auto xs = this->InputTensors(scope); |
| 110 | + bool need_run = std::all_of( |
| 111 | + xs.begin(), xs.end(), |
| 112 | + [](const framework::LoDTensor *t) { return t->numel() != 0; }); |
| 113 | + |
| 114 | + if (need_run) { |
| 115 | + auto *scope_var = scope.FindVar(Input("Scope")); |
| 116 | + PADDLE_ENFORCE(scope_var != nullptr, "Must set scope"); |
| 117 | + auto &scopes = scope_var->Get<std::vector<framework::Scope *>>(); |
| 118 | + framework::Scope &cur_scope = *scopes[0]; |
| 119 | + |
| 120 | + auto *block = Attr<framework::BlockDescBind *>("block"); |
| 121 | + framework::Executor exec(dev_ctx); |
| 122 | + exec.Run(*block->Program(), &cur_scope, block->ID(), false); |
| 123 | + |
| 124 | + AssignLocalGradientToGlobal(dev_ctx, cur_scope, Inputs("Params"), |
| 125 | + Outputs(framework::GradVarName("Params"))); |
| 126 | + |
| 127 | + AssignLocalGradientToGlobal(dev_ctx, cur_scope, Inputs("X"), |
| 128 | + Outputs(framework::GradVarName("X"))); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private: |
| 133 | + void AssignLocalGradientToGlobal( |
| 134 | + const platform::DeviceContext &dev_ctx, const framework::Scope &cur_scope, |
| 135 | + const std::vector<std::string> &p_names, |
| 136 | + const std::vector<std::string> &pg_names) const { |
| 137 | + for (size_t i = 0; i < p_names.size(); ++i) { |
| 138 | + auto out_grad_name = pg_names[i]; |
| 139 | + auto in_grad_name = framework::GradVarName(p_names[i]); |
| 140 | + auto *in_var = cur_scope.FindVar(in_grad_name); |
| 141 | + if (in_var == nullptr) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + auto new_in_grad_name = cur_scope.Rename(in_grad_name); |
| 145 | + auto assign = |
| 146 | + framework::OpRegistry::CreateOp("assign", {{"X", {new_in_grad_name}}}, |
| 147 | + {{"Out", {out_grad_name}}}, {}); |
| 148 | + assign->Run(cur_scope, dev_ctx); |
| 149 | + cur_scope.Rename(new_in_grad_name, in_grad_name); |
| 150 | + } |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +class ConditionalBlockGradInferShape : public framework::InferShapeBase { |
| 155 | + public: |
| 156 | + void operator()(framework::InferShapeContext *context) const override { |
| 157 | + PADDLE_ENFORCE(context->HasInputs("X")); |
| 158 | + if (context->HasInputs("Params")) { |
| 159 | + PADDLE_ENFORCE(context->HasOutputs(framework::GradVarName("Params"))); |
| 160 | + context->SetOutputsDim(framework::GradVarName("Params"), |
| 161 | + context->GetInputsDim("Params")); |
| 162 | + } |
| 163 | + PADDLE_ENFORCE(context->HasOutputs(framework::GradVarName("X"))); |
| 164 | + context->SetOutputsDim(framework::GradVarName("X"), |
| 165 | + context->GetInputsDim("X")); |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +class ConditionalBlockGradMaker : public framework::SingleGradOpDescMaker { |
| 170 | + public: |
| 171 | + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; |
| 172 | + |
| 173 | + protected: |
| 174 | + std::unique_ptr<framework::OpDescBind> Apply() const override { |
| 175 | + auto grad_op = new framework::OpDescBind(); |
| 176 | + grad_op->SetType("conditional_block_grad"); |
| 177 | + grad_op->SetInput("X", Input("X")); |
| 178 | + grad_op->SetInput("Params", Input("Params")); |
| 179 | + grad_op->SetInput("Out", Output("Out")); |
| 180 | + grad_op->SetInput(framework::GradVarName("Out"), OutputGrad("Out")); |
| 181 | + grad_op->SetInput("Scope", Output("Scope")); |
| 182 | + grad_op->SetOutput(framework::GradVarName("X"), InputGrad("X")); |
| 183 | + grad_op->SetOutput(framework::GradVarName("Params"), InputGrad("Params")); |
| 184 | + grad_op->SetBlockAttr("block", *this->grad_block_[0]); |
| 185 | + return std::unique_ptr<framework::OpDescBind>(grad_op); |
| 186 | + } |
| 187 | +}; |
| 188 | + |
| 189 | +} // namespace operators |
| 190 | +} // namespace paddle |
| 191 | + |
| 192 | +namespace ops = paddle::operators; |
| 193 | +REGISTER_OPERATOR(conditional_block, ops::ConditionalBlockOp, |
| 194 | + ops::ConditionalBlockOpProtoMaker, |
| 195 | + ops::ConditionalBlockGradMaker); |
| 196 | +REGISTER_OPERATOR(conditional_block_grad, ops::ConditionalBlockGradOp, |
| 197 | + ops::ConditionalBlockGradInferShape); |
0 commit comments