Skip to content

Commit

Permalink
Merge branch 'develop' of /~https://github.com/PaddlePaddle/paddle into…
Browse files Browse the repository at this point in the history
… second
  • Loading branch information
AshburnLee committed Mar 11, 2022
2 parents c4c52a9 + ad037ca commit db0e2c8
Show file tree
Hide file tree
Showing 32 changed files with 1,012 additions and 552 deletions.
44 changes: 8 additions & 36 deletions paddle/fluid/operators/erf_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License. */
#include <string>
#include <unordered_map>

#include "paddle/fluid/operators/erf_op.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {
Expand All @@ -29,18 +31,6 @@ class ErfOp : public framework::OperatorWithKernel {
const framework::AttributeMap &attrs)
: OperatorWithKernel(type, inputs, outputs, attrs) {}

void InferShape(framework::InferShapeContext *ctx) const override {
PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true,
platform::errors::InvalidArgument(
"Input(%s) of ErfOp should not be null.", "X"));
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
platform::errors::InvalidArgument(
"Output(%s) of ErfOp should not be null.", "Out"));

ctx->ShareDim("X", /*->*/ "Out");
ctx->ShareLoD("X", /*->*/ "Out");
}

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
Expand Down Expand Up @@ -116,28 +106,10 @@ class ErfGradOpMaker : public framework::SingleGradOpMaker<T> {

namespace ops = paddle::operators;

DECLARE_INFER_SHAPE_FUNCTOR(erf, ErfInferShapeFunctor,
PD_INFER_META(phi::UnchangedInferMeta));
REGISTER_OPERATOR(erf, ops::ErfOp, ops::ErfOpMaker,
ops::ErfGradOpMaker<paddle::framework::OpDesc>,
ops::ErfGradOpMaker<paddle::imperative::OpBase>);
ops::ErfGradOpMaker<paddle::imperative::OpBase>,
ErfInferShapeFunctor);
REGISTER_OPERATOR(erf_grad, ops::ErfGradOp);
REGISTER_OP_CPU_KERNEL(
erf, ops::ErfKernel<paddle::platform::CPUDeviceContext, float>,
ops::ErfKernel<paddle::platform::CPUDeviceContext, double>,
ops::ErfKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>);
REGISTER_OP_CPU_KERNEL(
erf_grad, ops::ErfGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::ErfGradKernel<paddle::platform::CPUDeviceContext, double>,
ops::ErfGradKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>);

REGISTER_OP_CUDA_KERNEL(
erf, ops::ErfKernel<paddle::platform::CUDADeviceContext, float>,
ops::ErfKernel<paddle::platform::CUDADeviceContext, double>,
ops::ErfKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>);
REGISTER_OP_CUDA_KERNEL(
erf_grad, ops::ErfGradKernel<paddle::platform::CUDADeviceContext, float>,
ops::ErfGradKernel<paddle::platform::CUDADeviceContext, double>,
ops::ErfGradKernel<paddle::platform::CUDADeviceContext,
paddle::platform::float16>);
66 changes: 0 additions & 66 deletions paddle/fluid/operators/erf_op.h

This file was deleted.

