Skip to content

Commit d0ef682

Browse files
authored
Merge pull request #16274 from sneaxiy/fix_grad_maker
Remove unused variables in op grad maker
2 parents 804afc5 + 023a3a3 commit d0ef682

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

paddle/fluid/framework/scope.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ DEFINE_double(
3434
"Memory size threshold (GB) when the garbage collector clear tensors."
3535
"Disabled when this value is less than 0");
3636

37-
DEFINE_bool(fast_eager_deletion_mode, false,
37+
DEFINE_bool(fast_eager_deletion_mode, true,
3838
"Fast eager deletion mode. If enabled, memory would release "
3939
"immediately without waiting GPU kernel ends.");
4040

paddle/fluid/operators/conv_transpose_op.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/operators/conv_transpose_op.h"
16+
#include <memory>
1617
#include <string>
1718
#include <vector>
1819

@@ -344,6 +345,28 @@ framework::OpKernelType ConvTransposeOpGrad::GetExpectedKernelType(
344345
ctx.GetPlace(), layout_, library_);
345346
}
346347

348+
class ConvTransposeGradOpDescMaker : public framework::SingleGradOpDescMaker {
349+
public:
350+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
351+
352+
protected:
353+
std::unique_ptr<framework::OpDesc> Apply() const override {
354+
std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
355+
op->SetType(ForwardOp().Type() + "_grad");
356+
op->SetInput("Input", Input("Input"));
357+
op->SetInput("Filter", Input("Filter"));
358+
op->SetOutput(framework::GradVarName("Input"), InputGrad("Input"));
359+
op->SetOutput(framework::GradVarName("Filter"), InputGrad("Filter"));
360+
if (ForwardOp().Inputs().count("Bias") > 0) {
361+
op->SetInput("Bias", Input("Bias"));
362+
op->SetOutput(framework::GradVarName("Bias"), InputGrad("Bias"));
363+
}
364+
op->SetInput(framework::GradVarName("Output"), OutputGrad("Output"));
365+
op->SetAttrMap(Attrs());
366+
return op;
367+
}
368+
};
369+
347370
} // namespace operators
348371
} // namespace paddle
349372

@@ -352,7 +375,7 @@ namespace ops = paddle::operators;
352375
// conv2d_transpose
353376
REGISTER_OPERATOR(conv2d_transpose, ops::ConvTransposeOp,
354377
ops::Conv2DTransposeOpMaker,
355-
paddle::framework::DefaultGradOpDescMaker<true>);
378+
ops::ConvTransposeGradOpDescMaker);
356379
REGISTER_OPERATOR(conv2d_transpose_grad, ops::ConvTransposeOpGrad);
357380

