Skip to content

Commit

Permalink
[CleanOps]del_unuseful_op10 (PaddlePaddle#57810)
Browse files Browse the repository at this point in the history
* del_unuseful_op10
  • Loading branch information
wanghuancoder authored and Frida-a committed Oct 14, 2023
1 parent 2c8c59d commit 16e9f3b
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 1,875 deletions.
167 changes: 31 additions & 136 deletions paddle/fluid/operators/flatten_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,53 @@ limitations under the License. */

namespace paddle {
namespace operators {

class FlattenOp : public framework::OperatorWithKernel {
// FIXME(zcd): flatten2 adds an intermediate output(XShape) based on flatten,
// the XShape is used to carry the shape and lod of X which will be used in
// flatten_grad, in this way, the framework can reuse the memory of X
// immediately the flatten2_op is finished.
// Considering compatibility issues, we could not fix flatten2_op
class Flatten2Op : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Flatten");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Flatten");
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Flatten2");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Flatten2");
const auto &axis = ctx->Attrs().Get<int>("axis");
const auto &in_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE_GE(axis,
0,
platform::errors::InvalidArgument(
"The axis should be greater than or equal to 0."));
if (in_dims.size() > 0) {
PADDLE_ENFORCE_LE(
axis,
in_dims.size(),
platform::errors::InvalidArgument(
"The axis should be less than or equal to input tensor's rank."));
}
PADDLE_ENFORCE_LE(
axis,
in_dims.size(),
platform::errors::InvalidArgument(
"The axis should be less than or equal to input tensor's rank"));

const auto &out_dims = GetOutputShape(axis, in_dims);
const auto &out_dims = Flatten2Op::GetOutputShape(axis, in_dims);
ctx->SetOutputDim("Out", phi::make_ddim(out_dims));
if (in_dims[0] == out_dims[0]) {
// Only pass LoD when the first dimension of output and Input(X)
// are the same.
ctx->ShareLoD("X", "Out");
}
if (!ctx->HasOutput("XShape")) return;
// OP_INOUT_CHECK(ctx->HasOutput("XShape"), "Output", "XShape", "Flatten2");
std::vector<int64_t> xshape_dims(in_dims.size() + 1);
xshape_dims[0] = 0;
for (int i = 0; i < in_dims.size(); ++i) {
xshape_dims[i + 1] = in_dims[i];
}
ctx->SetOutputDim("XShape", phi::make_ddim(xshape_dims));
ctx->ShareLoD("X", "XShape");
}

phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
auto input_data_type =
framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");
return phi::KernelKey(input_data_type, ctx.GetPlace());
}

static std::vector<int32_t> GetOutputShape(const int axis,
Expand Down Expand Up @@ -85,17 +103,9 @@ class FlattenOp : public framework::OperatorWithKernel {
out_shape[1] = static_cast<int32_t>(inner);
return out_shape;
}

protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
auto input_data_type =
framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");
return phi::KernelKey(input_data_type, ctx.GetPlace());
}
};

class FlattenOpMaker : public framework::OpProtoAndCheckerMaker {
class Flatten2OpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X", "(Tensor) A tensor of rank >= axis.");
Expand Down Expand Up @@ -145,96 +155,6 @@ Case 2:
We get:
Out.shape = (1, 3 * 100 * 100 * 4)
)DOC");
}
};

class FlattenGradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *context) const override {
context->SetOutputDim(framework::GradVarName("X"),
context->GetInputDim("X"));
context->ShareLoD("X", framework::GradVarName("X"));
}

protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out"));
return phi::KernelKey(input_data_type, ctx.GetPlace());
}
};

template <typename T>
class FlattenGradOpMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

void Apply(GradOpPtr<T> grad_op) const override {
grad_op->SetType("flatten_grad");
grad_op->SetInput("X", this->Input("X"));
grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
grad_op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
grad_op->SetAttrMap(this->Attrs());
}
};

// FIXME(zcd): flatten2 adds an intermediate output(XShape) based on flatten,
// the XShape is used to carry the shape and lod of X which will be used in
// flatten_grad, in this way, the framework can reuse the memory of X
// immediately the flatten2_op is finished.
// Considering compatibility issues, we could not fix flatten2_op
class Flatten2Op : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext *ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Flatten2");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Flatten2");
const auto &axis = ctx->Attrs().Get<int>("axis");
const auto &in_dims = ctx->GetInputDim("X");
PADDLE_ENFORCE_GE(axis,
0,
platform::errors::InvalidArgument(
"The axis should be greater than or equal to 0."));
PADDLE_ENFORCE_LE(
axis,
in_dims.size(),
platform::errors::InvalidArgument(
"The axis should be less than or equal to input tensor's rank"));

