Merge pull request #129 from alexander-g/no_zero_size_tensors

Disallowing zero sized tensors
This commit is contained in:
Alejandro Saucedo 2021-01-31 07:19:07 +00:00 committed by GitHub
commit 4b8c1c49f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View file

@ -268,10 +268,14 @@ Tensor::createBuffer()
throw std::runtime_error("Kompute Tensor device is null");
}
this->mFreeBuffer = true;
vk::BufferUsageFlags usageFlags = this->getBufferUsageFlags();
vk::DeviceSize bufferSize = this->memorySize();
if(bufferSize<1){
throw std::runtime_error("Kompute Tensor attempted to create a zero-sized buffer");
}
this->mFreeBuffer = true;
SPDLOG_DEBUG("Kompute Tensor creating buffer with memory size: {}, and "
"usage flags: {}",

View file

@ -39,7 +39,7 @@ class Tensor
* Default constructor with data provided which would be used to create the
* respective vulkan buffer and memory.
*
* @param data Vector of data that will be used by the tensor
* @param data Non-zero-sized vector of data that will be used by the tensor
* @param tensorType Type for the tensor which is of type TensorTypes
*/
Tensor(const std::vector<float>& data,

View file

@ -113,3 +113,21 @@ TEST(TestOpTensorCreate, NoErrorIfTensorFreedBefore)
EXPECT_FALSE(tensorA->isInit());
EXPECT_FALSE(tensorB->isInit());
}
TEST(TestOpTensorCreate, ExceptionOnZeroSizeTensor)
{
std::vector<float> testVecA;
std::shared_ptr<kp::Tensor> tensorA{ new kp::Tensor(testVecA) };
kp::Manager mgr;
try{
mgr.evalOpDefault<kp::OpTensorCreate>({ tensorA });
} catch( const std::runtime_error& err ) {
// check exception
ASSERT_TRUE( std::string(err.what()).find("zero-sized") != std::string::npos );
}
}