358381
REGISTER_OP_CPU_KERNEL(
@@ -368,7 +391,7 @@ REGISTER_OP_CPU_KERNEL(
368391
// conv3d_transpose
369392
REGISTER_OPERATOR(conv3d_transpose, ops::ConvTransposeOp,
370393
ops::Conv3DTransposeOpMaker,
371-
paddle::framework::DefaultGradOpDescMaker<true>);
394+
ops::ConvTransposeGradOpDescMaker);
372395
REGISTER_OPERATOR(conv3d_transpose_grad, ops::ConvTransposeOpGrad);
373396

374397
REGISTER_OP_CPU_KERNEL(
@@ -384,7 +407,7 @@ REGISTER_OP_CPU_KERNEL(
384407
// depthwise conv2d_transpose
385408
REGISTER_OPERATOR(depthwise_conv2d_transpose, ops::ConvTransposeOp,
386409
ops::Conv2DTransposeOpMaker,
387-
paddle::framework::DefaultGradOpDescMaker<true>);
410+
ops::ConvTransposeGradOpDescMaker);
388411
REGISTER_OPERATOR(depthwise_conv2d_transpose_grad, ops::ConvTransposeOpGrad);
389412

390413
REGISTER_OP_CPU_KERNEL(

paddle/fluid/operators/dropout_op.cc

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/operators/dropout_op.h"
16+
#include <memory>
1617
#include <string>
1718

1819
namespace paddle {
@@ -106,21 +107,31 @@ class DropoutOpGrad : public framework::OperatorWithKernel {
106107
PADDLE_ENFORCE_EQ(ctx->Attrs().Get<bool>("is_test"), false,
107108
"GradOp is only callable when is_test is false");
108109

109-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) must not be null.");
110110
PADDLE_ENFORCE(ctx->HasInput("Mask"), "Mask must not be null.");
111111
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
112112
"Input(Out@GRAD) must not be null.");
113113

114-
auto x_dims = ctx->GetInputDim("X");
115114
auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
116-
PADDLE_ENFORCE_EQ(x_dims, out_dims,
117-
"Dimensions of Input(X) and Out@Grad must be the same.");
118-
auto mask_dims = ctx->GetInputDim("Mask");
119-
PADDLE_ENFORCE_EQ(x_dims, mask_dims,
120-
"Dimensions of Input(X) and Mask must be the same.");
121-
122-
ctx->SetOutputDim(framework::GradVarName("X"), x_dims);
123-
ctx->ShareLoD("X", /*->*/ framework::GradVarName("X"));
115+
116+
ctx->SetOutputDim(framework::GradVarName("X"), out_dims);
117+
ctx->ShareLoD(framework::GradVarName("Out"),
118+
/*->*/ framework::GradVarName("X"));
119+
}
120+
};
121+
122+
class DropoutGradOpDescMaker : public framework::SingleGradOpDescMaker {
123+
public:
124+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
125+
126+
protected:
127+
std::unique_ptr<framework::OpDesc> Apply() const override {
128+
std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
129+
op->SetType("dropout_grad");
130+
op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
131+
op->SetInput("Mask", Output("Mask"));
132+
op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
133+
op->SetAttrMap(Attrs());
134+
return op;
124135
}
125136
};
126137

@@ -129,7 +140,7 @@ class DropoutOpGrad : public framework::OperatorWithKernel {
129140

130141
namespace ops = paddle::operators;
131142
REGISTER_OPERATOR(dropout, ops::DropoutOp, ops::DropoutOpMaker,
132-
paddle::framework::DefaultGradOpDescMaker<true>);
143+
ops::DropoutGradOpDescMaker);
133144
REGISTER_OPERATOR(dropout_grad, ops::DropoutOpGrad);
134145
REGISTER_OP_CPU_KERNEL(
135146
dropout, ops::CPUDropoutKernel<paddle::platform::CPUDeviceContext, float>,

paddle/fluid/operators/layer_norm_op.cc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/operators/layer_norm_op.h"
16+
#include <memory>
1617

1718
namespace paddle {
1819
namespace operators {
@@ -133,7 +134,7 @@ class LayerNormGradOp : public framework::OperatorWithKernel {
133134
}
134135
if (ctx->HasOutput(framework::GradVarName("Bias"))) {
135136
ctx->SetOutputDim(framework::GradVarName("Bias"),
136-
ctx->GetInputDim("Bias"));
137+
ctx->GetInputDim("Scale"));
137138
}
138139
}
139140

@@ -157,12 +158,39 @@ class LayerNormGradOp : public framework::OperatorWithKernel {
157158
}
158159
};
159160

161+
class LayerNormGradOpDescMaker : public framework::SingleGradOpDescMaker {
162+
public:
163+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
164+
165+
protected:
166+
std::unique_ptr<framework::OpDesc> Apply() const override {
167+
std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
168+
op->SetType("layer_norm_grad");
169+
op->SetInput("X", Input("X"));
170+
op->SetInput("Mean", Output("Mean"));
171+
op->SetInput("Variance", Output("Variance"));
172+
if (ForwardOp().Inputs().count("Scale") > 0) {
173+
op->SetInput("Scale", Input("Scale"));
174+
op->SetOutput(framework::GradVarName("Scale"), InputGrad("Scale"));
175+
}
176+
177+
if (ForwardOp().Inputs().count("Bias") > 0) {
178+
op->SetOutput(framework::GradVarName("Bias"), InputGrad("Bias"));
179+
}
180+
181+
op->SetInput(framework::GradVarName("Y"), OutputGrad("Y"));
182+
op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
183+
op->SetAttrMap(Attrs());
184+
return op;
185+
}
186+
};
187+
160188
} // namespace operators
161189
} // namespace paddle
162190