30 changes: 1 addition & 29 deletions paddle/fluid/operators/expand_as_v2_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,9 @@ REGISTER_OPERATOR(expand_as_v2, ops::ExpandAsV2Op, ops::ExpandAsV2OpMaker,
ops::ExpandAsV2GradOpMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(expand_as_v2_grad, ops::ExpandAsV2GradOp,
ops::ExpandAsV2GradNoNeedBufVarsInferer);
REGISTER_OP_CPU_KERNEL(
expand_as_v2,
ops::ExpandAsV2Kernel<paddle::platform::CPUDeviceContext, float>,
ops::ExpandAsV2Kernel<paddle::platform::CPUDeviceContext, double>,
ops::ExpandAsV2Kernel<paddle::platform::CPUDeviceContext, int>,
ops::ExpandAsV2Kernel<paddle::platform::CPUDeviceContext, int64_t>,
ops::ExpandAsV2Kernel<paddle::platform::CPUDeviceContext, bool>);
REGISTER_OP_CPU_KERNEL(
expand_as_v2_grad,
ops::ExpandAsV2GradKernel<paddle::platform::CPUDeviceContext, int>,
ops::ExpandAsV2GradKernel<paddle::platform::CPUDeviceContext, int64_t>,
ops::ExpandAsV2GradKernel<paddle::platform::CPUDeviceContext, float>,
ops::ExpandAsV2GradKernel<paddle::platform::CPUDeviceContext, double>);
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
REGISTER_OP_CUDA_KERNEL(
expand_as_v2,
ops::ExpandAsV2Kernel<paddle::platform::CUDADeviceContext, float>,
ops::ExpandAsV2Kernel<paddle::platform::CUDADeviceContext, double>,
ops::ExpandAsV2Kernel<paddle::platform::CUDADeviceContext, int>,
ops::ExpandAsV2Kernel<paddle::platform::CUDADeviceContext, int64_t>,
ops::ExpandAsV2Kernel<paddle::platform::CUDADeviceContext, bool>);
REGISTER_OP_CUDA_KERNEL(
expand_as_v2_grad,
ops::ExpandAsV2GradKernel<paddle::platform::CUDADeviceContext, int>,
ops::ExpandAsV2GradKernel<paddle::platform::CUDADeviceContext, int64_t>,
ops::ExpandAsV2GradKernel<paddle::platform::CUDADeviceContext, float>,
ops::ExpandAsV2GradKernel<paddle::platform::CUDADeviceContext, double>);
#endif

REGISTER_OP_VERSION(expand_as_v2)
.AddCheckpoint(
R"ROC(fix expand_as_v2 and add new input [Y])ROC",
paddle::framework::compatible::OpVersionDesc().NewInput(
"Y", "Expand X according to the shape of Y"));
"Y", "Expand X according to the shape of Y"));
214 changes: 0 additions & 214 deletions paddle/fluid/operators/expand_as_v2_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,219 +32,5 @@ template <typename T, size_t D, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;

template <typename DeviceContext, typename T>
class ExpandAsV2Kernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto rank = context.Input<Tensor>("X")->dims().size();
auto target_shape = context.Attr<std::vector<int>>("target_shape");
auto target_rank = target_shape.size();
PADDLE_ENFORCE_GE(target_rank, rank,
platform::errors::InvalidArgument(
"The rank (%d) of the input 'target_tensor' for "
"expand_as_v2 op must be greater than or equal to "
"the rank (%d) of the input 'x'.",
target_rank, rank));
PADDLE_ENFORCE_GE(rank, 1, platform::errors::InvalidArgument(
"The rank (%d) of the input 'x' for "
"expand_as_v2 op must be positive.",
rank));
PADDLE_ENFORCE_LE(target_rank, MAX_RANK_SUPPORTED,
platform::errors::InvalidArgument(
"The rank (%d) of the input 'target_tensor' for "
"expand_as_v2 op must be less than or equal to %d.",
target_rank, MAX_RANK_SUPPORTED));

switch (target_rank) {
case 1:
ExpandAs<1>(context);
break;
case 2:
ExpandAs<2>(context);
break;
case 3:
ExpandAs<3>(context);
break;
case 4:
ExpandAs<4>(context);
break;
case 5:
ExpandAs<5>(context);
break;
case 6:
ExpandAs<6>(context);
break;
}
}

