Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallowing zero sized tensors #129

Merged
merged 2 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}",
Expand Down
2 changes: 1 addition & 1 deletion src/include/kompute/Tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/TestOpTensorCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}