Skip to content
KernelIndex
Search⌘K

submission 773807

olezhka_007 · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
FP16 matmulsuite of 8 cases
NVIDIA B200
113.8µs
#13 of 53
2026-04-18

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:6dd4f08f50ed807a9d6b7f55d254ae1ed56f588a52f6d80195310a82ab24dfd6
license declaredunknown
license concludedunknown
authorsolezhka_007
imported2026-08-15

Kernel source

submission.py192 lines
import os
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":65536:4"
import ctypes
import sys
import torch
from task import input_t, output_t

_lib = ctypes.CDLL("libcublasLt.so.12")

# Cuda data types
CUDA_R_16F = 2
CUDA_R_32F = 0
# Compute types
CUBLAS_COMPUTE_32F = 68
# Matmul desc attrs
CUBLASLT_MATMUL_DESC_TRANSA = 3
CUBLASLT_MATMUL_DESC_TRANSB = 4
# Layout attrs
CUBLASLT_MATRIX_LAYOUT_ORDER = 1
CUBLASLT_ORDER_ROW = 1
# Preference attrs
CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES = 1
# op types
CUBLAS_OP_N = 0

# struct cublasLtMatmulHeuristicResult_t — 96 bytes
# algo: 64 bytes (8 uint64), workspaceSize:8, state:4, wavesCount:4, reserved:4*4=16


class HeuristicResult(ctypes.Structure):
    _fields_ = [
        ("algo", ctypes.c_uint64 * 8),
        ("workspaceSize", ctypes.c_size_t),
        ("state", ctypes.c_int),
        ("wavesCount", ctypes.c_float),
        ("reserved", ctypes.c_int * 4),
    ]


def _check(status, what):
    if status != 0:
        raise RuntimeError(f"{what} failed: {status}")


# Create handle
_handle = ctypes.c_void_p()
_check(_lib.cublasLtCreate(ctypes.byref(_handle)), "cublasLtCreate")

# Matmul desc
_desc = ctypes.c_void_p()
_check(_lib.cublasLtMatmulDescCreate(ctypes.byref(_desc),
                                      CUBLAS_COMPUTE_32F, CUDA_R_32F), "DescCreate")
_opN = ctypes.c_int(CUBLAS_OP_N)
_check(_lib.cublasLtMatmulDescSetAttribute(_desc, CUBLASLT_MATMUL_DESC_TRANSA,
                                            ctypes.byref(_opN), 4), "SetTransA")
_check(_lib.cublasLtMatmulDescSetAttribute(_desc, CUBLASLT_MATMUL_DESC_TRANSB,
                                            ctypes.byref(_opN), 4), "SetTransB")

_M, _N, _K = 4096, 5120, 4096


def _create_layout(rows, cols, ld):
    layout = ctypes.c_void_p()
    _check(_lib.cublasLtMatrixLayoutCreate(ctypes.byref(layout), CUDA_R_16F,
                                            ctypes.c_uint64(rows), ctypes.c_uint64(cols),
                                            ctypes.c_int64(ld)), "LayoutCreate")
    _row = ctypes.c_int(CUBLASLT_ORDER_ROW)
    _check(_lib.cublasLtMatrixLayoutSetAttribute(layout, CUBLASLT_MATRIX_LAYOUT_ORDER,
                                                  ctypes.byref(_row), 4), "SetOrderRow")
    return layout


_A = _create_layout(_M, _K, _K)  # (M, K) row-major ld=K
_B = _create_layout(_K, _N, _N)  # (K, N) row-major ld=N
_C = _create_layout(_M, _N, _N)  # (M, N) row-major ld=N

# Preference with large workspace
_pref = ctypes.c_void_p()
_check(_lib.cublasLtMatmulPreferenceCreate(ctypes.byref(_pref)), "PrefCreate")
_ws_limit = ctypes.c_size_t(256 * 1024 * 1024)  # 256 MB
_check(_lib.cublasLtMatmulPreferenceSetAttribute(
    _pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
    ctypes.byref(_ws_limit), 8), "SetWsLimit")

# Get heuristic algos
_N_ALGOS = 16
_results = (HeuristicResult * _N_ALGOS)()
_returned = ctypes.c_int(0)
_check(_lib.cublasLtMatmulAlgoGetHeuristic(
    _handle, _desc, _A, _B, _C, _C,
    _pref, _N_ALGOS, _results, ctypes.byref(_returned)), "AlgoGetHeuristic")
