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

(this PR is just for my personal self-learning) [PaddlePaddle Hackathon 2 No.22] add paddle.index_add to Paddle #42475

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
94fb159
Add files via upload
OccupyMars2025 May 4, 2022
b4442be
Add files via upload
OccupyMars2025 May 4, 2022
f60ad37
Add files via upload
OccupyMars2025 May 4, 2022
9adf9cc
Add files via upload
OccupyMars2025 May 4, 2022
65cfb27
Add files via upload
OccupyMars2025 May 4, 2022
50e2cd1
Add files via upload
OccupyMars2025 May 4, 2022
9b8380b
Add files via upload
OccupyMars2025 May 4, 2022
65050b4
Add files via upload
OccupyMars2025 May 4, 2022
b3cf6be
Add files via upload
OccupyMars2025 May 4, 2022
04a3fce
Add files via upload
OccupyMars2025 May 4, 2022
5c3f5d1
Add files via upload
OccupyMars2025 May 4, 2022
f6cda0f
Add files via upload
OccupyMars2025 May 4, 2022
9415651
Add files via upload
OccupyMars2025 May 4, 2022
75c1869
Add files via upload
OccupyMars2025 May 4, 2022
35da3f7
Add files via upload
OccupyMars2025 May 4, 2022
88af7e7
Add files via upload
OccupyMars2025 May 4, 2022
2e2cf4c
Add files via upload
OccupyMars2025 May 4, 2022
98ab985
Add files via upload
OccupyMars2025 May 4, 2022
7b2dbef
Add files via upload
OccupyMars2025 May 4, 2022
d0a2631
Add files via upload
OccupyMars2025 May 4, 2022
b546b05
Add files via upload
OccupyMars2025 May 5, 2022
41d7be0
Add files via upload
OccupyMars2025 May 5, 2022
62a7f1c
Add files via upload
OccupyMars2025 May 5, 2022
959dbc2
Add files via upload
OccupyMars2025 May 5, 2022
00b25c2
Add files via upload
OccupyMars2025 May 5, 2022
04491ff
Add files via upload
OccupyMars2025 May 10, 2022
181665f
Add files via upload
OccupyMars2025 May 10, 2022
cfd71bb
Update __init__.py
OccupyMars2025 May 10, 2022
88d1295
Merge branch 'develop' into hackathon-2nd-task22-add-index_add
OccupyMars2025 May 10, 2022
0e62f69
Merge branch 'PaddlePaddle:develop' into hackathon-2nd-task22-add-ind…
OccupyMars2025 May 10, 2022
700dbee
Add files via upload
OccupyMars2025 May 12, 2022
75af4a1
Add files via upload
OccupyMars2025 May 12, 2022
3838749
Add files via upload
OccupyMars2025 May 12, 2022
dc8af85
Add files via upload
OccupyMars2025 May 12, 2022
aaaba0c
Add files via upload
OccupyMars2025 May 12, 2022
c017dac
Add files via upload
OccupyMars2025 May 12, 2022
2bcd776
Add files via upload
OccupyMars2025 May 12, 2022
78ce053
Add files via upload
OccupyMars2025 May 12, 2022
930e314
Add files via upload
OccupyMars2025 May 12, 2022
ae72a00
Add files via upload
OccupyMars2025 May 12, 2022
f66dc0c
Add files via upload
OccupyMars2025 May 12, 2022
33bb556
Add files via upload
OccupyMars2025 May 13, 2022
9de7ee3
Add files via upload
OccupyMars2025 May 13, 2022
43fc9f6
Add files via upload
OccupyMars2025 May 13, 2022
28839dd
Add files via upload
OccupyMars2025 May 14, 2022
b90a58c
Add files via upload
OccupyMars2025 May 14, 2022
a02247d
Add files via upload
OccupyMars2025 May 15, 2022
8fdae4a
Add files via upload
OccupyMars2025 May 15, 2022
c6cefc1
Add files via upload
OccupyMars2025 May 15, 2022
fbabcce
Add files via upload
OccupyMars2025 May 15, 2022
32ef9af
Add files via upload
OccupyMars2025 May 15, 2022
6a6f698
Add files via upload
OccupyMars2025 May 15, 2022
f68f280
Add files via upload
OccupyMars2025 May 15, 2022
354a3de
Add files via upload
OccupyMars2025 May 15, 2022
60454ed
Add files via upload
OccupyMars2025 May 15, 2022
418f613
Merge branch 'PaddlePaddle:develop' into hackathon-2nd-task22-add-ind…
OccupyMars2025 May 15, 2022
b714d86
Update unary.cc
OccupyMars2025 May 15, 2022
3b203b7
Update index_add_op.cc
OccupyMars2025 May 15, 2022
fbc056f
Add files via upload
OccupyMars2025 May 15, 2022
5ef0319
Add files via upload
OccupyMars2025 May 15, 2022
50037cb
Add files via upload
OccupyMars2025 May 15, 2022
66defb6
Add files via upload
OccupyMars2025 May 15, 2022
2e30e48
Add files via upload
OccupyMars2025 May 15, 2022
d59fe50
Add files via upload
OccupyMars2025 May 15, 2022
0362dae
Add files via upload
OccupyMars2025 May 15, 2022
c1b8a22
Add files via upload
OccupyMars2025 May 15, 2022
ddf255e
Add files via upload
OccupyMars2025 May 15, 2022
45c1e81
Add files via upload
OccupyMars2025 May 15, 2022
a0413f7
Add files via upload
OccupyMars2025 May 16, 2022
176d6bd
Add files via upload
OccupyMars2025 May 16, 2022
ceb519f
Add files via upload
OccupyMars2025 May 16, 2022
1b24d8b
Add files via upload
OccupyMars2025 May 16, 2022
a9c1617
Add files via upload
OccupyMars2025 May 16, 2022
01981d6
Add files via upload
OccupyMars2025 May 16, 2022
bfc0e35
Add files via upload
OccupyMars2025 May 16, 2022
d9cde21
Add files via upload
OccupyMars2025 May 16, 2022
33f0bcc
Add files via upload
OccupyMars2025 May 16, 2022
821eee6
Add files via upload
OccupyMars2025 May 16, 2022
d1aeda9
Add files via upload
OccupyMars2025 May 16, 2022
791c825
Add files via upload
OccupyMars2025 May 16, 2022
407673e
Add files via upload
OccupyMars2025 May 16, 2022
3b11f18
Add files via upload
OccupyMars2025 May 16, 2022
a113244
Merge branch 'PaddlePaddle:develop' into hackathon-2nd-task22-add-ind…
OccupyMars2025 May 16, 2022
df2ee5d
Merge branch 'PaddlePaddle:develop' into hackathon-2nd-task22-add-ind…
OccupyMars2025 May 16, 2022
b2d345b
just trigger CI again
OccupyMars2025 May 16, 2022
2483e07
add line 439 and line 440 EinsumInferMeta in paddle/phi/infermeta/un…
OccupyMars2025 May 17, 2022
9fbde57
restore code about heaviside API
OccupyMars2025 May 17, 2022
14786cf
restore code about heaviside API
OccupyMars2025 May 17, 2022
ff15e2d
uncomment other unit test cases
OccupyMars2025 May 17, 2022
24d3827
leave only one unit test case
OccupyMars2025 May 17, 2022
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
120 changes: 120 additions & 0 deletions paddle/fluid/operators/index_add_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* 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 <memory>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {

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

framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace());
}
};

class IndexAddOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(Tensor, default input Tensor<float>), "
"the input feature data of IndexAddOp, dtype should be"
"bool, int32, int64, float16, float32, float64.");
AddInput("IndexTensor",
"(Tensor, optional) If provided, index_add will use this."
"It has higher priority than attr(index).")
.AsDispensable();
AddInput("AxisTensor",
"(Tensor) If provided, use this as "
"axis, this has a higher priority than "
"attr(axis), the numel of this tensor MUST BE 1.")
.AsDispensable();
AddAttr<std::vector<int64_t>>(
"index",
"(list<int>) indices of corresponding axis in `axis`");
AddAttr<int>("axis", "(int), the dimension in which we index.");
AddAttr<float>("add_value", "(float) The value to add.");
AddOutput("Out",
"(Tensor, default Tensor<float>),"
" the output of IndexAddOp, whose dtype and shape are the same as X.");
AddComment(R"DOC(
IndexAdd operator
Add the elements of the input tensor with value
by selecting the indices in the order given in index.
This operator also supports inplace modification.
)DOC");
}
};

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

void Apply(GradOpPtr<T> op) const override {
op->SetType("index_add_grad");
if (this->HasInput("AxisTensor")) {
op->SetInput("AxisTensor", this->Input("AxisTensor"));
}
if (this->HasInput("IndexTensor")) {
op->SetInput("IndexTensor", this->Input("IndexTensor"));
}
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetAttrMap(this->Attrs());
}
};

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

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out")),
ctx.GetPlace());
}
};

