Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,65 @@ class TensorAttributes
return {ErrorCode::OK, ""};
}

/// @brief Checks if two tensors are logically identical in terms of shape,
/// layout, data type, value, and structural role in the graph.
/// @note This intentionally ignores the human-readable string name.
bool logicallyEquals(const TensorAttributes& other) const
{
if(this->_dataType != other._dataType)
{
return false;
}
if(this->_dim != other._dim)
{
return false;
}
if(this->_stride != other._stride)
{
return false;
}
if(this->_isVirtual != other._isVirtual)
{
return false;
}
// Compare pass-by-value scalar variants
if(this->_value != other._value)
{
return false;
}

return true;
}

/// @brief Absolute equality check including non-functional metadata like names.
bool operator==(const TensorAttributes& other) const
{

if(!logicallyEquals(other))
{
return false;
}
if(this->_name != other._name)
{
return false;
}
if(this->_uidSet != other._uidSet)
{
return false;
}
if(this->_uidSet && (this->_uid != other._uid))
{
return false;
}

return true;
}

bool operator!=(const TensorAttributes& other) const
{
return !(*this == other);
}

private:
int64_t _uid = 0;
bool _uidSet = false;
Expand Down
39 changes: 39 additions & 0 deletions projects/hipdnn/frontend/tests/TestTensorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,42 @@ TEST(TestTensorAttributes, ValidateDataType)
EXPECT_EQ(result.code, errorCode) << "For " + std::string(to_string(dataType));
}
}

TEST(TestTensorAttributes, TensorLogicalAndStrictEquality)
{
TensorAttributes tensorA;
tensorA.set_dim({1, 64, 28, 28});
tensorA.set_stride({50176, 784, 28, 1});
tensorA.set_data_type(DataType::HALF);
tensorA.set_uid(1);
tensorA.set_name("Tensor_A");

TensorAttributes tensorB;
tensorB.set_dim({1, 64, 28, 28});
tensorB.set_stride({50176, 784, 28, 1});
tensorB.set_data_type(DataType::HALF);
tensorB.set_uid(2);
tensorB.set_name("Tensor_B");

EXPECT_TRUE(tensorA.logicallyEquals(tensorB));
EXPECT_TRUE(tensorB.logicallyEquals(tensorA));

EXPECT_FALSE(tensorA == tensorB);
EXPECT_TRUE(tensorA != tensorB);

tensorB.set_name("Tensor_A");
tensorB.set_uid(1);
EXPECT_TRUE(tensorA == tensorB);
EXPECT_FALSE(tensorA != tensorB);

tensorB.set_is_virtual(true);
EXPECT_FALSE(tensorA.logicallyEquals(tensorB));
EXPECT_FALSE(tensorA == tensorB);

const TensorAttributes scalarA(2.5f);
const TensorAttributes scalarB(2.5f);
const TensorAttributes scalarC(3.5f);

EXPECT_TRUE(scalarA.logicallyEquals(scalarB));
EXPECT_FALSE(scalarA.logicallyEquals(scalarC));
}
Loading