Skip to content
KernelIndex
Search⌘K

submission 69558

mdouglas · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-gemv-69558?include=source"
interfacepython
Compatibility
measured onNVIDIA B200
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp8_e4m3, nvfp4

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
NVFP4 GEMVsuite of 3 cases
NVIDIA B200
1.15ms
#625 of 678
2025-11-11

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:e613d0a275ba9459b958e6b57a3134ed4cdbede6381dcdbb1ffe1e985fb7f933
license declaredunknown
license concludedunknown
authorsmdouglas
imported2026-08-15

Techniques

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

autotune@torch.compile(mode="max-autotune-no-cudagraphs")
fp4PyTorch reference implementation of NVFP4 block-scaled GEMV.

Kernel source

submission.py59 lines
import torch
from task import input_t, output_t

torch._dynamo.config.cache_size_limit = 32

# Helper function to convert scale factor tensor to blocked format
@torch.compile(dynamic=False, mode="reduce-overhead", fullgraph=True)
def to_blocked(input_matrix):
    rows, cols = input_matrix.shape
    blocks = input_matrix.view(rows // 128, 128, cols // 4, 4).permute(0, 2, 1, 3)
    #rearranged = blocks.reshape(-1, 4, 32, 4).transpose(1, 2).reshape(-1, 32, 16)
    #return rearranged.flatten()
    return blocks.reshape(-1, 4, 32, 4).transpose(1, 2).flatten()

@torch.compile(dynamic=False, mode="reduce-overhead", fullgraph=True)
def _inner(a_ref, b_ref, sfa_ref, sfb_ref, l_idx):
    scale_a = to_blocked(sfa_ref[..., l_idx])
    scale_b = to_blocked(sfb_ref[..., l_idx])

    # (m, k) @ (n, k).T -> (m, n)
    return torch._scaled_mm(
        a_ref[..., l_idx],
        b_ref[..., l_idx].transpose(0, 1),
        scale_a,
        scale_b,
        bias=None,
        out_dtype=torch.float16,
    )[:, 0]


@torch.compile(mode="max-autotune-no-cudagraphs")
def batched_gemv_impl(a_ref, b_ref, c_ref, sfa_ref, sfb_ref):
    _, _, l = b_ref.shape

    for l_idx in range(l):
        c_ref[:, 0, l_idx] = _inner(a_ref, b_ref, sfa_ref, sfb_ref, l_idx)

    return c_ref

def custom_kernel(
    data: input_t,
) -> output_t:
    """
    PyTorch reference implementation of NVFP4 block-scaled GEMV.
    """
    a_ref, b_ref, sfa_ref_cpu, sfb_ref_cpu, _, _, c_ref = data

    sfa_ref_gpu = sfa_ref_cpu.cuda()
    sfb_ref_gpu = sfb_ref_cpu.cuda()

    return batched_gemv_impl(
        a_ref,
        b_ref,
        c_ref,
        sfa_ref_gpu,
        sfb_ref_gpu,
    )

scrolls · 59 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 69391.

import torch
from task import input_t, output_t
+ torch._dynamo.config.cache_size_limit = 32
- # Helper function for ceiling division
- def ceil_div(a, b):
- return (a + b - 1) // b
-
-
# Helper function to convert scale factor tensor to blocked format
- @torch.compile()
+ @torch.compile(dynamic=False, mode="reduce-overhead", fullgraph=True)
def to_blocked(input_matrix):
rows, cols = input_matrix.shape
+ blocks = input_matrix.view(rows // 128, 128, cols // 4, 4).permute(0, 2, 1, 3)
+ #rearranged = blocks.reshape(-1, 4, 32, 4).transpose(1, 2).reshape(-1, 32, 16)
+ #return rearranged.flatten()
+ return blocks.reshape(-1, 4, 32, 4).transpose(1, 2).flatten()
- # Please ensure rows and cols are multiples of 128 and 4 respectively
- n_row_blocks = ceil_div(rows, 128)
- n_col_blocks = ceil_div(cols, 4)
-
- padded = input_matrix
- blocks = padded.view(n_row_blocks, 128, n_col_blocks, 4).permute(0, 2, 1, 3)
- rearranged = blocks.reshape(-1, 4, 32, 4).transpose(1, 2).reshape(-1, 32, 16)
-
- return rearranged.flatten()
-
-
+ @torch.compile(dynamic=False, mode="reduce-overhead", fullgraph=True)
def _inner(a_ref, b_ref, sfa_ref, sfb_ref, l_idx):
- scale_a = to_blocked(sfa_ref[:, :, l_idx])
- scale_b = to_blocked(sfb_ref[:, :, l_idx])
+ scale_a = to_blocked(sfa_ref[..., l_idx])
+ scale_b = to_blocked(sfb_ref[..., l_idx])
+
# (m, k) @ (n, k).T -> (m, n)
- # (m, k) @ (n, k).T -> (m, n)
- res = torch._scaled_mm(
- a_ref[:, :, l_idx],
- b_ref[:, :, l_idx].transpose(0, 1),
+ return torch._scaled_mm(
+ a_ref[..., l_idx],
+ b_ref[..., l_idx].transpose(0, 1),
scale_a,
scale_b,
bias=None,
out_dtype=torch.float16,
- )
- return res[:, 0]
-
+ )[:, 0]
+
+
+ @torch.compile(mode="max-autotune-no-cudagraphs")
+ def batched_gemv_impl(a_ref, b_ref, c_ref, sfa_ref, sfb_ref):
+ _, _, l = b_ref.shape
+
+ for l_idx in range(l):
+ c_ref[:, 0, l_idx] = _inner(a_ref, b_ref, sfa_ref, sfb_ref, l_idx)
+
+ return c_ref
+
def custom_kernel(
data: input_t,
) -> output_t:
⋯ 2 unchanged lines
"""
a_ref, b_ref, sfa_ref_cpu, sfb_ref_cpu, _, _, c_ref = data
- # Get dimensions from MxNxL layout
- _, _, l = c_ref.shape
-
sfa_ref_gpu = sfa_ref_cpu.cuda()
sfb_ref_gpu = sfb_ref_cpu.cuda()
- # Call torch._scaled_mm to compute the GEMV result
- for l_idx in range(l):
- c_ref[:, 0, l_idx] = _inner(a_ref, b_ref, sfa_ref_gpu, sfb_ref_gpu, l_idx)
+ return batched_gemv_impl(
+ a_ref,
+ b_ref,
+ c_ref,
+ sfa_ref_gpu,
+ sfb_ref_gpu,
+ )
- return c_ref
scrolls · 90 diff lines total

Best evidence level for this revision: reported

JSON