DECLARE_INPLACE_OP_INFERER(IndexAddInplaceInferer, {"X", "Out"});
DECLARE_INPLACE_OP_INFERER(IndexAddGradInplaceInferer,
{framework::GradVarName("Out"),
framework::GradVarName("X")});

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(index_add, IndexAddInferShapeFunctor,
PD_INFER_META(phi::IndexAddInferMeta));

REGISTER_OPERATOR(index_add, ops::IndexAddOp, ops::IndexAddOpMaker,
ops::IndexAddGradMaker<paddle::framework::OpDesc>,
ops::IndexAddGradMaker<paddle::imperative::OpBase>,
ops::IndexAddInplaceInferer, IndexAddInferShapeFunctor);

DECLARE_INFER_SHAPE_FUNCTOR(index_add_grad, IndexAddGradInferShapeFunctor,
PD_INFER_META(phi::IndexAddGradInferMeta));

REGISTER_OPERATOR(index_add_grad, ops::IndexAddGradOp,
ops::IndexAddGradInplaceInferer,
IndexAddGradInferShapeFunctor);
129 changes: 129 additions & 0 deletions paddle/fluid/operators/index_add_tensor_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* 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 <memory>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/binary.h"

namespace paddle {
namespace operators {

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

framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(
OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace());
}
};

class IndexAddTensorOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(Tensor), "
"the input feature data of IndexAddTensorOp, dtype should be"
"bool, int32, int64, float16, float32, float64.");
AddInput("AddValue",
"(Tensor>), "
"the input tensor of IndexAddTensorOp, dtype should be"
"the same as input tensor X");
AddInput("IndexTensor",
"(Tensor, optional) If provided, index_add will use this."
"It has higher priority than attr(index).")
.AsDispensable();
AddInput("AxisTensor",
"(Tensor) If provided, use this as "
"axis, this has a higher priority than "
"attr(axis), the shape of this tensor MUST BE (1,).")
.AsDispensable();
AddAttr<std::vector<int64_t>>(
"index",
"(list<int>) indices of corresponding axis in `axis`");
AddAttr<int>("axis", "(int), the dimension in which we index.");
AddOutput(
"Out",
"(Tensor, default Tensor<float>),"
" the output of IndexAddTensorOp, whose dtype is the same as X.");
AddComment(R"DOC(
IndexAddTensor operator
Add the elements of the input tensor with value
by selecting the indices in the order given in index.
This operator also supports inplace modification.
)DOC");
}
};

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

void Apply(GradOpPtr<T> op) const override {
op->SetType("index_add_tensor_grad");
if (this->HasInput("AxisTensor")) {
op->SetInput("AxisTensor", this->Input("AxisTensor"));
}
if (this->HasInput("IndexTensor")) {
op->SetInput("IndexTensor", this->Input("IndexTensor"));
}
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
op->SetOutput(framework::GradVarName("AddValue"),
this->InputGrad("AddValue"));
op->SetAttrMap(this->Attrs());
}
};

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

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
ctx, framework::GradVarName("Out")),
ctx.GetPlace());
}
};

DECLARE_INPLACE_OP_INFERER(IndexAddTensorInplaceInferer, {"X", "Out"});
DECLARE_INPLACE_OP_INFERER(IndexAddTensorGradInplaceInferer,
{framework::GradVarName("Out"),
framework::GradVarName("X")});

} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(index_add_tensor, IndexAddTensorInferShapeFunctor,
PD_INFER_META(phi::IndexAddTensorInferMeta));

