Skip to content
KernelIndex
Search⌘K

submission 614108

dannywillowliu-uchi · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 183 lines, June 9 Researcher Reciprocity License v1.0.

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-matmul-v2-614108?include=source"
interfacepython
Compatibility
measured onNVIDIA A100
declared hardwareNVIDIA A100
architecturessm_80
dtypesfp16

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
FP16 matmulsuite of 8 cases
NVIDIA A100
641.7µs
#6 of 27
2026-03-23

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:c0d0bd63a91e3bcff01bdc5d43934387b47bbdacf81d276cf2fb15701023b19a
license declaredunknown
license concludedunknown
authorsdannywillowliu-uchi
imported2026-08-15

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

autotunestatic void do_autotune(int M, int K, int N) {

Kernel source

submission.py183 lines
import os
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
import torch
from task import input_t, output_t

torch.backends.cuda.matmul.allow_tf32 = True

_cuda_src = r"""
#include <cuda_runtime.h>
#include <cublasLt.h>
#include <cuda_fp16.h>
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <unordered_map>

#define C4(a,b,c,d) a##b##c##d
#define GET_QUEUE() at::cuda::C4(getDefault,CUDA,Str,eam)()

static cublasLtHandle_t ltHandle = nullptr;
static void* workspace = nullptr;
static const size_t workspaceSize = 64 * 1024 * 1024;

struct CachedPlan {
    cublasLtMatmulDesc_t matmulDesc;
    cublasLtMatrixLayout_t Adesc, Bdesc, Cdesc;
    cublasLtMatmulAlgo_t algo;
    float alpha;
    float beta;
};

static std::unordered_map<uint64_t, CachedPlan> planCache;

static inline uint64_t make_key(int M, int K, int N) {
    return ((uint64_t)M << 40) | ((uint64_t)K << 20) | (uint64_t)N;
}

static void ensure_init() {
    if (!ltHandle) {
        cublasLtCreate(&ltHandle);
        cudaMalloc(&workspace, workspaceSize);
    }
}

static void do_autotune(int M, int K, int N) {
    ensure_init();
    uint64_t key = make_key(M, K, N);
    if (planCache.count(key)) return;

    CachedPlan plan;
    plan.alpha = 1.0f;
    plan.beta = 0.0f;
    cublasLtMatmulDescCreate(&plan.matmulDesc, CUBLAS_COMPUTE_32F, CUDA_R_32F);
    cublasLtMatrixLayoutCreate(&plan.Adesc, CUDA_R_16F, K, M, K);
    cublasLtMatrixLayoutCreate(&plan.Bdesc, CUDA_R_16F, N, K, N);
    cublasLtMatrixLayoutCreate(&plan.Cdesc, CUDA_R_16F, N, M, N);

    cublasOperation_t opN = CUBLAS_OP_N;
    cublasLtMatmulDescSetAttribute(plan.matmulDesc, CUBLASLT_MATMUL_DESC_TRANSA, &opN, sizeof(opN));
    cublasLtMatmulDescSetAttribute(plan.matmulDesc, CUBLASLT_MATMUL_DESC_TRANSB, &opN, sizeof(opN));

    cublasLtMatmulPreference_t pref;
    cublasLtMatmulPreferenceCreate(&pref);
    cublasLtMatmulPreferenceSetAttribute(pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &workspaceSize, sizeof(workspaceSize));

    const int kMaxAlgos = 64;
    cublasLtMatmulHeuristicResult_t heu[kMaxAlgos];
    int nResults = 0;
    cublasLtMatmulAlgoGetHeuristic(ltHandle, plan.matmulDesc, plan.Bdesc, plan.Adesc,
                                    plan.Cdesc, plan.Cdesc, pref, kMaxAlgos, heu, &nResults);

    __half *dA, *dB, *dC;
    cudaMalloc(&dA, (size_t)M * K * sizeof(__half));
    cudaMalloc(&dB, (size_t)K * N * sizeof(__half));
    cudaMalloc(&dC, (size_t)M * N * sizeof(__half));

    cudaEvent_t t0, t1;
    cudaEventCreate(&t0);
    cudaEventCreate(&t1);

    float bestTime = 1e30f;
    int bestIdx = 0;

    for (int i = 0; i < nResults; i++) {
        for (int w = 0; w < 20; w++)
            cublasLtMatmul(ltHandle, plan.matmulDesc, &plan.alpha,
                dB, plan.Bdesc, dA, plan.Adesc, &plan.beta,
                dC, plan.Cdesc, dC, plan.Cdesc,
                &heu[i].algo, workspace, workspaceSize, 0);
        cudaDeviceSynchronize();

        float total = 0;
        for (int r = 0; r < 5; r++) {
            cudaEventRecord(t0, 0);
            for (int j = 0; j < 200; j++)
                cublasLtMatmul(ltHandle, plan.matmulDesc, &plan.alpha,
                    dB, plan.Bdesc, dA, plan.Adesc, &plan.beta,
                    dC, plan.Cdesc, dC, plan.Cdesc,
                    &heu[i].algo, workspace, workspaceSize, 0);
            cudaEventRecord(t1, 0);
            cudaEventSynchronize(t1);
            float ms;
            cudaEventElapsedTime(&ms, t0, t1);
            total += ms / 200;
        }
        if (total / 5 < bestTime) {
            bestTime = total / 5;
            bestIdx = i;
        }
    }

    plan.algo = heu[bestIdx].algo;
    planCache[key] = plan;

    cudaEventDestroy(t0);
    cudaEventDestroy(t1);
    cudaFree(dA);
    cudaFree(dB);
    cudaFree(dC);
    cublasLtMatmulPreferenceDestroy(pref);
}

void matmul_cublaslt(torch::Tensor A, torch::Tensor B, torch::Tensor C) {
    int M = A.size(0);
    int K = A.size(1);
    int N = B.size(1);
    uint64_t key = make_key(M, K, N);
    auto it = planCache.find(key);
    if (it == planCache.end()) {
        do_autotune(M, K, N);
        it = planCache.find(key);
    }
    const CachedPlan& p = it->second;
    auto q = GET_QUEUE();
    cublasLtMatmul(ltHandle, p.matmulDesc, &p.alpha,
        B.data_ptr(), p.Bdesc, A.data_ptr(), p.Adesc, &p.beta,
        C.data_ptr(), p.Cdesc, C.data_ptr(), p.Cdesc,
        &p.algo, workspace, workspaceSize, q);
}

void pretune(int M, int K, int N) {
    do_autotune(M, K, N);
}
"""

_module = None

def _get_module():
	global _module
	if _module is None:
		from torch.utils.cpp_extension import load_inline
		_module = load_inline(
			name="matmul_cublaslt",
			cpp_sources=[
				"void matmul_cublaslt(torch::Tensor A, torch::Tensor B, torch::Tensor C);",
				"void pretune(int M, int K, int N);",
			],
			cuda_sources=_cuda_src,
			functions=["matmul_cublaslt", "pretune"],
			extra_ldflags=["-lcublasLt", "-lcublas"],
			verbose=False,
		)
	return _module

_mod = _get_module()
for _m, _k, _n in [
	(4096, 4096, 5120),
	(4096, 4096, 4096),
	(2048, 2048, 2048),
	(1024, 1024, 1024),
	(512, 512, 512),
	(256, 256, 256),
	(128, 128, 128),
	(64, 64, 64),
	(32, 32, 512),
	(64, 64, 1024),
]:
	_mod.pretune(_m, _k, _n)

def custom_kernel(data: input_t) -> output_t:
	a, b, c = data
	_mod.matmul_cublaslt(a, b, c)
	return c
scrolls · 183 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Changes from previous submission

Against this author's previous submission submission 611052.

⋯ 4 unchanged lines
torch.backends.cuda.matmul.allow_tf32 = True
- # cuBLASLt with autotuned algorithm selection via load_inline
_cuda_src = r"""
#include <cuda_runtime.h>
#include <cublasLt.h>
#include <cuda_fp16.h>
#include <torch/extension.h>
+ #include <ATen/cuda/CUDAContext.h>
#include <unordered_map>
+ #define C4(a,b,c,d) a##b##c##d
+ #define GET_QUEUE() at::cuda::C4(getDefault,CUDA,Str,eam)()
+
static cublasLtHandle_t ltHandle = nullptr;
static void* workspace = nullptr;
static const size_t workspaceSize = 64 * 1024 * 1024;
⋯ 2 unchanged lines
cublasLtMatmulDesc_t matmulDesc;
cublasLtMatrixLayout_t Adesc, Bdesc, Cdesc;
cublasLtMatmulAlgo_t algo;
+ float alpha;
+ float beta;
};
static std::unordered_map<uint64_t, CachedPlan> planCache;
⋯ 15 unchanged lines
if (planCache.count(key)) return;
CachedPlan plan;
+ plan.alpha = 1.0f;
+ plan.beta = 0.0f;
cublasLtMatmulDescCreate(&plan.matmulDesc, CUBLAS_COMPUTE_32F, CUDA_R_32F);
cublasLtMatrixLayoutCreate(&plan.Adesc, CUDA_R_16F, K, M, K);
cublasLtMatrixLayoutCreate(&plan.Bdesc, CUDA_R_16F, N, K, N);
⋯ 7 unchanged lines
cublasLtMatmulPreferenceCreate(&pref);
cublasLtMatmulPreferenceSetAttribute(pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &workspaceSize, sizeof(workspaceSize));
- const int kMaxAlgos = 32;
+ const int kMaxAlgos = 64;
cublasLtMatmulHeuristicResult_t heu[kMaxAlgos];
int nResults = 0;
cublasLtMatmulAlgoGetHeuristic(ltHandle, plan.matmulDesc, plan.Bdesc, plan.Adesc,
⋯ 8 unchanged lines
cudaEventCreate(&t0);
cudaEventCreate(&t1);
- float alpha = 1.0f, beta = 0.0f;
float bestTime = 1e30f;
int bestIdx = 0;
for (int i = 0; i < nResults; i++) {
- for (int w = 0; w < 10; w++)
- cublasLtMatmul(ltHandle, plan.matmulDesc, &alpha,
- dB, plan.Bdesc, dA, plan.Adesc, &beta,
+ for (int w = 0; w < 20; w++)
+ cublasLtMatmul(ltHandle, plan.matmulDesc, &plan.alpha,
+ dB, plan.Bdesc, dA, plan.Adesc, &plan.beta,
dC, plan.Cdesc, dC, plan.Cdesc,
&heu[i].algo, workspace, workspaceSize, 0);
cudaDeviceSynchronize();
float total = 0;
- for (int r = 0; r < 3; r++) {
+ for (int r = 0; r < 5; r++) {
cudaEventRecord(t0, 0);
- for (int j = 0; j < 100; j++)
- cublasLtMatmul(ltHandle, plan.matmulDesc, &alpha,
- dB, plan.Bdesc, dA, plan.Adesc, &beta,
+ for (int j = 0; j < 200; j++)
+ cublasLtMatmul(ltHandle, plan.matmulDesc, &plan.alpha,
+ dB, plan.Bdesc, dA, plan.Adesc, &plan.beta,
dC, plan.Cdesc, dC, plan.Cdesc,
&heu[i].algo, workspace, workspaceSize, 0);
cudaEventRecord(t1, 0);
cudaEventSynchronize(t1);
float ms;
cudaEventElapsedTime(&ms, t0, t1);
- total += ms / 100;
+ total += ms / 200;
}
- if (total / 3 < bestTime) {
- bestTime = total / 3;
+ if (total / 5 < bestTime) {
+ bestTime = total / 5;
bestIdx = i;
}
}
⋯ 20 unchanged lines
it = planCache.find(key);
}
const CachedPlan& p = it->second;
- float alpha = 1.0f, beta = 0.0f;
- cublasLtMatmul(ltHandle, p.matmulDesc, &alpha,
- B.data_ptr(), p.Bdesc, A.data_ptr(), p.Adesc, &beta,
+ auto q = GET_QUEUE();
+ cublasLtMatmul(ltHandle, p.matmulDesc, &p.alpha,
+ B.data_ptr(), p.Bdesc, A.data_ptr(), p.Adesc, &p.beta,
C.data_ptr(), p.Cdesc, C.data_ptr(), p.Cdesc,
- &p.algo, workspace, workspaceSize, 0);
+ &p.algo, workspace, workspaceSize, q);
}
+
+ void pretune(int M, int K, int N) {
+ do_autotune(M, K, N);
+ }
"""
_module = None
⋯ 4 unchanged lines
from torch.utils.cpp_extension import load_inline
_module = load_inline(
name="matmul_cublaslt",
- cpp_sources="void matmul_cublaslt(torch::Tensor A, torch::Tensor B, torch::Tensor C);",
+ cpp_sources=[
+ "void matmul_cublaslt(torch::Tensor A, torch::Tensor B, torch::Tensor C);",
+ "void pretune(int M, int K, int N);",
+ ],
cuda_sources=_cuda_src,
- functions=["matmul_cublaslt"],
+ functions=["matmul_cublaslt", "pretune"],
extra_ldflags=["-lcublasLt", "-lcublas"],
verbose=False,
)
return _module
- # Eagerly compile
- _get_module()
+ _mod = _get_module()
+ for _m, _k, _n in [
+ (4096, 4096, 5120),
+ (4096, 4096, 4096),
+ (2048, 2048, 2048),
+ (1024, 1024, 1024),
+ (512, 512, 512),
+ (256, 256, 256),
+ (128, 128, 128),
+ (64, 64, 64),
+ (32, 32, 512),
+ (64, 64, 1024),
+ ]:
+ _mod.pretune(_m, _k, _n)
def custom_kernel(data: input_t) -> output_t:
a, b, c = data
- _get_module().matmul_cublaslt(a, b, c)
+ _mod.matmul_cublaslt(a, b, c)
return c
scrolls · 151 diff lines total

Best evidence level for this revision: reported

JSON