From c719add76e470080d369b7a8e6dca34d0376864b Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Tue, 28 Sep 2021 10:46:50 +0800 Subject: [PATCH] reduce calls to SizeOfType (#36110) --- paddle/fluid/framework/tensor.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/framework/tensor.cc b/paddle/fluid/framework/tensor.cc index 4f6eb803d1..fbd7aa588d 100644 --- a/paddle/fluid/framework/tensor.cc +++ b/paddle/fluid/framework/tensor.cc @@ -29,14 +29,16 @@ void Tensor::check_memory_size() const { PADDLE_ENFORCE_NOT_NULL(holder_, platform::errors::PreconditionNotMet( "Tensor holds no memory. " "Call Tensor::mutable_data firstly.")); + size_t size = numel() * SizeOfType(type()); + PADDLE_ENFORCE_LE( - numel() * SizeOfType(type()), memory_size(), + size, memory_size(), platform::errors::PreconditionNotMet( "Tensor's dimension is out of bound." "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())); + size, memory_size())); } Tensor::Tensor(const proto::VarType::Type& dtype) -- GitLab