未验证 提交 c10dcff1 编写于 作者: G GaoWei8 提交者: GitHub

refine PADDLE_ENFORCE (#25456)

* Refine PADDLE_ENFORCE in paddle/fluid/platform
test=develop
上级 6c0982b9
...@@ -81,7 +81,7 @@ class NCCLCommContext { ...@@ -81,7 +81,7 @@ class NCCLCommContext {
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(
comm_map_.count(ring_id), 0, comm_map_.count(ring_id), 0,
platform::errors::InvalidArgument( 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, PADDLE_ENFORCE_EQ(comm_map_.at(ring_id).size(), 1,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"One device id should be specified to retrieve from " "One device id should be specified to retrieve from "
...@@ -94,11 +94,11 @@ class NCCLCommContext { ...@@ -94,11 +94,11 @@ class NCCLCommContext {
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(
comm_map_.count(ring_id), 0, comm_map_.count(ring_id), 0,
platform::errors::InvalidArgument( 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( PADDLE_ENFORCE_GT(
comm_map_.at(ring_id).count(dev_id), 0, comm_map_.at(ring_id).count(dev_id), 0,
platform::errors::InvalidArgument( 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)); dev_id, ring_id));
return comm_map_.at(ring_id).at(dev_id).get(); return comm_map_.at(ring_id).at(dev_id).get();
} }
......
...@@ -44,8 +44,8 @@ void SetNumThreads(int num_threads) { ...@@ -44,8 +44,8 @@ void SetNumThreads(int num_threads) {
omp_set_num_threads(real_num_threads); omp_set_num_threads(real_num_threads);
#else #else
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"The library (except OPENBLAS, MKLML) is to be implemented, thus " "This library (except OPENBLAS, MKLML) is not supported yet, so the"
"number of threads can not be set.")); "number of threads cannot be set."));
#endif #endif
} }
......
...@@ -50,11 +50,11 @@ std::shared_ptr<CudaStreamObject> CudaStreamResourcePool::New(int dev_idx) { ...@@ -50,11 +50,11 @@ std::shared_ptr<CudaStreamObject> CudaStreamResourcePool::New(int dev_idx) {
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
dev_idx, 0, dev_idx, 0,
platform::errors::InvalidArgument( 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( PADDLE_ENFORCE_LT(
dev_idx, pool_.size(), dev_idx, pool_.size(),
platform::errors::OutOfRange( 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)); pool_.size(), dev_idx));
return pool_[dev_idx]->New(); return pool_[dev_idx]->New();
} }
...@@ -89,11 +89,11 @@ std::shared_ptr<CudaEventObject> CudaEventResourcePool::New(int dev_idx) { ...@@ -89,11 +89,11 @@ std::shared_ptr<CudaEventObject> CudaEventResourcePool::New(int dev_idx) {
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
dev_idx, 0, dev_idx, 0,
platform::errors::InvalidArgument( 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( PADDLE_ENFORCE_LT(
dev_idx, pool_.size(), dev_idx, pool_.size(),
platform::errors::OutOfRange( 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)); pool_.size(), dev_idx));
return pool_[dev_idx]->New(); return pool_[dev_idx]->New();
} }
......
...@@ -142,8 +142,8 @@ inline ActivationMode StringToActivationMode(const std::string& str) { ...@@ -142,8 +142,8 @@ inline ActivationMode StringToActivationMode(const std::string& str) {
} else if (str == "bandpass") { } else if (str == "bandpass") {
return ActivationMode::kBandPass; return ActivationMode::kBandPass;
} else { } else {
PADDLE_THROW( PADDLE_THROW(platform::errors::Unimplemented(
platform::errors::Unimplemented("Unknown activation string: %s.", str)); "Unknown CUDNN activation string: %s.", str));
} }
} }
......
...@@ -60,10 +60,10 @@ platform::DeviceCode* DeviceCodePool::Get(const platform::Place& place, ...@@ -60,10 +60,10 @@ platform::DeviceCode* DeviceCodePool::Get(const platform::Place& place,
} }
DeviceCodePool::DeviceCodePool(const std::vector<platform::Place>& places) { DeviceCodePool::DeviceCodePool(const std::vector<platform::Place>& places) {
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(places.size(), 0,
places.size(), 0, errors::InvalidArgument(
errors::InvalidArgument( "Expected the number of places >= 1. But received %d.",
"Expected the number of places >= 1. Expected %d.", places.size())); places.size()));
// Remove the duplicated places // Remove the duplicated places
std::set<Place> set; std::set<Place> set;
for (auto& p : places) { for (auto& p : places) {
......
...@@ -103,9 +103,9 @@ DeviceContextPool::DeviceContextPool( ...@@ -103,9 +103,9 @@ DeviceContextPool::DeviceContextPool(
#ifdef PADDLE_WITH_CUDA #ifdef PADDLE_WITH_CUDA
EmplaceDeviceContext<CUDADeviceContext, CUDAPlace>(&device_contexts_, p); EmplaceDeviceContext<CUDADeviceContext, CUDAPlace>(&device_contexts_, p);
#else #else
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(
"'CUDAPlace is not supported. Please re-compile with WITH_GPU." platform::errors::Unimplemented("CUDAPlace is not supported. Please "
"option")); "re-compile with WITH_GPU option."));
#endif #endif
} else if (platform::is_cuda_pinned_place(p)) { } else if (platform::is_cuda_pinned_place(p)) {
#ifdef PADDLE_WITH_CUDA #ifdef PADDLE_WITH_CUDA
...@@ -113,8 +113,8 @@ DeviceContextPool::DeviceContextPool( ...@@ -113,8 +113,8 @@ DeviceContextPool::DeviceContextPool(
&device_contexts_, p); &device_contexts_, p);
#else #else
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"'CUDAPlace' is not supported. Please re-compile with WITH_GPU." "CUDAPlace is not supported. Please re-compile with WITH_GPU "
"option")); "option."));
#endif #endif
} }
} }
......
...@@ -30,21 +30,25 @@ namespace dynload { ...@@ -30,21 +30,25 @@ namespace dynload {
extern std::once_flag tensorrt_dso_flag; extern std::once_flag tensorrt_dso_flag;
extern void* tensorrt_dso_handle; extern void* tensorrt_dso_handle;
#define DECLARE_DYNAMIC_LOAD_TENSORRT_WRAP(__name) \ #define DECLARE_DYNAMIC_LOAD_TENSORRT_WRAP(__name) \
struct DynLoad__##__name { \ struct DynLoad__##__name { \
template <typename... Args> \ template <typename... Args> \
auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \
using tensorrt_func = decltype(&::__name); \ using tensorrt_func = decltype(&::__name); \
std::call_once(tensorrt_dso_flag, []() { \ std::call_once(tensorrt_dso_flag, []() { \
tensorrt_dso_handle = \ tensorrt_dso_handle = \
paddle::platform::dynload::GetTensorRtDsoHandle(); \ paddle::platform::dynload::GetTensorRtDsoHandle(); \
PADDLE_ENFORCE(tensorrt_dso_handle, "load tensorrt so failed"); \ PADDLE_ENFORCE_NOT_NULL(tensorrt_dso_handle, \
}); \ platform::errors::Unavailable( \
static void* p_##__name = dlsym(tensorrt_dso_handle, #__name); \ "Load tensorrt %s failed", #__name)); \
PADDLE_ENFORCE(p_##__name, "load %s failed", #__name); \ }); \
return reinterpret_cast<tensorrt_func>(p_##__name)(args...); \ 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<tensorrt_func>(p_##__name)(args...); \
} \
}; \
extern DynLoad__##__name __name extern DynLoad__##__name __name
#define TENSORRT_RAND_ROUTINE_EACH(__macro) \ #define TENSORRT_RAND_ROUTINE_EACH(__macro) \
......
...@@ -344,10 +344,10 @@ class RecordedCudaMallocHelper { ...@@ -344,10 +344,10 @@ class RecordedCudaMallocHelper {
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
dev_id, 0, dev_id, 0,
platform::errors::OutOfRange( 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( PADDLE_ENFORCE_LT(
dev_id, instances_.size(), 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())); dev_id, instances_.size()));
return instances_[dev_id].get(); return instances_[dev_id].get();
} }
......
...@@ -39,7 +39,7 @@ inline GpuLaunchParamConfig GetGpuLaunchConfig1D( ...@@ -39,7 +39,7 @@ inline GpuLaunchParamConfig GetGpuLaunchConfig1D(
const platform::CUDADeviceContext& context, int element_count) { const platform::CUDADeviceContext& context, int element_count) {
PADDLE_ENFORCE_GT(element_count, 0, platform::errors::InvalidArgument( PADDLE_ENFORCE_GT(element_count, 0, platform::errors::InvalidArgument(
"element count should greater than 0," "element count should greater than 0,"
" but received value is:%d", " but received value is %d.",
element_count)); element_count));
const int theory_thread_count = element_count; const int theory_thread_count = element_count;
......
...@@ -117,14 +117,18 @@ void InitCupti() { ...@@ -117,14 +117,18 @@ void InitCupti() {
#ifdef PADDLE_WITH_CUPTI #ifdef PADDLE_WITH_CUPTI
if (FLAGS_multiple_of_cupti_buffer_size == 1) return; if (FLAGS_multiple_of_cupti_buffer_size == 1) return;
size_t attrValue = 0, attrValueSize = sizeof(size_t); size_t attrValue = 0, attrValueSize = sizeof(size_t);
#define MULTIPLY_ATTR_VALUE(attr) \ #define MULTIPLY_ATTR_VALUE(attr) \
{ \ { \
PADDLE_ENFORCE(!platform::dynload::cuptiActivityGetAttribute( \ PADDLE_ENFORCE_EQ( \
attr, &attrValueSize, &attrValue)); \ !platform::dynload::cuptiActivityGetAttribute(attr, &attrValueSize, \
attrValue *= FLAGS_multiple_of_cupti_buffer_size; \ &attrValue), \
LOG(WARNING) << "Set " #attr " " << attrValue << " byte"; \ true, platform::errors::Unavailable("Get cupti attribute failed.")); \
PADDLE_ENFORCE(!platform::dynload::cuptiActivitySetAttribute( \ attrValue *= FLAGS_multiple_of_cupti_buffer_size; \
attr, &attrValueSize, &attrValue)); \ 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);
MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE_CDP); MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE_CDP);
......
...@@ -500,17 +500,17 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT<T, dnnl::binary> { ...@@ -500,17 +500,17 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT<T, dnnl::binary> {
if (!this->isCached()) { if (!this->isCached()) {
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
x->layout(), DataLayout::kMKLDNN, 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( PADDLE_ENFORCE_NE(
x->format(), MKLDNNMemoryFormat::undef, 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( PADDLE_ENFORCE_EQ(
y->layout(), DataLayout::kMKLDNN, 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( PADDLE_ENFORCE_NE(
y->format(), MKLDNNMemoryFormat::undef, 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_x_tz = framework::vectorize(x->dims());
const auto src_y_tz = framework::vectorize(y->dims()); const auto src_y_tz = framework::vectorize(y->dims());
...@@ -774,10 +774,10 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT<T, mkldnn::pooling_forward, ...@@ -774,10 +774,10 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT<T, mkldnn::pooling_forward,
if (!this->isCached()) { if (!this->isCached()) {
PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Wrong layout set for Input tensor")); "Wrong layout set for Input tensor."));
PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::undef, PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::undef,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Wrong format set for Input tensor")); "Wrong format set for Input tensor."));
const std::string pooling_type = ctx.Attr<std::string>("pooling_type"); const std::string pooling_type = ctx.Attr<std::string>("pooling_type");
...@@ -795,15 +795,21 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT<T, mkldnn::pooling_forward, ...@@ -795,15 +795,21 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT<T, mkldnn::pooling_forward,
ctx.Attr<std::string>("padding_algorithm"); ctx.Attr<std::string>("padding_algorithm");
// Only 2D pooling is supported now // Only 2D pooling is supported now
PADDLE_ENFORCE_EQ(ksize.size(), 2, PADDLE_ENFORCE_EQ(
platform::errors::InvalidArgument( ksize.size(), 2,
"ksize must be 2D, i.e. 2D pooling")); platform::errors::InvalidArgument(
PADDLE_ENFORCE_EQ(pooling_type == "max" || pooling_type == "avg", true, "The ksize must be 2D, i.e. 2D pooling, but received %dD.",
platform::errors::InvalidArgument( ksize.size()));
"pooling_type must be 'max' or 'avg'")); PADDLE_ENFORCE_EQ(
PADDLE_ENFORCE_EQ(input->dims().size(), 4, pooling_type == "max" || pooling_type == "avg", true,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Input dim must be with 4, i.e. NCHW")); "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(); const auto input_dims = input->dims();
framework::DDim data_dims = framework::DDim data_dims =
...@@ -1421,7 +1427,7 @@ static std::shared_ptr<mkldnn::memory> SetDstMemory( ...@@ -1421,7 +1427,7 @@ static std::shared_ptr<mkldnn::memory> SetDstMemory(
residual_param_data, residual_param_data,
platform::errors::PreconditionNotMet("Residual parameter is required for " platform::errors::PreconditionNotMet("Residual parameter is required for "
"the DNNL conv+elementwise_add " "the DNNL conv+elementwise_add "
"fusion, but now it is missing")); "fusion, but now it is missing."));
std::shared_ptr<mkldnn::memory> user_residual_memory_p = std::shared_ptr<mkldnn::memory> user_residual_memory_p =
handler->AcquireResidualDataMemory(user_residual_md, handler->AcquireResidualDataMemory(user_residual_md,
to_void_cast<T>(residual_param_data)); to_void_cast<T>(residual_param_data));
......
...@@ -96,8 +96,9 @@ struct NCCLContextMap { ...@@ -96,8 +96,9 @@ struct NCCLContextMap {
explicit NCCLContextMap(const std::vector<platform::Place> &places, explicit NCCLContextMap(const std::vector<platform::Place> &places,
ncclUniqueId *nccl_id = nullptr, ncclUniqueId *nccl_id = nullptr,
size_t num_trainers = 1, size_t trainer_id = 0) { size_t num_trainers = 1, size_t trainer_id = 0) {
PADDLE_ENFORCE_EQ(!places.empty(), true, platform::errors::InvalidArgument( PADDLE_ENFORCE_EQ(!places.empty(), true,
"The NCCL place is empty.")); platform::errors::InvalidArgument(
"The NCCL place should not be empty."));
order_.reserve(places.size()); order_.reserve(places.size());
for (auto &p : places) { for (auto &p : places) {
int dev_id = BOOST_GET_CONST(CUDAPlace, p).device; int dev_id = BOOST_GET_CONST(CUDAPlace, p).device;
...@@ -276,8 +277,9 @@ class NCCLCommunicator { ...@@ -276,8 +277,9 @@ class NCCLCommunicator {
PADDLE_ENFORCE_GT( PADDLE_ENFORCE_GT(
inter_trainers_num, 1, inter_trainers_num, 1,
platform::errors::InvalidArgument("inter_trainers_num:%llu must > 1", platform::errors::InvalidArgument(
inter_trainers_num)); "The inter_trainers_num:%llu should be larger than 1.",
inter_trainers_num));
int inter_trainer_id = trainer_id % inter_trainers_num; int inter_trainer_id = trainer_id % inter_trainers_num;
for (size_t i = 0; i < inter_nccl_ids.size(); i++) { for (size_t i = 0; i < inter_nccl_ids.size(); i++) {
......
...@@ -94,10 +94,9 @@ void MemEvenRecorder::PushMemRecord(const void *ptr, const Place &place, ...@@ -94,10 +94,9 @@ void MemEvenRecorder::PushMemRecord(const void *ptr, const Place &place,
if (g_state == ProfilerState::kDisabled) return; if (g_state == ProfilerState::kDisabled) return;
std::lock_guard<std::mutex> guard(mtx_); std::lock_guard<std::mutex> guard(mtx_);
auto &events = address_memevent_[place]; auto &events = address_memevent_[place];
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(events.count(ptr), 0,
events.count(ptr), 0, platform::errors::InvalidArgument(
platform::errors::InvalidArgument( "The Place can't exist in the stage of PushMemRecord"));
"The Place can't exist in the stage of PushMemRecord"));
events.emplace(ptr, std::unique_ptr<RecordMemEvent>( events.emplace(ptr, std::unique_ptr<RecordMemEvent>(
new MemEvenRecorder::RecordMemEvent(place, size))); new MemEvenRecorder::RecordMemEvent(place, size)));
} }
......
...@@ -570,7 +570,7 @@ void PrintProfiler( ...@@ -570,7 +570,7 @@ void PrintProfiler(
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
"Except profiler state must to be one of ['CPU', 'GPU' 'ALL'], but " "Except profiler state must to be one of ['CPU', 'GPU' 'ALL'], but "
"received Invalid profiler state")); "received Invalid profiler state."));
} }
if (merge_thread) { if (merge_thread) {
......
...@@ -60,7 +60,7 @@ class ResourcePool : public std::enable_shared_from_this<ResourcePool<T>> { ...@@ -60,7 +60,7 @@ class ResourcePool : public std::enable_shared_from_this<ResourcePool<T>> {
obj = creator_(); obj = creator_();
PADDLE_ENFORCE_NOT_NULL(obj, PADDLE_ENFORCE_NOT_NULL(obj,
platform::errors::PermissionDenied( platform::errors::PermissionDenied(
"The creator should not return nullptr")); "The creator should not return nullptr."));
VLOG(10) << "Create new instance " << TypePtrName(); VLOG(10) << "Create new instance " << TypePtrName();
} else { } else {
obj = instances_.back(); obj = instances_.back();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册