mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-03-15 20:57:40 +00:00
Add generate_identity_sequences helper and replace lambdas with named functors (#4828) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Add `generate_identity_sequences<N>()` helper that returns `Tuple<Sequence<0>, Sequence<1>, ..., Sequence<N-1>>` - Replace lambdas with named functors in `transform_tensor_descriptor` - Add `unpack_and_merge_sequences` helper functor - Reduces `transform_tensor_descriptor` instantiations from 388 to 32 (92% reduction) ## Motivation Multiple call sites use `generate_tuple([](auto i) { return Sequence<i>{}; }, Number<N>{})` pattern. A named helper reduces lambda instantiations. Additionally, each lambda in `transform_tensor_descriptor` creates a unique closure type, causing the function to be instantiated separately for every call site. Named functors share a single type, so the compiler reuses the same instantiation. ## Changes ### Part 1: generate_identity_sequences helper - Replaces common lambda pattern for generating identity sequences - Each lambda expression creates a unique closure type, causing separate template instantiations at every call site - Named helper shares a single type across all uses ### Part 2: Named functors in transform_tensor_descriptor - Add `unpack_and_merge_sequences` helper to replace lambda in `GetNumOfHiddenDimension` - Use `generate_identity_sequences` in `matrix_padder.hpp` ## Test Plan - [x] Added 7 unit tests: - 4 tests for `generate_identity_sequences` - 3 tests for `unpack_and_merge_sequences` - [ ] Waiting for full CI ## Related PRs This PR merges the functionality from: - ROCm/composable_kernel#3588 (generate_identity_sequences helper) - ROCm/composable_kernel#3589 (Named functors in transform_tensor_descriptor) Part of PR stack for issue #4229 (Reduce CK/CKTile Build Times) **Note:** This PR supersedes #4283, ROCm/composable_kernel#3588 and ROCm/composable_kernel#3589, which can be closed once this is merged.
66 lines
2.3 KiB
C++
66 lines
2.3 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include <gtest/gtest.h>
|
|
#include "ck/utility/sequence_helper.hpp"
|
|
#include "ck/utility/tuple_helper.hpp"
|
|
|
|
using namespace ck;
|
|
|
|
// Tests for generate_identity_sequences (PR #3588)
|
|
TEST(GenerateIdentitySequences, Size5)
|
|
{
|
|
auto result = generate_identity_sequences(Number<5>{});
|
|
auto expected =
|
|
make_tuple(Sequence<0>{}, Sequence<1>{}, Sequence<2>{}, Sequence<3>{}, Sequence<4>{});
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|
|
|
|
TEST(GenerateIdentitySequences, Size1)
|
|
{
|
|
auto result = generate_identity_sequences(Number<1>{});
|
|
auto expected = make_tuple(Sequence<0>{});
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|
|
|
|
TEST(GenerateIdentitySequences, Size0)
|
|
{
|
|
auto result = generate_identity_sequences(Number<0>{});
|
|
auto expected = make_tuple();
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|
|
|
|
TEST(GenerateIdentitySequences, WithNumber)
|
|
{
|
|
constexpr auto result = generate_identity_sequences(Number<3>{});
|
|
EXPECT_EQ(result.Size(), 3);
|
|
EXPECT_TRUE((is_same<decltype(result.At(Number<0>{})), const Sequence<0>&>::value));
|
|
EXPECT_TRUE((is_same<decltype(result.At(Number<1>{})), const Sequence<1>&>::value));
|
|
EXPECT_TRUE((is_same<decltype(result.At(Number<2>{})), const Sequence<2>&>::value));
|
|
}
|
|
|
|
// Tests for unpack_and_merge_sequences (PR #3589)
|
|
TEST(UnpackAndMergeSequences, MergeMultipleSequences)
|
|
{
|
|
auto input = make_tuple(Sequence<1, 2>{}, Sequence<3, 4>{}, Sequence<5, 6>{});
|
|
auto result = unpack_and_merge_sequences(input);
|
|
auto expected = Sequence<1, 2, 3, 4, 5, 6>{};
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|
|
|
|
TEST(UnpackAndMergeSequences, SingleSequence)
|
|
{
|
|
auto input = make_tuple(Sequence<10, 20, 30>{});
|
|
auto result = unpack_and_merge_sequences(input);
|
|
auto expected = Sequence<10, 20, 30>{};
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|
|
|
|
TEST(UnpackAndMergeSequences, TwoSequences)
|
|
{
|
|
auto input = make_tuple(Sequence<100>{}, Sequence<200, 300>{});
|
|
auto result = unpack_and_merge_sequences(input);
|
|
auto expected = Sequence<100, 200, 300>{};
|
|
EXPECT_TRUE((is_same<decltype(result), decltype(expected)>::value));
|
|
}
|