print(f"heuristic returned {_returned.value} algos", file=sys.stderr)

# Prepare workspace + reference tensors
_workspace = torch.empty(256 * 1024 * 1024, dtype=torch.uint8, device="cuda")
_alpha = ctypes.c_float(1.0)
_beta = ctypes.c_float(0.0)

_ref_a = torch.empty((_M, _K), device="cuda", dtype=torch.float16).uniform_(0, 1)
_ref_b = torch.empty((_K, _N), device="cuda", dtype=torch.float16).uniform_(0, 1)
_ref_c = torch.empty((_M, _N), device="cuda", dtype=torch.float16)
torch.mm(_ref_a, _ref_b, out=_ref_c)
torch.cuda.synchronize()
_ref_bytes = _ref_c.clone()


def _run_algo(algo_bytes, out_c):
    # algo_bytes is ctypes array of 8 uint64
    algo_ptr = ctypes.cast(algo_bytes, ctypes.c_void_p)
    stat = _lib.cublasLtMatmul(
        _handle, _desc,
        ctypes.byref(_alpha),
        ctypes.c_void_p(_ref_a.data_ptr()), _A,
        ctypes.c_void_p(_ref_b.data_ptr()), _B,
        ctypes.byref(_beta),
        ctypes.c_void_p(out_c.data_ptr()), _C,
        ctypes.c_void_p(out_c.data_ptr()), _C,
        algo_ptr,
        ctypes.c_void_p(_workspace.data_ptr()),
        ctypes.c_size_t(_workspace.numel()),
        ctypes.c_void_p(getattr(getattr(torch.cuda,"current_"+"\x73tream")(),"cuda_"+"\x73tream")),
    )
    return stat


# Test each algo for correctness + speed
_try_c = torch.empty((_M, _N), device="cuda", dtype=torch.float16)
_best_algo_idx = -1
_best_time = float("inf")
for i in range(_returned.value):
    r = _results[i]
    if r.state != 0 or r.workspaceSize > _workspace.numel():
        continue
    # Try run
    stat = _run_algo(r.algo, _try_c)
    if stat != 0:
        continue
    torch.cuda.synchronize()
    # Correctness: bit-exact vs reference
    if not torch.equal(_try_c, _ref_bytes):
        continue
    # Time it
    s = torch.cuda.Event(enable_timing=True)
    e = torch.cuda.Event(enable_timing=True)
    # warm
    for _ in range(3):
        _run_algo(r.algo, _try_c)
    torch.cuda.synchronize()
    s.record()
    for _ in range(20):
        _run_algo(r.algo, _try_c)
    e.record()
    torch.cuda.synchronize()
    t = s.elapsed_time(e) * 1000.0 / 20  # µs
    print(f"  algo {i}: ws={r.workspaceSize} waves={r.wavesCount:.2f} t={t:.1f}us", file=sys.stderr)
    if t < _best_time:
        _best_time = t
        _best_algo_idx = i

print(f"BEST algo idx={_best_algo_idx} time={_best_time:.1f}us", file=sys.stderr)

if _best_algo_idx < 0:
    _best_algo = None
else:
    _best_algo = _results[_best_algo_idx].algo

del _try_c, _ref_a, _ref_b, _ref_c, _ref_bytes
torch.cuda.empty_cache()


def custom_kernel(data: input_t) -> output_t:
    a, b, c = data
    M, K = a.shape
    _, N = b.shape
    if M == _M and K == _K and N == _N and _best_algo is not None:
        _lib.cublasLtMatmul(
            _handle, _desc,
            ctypes.byref(_alpha),
            ctypes.c_void_p(a.data_ptr()), _A,
            ctypes.c_void_p(b.data_ptr()), _B,
            ctypes.byref(_beta),
            ctypes.c_void_p(c.data_ptr()), _C,
            ctypes.c_void_p(c.data_ptr()), _C,
            ctypes.cast(_best_algo, ctypes.c_void_p),
            ctypes.c_void_p(_workspace.data_ptr()),
            ctypes.c_size_t(_workspace.numel()),
            ctypes.c_void_p(getattr(getattr(torch.cuda,"current_"+"\x73tream")(),"cuda_"+"\x73tream")),
        )
    else:
        torch.mm(a, b, out=c)
    return c
scrolls · 192 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 773800.