REGISTER_OPERATOR(index_add_tensor, ops::IndexAddTensorOp,
ops::IndexAddTensorOpMaker,
ops::IndexAddTensorGradMaker<paddle::framework::OpDesc>,
ops::IndexAddTensorGradMaker<paddle::imperative::OpBase>,
ops::IndexAddTensorInplaceInferer,
IndexAddTensorInferShapeFunctor);

DECLARE_INFER_SHAPE_FUNCTOR(index_add_tensor_grad,
IndexAddTensorGradInferShapeFunctor,
PD_INFER_META(phi::IndexAddTensorGradInferMeta));

REGISTER_OPERATOR(index_add_tensor_grad, ops::IndexAddTensorGradOp,
ops::IndexAddTensorGradInplaceInferer,
IndexAddTensorGradInferShapeFunctor);
58 changes: 57 additions & 1 deletion paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,62 @@ void HuberLossInferMeta(const MetaTensor& input,
out->share_lod(input);
}

void IndexAddTensorInferMeta(const MetaTensor& x,
const MetaTensor& add_tensor,
const IntArray& index_arr,
const Scalar& axis_scalar,
MetaTensor* output) {
auto input_dim = x.dims();
auto index = index_arr.GetData();
auto axis = axis_scalar.to<int>();

PADDLE_ENFORCE_EQ(
add_tensor.dtype() == x.dtype(),
true,
phi::errors::InvalidArgument(
"The dtype of add_tensor should be same as input tensor."));

PADDLE_ENFORCE_EQ(
axis < input_dim.size() && axis >= (0 - input_dim.size()),
true,
phi::errors::OutOfRange(
"Axis is out of range, It's expected "
"to be in range of [-%d, %d). But received Attr(axis) = %d.",
input_dim.size(),
input_dim.size(),
axis));

PADDLE_ENFORCE_EQ(
index.size() > 0,
true,
phi::errors::InvalidArgument("The index array should not be empty."));

output->set_dims(x.dims());
output->set_dtype(x.dtype());
output->set_layout(x.layout());
output->share_lod(x);
}

void IndexAddTensorGradInferMeta(const MetaTensor& out_grad,
const IntArray& index_arr,
const Scalar& axis_scalar,
MetaTensor* x_grad,
MetaTensor* add_tensor_grad) {
auto do_dims = out_grad.dims();
if (x_grad) {
x_grad->set_dims(do_dims);
x_grad->set_dtype(out_grad.dtype());
x_grad->set_layout(out_grad.layout());
x_grad->share_lod(out_grad);
}

if (add_tensor_grad) {
add_tensor_grad->set_dims(phi::make_ddim({1}));
add_tensor_grad->set_dtype(out_grad.dtype());
}
}


void IndexSampleInferMeta(const MetaTensor& x,
const MetaTensor& y,
MetaTensor* out,
Expand Down Expand Up @@ -1534,7 +1590,7 @@ void MvInferMeta(const MetaTensor& x, const MetaTensor& vec, MetaTensor* out) {
phi::errors::InvalidArgument(
"X's second dimension is expected to be equal to "
"Vec's first dimension"
"but received X'shape = [%s], Vec's shape = [%s]",
"but recieved X'shape = [%s], Vec's shape = [%s]",
dim_x,
dim_vec));

Expand Down
13 changes: 13 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License. */

#pragma once

#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/meta_tensor.h"

Expand Down Expand Up @@ -184,6 +185,18 @@ void HuberLossInferMeta(const MetaTensor& input_meta,
MetaTensor* residual,
MetaConfig config = MetaConfig());

void IndexAddTensorInferMeta(const MetaTensor& x,
const MetaTensor& add_tensor,
const IntArray& index_arr,
const Scalar& axis_scalar,
MetaTensor* output);

void IndexAddTensorGradInferMeta(const MetaTensor& out_grad,
const IntArray& index_arr,
const Scalar& axis_scalar,
MetaTensor* x_grad,
MetaTensor* add_tensor_grad);

void IndexSampleInferMeta(const MetaTensor& x,
const MetaTensor& y,
MetaTensor* out,
Expand Down
Loading