KernelBench hard deprecated · RTX PRO 6000
TopK Bitonic Kimi K2.7-Code
wrongdid not score
Wrong-answer bug: pass-2 row indexing uses the wrong K. Pass 1 writes per-chunk candidates with row stride chunks_per_row*k (k=16), but pass 2 is dispatched with a template K rounded up from max_candidates (e.g. K=32 at 128 candidates), so it computes real_count=chunks_per_row*32 and in_base=row*real_count, reading misaligned candidate memory for every row>0. Shape 3 (batch=16,n=12000,k=16) fails with 227/256 mismatched (max_abs_diff 1.48, worst row 7); single-row shapes pass because row 0 is unaffected.
Kernel source (redacted)
"""Custom CUDA top-k kernel: CUB block radix sort in chunks + bitonic merge.
Pass 1: one block per 8192-element chunk, CUB BlockRadixSort produces the
chunk's top-K values and indices written to global memory.
Pass 2: one block per row, bitonic sort of (chunks * K) candidates produces
the final top-K.
k=1 is handled with a dedicated warp-reduction argmax.
"""
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
_CUDA_SOURCE = r"""
#include <cuda_runtime.h>
#include <cub/block/block_radix_sort.cuh>
#include <float.h>
#include <stdint.h>
#include <torch/extension.h>
#include <tuple>
// ----------------------------------------------------------------------------
// Shared-memory bitonic sort (descending by value; indices carried along)
// ----------------------------------------------------------------------------
template <int N>
__device__ __forceinline__ void bitonic_sort_desc(float* __restrict__ val,
int* __restrict__ idx) {
for (int k = 2; k <= N; k <<= 1) {
for (int j = k >> 1; j > 0; j >>= 1) {
int tid = threadIdx.x;
for (int i = tid; i < N; i += blockDim.x) {
int lo = i;
int hi = i ^ j;
if (hi > lo) {
if ((lo & k) == 0) {
if (val[lo] < val[hi]) {
float tv = val[lo]; val[lo] = val[hi]; val[hi] = tv;
int ti = idx[lo]; idx[lo] = idx[hi]; idx[hi] = ti;
}
} else {
if (val[lo] > val[hi]) {
float tv = val[lo]; val[lo] = val[hi]; val[hi] = tv;
int ti = idx[lo]; idx[lo] = idx[hi]; idx[hi] = ti;
}
}
}
}
__syncthreads();
}
}
}
// ----------------------------------------------------------------------------
// k = 1 (argmax via warp reductions)
// ----------------------------------------------------------------------------
__global__ void topk_k1_kernel(const float* __restrict__ x,
float* __restrict__ out_val,
int64_t* __restrict__ out_idx,
int B, int N) {
int row = blockIdx.x;
if (row >= B) return;
const float* row_x = x + (int64_t)row * N;
float best_val = -FLT_MAX;
int best_idx = 0;
int tid = threadIdx.x;
int nt = blockDim.x;
for (int i = tid; i < N; i += nt) {
float v = row_x[i];
if (v > best_val) {
best_val = v;
best_idx = i;
}
}
#pragma unroll
for (int offset = 16; offset > 0; offset >>= 1) {
float other_v = __shfl_down_sync(0xFFFFFFFF, best_val, offset);
int other_i = __shfl_down_sync(0xFFFFFFFF, best_idx, offset);
if (other_v > best_val) {
best_val = other_v;
best_idx = other_i;
}
}
__shared__ float warp_val[32];
__shared__ int warp_idx[32];
int lane = tid & 31;
int wid = tid >> 5;
if (lane == 0) {
warp_val[wid] = best_val;
warp_idx[wid] = best_idx;
}
__syncthreads();
if (wid == 0) {
int num_warps = nt >> 5;
float v = (lane < num_warps) ? warp_val[lane] : -FLT_MAX;
int i = (lane < num_warps) ? warp_idx[lane] : 0;
#pragma unroll
for (int offset = 16; offset > 0; offset >>= 1) {
float other_v = __shfl_down_sync(0xFFFFFFFF, v, offset);
int other_i = __shfl_down_sync(0xFFFFFFFF, i, offset);
if (other_v > v) {
v = other_v;
i = other_i;
}
}
if (lane == 0) {
out_val[row] = v;
out_idx[row] = (int64_t)i;
}
}
}
// ----------------------------------------------------------------------------
// Pass 1: CUB block radix sort per 8192-element chunk
// ----------------------------------------------------------------------------
template <int K, int CHUNK>
__global__ void topk_pass1_kernel(const float* __restrict__ x,
float* __restrict__ chunk_val,
int* __restrict__ chunk_idx,
int B, int N, int chunks_per_row) {
int block_id = blockIdx.x;
int total_blocks = B * chunks_per_row;
if (block_id >= total_blocks) return;
int row = block_id / chunks_per_row;
int chunk = block_id - row * chunks_per_row;
const float* row_x = x + (int64_t)row * N;
int base = chunk * CHUNK;
int len = min(CHUNK, N - base);
constexpr int BLOCK_THREADS = 1024;
constexpr int ITEMS_PER_THREAD = CHUNK / BLOCK_THREADS; // 2
typedef cub::BlockRadixSort<float, BLOCK_THREADS, ITEMS_PER_THREAD, int> BlockSort;
__shared__ typename BlockSort::TempStorage temp_storage;
float thread_keys[ITEMS_PER_THREAD];
int thread_values[ITEMS_PER_THREAD];
#pragma unroll
for (int i = 0; i < ITEMS_PER_THREAD; ++i) {
int idx = threadIdx.x + i * BLOCK_THREADS;
if (idx < len) {
thread_keys[i] = row_x[base + idx];
thread_values[i] = base + idx;
} else {
thread_keys[i] = -FLT_MAX;
thread_values[i] = 0;
}
}
BlockSort(temp_storage).SortDescendingBlockedToStriped(thread_keys, thread_values);
int tid = threadIdx.x;
if (tid < K) {
int out_base = (row * chunks_per_row + chunk) * K;
chunk_val[out_base + tid] = thread_keys[0];
chunk_idx[out_base + tid] = thread_values[0];
}
}
// ----------------------------------------------------------------------------
// Pass 2: bitonic sort of candidates, write final top-K
// ----------------------------------------------------------------------------
template <int K, int MAX_CANDIDATES>
__global__ void topk_pass2_kernel(const float* __restrict__ chunk_val,
const int* __restrict__ chunk_idx,
float* __restrict__ out_val,
int64_t* __restrict__ out_idx,
int B, int chunks_per_row) {
int row = blockIdx.x;
if (row >= B) return;
float* row_out_v = out_val + (int64_t)row * K;
int64_t* row_out_i = out_idx + (int64_t)row * K;
extern __shared__ char smem[];
float* s_val = (float*)smem;
int* s_idx = (int*)(s_val + MAX_CANDIDATES);
int tid = threadIdx.x;
int nt = blockDim.x;
int real_count = chunks_per_row * K;
int in_base = row * real_count;
for (int i = tid; i < MAX_CANDIDATES; i += nt) {
if (i < real_count) {
s_val[i] = chunk_val[in_base + i];
s_idx[i] = chunk_idx[in_base + i];
} else {
s_val[i] = -FLT_MAX;
s_idx[i] = 0;
}
}
__syncthreads();
bitonic_sort_desc<MAX_CANDIDATES>(s_val, s_idx);
for (int i = tid; i < K; i += nt) {
row_out_v[i] = s_val[i];
row_out_i[i] = (int64_t)s_idx[i];
}
}
// ----------------------------------------------------------------------------
// Dispatch wrapper
// ----------------------------------------------------------------------------
std::tuple<torch::Tensor, torch::Tensor> topk_forward(torch::Tensor x, int64_t k) {
int B = x.size(0);
int N = x.size(1);
auto out_val = torch::empty({B, k}, x.options());
auto out_idx = torch::empty({B, k}, torch::dtype(torch::kLong).device(x.device()));
if (k == 1) {
topk_k1_kernel<<<B, 1024>>>(
x.data_ptr<float>(),
out_val.data_ptr<float>(),
out_idx.data_ptr<int64_t>(),
B, N);
return std::make_tuple(out_val, out_idx);
}
constexpr int CHUNK = 2048;
int chunks_per_row = (N + CHUNK - 1) / CHUNK;
int total_candidates = chunks_per_row * k;
int max_candidates = 1;
while (max_candidates < total_candidates) max_candidates <<= 1;
auto chunk_val = torch::empty({B, chunks_per_row, k}, x.options());
auto chunk_idx = torch::empty({B, chunks_per_row, k}, torch::dtype(torch::kInt).device(x.device()));
int pass1_blocks = B * chunks_per_row;
int pass1_threads = 1024;
int pass1_smem = 0;
switch ((int)k) {
case 8:
topk_pass1_kernel<8, CHUNK><<<pass1_blocks, pass1_threads, pass1_smem>>>(
x.data_ptr<float>(), chunk_val.data_ptr<float>(),
chunk_idx.data_ptr<int>(), B, N, chunks_per_row);
break;
case 16:
topk_pass1_kernel<16, CHUNK><<<pass1_blocks, pass1_threads, pass1_smem>>>(
x.data_ptr<float>(), chunk_val.data_ptr<float>(),
chunk_idx.data_ptr<int>(), B, N, chunks_per_row);
break;
case 32:
topk_pass1_kernel<32, CHUNK><<<pass1_blocks, pass1_threads, pass1_smem>>>(
x.data_ptr<float>(), chunk_val.data_ptr<float>(),
chunk_idx.data_ptr<int>(), B, N, chunks_per_row);
break;
case 64:
topk_pass1_kernel<64, CHUNK><<<pass1_blocks, pass1_threads, pass1_smem>>>(
x.data_ptr<float>(), chunk_val.data_ptr<float>(),
chunk_idx.data_ptr<int>(), B, N, chunks_per_row);
break;
default:
TORCH_CHECK(false, "unsupported k for fast path");
}
int pass2_blocks = B;
int pass2_threads = 1024;
int pass2_smem = max_candidates * (sizeof(float) + sizeof(int));
switch (max_candidates) {
case 4096:
topk_pass2_kernel<64, 4096><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 2048:
topk_pass2_kernel<64, 2048><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 1024:
topk_pass2_kernel<64, 1024><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 256:
topk_pass2_kernel<32, 256><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 128:
topk_pass2_kernel<32, 128><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 64:
topk_pass2_kernel<16, 64><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 32:
topk_pass2_kernel<8, 32><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 16:
topk_pass2_kernel<8, 16><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
case 8:
topk_pass2_kernel<8, 8><<<pass2_blocks, pass2_threads, pass2_smem>>>(
chunk_val.data_ptr<float>(), chunk_idx.data_ptr<int>(),
out_val.data_ptr<float>(), out_idx.data_ptr<int64_t>(),
B, chunks_per_row);
break;
default:
TORCH_CHECK(false, "unsupported max_candidates");
}
return std::make_tuple(out_val, out_idx);
}
"""
_CPP_SOURCE = r"""
#include <torch/extension.h>
#include <tuple>
std::tuple<torch::Tensor, torch::Tensor> topk_forward(torch::Tensor x, int64_t k);
"""
_topk_cuda = load_inline(
name="topk_cub_8192",
cpp_sources=_CPP_SOURCE,
cuda_sources=_CUDA_SOURCE,
functions=["topk_forward"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
verbose=False,
)
class Model(nn.Module):
def __init__(self, batch: int, n: int, k: int):
super().__init__()
self.batch, self.n, self.k = batch, n, k
self.register_buffer("_dummy", torch.zeros(1))
def forward(self, x: torch.Tensor):
values, indices = _topk_cuda.topk_forward(x, self.k)
return values, indices
batch = 64
n = 8192
k = 8
def get_inputs():
x = torch.randn(batch, n, dtype=torch.float32)
return [x]
def get_init_inputs():
return [batch, n, k]
20260613_163914_kimi-claude_kimi-k2.7-code_05_topk_bitonic