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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Tensor.cpp
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
18 changes: 18 additions & 0 deletions test/TestOpTensorCreate.cpp
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_STREQ("Kompute Tensor attempted to create a zero-sized buffer", err.what() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StrEq comparison may be too brittle for test, eventually it would be interesting to explore structured errror/exception handling, but for now is there a way to just have a check for "zero sized" in the string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May also be worth adding an extra comment in the constructor so it's explicit that it will throw an error if empty tensor

}

}