Files
TheRock/rocm-libraries/dnn-providers/integration-tests/tests/gpu-ref/TestShallowGpuTensor.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

135 lines
4.2 KiB
C++

// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <gtest/gtest.h>
#include <hipdnn-gpu-ref/ShallowGpuTensor.hpp>
#include <hipdnn_data_sdk/utilities/Workspace.hpp>
#include <hipdnn_test_sdk/utilities/TestUtilities.hpp>
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <stdexcept>
#include <vector>
using hipdnn_data_sdk::utilities::MemoryLocation;
using hipdnn_data_sdk::utilities::Workspace;
using hipdnn_gpu_ref::ShallowGpuTensor;
namespace
{
size_t computeElementSpace(const std::vector<int64_t>& dims, const std::vector<int64_t>& strides)
{
return static_cast<size_t>(
std::inner_product(dims.begin(),
dims.end(),
strides.begin(),
int64_t{1},
std::plus<>(),
[](int64_t len, int64_t stride) { return (len - 1) * stride; }));
}
} // namespace
TEST(TestGpuShallowTensor, PackedTensorReportsCorrectShape)
{
SKIP_IF_NO_DEVICES();
const std::vector<int64_t> dims = {1, 3, 2, 2};
const std::vector<int64_t> strides = {12, 4, 2, 1};
const auto elementSpace = computeElementSpace(dims, strides);
const Workspace workspace(elementSpace * sizeof(float));
const ShallowGpuTensor<float> tensor(workspace.get(), dims, strides);
EXPECT_EQ(tensor.dims(), dims);
EXPECT_EQ(tensor.strides(), strides);
EXPECT_TRUE(tensor.isPacked());
EXPECT_EQ(tensor.elementCount(), 12u);
EXPECT_EQ(tensor.elementSpace(), elementSpace);
}
TEST(TestGpuShallowTensor, NonPackedTensorReportsCorrectElementSpace)
{
SKIP_IF_NO_DEVICES();
const std::vector<int64_t> dims = {2, 2, 2, 2};
const std::vector<int64_t> strides = {2, 4, 8, 16};
const auto elementSpace = computeElementSpace(dims, strides);
const Workspace workspace(elementSpace * sizeof(float));
const ShallowGpuTensor<float> tensor(workspace.get(), dims, strides);
EXPECT_EQ(tensor.elementCount(), 16u);
EXPECT_EQ(tensor.elementSpace(), elementSpace);
EXPECT_FALSE(tensor.isPacked());
}
TEST(TestGpuShallowTensor, MemoryExposesDevicePointer)
{
SKIP_IF_NO_DEVICES();
const Workspace workspace(4 * sizeof(float));
ShallowGpuTensor<float> tensor(workspace.get(), {1, 1, 2, 2}, {4, 4, 2, 1});
EXPECT_EQ(tensor.memory().deviceData(), workspace.get());
EXPECT_EQ(tensor.memory().location(), MemoryLocation::DEVICE);
}
TEST(TestGpuShallowTensor, HostFillOperationsThrow)
{
SKIP_IF_NO_DEVICES();
const Workspace workspace(4 * sizeof(float));
ShallowGpuTensor<float> tensor(workspace.get(), {1, 1, 2, 2}, {4, 4, 2, 1});
EXPECT_THROW(tensor.fillWithValue(1.0f), std::runtime_error);
EXPECT_THROW(tensor.fillWithRandomValues(-1.0f, 1.0f, 42), std::runtime_error);
std::array<float, 4> hostData = {1.0f, 2.0f, 3.0f, 4.0f};
EXPECT_THROW(tensor.fillWithData(hostData.data(), sizeof(hostData)), std::runtime_error);
}
TEST(TestGpuShallowTensor, HostMemoryAccessThrows)
{
SKIP_IF_NO_DEVICES();
const Workspace workspace(4 * sizeof(float));
ShallowGpuTensor<float> tensor(workspace.get(), {1, 1, 2, 2}, {4, 4, 2, 1});
auto& mem = tensor.memory();
EXPECT_THROW(mem.hostData(), std::runtime_error);
EXPECT_THROW(mem.hostDataAsync(), std::runtime_error);
EXPECT_THROW(mem.markHostModified(), std::runtime_error);
}
TEST(TestGpuShallowTensor, MoveConstruction)
{
SKIP_IF_NO_DEVICES();
const std::vector<int64_t> dims = {1, 1, 3, 3};
const std::vector<int64_t> strides = {9, 9, 3, 1};
const auto elementSpace = computeElementSpace(dims, strides);
const Workspace workspace(elementSpace * sizeof(float));
ShallowGpuTensor<float> source(workspace.get(), dims, strides);
ShallowGpuTensor<float> dest(std::move(source));
EXPECT_EQ(dest.dims(), dims);
EXPECT_EQ(dest.strides(), strides);
EXPECT_EQ(dest.memory().deviceData(), workspace.get());
EXPECT_EQ(dest.elementCount(), 9u);
// Source memory is emptied — intentionally inspecting moved-from state
EXPECT_EQ(source.memory().deviceData(), nullptr); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(source.memory().count(), 0u);
EXPECT_TRUE(source.memory().empty());
}