|
|
|
@ -27,6 +27,7 @@ limitations under the License. */
|
|
|
|
|
#include "paddle/fluid/framework/eigen.h"
|
|
|
|
|
#include "paddle/fluid/framework/op_registry.h"
|
|
|
|
|
#include "paddle/fluid/operators/math/blas.h"
|
|
|
|
|
#include "paddle/fluid/platform/enforce.h"
|
|
|
|
|
#include "paddle/fluid/platform/float16.h"
|
|
|
|
|
|
|
|
|
|
#ifdef PADDLE_WITH_MKLDNN
|
|
|
|
@ -53,12 +54,14 @@ inline void ExtractActivationTensor(const framework::ExecutionContext& context,
|
|
|
|
|
framework::Tensor** Out) {
|
|
|
|
|
auto x_var = context.InputVar("X");
|
|
|
|
|
auto out_var = context.OutputVar("Out");
|
|
|
|
|
PADDLE_ENFORCE(x_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(x_var,
|
|
|
|
|
platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable X, variable name = %s",
|
|
|
|
|
context.InputName("X"));
|
|
|
|
|
PADDLE_ENFORCE(out_var != nullptr,
|
|
|
|
|
context.InputName("X")));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
out_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get output Variable Out, variable name = %s",
|
|
|
|
|
context.OutputName("Out"));
|
|
|
|
|
context.OutputName("Out")));
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(context.Type())) {
|
|
|
|
|
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
|
|
|
|
|
*Out = paddle::framework::GetMutableLoDTensorOrSelectedRowsValueFromVar(
|
|
|
|
@ -68,9 +71,10 @@ inline void ExtractActivationTensor(const framework::ExecutionContext& context,
|
|
|
|
|
*Out = context.Output<framework::Tensor>("Out");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(*Out != nullptr,
|
|
|
|
|
"Cannot get output tensor Out, variable name = %s",
|
|
|
|
|
context.OutputName("Out"));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(*Out, platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the Variable "
|
|
|
|
|
"Output(Out), variable name = %s",
|
|
|
|
|
context.OutputName("Out")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <ActBwdOpFwdDeps kDepValue>
|
|
|
|
@ -84,18 +88,22 @@ inline void ExtractActivationGradTensor(
|
|
|
|
|
|
|
|
|
|
if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
|
|
|
|
|
out_var = context.InputVar("Out");
|
|
|
|
|
PADDLE_ENFORCE(out_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
out_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
context.InputName("Out"));
|
|
|
|
|
context.InputName("Out")));
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE(out_grad_var != nullptr,
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
out_grad_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable %s, variable name = %s",
|
|
|
|
|
framework::GradVarName("Out"),
|
|
|
|
|
context.InputName(framework::GradVarName("Out")));
|
|
|
|
|
PADDLE_ENFORCE(x_grad_var != nullptr,
|
|
|
|
|
context.InputName(framework::GradVarName("Out"))));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
x_grad_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get output Variable %s, variable name = %s",
|
|
|
|
|
framework::GradVarName("X"),
|
|
|
|
|
context.OutputName(framework::GradVarName("X")));
|
|
|
|
|
context.OutputName(framework::GradVarName("X"))));
|
|
|
|
|
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(context.Type())) {
|
|
|
|
|
*dOut = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(
|
|
|
|
@ -122,16 +130,18 @@ inline void ExtractActivationGradTensor(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PADDLE_ENFORCE(*dX != nullptr,
|
|
|
|
|
"Cannot get output tensor %s, variable name = %s",
|
|
|
|
|
framework::GradVarName("X"),
|
|
|
|
|
context.OutputName(framework::GradVarName("X")));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(*dX,
|
|
|
|
|
platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the Variable "
|
|
|
|
|
"Output(Out), variable name = %s",
|
|
|
|
|
context.OutputName(framework::GradVarName("X"))));
|
|
|
|
|
|
|
|
|
|
if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
|
|
|
|
|
auto x_var = context.InputVar("X");
|
|
|
|
|
PADDLE_ENFORCE(x_var != nullptr,
|
|
|
|
|
"Cannot get input tensor X, variable name = %s",
|
|
|
|
|
context.InputName("X"));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(x_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the "
|
|
|
|
|
"Variable Input(X), variable name = %s",
|
|
|
|
|
context.InputName("X")));
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(context.Type())) {
|
|
|
|
|
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
|
|
|
|
|
} else {
|
|
|
|
@ -1186,9 +1196,10 @@ inline void ExtractActivationDoubleGradTensor(
|
|
|
|
|
framework::Tensor** ddOut) {
|
|
|
|
|
auto ddx_var = ctx.InputVar("DDX");
|
|
|
|
|
auto ddo_var = ctx.OutputVar("DDOut");
|
|
|
|
|
PADDLE_ENFORCE(ddx_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
ddx_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("DDX"));
|
|
|
|
|
ctx.InputName("DDX")));
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
|
|
|
|
|
*ddX = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*ddx_var);
|
|
|
|
|
if (ddo_var) {
|
|
|
|
@ -1201,15 +1212,18 @@ inline void ExtractActivationDoubleGradTensor(
|
|
|
|
|
*ddOut = ctx.Output<framework::Tensor>("DDOut");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE(*ddX != nullptr,
|
|
|
|
|
"Cannot get output tensor DDX, variable name = %s",
|
|
|
|
|
ctx.OutputName("DDX"));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
*ddX,
|
|
|
|
|
platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the Variable Output, variable name = %s",
|
|
|
|
|
ctx.OutputName("DDX")));
|
|
|
|
|
|
|
|
|
|
if (static_cast<int>(kDepValue) & static_cast<int>(kDepX)) {
|
|
|
|
|
auto x_var = ctx.InputVar("X");
|
|
|
|
|
PADDLE_ENFORCE(x_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
x_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("X"));
|
|
|
|
|
ctx.InputName("X")));
|
|
|
|
|
auto dx_var = ctx.OutputVar("DX");
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
|
|
|
|
|
*X = paddle::framework::GetLoDTensorOrSelectedRowsValueFromVar(*x_var);
|
|
|
|
@ -1229,9 +1243,11 @@ inline void ExtractActivationDoubleGradTensor(
|
|
|
|
|
}
|
|
|
|
|
if (static_cast<int>(kDepValue) & static_cast<int>(kDepOut)) {
|
|
|
|
|
auto out_var = ctx.InputVar("Out");
|
|
|
|
|
PADDLE_ENFORCE(out_var != nullptr,
|
|
|
|
|
"Cannot get input tensor Out, variable name = %s",
|
|
|
|
|
ctx.InputName("Out"));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
out_var,
|
|
|
|
|
platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("Out")));
|
|
|
|
|
auto dout_var = ctx.OutputVar("DOut");
|
|
|
|
|
if (CanBeUsedBySelectedRows.count(ctx.Type())) {
|
|
|
|
|
*Out =
|
|
|
|
@ -1438,22 +1454,26 @@ inline void ExtractDoubleGradTensorWithInputDOut(
|
|
|
|
|
// extract ddX(output), ddOut(input)
|
|
|
|
|
auto ddx_var = ctx.InputVar("DDX");
|
|
|
|
|
auto ddo_var = ctx.OutputVar("DDOut");
|
|
|
|
|
PADDLE_ENFORCE(ddx_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
ddx_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("DDX"));
|
|
|
|
|
ctx.InputName("DDX")));
|
|
|
|
|
*ddX = ctx.Input<framework::Tensor>("DDX");
|
|
|
|
|
if (ddo_var) {
|
|
|
|
|
*ddOut = ctx.Output<framework::Tensor>("DDOut");
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE(*ddX != nullptr,
|
|
|
|
|
"Cannot get output tensor DDX, variable name = %s",
|
|
|
|
|
ctx.OutputName("DDX"));
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
ddX,
|
|
|
|
|
platform::errors::NotFound(
|
|
|
|
|
"Cannot get the tensor from the Variable DDX, variable name = %s",
|
|
|
|
|
ctx.OutputName("DDX")));
|
|
|
|
|
|
|
|
|
|
// extract x(input), dx(output)
|
|
|
|
|
auto x_var = ctx.InputVar("X");
|
|
|
|
|
PADDLE_ENFORCE(x_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
x_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("X"));
|
|
|
|
|
ctx.InputName("X")));
|
|
|
|
|
auto dx_var = ctx.OutputVar("DX");
|
|
|
|
|
*X = ctx.Input<framework::Tensor>("X");
|
|
|
|
|
if (dx_var) {
|
|
|
|
@ -1531,22 +1551,25 @@ class SqrtDoubleGradKernel
|
|
|
|
|
// extract ddx(input), ddout(output)
|
|
|
|
|
auto ddx_var = ctx.InputVar("DDX");
|
|
|
|
|
auto ddo_var = ctx.OutputVar("DDOut");
|
|
|
|
|
PADDLE_ENFORCE(ddx_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
ddx_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable DDX, variable name = %s",
|
|
|
|
|
ctx.InputName("DDX"));
|
|
|
|
|
ctx.InputName("DDX")));
|
|
|
|
|
ddX = ctx.Input<framework::Tensor>("DDX");
|
|
|
|
|
if (ddo_var) {
|
|
|
|
|
ddOut = ctx.Output<framework::Tensor>("DDOut");
|
|
|
|
|
}
|
|
|
|
|
PADDLE_ENFORCE(ddX != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
ddX, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable DDX, variable name = %s",
|
|
|
|
|
ctx.InputName("DDX"));
|
|
|
|
|
ctx.InputName("DDX")));
|
|
|
|
|
|
|
|
|
|
// extract out(input), dout(output)
|
|
|
|
|
auto out_var = ctx.InputVar("Out");
|
|
|
|
|
PADDLE_ENFORCE(out_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
out_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable Out, variable name = %s",
|
|
|
|
|
ctx.InputName("Out"));
|
|
|
|
|
ctx.InputName("Out")));
|
|
|
|
|
auto dout_var = ctx.OutputVar("DOut");
|
|
|
|
|
Out = ctx.Input<framework::Tensor>("Out");
|
|
|
|
|
if (dout_var) {
|
|
|
|
@ -1555,9 +1578,10 @@ class SqrtDoubleGradKernel
|
|
|
|
|
|
|
|
|
|
// extract dx(input)
|
|
|
|
|
auto dx_var = ctx.InputVar("DX");
|
|
|
|
|
PADDLE_ENFORCE(dx_var != nullptr,
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(
|
|
|
|
|
dx_var, platform::errors::NotFound(
|
|
|
|
|
"Cannot get input Variable DX, variable name = %s",
|
|
|
|
|
ctx.InputName("DX"));
|
|
|
|
|
ctx.InputName("DX")));
|
|
|
|
|
if (dx_var) {
|
|
|
|
|
dX = ctx.Input<framework::Tensor>("DX");
|
|
|
|
|
}
|
|
|
|
@ -1608,8 +1632,11 @@ class PowKernel : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
|
|
|
|
|
}
|
|
|
|
|
auto factor =
|
|
|
|
|
std::vector<float>(factor_data, factor_data + factor_tensor->numel());
|
|
|
|
|
PADDLE_ENFORCE_EQ(factor.size(), 1,
|
|
|
|
|
"The shape of factor(tensor) MUST BE [1].");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
factor.size(), 1,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"The shape of factor(tensor) must be [1] rather than %d",
|
|
|
|
|
factor.size()));
|
|
|
|
|
for (auto& attr : attrs) {
|
|
|
|
|
*attr.second = factor[0];
|
|
|
|
|
}
|
|
|
|
@ -1660,8 +1687,11 @@ class PowGradKernel
|
|
|
|
|
}
|
|
|
|
|
auto factor =
|
|
|
|
|
std::vector<float>(factor_data, factor_data + factor_tensor->numel());
|
|
|
|
|
PADDLE_ENFORCE_EQ(factor.size(), 1,
|
|
|
|
|
"The shape of factor(tensor) MUST BE [1].");
|
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
|
factor.size(), 1,
|
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
|
"The shape of factor(tensor) must be [1] rather than %d",
|
|
|
|
|
factor.size()));
|
|
|
|
|
for (auto& attr : attrs) {
|
|
|
|
|
*attr.second = factor[0];
|
|
|
|
|
}
|
|
|
|
|