未验证 提交 13804ed8 编写于 作者: W WeiXin 提交者: GitHub

Error msg/polish tensor error msg (#26976)

* polish one line error message in tensor.cc

* polish error messages in tensor.cc,tensor.h tensor_impl.h

* polish error messages in tensor.cc tensor.h tensor_impl.h

* polish error messages in tensor.cc,tensor.h tensor_impl.h

* polish error messages in tensor.cc tensor.h tensor_impl.h tensor_test.cc

* polish error messages in tensor.cc tensor.h tensor_impl.h
上级 c7b9d97f
...@@ -19,13 +19,17 @@ namespace paddle { ...@@ -19,13 +19,17 @@ namespace paddle {
namespace framework { namespace framework {
extern size_t SizeOfType(proto::VarType::Type type); extern size_t SizeOfType(proto::VarType::Type type);
void Tensor::check_memory_size() const { void Tensor::check_memory_size() const {
PADDLE_ENFORCE_NOT_NULL( PADDLE_ENFORCE_NOT_NULL(holder_, platform::errors::PreconditionNotMet(
holder_, "Tensor holds no memory. Call Tensor::mutable_data first."); "Tensor holds no memory. "
"Call Tensor::mutable_data firstly."));
PADDLE_ENFORCE_LE( PADDLE_ENFORCE_LE(
numel() * SizeOfType(type()), memory_size(), numel() * SizeOfType(type()), memory_size(),
"Tensor's dims_ is out of bound. Call Tensor::mutable_data " platform::errors::PreconditionNotMet(
"first to re-allocate memory.\n" "Tensor's dimension is out of bound."
"or maybe the required data-type mismatches the data already stored."); "Tensor's dimension must be equal or less than the size of its "
"memory."
"But received Tensor's dimension is d%, memory's size is %d.",
numel() * SizeOfType(type()), memory_size()));
} }
Tensor::Tensor(const proto::VarType::Type& dtype) : type_(dtype), offset_(0) {} Tensor::Tensor(const proto::VarType::Type& dtype) : type_(dtype), offset_(0) {}
...@@ -37,15 +41,21 @@ size_t Tensor::memory_size() const { ...@@ -37,15 +41,21 @@ size_t Tensor::memory_size() const {
void* Tensor::mutable_data(const platform::Place& place, void* Tensor::mutable_data(const platform::Place& place,
proto::VarType::Type type, size_t requested_size) { proto::VarType::Type type, size_t requested_size) {
type_ = type; type_ = type;
PADDLE_ENFORCE_GE(numel(), 0, PADDLE_ENFORCE_GE(
"When calling this method, the Tensor's numel must be " numel(), 0,
"equal or larger than zero. " platform::errors::PreconditionNotMet(
"Please check Tensor::dims, or Tensor::Resize has been " "The Tensor's element number must be equal or greater than zero. "
"called first. The Tensor's shape is [", "The Tensor's shape is [",
dims(), "] now"); dims(), "] now"));
size_t size = numel() * SizeOfType(type); size_t size = numel() * SizeOfType(type);
if (requested_size) { if (requested_size) {
PADDLE_ENFORCE_GE(requested_size, size); PADDLE_ENFORCE_GE(
requested_size, size,
platform::errors::InvalidArgument(
"The requested memory size is less than the memory size of Tensor. "
"But received requested memory size is d%, "
"memory size of Tensor is %d.",
requested_size, size));
size = requested_size; size = requested_size;
} }
/* some versions of boost::variant don't have operator!= */ /* some versions of boost::variant don't have operator!= */
...@@ -62,8 +72,8 @@ void* Tensor::mutable_data(const platform::Place& place, ...@@ -62,8 +72,8 @@ void* Tensor::mutable_data(const platform::Place& place,
void* Tensor::mutable_data(const platform::Place& place, void* Tensor::mutable_data(const platform::Place& place,
size_t requested_size) { size_t requested_size) {
PADDLE_ENFORCE_NOT_NULL( PADDLE_ENFORCE_NOT_NULL(this->holder_, platform::errors::PreconditionNotMet(
this->holder_, "Cannot invoke mutable data if current hold nothing."); "The tensor is not initialized."));
return mutable_data(place, type_, requested_size); return mutable_data(place, type_, requested_size);
} }
...@@ -75,12 +85,20 @@ Tensor& Tensor::ShareDataWith(const Tensor& src) { ...@@ -75,12 +85,20 @@ Tensor& Tensor::ShareDataWith(const Tensor& src) {
Tensor Tensor::Slice(int64_t begin_idx, int64_t end_idx) const { Tensor Tensor::Slice(int64_t begin_idx, int64_t end_idx) const {
check_memory_size(); check_memory_size();
PADDLE_ENFORCE_GE(begin_idx, 0, PADDLE_ENFORCE_GE(
"The start row index must be greater than 0."); begin_idx, 0,
PADDLE_ENFORCE_LE(end_idx, dims_[0], "The end row index is out of bound."); platform::errors::OutOfRange("The start row index must be greater than 0."
"But received the start index is d%.",
begin_idx));
PADDLE_ENFORCE_LE(
end_idx, dims_[0],
platform::errors::OutOfRange("The end row index is out of bound."));
PADDLE_ENFORCE_LT( PADDLE_ENFORCE_LT(
begin_idx, end_idx, begin_idx, end_idx,
"The start row index must be lesser than the end row index."); platform::errors::InvalidArgument(
"The start row index must be less than the end row index."
"But received the start index = %d, the end index = %d.",
begin_idx, end_idx));
if (dims_[0] == 1) { if (dims_[0] == 1) {
return *this; return *this;
......
...@@ -131,13 +131,17 @@ class Tensor { ...@@ -131,13 +131,17 @@ class Tensor {
const platform::Place& place() const { const platform::Place& place() const {
PADDLE_ENFORCE_NOT_NULL( PADDLE_ENFORCE_NOT_NULL(
holder_, "Tensor not initialized yet when Tensor::place() is called."); holder_,
platform::errors::PreconditionNotMet(
"Tensor not initialized yet when Tensor::place() is called."));
return holder_->place(); return holder_->place();
} }
proto::VarType::Type type() const { proto::VarType::Type type() const {
PADDLE_ENFORCE_NOT_NULL( PADDLE_ENFORCE_NOT_NULL(
holder_, "Tensor not initialized yet when Tensor::type() is called."); holder_,
platform::errors::PreconditionNotMet(
"Tensor not initialized yet when Tensor::type() is called."));
return type_; return type_;
} }
......
...@@ -43,9 +43,13 @@ inline T* Tensor::data() { ...@@ -43,9 +43,13 @@ inline T* Tensor::data() {
check_memory_size(); check_memory_size();
bool valid = bool valid =
std::is_same<T, void>::value || type_ == DataTypeTrait<T>::DataType(); std::is_same<T, void>::value || type_ == DataTypeTrait<T>::DataType();
PADDLE_ENFORCE( PADDLE_ENFORCE_EQ(
valid, "Tensor holds the wrong type, it holds %s, but desires to be %s", valid, true,
DataTypeToString(type_), DataTypeToString(DataTypeTrait<T>::DataType())); platform::errors::InvalidArgument(
"Tensor holds the wrong type, it holds %s, but desires to be %s",
DataTypeToString(type_),
DataTypeToString(DataTypeTrait<T>::DataType())));
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(holder_->ptr()) + return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(holder_->ptr()) +
offset_); offset_);
} }
...@@ -69,9 +73,12 @@ inline T* Tensor::mutable_data(const platform::Place& place, ...@@ -69,9 +73,12 @@ inline T* Tensor::mutable_data(const platform::Place& place,
inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) { inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {
int rank = src.dims().size(); int rank = src.dims().size();
PADDLE_ENFORCE_GE( PADDLE_ENFORCE_GE(
rank, 2, rank, 2, platform::errors::InvalidArgument(
"'ReshapeToMatrix()' is only used for flatten high rank " "'ReshapeToMatrix()' is only used for flatten high rank "
"tensors to matrixs. Can not be used in reshaping vectors."); "tensors to matrixs. The dimensions of Tensor must be "
"greater or equal than 2. "
"But received dimensions of Tensor is %d",
rank));
if (rank == 2) { if (rank == 2) {
return src; return src;
} }
......
...@@ -41,7 +41,7 @@ TEST(Tensor, DataAssert) { ...@@ -41,7 +41,7 @@ TEST(Tensor, DataAssert) {
std::string ex_msg = err.what(); std::string ex_msg = err.what();
EXPECT_TRUE(ex_msg.find("holder_ should not be null") != std::string::npos); EXPECT_TRUE(ex_msg.find("holder_ should not be null") != std::string::npos);
EXPECT_TRUE(ex_msg.find("Tensor holds no memory. Call " EXPECT_TRUE(ex_msg.find("Tensor holds no memory. Call "
"Tensor::mutable_data first.") != "Tensor::mutable_data firstly.") !=
std::string::npos); std::string::npos);
} }
ASSERT_TRUE(caught); ASSERT_TRUE(caught);
...@@ -157,7 +157,7 @@ TEST(Tensor, ShareDataWith) { ...@@ -157,7 +157,7 @@ TEST(Tensor, ShareDataWith) {
EXPECT_TRUE(ex_msg.find("holder_ should not be null") != EXPECT_TRUE(ex_msg.find("holder_ should not be null") !=
std::string::npos); std::string::npos);
EXPECT_TRUE(ex_msg.find("Tensor holds no memory. Call " EXPECT_TRUE(ex_msg.find("Tensor holds no memory. Call "
"Tensor::mutable_data first.") != "Tensor::mutable_data firstly.") !=
std::string::npos); std::string::npos);
} }
ASSERT_TRUE(caught); ASSERT_TRUE(caught);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册