From ee0ac837ef4bdf91078dc0f580b56db855591a8b Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Tue, 8 Nov 2022 21:47:32 +0800 Subject: [PATCH 1/3] remove framework/data_type.h from phi --- paddle/phi/kernels/cpu/arg_min_max_kernel.cc | 10 +++++----- paddle/phi/kernels/cpu/cumprod_grad_kernel.cc | 4 ++-- paddle/phi/kernels/cpu/unique_consecutive_kernel.cc | 10 +++++----- paddle/phi/kernels/funcs/math_function.cc | 2 +- paddle/phi/kernels/funcs/math_function.cu | 2 +- paddle/phi/kernels/funcs/math_function_impl.h | 2 +- paddle/phi/kernels/gpu/arg_min_max_kernel.cu | 11 +++++------ paddle/phi/kernels/gpu/cumprod_grad_kernel.cu | 4 ++-- paddle/phi/kernels/gpu/unique_consecutive_kernel.cu | 10 +++++----- paddle/phi/kernels/impl/isclose_kernel_impl.h | 1 - 10 files changed, 27 insertions(+), 29 deletions(-) diff --git a/paddle/phi/kernels/cpu/arg_min_max_kernel.cc b/paddle/phi/kernels/cpu/arg_min_max_kernel.cc index 13e401b59d643..01a99de7480df 100644 --- a/paddle/phi/kernels/cpu/arg_min_max_kernel.cc +++ b/paddle/phi/kernels/cpu/arg_min_max_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/utils/data_type.h" #include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/math_function.h" @@ -141,15 +142,14 @@ void ArgMinMaxKernel(const Context& dev_ctx, int dtype, DenseTensor* out) { if (dtype < 0) { - paddle::framework::VisitDataTypeTiny( - static_cast( - paddle::framework::proto::VarType::INT64), + phi::VisitDataTypeTiny( + phi::DataType::INT64, VisitDataArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); return; } - paddle::framework::VisitDataTypeTiny( - static_cast(dtype), + phi::VisitDataTypeTiny( + static_cast(dtype), VisitDataArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); } diff --git a/paddle/phi/kernels/cpu/cumprod_grad_kernel.cc b/paddle/phi/kernels/cpu/cumprod_grad_kernel.cc index a25f9650fc50f..2777622612a81 100644 --- a/paddle/phi/kernels/cpu/cumprod_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/cumprod_grad_kernel.cc @@ -23,7 +23,7 @@ #include "paddle/phi/kernels/funcs/for_range.h" // NOTE(@xiongkun): use of IsComplex<> -#include "paddle/fluid/framework/data_type.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { template @@ -51,7 +51,7 @@ void CumprodGradKernel(const Context& dev_ctx, const T* out_data_deal; Allocator::AllocationPtr x_conj; Allocator::AllocationPtr out_conj; - if (paddle::framework::IsComplex::value) { + if (phi::IsComplexType(x.dtype())) { x_conj = const_cast(dev_ctx.GetAllocator()) .Allocate(numel * sizeof(T)); auto* x_data_conj = reinterpret_cast(x_conj->ptr()); diff --git a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc index 86fe53b72c985..264f201132843 100644 --- a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc +++ b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc @@ -19,7 +19,7 @@ #include "paddle/phi/core/errors.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/fluid/framework/data_type.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -33,8 +33,8 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, DenseTensor* out, DenseTensor* index, DenseTensor* counts) { - auto data_type = static_cast(dtype); - if (data_type == paddle::framework::proto::VarType::INT32) { + auto data_type = static_cast(dtype); + if (data_type == phi::DataType::INT32) { PADDLE_ENFORCE_LE( x.numel(), INT_MAX, @@ -46,13 +46,13 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, } if (axis.empty()) { - paddle::framework::VisitDataTypeTiny( + phi::VisitDataTypeTiny( data_type, UniqueConsecutiveFlattenedTensorFunctor( dev_ctx, x, out, return_inverse, return_counts, index, counts)); } else { int valid_axis = axis[0]; - paddle::framework::VisitDataTypeTiny( + phi::VisitDataTypeTiny( data_type, UniqueConsecutiveDimFunctor(dev_ctx, x, diff --git a/paddle/phi/kernels/funcs/math_function.cc b/paddle/phi/kernels/funcs/math_function.cc index 7cad886654852..8b6fd117e9651 100644 --- a/paddle/phi/kernels/funcs/math_function.cc +++ b/paddle/phi/kernels/funcs/math_function.cc @@ -26,10 +26,10 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/platform/bfloat16.h" #include "paddle/fluid/platform/float16.h" #include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/common/data_type.h" #include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/math_function_impl.h" #include "unsupported/Eigen/CXX11/Tensor" diff --git a/paddle/phi/kernels/funcs/math_function.cu b/paddle/phi/kernels/funcs/math_function.cu index ea2b23ca813d3..7e7a6c30c34fc 100644 --- a/paddle/phi/kernels/funcs/math_function.cu +++ b/paddle/phi/kernels/funcs/math_function.cu @@ -14,12 +14,12 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/platform/bfloat16.h" #include "paddle/fluid/platform/float16.h" #include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/common/data_type.h" #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function_impl.h" diff --git a/paddle/phi/kernels/funcs/math_function_impl.h b/paddle/phi/kernels/funcs/math_function_impl.h index 1ab9455215e9a..b59a249bbbf04 100644 --- a/paddle/phi/kernels/funcs/math_function_impl.h +++ b/paddle/phi/kernels/funcs/math_function_impl.h @@ -16,7 +16,7 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/data_type.h" +#include "paddle/phi/common/data_type.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace phi { diff --git a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu index 13db18534955b..f0400e258b229 100644 --- a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu +++ b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu @@ -28,8 +28,8 @@ namespace cub = hipcub; #endif #include -#include "paddle/fluid/framework/data_type.h" #include "paddle/phi/core/ddim.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -209,15 +209,14 @@ void ArgMinMaxOpCUDAKernel(const Context& dev_ctx, int dtype, DenseTensor* out) { if (dtype < 0) { - paddle::framework::VisitDataTypeTiny( - static_cast( - paddle::framework::proto::VarType::INT64), + phi::VisitDataTypeTiny( + phi::DataType::INT64, VisitDataCudaArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); return; } - paddle::framework::VisitDataTypeTiny( - static_cast(dtype), + phi::VisitDataTypeTiny( + static_cast(dtype), VisitDataCudaArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); } diff --git a/paddle/phi/kernels/gpu/cumprod_grad_kernel.cu b/paddle/phi/kernels/gpu/cumprod_grad_kernel.cu index aee1aaa9555b6..d8e375825cdab 100644 --- a/paddle/phi/kernels/gpu/cumprod_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/cumprod_grad_kernel.cu @@ -24,7 +24,7 @@ #include "paddle/phi/kernels/funcs/elementwise_functor.h" #include "paddle/phi/kernels/funcs/for_range.h" // NOTE(@xiongkun): use of IsComplex<> -#include "paddle/fluid/framework/data_type.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -152,7 +152,7 @@ void CumprodGradKernel(const Context &dev_ctx, const T *y_data_deal; Allocator::AllocationPtr x_conj; Allocator::AllocationPtr y_conj; - if (paddle::framework::IsComplex::value) { + if (phi::IsComplexType(x.dtype())) { x_conj = const_cast(dev_ctx.GetAllocator()) .Allocate(numel * sizeof(T)); auto *x_data_conj = reinterpret_cast(x_conj->ptr()); diff --git a/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu b/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu index 4ce91a0dd66b4..852e14b06c7a3 100644 --- a/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu +++ b/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu @@ -21,7 +21,7 @@ #include "paddle/phi/core/errors.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/fluid/framework/data_type.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -35,8 +35,8 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, DenseTensor* out, DenseTensor* index, DenseTensor* counts) { - auto data_type = static_cast(dtype); - if (data_type == paddle::framework::proto::VarType::INT32) { + auto data_type = static_cast(dtype); + if (data_type == phi::DataType::INT32) { PADDLE_ENFORCE_LE( x.numel() + 1, INT_MAX, @@ -49,14 +49,14 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, // if 'axis' is not required, flatten the Tensor. if (axis.empty()) { - paddle::framework::VisitDataTypeTiny( + phi::VisitDataTypeTiny( data_type, UniqueConsecutiveFlattenedCUDAFunctor( dev_ctx, x, out, return_inverse, return_counts, index, counts)); } else { // 'axis' is required. int valid_axis = axis[0]; - paddle::framework::VisitDataTypeTiny( + phi::VisitDataTypeTiny( data_type, UniqueConsecutiveDimsCUDAFunctor(dev_ctx, x, diff --git a/paddle/phi/kernels/impl/isclose_kernel_impl.h b/paddle/phi/kernels/impl/isclose_kernel_impl.h index 25247ceaff6c0..cf7171656486c 100644 --- a/paddle/phi/kernels/impl/isclose_kernel_impl.h +++ b/paddle/phi/kernels/impl/isclose_kernel_impl.h @@ -16,7 +16,6 @@ #include #include -#include "paddle/fluid/framework/data_type.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/data_type.h" From 0319598ba6a31b47b899bb83ff2c372bf562c6ed Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Wed, 9 Nov 2022 10:08:46 +0800 Subject: [PATCH 2/3] fix CI fail: map proto::VarType to phi::DataType --- paddle/phi/core/utils/data_type.h | 11 +++++++++++ paddle/phi/kernels/arg_min_max_kernel.h | 1 + paddle/phi/kernels/cpu/arg_min_max_kernel.cc | 2 +- paddle/phi/kernels/cpu/unique_consecutive_kernel.cc | 4 +--- paddle/phi/kernels/gpu/arg_min_max_kernel.cu | 3 +-- paddle/phi/kernels/gpu/unique_consecutive_kernel.cu | 4 +--- paddle/phi/kernels/unique_consecutive_kernel.h | 1 + 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/paddle/phi/core/utils/data_type.h b/paddle/phi/core/utils/data_type.h index 9877149dc52bd..5f1864d3593d2 100644 --- a/paddle/phi/core/utils/data_type.h +++ b/paddle/phi/core/utils/data_type.h @@ -14,6 +14,7 @@ limitations under the License. */ #pragma once #include +#include #include #include @@ -23,6 +24,16 @@ limitations under the License. */ namespace phi { +// Here we can't depend on the fluid proto::VarType, so we use the dtype enum +// value directly. See also `assign_value_sig.cc`. +static std::map var_type_map{{1, phi::DataType::INT16}, + {2, phi::DataType::INT32}, + {3, phi::DataType::INT64}, + {4, phi::DataType::FLOAT16}, + {5, phi::DataType::FLOAT32}, + {6, phi::DataType::FLOAT64}, + {20, phi::DataType::UINT8}}; + #define _PhiForEachDataTypeHelper_(callback, cpp_type, data_type) \ callback(cpp_type, data_type); diff --git a/paddle/phi/kernels/arg_min_max_kernel.h b/paddle/phi/kernels/arg_min_max_kernel.h index 258c8f21e0540..1321de9095173 100644 --- a/paddle/phi/kernels/arg_min_max_kernel.h +++ b/paddle/phi/kernels/arg_min_max_kernel.h @@ -16,6 +16,7 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { diff --git a/paddle/phi/kernels/cpu/arg_min_max_kernel.cc b/paddle/phi/kernels/cpu/arg_min_max_kernel.cc index 01a99de7480df..999cb16620d26 100644 --- a/paddle/phi/kernels/cpu/arg_min_max_kernel.cc +++ b/paddle/phi/kernels/cpu/arg_min_max_kernel.cc @@ -149,7 +149,7 @@ void ArgMinMaxKernel(const Context& dev_ctx, return; } phi::VisitDataTypeTiny( - static_cast(dtype), + var_type_map[dtype], VisitDataArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); } diff --git a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc index 264f201132843..43aa2df4dcc19 100644 --- a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc +++ b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc @@ -19,8 +19,6 @@ #include "paddle/phi/core/errors.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/core/utils/data_type.h" - namespace phi { template @@ -33,7 +31,7 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, DenseTensor* out, DenseTensor* index, DenseTensor* counts) { - auto data_type = static_cast(dtype); + auto data_type = var_type_map[dtype]; if (data_type == phi::DataType::INT32) { PADDLE_ENFORCE_LE( x.numel(), diff --git a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu index f0400e258b229..97f0417392c24 100644 --- a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu +++ b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu @@ -29,7 +29,6 @@ namespace cub = hipcub; #include #include "paddle/phi/core/ddim.h" -#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -216,7 +215,7 @@ void ArgMinMaxOpCUDAKernel(const Context& dev_ctx, return; } phi::VisitDataTypeTiny( - static_cast(dtype), + var_type_map[dtype], VisitDataCudaArgMinMaxFunctor( dev_ctx, x, axis.to(), keepdims, flatten, out)); } diff --git a/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu b/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu index 852e14b06c7a3..9eb9309bb496f 100644 --- a/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu +++ b/paddle/phi/kernels/gpu/unique_consecutive_kernel.cu @@ -21,8 +21,6 @@ #include "paddle/phi/core/errors.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/core/utils/data_type.h" - namespace phi { template @@ -35,7 +33,7 @@ void UniqueConsecutiveKernel(const Context& dev_ctx, DenseTensor* out, DenseTensor* index, DenseTensor* counts) { - auto data_type = static_cast(dtype); + auto data_type = var_type_map[dtype]; if (data_type == phi::DataType::INT32) { PADDLE_ENFORCE_LE( x.numel() + 1, diff --git a/paddle/phi/kernels/unique_consecutive_kernel.h b/paddle/phi/kernels/unique_consecutive_kernel.h index ade35d4d49730..2d68b9fac5284 100644 --- a/paddle/phi/kernels/unique_consecutive_kernel.h +++ b/paddle/phi/kernels/unique_consecutive_kernel.h @@ -17,6 +17,7 @@ #include #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { From a0b60bb461e290fa73778bdc67c360f2b243d348 Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Wed, 9 Nov 2022 12:29:55 +0800 Subject: [PATCH 3/3] refactor code to add more detailed comments --- paddle/phi/core/utils/data_type.h | 7 +++++++ paddle/phi/kernels/arg_min_max_kernel.h | 1 - paddle/phi/kernels/cpu/unique_consecutive_kernel.cc | 1 + paddle/phi/kernels/gpu/arg_min_max_kernel.cu | 2 +- paddle/phi/kernels/unique_consecutive_kernel.h | 1 - paddle/phi/kernels/xpu/arg_min_max_kernel.cc | 1 + 6 files changed, 10 insertions(+), 3 deletions(-) diff --git a/paddle/phi/core/utils/data_type.h b/paddle/phi/core/utils/data_type.h index 5f1864d3593d2..5e53d8c95b0b9 100644 --- a/paddle/phi/core/utils/data_type.h +++ b/paddle/phi/core/utils/data_type.h @@ -26,6 +26,13 @@ namespace phi { // Here we can't depend on the fluid proto::VarType, so we use the dtype enum // value directly. See also `assign_value_sig.cc`. +// proto::VarType::INT16 -> 1 -> phi::DataType::INT16 +// proto::VarType::INT32 -> 2 -> phi::DataType::INT32 +// proto::VarType::INT64 -> 3 -> phi::DataType::INT64 +// proto::VarType::FP16 -> 4 -> phi::DataType::FLOAT16 +// proto::VarType::FP32 -> 5 -> phi::DataType::FLOAT32 +// proto::VarType::FP64 -> 6 -> phi::DataType::FLOAT64 +// proto::VarType::UINT8 -> 20 -> phi::DataType::UINT8 static std::map var_type_map{{1, phi::DataType::INT16}, {2, phi::DataType::INT32}, {3, phi::DataType::INT64}, diff --git a/paddle/phi/kernels/arg_min_max_kernel.h b/paddle/phi/kernels/arg_min_max_kernel.h index 1321de9095173..258c8f21e0540 100644 --- a/paddle/phi/kernels/arg_min_max_kernel.h +++ b/paddle/phi/kernels/arg_min_max_kernel.h @@ -16,7 +16,6 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/utils/data_type.h" namespace phi { diff --git a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc index 43aa2df4dcc19..07df5f1f566a4 100644 --- a/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc +++ b/paddle/phi/kernels/cpu/unique_consecutive_kernel.cc @@ -18,6 +18,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/errors.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { diff --git a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu index 97f0417392c24..0cbf206bb3689 100644 --- a/paddle/phi/kernels/gpu/arg_min_max_kernel.cu +++ b/paddle/phi/kernels/gpu/arg_min_max_kernel.cu @@ -29,7 +29,7 @@ namespace cub = hipcub; #include #include "paddle/phi/core/ddim.h" - +#include "paddle/phi/core/utils/data_type.h" namespace phi { namespace { // NOLINT diff --git a/paddle/phi/kernels/unique_consecutive_kernel.h b/paddle/phi/kernels/unique_consecutive_kernel.h index 2d68b9fac5284..ade35d4d49730 100644 --- a/paddle/phi/kernels/unique_consecutive_kernel.h +++ b/paddle/phi/kernels/unique_consecutive_kernel.h @@ -17,7 +17,6 @@ #include #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/utils/data_type.h" namespace phi { diff --git a/paddle/phi/kernels/xpu/arg_min_max_kernel.cc b/paddle/phi/kernels/xpu/arg_min_max_kernel.cc index a48e2155a251a..b3a73931723ac 100644 --- a/paddle/phi/kernels/xpu/arg_min_max_kernel.cc +++ b/paddle/phi/kernels/xpu/arg_min_max_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/xpu/xpu_context.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi {