Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove intermediate output's gradient from inputs of grad_op. #10029

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions paddle/fluid/operators/gru_unit_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ output: h_t = dot((1 - u_t), h_{t-1}) + dot(u_t, {h}_t)

which is same as one time step of GRU Operator.

@note To implement the complete GRU unit, fully-connected operator must be
@note To implement the complete GRU unit, fully-connected operator must be
used before to feed xu, xr and xc as the Input of GRUUnit operator.

)DOC");
Expand Down Expand Up @@ -194,13 +194,45 @@ class GRUUnitGradOp : public framework::OperatorWithKernel {
}
};

class GRUUnitGradOpMaker : public framework::SingleGradOpDescMaker {
public:
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

protected:
std::unique_ptr<framework::OpDesc> Apply() const override {
auto* op = new framework::OpDesc();
op->SetType("gru_unit_grad");

op->SetInput("Input", Input("Input"));
op->SetInput("HiddenPrev", Input("HiddenPrev"));
op->SetInput("Weight", Input("Weight"));
op->SetInput("Bias", Input("Bias"));

op->SetInput("Hidden", Output("Hidden"));
op->SetInput("Gate", Output("Gate"));
op->SetInput("ResetHiddenPrev", Output("ResetHiddenPrev"));
op->SetInput(framework::GradVarName("Hidden"), OutputGrad("Hidden"));

op->SetAttrMap(Attrs());

op->SetOutput(framework::GradVarName("Input"), InputGrad("Input"));
op->SetOutput(framework::GradVarName("HiddenPrev"),
InputGrad("HiddenPrev"));
op->SetOutput(framework::GradVarName("Weight"), InputGrad("Weight"));
op->SetOutput(framework::GradVarName("Bias"), InputGrad("Bias"));
return std::unique_ptr<framework::OpDesc>(op);
}
};

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;

REGISTER_OPERATOR(gru_unit, ops::GRUUnitOp, ops::GRUUnitOpMaker,
paddle::framework::DefaultGradOpDescMaker<true>);
ops::GRUUnitGradOpMaker);
REGISTER_OPERATOR(gru_unit_grad, ops::GRUUnitGradOp);

REGISTER_OP_CPU_KERNEL(
gru_unit, ops::GRUUnitKernel<paddle::platform::CPUDeviceContext, float>,
ops::GRUUnitKernel<paddle::platform::CPUDeviceContext, double>);
Expand Down