KernelBench hard deprecated · RTX PRO 6000

TopK Bitonic DeepSeek V4 Pro

cleandid not score

manually audited: clean

Honest hand-written CUDA top-k via load_inline: a stream kernel (per-thread binary-insertion top-k, warp-shuffle merge, then shared-memory warp-merge) plus a separate hand-rolled bitonic merge kernel that combines per-chunk results. Per- shape tuned chunk sizes. No CUDA graph, no caching, no data_ptr identity — fresh outputs each call. 0.014 sits in the launch-overhead-bound ceiling band (metric artifact, not weakness).

harnessdeepseek-claudeagent session1h 7mtotal wall1h 8mcheck31sbenchmark1soutput tokens186,028cost$14.99gpu-lock wait17sgpu-lock held15sregimememory

Per-shape vs governing ceilingeach shape graded against whichever binds — fp32 compute or HBM bandwidth

1×131072×640.207 ms0.1%0.00 TB/s · 0% of 1.8 TB/s HBM · also 0 TFLOPS (0% of compute)
64×8192×80.048 ms2.4%0.04 TB/s · 2% of 1.8 TB/s HBM · also 0 TFLOPS (0% of compute)
32×16384×320.101 ms1.2%0.02 TB/s · 1% of 1.8 TB/s HBM · also 0 TFLOPS (0% of compute)
16×12000×160.038 ms1.1%0.02 TB/s · 1% of 1.8 TB/s HBM · also 0 TFLOPS (0% of compute)
128×4096×10.010 ms12.1%0.22 TB/s · 12% of 1.8 TB/s HBM · also 0 TFLOPS (0% of compute)

compute-bound memory-bound · bar + right column = official fraction of the ceiling (the geomean input)

geomean(0.1% · 2.4% · 1.2% · 1.1% · 12.1%) = 1.4%

Kernel source (redacted)
"""Top-k via CUDA streaming + bitonic merge.

Two-kernel approach:
  - Stream kernel: each block processes one chunk with warp-shuffle + shared-memory reduction.
  - Merge kernel: bitonic sort combines chunk results.

Optimized chunk sizes per shape for best parallelism/merge tradeoff.
"""

from typing import List
import torch
import torch.nn as nn
import torch.utils.cpp_extension

OP_TYPE = "topk"
SUPPORTED_PRECISIONS = ["fp32"]
HARDWARE_REQUIRED = ["RTX_PRO_6000", "H100", "B200"]

_cpp_src = r"""
#include <cstdint>
extern "C" {
void launch_stream_64(int64_t,int64_t,int64_t,int,int,int,int);
void launch_stream_32(int64_t,int64_t,int64_t,int,int,int,int);
void launch_stream_16(int64_t,int64_t,int64_t,int,int,int,int);
void launch_stream_8(int64_t,int64_t,int64_t,int,int,int,int);
void launch_stream_1(int64_t,int64_t,int64_t,int,int,int,int);
void launch_merge(int64_t,int64_t,int64_t,int64_t,int,int,int);
}
"""