const auto &out_dims = FlattenOp::GetOutputShape(axis, in_dims);
ctx->SetOutputDim("Out", phi::make_ddim(out_dims));
if (in_dims[0] == out_dims[0]) {
// Only pass LoD when the first dimension of output and Input(X)
// are the same.
ctx->ShareLoD("X", "Out");
}
if (!ctx->HasOutput("XShape")) return;
// OP_INOUT_CHECK(ctx->HasOutput("XShape"), "Output", "XShape", "Flatten2");
std::vector<int64_t> xshape_dims(in_dims.size() + 1);
xshape_dims[0] = 0;
for (int i = 0; i < in_dims.size(); ++i) {
xshape_dims[i + 1] = in_dims[i];
}
ctx->SetOutputDim("XShape", phi::make_ddim(xshape_dims));
ctx->ShareLoD("X", "XShape");
}

phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
auto input_data_type =
framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");
return phi::KernelKey(input_data_type, ctx.GetPlace());
}
};

class Flatten2OpMaker : public FlattenOpMaker {
public:
void Make() override {
FlattenOpMaker::Make();
AddOutput("XShape",
"XShape is just used to store the shape and lod of X, which will "
"be used in FlattenGradOp.")
Expand Down Expand Up @@ -293,17 +213,6 @@ DECLARE_NO_NEED_BUFFER_VARS_INFERER(FlattenGradNoNeedBufferVarsInferer, "X");
} // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(flatten,
ops::FlattenOp,
ops::FlattenOpMaker,
ops::FlattenGradOpMaker<paddle::framework::OpDesc>,
ops::FlattenGradOpMaker<paddle::imperative::OpBase>,
ops::FlattenOpInplaceInferer);
REGISTER_OPERATOR(flatten_grad,
ops::FlattenGradOp,
ops::FlattenGradInplaceInferer,
ops::FlattenGradNoNeedBufferVarsInferer);

