diff --git a/example/ck_tile/99_toy_example/04_codegen_flash_attention_fwd/flash_attention_fwd.hpp b/example/ck_tile/99_toy_example/04_codegen_flash_attention_fwd/flash_attention_fwd.hpp index 01580b586d..b4eea2ad04 100644 --- a/example/ck_tile/99_toy_example/04_codegen_flash_attention_fwd/flash_attention_fwd.hpp +++ b/example/ck_tile/99_toy_example/04_codegen_flash_attention_fwd/flash_attention_fwd.hpp @@ -142,36 +142,90 @@ struct FlashAttentionFwd } }; -// TODO: change to only dec -template +struct flash_attention_fwd_traits_ +{ + using SaccDataType = ck_tile::remove_cvref_t; + using SMPLComputeDataType = ck_tile::remove_cvref_t; + using PDataType = ck_tile::remove_cvref_t; + using OaccDataType = ck_tile::remove_cvref_t; + + static constexpr index_t kBlockSize = kBlockSize_; + static constexpr index_t kHeadDim = kHeadDim_; + static constexpr index_t kM0PerBlock = kM0PerBlock_; + static constexpr index_t kN0PerBlock = kN0PerBlock_; + static constexpr index_t kK0PerBlock = kK0PerBlock_; + static constexpr index_t kN1PerBlock = kN1PerBlock_; + static constexpr index_t kK1PerBlock = kK1PerBlock_; + + static constexpr ck_tile::index_t kWarpPerCu = 8; // 2 warps per SIMD + static constexpr ck_tile::index_t kWarpPerBlock = kBlockSize / warpSize; + static constexpr ck_tile::index_t kBlockPerCu = kWarpPerCu / kWarpPerBlock; +}; + +// TODO: fwd_api.cpp, fwd_common.cpp +template -float flash_attention_fwd(const FlashAttnArgs& a, + index_t kBlockSize, + index_t kHeadDim, + index_t kM0PerBlock, + index_t kN0PerBlock, + index_t kK0PerBlock, + index_t kN1PerBlock, + index_t kK1PerBlock> +using traits_ = flash_attention_fwd_traits_; + +// Note: this internal API only declare, not define here, otherwise will block `make -j` +template +float flash_attention_fwd_(const FlashAttnArgs& a, + const ck_tile::stream_config& stream_config); + +// TODO: fwd_common.cpp +template +float flash_attention_fwd_(const FlashAttnArgs& a, const ck_tile::stream_config& stream_config) { - constexpr ck_tile::index_t kM0PerBlock = 128; - constexpr ck_tile::index_t kN0PerBlock = 128; - constexpr ck_tile::index_t kK0PerBlock = 32; - constexpr ck_tile::index_t kN1PerBlock = 128; - constexpr ck_tile::index_t kK1PerBlock = 32; - - constexpr ck_tile::index_t kBlockSize = 256; - constexpr ck_tile::index_t kHeadDim = 128; - - ck_tile::index_t kGridSize = a.Batch * (a.M0 / kM0PerBlock) * (a.N1 / kN1PerBlock); + using SaccDataType = typename Traits_::SaccDataType; + using SMPLComputeDataType = typename Traits_::SMPLComputeDataType; + using PDataType = typename Traits_::PDataType; + using OaccDataType = typename Traits_::OaccDataType; + + index_t kGridSize = a.Batch * (a.M0 / Traits_::kM0PerBlock) * (a.N1 / Traits_::kN1PerBlock); std::cout << "grid size " << kGridSize << std::endl; - constexpr ck_tile::index_t kWarpPerCu = 8; // 2 warps per SIMD - constexpr ck_tile::index_t kWarpPerBlock = kBlockSize / warpSize; - constexpr ck_tile::index_t kBlockPerCu = kWarpPerCu / kWarpPerBlock; - return ck_tile::launch_kernel(stream_config, - ck_tile::make_kernel( + ck_tile::make_kernel( ck_tile::FlashAttentionFwd{}, + Traits_::kBlockSize, + Traits_::kHeadDim, + Traits_::kM0PerBlock, + Traits_::kN0PerBlock, + Traits_::kK0PerBlock, + Traits_::kN1PerBlock, + Traits_::kK1PerBlock>{}, kGridSize, - kBlockSize, + Traits_::kBlockSize, 0, a.q_ptr, a.k_ptr, @@ -209,4 +263,44 @@ float flash_attention_fwd(const FlashAttnArgs +float flash_attention_fwd(const FlashAttnArgs& a, + const ck_tile::stream_config& stream_config) { + constexpr ck_tile::index_t kM0PerBlock = 128; + constexpr ck_tile::index_t kN0PerBlock = 128; + constexpr ck_tile::index_t kK0PerBlock = 32; + constexpr ck_tile::index_t kN1PerBlock = 128; + constexpr ck_tile::index_t kK1PerBlock = 32; + + constexpr ck_tile::index_t kBlockSize = 256; + constexpr ck_tile::index_t kHeadDim = 128; + + return flash_attention_fwd_> + (a, stream_config); + +} + } // namespace ck_tile