From 4e44f5da0a7035ad3632f1b47f1d179b1a313c48 Mon Sep 17 00:00:00 2001 From: Uros Marinkovic Date: Thu, 18 Dec 2025 15:07:54 +0000 Subject: [PATCH 1/4] optimize fillWithValue for packed tensors --- .../sdk/include/hipdnn_sdk/utilities/Tensor.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp index 8cb6310f41e..3860590cd63 100644 --- a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp +++ b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp @@ -465,9 +465,17 @@ class Tensor : public TensorBase void fillWithValue(T value) override { _memory.markHostModified(); - iterateAlongDimensions(_dims, [&](const std::vector& indices) { - this->setHostValue(value, indices); - }); + if(isPacked()) + { + auto data{_memory.hostData()}; + std::fill(data, data + _element_count, value); + } + else + { + iterateAlongDimensions(_dims, [&](const std::vector& indices) { + this->setHostValue(value, indices); + }); + } } void fillWithRandomValues(T min, T max, unsigned int seed = std::random_device{}()) override From 7d040b8beeb58488800bfc2eb0a24493b1aee621 Mon Sep 17 00:00:00 2001 From: Uros Marinkovic Date: Thu, 18 Dec 2025 16:25:36 +0000 Subject: [PATCH 2/4] optimize fillWithRandomValues for packed tensors --- .../include/hipdnn_sdk/utilities/Tensor.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp index 3860590cd63..cd5a5b1c8d0 100644 --- a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp +++ b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp @@ -486,9 +486,21 @@ class Tensor : public TensorBase static_cast(max)); _memory.markHostModified(); - iterateAlongDimensions(_dims, [&](const std::vector& indices) { - this->setHostValue(static_cast(distribution(generator)), indices); - }); + + if(isPacked()) + { + auto data{_memory.hostData()}; + for(size_t i{0}; i < _element_count; i++) + { + data[i] = static_cast(distribution(generator)); + } + } + else + { + iterateAlongDimensions(_dims, [&](const std::vector& indices) { + this->setHostValue(static_cast(distribution(generator)), indices); + }); + } } bool isPacked() const override From f8128df33464f3c35f5a13ebee55b6c1223e22f2 Mon Sep 17 00:00:00 2001 From: Uros Marinkovic Date: Thu, 18 Dec 2025 17:28:32 +0000 Subject: [PATCH 3/4] fix typo in element count --- projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp index cd5a5b1c8d0..74b5f9b7421 100644 --- a/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp +++ b/projects/hipdnn/sdk/include/hipdnn_sdk/utilities/Tensor.hpp @@ -468,7 +468,7 @@ class Tensor : public TensorBase if(isPacked()) { auto data{_memory.hostData()}; - std::fill(data, data + _element_count, value); + std::fill(data, data + _elementCount, value); } else { @@ -490,7 +490,7 @@ class Tensor : public TensorBase if(isPacked()) { auto data{_memory.hostData()}; - for(size_t i{0}; i < _element_count; i++) + for(size_t i{0}; i < _elementCount; i++) { data[i] = static_cast(distribution(generator)); } From 0d2f62929c095e863f695da30bb085af27220fef Mon Sep 17 00:00:00 2001 From: Uros Marinkovic Date: Mon, 22 Dec 2025 15:38:53 +0000 Subject: [PATCH 4/4] Added tests for non-packed codepaths --- .../hipdnn/sdk/tests/utilities/TestTensor.cpp | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/projects/hipdnn/sdk/tests/utilities/TestTensor.cpp b/projects/hipdnn/sdk/tests/utilities/TestTensor.cpp index 673ff62b913..822c8e4a498 100644 --- a/projects/hipdnn/sdk/tests/utilities/TestTensor.cpp +++ b/projects/hipdnn/sdk/tests/utilities/TestTensor.cpp @@ -39,7 +39,7 @@ TEST(TestTensor, BasicRowMajorUsage) EXPECT_EQ(tensor.strides()[3], 1); } -TEST(TestTensor, FillWithValues) +TEST(TestTensor, FillWithValuesPacked) { Tensor tensor({1, 2, 3, 4}); @@ -52,7 +52,19 @@ TEST(TestTensor, FillWithValues) } } -TEST(TestTensor, FillWithRandomValues) +TEST(TestTensor, FillWithValuesNonPacked) +{ + Tensor tensor({1, 2, 3, 4}, {30, 15, 5, 1}); + + tensor.fillWithValue(1.0f); + + for(auto it{tensor.cbegin()}; it != tensor.cend(); ++it) + { + EXPECT_FLOAT_EQ(1.0f, (*reinterpret_cast((*it)))); + } +} + +TEST(TestTensor, FillWithRandomValuesPacked) { Tensor tensor({1, 2, 3, 4}); @@ -66,6 +78,20 @@ TEST(TestTensor, FillWithRandomValues) } } +TEST(TestTensor, FillWithRandomValuesNonPacked) +{ + Tensor tensor({1, 2, 3, 4}, {30, 15, 5, 1}); + + tensor.fillWithRandomValues(1.0f, 3.0f); + + for(auto it{tensor.cbegin()}; it != tensor.cend(); ++it) + { + auto val{(*reinterpret_cast((*it)))}; + EXPECT_GE(val, 1.0f); + EXPECT_LE(val, 3.0f); + } +} + TEST(TestTensor, BasicNhwcUsage) { Tensor tensor({1, 2, 3, 4}, TensorLayout::NHWC);