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

Simplify size op impl #45808

Merged
merged 4 commits into from
Sep 9, 2022
Merged
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
32 changes: 0 additions & 32 deletions paddle/phi/kernels/cpu/size_kernel.cc

This file was deleted.

31 changes: 0 additions & 31 deletions paddle/phi/kernels/gpu/size_kernel.cu

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include "paddle/phi/kernels/size_kernel.h"

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"

namespace phi {

template <typename T, typename Context>
template <typename Context>
void SizeKernel(const Context& ctx,
const DenseTensor& input,
DenseTensor* out) {
auto place = ctx.GetPlace();
auto out_data = ctx.template Alloc<int64_t>(out);
auto cpu_place = phi::CPUPlace();
if (place == cpu_place) {
out_data[0] = input.numel();
} else {
DenseTensor cpu_tensor;
cpu_tensor.Resize(out->dims());
auto cpu_data = ctx.template HostAlloc<int64_t>(&cpu_tensor);
cpu_data[0] = input.numel();
phi::Copy(ctx, cpu_tensor, place, false, out);
}
auto* out_data = ctx.template HostAlloc<int64_t>(out);
out_data[0] = input.numel();
}

} // namespace phi

PD_REGISTER_GENERAL_KERNEL(
size, CPU, ALL_LAYOUT, phi::SizeKernel<phi::CPUContext>, ALL_DTYPE) {
kernel->OutputAt(0).SetDataType(phi::DataType::INT64);
}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_GENERAL_KERNEL(
size, GPU, ALL_LAYOUT, phi::SizeKernel<phi::GPUContext>, ALL_DTYPE) {
kernel->OutputAt(0)
.SetBackend(phi::Backend::CPU)
.SetDataType(phi::DataType::INT64);
}
#endif
2 changes: 1 addition & 1 deletion paddle/phi/kernels/size_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace phi {

template <typename T, typename Context>
template <typename Context>
void SizeKernel(const Context& ctx, const DenseTensor& input, DenseTensor* out);

} // namespace phi
3 changes: 3 additions & 0 deletions python/paddle/distributed/collective.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,9 @@ def all_gather_object(object_list, obj, group=None):
), "all_gather_object doesn't support static graph mode."

tensor, len_of_tensor = _convert_object_to_tensor(obj)
if paddle.get_device() != "cpu":
len_of_tensor = len_of_tensor._copy_to(
paddle.framework._current_expected_place(), False)

# gather len_of_tensor from all ranks
list_len_of_tensor = []
Expand Down