_cuda_src = r"""
#include <cuda_runtime.h>
#include <cstdint>
#include <cfloat>
#include <math.h>

template<int K>
__device__ __noinline__ void merge_lists(
    const float* av, const int* ai, const float* bv, const int* bi, float* dv, int* di
) { int ia=0,ib=0,io=0; while(io<K){ bool ta=(ia<K)&&(ib>=K||av[ia]>=bv[ib]); dv[io]=ta?av[ia]:bv[ib]; di[io]=ta?ai[ia]:bi[ib]; ia+=ta; ib+=!ta; io++; } }

template <int K, int NT>
__global__ void stream_kernel(
    const float* __restrict__ x, float* __restrict__ ov, int64_t* __restrict__ oi,
    int n, int cs, int nc
) {
    int gid=blockIdx.x, bid=gid/nc, cid=gid%nc, tid=threadIdx.x;
    int64_t ro=(int64_t)bid*n;
    int start=cid*cs, end=min(start+cs,n);
    if(start>=end) return;
    float lv[K]; int li[K];
    for(int i=0;i<K;++i){ lv[i]=-INFINITY; li[i]=-1; }
    for(int pos=start+tid; pos<end; pos+=NT) {
        float v = x[ro+pos];
        if(v > lv[K-1]) {
            int lo=0,hi=K-1; while(lo<hi){ int mid=(lo+hi)>>1; if(v>lv[mid])hi=mid; else lo=mid+1; }
            for(int i=K-1;i>lo;--i){ lv[i]=lv[i-1]; li[i]=li[i-1]; } lv[lo]=v; li[lo]=pos;
        }
    }
    const int lane=tid&31;
    for(int off=16;off>0;off>>=1){ float pv[K];int pi[K];
        for(int i=0;i<K;++i){ pv[i]=__shfl_down_sync(0xffffffff,lv[i],off); pi[i]=__shfl_down_sync(0xffffffff,li[i],off); }
        if(lane<off){ float mv[K];int mi[K]; merge_lists<K>(lv,li,pv,pi,mv,mi); for(int i=0;i<K;++i){lv[i]=mv[i];li[i]=mi[i];} }
    }
    constexpr int NW=NT/32, NS=NW*K;
    __shared__ float sv[NS]; __shared__ int si[NS]; int wid=tid/32;
    if(lane==0){ for(int i=0;i<K;++i){ sv[wid*K+i]=lv[i]; si[wid*K+i]=li[i]; } }
    __syncthreads();
    for(int stride=NW/2;stride>0;stride>>=1){ if(lane==0&&wid<stride){ float mv[K];int mi[K]; merge_lists<K>(&sv[wid*K],&si[wid*K],&sv[(wid+stride)*K],&si[(wid+stride)*K],mv,mi); for(int i=0;i<K;++i){sv[wid*K+i]=mv[i];si[wid*K+i]=mi[i];} } __syncthreads(); }
    if(tid==0){ int64_t off=(int64_t)gid*K; for(int i=0;i<K;++i){ ov[off+i]=sv[i]; oi[off+i]=(int64_t)si[i]; } }
}

__global__ void merge_kernel(
    const float* __restrict__ cv, const int64_t* __restrict__ ci,
    float* __restrict__ ov, int64_t* __restrict__ oi, int nc, int k
) {
    int bid=blockIdx.x, tid=threadIdx.x, total=nc*k;
    extern __shared__ char smem[];
    float* sv=(float*)smem; int* si=(int*)(smem+total*sizeof(float));
    for(int i=tid;i<total;i+=blockDim.x){ int64_t src=(int64_t)bid*total+i; sv[i]=cv[src]; si[i]=(int)ci[src]; }
    __syncthreads();
    int n2=1; while(n2<total) n2<<=1;
    for(int stage=2;stage<=n2;stage<<=1){ for(int step=stage>>1;step>0;step>>=1){ for(int i=tid;i<n2;i+=blockDim.x){
        int ixj=i^step; if(ixj<=i) continue;
        float vi=(i<total)?sv[i]:-INFINITY, vj=(ixj<total)?sv[ixj]:-INFINITY;
        bool asc=((i&stage)==0), sw=asc?(vi<vj):(vi>vj);
        if(sw){ if(i<total&&ixj<total){ float tv=sv[i];sv[i]=sv[ixj];sv[ixj]=tv; int ti=si[i];si[i]=si[ixj];si[ixj]=ti; }
                else if(i<total){ sv[i]=vj; si[i]=(ixj<total)?si[ixj]:-1; }
                else if(ixj<total){ sv[i]=vi; si[i]=(i<total)?si[i]:-1; } }
    } __syncthreads(); } }
    for(int i=tid;i<k;i+=blockDim.x){ ov[(int64_t)bid*k+i]=sv[i]; oi[(int64_t)bid*k+i]=(int64_t)si[i]; }
}

extern "C" {
#define L(K,NT) void launch_stream_##K(int64_t xp,int64_t ovp,int64_t oip,int B,int n,int cs,int nc){ \
    auto*x=(const float*)xp;auto*ov=(float*)ovp;auto*oi=(int64_t*)oip; stream_kernel<K,NT><<<B*nc,NT>>>(x,ov,oi,n,cs,nc); }
L(64,128) L(32,128) L(16,128) L(8,128) L(1,128)
#undef L
void launch_merge(int64_t cvp,int64_t cip,int64_t ovp,int64_t oip,int B,int nc,int k){
    auto*cv=(const float*)cvp;auto*ci=(const int64_t*)cip;auto*ov=(float*)ovp;auto*oi=(int64_t*)oip;
    int total=nc*k, block=total<1024?total:1024; size_t sm=total*(sizeof(float)+sizeof(int));
    merge_kernel<<<B,block,sm>>>(cv,ci,ov,oi,nc,k);
}
}
"""

_props = torch.cuda.get_device_properties(0)
_arch = f"sm_{_props.major}{_props.minor}"
_cflags = ["-O3","--use_fast_math",f"-arch={_arch}","--expt-relaxed-constexpr","-std=c++17","-DNDEBUG"]
_mod = None

def _get_mod():
    global _mod
    if _mod is None:
        _mod = torch.utils.cpp_extension.load_inline(
            name="topk_kern",cpp_sources=_cpp_src,cuda_sources=_cuda_src,
            functions=["launch_stream_64","launch_stream_32","launch_stream_16","launch_stream_8","launch_stream_1","launch_merge"],
            extra_cuda_cflags=_cflags,verbose=False)
    return _mod

batch=64; n=8192; k=8
def get_inputs(): return [torch.randn(batch,n,dtype=torch.float32)]
def get_init_inputs(): return [batch,n,k]

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):
        B,N,K=self.batch,self.n,self.k
        # Best chunk sizes per shape (tuned)
        cfg = {(1,131072,64):(2048,64), (64,8192,8):(8192,1), (32,16384,32):(4096,4),
               (16,12000,16):(1500,8), (128,4096,1):(4096,1)}
        cs,nc = cfg.get((B,N,K), (N,1))
        m=_get_mod()
        launch={64:m.launch_stream_64,32:m.launch_stream_32,16:m.launch_stream_16,8:m.launch_stream_8,1:m.launch_stream_1}[K]
        if nc==1:
            ov=torch.empty(B,K,dtype=torch.float32,device=x.device)
            oi=torch.empty(B,K,dtype=torch.int64,device=x.device)
            launch(x.data_ptr(),ov.data_ptr(),oi.data_ptr(),B,N,cs,nc)
            return ov,oi
        total=B*nc; cv=torch.empty(total,K,dtype=torch.float32,device=x.device)
        ci=torch.empty(total,K,dtype=torch.int64,device=x.device)
        launch(x.data_ptr(),cv.data_ptr(),ci.data_ptr(),B,N,cs,nc)
        ov=torch.empty(B,K,dtype=torch.float32,device=x.device)
        oi=torch.empty(B,K,dtype=torch.int64,device=x.device)
        m.launch_merge(cv.data_ptr(),ci.data_ptr(),ov.data_ptr(),oi.data_ptr(),B,nc,K)
        return ov,oi

20260615_132230_deepseek-claude_deepseek-v4-pro_05_topk_bitonic