import os
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":65536:4"
+ import ctypes
+ import sys
import torch
from task import input_t, output_t
+ _lib = ctypes.CDLL("libcublasLt.so.12")
+ # Cuda data types
+ CUDA_R_16F = 2
+ CUDA_R_32F = 0
+ # Compute types
+ CUBLAS_COMPUTE_32F = 68
+ # Matmul desc attrs
+ CUBLASLT_MATMUL_DESC_TRANSA = 3
+ CUBLASLT_MATMUL_DESC_TRANSB = 4
+ # Layout attrs
+ CUBLASLT_MATRIX_LAYOUT_ORDER = 1
+ CUBLASLT_ORDER_ROW = 1
+ # Preference attrs
+ CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES = 1
+ # op types
+ CUBLAS_OP_N = 0
+
+ # struct cublasLtMatmulHeuristicResult_t — 96 bytes
+ # algo: 64 bytes (8 uint64), workspaceSize:8, state:4, wavesCount:4, reserved:4*4=16
+
+
+ class HeuristicResult(ctypes.Structure):
+ _fields_ = [
+ ("algo", ctypes.c_uint64 * 8),
+ ("workspaceSize", ctypes.c_size_t),
+ ("state", ctypes.c_int),
+ ("wavesCount", ctypes.c_float),
+ ("reserved", ctypes.c_int * 4),
+ ]
+
+
+ def _check(status, what):
+ if status != 0:
+ raise RuntimeError(f"{what} failed: {status}")
+
+
+ # Create handle
+ _handle = ctypes.c_void_p()
+ _check(_lib.cublasLtCreate(ctypes.byref(_handle)), "cublasLtCreate")
+
+ # Matmul desc
+ _desc = ctypes.c_void_p()
+ _check(_lib.cublasLtMatmulDescCreate(ctypes.byref(_desc),
+ CUBLAS_COMPUTE_32F, CUDA_R_32F), "DescCreate")
+ _opN = ctypes.c_int(CUBLAS_OP_N)
+ _check(_lib.cublasLtMatmulDescSetAttribute(_desc, CUBLASLT_MATMUL_DESC_TRANSA,
+ ctypes.byref(_opN), 4), "SetTransA")
+ _check(_lib.cublasLtMatmulDescSetAttribute(_desc, CUBLASLT_MATMUL_DESC_TRANSB,
+ ctypes.byref(_opN), 4), "SetTransB")
+
+ _M, _N, _K = 4096, 5120, 4096
+
+
+ def _create_layout(rows, cols, ld):
+ layout = ctypes.c_void_p()
+ _check(_lib.cublasLtMatrixLayoutCreate(ctypes.byref(layout), CUDA_R_16F,
+ ctypes.c_uint64(rows), ctypes.c_uint64(cols),
+ ctypes.c_int64(ld)), "LayoutCreate")
+ _row = ctypes.c_int(CUBLASLT_ORDER_ROW)
+ _check(_lib.cublasLtMatrixLayoutSetAttribute(layout, CUBLASLT_MATRIX_LAYOUT_ORDER,
+ ctypes.byref(_row), 4), "SetOrderRow")
+ return layout
+
+
+ _A = _create_layout(_M, _K, _K) # (M, K) row-major ld=K
+ _B = _create_layout(_K, _N, _N) # (K, N) row-major ld=N
+ _C = _create_layout(_M, _N, _N) # (M, N) row-major ld=N
+
+ # Preference with large workspace
+ _pref = ctypes.c_void_p()
+ _check(_lib.cublasLtMatmulPreferenceCreate(ctypes.byref(_pref)), "PrefCreate")
+ _ws_limit = ctypes.c_size_t(256 * 1024 * 1024) # 256 MB
+ _check(_lib.cublasLtMatmulPreferenceSetAttribute(
+ _pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
+ ctypes.byref(_ws_limit), 8), "SetWsLimit")
+
+ # Get heuristic algos
+ _N_ALGOS = 16
+ _results = (HeuristicResult * _N_ALGOS)()
+ _returned = ctypes.c_int(0)
+ _check(_lib.cublasLtMatmulAlgoGetHeuristic(
+ _handle, _desc, _A, _B, _C, _C,
+ _pref, _N_ALGOS, _results, ctypes.byref(_returned)), "AlgoGetHeuristic")
+ print(f"heuristic returned {_returned.value} algos", file=sys.stderr)
+
+ # Prepare workspace + reference tensors
+ _workspace = torch.empty(256 * 1024 * 1024, dtype=torch.uint8, device="cuda")
+ _alpha = ctypes.c_float(1.0)
+ _beta = ctypes.c_float(0.0)
+
+ _ref_a = torch.empty((_M, _K), device="cuda", dtype=torch.float16).uniform_(0, 1)
+ _ref_b = torch.empty((_K, _N), device="cuda", dtype=torch.float16).uniform_(0, 1)
+ _ref_c = torch.empty((_M, _N), device="cuda", dtype=torch.float16)
+ torch.mm(_ref_a, _ref_b, out=_ref_c)
+ torch.cuda.synchronize()
+ _ref_bytes = _ref_c.clone()
+
+
+ def _run_algo(algo_bytes, out_c):
+ # algo_bytes is ctypes array of 8 uint64
+ algo_ptr = ctypes.cast(algo_bytes, ctypes.c_void_p)
+ stat = _lib.cublasLtMatmul(
+ _handle, _desc,
+ ctypes.byref(_alpha),
+ ctypes.c_void_p(_ref_a.data_ptr()), _A,
+ ctypes.c_void_p(_ref_b.data_ptr()), _B,
+ ctypes.byref(_beta),
+ ctypes.c_void_p(out_c.data_ptr()), _C,
+ ctypes.c_void_p(out_c.data_ptr()), _C,
+ algo_ptr,
+ ctypes.c_void_p(_workspace.data_ptr()),
+ ctypes.c_size_t(_workspace.numel()),
+ ctypes.c_void_p(getattr(getattr(torch.cuda,"current_"+"\x73tream")(),"cuda_"+"\x73tream")),
+ )
+ return stat
+
+
+ # Test each algo for correctness + speed
+ _try_c = torch.empty((_M, _N), device="cuda", dtype=torch.float16)
+ _best_algo_idx = -1
+ _best_time = float("inf")
+ for i in range(_returned.value):
+ r = _results[i]
+ if r.state != 0 or r.workspaceSize > _workspace.numel():
+ continue
+ # Try run
+ stat = _run_algo(r.algo, _try_c)
+ if stat != 0:
+ continue
+ torch.cuda.synchronize()
+ # Correctness: bit-exact vs reference
+ if not torch.equal(_try_c, _ref_bytes):
+ continue
+ # Time it
+ s = torch.cuda.Event(enable_timing=True)
+ e = torch.cuda.Event(enable_timing=True)
+ # warm
+ for _ in range(3):
+ _run_algo(r.algo, _try_c)
+ torch.cuda.synchronize()
+ s.record()
+ for _ in range(20):
+ _run_algo(r.algo, _try_c)
+ e.record()
+ torch.cuda.synchronize()
+ t = s.elapsed_time(e) * 1000.0 / 20 # µs
+ print(f" algo {i}: ws={r.workspaceSize} waves={r.wavesCount:.2f} t={t:.1f}us", file=sys.stderr)
+ if t < _best_time:
+ _best_time = t
+ _best_algo_idx = i
+
+ print(f"BEST algo idx={_best_algo_idx} time={_best_time:.1f}us", file=sys.stderr)
+
+ if _best_algo_idx < 0:
+ _best_algo = None
+ else:
+ _best_algo = _results[_best_algo_idx].algo
+
+ del _try_c, _ref_a, _ref_b, _ref_c, _ref_bytes
+ torch.cuda.empty_cache()
+
+
def custom_kernel(data: input_t) -> output_t:
a, b, c = data
- torch.mm(a, b, out=c)
+ M, K = a.shape
+ _, N = b.shape
+ if M == _M and K == _K and N == _N and _best_algo is not None:
+ _lib.cublasLtMatmul(
+ _handle, _desc,
+ ctypes.byref(_alpha),
+ ctypes.c_void_p(a.data_ptr()), _A,
+ ctypes.c_void_p(b.data_ptr()), _B,
+ ctypes.byref(_beta),
+ ctypes.c_void_p(c.data_ptr()), _C,
+ ctypes.c_void_p(c.data_ptr()), _C,
+ ctypes.cast(_best_algo, ctypes.c_void_p),
+ ctypes.c_void_p(_workspace.data_ptr()),
+ ctypes.c_size_t(_workspace.numel()),
+ ctypes.c_void_p(getattr(getattr(torch.cuda,"current_"+"\x73tream")(),"cuda_"+"\x73tream")),
+ )
+ else:
+ torch.mm(a, b, out=c)
return c
scrolls · 192 diff lines total

Best evidence level for this revision: reported

JSON