protected:
template <int Rank>
void ExpandAs(const framework::ExecutionContext& context) const {
auto* in0 = context.Input<Tensor>("X");
auto in_dims = in0->dims();
auto target_shape = context.Attr<std::vector<int>>("target_shape");
auto vec_in_dims = phi::vectorize<int>(in_dims);
auto diff = target_shape.size() - vec_in_dims.size();
vec_in_dims.insert(vec_in_dims.begin(), diff, 1);
std::vector<int> repeat_times(vec_in_dims.size());
for (size_t i = 0; i < vec_in_dims.size(); ++i) {
PADDLE_ENFORCE_NE(target_shape[i], 0,
platform::errors::InvalidArgument(
"The value of target shape cannot be zero."));
if (i < diff) {
PADDLE_ENFORCE_GT(
target_shape[i], 0,
platform::errors::InvalidArgument(
"The expanded size (%d) for non-existing dimensions must be "
"positive for expand_as_v2 op.",
target_shape[i]));
repeat_times[i] = target_shape[i];
} else if (target_shape[i] > 0) {
if (vec_in_dims[i] != 1) {
PADDLE_ENFORCE_EQ(
vec_in_dims[i], target_shape[i],
platform::errors::InvalidArgument(
"The value (%d) of the non-singleton dimension does not match"
" the corresponding value (%d) in shape for expand_as_v2 op.",
vec_in_dims[i], target_shape[i]));
repeat_times[i] = 1;
} else {
repeat_times[i] = target_shape[i];
}
} else {
PADDLE_ENFORCE_EQ(
target_shape[i], -1,
platform::errors::InvalidArgument(
"When the value in shape is negative for expand_as_v2 op, "
"only -1 is supported, but the value received is %d.",
target_shape[i]));
repeat_times[i] = 1;
}
}
auto* out0 = context.Output<Tensor>("Out");
Eigen::DSizes<Eigen::DenseIndex, Rank> bcast_dims;
for (size_t i = 0; i < repeat_times.size(); ++i) {
bcast_dims[i] = repeat_times[i];
}

framework::DDim new_in_dims = phi::make_ddim(vec_in_dims);
framework::DDim out_dims = phi::make_ddim(target_shape);

out0->Resize(out_dims);
auto x = EigenTensor<T, Rank>::From(*in0, new_in_dims);
out0->mutable_data<T>(context.GetPlace());
auto y = EigenTensor<T, Rank>::From(*out0, out_dims);
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
EigenBroadcast<std::decay_t<decltype(place)>, T, Rank>::Eval(place, y, x,
bcast_dims);
}
};

template <typename DeviceContext, typename T>
class ExpandAsV2GradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
auto* in0 = context.Input<Tensor>("X");
auto target_shape = context.Attr<std::vector<int>>("target_shape");
auto x_dims = in0->dims();
auto vec_in_dims = phi::vectorize<int>(x_dims);
auto diff = target_shape.size() - vec_in_dims.size();
vec_in_dims.insert(vec_in_dims.begin(), diff, 1);
std::vector<int> repeat_times(vec_in_dims.size());
for (size_t i = 0; i < vec_in_dims.size(); ++i) {
repeat_times[i] = target_shape[i] / vec_in_dims[i];
}
std::vector<int> reshape_dims_vec;
std::vector<int> reduce_dims_vec;
for (size_t i = 0; i < repeat_times.size(); ++i) {
reduce_dims_vec.push_back(reshape_dims_vec.size());
reshape_dims_vec.push_back(repeat_times[i]);
reshape_dims_vec.push_back(vec_in_dims[i]);
}

int dims = reduce_dims_vec.size();
bool just_copy = true;
for (size_t i = 0; i < repeat_times.size(); i++) {
if (repeat_times[i] != 1) {
just_copy = false;
break;
}
}
// no need reduce, just copy
if (just_copy) {
auto* in0 = context.Input<Tensor>(framework::GradVarName("Out"));
auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
out0->mutable_data<T>(context.GetPlace());
framework::TensorCopy(*in0, context.GetPlace(), context.device_context(),
out0);
} else {
PADDLE_ENFORCE_GE(dims, 1,
platform::errors::InvalidArgument(
"The rank of the input 'Out@GRAD' for "
"expand_as_v2_grad op must be greater than or "
"equal to 1, but the value received is %d.",
dims));
PADDLE_ENFORCE_LE(dims, MAX_RANK_SUPPORTED,
platform::errors::InvalidArgument(
"The rank of the input 'Out@GRAD' for "
"expand_as_v2_grad op must be less than or equal "
"to %d, but the value received is %d.",
MAX_RANK_SUPPORTED, dims));
switch (dims) {
case 1:
ExpandAsBackward<1>(context, reshape_dims_vec, reduce_dims_vec);
break;
case 2:
ExpandAsBackward<2>(context, reshape_dims_vec, reduce_dims_vec);
break;
case 3:
ExpandAsBackward<3>(context, reshape_dims_vec, reduce_dims_vec);
break;
case 4:
ExpandAsBackward<4>(context, reshape_dims_vec, reduce_dims_vec);
break;
case 5:
ExpandAsBackward<5>(context, reshape_dims_vec, reduce_dims_vec);
break;
case 6:
ExpandAsBackward<6>(context, reshape_dims_vec, reduce_dims_vec);
break;
default:
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support tensor with rank being between 1 and 6. But "
"received tensor's rank = %d.",
dims));
}
}
}

