-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Prim] add reduce_as op for paddle #63064
Merged
Merged
Changes from 14 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
f9ba2e5
add the sum_as op for paddle - part(forward)
zeroRains 6ab282a
fix the test bug
zeroRains 7b2b5d6
add the sum_as_grad but have bug in test
zeroRains f9a62ad
remove uncessary args but backward computing still have bug
zeroRains b7760da
fix the python registor
zeroRains 5c8bc4c
modif the test and and some case
zeroRains a16be62
modify the description of python api
zeroRains da822f5
fix tyop
zeroRains 38bd332
fix the bug in test which write base on OpTest
zeroRains 9e7fa93
remove the useless function in test
zeroRains d8c22f2
modift the size of the test tenor
zeroRains 6101781
Update test/legacy_test/test_sum_as_op.py
cyber-pioneer 91d6d2b
Update test/legacy_test/test_sum_as_op.py
cyber-pioneer 11e96ed
fix code style
zeroRains 6f4325d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains 4636403
add dynamic shape test and modify the doc
zeroRains 89f0d8b
Update test_sum_as_op.py
zeroRains e7dd69a
Update test_sum_as_op.py
zeroRains 1339391
fix the bug in convert_np_dtype_to_dtype_
zeroRains 9c06455
Merge branch 'sum' of https://github.com/zeroRains/Paddle into sum
zeroRains 76ff0e4
Update core.py
zeroRains 05ad7c1
change the variable name
zeroRains ee8cb10
Merge branch 'sum' of https://github.com/zeroRains/Paddle into sum
zeroRains 19a39ef
remove spaces
zeroRains b9b07cc
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains 437a6cd
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains 7141a70
add an assert for get_reduce_dims
zeroRains 009cf48
fix the bug
zeroRains 9fc3081
Update common_shape.h
zeroRains 21157bc
modife sum_as to reduce_as
zeroRains c7ffb0a
Merge branch 'sum' of https://github.com/zeroRains/Paddle into sum
zeroRains c96d295
fix the file name
zeroRains 287d4f1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains d5d3d7b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains 2d83a1b
Merge commit 'refs/pull/63064/head' of https://github.com/PaddlePaddl…
cyber-pioneer c6f7e55
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zeroRains 7f2168e
modify the test time
zeroRains 702390a
Merge commit 'refs/pull/63064/head' of https://github.com/PaddlePaddl…
cyber-pioneer 1ce4a5b
fix test case
cyber-pioneer d8ac719
fix the date
zeroRains 754482f
Merge branch 'develop' into sum
zeroRains 0dfe7fa
fix code style
zeroRains File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
zeroRains marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Copyright (c) 2024 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/sum_as_kernel.h" | ||
|
||
#include "paddle/phi/core/device_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/impl/reduce_grad.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsGradKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
const DenseTensor& out_grad, | ||
DenseTensor* x_grad) { | ||
auto reduce_dim = phi::funcs::GetReduceDims(x, y); | ||
bool reduce_all = recompute_reduce_all(x, reduce_dim); | ||
ReduceGradKernel<Context, T, funcs::SumGradFunctor, true>(dev_ctx, | ||
x, | ||
paddle::none, | ||
out_grad, | ||
reduce_dim, | ||
false, | ||
reduce_all, | ||
x_grad); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(sum_as_grad, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::SumAsGradKernel, | ||
bool, | ||
float, | ||
double, | ||
phi::dtype::float16, | ||
phi::dtype::bfloat16, | ||
int16_t, | ||
int, | ||
int64_t, | ||
uint8_t, | ||
int8_t) { | ||
kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
zeroRains marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Copyright (c) 2024 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/sum_as_kernel.h" | ||
|
||
#include "paddle/phi/core/device_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/cpu/reduce.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
DenseTensor* out) { | ||
auto reduce_dim = phi::funcs::GetReduceDims(x, y); | ||
bool reduce_all = recompute_reduce_all(x, reduce_dim); | ||
phi::Reduce<CPUContext, T, phi::funcs::SumFunctor>( | ||
dev_ctx, x, reduce_all, reduce_dim, false, out->type(), out); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(sum_as, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::SumAsKernel, | ||
bool, | ||
float, | ||
double, | ||
phi::dtype::float16, | ||
phi::dtype::bfloat16, | ||
int16_t, | ||
int, | ||
int64_t, | ||
uint8_t, | ||
int8_t) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
// Copyright (c) 2024 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/sum_as_grad_kernel.h" | ||
|
||
#include "paddle/phi/backends/gpu/gpu_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/funcs/reduce_function.h" | ||
#include "paddle/phi/kernels/gpu/reduce_grad.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsGradKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
const DenseTensor& out_grad, | ||
DenseTensor* x_grad) { | ||
auto reduce_dim = phi::funcs::GetReduceDims(x, y); | ||
bool reduce_all = recompute_reduce_all(x, reduce_dim); | ||
auto update_dims = common::vectorize(x.dims()); | ||
for (auto i : reduce_dim) { | ||
update_dims[i] = 1; | ||
} | ||
|
||
DenseTensor new_out_grad(out_grad.type()); | ||
new_out_grad.ShareDataWith(out_grad); | ||
new_out_grad.Resize(common::make_ddim(update_dims)); | ||
|
||
dev_ctx.Alloc(x_grad, x.dtype()); | ||
using MPType = typename phi::dtype::MPTypeTrait<T>::Type; | ||
phi::ReduceGrad<phi::kps::IdentityFunctor<T, MPType>>( | ||
dev_ctx, | ||
&new_out_grad, | ||
x_grad, | ||
out_grad.dtype(), | ||
phi::kps::IdentityFunctor<T, MPType>()); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(sum_as_grad, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::SumAsGradKernel, | ||
bool, | ||
float, | ||
double, | ||
phi::dtype::float16, | ||
phi::dtype::bfloat16, | ||
int16_t, | ||
int, | ||
int64_t, | ||
uint8_t, | ||
int8_t) { | ||
kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
// Copyright (c) 2024 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/sum_as_kernel.h" | ||
|
||
#include "paddle/phi/backends/gpu/gpu_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/reduce_sum_kernel.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
DenseTensor* out) { | ||
auto reduce_dim = phi::funcs::GetReduceDims(x, y); | ||
dev_ctx.template Alloc<T>(out); | ||
phi::SumKernel<T, Context>(dev_ctx, x, reduce_dim, out->type(), false, out); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(sum_as, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::SumAsKernel, | ||
bool, | ||
float, | ||
double, | ||
phi::dtype::float16, | ||
phi::dtype::bfloat16, | ||
int16_t, | ||
int, | ||
int64_t, | ||
uint8_t, | ||
int8_t) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) 2024 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/core/device_context.h" | ||
#include "paddle/phi/kernels/funcs/common_shape.h" | ||
#include "paddle/phi/kernels/funcs/reduce_functor.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsGradKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
const DenseTensor& out_grad, | ||
DenseTensor* x_grad); | ||
|
||
} // namespace phi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2024 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/core/device_context.h" | ||
#include "paddle/phi/kernels/funcs/common_shape.h" | ||
#include "paddle/phi/kernels/funcs/reduce_functor.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void SumAsKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
const DenseTensor& y, | ||
DenseTensor* out); | ||
|
||
} // namespace phi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
输入参数 y 这里的作用是提供output的维度信息,但
y
的命名表示不出来这一层含义,是否可以换成target
之类更准确的变量名?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done