Instructions to use replicate/flashinfer-draft with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/flashinfer-draft with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/flashinfer-draft", version=1) - Notebooks
- Google Colab
- Kaggle
Download include/flashinfer/attention/hopper.cuh from replicate/flashinfer-draft: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/replicate/flashinfer-draft/resolve/main/include/flashinfer/attention/hopper.cuh
- Command line
-
hf download hf://replicate/flashinfer-draft/include/flashinfer/attention/hopper.cuh
-
curl -L -o hopper.cuh https://huggingface.co/replicate/flashinfer-draft/resolve/main/include/flashinfer/attention/hopper.cuh
13.5 kB
| /* | |
| * Copyright (c) 2023 by FlashInfer team. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| namespace flashinfer { | |
| using namespace cute::SM90::GMMA; | |
| // using WGMMA_NN_64x32x16_F32BF16BF16_SS = | |
| template <typename DTypeIn, typename DTypeOut, int M, int N, int K, Major TransposeA, | |
| Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS {}; | |
| template <typename DTypeIn, typename DTypeOut, int M, int N, int K, Major TransposeA, | |
| Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS {}; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS<__half, float, 64, 16, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint32_t* a_frag, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x16x16_F32F16F16_RS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| EXPAND_FRAG_ARGS_4(a_frag), desc_b, EXPAND_FRAG_ARGS_8(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__half, float, 64, 16, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x16x16_F32F16F16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_8(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS<__half, float, 64, 32, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint32_t* a_frag, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x32x16_F32F16F16_RS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| EXPAND_FRAG_ARGS_4(a_frag), desc_b, EXPAND_FRAG_ARGS_16(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__half, float, 64, 32, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x32x16_F32F16F16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_16(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS<__half, float, 64, 64, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint32_t* a_frag, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x64x16_F32F16F16_RS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| EXPAND_FRAG_ARGS_4(a_frag), desc_b, EXPAND_FRAG_ARGS_32(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__half, float, 64, 64, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x64x16_F32F16F16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_32(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS<__half, float, 64, 128, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint32_t* a_frag, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x128x16_F32F16F16_RS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| EXPAND_FRAG_ARGS_4(a_frag), desc_b, EXPAND_FRAG_ARGS_64(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__half, float, 64, 128, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x128x16_F32F16F16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_64(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_RS<__half, float, 64, 256, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint32_t* a_frag, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x256x16_F32F16F16_RS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| EXPAND_FRAG_ARGS_4(a_frag), desc_b, EXPAND_FRAG_ARGS_128(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__half, float, 64, 256, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x256x16_F32F16F16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_128(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__nv_bfloat16, float, 64, 16, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x16x16_F32BF16BF16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_8(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__nv_bfloat16, float, 64, 32, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x32x16_F32BF16BF16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_16(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__nv_bfloat16, float, 64, 64, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x64x16_F32BF16BF16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_32(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__nv_bfloat16, float, 64, 128, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x128x16_F32BF16BF16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_64(d_frag), scale_d); | |
| } | |
| }; | |
| template <Major TransposeA, Major TransposeB, ScaleIn scaleA, ScaleIn scaleB> | |
| struct WGMMA_ASYNC_SS<__nv_bfloat16, float, 64, 256, 16, TransposeA, TransposeB, scaleA, scaleB> { | |
| template <bool init> | |
| static __device__ __forceinline__ void op(uint64_t desc_a, uint64_t desc_b, float* d_frag) { | |
| constexpr auto scale_d = init ? ScaleOut::Zero : ScaleOut::One; | |
| MMA_64x256x16_F32BF16BF16_SS<TransposeA, TransposeB, scaleA, scaleB>::fma( | |
| desc_a, desc_b, EXPAND_FRAG_ARGS_128(d_frag), scale_d); | |
| } | |
| }; | |
| using Swizzle128B = cute::Swizzle<3, 4, 3>; | |
| using Swizzle64B = cute::Swizzle<2, 4, 3>; | |
| using Swizzle32B = cute::Swizzle<1, 4, 3>; | |
| template <SwizzleMode swizzle_mode, uint32_t stride> | |
| __device__ __forceinline__ uint32_t get_swizzle_offset(uint32_t i, uint32_t j) { | |
| constexpr uint32_t M = 8; | |
| if constexpr (swizzle_mode == SwizzleMode::k128B) { | |
| constexpr uint32_t N = 8; | |
| return Swizzle128B{}(((i / M) * M * stride + ((j / N) * M + i % M) * N + (j % N)) << 4) >> 4; | |
| } else { | |
| constexpr uint32_t N = 4; | |
| return Swizzle64B{}(((i / M) * M * stride + ((j / N) * M + i % M) * N + (j % N)) << 4) >> 4; | |
| } | |
| } | |
| __device__ __forceinline__ uint64_t matrix_descriptor_encode(uint64_t x) { | |
| return (((x) & 0x3FFFF) >> 0x4); | |
| } | |
| template <SwizzleMode swizzle_mode, uint64_t leading_byte_offset, uint64_t stride_byte_offset, | |
| typename T> | |
| __device__ uint64_t make_smem_desc(T* ptr) { | |
| uint32_t addr = static_cast<uint32_t>(__cvta_generic_to_shared(ptr)); | |
| uint64_t desc = 0x0000000000000000; | |
| desc |= matrix_descriptor_encode(addr); | |
| // leading byte offset | |
| desc |= matrix_descriptor_encode(leading_byte_offset) << 16; | |
| // stride byte offset | |
| desc |= matrix_descriptor_encode(stride_byte_offset) << 32; | |
| desc |= ((swizzle_mode == SwizzleMode::k128B) ? 1llu | |
| : (swizzle_mode == SwizzleMode::k64B) ? 2llu | |
| : 3llu) | |
| << 62; | |
| return desc; | |
| } | |
| __device__ __forceinline__ void warpgroup_arrive() { cute::warpgroup_arrive(); } | |
| template <int N> | |
| __device__ __forceinline__ void warpgroup_wait() { | |
| cute::warpgroup_wait<N>(); | |
| } | |
| __device__ __forceinline__ void warpgroup_commit_batch() { cute::warpgroup_commit_batch(); } | |
| template <uint32_t size> | |
| __device__ __forceinline__ void warpgroup_fence_frag(float* frag) { | |
| for (uint32_t i = 0; i < size; ++i) { | |
| cute::warpgroup_fence_operand(frag[i]); | |
| } | |
| } | |
| }; // namespace flashinfer | |