REGISTER_OPERATOR(flatten2,
ops::Flatten2Op,
ops::Flatten2OpMaker,
Expand All @@ -314,20 +223,6 @@ REGISTER_OPERATOR(flatten2_grad,
ops::Flatten2GradOp,
ops::FlattenGradInplaceInferer);

REGISTER_OP_CPU_KERNEL(flatten,
ops::FlattenKernel<phi::CPUContext, float>,
ops::FlattenKernel<phi::CPUContext, double>,
ops::FlattenKernel<phi::CPUContext, uint8_t>,
ops::FlattenKernel<phi::CPUContext, int>,
ops::FlattenKernel<phi::CPUContext, int8_t>,
ops::FlattenKernel<phi::CPUContext, int64_t>);
REGISTER_OP_CPU_KERNEL(flatten_grad,
ops::FlattenGradKernel<phi::CPUContext, float>,
ops::FlattenGradKernel<phi::CPUContext, double>,
ops::FlattenGradKernel<phi::CPUContext, uint8_t>,
ops::FlattenGradKernel<phi::CPUContext, int>,
ops::FlattenGradKernel<phi::CPUContext, int8_t>,
ops::FlattenGradKernel<phi::CPUContext, int64_t>);
REGISTER_OP_CPU_KERNEL(flatten2,
ops::Flatten2Kernel<phi::CPUContext, float>,
ops::Flatten2Kernel<phi::CPUContext, double>,
Expand Down
14 changes: 0 additions & 14 deletions paddle/fluid/operators/flatten_op.cu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ limitations under the License. */

namespace ops = paddle::operators;

REGISTER_OP_CUDA_KERNEL(flatten,
ops::FlattenKernel<phi::GPUContext, float>,
ops::FlattenKernel<phi::GPUContext, double>,
ops::FlattenKernel<phi::GPUContext, uint8_t>,
ops::FlattenKernel<phi::GPUContext, int>,
ops::FlattenKernel<phi::GPUContext, int8_t>,
ops::FlattenKernel<phi::GPUContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(flatten_grad,
ops::FlattenGradKernel<phi::GPUContext, float>,
ops::FlattenGradKernel<phi::GPUContext, double>,
ops::FlattenGradKernel<phi::GPUContext, uint8_t>,
ops::FlattenGradKernel<phi::GPUContext, int>,
ops::FlattenGradKernel<phi::GPUContext, int8_t>,
ops::FlattenGradKernel<phi::GPUContext, int64_t>);
REGISTER_OP_CUDA_KERNEL(flatten2,
ops::Flatten2Kernel<phi::GPUContext, float>,
ops::Flatten2Kernel<phi::GPUContext, double>,
Expand Down
50 changes: 5 additions & 45 deletions paddle/fluid/operators/flatten_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class FlattenKernel : public framework::OpKernel<T> {
class Flatten2Kernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &context) const override {
auto &axes = context.Attr<int>("axis");

auto *in = context.Input<phi::DenseTensor>("X");
auto x_dims = in->dims();

auto *out = context.Output<phi::DenseTensor>("Out");

auto &axes = context.Attr<int>("axis");
auto x_dims = in->dims();
auto out_dims = phi::make_ddim(GetOutputShape(axes, x_dims));

out->mutable_data(context.GetPlace(), in->type());
Expand Down Expand Up @@ -68,48 +70,6 @@ class FlattenKernel : public framework::OpKernel<T> {
}
};

template <typename DeviceContext, typename T>
class FlattenGradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &ctx) const override {
auto *d_x = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
auto *d_out = ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
auto in_dims = ctx.Input<phi::DenseTensor>("X")->dims();

d_x->mutable_data(ctx.GetPlace(), d_out->type());
framework::TensorCopy(
*d_out,
ctx.GetPlace(),
ctx.template device_context<platform::DeviceContext>(),
d_x);
d_x->Resize(in_dims);
}
};

template <typename DeviceContext, typename T>
class Flatten2Kernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext &context) const override {
auto &axes = context.Attr<int>("axis");

auto *in = context.Input<phi::DenseTensor>("X");
auto x_dims = in->dims();

auto *out = context.Output<phi::DenseTensor>("Out");

auto out_dims = phi::make_ddim(
FlattenKernel<DeviceContext, T>::GetOutputShape(axes, x_dims));

out->mutable_data(context.GetPlace(), in->type());
framework::TensorCopy(
*in,
context.GetPlace(),
context.template device_context<platform::DeviceContext>(),
out);
out->Resize(out_dims);
}
};

template <typename DeviceContext, typename T>
class Flatten2GradKernel : public framework::OpKernel<T> {
public:
Expand Down
12 changes: 0 additions & 12 deletions paddle/fluid/operators/flatten_op_xpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ limitations under the License. */
namespace ops = paddle::operators;
namespace plat = paddle::platform;

REGISTER_OP_XPU_KERNEL(
flatten,
ops::FlattenKernel<paddle::platform::XPUDeviceContext, float>,
ops::FlattenKernel<paddle::platform::XPUDeviceContext, int>,
ops::FlattenKernel<paddle::platform::XPUDeviceContext, int8_t>,
ops::FlattenKernel<paddle::platform::XPUDeviceContext, int64_t>);
REGISTER_OP_XPU_KERNEL(
flatten_grad,
ops::FlattenGradKernel<paddle::platform::XPUDeviceContext, float>,
ops::FlattenGradKernel<paddle::platform::XPUDeviceContext, int>,
ops::FlattenGradKernel<paddle::platform::XPUDeviceContext, int8_t>,
ops::FlattenGradKernel<paddle::platform::XPUDeviceContext, int64_t>);
REGISTER_OP_XPU_KERNEL(
flatten2,
ops::Flatten2Kernel<paddle::platform::XPUDeviceContext, float>,
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/mkldnn/reshape_mkldnn_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class ReshapeMKLDNNKernel : public framework::OpKernel<T> {
x_dims = x->dims();
auto axes = ctx.Attr<int>("axis");
out_dims = phi::make_ddim(
FlattenKernel<phi::CPUContext, float>::GetOutputShape(axes, x_dims));
Flatten2Kernel<phi::CPUContext, float>::GetOutputShape(axes, x_dims));
}

protected:
Expand Down
Loading

0 comments on commit 16e9f3b

Please sign in to comment.