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

refine PADDLE_ENFORCE (#25456)

* Refine PADDLE_ENFORCE in paddle/fluid/platform
test=develop
上级 6c0982b9
......@@ -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();
}
......
......@@ -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
}
......
......@@ -50,11 +50,11 @@ std::shared_ptr<CudaStreamObject> 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<CudaEventObject> 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();
}
......
......@@ -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));
}
}
......
......@@ -60,10 +60,10 @@ platform::DeviceCode* DeviceCodePool::Get(const platform::Place& place,
}
DeviceCodePool::DeviceCodePool(const std::vector<platform::Place>& 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<Place> set;
for (auto& p : places) {
......
......@@ -103,9 +103,9 @@ DeviceContextPool::DeviceContextPool(
#ifdef PADDLE_WITH_CUDA
EmplaceDeviceContext<CUDADeviceContext, CUDAPlace>(&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
}
}
......
......@@ -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 <typename... Args> \
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<tensorrt_func>(p_##__name)(args...); \
} \
}; \
#define DECLARE_DYNAMIC_LOAD_TENSORRT_WRAP(__name) \
struct DynLoad__##__name { \
template <typename... Args> \
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<tensorrt_func>(p_##__name)(args...); \
} \
}; \
extern DynLoad__##__name __name
#define TENSORRT_RAND_ROUTINE_EACH(__macro) \
......
......@@ -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();
}
......
......@@ -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;
......
......@@ -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);
......
......@@ -500,17 +500,17 @@ class BinaryMKLDNNHandler : public platform::MKLDNNHandlerT<T, dnnl::binary> {
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 MKLDNNHandlerT<T, mkldnn::pooling_forward,
if (!this->isCached()) {
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<std::string>("pooling_type");
......@@ -795,15 +795,21 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT<T, mkldnn::pooling_forward,
ctx.Attr<std::string>("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<mkldnn::memory> 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<mkldnn::memory> user_residual_memory_p =
handler->AcquireResidualDataMemory(user_residual_md,
to_void_cast<T>(residual_param_data));
......
......@@ -96,8 +96,9 @@ struct NCCLContextMap {
explicit NCCLContextMap(const std::vector<platform::Place> &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++) {
......
......@@ -94,10 +94,9 @@ void MemEvenRecorder::PushMemRecord(const void *ptr, const Place &place,
if (g_state == ProfilerState::kDisabled) return;
std::lock_guard<std::mutex> 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<RecordMemEvent>(
new MemEvenRecorder::RecordMemEvent(place, size)));
}
......
......@@ -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) {
......
......@@ -60,7 +60,7 @@ class ResourcePool : public std::enable_shared_from_this<ResourcePool<T>> {
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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册