From 7353e9e9120e84437e753d12245a42ae5ef4d455 Mon Sep 17 00:00:00 2001 From: Yiqun Liu Date: Thu, 29 Jun 2023 21:45:19 +0800 Subject: [PATCH] Fix compiling on XPU related to MPTypeTrait. (#54924) * Fix compiling on XPU related to MPTypeTrait. * Unify the use of MPTypeTrait. * Fix compiling error. --- paddle/fluid/operators/activation_op.cu.h | 6 +-- paddle/fluid/operators/amp/fp16_type_traits.h | 44 ------------------- .../operators/fused/fused_dropout_common.h | 2 +- .../fused/fused_residual_dropout_bias.h | 7 +-- .../gaussian_random_batch_size_like_op.cu | 5 ++- paddle/fluid/operators/index_impl.cu.h | 4 +- .../operators/optimizers/lars_momentum_op.cu | 4 +- paddle/fluid/operators/optimizers/sgd_op.cu | 4 +- .../operators/optimizers/sparse_momentum_op.h | 4 +- paddle/fluid/operators/reduce_ops/reduce_op.h | 2 +- .../phi/kernels/funcs/check_numerics_utils.h | 2 +- .../primitive/compute_primitives_xpu2.h | 15 +------ .../fused/fused_dropout_act_bias_test.cu | 3 +- 13 files changed, 25 insertions(+), 77 deletions(-) delete mode 100644 paddle/fluid/operators/amp/fp16_type_traits.h diff --git a/paddle/fluid/operators/activation_op.cu.h b/paddle/fluid/operators/activation_op.cu.h index 08a8d9a0896..d9b1545abce 100644 --- a/paddle/fluid/operators/activation_op.cu.h +++ b/paddle/fluid/operators/activation_op.cu.h @@ -12,10 +12,10 @@ limitations under the License. */ #pragma once #include "paddle/fluid/operators/activation_op.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h" #include "paddle/fluid/platform/bfloat16.h" #include "paddle/phi/backends/gpu/gpu_device_function.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/kernels/funcs/activation_functor.h" namespace paddle { @@ -23,7 +23,7 @@ namespace operators { template struct CudaSoftReluFunctor : public BaseActivationFunctor { - using MPType = typename details::MPTypeTrait::Type; + using MPType = typename phi::dtype::MPTypeTrait::Type; MPType one = static_cast(1.0f); float threshold; @@ -44,7 +44,7 @@ struct CudaSoftReluFunctor : public BaseActivationFunctor { template struct CudaSoftReluGradFunctor : public BaseActivationFunctor { - using MPType = typename details::MPTypeTrait::Type; + using MPType = typename phi::dtype::MPTypeTrait::Type; MPType one = static_cast(1.0f); float threshold; diff --git a/paddle/fluid/operators/amp/fp16_type_traits.h b/paddle/fluid/operators/amp/fp16_type_traits.h deleted file mode 100644 index 56aebe90788..00000000000 --- a/paddle/fluid/operators/amp/fp16_type_traits.h +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright (c) 2020 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/fluid/platform/bfloat16.h" -#include "paddle/fluid/platform/float16.h" - -namespace paddle { -namespace operators { -namespace details { - -template -class MPTypeTrait { - public: - using Type = T; -}; - -template <> -class MPTypeTrait { - public: - using Type = float; -}; - -template <> -class MPTypeTrait { - public: - using Type = float; -}; - -} // namespace details -} // namespace operators -} // namespace paddle diff --git a/paddle/fluid/operators/fused/fused_dropout_common.h b/paddle/fluid/operators/fused/fused_dropout_common.h index 06606f4a1ad..ccd09910948 100644 --- a/paddle/fluid/operators/fused/fused_dropout_common.h +++ b/paddle/fluid/operators/fused/fused_dropout_common.h @@ -19,12 +19,12 @@ limitations under the License. */ #include #include "paddle/fluid/memory/memory.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/fused/quant_dequant_kernel.h" #include "paddle/fluid/platform/device/gpu/gpu_launch_config.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/float16.h" #include "paddle/phi/backends/gpu/gpu_device_function.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/kernels/funcs/aligned_vector.h" #include "paddle/phi/kernels/funcs/functors.h" #include "paddle/phi/kernels/funcs/layer_norm_impl.cu.h" diff --git a/paddle/fluid/operators/fused/fused_residual_dropout_bias.h b/paddle/fluid/operators/fused/fused_residual_dropout_bias.h index 43c25bcaf10..ea6c771656c 100644 --- a/paddle/fluid/operators/fused/fused_residual_dropout_bias.h +++ b/paddle/fluid/operators/fused/fused_residual_dropout_bias.h @@ -15,6 +15,7 @@ limitations under the License. */ #pragma once #include "paddle/fluid/operators/fused/fused_dropout_common.h" +#include "paddle/phi/common/amp_type_traits.h" namespace paddle { namespace operators { @@ -45,8 +46,8 @@ __forceinline__ __device__ void FusedResidualDropoutBiasOneThread( OutType *dst, MaskType *mask, const bool is_test, - typename details::MPTypeTrait::Type *mean_val, - typename details::MPTypeTrait::Type *var_val, + typename phi::dtype::MPTypeTrait::Type *mean_val, + typename phi::dtype::MPTypeTrait::Type *var_val, Functor act_func, const float quant_last_in_scale = 1.0, const float *dequant_out_scale_data = nullptr, @@ -61,7 +62,7 @@ __forceinline__ __device__ void FusedResidualDropoutBiasOneThread( using StoreOutType = phi::AlignedVector; using MaskStoreT = phi::AlignedVector; - using U = typename details::MPTypeTrait::Type; + using U = typename phi::dtype::MPTypeTrait::Type; LoadInType src_vec; LoadT residual_vec; diff --git a/paddle/fluid/operators/gaussian_random_batch_size_like_op.cu b/paddle/fluid/operators/gaussian_random_batch_size_like_op.cu index 7546c3c350d..9c5244976fc 100644 --- a/paddle/fluid/operators/gaussian_random_batch_size_like_op.cu +++ b/paddle/fluid/operators/gaussian_random_batch_size_like_op.cu @@ -11,11 +11,12 @@ 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 #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" @@ -37,7 +38,7 @@ struct GaussianGenerator { __host__ __device__ T operator()(const unsigned int n) const { thrust::minstd_rand rng; rng.seed(seed_); - using MT = typename details::MPTypeTrait::Type; + using MT = typename phi::dtype::MPTypeTrait::Type; thrust::normal_distribution dist(static_cast(mean_), static_cast(std_)); unsigned int new_n = n + offset_; diff --git a/paddle/fluid/operators/index_impl.cu.h b/paddle/fluid/operators/index_impl.cu.h index 9d8f22661e7..7d2cdae8795 100644 --- a/paddle/fluid/operators/index_impl.cu.h +++ b/paddle/fluid/operators/index_impl.cu.h @@ -11,15 +11,17 @@ 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 #include #include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/core/generator.h" #include "paddle/phi/core/hostdevice.h" #include "paddle/phi/kernels/funcs/aligned_vector.h" diff --git a/paddle/fluid/operators/optimizers/lars_momentum_op.cu b/paddle/fluid/operators/optimizers/lars_momentum_op.cu index 83293c991e9..20769747a4a 100644 --- a/paddle/fluid/operators/optimizers/lars_momentum_op.cu +++ b/paddle/fluid/operators/optimizers/lars_momentum_op.cu @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/operators/optimizers/lars_momentum_op.h" #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/kernels/funcs/aligned_vector.h" #include "paddle/phi/kernels/funcs/math_cuda_utils.h" @@ -34,7 +34,7 @@ namespace paddle { namespace operators { template -using MultiPrecisionType = typename details::MPTypeTrait::Type; +using MultiPrecisionType = typename phi::dtype::MPTypeTrait::Type; __device__ __forceinline__ float Sqrt(float x) { return sqrtf(x); } __device__ __forceinline__ double Sqrt(double x) { return sqrt(x); } diff --git a/paddle/fluid/operators/optimizers/sgd_op.cu b/paddle/fluid/operators/optimizers/sgd_op.cu index ee7b0f4c7e4..f6d2435590f 100644 --- a/paddle/fluid/operators/optimizers/sgd_op.cu +++ b/paddle/fluid/operators/optimizers/sgd_op.cu @@ -14,9 +14,9 @@ limitations under the License. */ #include -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/optimizers/sgd_op.h" #include "paddle/phi/backends/gpu/gpu_primitives.h" +#include "paddle/phi/common/amp_type_traits.h" namespace paddle { namespace operators { @@ -77,7 +77,7 @@ class SGDOpKernel : public framework::OpKernel { ctx.InputNames("Param").front(), paddle::framework::ToTypeName(param_var->Type()))); - using MPDType = typename details::MPTypeTrait::Type; + using MPDType = typename phi::dtype::MPTypeTrait::Type; auto* param = ctx.Input("Param"); auto* param_out = ctx.Output("ParamOut"); diff --git a/paddle/fluid/operators/optimizers/sparse_momentum_op.h b/paddle/fluid/operators/optimizers/sparse_momentum_op.h index 38de9c43903..f1b162be466 100644 --- a/paddle/fluid/operators/optimizers/sparse_momentum_op.h +++ b/paddle/fluid/operators/optimizers/sparse_momentum_op.h @@ -21,9 +21,9 @@ #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/platform/float16.h" #include "paddle/fluid/platform/for_range.h" +#include "paddle/phi/common/amp_type_traits.h" #ifdef __NVCC__ #include "cub/cub.cuh" @@ -37,7 +37,7 @@ namespace paddle { namespace operators { template -using MultiPrecisionType = typename details::MPTypeTrait::Type; +using MultiPrecisionType = typename phi::dtype::MPTypeTrait::Type; enum class RegularizationType { kNONE = 0, diff --git a/paddle/fluid/operators/reduce_ops/reduce_op.h b/paddle/fluid/operators/reduce_ops/reduce_op.h index 40c82619db4..ab1c48a08f0 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_op.h +++ b/paddle/fluid/operators/reduce_ops/reduce_op.h @@ -832,7 +832,7 @@ class ReduceCudaGradKernel : public framework::OpKernel { pt_out_dtype = d_out->dtype(); } - using MPType = typename kps::details::MPTypeTrait::Type; + using MPType = typename phi::dtype::MPTypeTrait::Type; phi::ReduceGrad>(dev_ctx, pt_d_out.get(), pt_d_x.get(), diff --git a/paddle/phi/kernels/funcs/check_numerics_utils.h b/paddle/phi/kernels/funcs/check_numerics_utils.h index c9ca80294e4..473d7994058 100644 --- a/paddle/phi/kernels/funcs/check_numerics_utils.h +++ b/paddle/phi/kernels/funcs/check_numerics_utils.h @@ -265,7 +265,7 @@ static void CheckNumericsCpuImpl(const T* value_ptr, } else if (std::isinf(value)) { thread_num_inf[tid] += 1; } - if (value == 0) { + if (value == static_cast(0)) { thread_num_zero[tid] += 1; } } diff --git a/paddle/phi/kernels/primitive/compute_primitives_xpu2.h b/paddle/phi/kernels/primitive/compute_primitives_xpu2.h index 2fecebaf3d2..07a3ad4ed94 100644 --- a/paddle/phi/kernels/primitive/compute_primitives_xpu2.h +++ b/paddle/phi/kernels/primitive/compute_primitives_xpu2.h @@ -13,7 +13,8 @@ // limitations under the License. #pragma once -#include "paddle/phi/common/float16.h" + +#include "paddle/phi/common/amp_type_traits.h" #include "xpu/kernel/cluster_header.h" #include "xpu/kernel/debug.h" #include "xpu/kernel/math.h" @@ -27,18 +28,6 @@ namespace details { // kLocalMode: thread reduce, each thread gets an output; enum ReduceMode { kGlobalMode, kLocalMode }; -template -class MPTypeTrait { - public: - using Type = T; -}; - -template <> -class MPTypeTrait { - public: - using Type = float; -}; - static inline __device__ void sync_all() { __asm__ __volatile__( "sync_local\t\n" diff --git a/test/cpp/fluid/fused/fused_dropout_act_bias_test.cu b/test/cpp/fluid/fused/fused_dropout_act_bias_test.cu index ddcc3f513d5..29fb7701d0c 100644 --- a/test/cpp/fluid/fused/fused_dropout_act_bias_test.cu +++ b/test/cpp/fluid/fused/fused_dropout_act_bias_test.cu @@ -17,8 +17,8 @@ limitations under the License. */ #include #include -#include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/fused/fused_dropout_act_bias.h" +#include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/functors.h" #include "test/cpp/fluid/fused/fused_dropout_test.h" @@ -30,7 +30,6 @@ PD_DECLARE_KERNEL(dropout_grad, GPU, ALL_LAYOUT); namespace framework = paddle::framework; namespace platform = paddle::platform; -namespace details = paddle::operators::details; /** * @brief the unittest of fused_dropout_act_bias -- GitLab