Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PaddlePaddle Hackathon 2】20、为 Paddle 新增 cummax API #42480

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions paddle/fluid/operators/cummax_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {

class CummaxOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
};

class CummaxOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X", "(Tensor), The input tensor of cummax operator");
AddOutput("Out", "(Tensor), The output tensor of cummax operator");
AddOutput("Indices", "(Tensor), The indices of cummax elements");
AddAttr<int64_t>("axis",
"The dimension of accumulate along, -1 mean the last"
"dimension [default -1]")
.SetDefault(-1);
AddComment(R"DOC(
Return the cumulative maximum elements and indices of input tensor along a given
axis. For example, if input X is a tensor, the output will also be a the same
shape tensor, and elements y[i] = max(x[0], x[1], ..., x[i]).
)DOC");
}
};

class CummaxGradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
};


template <typename T>
class CummaxGradOpMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
protected:
void Apply(GradOpPtr<T> op) const override {
op->SetType("cummax_grad");
op->SetInput("X", this->Input("X"));
op->SetInput("Indices", this->Output("Indices"));
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
}
};

DECLARE_NO_NEED_BUFFER_VARS_INFERER(CummaxGradNoNeedBufferVarsInferer, "X");

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(cummax, CummaxInferShapeFunctor,
PD_INFER_META(phi::CummaxInferMeta));
REGISTER_OPERATOR(cummax, ops::CummaxOp, ops::CummaxOpMaker,
ops::CummaxGradOpMaker<paddle::framework::OpDesc>,
ops::CummaxGradOpMaker<paddle::imperative::OpBase>,
CummaxInferShapeFunctor);
DECLARE_INFER_SHAPE_FUNCTOR(cummax_grad,
CummaxGradInferShapeFunctor,
PD_INFER_META(phi::CummaxGradInferMeta));

REGISTER_OPERATOR(cummax_grad, ops::CummaxGradOp,
CummaxGradInferShapeFunctor);
14 changes: 13 additions & 1 deletion paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
}
}

void CummaxGradInferMeta(const MetaTensor& x,
const MetaTensor& indices,
const MetaTensor& out_grad,
MetaTensor* x_grad) {
const auto& dtype = out_grad.dtype();
x_grad->set_dims(x.dims());
x_grad->share_lod(x);
x_grad->set_dtype(dtype);
}

void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
const MetaTensor& softmax,
const MetaTensor& loss_grad,
Expand Down Expand Up @@ -320,7 +330,9 @@ void KernelWithXShapeInferMeta(const MetaTensor& xshape, MetaTensor* dx) {
}

void MaxPoolWithIndexGradInferMeta(const MetaTensor& x,
const MetaTensor& mask,
const MetaTensor&

,
const MetaTensor& dout,
const std::vector<int>& kernel_size,
const std::vector<int>& strides,
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/infermeta/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ void Conv2dTransposeDoubleGradInferMeta(const MetaTensor& x,
MetaTensor* dfilter,
MetaTensor* ddout);

void CummaxGradInferMeta(const MetaTensor& x,
const MetaTensor& indices,
const MetaTensor& out_grad,
MetaTensor* x_grad);

void CrossEntropyWithSoftmaxGradInferMeta(const MetaTensor& label,
const MetaTensor& softmax,
const MetaTensor& loss_grad,
Expand Down
28 changes: 28 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out) {
out->set_layout(x.layout());
}


void CummaxInferMeta(const MetaTensor& x,
int axis,
MetaTensor* out,
MetaTensor* indices) {
const auto& x_dims = x.dims();
const auto& num_dims = x_dims.size();

PADDLE_ENFORCE_GE(
axis,
-num_dims,
phi::errors::InvalidArgument(
"'axis'(%d) must be greater than or equal to -num_dims(%d).",
axis, -num_dims));
PADDLE_ENFORCE_LT(
axis,
num_dims,
phi::errors::InvalidArgument(
"'axis'(%d) must be less than num_dims(%d).", axis, num_dims));

out->share_dims(x);
out->set_dtype(x.dtype());
indices->share_dims(x);
indices->set_dtype(DataType::INT64);
out->share_lod(x);
indices->share_lod(x);
}

void CumsumInferMeta(const MetaTensor& x,
int axis,
bool flatten,
Expand Down
5 changes: 5 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out);

void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out);

void CummaxInferMeta(const MetaTensor& x,
int axis,
MetaTensor* out,
MetaTensor* indices);

void CumsumInferMeta(const MetaTensor& x,
int axis,
bool flatten,
Expand Down
29 changes: 29 additions & 0 deletions paddle/phi/kernels/cpu/cummax_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/cummax_grad_kernel.h"
#include "paddle/phi/kernels/impl/cummax_grad_kernel_impl.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"


PD_REGISTER_KERNEL(cummax_grad,
CPU,
ALL_LAYOUT,
phi::CumsumGradKernel,
float,
double,
int,
int64_t) {}
28 changes: 28 additions & 0 deletions paddle/phi/kernels/cpu/cummax_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/cummax_kernel.h"
#include "paddle/phi/kernels/impl/cummax_kernel_impl.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

PD_REGISTER_KERNEL(cummax,
CPU,
ALL_LAYOUT,
phi::CummaxKernel,
float,
double,
int,
int64_t) {}
28 changes: 28 additions & 0 deletions paddle/phi/kernels/cummax_grad_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void CumsumGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& indices,
const DenseTensor& out_grad,
DenseTensor* x_grad);

} // namespace phi
28 changes: 28 additions & 0 deletions paddle/phi/kernels/cummax_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void CummaxKernel(const Context& dev_ctx,
const DenseTensor& x,
int axis,
DenseTensor* out,
DenseTensor* indices);

} // namespace phi
29 changes: 29 additions & 0 deletions paddle/phi/kernels/gpu/cummax_grad_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/cummax_grad_kernel.h"
#include "paddle/phi/kernels/impl/cummax_grad_kernel_impl.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"


PD_REGISTER_KERNEL(cummax_grad,
GPU,
ALL_LAYOUT,
phi::CumsumGradKernel,
float,
double,
int,
int64_t) {}
28 changes: 28 additions & 0 deletions paddle/phi/kernels/gpu/cummax_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/cummax_kernel.h"
#include "paddle/phi/kernels/impl/cummax_kernel_impl.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

PD_REGISTER_KERNEL(cummax,
GPU,
ALL_LAYOUT,
phi::CummaxKernel,
float,
double,
int,
int64_t) {}
32 changes: 32 additions & 0 deletions paddle/phi/kernels/impl/cummax_grad_kernel_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/scatter.h"

namespace phi {
template <typename T, typename Context>
void CumsumGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& indices,
const DenseTensor& out_grad,
DenseTensor* x_grad) {
dev_ctx.template Alloc<T>(x_grad);
// phi::ScatterKernel(dev_ctx, x, indices, out_grad, true, x_grad);
phi::funcs::ScatterAssignAdd<T, int64_t>(dev_ctx, out_grad, indices, x_grad);
}

} // namespace phi
Loading