From 2babd6ff675a7c1516aa656a4640d256605adcf7 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 23 Oct 2020 11:34:21 +0800 Subject: [PATCH] Add compile limit for PADDLE_ENFORCE without error message (#28221) * add compile limit for paddle enforce * polish elementwise_op_function.cu.h * fix failed unittest * fix windows compile failed * detail polish * revert no type constructor --- .../framework/ir/attention_lstm_fuse_pass.cc | 4 +++- .../memory/allocation/best_fit_allocator.cc | 4 ++-- .../memory/allocation/retry_allocator_test.cc | 5 ++-- .../operators/affine_grid_cudnn_op.cu.cc | 14 ++++------- paddle/fluid/operators/clip_op.h | 3 ++- paddle/fluid/platform/errors.h | 23 +++++-------------- 6 files changed, 21 insertions(+), 32 deletions(-) diff --git a/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc b/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc index a4b4308678..3fdc389102 100644 --- a/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc +++ b/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc @@ -108,7 +108,9 @@ void FindWhileOp(Graph* graph) { GraphSafeRemoveNodes(graph, marked_nodes); } -#define CHECK_P1(x) PADDLE_ENFORCE_NOT_NULL(x); +#define CHECK_P1(x) \ + PADDLE_ENFORCE_NOT_NULL( \ + x, platform::errors::NotFound("%s is a null pointer.", #x)) #define CHECK_P2(x0, x1) \ CHECK_P1(x0); \ CHECK_P1(x1); diff --git a/paddle/fluid/memory/allocation/best_fit_allocator.cc b/paddle/fluid/memory/allocation/best_fit_allocator.cc index 2b8d2164f6..e725a215ff 100644 --- a/paddle/fluid/memory/allocation/best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/best_fit_allocator.cc @@ -158,8 +158,8 @@ Allocation* BestFitAllocator::AllocateImpl(size_t size) { } } if (UNLIKELY(highest_set_bit == free_chunks_.size())) { - PADDLE_THROW_BAD_ALLOC("Cannot allocate %d, All fragments size is %d", size, - FreeSize()); + PADDLE_THROW_BAD_ALLOC(platform::errors::ResourceExhausted( + "Cannot allocate %d, All fragments size is %d.", size, FreeSize())); } auto chunk_it = SplitChunk(size, highest_set_bit, map_it); return new BestFitAllocation(this, chunk_it); diff --git a/paddle/fluid/memory/allocation/retry_allocator_test.cc b/paddle/fluid/memory/allocation/retry_allocator_test.cc index 5d3e133f97..b80e48460b 100644 --- a/paddle/fluid/memory/allocation/retry_allocator_test.cc +++ b/paddle/fluid/memory/allocation/retry_allocator_test.cc @@ -105,7 +105,8 @@ class DummyAllocator : public Allocator { protected: Allocation *AllocateImpl(size_t size) override { - PADDLE_THROW_BAD_ALLOC("Always BadAlloc"); + PADDLE_THROW_BAD_ALLOC(platform::errors::ResourceExhausted( + "Here is a test exception, always BadAlloc.")); } void FreeImpl(Allocation *) override {} @@ -120,7 +121,7 @@ TEST(RetryAllocator, RetryAllocatorLastAllocFailure) { ASSERT_TRUE(false); allocation.reset(); } catch (BadAlloc &ex) { - ASSERT_TRUE(std::string(ex.what()).find("Always BadAlloc") != + ASSERT_TRUE(std::string(ex.what()).find("always BadAlloc") != std::string::npos); } } diff --git a/paddle/fluid/operators/affine_grid_cudnn_op.cu.cc b/paddle/fluid/operators/affine_grid_cudnn_op.cu.cc index 009c397e00..c09f71f46c 100644 --- a/paddle/fluid/operators/affine_grid_cudnn_op.cu.cc +++ b/paddle/fluid/operators/affine_grid_cudnn_op.cu.cc @@ -28,10 +28,9 @@ class CUDNNAffineGridOpKernel : public framework::OpKernel { void Compute(const framework::ExecutionContext& ctx) const override { PADDLE_ENFORCE_EQ( platform::is_gpu_place(ctx.GetPlace()), true, - platform::errors::InvalidArgument("Only " - "support for CUDAPlace.Please switch " - "your context from CPUPlace to " - "CUDAPlace or update your cudnn.")); + platform::errors::InvalidArgument( + "Only support for CUDAPlace.Please switch your context from " + "CPUPlace to CUDAPlace or update your cudnn.")); auto& dev_ctx = ctx.template device_context(); auto handle = dev_ctx.cudnn_handle(); auto* theta = ctx.Input("Theta"); @@ -106,12 +105,9 @@ class CUDNNAffineGridGradOpKernel : public framework::OpKernel { const T* output_grad_data = output_grad->data(); T* theta_grad_data = theta_grad->mutable_data(ctx.GetPlace()); - PADDLE_ENFORCE_EQ( + PADDLE_ENFORCE_CUDA_SUCCESS( platform::dynload::cudnnSpatialTfGridGeneratorBackward( - handle, cudnn_st_desc, output_grad_data, theta_grad_data), - 0, - "Some errors " - "has occurred during forward computation in cudnn;"); + handle, cudnn_st_desc, output_grad_data, theta_grad_data)); } }; diff --git a/paddle/fluid/operators/clip_op.h b/paddle/fluid/operators/clip_op.h index 68f5d5460e..097b5e4863 100644 --- a/paddle/fluid/operators/clip_op.h +++ b/paddle/fluid/operators/clip_op.h @@ -133,7 +133,8 @@ class ClipKernel : public framework::OpKernel { trans(context.template device_context(), out_data, out_data + numel, out_data, ClipFunctor(min, max)); } else { - PADDLE_THROW("ClipOp only supports LoDTensor and SelectedRows"); + PADDLE_THROW(platform::errors::Unavailable( + "ClipOp only supports LoDTensor and SelectedRows.")); } } }; diff --git a/paddle/fluid/platform/errors.h b/paddle/fluid/platform/errors.h index 5c145845fa..a2f2e7c130 100644 --- a/paddle/fluid/platform/errors.h +++ b/paddle/fluid/platform/errors.h @@ -30,33 +30,22 @@ typedef ::paddle::platform::error::Code Code; class ErrorSummary { public: - // Note(chenweihang): Final deprecated constructor - // This constructor is only used to be compatible with - // current existing no error message PADDLE_ENFORCE_* - ErrorSummary() { - code_ = paddle::platform::error::LEGACY; - msg_ = - "An error occurred here. There is no accurate error hint for this " - "error yet. We are continuously in the process of increasing hint for " - "this kind of error check. It would be helpful if you could inform us " - "of how this conversion went by opening a github issue. And we will " - "resolve it with high priority.\n" - " - New issue link: " - "https://github.com/PaddlePaddle/Paddle/issues/new\n" - " - Recommended issue content: all error stack information"; - } - // Note(chenweihang): Final deprecated constructor // This constructor is used to be compatible with // current existing untyped PADDLE_ENFORCE_* // PADDLE_ENFORCE + // Note(chenweihang): Windows openblas need this + // constructor for compiling PADDLE_ENFORCE in *.cu, + // this is a bug cause we can't remove this + // constructor now. template explicit ErrorSummary(Args... args) { code_ = paddle::platform::error::LEGACY; msg_ = paddle::string::Sprintf(args...); } - // Note(chenweihang): Recommended constructor + // Note(chenweihang): Only recommended constructor + // No longer supports PADDLE_ENFORCE without type or without error message explicit ErrorSummary(Code code, std::string msg) : code_(code), msg_(msg) {} Code code() const { return code_; }