Skip to content

Commit abf9832

Browse files
committed
tiny change to save memory
1 parent f86198e commit abf9832

File tree

8 files changed

+103
-13
lines changed

8 files changed

+103
-13
lines changed

paddle/fluid/framework/grad_op_desc_maker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ class GradOpDescMakerBase {
129129

130130
std::string ForwardOpType() const { return this->fwd_op_.Type(); }
131131

132+
protected:
133+
const OpDesc& ForwardOp() const { return fwd_op_; }
134+
132135
private:
133136
const OpDesc& fwd_op_;
134137
const std::unordered_set<std::string>& no_grad_set_;

paddle/fluid/operators/elementwise_mul_op.cc

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

1515
#include "paddle/fluid/operators/elementwise_mul_op.h"
16+
#include <string>
1617
#include "paddle/fluid/operators/elementwise_op.h"
18+
19+
namespace paddle {
20+
namespace operators {
21+
22+
class ElementwiseMulOpGradDescMaker : public framework::SingleGradOpDescMaker {
23+
public:
24+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
25+
26+
protected:
27+
std::unique_ptr<framework::OpDesc> Apply() const override {
28+
std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
29+
op->SetType("elementwise_mul_grad");
30+
op->SetInput("X", Input("X"));
31+
op->SetInput("Y", Input("Y"));
32+
op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
33+
op->SetAttrMap(Attrs());
34+
op->SetOutput(::paddle::framework::GradVarName("X"), InputGrad("X"));
35+
op->SetOutput(::paddle::framework::GradVarName("Y"), InputGrad("Y"));
36+
return op;
37+
}
38+
};
39+
40+
class ElementwiseMulOpMaker : public ElementwiseOpMaker {
41+
protected:
42+
virtual std::string GetName() const { return "Mul"; }
43+
virtual std::string GetEquation() const { return "Out = X \\\\odot Y"; }
44+
};
45+
46+
} // namespace operators
47+
} // namespace paddle
48+
1749
namespace ops = paddle::operators;
18-
REGISTER_ELEMWISE_OP(elementwise_mul, "Mul", "Out = X \\\\odot Y");
50+
// REGISTER_ELEMWISE_OP(elementwise_mul, "Mul", "Out = X \\\\odot Y");
51+
REGISTER_OPERATOR(elementwise_mul, ops::ElementwiseOp,
52+
ops::ElementwiseMulOpMaker, ops::ElementwiseOpInferVarType,
53+
ops::ElementwiseMulOpGradDescMaker);
54+
REGISTER_OPERATOR(elementwise_mul_grad, ops::ElementwiseOpGrad);
55+
1956
REGISTER_OP_CPU_KERNEL(
2057
elementwise_mul,
2158
ops::ElementwiseMulKernel<paddle::platform::CPUDeviceContext, float>,

paddle/fluid/operators/elementwise_mul_op.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ class ElementwiseMulGradKernel : public framework::OpKernel<T> {
5757

5858
auto* x = ctx.Input<Tensor>("X");
5959
auto* y = ctx.Input<Tensor>("Y");
60-
auto* out = ctx.Input<Tensor>("Out");
60+
// auto* out = ctx.Input<Tensor>("Out");
6161
auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
62+
auto* out = dout; // out is not necessary
6263
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
6364
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
6465
int axis = ctx.Attr<int>("axis");

paddle/fluid/operators/matmul_op.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class MatMulKernel : public framework::OpKernel<T> {
5959
RowMatrixFromVector(x.dims()), 0, context.Attr<bool>("transpose_X"));
6060
auto mat_dim_b = math::CreateMatrixDescriptor(
6161
ColumnMatrixFromVector(y.dims()), 0, context.Attr<bool>("transpose_Y"));
62-
blas.MatMul(x, mat_dim_a, y, mat_dim_b, T(1), out, T(0));
62+
auto scale = static_cast<T>(context.Attr<float>("scale"));
63+
auto bias = static_cast<T>(context.Attr<float>("bias"));
64+
blas.MatMul(x, mat_dim_a, y, mat_dim_b, scale, out, bias);
6365
}
6466
};
6567

@@ -185,7 +187,8 @@ class MatMulGradKernel : public framework::OpKernel<T> {
185187
auto blas = math::GetBlas<DeviceContext, T>(context);
186188
auto mat_dim_a = math::CreateMatrixDescriptor(a.dims(), 0, trans_a);
187189
auto mat_dim_b = math::CreateMatrixDescriptor(b.dims(), 0, trans_b);
188-
blas.MatMul(a, mat_dim_a, b, mat_dim_b, T(1), out, T(0));
190+
blas.MatMul(a, mat_dim_a, b, mat_dim_b,
191+
static_cast<T>(context.Attr<float>("scale")), out, T(0));
189192
}
190193

191194
void CalcInputGrad(const framework::ExecutionContext &context,
@@ -334,6 +337,8 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker {
334337
R"DOC(If true, use the transpose of `Y`.
335338
)DOC")
336339
.SetDefault(false);
340+
AddAttr<float>("scale", "Scale").SetDefault(1.0f);
341+
AddAttr<float>("bias", "Bias").SetDefault(0.0f);
337342
AddComment(R"DOC(
338343
MatMul Operator.
339344

paddle/fluid/operators/mul_op.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,29 @@ class MulGradOp : public framework::OperatorWithKernel {
156156
}
157157
};
158158

159+
class MulOpGradMaker : public framework::SingleGradOpDescMaker {
160+
public:
161+
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
162+
163+
protected:
164+
std::unique_ptr<framework::OpDesc> Apply() const override {
165+
std::unique_ptr<framework::OpDesc> retv(new framework::OpDesc());
166+
retv->SetType("mul_grad");
167+
retv->SetInput("X", Input("X"));
168+
retv->SetInput("Y", Input("Y"));
169+
retv->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
170+
retv->SetOutput(framework::GradVarName("X"), InputGrad("X"));
171+
retv->SetOutput(framework::GradVarName("Y"), InputGrad("Y"));
172+
retv->SetAttrMap(Attrs());
173+
return retv;
174+
}
175+
};
176+
159177
} // namespace operators
160178
} // namespace paddle
161179