protected:
template <int Dims>
void ExpandAsBackward(const framework::ExecutionContext& context,
const std::vector<int>& reshape_dims_vec,
const std::vector<int>& reduce_dims_vec) const {
size_t reshape_size = reshape_dims_vec.size();
size_t reduce_size = reduce_dims_vec.size();
auto* in0 = context.Input<Tensor>(framework::GradVarName("Out"));
auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
out0->mutable_data<T>(context.GetPlace());
auto x_grad = EigenVector<T>::Flatten(*out0);
Eigen::DSizes<Eigen::DenseIndex, Dims * 2> reshape_dims;
for (size_t i = 0; i < reshape_size; ++i) {
reshape_dims[i] = reshape_dims_vec[i];
}
Eigen::DSizes<Eigen::DenseIndex, Dims> reduce_dims;
for (size_t i = 0; i < reduce_size; ++i) {
reduce_dims[i] = reduce_dims_vec[i];
}
auto out_grad = EigenVector<T>::Flatten(*in0);
auto& place =
*context.template device_context<DeviceContext>().eigen_device();
EigenBroadcastGrad<std::decay_t<decltype(place)>, T, Dims>::Eval(
place, x_grad, out_grad, reduce_dims, reshape_dims);
}
};

} // namespace operators
} // namespace paddle
Loading

1 comment on commit db0e2c8

@paddle-bot-old
Copy link

@paddle-bot-old paddle-bot-old bot commented on db0e2c8 Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵️ CI failures summary

🔍 PR: #40448 Commit ID: db0e2c8 contains failed CI.

🔹 Failed: PR-CI-Windows

test_failed
2022-03-12 04:02:52 The following tests FAILED:
2022-03-12 04:02:52 1601 - test_rnn_encoder_decoder (Timeout)
2022-03-12 04:02:52 1601 - test_rnn_encoder_decoder (Timeout)
2022-03-12 04:02:52 1601 - test_rnn_encoder_decoder (Timeout)
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>goto:eof
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>set error_code=8
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>for /F %# in ('wmic os get localdatetime|findstr 20') do set end=%#
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>set end=20220312040252.673000+480
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>set end=0312040252
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>call :timestamp "0312024400" "0312040252" "1 card TestCases Total"
2022-03-12 04:02:52 C:\Users\Administrator\Downloads\workspace\7f9a2981-ac59-4851-b15b-189916dbbd2e\Paddle\build>setlocal enabledelayedexpansion
2022-03-12 04:02:52 1046640
2022-03-12 04:02:52 "Windows 1 card TestCases Total Time: 4732s"
2022-03-12 04:02:52 ipipe_log_param_Windows_1_card_TestCases_Total_Time: 4732s
2022-03-12 04:02:52 1046640
2022-03-12 04:02:52 "Windows TestCases Total Time: 4732s"
2022-03-12 04:02:52 ipipe_log_param_Windows_TestCases_Total_Time: 4732s
2022-03-12 04:02:52 Running unit tests failed, will exit
2022-03-12 04:02:52 CI failed, try to upload built wheel.
2022-03-12 04:02:52 build\python\dist\paddlepaddle_gpu-0.0.0-cp37-cp37m-win_amd64.whl
2022-03-12 04:02:52 复制了 1 个文件

Please sign in to comment.