163191
namespace ops = paddle::operators;
164192
REGISTER_OPERATOR(layer_norm, ops::LayerNormOp, ops::LayerNormOpMaker,
165-
paddle::framework::DefaultGradOpDescMaker<true>);
193+
ops::LayerNormGradOpDescMaker);
166194
REGISTER_OPERATOR(layer_norm_grad, ops::LayerNormGradOp);
167195
REGISTER_OP_CPU_KERNEL(
168196
layer_norm, ops::LayerNormKernel<paddle::platform::CPUDeviceContext, float>,

paddle/fluid/operators/layer_norm_op.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
245245
void Compute(const framework::ExecutionContext& ctx) const override {
246246
const float epsilon = ctx.Attr<float>("epsilon");
247247
auto x = *ctx.Input<Tensor>("X");
248-
auto* y = ctx.Input<Tensor>("Y");
249248
auto* mean = ctx.Input<Tensor>("Mean");
250249
auto* var = ctx.Input<Tensor>("Variance");
251250
auto* scale = ctx.Input<Tensor>("Scale");
252-
auto* bias = ctx.Input<Tensor>("Bias");
253251
auto d_y = *ctx.Input<Tensor>(framework::GradVarName("Y"));
254252
const auto begin_norm_axis = ctx.Attr<int>("begin_norm_axis");
255253

@@ -275,18 +273,13 @@ class LayerNormGradKernel : public framework::OpKernel<T> {
275273
x.Resize(matrix_shape);
276274
temp.mutable_data<T>(matrix_shape, ctx.GetPlace());
277275

278-
if (!(bias && scale)) {
279-
temp_norm.ShareDataWith(*y);
280-
temp_norm.Resize(matrix_shape);
281-
} else {
282-
temp_norm.mutable_data<T>(matrix_shape, ctx.GetPlace());
283-
// get x_norm
284-
ElementwiseComputeEx<SubFunctor<T>, DeviceContext, T>(
285-
ctx, &x, mean, /*axis*/ 0, SubFunctor<T>(), &temp_norm);
286-
ElementwiseComputeEx<DivAndSqrtFunctor<T>, DeviceContext, T>(
287-
ctx, &temp_norm, var, /*axis*/ 0,
288-
DivAndSqrtFunctor<T>(static_cast<T>(epsilon)), &temp_norm);
289-
}
276+
temp_norm.mutable_data<T>(matrix_shape, ctx.GetPlace());
277+
// get x_norm
278+
ElementwiseComputeEx<SubFunctor<T>, DeviceContext, T>(
279+
ctx, &x, mean, /*axis*/ 0, SubFunctor<T>(), &temp_norm);
280+
ElementwiseComputeEx<DivAndSqrtFunctor<T>, DeviceContext, T>(
281+
ctx, &temp_norm, var, /*axis*/ 0,
282+
DivAndSqrtFunctor<T>(static_cast<T>(epsilon)), &temp_norm);
290283
}
291284

292285
if (d_bias) {

paddle/fluid/operators/softmax_with_cross_entropy_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

1515
#include "paddle/fluid/operators/softmax_with_cross_entropy_op.h"
16+
#include <memory>
1617

1718
namespace paddle {
1819
namespace operators {
@@ -187,7 +188,6 @@ class SoftmaxGradMaker : public framework::SingleGradOpDescMaker {
187188
grad_op->SetType("softmax_with_cross_entropy_grad");
188189
grad_op->SetInput("Label", Input("Label"));
189190
grad_op->SetInput("Softmax", Output("Softmax"));
190-
grad_op->SetInput("Loss", Output("Loss"));
191191
grad_op->SetInput(framework::GradVarName("Softmax"), OutputGrad("Softmax"));
192192
grad_op->SetInput(framework::GradVarName("Loss"), OutputGrad("Loss"));
193193
grad_op->SetOutput(framework::GradVarName("Logits"), InputGrad("Logits"));

0 commit comments

Comments
 (0)