162180
namespace ops = paddle::operators;
163-
REGISTER_OPERATOR(mul, ops::MulOp, ops::MulOpMaker,
164-
paddle::framework::DefaultGradOpDescMaker<true>);
181+
REGISTER_OPERATOR(mul, ops::MulOp, ops::MulOpMaker, ops::MulOpGradMaker);
165182
REGISTER_OPERATOR(mul_grad, ops::MulGradOp);
166183
REGISTER_OP_CPU_KERNEL(
167184
mul, ops::MulKernel<paddle::platform::CPUDeviceContext, float>,

paddle/fluid/operators/scale_op.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Multiply the input tensor with a float scalar to scale the input tensor.
4949
)DOC");
5050
AddAttr<float>("scale", "The scaling factor of the scale operator.")
5151
.SetDefault(1.0);
52+
AddAttr<float>("bias", "The bias of the scale operator.").SetDefault(0.0);
5253
}
5354
};
5455

@@ -62,6 +63,7 @@ class ScaleGradMaker : public framework::SingleGradOpDescMaker {
6263
grad_op->SetInput("X", OutputGrad("Out"));
6364
grad_op->SetOutput("Out", InputGrad("X"));
6465
grad_op->SetAttr("scale", GetAttr("scale"));
66+
grad_op->SetAttr("bias", 0.0f);
6567
return std::unique_ptr<framework::OpDesc>(grad_op);
6668
}
6769
};

paddle/fluid/operators/scale_op.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,24 @@ class ScaleKernel : public framework::OpKernel<T> {
2929

3030
auto scale = static_cast<T>(context.Attr<float>("scale"));
3131

32-
auto eigen_out = framework::EigenVector<T>::Flatten(*tensor);
32+
PADDLE_ENFORCE_EQ(in->dims(), out->dims(),
33+
"in and out should have the same dim");
34+
35+
auto scale = static_cast<T>(ctx.Attr<float>("scale"));
36+
auto bias = static_cast<T>(ctx.Attr<float>("bias"));
37+
38+
if (in_var->IsType<framework::SelectedRows>() && in_var != out_var) {
39+
auto& in_slr = in_var->Get<framework::SelectedRows>();
40+
auto* out_slr = out_var->GetMutable<framework::SelectedRows>();
41+
out_slr->set_rows(in_slr.rows());
42+
out_slr->set_height(in_slr.height());
43+
}
44+
45+
auto eigen_out = framework::EigenVector<T>::Flatten(*out);
3346
auto eigen_in = framework::EigenVector<T>::Flatten(*in);
34-
auto& dev =
35-
*context.template device_context<DeviceContext>().eigen_device();
36-
eigen_out.device(dev) = scale * eigen_in;
47+
auto& dev = *ctx.template device_context<DeviceContext>().eigen_device();
48+
eigen_out.device(dev) =
49+
static_cast<T>(scale) * eigen_in + static_cast<T>(bias);
3750
}
3851
};
3952

python/paddle/fluid/layers/nn.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,7 +3314,13 @@ def l2_normalize(x, axis, epsilon=1e-12, name=None):
33143314
return out
33153315

33163316

3317-
def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
3317+
def matmul(x,
3318+
y,
3319+
transpose_x=False,
3320+
transpose_y=False,
3321+
scale=1.0,
3322+
bias=0.0,
3323+
name=None):
33183324
"""
33193325
Applies matrix multiplication to two tensors.
33203326
@@ -3348,6 +3354,8 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
33483354
y (Variable): The input variable which is a Tensor or LoDTensor.
33493355
transpose_x (bool): Whether to transpose :math:`x` before multiplication.
33503356
transpose_y (bool): Whether to transpose :math:`y` before multiplication.
3357+
scale (float): The scale of output. Default 1.0.
3358+
bias (float): The bias added to output. Default 0.0.
33513359
name(str|None): A name for this layer(optional). If set None, the layer
33523360
will be named automatically.
33533361
@@ -3415,8 +3423,12 @@ def __check_input(x, y):
34153423
inputs={'X': x,
34163424
'Y': y},
34173425
outputs={'Out': out},
3418-
attrs={'transpose_X': transpose_x,
3419-
'transpose_Y': transpose_y})
3426+
attrs={
3427+
'transpose_X': transpose_x,
3428+
'transpose_Y': transpose_y,
3429+
'scale': scale,
3430+
'bias': bias
3431+
})
34203432
return out
34213433

34223434

0 commit comments

Comments
 (0)