From 0a46d345382b509b9811b00eec2b09bd9b2f82a5 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Wed, 4 Sep 2019 17:35:50 +0800 Subject: [PATCH] refine some PADDLE_ENFORCE codes for unify PADDLE_ASSERT_MSG (#19607) test=develop --- paddle/fluid/framework/operator.h | 2 +- paddle/fluid/operators/conv_cudnn_op.cu.cc | 1 - paddle/fluid/operators/gather.h | 12 +++++++++--- paddle/fluid/operators/lstm_unit_op.cu | 1 - paddle/fluid/operators/math/blas_impl.h | 6 +++++- paddle/fluid/operators/scatter.cu.h | 12 ++++++++---- paddle/fluid/operators/scatter.h | 22 +++++++++++++++------- 7 files changed, 38 insertions(+), 18 deletions(-) diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index 037d2e41b3..03f083cdbd 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -341,7 +341,7 @@ class ExecutionContext { #ifdef PADDLE_WITH_CUDA const inline platform::CUDADeviceContext& cuda_device_context() const { - PADDLE_ENFORCE(platform::is_gpu_place(device_context_.GetPlace())); + PADDLE_ENFORCE_EQ(platform::is_gpu_place(device_context_.GetPlace()), true); return *reinterpret_cast( &device_context_); } diff --git a/paddle/fluid/operators/conv_cudnn_op.cu.cc b/paddle/fluid/operators/conv_cudnn_op.cu.cc index 7aa1419126..ecedb7d70f 100644 --- a/paddle/fluid/operators/conv_cudnn_op.cu.cc +++ b/paddle/fluid/operators/conv_cudnn_op.cu.cc @@ -18,7 +18,6 @@ limitations under the License. */ #include "paddle/fluid/operators/conv_cudnn_helper.h" #include "paddle/fluid/operators/conv_cudnn_op_cache.h" #include "paddle/fluid/operators/conv_op.h" -#include "paddle/fluid/platform/assert.h" #include "paddle/fluid/platform/cudnn_helper.h" #include "paddle/fluid/platform/cudnn_workspace_helper.h" #include "paddle/fluid/platform/float16.h" diff --git a/paddle/fluid/operators/gather.h b/paddle/fluid/operators/gather.h index d2f519c162..26fb93c2eb 100644 --- a/paddle/fluid/operators/gather.h +++ b/paddle/fluid/operators/gather.h @@ -36,10 +36,16 @@ using framework::Tensor; template void CPUGather(const platform::DeviceContext& ctx, const Tensor& src, const Tensor& index, Tensor* output) { - PADDLE_ENFORCE(platform::is_cpu_place(ctx.GetPlace())); + PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true); // check index of shape 1-D - PADDLE_ENFORCE(index.dims().size() == 1 || - (index.dims().size() == 2 && index.dims()[1] == 1)); + if (index.dims().size() == 2) { + PADDLE_ENFORCE_EQ(index.dims()[1], 1, + "index.dims()[1] should be 1 when index.dims().size() == " + "2 in gather_op."); + } else { + PADDLE_ENFORCE_EQ(index.dims().size(), 1, + "index.dims().size() should be 1 or 2 in gather_op."); + } int64_t index_size = index.dims()[0]; auto src_dims = src.dims(); diff --git a/paddle/fluid/operators/lstm_unit_op.cu b/paddle/fluid/operators/lstm_unit_op.cu index acf094238f..87451cb127 100644 --- a/paddle/fluid/operators/lstm_unit_op.cu +++ b/paddle/fluid/operators/lstm_unit_op.cu @@ -19,7 +19,6 @@ https://github.com/caffe2/caffe2/blob/master/caffe2/operators/lstm_unit_op_gpu.c #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/cross_entropy_op.h" #include "paddle/fluid/operators/lstm_unit_op.h" -#include "paddle/fluid/platform/assert.h" #include "paddle/fluid/platform/hostdevice.h" namespace paddle { diff --git a/paddle/fluid/operators/math/blas_impl.h b/paddle/fluid/operators/math/blas_impl.h index 2f7aeb7058..8bc1bd720c 100644 --- a/paddle/fluid/operators/math/blas_impl.h +++ b/paddle/fluid/operators/math/blas_impl.h @@ -666,7 +666,11 @@ void Blas::MatMul(const framework::Tensor &mat_a, mat_b.data(), beta, mat_out->data()); } else { PADDLE_ENFORCE(dim_a.batch_size_ == dim_b.batch_size_ || - dim_a.batch_size_ == 0 || dim_b.batch_size_ == 0); + dim_a.batch_size_ == 0 || dim_b.batch_size_ == 0, + "dim_a.batch_size should be equal to dim_b.batch_size, or " + "one of dim_a.batch_size and dim_b.batch_size should be 0. " + "But got dim_a.batch_size = %d, dim_b.batch_size = %d.", + dim_a.batch_size_, dim_b.batch_size_); this->template BatchedGEMM( transA, transB, dim_a.height_, dim_b.width_, dim_a.width_, alpha, mat_a.data(), mat_b.data(), beta, mat_out->data(), diff --git a/paddle/fluid/operators/scatter.cu.h b/paddle/fluid/operators/scatter.cu.h index f8d08b2e44..8d28173c8e 100644 --- a/paddle/fluid/operators/scatter.cu.h +++ b/paddle/fluid/operators/scatter.cu.h @@ -90,12 +90,16 @@ template void GPUScatterAssign(const framework::ExecutionContext& context, const Tensor& src, const Tensor& index, Tensor* output, bool overwrite = true) { - // PADDLE_ENFORCE(platform::is_gpu_place(place)); // check index of shape 1-D - const auto& ctx = context.device_context(); - PADDLE_ENFORCE(index.dims().size() == 1 || - (index.dims().size() == 2 && index.dims()[1] == 1)); + if (index.dims().size() == 2) { + PADDLE_ENFORCE_EQ(index.dims()[1], 1, + "index.dims()[1] should be 1 when index.dims().size() == " + "2 in scatter_op."); + } else { + PADDLE_ENFORCE_EQ(index.dims().size(), 1, + "index.dims().size() should be 1 or 2 in scatter_op."); + } int index_size = index.dims()[0]; auto src_dims = src.dims(); diff --git a/paddle/fluid/operators/scatter.h b/paddle/fluid/operators/scatter.h index 6d9d1863c2..2a88b96dd8 100644 --- a/paddle/fluid/operators/scatter.h +++ b/paddle/fluid/operators/scatter.h @@ -73,10 +73,16 @@ elementwise_inner_add(const framework::ExecutionContext& ctx, template void ScatterAssign(const platform::DeviceContext& ctx, const Tensor& src, const Tensor& index, Tensor* output) { - PADDLE_ENFORCE(platform::is_cpu_place(ctx.GetPlace())); + PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.GetPlace()), true); // check index of shape 1-D - PADDLE_ENFORCE(index.dims().size() == 1 || - (index.dims().size() == 2 && index.dims()[1] == 1)); + if (index.dims().size() == 2) { + PADDLE_ENFORCE_EQ(index.dims()[1], 1, + "index.dims()[1] should be 1 when index.dims().size() == " + "2 in scatter_op."); + } else { + PADDLE_ENFORCE_EQ(index.dims().size(), 1, + "index.dims().size() should be 1 or 2 in scatter_op."); + } int index_size = index.dims()[0]; auto src_dims = src.dims(); @@ -88,7 +94,7 @@ void ScatterAssign(const platform::DeviceContext& ctx, const Tensor& src, // check src shape and dst shape should match for (int i = 1; i < src_dims.size(); i++) - PADDLE_ENFORCE(src_dims[i] == dst_dims[i]); + PADDLE_ENFORCE_EQ(src_dims[i], dst_dims[i]); // slice size size_t slice_size = 1; @@ -105,10 +111,12 @@ void ScatterAssign(const platform::DeviceContext& ctx, const Tensor& src, template void ScatterAssignAdd(const framework::ExecutionContext& ctx, const Tensor& src, const Tensor& index, Tensor* output) { - PADDLE_ENFORCE(platform::is_cpu_place(ctx.device_context().GetPlace())); + PADDLE_ENFORCE_EQ(platform::is_cpu_place(ctx.device_context().GetPlace()), + true); // check index of shape 1-D PADDLE_ENFORCE(index.dims().size() == 1 || - (index.dims().size() == 2 && index.dims()[1] == 1)); + (index.dims().size() == 2 && index.dims()[1] == 1), + ""); int index_size = index.dims()[0]; auto src_dims = src.dims(); @@ -122,7 +130,7 @@ void ScatterAssignAdd(const framework::ExecutionContext& ctx, const Tensor& src, // check src shape and dst shape should match for (int i = 1; i < src_dims.size(); i++) - PADDLE_ENFORCE(src_dims[i] == dst_dims[i]); + PADDLE_ENFORCE_EQ(src_dims[i], dst_dims[i]); // slice size size_t slice_size = 1; -- GitLab