-
Notifications
You must be signed in to change notification settings - Fork 292
[CK Tile] contraction multi d - kernel & example #2901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
d4bc3a9
Initial commit. create batched_contraction_kernel file
msaffari-amd 15ec347
initial problem definition
msaffari-amd 7e731d4
implement initial example to launch kernel
msaffari-amd a440c72
add universal gemm to contraction. initial phase
msaffari-amd c9bf6f6
complete implementation for special case all Dims are 1 and no Ds
msaffari-amd dbaacf1
clean code
msaffari-amd 1bdc69f
initial changes to support multi dimensional G
msaffari-amd 751f229
more progress in implementing multiple G
msaffari-amd bc2fabe
tmp commit
msaffari-amd bd70caf
manage dynamic NumDimG in kernel
msaffari-amd baf2dd5
improving example for multi M,N,K,G handling. start generalizing kern…
msaffari-amd dc35e4f
implement the example for general Multi dimension G M N K and test di…
msaffari-amd 94f63bc
2 functions for reference using multi dimensional and flat indexing
msaffari-amd 4782643
clean the code for muti dimentional G, M, N, K contraction and add so…
msaffari-amd ad71007
Add Make descriptor function in kernel for merging Ms, Ns, Ks for A, …
msaffari-amd 1e65d79
some cleaning on kernel
msaffari-amd 9a1b0a3
clean the code for calculating the offsets from flatten batch number
msaffari-amd fdea5b8
Start adding MultiD support to kernel and example
msaffari-amd eaf377d
more changes to manage multi D in kernel and example
msaffari-amd 638d554
manage passing multi d to kernel and testing.
msaffari-amd a7e7156
complete multi D support in kernel. modify example code to support it
msaffari-amd 8384658
Correct algorithm to calc the correct offset values for D tensor batc…
msaffari-amd 131f166
Minor fix
msaffari-amd 5d83464
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
msaffari-amd 4f89943
Generalize example code for variable NumD tensors and apply cleanup b…
msaffari-amd a0e48cb
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
aosewski a3e0b29
Refactored code and addressed review feedback
msaffari-amd a09b54b
refactoring, cleaning, add documents, in kernel side and example codes
msaffari-amd 120c650
merge develop
msaffari-amd e2203ba
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
aosewski 617048c
Optimize batch offset calculation in kernel
msaffari-amd 01dfca2
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
msaffari-amd 2df6ce8
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
aosewski b430501
Inline CalculateBatchOffset in batched contraction kernel, update CHA…
msaffari-amd 143844f
Merge branch 'develop' into LWPCK-3688-cktile-contraction-multi-D
msaffari-amd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| add_executable(tile_example_batched_contraction EXCLUDE_FROM_ALL batched_contraction.cpp) | ||
| set(EXAMPLE_CONTRACTION_COMPILE_OPTIONS) | ||
| if(CK_USE_OCP_FP8) | ||
| list(APPEND EXAMPLE_CONTRACTION_COMPILE_OPTIONS -DCK_TILE_USE_OCP_FP8) | ||
| endif() | ||
|
|
||
| target_compile_options(tile_example_batched_contraction PRIVATE ${EXAMPLE_CONTRACTION_COMPILE_OPTIONS}) |
245 changes: 245 additions & 0 deletions
245
example/ck_tile/41_batched_contraction/batched_contraction.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| #include <hip/hip_runtime.h> | ||
|
|
||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <tuple> | ||
|
|
||
| #include "ck_tile/core.hpp" | ||
| #include "ck_tile/ops/epilogue.hpp" | ||
| #include "ck_tile/ops/gemm.hpp" | ||
| #include "ck_tile/host.hpp" | ||
|
|
||
| #include "ck_tile/ops/batched_contraction.hpp" | ||
| #include "contraction_utils.hpp" | ||
|
|
||
| template <typename ADataType, | ||
| typename BDataType, | ||
| typename DsDataType, | ||
| typename AccDataType, | ||
| typename EDataType, | ||
| typename ALayout, | ||
| typename BLayout, | ||
| typename DsLayout, | ||
| typename ELayout, | ||
| ck_tile::index_t NumDimG, | ||
| ck_tile::index_t NumDimM, | ||
| ck_tile::index_t NumDimN, | ||
| ck_tile::index_t NumDimK, | ||
| typename CDEElementWise = ck_tile::element_wise::PassThrough> | ||
|
|
||
| float batched_contraction_impl(const ck_tile::BatchedContractionHostArgs<DsDataType::size()>& args, | ||
| const ck_tile::stream_config& s) | ||
| { | ||
| constexpr ck_tile::index_t M_Tile = 256; | ||
| constexpr ck_tile::index_t N_Tile = 256; | ||
| constexpr ck_tile::index_t K_Tile = 64; | ||
|
|
||
| constexpr ck_tile::index_t M_Warp = 2; | ||
| constexpr ck_tile::index_t N_Warp = 2; | ||
| constexpr ck_tile::index_t K_Warp = 1; | ||
|
|
||
| constexpr ck_tile::index_t M_Warp_Tile = 32; | ||
| constexpr ck_tile::index_t N_Warp_Tile = 32; | ||
| constexpr ck_tile::index_t K_Warp_Tile = 16; | ||
|
|
||
| constexpr bool DoubleSmemBuffer = false; | ||
|
|
||
| constexpr bool kPadM = false; | ||
| constexpr bool kPadN = false; | ||
| constexpr bool kPadK = false; | ||
|
|
||
| constexpr bool TransposeC = false; | ||
|
|
||
| constexpr int kBlockPerCu = 1; | ||
| constexpr ck_tile::index_t TileParitionerGroupNum = 8; | ||
| constexpr ck_tile::index_t TileParitionerM01 = 4; | ||
|
|
||
| using GemmShape = | ||
| ck_tile::TileGemmShape<ck_tile::sequence<M_Tile, N_Tile, K_Tile>, | ||
| ck_tile::sequence<M_Warp, N_Warp, K_Warp>, | ||
| ck_tile::sequence<M_Warp_Tile, N_Warp_Tile, K_Warp_Tile>>; | ||
| using TilePartitioner = ck_tile:: | ||
| GemmSpatiallyLocalTilePartitioner<GemmShape, TileParitionerGroupNum, TileParitionerM01>; | ||
|
|
||
| using Traits = ck_tile::TileGemmTraits<kPadM, kPadN, kPadK, ALayout, BLayout, ELayout>; | ||
| using GemmUniversalTraits = ck_tile::TileGemmUniversalTraits<kPadM, | ||
| kPadN, | ||
| kPadK, | ||
| DoubleSmemBuffer, | ||
| ALayout, | ||
| BLayout, | ||
| ELayout, | ||
| TransposeC>; | ||
|
|
||
| using Problem = ck_tile::BatchedContractionProblem<ADataType, | ||
| BDataType, | ||
| DsDataType, | ||
| EDataType, | ||
| NumDimG, // NumDimG | ||
| NumDimM, // NumDimM | ||
| NumDimN, // NumDimN | ||
| NumDimK, // NumDimK | ||
| DsDataType::size() // NumDTensor | ||
| >; | ||
|
|
||
| using GemmPipelineProblem = | ||
| ck_tile::GemmPipelineProblem<ADataType, BDataType, AccDataType, GemmShape, Traits>; | ||
|
|
||
| using BaseGemmPipeline = UNIVERSAL_GEMM_PIPELINE<GemmPipelineProblem>; | ||
|
|
||
| ck_tile::index_t K_total = 1; | ||
| for(ck_tile::index_t i = NumDimG + NumDimM; i < NumDimG + NumDimM + NumDimK; ++i) | ||
| { | ||
| K_total *= args.A_dims[i]; | ||
| } | ||
|
|
||
| const ck_tile::index_t num_loop = TilePartitioner::GetLoopNum(K_total); | ||
| const bool has_hot_loop = BaseGemmPipeline::BlockHasHotloop(num_loop); | ||
| const ck_tile::TailNumber tail_num = BaseGemmPipeline::GetBlockLoopTailNum(num_loop); | ||
|
|
||
| float ave_time{0}; | ||
|
|
||
| const auto Run = [&](const auto has_hot_loop_, const auto tail_number_) { | ||
| constexpr bool has_hot_loop_v = has_hot_loop_.value; | ||
| constexpr auto tail_number_v = tail_number_.value; | ||
| constexpr auto scheduler = GEMM_PIPELINE_SCHEDULER; | ||
| constexpr auto memory_operation = | ||
| ck_tile::memory_operation_enum::set; // Always set (no atomic_add) | ||
|
|
||
| using UniversalGemmProblem = ck_tile::UniversalGemmPipelineProblem<ADataType, | ||
| BDataType, | ||
| AccDataType, | ||
| GemmShape, | ||
| GemmUniversalTraits, | ||
| scheduler, | ||
| has_hot_loop_v, | ||
| tail_number_v>; | ||
|
|
||
| using GemmPipeline = GEMM_PIPELINE<UniversalGemmProblem>; | ||
|
|
||
| using GemmEpilogue = ck_tile::CShuffleEpilogue< | ||
| ck_tile::CShuffleEpilogueProblem<ADataType, | ||
| BDataType, | ||
| DsDataType, | ||
| AccDataType, | ||
| EDataType, | ||
| DsLayout, | ||
| ELayout, | ||
| CDEElementWise, | ||
| TilePartitioner::MPerBlock, | ||
| TilePartitioner::NPerBlock, | ||
| M_Warp, | ||
| N_Warp, | ||
| M_Warp_Tile, | ||
| N_Warp_Tile, | ||
| K_Warp_Tile, | ||
| UniversalGemmProblem::TransposeC, | ||
| memory_operation>>; | ||
|
|
||
| using Kernel = | ||
| ck_tile::BatchedContractionKernel<Problem, TilePartitioner, GemmPipeline, GemmEpilogue>; | ||
| auto kargs = Kernel::MakeKernelArgs(args); | ||
|
|
||
| const dim3 grids = Kernel::GridSize(kargs); | ||
| const dim3 blocks = Kernel::GetBlockSize(); | ||
|
|
||
| if(!Kernel::IsSupportedArguments(kargs)) | ||
| { | ||
| throw std::runtime_error("Wrong! Arguments not supported! Skipping contraction!\n"); | ||
| } | ||
|
|
||
| if(s.log_level_ > 0) | ||
| { | ||
| std::cout << "Launching kernel with args: " << Kernel::GetKernelName() << '\n' | ||
| << "shape: " << GemmShape::GetName() << '\n' | ||
| << "problem: " << GemmPipelineProblem::GetName() << '\n' | ||
| << "pipeline: " << GemmPipeline::GetName() << '\n' | ||
| << "grid: {" << grids.x << ", " << grids.y << ", " << grids.z << "}" | ||
| << ", blocks: {" << blocks.x << ", " << blocks.y << ", " << blocks.z << "}" | ||
| << std::endl; | ||
| } | ||
|
|
||
| auto kernel = ck_tile::make_kernel<kBlockPerCu>(Kernel{}, grids, blocks, 0, kargs); | ||
|
|
||
| ave_time = ck_tile::launch_kernel(s, kernel); | ||
|
|
||
| return ave_time; | ||
| }; | ||
|
|
||
| BaseGemmPipeline::TailHandler(Run, has_hot_loop, tail_num); | ||
|
|
||
| return ave_time; | ||
| } | ||
|
|
||
| #define HANDLE_CASE(G, M, N, K) \ | ||
| if(num_g_dims == G && num_m_dims == M && num_n_dims == N && num_k_dims == K) \ | ||
| { \ | ||
| return batched_contraction_impl<ADataType, \ | ||
| BDataType, \ | ||
| DsDataType, \ | ||
| AccDataType, \ | ||
| EDataType, \ | ||
| ALayout, \ | ||
| BLayout, \ | ||
| DsLayout, \ | ||
| ELayout, \ | ||
| G, \ | ||
| M, \ | ||
| N, \ | ||
| K, \ | ||
| CDEElementWise>(args, s); \ | ||
| } | ||
|
|
||
| template <typename ADataType, | ||
| typename BDataType, | ||
| typename DsDataType, | ||
| typename AccDataType, | ||
| typename EDataType, | ||
| typename ALayout, | ||
| typename BLayout, | ||
| typename DsLayout, | ||
| typename ELayout, | ||
| typename CDEElementWise = ck_tile::element_wise::PassThrough> | ||
| float batched_contraction(const ck_tile::BatchedContractionHostArgs<DsDataType::size()>& args, | ||
| const ck_tile::stream_config& s, | ||
| ck_tile::index_t num_g_dims, | ||
| ck_tile::index_t num_m_dims, | ||
| ck_tile::index_t num_n_dims, | ||
| ck_tile::index_t num_k_dims) | ||
| { | ||
| std::cout << "Dimensions: G=" << num_g_dims << ", M=" << num_m_dims << ", N=" << num_n_dims | ||
| << ", K=" << num_k_dims << std::endl; | ||
|
|
||
| HANDLE_CASE(1, 1, 1, 1); | ||
| HANDLE_CASE(2, 1, 1, 1); | ||
| HANDLE_CASE(2, 2, 2, 1); | ||
| HANDLE_CASE(1, 2, 1, 1); | ||
| HANDLE_CASE(1, 1, 1, 2); | ||
| HANDLE_CASE(2, 2, 2, 2); | ||
| HANDLE_CASE(4, 4, 4, 4); | ||
|
|
||
| throw std::runtime_error( | ||
| "Unsupported dimension combination: G=" + std::to_string(num_g_dims) + | ||
| ", M=" + std::to_string(num_m_dims) + ", N=" + std::to_string(num_n_dims) + | ||
| ", K=" + std::to_string(num_k_dims) + ". Please add this combination to the kernel."); | ||
| } | ||
|
|
||
| #include "run_batched_contraction_example.inc" | ||
|
|
||
| int main(int argc, char* argv[]) | ||
| { | ||
| try | ||
| { | ||
| return !run_batched_contraction_example(argc, argv); | ||
| } | ||
| catch(const std::runtime_error& e) | ||
| { | ||
| std::cerr << "Runtime error: " << e.what() << '\n'; | ||
| return EXIT_FAILURE; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.