From c10dcff12de2e42408159f66c2a2350379369064 Mon Sep 17 00:00:00 2001 From: GaoWei8 <53294385+GaoWei8@users.noreply.github.com> Date: Wed, 15 Jul 2020 11:56:53 +0800 Subject: [PATCH] refine PADDLE_ENFORCE (#25456) * Refine PADDLE_ENFORCE in paddle/fluid/platform test=develop --- paddle/fluid/platform/collective_helper.h | 6 +-- paddle/fluid/platform/cpu_helper.cc | 4 +- paddle/fluid/platform/cuda_resource_pool.cc | 8 ++-- paddle/fluid/platform/cudnn_helper.h | 4 +- paddle/fluid/platform/device_code.cc | 8 ++-- paddle/fluid/platform/device_context.cc | 10 ++--- paddle/fluid/platform/dynload/tensorrt.h | 34 +++++++++-------- paddle/fluid/platform/gpu_info.cc | 4 +- .../fluid/platform/gpu_launch_param_config.h | 2 +- paddle/fluid/platform/init.cc | 20 ++++++---- paddle/fluid/platform/mkldnn_reuse.h | 38 +++++++++++-------- paddle/fluid/platform/nccl_helper.h | 10 +++-- paddle/fluid/platform/profiler.cc | 7 ++-- paddle/fluid/platform/profiler_helper.h | 2 +- paddle/fluid/platform/resource_pool.h | 2 +- 15 files changed, 87 insertions(+), 72 deletions(-) diff --git a/paddle/fluid/platform/collective_helper.h b/paddle/fluid/platform/collective_helper.h index f632550c65..cc19fd5ac4 100644 --- a/paddle/fluid/platform/collective_helper.h +++ b/paddle/fluid/platform/collective_helper.h @@ -81,7 +81,7 @@ class NCCLCommContext { PADDLE_ENFORCE_GT( comm_map_.count(ring_id), 0, platform::errors::InvalidArgument( - "Comunicator in ring id %d has not been initialized.", ring_id)); + "Communicator in ring id %d has not been initialized.", ring_id)); PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(), 1, platform::errors::InvalidArgument( "One device id should be specified to retrieve from " @@ -94,11 +94,11 @@ class NCCLCommContext { PADDLE_ENFORCE_GT( comm_map_.count(ring_id), 0, platform::errors::InvalidArgument( - "Comunicator of ring id %d has not been initialized.", ring_id)); + "Communicator of ring id %d has not been initialized.", ring_id)); PADDLE_ENFORCE_GT( comm_map_.at(ring_id).count(dev_id), 0, platform::errors::InvalidArgument( - "Comunicator at device id %d has not been initialized in ring %d.", + "Communicator at device id %d has not been initialized in ring %d.", dev_id, ring_id)); return comm_map_.at(ring_id).at(dev_id).get(); } diff --git a/paddle/fluid/platform/cpu_helper.cc b/paddle/fluid/platform/cpu_helper.cc index dbc4e813d6..a402f39734 100644 --- a/paddle/fluid/platform/cpu_helper.cc +++ b/paddle/fluid/platform/cpu_helper.cc @@ -44,8 +44,8 @@ void SetNumThreads(int num_threads) { omp_set_num_threads(real_num_threads); #else PADDLE_THROW(platform::errors::Unimplemented( - "The library (except OPENBLAS, MKLML) is to be implemented, thus " - "number of threads can not be set.")); + "This library (except OPENBLAS, MKLML) is not supported yet, so the" + "number of threads cannot be set.")); #endif } diff --git a/paddle/fluid/platform/cuda_resource_pool.cc b/paddle/fluid/platform/cuda_resource_pool.cc index 65c8b96028..6ecb312d72 100644 --- a/paddle/fluid/platform/cuda_resource_pool.cc +++ b/paddle/fluid/platform/cuda_resource_pool.cc @@ -50,11 +50,11 @@ std::shared_ptr CudaStreamResourcePool::New(int dev_idx) { PADDLE_ENFORCE_GE( dev_idx, 0, platform::errors::InvalidArgument( - "dev_idx should be not less than 0, but got %d", dev_idx)); + "The dev_idx should be not less than 0, but got %d.", dev_idx)); PADDLE_ENFORCE_LT( dev_idx, pool_.size(), platform::errors::OutOfRange( - "dev_idx should be less than device count %d, but got %d", + "The dev_idx should be less than device count %d, but got %d.", pool_.size(), dev_idx)); return pool_[dev_idx]->New(); } @@ -89,11 +89,11 @@ std::shared_ptr CudaEventResourcePool::New(int dev_idx) { PADDLE_ENFORCE_GE( dev_idx, 0, platform::errors::InvalidArgument( - "dev_idx should be not less than 0, but got %d", dev_idx)); + "The dev_idx should be not less than 0, but got %d.", dev_idx)); PADDLE_ENFORCE_LT( dev_idx, pool_.size(), platform::errors::OutOfRange( - "dev_idx should be less than device count %d, but got %d", + "The dev_idx should be less than device count %d, but got %d.", pool_.size(), dev_idx)); return pool_[dev_idx]->New(); } diff --git a/paddle/fluid/platform/cudnn_helper.h b/paddle/fluid/platform/cudnn_helper.h index b1da9862aa..efb57e12fd 100644 --- a/paddle/fluid/platform/cudnn_helper.h +++ b/paddle/fluid/platform/cudnn_helper.h @@ -142,8 +142,8 @@ inline ActivationMode StringToActivationMode(const std::string& str) { } else if (str == "bandpass") { return ActivationMode::kBandPass; } else { - PADDLE_THROW( - platform::errors::Unimplemented("Unknown activation string: %s.", str)); + PADDLE_THROW(platform::errors::Unimplemented( + "Unknown CUDNN activation string: %s.", str)); } } diff --git a/paddle/fluid/platform/device_code.cc b/paddle/fluid/platform/device_code.cc index e8b2d5d4ed..9d5a0954b0 100644 --- a/paddle/fluid/platform/device_code.cc +++ b/paddle/fluid/platform/device_code.cc @@ -60,10 +60,10 @@ platform::DeviceCode* DeviceCodePool::Get(const platform::Place& place, } DeviceCodePool::DeviceCodePool(const std::vector& places) { - PADDLE_ENFORCE_GT( - places.size(), 0, - errors::InvalidArgument( - "Expected the number of places >= 1. Expected %d.", places.size())); + PADDLE_ENFORCE_GT(places.size(), 0, + errors::InvalidArgument( + "Expected the number of places >= 1. But received %d.", + places.size())); // Remove the duplicated places std::set set; for (auto& p : places) { diff --git a/paddle/fluid/platform/device_context.cc b/paddle/fluid/platform/device_context.cc index 72733f5153..38b0894c3f 100644 --- a/paddle/fluid/platform/device_context.cc +++ b/paddle/fluid/platform/device_context.cc @@ -103,9 +103,9 @@ DeviceContextPool::DeviceContextPool( #ifdef PADDLE_WITH_CUDA EmplaceDeviceContext(&device_contexts_, p); #else - PADDLE_THROW(platform::errors::Unimplemented( - "'CUDAPlace is not supported. Please re-compile with WITH_GPU." - "option")); + PADDLE_THROW( + platform::errors::Unimplemented("CUDAPlace is not supported. Please " + "re-compile with WITH_GPU option.")); #endif } else if (platform::is_cuda_pinned_place(p)) { #ifdef PADDLE_WITH_CUDA @@ -113,8 +113,8 @@ DeviceContextPool::DeviceContextPool( &device_contexts_, p); #else PADDLE_THROW(platform::errors::Unimplemented( - "'CUDAPlace' is not supported. Please re-compile with WITH_GPU." - "option")); + "CUDAPlace is not supported. Please re-compile with WITH_GPU " + "option.")); #endif } } diff --git a/paddle/fluid/platform/dynload/tensorrt.h b/paddle/fluid/platform/dynload/tensorrt.h index 35fa9e88b4..566f887014 100644 --- a/paddle/fluid/platform/dynload/tensorrt.h +++ b/paddle/fluid/platform/dynload/tensorrt.h @@ -30,21 +30,25 @@ namespace dynload { extern std::once_flag tensorrt_dso_flag; extern void* tensorrt_dso_handle; -#define DECLARE_DYNAMIC_LOAD_TENSORRT_WRAP(__name) \ - struct DynLoad__##__name { \ - template \ - auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ - using tensorrt_func = decltype(&::__name); \ - std::call_once(tensorrt_dso_flag, []() { \ - tensorrt_dso_handle = \ - paddle::platform::dynload::GetTensorRtDsoHandle(); \ - PADDLE_ENFORCE(tensorrt_dso_handle, "load tensorrt so failed"); \ - }); \ - static void* p_##__name = dlsym(tensorrt_dso_handle, #__name); \ - PADDLE_ENFORCE(p_##__name, "load %s failed", #__name); \ - return reinterpret_cast(p_##__name)(args...); \ - } \ - }; \ +#define DECLARE_DYNAMIC_LOAD_TENSORRT_WRAP(__name) \ + struct DynLoad__##__name { \ + template \ + auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ + using tensorrt_func = decltype(&::__name); \ + std::call_once(tensorrt_dso_flag, []() { \ + tensorrt_dso_handle = \ + paddle::platform::dynload::GetTensorRtDsoHandle(); \ + PADDLE_ENFORCE_NOT_NULL(tensorrt_dso_handle, \ + platform::errors::Unavailable( \ + "Load tensorrt %s failed", #__name)); \ + }); \ + static void* p_##__name = dlsym(tensorrt_dso_handle, #__name); \ + PADDLE_ENFORCE_NOT_NULL( \ + p_##__name, \ + platform::errors::Unavailable("Load tensorrt %s failed", #__name)); \ + return reinterpret_cast(p_##__name)(args...); \ + } \ + }; \ extern DynLoad__##__name __name #define TENSORRT_RAND_ROUTINE_EACH(__macro) \ diff --git a/paddle/fluid/platform/gpu_info.cc b/paddle/fluid/platform/gpu_info.cc index 2fddc23b43..5f63233d8b 100644 --- a/paddle/fluid/platform/gpu_info.cc +++ b/paddle/fluid/platform/gpu_info.cc @@ -344,10 +344,10 @@ class RecordedCudaMallocHelper { PADDLE_ENFORCE_GE( dev_id, 0, platform::errors::OutOfRange( - "Device id must be not less than 0, but got %d", dev_id)); + "Device id must be not less than 0, but got %d.", dev_id)); PADDLE_ENFORCE_LT( dev_id, instances_.size(), - platform::errors::OutOfRange("Device id %d exceeds gpu card number %d", + platform::errors::OutOfRange("Device id %d exceeds gpu card number %d.", dev_id, instances_.size())); return instances_[dev_id].get(); } diff --git a/paddle/fluid/platform/gpu_launch_param_config.h b/paddle/fluid/platform/gpu_launch_param_config.h index c1ea063360..40f4ef975e 100755 --- a/paddle/fluid/platform/gpu_launch_param_config.h +++ b/paddle/fluid/platform/gpu_launch_param_config.h @@ -39,7 +39,7 @@ inline GpuLaunchParamConfig GetGpuLaunchConfig1D( const platform::CUDADeviceContext& context, int element_count) { PADDLE_ENFORCE_GT(element_count, 0, platform::errors::InvalidArgument( "element count should greater than 0," - " but received value is:%d", + " but received value is %d.", element_count)); const int theory_thread_count = element_count; diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 9753a39c40..d9c8026bd2 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -117,14 +117,18 @@ void InitCupti() { #ifdef PADDLE_WITH_CUPTI if (FLAGS_multiple_of_cupti_buffer_size == 1) return; size_t attrValue = 0, attrValueSize = sizeof(size_t); -#define MULTIPLY_ATTR_VALUE(attr) \ - { \ - PADDLE_ENFORCE(!platform::dynload::cuptiActivityGetAttribute( \ - attr, &attrValueSize, &attrValue)); \ - attrValue *= FLAGS_multiple_of_cupti_buffer_size; \ - LOG(WARNING) << "Set " #attr " " << attrValue << " byte"; \ - PADDLE_ENFORCE(!platform::dynload::cuptiActivitySetAttribute( \ - attr, &attrValueSize, &attrValue)); \ +#define MULTIPLY_ATTR_VALUE(attr) \ + { \ + PADDLE_ENFORCE_EQ( \ + !platform::dynload::cuptiActivityGetAttribute(attr, &attrValueSize, \ + &attrValue), \ + true, platform::errors::Unavailable("Get cupti attribute failed.")); \ + attrValue *= FLAGS_multiple_of_cupti_buffer_size; \ + LOG(WARNING) << "Set " #attr " " << attrValue << " byte"; \ + PADDLE_ENFORCE_EQ( \ + !platform::dynload::cuptiActivitySetAttribute(attr, &attrValueSize, \ + &attrValue), \ + true, platform::errors::Unavailable("Set cupti attribute failed.")); \ } MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE); MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE_CDP); diff --git a/paddle/fluid/platform/mkldnn_reuse.h b/paddle/fluid/platform/mkldnn_reuse.h index 9204cde291..6dc495fb00 100644 --- a/paddle/fluid/platform/mkldnn_reuse.h +++ b/paddle/fluid/platform/mkldnn_reuse.h @@ -500,17 +500,17 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT { if (!this->isCached()) { PADDLE_ENFORCE_EQ( x->layout(), DataLayout::kMKLDNN, - platform::errors::InvalidArgument("Wrong layout set for X tensor")); + platform::errors::InvalidArgument("Wrong layout set for X tensor.")); PADDLE_ENFORCE_NE( x->format(), MKLDNNMemoryFormat::undef, - platform::errors::InvalidArgument("Wrong format set for X tensor")); + platform::errors::InvalidArgument("Wrong format set for X tensor.")); PADDLE_ENFORCE_EQ( y->layout(), DataLayout::kMKLDNN, - platform::errors::InvalidArgument("Wrong layout set for Y tensor")); + platform::errors::InvalidArgument("Wrong layout set for Y tensor.")); PADDLE_ENFORCE_NE( y->format(), MKLDNNMemoryFormat::undef, - platform::errors::InvalidArgument("Wrong format set for Y tensor")); + platform::errors::InvalidArgument("Wrong format set for Y tensor.")); const auto src_x_tz = framework::vectorize(x->dims()); const auto src_y_tz = framework::vectorize(y->dims()); @@ -774,10 +774,10 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerTisCached()) { PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, platform::errors::InvalidArgument( - "Wrong layout set for Input tensor")); + "Wrong layout set for Input tensor.")); PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::undef, platform::errors::InvalidArgument( - "Wrong format set for Input tensor")); + "Wrong format set for Input tensor.")); const std::string pooling_type = ctx.Attr("pooling_type"); @@ -795,15 +795,21 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT("padding_algorithm"); // Only 2D pooling is supported now - PADDLE_ENFORCE_EQ(ksize.size(), 2, - platform::errors::InvalidArgument( - "ksize must be 2D, i.e. 2D pooling")); - PADDLE_ENFORCE_EQ(pooling_type == "max" || pooling_type == "avg", true, - platform::errors::InvalidArgument( - "pooling_type must be 'max' or 'avg'")); - PADDLE_ENFORCE_EQ(input->dims().size(), 4, - platform::errors::InvalidArgument( - "Input dim must be with 4, i.e. NCHW")); + PADDLE_ENFORCE_EQ( + ksize.size(), 2, + platform::errors::InvalidArgument( + "The ksize must be 2D, i.e. 2D pooling, but received %dD.", + ksize.size())); + PADDLE_ENFORCE_EQ( + pooling_type == "max" || pooling_type == "avg", true, + platform::errors::InvalidArgument( + "The pooling_type must be 'max' or 'avg', but received %s.", + pooling_type)); + PADDLE_ENFORCE_EQ( + input->dims().size(), 4, + platform::errors::InvalidArgument( + "Input dim must be with 4, i.e. NCHW, but received %d.", + input->dims().size())); const auto input_dims = input->dims(); framework::DDim data_dims = @@ -1421,7 +1427,7 @@ static std::shared_ptr SetDstMemory( residual_param_data, platform::errors::PreconditionNotMet("Residual parameter is required for " "the DNNL conv+elementwise_add " - "fusion, but now it is missing")); + "fusion, but now it is missing.")); std::shared_ptr user_residual_memory_p = handler->AcquireResidualDataMemory(user_residual_md, to_void_cast(residual_param_data)); diff --git a/paddle/fluid/platform/nccl_helper.h b/paddle/fluid/platform/nccl_helper.h index 8ae88746fc..22550de5b3 100644 --- a/paddle/fluid/platform/nccl_helper.h +++ b/paddle/fluid/platform/nccl_helper.h @@ -96,8 +96,9 @@ struct NCCLContextMap { explicit NCCLContextMap(const std::vector &places, ncclUniqueId *nccl_id = nullptr, size_t num_trainers = 1, size_t trainer_id = 0) { - PADDLE_ENFORCE_EQ(!places.empty(), true, platform::errors::InvalidArgument( - "The NCCL place is empty.")); + PADDLE_ENFORCE_EQ(!places.empty(), true, + platform::errors::InvalidArgument( + "The NCCL place should not be empty.")); order_.reserve(places.size()); for (auto &p : places) { int dev_id = BOOST_GET_CONST(CUDAPlace, p).device; @@ -276,8 +277,9 @@ class NCCLCommunicator { PADDLE_ENFORCE_GT( inter_trainers_num, 1, - platform::errors::InvalidArgument("inter_trainers_num:%llu must > 1", - inter_trainers_num)); + platform::errors::InvalidArgument( + "The inter_trainers_num:%llu should be larger than 1.", + inter_trainers_num)); int inter_trainer_id = trainer_id % inter_trainers_num; for (size_t i = 0; i < inter_nccl_ids.size(); i++) { diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index fc1d9a8799..85759bc6e2 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -94,10 +94,9 @@ void MemEvenRecorder::PushMemRecord(const void *ptr, const Place &place, if (g_state == ProfilerState::kDisabled) return; std::lock_guard guard(mtx_); auto &events = address_memevent_[place]; - PADDLE_ENFORCE_EQ( - events.count(ptr), 0, - platform::errors::InvalidArgument( - "The Place can't exist in the stage of PushMemRecord")); + PADDLE_ENFORCE_EQ(events.count(ptr), 0, + platform::errors::InvalidArgument( + "The Place can't exist in the stage of PushMemRecord")); events.emplace(ptr, std::unique_ptr( new MemEvenRecorder::RecordMemEvent(place, size))); } diff --git a/paddle/fluid/platform/profiler_helper.h b/paddle/fluid/platform/profiler_helper.h index 36c577fa05..c79195aa0d 100644 --- a/paddle/fluid/platform/profiler_helper.h +++ b/paddle/fluid/platform/profiler_helper.h @@ -570,7 +570,7 @@ void PrintProfiler( } else { PADDLE_THROW(platform::errors::InvalidArgument( "Except profiler state must to be one of ['CPU', 'GPU' 'ALL'], but " - "received Invalid profiler state")); + "received Invalid profiler state.")); } if (merge_thread) { diff --git a/paddle/fluid/platform/resource_pool.h b/paddle/fluid/platform/resource_pool.h index d988d12a75..3603c0f24f 100644 --- a/paddle/fluid/platform/resource_pool.h +++ b/paddle/fluid/platform/resource_pool.h @@ -60,7 +60,7 @@ class ResourcePool : public std::enable_shared_from_this> { obj = creator_(); PADDLE_ENFORCE_NOT_NULL(obj, platform::errors::PermissionDenied( - "The creator should not return nullptr")); + "The creator should not return nullptr.")); VLOG(10) << "Create new instance " << TypePtrName(); } else { obj = instances_.back(); -- GitLab