mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-02 12:41:26 +00:00
This pull request builds on #3267 by proving the "validation" infrastructure, the means to compare a set of `Outputs`. The design of the validation infrastructure is relatively straight forward: - Each SIGNATURE should come with a `validate()` implementation, which should be implemented in a similar way that the other functions/types from `testing.hpp` are implemented. - `validate()` returns a `ValidationReport`, which is a structure that keeps all relevant information about comparing the tensors from two `Outputs`. Note that crucially, `validate()` should not do any reporting by itself. Rather, glue logic should be implemented by the user to turn `ValidationReport` into a relevant error message. - You can see this clue code for CK-Builder itself in `testing_utils.hpp`, its `MatchesReference()`. This functionality is relatively barebones right now, it will be expanded upon in a different PR to keep the scope of this one down. The comparison is done on the GPU (using an atomic for now), to keep tests relatively quick. Some notable items from this PR: - To help compare the tensors and with writing tests, I've written a generic function `tensor_foreach` which invokes a callback on every element of a tensor. - For that it was useful that the `TensorDescriptor` has a rank which is known at compile-time, so I've changed the implementation of `TensorDescriptor` for that. I felt like it was a better approach than keeping it dynamic, for multiple reasons: - This is C++ and we should use static typing where possible and useful. This way, we don't have to implement runtime assertions about the tensor rank. - We know already know the rank of tensors statically, as it can be derived from the SIGNATURE. - It simpifies the implementation of `tensor_foreach` and other comparison code. - There are a lot of new tests for validating the validation implementation, validating validation validation tests (Only 3 recursive levels though...). For a few of those functions, I felt like it would be useful to expose them to the user. - Doc comments everywhere.
173 lines
6.1 KiB
C++
173 lines
6.1 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "ck_tile/builder/testing/tensor_descriptor.hpp"
|
|
#include "testing_utils.hpp"
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include <array>
|
|
#include <vector>
|
|
|
|
namespace ckb = ck_tile::builder;
|
|
namespace ckt = ck_tile::builder::test;
|
|
|
|
using ::testing::ElementsAreArray;
|
|
using ::testing::Eq;
|
|
using ::testing::Throws;
|
|
|
|
TEST(TensorDescriptor, Basic)
|
|
{
|
|
constexpr auto dt = ckb::DataType::FP16;
|
|
constexpr size_t rank = 3;
|
|
ckt::Extent lengths = {123, 456, 789};
|
|
ckt::Extent strides = {456 * 789, 789, 1};
|
|
|
|
ckt::TensorDescriptor<dt, rank> descriptor(lengths, strides);
|
|
|
|
EXPECT_THAT(descriptor.get_lengths(), ElementsAreArray(lengths));
|
|
EXPECT_THAT(descriptor.get_strides(), ElementsAreArray(strides));
|
|
}
|
|
|
|
TEST(TensorDescriptor, ComputeSize)
|
|
{
|
|
constexpr auto dt = ckb::DataType::FP32;
|
|
constexpr size_t rank = 3;
|
|
ckt::Extent lengths = {305, 130, 924};
|
|
ckt::Extent strides = {1001 * 1000, 1, 1000};
|
|
|
|
ckt::TensorDescriptor<dt, rank> descriptor(lengths, strides);
|
|
|
|
// Compute the location of the last item in memory,
|
|
// then add one to get the minimum size.
|
|
size_t expected_size = 1;
|
|
size_t expected_numel = 1;
|
|
for(size_t i = 0; i < lengths.size(); ++i)
|
|
{
|
|
expected_size += (lengths[i] - 1) * strides[i];
|
|
expected_numel *= lengths[i];
|
|
}
|
|
|
|
EXPECT_THAT(descriptor.get_element_size(), Eq(expected_numel));
|
|
EXPECT_THAT(descriptor.get_element_space_size(), Eq(expected_size));
|
|
EXPECT_THAT(descriptor.get_element_space_size_in_bytes(),
|
|
Eq(expected_size * ckt::data_type_sizeof(dt)));
|
|
}
|
|
|
|
TEST(TensorDescriptor, PackedRightLayout)
|
|
{
|
|
const ckt::Extent lengths = {5125, 623, 1177, 1534};
|
|
const auto strides = ckt::PackedRightLayout{}(lengths);
|
|
|
|
EXPECT_THAT(strides, ElementsAreArray({623 * 1177 * 1534, 1177 * 1534, 1534, 1}));
|
|
}
|
|
|
|
TEST(TensorDescriptor, PackedLeftLayout)
|
|
{
|
|
const ckt::Extent lengths = {4, 15, 925, 662, 1462};
|
|
const auto strides = ckt::PackedLeftLayout{}(lengths);
|
|
|
|
EXPECT_THAT(strides, ElementsAreArray({1, 4, 4 * 15, 4 * 15 * 925, 4 * 15 * 925 * 662}));
|
|
}
|
|
|
|
TEST(TensorDescriptor, MakeDescriptor)
|
|
{
|
|
{
|
|
const ckt::Extent lengths = {10, 11, 12, 13, 14};
|
|
|
|
// Note: automatic inference of RANK.
|
|
const auto desc =
|
|
ckt::make_descriptor<ckb::DataType::INT32>(lengths, ckt::PackedRightLayout{});
|
|
|
|
EXPECT_THAT(desc.get_lengths(), ElementsAreArray(lengths));
|
|
EXPECT_THAT(desc.get_strides(),
|
|
ElementsAreArray({11 * 12 * 13 * 14, 12 * 13 * 14, 13 * 14, 14, 1}));
|
|
}
|
|
|
|
{
|
|
const ckt::Extent lengths = {4, 3, 2};
|
|
const ckt::Extent strides = {60, 1, 7};
|
|
|
|
// Note: automatic inference of RANK.
|
|
const auto desc = ckt::make_descriptor<ckb::DataType::FP8>(lengths, strides);
|
|
|
|
EXPECT_THAT(desc.get_lengths(), ElementsAreArray(lengths));
|
|
EXPECT_THAT(desc.get_strides(), ElementsAreArray(strides));
|
|
}
|
|
}
|
|
|
|
TEST(TensorDescriptor, GetSpaceDescriptor)
|
|
{
|
|
{
|
|
const auto desc = ckt::make_descriptor<ckb::DataType::FP32>(ckt::Extent{4, 4, 4},
|
|
ckt::PackedLeftLayout{});
|
|
const auto space = desc.get_space_descriptor();
|
|
|
|
const auto expected = 4 * 4 * 4;
|
|
|
|
EXPECT_THAT(decltype(space)::data_type, Eq(ckb::DataType::FP32));
|
|
EXPECT_THAT(decltype(space)::rank, Eq(1));
|
|
|
|
EXPECT_THAT(decltype(space)::data_type, Eq(ckb::DataType::FP32));
|
|
EXPECT_THAT(decltype(space)::rank, Eq(1));
|
|
EXPECT_THAT(space.get_lengths(), ElementsAreArray({expected}));
|
|
EXPECT_THAT(space.get_strides(), ElementsAreArray({1}));
|
|
EXPECT_THAT(space.get_element_size(), Eq(expected));
|
|
EXPECT_THAT(space.get_element_space_size(), Eq(expected));
|
|
}
|
|
|
|
{
|
|
const ckt::Extent lengths = {6, 3, 4};
|
|
const ckt::Extent strides = {102, 1, 2002};
|
|
const auto desc = ckt::make_descriptor<ckb::DataType::FP32>(lengths, strides);
|
|
const auto space = desc.get_space_descriptor();
|
|
|
|
// Compute the location of the last item in memory,
|
|
// then add one to get the minimum size.
|
|
size_t expected_size = 1;
|
|
for(size_t i = 0; i < lengths.size(); ++i)
|
|
{
|
|
expected_size += (lengths[i] - 1) * strides[i];
|
|
}
|
|
|
|
EXPECT_THAT(decltype(space)::data_type, Eq(ckb::DataType::FP32));
|
|
EXPECT_THAT(decltype(space)::rank, Eq(1));
|
|
EXPECT_THAT(space.get_lengths(), ElementsAreArray({expected_size}));
|
|
EXPECT_THAT(space.get_strides(), ElementsAreArray({1}));
|
|
EXPECT_THAT(space.get_element_size(), Eq(expected_size));
|
|
EXPECT_THAT(space.get_element_space_size(), Eq(expected_size));
|
|
}
|
|
}
|
|
|
|
TEST(TensorDescriptor, EmptyExtent)
|
|
{
|
|
// A rank-0 tensor points to a single element
|
|
const auto desc = ckt::make_descriptor<ckb::DataType::FP16>(ckt::Extent{}, ckt::Extent{});
|
|
EXPECT_THAT(decltype(desc)::rank, Eq(0));
|
|
EXPECT_THAT(desc.get_lengths().size(), Eq(0));
|
|
EXPECT_THAT(desc.get_strides().size(), Eq(0));
|
|
EXPECT_THAT(desc.get_element_size(), Eq(1));
|
|
EXPECT_THAT(desc.get_element_space_size(), Eq(1));
|
|
EXPECT_THAT(desc.get_element_space_size_in_bytes(), Eq(2));
|
|
|
|
// We expect a rank-1 tensor with the one dimension being 1.
|
|
const auto space = desc.get_space_descriptor();
|
|
|
|
const auto expected = 1;
|
|
|
|
EXPECT_THAT(decltype(space)::rank, Eq(1));
|
|
EXPECT_THAT(space.get_lengths(), ElementsAreArray({expected}));
|
|
EXPECT_THAT(space.get_strides(), ElementsAreArray({1}));
|
|
EXPECT_THAT(space.get_element_size(), Eq(expected));
|
|
EXPECT_THAT(space.get_element_space_size(), Eq(expected));
|
|
EXPECT_THAT(space.get_element_space_size_in_bytes(), Eq(2));
|
|
}
|
|
|
|
TEST(TensorDescriptor, ExtentFromVector)
|
|
{
|
|
EXPECT_THAT(ckt::Extent<4>::from_vector(std::vector<size_t>{1, 2, 3, 4}),
|
|
ElementsAreArray({1, 2, 3, 4}));
|
|
|
|
EXPECT_THAT([] { return ckt::Extent<5>::from_vector(std::vector<size_t>{1, 2}); },
|
|
Throws<std::runtime_error>());
|
|
}
|