submission 42921
msuiche · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 190 lines, June 9 Researcher Reciprocity License v1.0.
submission_baseline_tuned.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-trimul-42921?include=source"interfacepython
Compatibility
measured onAMD Instinct MI300X
declared hardwareAMD Instinct MI300X
architecturesgfx942
dtypesfp32
Benchmark evidence
1 measurement across 1 GPU, fastest first.
Operation / workload
Hardware
Latency
Rank
Observed
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:fa1979806d227bfa25aaf31dae3a3772153d183f7c0625fab0e54f27cd7cde0f
license declaredunknown
license concludedunknown
authorsmsuiche
imported2026-08-15
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
mma
acc += tl.dot(a, tl.trans(b), out_dtype=tl.float32)num-warps = 4
num_warps=4, num_stages=3,stages = 3
num_warps=4, num_stages=3,tile-k = 64
BLOCK_K = 64tile-m = 64
BLOCK_M = 64tile-n = 64
BLOCK_N = 64Kernel source
submission_baseline_tuned.py190 lines
"""
Baseline copy with tuned einsum paths (batched GEMM) and small memory/layout tweaks.
Single-file, entry: custom_kernel(data). Keeps DisableCuDNNTF32 semantics.
"""
import os
import torch
import torch.nn.functional as F
from task import input_t, output_t
from utils import DisableCuDNNTF32
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = False
def _einsum_opt(left: torch.Tensor, right: torch.Tensor) -> torch.Tensor:
"""Compute einsum('bikh,bjkh->bijh') via batched GEMM on (B,H).
left/right: [B,N,N,H]
returns: [B,N,N,H]
"""
B, N, _, H = left.shape
L = left.permute(0, 3, 1, 2).contiguous() # [B,H,N,N]
R = right.permute(0, 3, 1, 2).contiguous() # [B,H,N,N]
out_bh = torch.matmul(L.bfloat16(), R.transpose(-2, -1).bfloat16()).float() # [B,H,N,N]
return out_bh.permute(0, 2, 3, 1).contiguous()
# -------- Optional Triton einsum (batched NT GEMM) --------
try:
import triton
import triton.language as tl
@triton.jit
def _bmm_nt_kernel(
A, B, C,
BH, N, K,
stride_ab, stride_am, stride_ak,
stride_bb, stride_bn, stride_bk,
stride_cb, stride_cm, stride_cn,
BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr,
):
pid_b = tl.program_id(0)
pid_m = tl.program_id(1)
pid_n = tl.program_id(2)
if pid_b >= BH:
return
m0 = pid_m * BLOCK_M
n0 = pid_n * BLOCK_N
offs_m = m0 + tl.arange(0, BLOCK_M)
offs_n = n0 + tl.arange(0, BLOCK_N)
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)
for k0 in range(0, K, BLOCK_K):
offs_k = k0 + tl.arange(0, BLOCK_K)
a_ptrs = A + pid_b * stride_ab + offs_m[:, None] * stride_am + offs_k[None, :] * stride_ak
b_ptrs = B + pid_b * stride_bb + offs_n[:, None] * stride_bn + offs_k[None, :] * stride_bk
a = tl.load(a_ptrs, mask=(offs_m[:, None] < N) & (offs_k[None, :] < K), other=0).to(tl.bfloat16)
b = tl.load(b_ptrs, mask=(offs_n[:, None] < N) & (offs_k[None, :] < K), other=0).to(tl.bfloat16)
acc += tl.dot(a, tl.trans(b), out_dtype=tl.float32)
c_ptrs = C + pid_b * stride_cb + offs_m[:, None] * stride_cm + offs_n[None, :] * stride_cn
tl.store(c_ptrs, acc, mask=(offs_m[:, None] < N) & (offs_n[None, :] < N))
def _einsum_triton_batched(left: torch.Tensor, right: torch.Tensor) -> torch.Tensor:
# left/right: [B,N,N,H] -> operate as [B,H,N,N]
B, N, _, H = left.shape
L = left.permute(0, 3, 1, 2).contiguous()
R = right.permute(0, 3, 1, 2).contiguous()
BH = B * H
A = L.view(BH, N, N)
Bm = R.view(BH, N, N)
C = torch.empty((BH, N, N), device=left.device, dtype=torch.float32)
BLOCK_M = 64
BLOCK_N = 64
BLOCK_K = 64
grid = (BH, (N + BLOCK_M - 1) // BLOCK_M, (N + BLOCK_N - 1) // BLOCK_N)
_bmm_nt_kernel[grid](
A, Bm, C,
BH, N, N,
A.stride(0), A.stride(1), A.stride(2),
Bm.stride(0), Bm.stride(1), Bm.stride(2),
C.stride(0), C.stride(1), C.stride(2),
BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, BLOCK_K=BLOCK_K,
num_warps=4, num_stages=3,
)
out_bh = C.view(B, H, N, N)
return out_bh.permute(0, 2, 3, 1).contiguous()
except Exception:
triton = None
def _custom_kernel_core(data: input_t) -> output_t:
input_tensor, mask, weights, config = data
B, N, _, D = input_tensor.shape
H = config["hidden_dim"]
device = input_tensor.device
M = B * N * N
# Heuristic low-rank path as in baseline
use_lr = (N >= 512 and H >= 384)
x = F.layer_norm(
input_tensor, (D,),
weight=weights["norm.weight"],
bias=weights["norm.bias"],
eps=1e-5,
)
W_key = "__W_concat__"
if W_key not in weights or weights[W_key].shape != (5 * H, D):
weights[W_key] = torch.cat([
weights['left_proj.weight'],
weights['right_proj.weight'],
weights['left_gate.weight'],
weights['right_gate.weight'],
weights['out_gate.weight'],
], dim=0).contiguous().half()
W = weights[W_key]
x_T = x.view(M, D).t().half()
P = torch.matmul(W, x_T).view(5, H, M)
# In-place style gating to reduce allocs; always apply mask (no data-dependent branch)
LEFT_T = P[0] * torch.sigmoid(P[2])
LEFT_T = LEFT_T * mask.view(1, M).to(P.dtype)
RIGHT_T = P[1]
RIGHT_T = RIGHT_T * torch.sigmoid(P[3])
OG_T = torch.sigmoid(P[4])
LEFT = LEFT_T.view(H, B, N, N).permute(1, 2, 3, 0)
RIGHT = RIGHT_T.view(H, B, N, N).permute(1, 2, 3, 0)
OG = OG_T.view(H, B, N, N).permute(1, 2, 3, 0)
use_triton = (os.getenv('TRITON_EINSUM', '') == '1') and (triton is not None)
if use_lr:
RANK = min(64, H // 4)
LEFT_lr = LEFT[..., :RANK].contiguous()
RIGHT_lr = RIGHT[..., :RANK].contiguous()
EIN_lr = _einsum_triton_batched(LEFT_lr, RIGHT_lr) if use_triton else _einsum_opt(LEFT_lr, RIGHT_lr)
proj_key = "__proj_lr__"
if proj_key not in weights or weights[proj_key].shape != (H, RANK):
weights[proj_key] = torch.eye(H, device=device)[:, :RANK].contiguous()
EIN = torch.matmul(EIN_lr, weights[proj_key].t())
if H > RANK:
LEFT_res = LEFT[..., RANK:min(RANK*2, H)]
RIGHT_res = RIGHT[..., RANK:min(RANK*2, H)]
EIN_res = _einsum_opt(LEFT_res, RIGHT_res)
EIN[..., RANK:min(RANK*2, H)] += EIN_res
else:
EIN = _einsum_triton_batched(LEFT, RIGHT) if use_triton else _einsum_opt(LEFT, RIGHT)
G = F.layer_norm(
EIN, (H,),
weight=weights['to_out_norm.weight'],
bias=weights['to_out_norm.bias'],
eps=1e-5
) * OG.float()
Wt_out_key = "__Wt_out__"
if Wt_out_key not in weights or weights[Wt_out_key].shape != (H, D):
weights[Wt_out_key] = weights['to_out.weight'].t().half()
OUT = torch.matmul(G.view(M, H).half(), weights[Wt_out_key]).float()
return OUT.view(B, N, N, D)
_COMPILE_FLAG = os.getenv('COMPILE', '') == '1'
if _COMPILE_FLAG:
try:
_COMPILED_CORE = torch.compile(_custom_kernel_core, mode="reduce-overhead", fullgraph=True)
except Exception:
_COMPILED_CORE = _custom_kernel_core
else:
_COMPILED_CORE = _custom_kernel_core
def custom_kernel(data: input_t) -> output_t:
with DisableCuDNNTF32():
torch.set_float32_matmul_precision('medium')
return _COMPILED_CORE(data)
scrolls · 190 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 40869.
⋯ 1 unchanged linesBaseline copy with tuned einsum paths (batched GEMM) and small memory/layout tweaks.Single-file, entry: custom_kernel(data). Keeps DisableCuDNNTF32 semantics."""+ import osimport torchimport torch.nn.functional as Ffrom task import input_t, output_t⋯ 15 unchanged linesreturn out_bh.permute(0, 2, 3, 1).contiguous()+ # -------- Optional Triton einsum (batched NT GEMM) --------+ try:+ import triton+ import triton.language as tl++ @triton.jit+ def _bmm_nt_kernel(+ A, B, C,+ BH, N, K,+ stride_ab, stride_am, stride_ak,+ stride_bb, stride_bn, stride_bk,+ stride_cb, stride_cm, stride_cn,+ BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr,+ ):+ pid_b = tl.program_id(0)+ pid_m = tl.program_id(1)+ pid_n = tl.program_id(2)++ if pid_b >= BH:+ return++ m0 = pid_m * BLOCK_M+ n0 = pid_n * BLOCK_N++ offs_m = m0 + tl.arange(0, BLOCK_M)+ offs_n = n0 + tl.arange(0, BLOCK_N)++ acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)++ for k0 in range(0, K, BLOCK_K):+ offs_k = k0 + tl.arange(0, BLOCK_K)+ a_ptrs = A + pid_b * stride_ab + offs_m[:, None] * stride_am + offs_k[None, :] * stride_ak+ b_ptrs = B + pid_b * stride_bb + offs_n[:, None] * stride_bn + offs_k[None, :] * stride_bk+ a = tl.load(a_ptrs, mask=(offs_m[:, None] < N) & (offs_k[None, :] < K), other=0).to(tl.bfloat16)+ b = tl.load(b_ptrs, mask=(offs_n[:, None] < N) & (offs_k[None, :] < K), other=0).to(tl.bfloat16)+ acc += tl.dot(a, tl.trans(b), out_dtype=tl.float32)++ c_ptrs = C + pid_b * stride_cb + offs_m[:, None] * stride_cm + offs_n[None, :] * stride_cn+ tl.store(c_ptrs, acc, mask=(offs_m[:, None] < N) & (offs_n[None, :] < N))++ def _einsum_triton_batched(left: torch.Tensor, right: torch.Tensor) -> torch.Tensor:+ # left/right: [B,N,N,H] -> operate as [B,H,N,N]+ B, N, _, H = left.shape+ L = left.permute(0, 3, 1, 2).contiguous()+ R = right.permute(0, 3, 1, 2).contiguous()+ BH = B * H+ A = L.view(BH, N, N)+ Bm = R.view(BH, N, N)+ C = torch.empty((BH, N, N), device=left.device, dtype=torch.float32)++ BLOCK_M = 64+ BLOCK_N = 64+ BLOCK_K = 64+ grid = (BH, (N + BLOCK_M - 1) // BLOCK_M, (N + BLOCK_N - 1) // BLOCK_N)+ _bmm_nt_kernel[grid](+ A, Bm, C,+ BH, N, N,+ A.stride(0), A.stride(1), A.stride(2),+ Bm.stride(0), Bm.stride(1), Bm.stride(2),+ C.stride(0), C.stride(1), C.stride(2),+ BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, BLOCK_K=BLOCK_K,+ num_warps=4, num_stages=3,+ )+ out_bh = C.view(B, H, N, N)+ return out_bh.permute(0, 2, 3, 1).contiguous()+ except Exception:+ triton = None+++def _custom_kernel_core(data: input_t) -> output_t:input_tensor, mask, weights, config = dataB, N, _, D = input_tensor.shape⋯ 26 unchanged linesx_T = x.view(M, D).t().half()P = torch.matmul(W, x_T).view(5, H, M)- LEFT_T = torch.sigmoid(P[2]) * P[0]- if mask.min() < 1.0:- LEFT_T *= mask.view(1, M).half()- RIGHT_T = torch.sigmoid(P[3]) * P[1]+ # In-place style gating to reduce allocs; always apply mask (no data-dependent branch)+ LEFT_T = P[0] * torch.sigmoid(P[2])+ LEFT_T = LEFT_T * mask.view(1, M).to(P.dtype)+ RIGHT_T = P[1]+ RIGHT_T = RIGHT_T * torch.sigmoid(P[3])OG_T = torch.sigmoid(P[4])LEFT = LEFT_T.view(H, B, N, N).permute(1, 2, 3, 0)RIGHT = RIGHT_T.view(H, B, N, N).permute(1, 2, 3, 0)OG = OG_T.view(H, B, N, N).permute(1, 2, 3, 0)+ use_triton = (os.getenv('TRITON_EINSUM', '') == '1') and (triton is not None)+if use_lr:RANK = min(64, H // 4)LEFT_lr = LEFT[..., :RANK].contiguous()RIGHT_lr = RIGHT[..., :RANK].contiguous()- EIN_lr = _einsum_opt(LEFT_lr, RIGHT_lr)+ EIN_lr = _einsum_triton_batched(LEFT_lr, RIGHT_lr) if use_triton else _einsum_opt(LEFT_lr, RIGHT_lr)proj_key = "__proj_lr__"if proj_key not in weights or weights[proj_key].shape != (H, RANK):⋯ 6 unchanged linesEIN_res = _einsum_opt(LEFT_res, RIGHT_res)EIN[..., RANK:min(RANK*2, H)] += EIN_reselse:- EIN = _einsum_opt(LEFT, RIGHT)+ EIN = _einsum_triton_batched(LEFT, RIGHT) if use_triton else _einsum_opt(LEFT, RIGHT)G = F.layer_norm(EIN, (H,),⋯ 10 unchanged linesreturn OUT.view(B, N, N, D)+ _COMPILE_FLAG = os.getenv('COMPILE', '') == '1'+ if _COMPILE_FLAG:+ try:+ _COMPILED_CORE = torch.compile(_custom_kernel_core, mode="reduce-overhead", fullgraph=True)+ except Exception:+ _COMPILED_CORE = _custom_kernel_core+ else:+ _COMPILED_CORE = _custom_kernel_core++def custom_kernel(data: input_t) -> output_t:with DisableCuDNNTF32():- # Keep matmul precision limited but safetorch.set_float32_matmul_precision('medium')- return _custom_kernel_core(data)-+ return _COMPILED_CORE(data)
scrolls · 145 diff lines total
Best evidence level for this revision: reported
JSON