!2946 Added empty tensor support

Merge pull request !2946 from EricZ/emtpy-tensor
This commit is contained in:
mindspore-ci-bot 2020-07-09 09:38:32 +08:00 committed by Gitee
commit 5355deca04
3 changed files with 27 additions and 0 deletions

View File

@ -513,6 +513,15 @@ const unsigned char *Tensor::GetBuffer() const {
return data_;
}
// check for empty
bool Tensor::HasData() const {
if (data_ == nullptr) {
return true;
} else {
return false;
}
}
unsigned char *Tensor::GetMutableBuffer() {
if (!shape_.known() || type_ == DataType::DE_UNKNOWN) {
return nullptr;

View File

@ -277,6 +277,10 @@ class Tensor {
// @return
const TensorShape &shape() const { return shape_; }
/// Check if tensor has data
/// \return bool - true if tensor is empty
bool HasData() const;
// Reshape the tensor. The given shape should have the same number of elements in the Tensor
// @param shape
virtual Status Reshape(const TensorShape &shape);

View File

@ -432,3 +432,17 @@ TEST_F(MindDataTestTensorDE, TensorConcatenate) {
s = t1->Concatenate({5}, t2);
EXPECT_FALSE(s.IsOk());
}
TEST_F(MindDataTestTensorDE, TensorEmpty) {
std::shared_ptr<Tensor> t = std::make_shared<Tensor>(TensorShape({2, 3}), DataType(DataType::DE_UINT64));
ASSERT_TRUE(t->HasData());
}
TEST_F(MindDataTestTensorDE, TensorEmptyInvalidate) {
std::vector<uint32_t> values1 = {1, 2, 3, 0, 0, 0};
std::shared_ptr<Tensor> t;
Tensor::CreateTensor(&t, values1);
t->Invalidate();
ASSERT_TRUE(t->HasData());
}