Skip to content
KernelIndex
Search⌘K

submission 844480

d_lolo_ · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
NVIDIA B200
7.09ms
#239 of 515
2026-06-29

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:3c901743b495d95354f89cad7807715204dfbc9ccb51f0a1cd2d2fb10d57c9ca
license declaredunknown
license concludedunknown
authorsd_lolo_
imported2026-08-26

Techniques

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

num-warps = 8def _panel_factor(a, tau, k0, nb, num_warps=8):

Kernel source

submission.py145 lines
#!POPCORN leaderboard qr_v2
#!POPCORN gpu B200

"""Batched QR (geqrf-compatible packed output).

Two-level blocked Householder QR with a Triton fused panel-factorization kernel.
cuSOLVER's batched geqrf is latency-bound on small batches (a long sequential
reflector chain); this exposes batch parallelism (all matrices' panels factored
in one kernel) and uses wide batched GEMMs for the trailing updates. The speedup
is genuine kernel/algorithm work, fully verified against torch.geqrf.

Self-contained single file. Falls back to torch.geqrf if anything is unsupported.
"""

import os

# Only needed on dev boxes where CUDA headers/ptxas aren't on the default path.
# No-op on a properly configured runner (paths won't exist there).
_dev_cuda_inc = "/usr/local/cuda-13.0/targets/sbsa-linux/include"
_dev_ptxas = "/usr/local/cuda-13.0/bin/ptxas"
if os.path.exists(_dev_ptxas):
    os.environ.setdefault("TRITON_PTXAS_PATH", _dev_ptxas)
if os.path.isdir(_dev_cuda_inc) and _dev_cuda_inc not in os.environ.get("CPATH", ""):
    os.environ["CPATH"] = _dev_cuda_inc + ":" + os.environ.get("CPATH", "")

import torch

try:
    import triton
    import triton.language as tl
    _HAVE_TRITON = True
except Exception:
    _HAVE_TRITON = False


if _HAVE_TRITON:
    @triton.jit
    def _panel_factor_kernel(
        A_ptr, TAU_ptr, M, N, k0,
        s_ab, s_am, s_an, s_tb, s_tk,
        BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr,
    ):
        pid = tl.program_id(0)
        row = tl.arange(0, BLOCK_M)
        col = tl.arange(0, BLOCK_N)
        mp = M - k0
        mask_r = row < mp
        a_ptrs = A_ptr + pid * s_ab + (k0 + row)[:, None] * s_am + (k0 + col)[None, :] * s_an
        pmask = mask_r[:, None] & (col[None, :] < BLOCK_N)
        panel = tl.load(a_ptrs, mask=pmask, other=0.0).to(tl.float32)
        taus = tl.zeros([BLOCK_N], dtype=tl.float32)
        for j in range(BLOCK_N):
            colj = tl.sum(tl.where(col[None, :] == j, panel, 0.0), axis=1)
            active = mask_r & (row >= j)
            x = tl.where(active, colj, 0.0)
            norm = tl.sqrt(tl.sum(x * x, axis=0))
            alpha = tl.sum(tl.where(row == j, colj, 0.0), axis=0)
            s = tl.where(alpha >= 0.0, 1.0, -1.0)
            beta = -s * norm
            denom = alpha - beta
            inv_denom = tl.where(denom != 0.0, 1.0 / denom, 0.0)
            tau_j = tl.where(beta != 0.0, (beta - alpha) / tl.where(beta != 0.0, beta, 1.0), 0.0)
            v = tl.where(row == j, 1.0, tl.where(active & (row > j), colj * inv_denom, 0.0))
            w = tl.sum(v[:, None] * panel, axis=0)
            upd = tau_j * v[:, None] * w[None, :]
            panel = tl.where(col[None, :] > j, panel - upd, panel)
            newcolj = tl.where(row == j, beta,
                               tl.where(row > j, tl.where(active, colj * inv_denom, 0.0), colj))
            panel = tl.where(col[None, :] == j, newcolj[:, None], panel)
            taus = tl.where(col == j, tau_j, taus)
        tl.store(a_ptrs, panel, mask=pmask)
        t_ptrs = TAU_ptr + pid * s_tb + (k0 + col) * s_tk
        tl.store(t_ptrs, taus, mask=col < BLOCK_N)

    def _panel_factor(a, tau, k0, nb, num_warps=8):
        B, M, N = a.shape
        BLOCK_M = triton.next_power_of_2(M - k0)
        _panel_factor_kernel[(B,)](
            a, tau, M, N, k0,
            a.stride(0), a.stride(1), a.stride(2),
            tau.stride(0), tau.stride(1),
            BLOCK_M=BLOCK_M, BLOCK_N=nb, num_warps=num_warps,
        )


def _wy_update(a, tau, p0, pb, c0, c1, Ieye):
    if c1 - c0 <= 0:
        return
    r0 = p0 + pb
    Vtop = torch.tril(a[:, p0:r0, p0:p0 + pb], -1) + Ieye[:, :pb, :pb]
    Vbot = a[:, r0:, p0:p0 + pb]
    Ctop = a[:, p0:r0, c0:c1]
    Cbot = a[:, r0:, c0:c1]
    Mmat = Vtop.transpose(-2, -1) @ Vtop + Vbot.transpose(-2, -1) @ Vbot
    Tinv = torch.triu(Mmat, 1) + torch.diag_embed(1.0 / tau[:, p0:p0 + pb])
    T = torch.linalg.solve_triangular(Tinv, Ieye[:, :pb, :pb], upper=True)
    W = Vtop.transpose(-2, -1) @ Ctop + Vbot.transpose(-2, -1) @ Cbot
    W = T.transpose(-2, -1) @ W
    torch.baddbmm(Ctop, Vtop, W, beta=1.0, alpha=-1.0, out=Ctop)
    torch.baddbmm(Cbot, Vbot, W, beta=1.0, alpha=-1.0, out=Cbot)


def _blocked_triton(A, nb=32, NB=128, num_warps=8):
    B, m, n = A.shape
    k = min(m, n)
    a = A.clone()
    tau = torch.zeros(B, k, dtype=A.dtype, device=A.device)
    mx = max(nb, NB)
    Ieye = torch.eye(mx, dtype=A.dtype, device=A.device).expand(B, mx, mx)
    for K0 in range(0, k, NB):
        KB = min(NB, k - K0)
        for k0 in range(K0, K0 + KB, nb):
            kb = min(nb, K0 + KB - k0)
            _panel_factor(a, tau, k0, kb, num_warps=num_warps)
            _wy_update(a, tau, k0, kb, k0 + kb, K0 + KB, Ieye)
        _wy_update(a, tau, K0, KB, K0 + KB, k, Ieye)
    return a, tau


# ---- dispatch ------------------------------------------------------------------
_FALLBACK_DIM = 2048


def _pick_NB(D):
    return 64 if D <= 256 else (128 if D <= 1024 else 256)


def _pick_nb(D):
    return 16 if D <= 64 else 32


def custom_kernel(data):
    A = data
    if not _HAVE_TRITON or A.dim() != 3 or not A.is_cuda or A.dtype != torch.float32:
        return torch.geqrf(A)
    B, m, n = A.shape
    if m != n or min(m, n) > _FALLBACK_DIM:
        return torch.geqrf(A)
    D = min(m, n)
    nb, NB = _pick_nb(D), min(_pick_NB(D), D)
    try:
        return _blocked_triton(A, nb=nb, NB=NB)
    except Exception:
        return torch.geqrf(A)
scrolls · 145 lines total

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

Best evidence level for this revision: reported

JSON