Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions csrc/cake_kimi_k3_mla/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ backend="cake")`): Kimi-K3 multi-head latent attention over an FP8 (E4M3) paged
One source per program, compiled for the architecture the device runs; a kernel whose two
architecture lowerings do not fold keeps one program per architecture:

* `main_rt16` -> `cake_kimi_k3_mla_fp8_paged_attention_0671389304e63170bbbc` (sm_100a, sm_103a)
* `main_rt32` -> `cake_kimi_k3_mla_fp8_paged_attention_a8000b087206eb1c3fe9` (sm_100a, sm_103a)
* `main_rt48` -> `cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1` (sm_100a, sm_103a)
* `main_rt64` -> `cake_kimi_k3_mla_fp8_paged_attention_def8106415e373567c7e` (sm_100a, sm_103a)
* `main_rt96` -> `cake_kimi_k3_mla_fp8_paged_attention_4333f9691fff77af1b1f` (sm_100a, sm_103a)
* `main_wide` -> `cake_kimi_k3_mla_fp8_paged_attention_2b9895b6bdfdcc6ff476` (sm_100a), `cake_kimi_k3_mla_fp8_paged_attention_5d067c89074fe8216a6c` (sm_103a)
* `reduce_cta` -> `cake_kimi_k3_mla_fp8_paged_attention_037deeabf96bd0d2d769` (sm_100a, sm_103a)
* `reduce_w1` -> `cake_kimi_k3_mla_fp8_paged_attention_977c0b337b3f4232a99f` (sm_100a, sm_103a)
* `reduce_w2` -> `cake_kimi_k3_mla_fp8_paged_attention_b1c5eb5cb28aa248c2a9` (sm_100a, sm_103a)
* `reduce_w4` -> `cake_kimi_k3_mla_fp8_paged_attention_213d59cceca7f0a817c0` (sm_100a, sm_103a)
* `main_rt16` -> `cake_kimi_k3_mla_fp8_paged_attention_b0d0c47e81edc26da19e` (sm_100a, sm_103a)
* `main_rt32` -> `cake_kimi_k3_mla_fp8_paged_attention_2e4e7903daf6e75c836c` (sm_100a, sm_103a)
* `main_rt48` -> `cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273` (sm_100a, sm_103a)
* `main_rt64` -> `cake_kimi_k3_mla_fp8_paged_attention_29b047f732fcfdfeb85b` (sm_100a, sm_103a)
* `main_rt96` -> `cake_kimi_k3_mla_fp8_paged_attention_07d8fd7941e0ab779c08` (sm_100a, sm_103a)
* `main_wide` -> `cake_kimi_k3_mla_fp8_paged_attention_ea17187e76fd47ab2653` (sm_100a, sm_103a)
* `reduce_cta` -> `cake_kimi_k3_mla_fp8_paged_attention_9c450b8e82598779f1b2` (sm_100a, sm_103a)
* `reduce_w1` -> `cake_kimi_k3_mla_fp8_paged_attention_ab133c42d484f9421945` (sm_100a, sm_103a)
* `reduce_w2` -> `cake_kimi_k3_mla_fp8_paged_attention_9f53b9e3e16d64b967c6` (sm_100a, sm_103a)
* `reduce_w4` -> `cake_kimi_k3_mla_fp8_paged_attention_582ec28a9ab46c704912` (sm_100a, sm_103a)

Per-architecture programs:

* `main_wide`: its sm_103a variant computes the row max with the TMEM reduction load (`tcgen05.ld.red`) and the sm_100a variant with a three-input max tree; the two lowerings differ in too many statements to fold under architecture guards, so the wide decode kernel keeps one program per architecture.
* none: every kernel's two architecture lowerings fold into one source.

* `main_rt16` .. `main_rt96`: swapped-AB tcgen05 kernel, one per row tile (packed (token, head)
rows per CTA; the smallest tile holding a request's rows is taken).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

// Generated by the Cake source exporter — do not edit.
// tvm-ffi direct-source launcher for Cake kernel 'kernel_cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1'.
// tvm-ffi direct-source launcher for Cake kernel 'kernel_cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273'.
#include <cuda.h>
#include <cuda_bf16.h>
#include <cuda_fp16.h>
Expand All @@ -25,10 +25,10 @@

#include <cstdint>

extern "C" __global__ void kernel_cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1(const __grid_constant__ CUtensorMap tmap_q, const __grid_constant__ CUtensorMap tmap_qr, const __grid_constant__ CUtensorMap tmap_k, const __grid_constant__ CUtensorMap tmap_kr, __nv_bfloat16* __restrict__ partial_O, float* __restrict__ partial_max, float* __restrict__ partial_sum, int* __restrict__ seq_lens, int* __restrict__ cum_seq_lens_q, int* __restrict__ page_table, float softmax_scale_log2, float bmm2_scale, int num_heads, int num_split, int max_pages_per_seq);
extern "C" __global__ void kernel_cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273(const __grid_constant__ CUtensorMap tmap_q, const __grid_constant__ CUtensorMap tmap_qr, const __grid_constant__ CUtensorMap tmap_k, const __grid_constant__ CUtensorMap tmap_kr, __nv_bfloat16* __restrict__ partial_O, float* __restrict__ partial_max, float* __restrict__ partial_sum, int* __restrict__ seq_lens, int* __restrict__ cum_seq_lens_q, int* __restrict__ page_table, float softmax_scale_log2, float bmm2_scale, int num_heads, int num_split, int max_pages_per_seq);


namespace cake_host_shim_b213e7a381b39a86 {
namespace cake_host_shim_e7b0c81bd7db18c2 {

using tvm::ffi::TensorView;

Expand Down Expand Up @@ -226,7 +226,14 @@ inline CUtensorMap EncodeTma_tmap_kr(const TensorView& t) {
void Run(TensorView arg_tmap_q, TensorView arg_tmap_qr, TensorView arg_tmap_k, TensorView arg_tmap_kr, TensorView arg_partial_O, TensorView arg_partial_max, TensorView arg_partial_sum, TensorView arg_seq_lens, TensorView arg_cum_seq_lens_q, TensorView arg_page_table, double arg_softmax_scale_log2, double arg_bmm2_scale, int64_t arg_num_heads, int64_t arg_num_split, int64_t arg_max_pages_per_seq, int64_t grid_x, int64_t grid_y, int64_t grid_z) {
DLDevice dev = arg_tmap_q.device();
tvm::ffi::CUDADeviceGuard device_guard(dev.device_id);
TVM_FFI_CHECK_CUDA_ERROR(cudaSetDevice(dev.device_id)); // binds the context for the encoders
{
// Bind the primary context for the tensor-map encoders once per thread and device.
static thread_local int encoder_context_device = -1;
if (encoder_context_device != dev.device_id) {
TVM_FFI_CHECK_CUDA_ERROR(cudaSetDevice(dev.device_id));
encoder_context_device = dev.device_id;
}
}
check_cuda_tensor(arg_tmap_q, "tmap_q");
check_dtype(arg_tmap_q, DLDataType{kDLUInt, 8, 1}, "tmap_q");
check_cuda_tensor(arg_tmap_qr, "tmap_qr");
Expand Down Expand Up @@ -294,18 +301,23 @@ void Run(TensorView arg_tmap_q, TensorView arg_tmap_qr, TensorView arg_tmap_k, T
void* kargs[] = {&p_tmap_q, &p_tmap_qr, &p_tmap_k, &p_tmap_kr, &p_partial_O, &p_partial_max, &p_partial_sum, &p_seq_lens, &p_cum_seq_lens_q, &p_page_table, &v_softmax_scale_log2, &v_bmm2_scale, &v_num_heads, &v_num_split, &v_max_pages_per_seq};

static const bool smem_ready = CakeSetMaxDynamicSmem(
reinterpret_cast<const void*>(kernel_cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1), 227328);
reinterpret_cast<const void*>(kernel_cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273), 227328);
(void)smem_ready;
dim3 grid((uint32_t)grid_x, (uint32_t)grid_y, (uint32_t)grid_z);
dim3 block(512u, 1u, 1u);

cudaError_t launch_status = cudaLaunchKernel(
reinterpret_cast<const void*>(kernel_cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1), grid, block, kargs, 227328u, stream);
cudaLaunchConfig_t config{};
config.gridDim = grid;
config.blockDim = block;
config.dynamicSmemBytes = 227328u;
config.stream = stream;
cudaError_t launch_status = cudaLaunchKernelExC(
&config, reinterpret_cast<const void*>(kernel_cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273), kargs);
TVM_FFI_CHECK(launch_status == cudaSuccess, RuntimeError)
<< "cudaLaunchKernel for kernel_cake_kimi_k3_mla_fp8_paged_attention_e6e3f7bd92aa583f4cc1 failed: "
<< "cudaLaunchKernelExC for kernel_cake_kimi_k3_mla_fp8_paged_attention_03fb1f7512d6581b0273 failed: "
<< cudaGetErrorString(launch_status);
}

} // namespace cake_host_shim_b213e7a381b39a86
} // namespace cake_host_shim_e7b0c81bd7db18c2

TVM_FFI_DLL_EXPORT_TYPED_FUNC(run, cake_host_shim_b213e7a381b39a86::Run);
TVM_FFI_DLL_EXPORT_TYPED_FUNC(run, cake_host_shim_e7b0c81bd7db18c2::Run);
Loading
Loading