Skip to content
KernelIndex
Search⌘K

submission 107135

Venkat Raman · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_direct_assign.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-gemv-107135?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
23.3µs
#66 of 678
2025-11-26

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:a3196b231c44f924e6035485d5642793dc9674576f8d1582638b427b394ec1b0
license declaredunknown
license concludedunknown
authorsVenkat Raman
imported2026-08-15

Techniques

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

fp4"""NVFP4 GEMV - Direct Assignment without Intermediate

Kernel source

submission_direct_assign.py38 lines
"""NVFP4 GEMV - Direct Assignment without Intermediate

Based on submission_final_ultra.py (24.4μs baseline on server).

Try to minimize intermediate tensor creation by restructuring the assignment.
"""

import torch

_s = [torch.cuda.Stream() for _ in range(8)]
_mm = torch._scaled_mm


def custom_kernel(data):
    a, b, _, _, sfa_p, sfb_p, c = data
    L = a.shape[2]

    for l in range(L):
        with torch.cuda.stream(_s[l]):
            # Get result first
            result = _mm(
                a.select(2, l),
                b.select(2, l).T,
                sfa_p.select(-1, l).permute(2, 4, 0, 1, 3).flatten(),
                sfb_p.select(-1, l).permute(2, 4, 0, 1, 3).flatten(),
                bias=None,
                out_dtype=torch.float16
            )

            # Direct select on both sides - may avoid some indexing overhead
            c.select(2, l).select(1, 0).copy_(result.select(1, 0))

    torch.cuda.synchronize()
    return c


__all__ = ["custom_kernel"]
scrolls · 38 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 106998.

- """NVFP4 GEMV - Target 12μs: All Optimizations Combined
+ """NVFP4 GEMV - Direct Assignment without Intermediate
- Building on submission_final_ultra.py (24.4μs on server), this adds:
+ Based on submission_final_ultra.py (24.4μs baseline on server).
- 1. Scale factor caching (by tensor data_ptr) - eliminates ~L×2 permute kernels
- 2. Optimized stream wait (wait_stream instead of synchronize per stream)
- 3. Direct view operations (select().copy_() instead of slice assignment)
-
- Target: 24.4 μs → 12 μs on competition server
+ Try to minimize intermediate tensor creation by restructuring the assignment.
"""
import torch
- # Global caches
- _streams = {}
- _scale_cache = {}
+ _s = [torch.cuda.Stream() for _ in range(8)]
+ _mm = torch._scaled_mm
- def get_stream(idx):
- """Get or create CUDA stream."""
- if idx not in _streams:
- _streams[idx] = torch.cuda.Stream()
- return _streams[idx]
-
-
- def permuted_to_blocked(permuted_tensor, l_idx):
- """Convert pre-permuted scale factor to blocked format."""
- tensor = permuted_tensor.select(-1, l_idx)
- return tensor.permute(2, 4, 0, 1, 3).reshape(-1, 32, 16).flatten().contiguous()
-
-
- def get_all_scales(sfa_permuted, sfb_permuted, L):
- """Get cached scales or compute and cache them."""
- # Use data pointer as cache key
- key = (sfa_permuted.data_ptr(), sfb_permuted.data_ptr(), L)
-
- if key not in _scale_cache:
- # Compute all scales at once and cache
- scales_a = []
- scales_b = []
- for l in range(L):
- scales_a.append(permuted_to_blocked(sfa_permuted, l))
- scales_b.append(permuted_to_blocked(sfb_permuted, l))
- _scale_cache[key] = (scales_a, scales_b)
-
- return _scale_cache[key]
-
-
def custom_kernel(data):
- """Optimized NVFP4 GEMV kernel targeting 12μs."""
- a, b, sfa, sfb, sfa_permuted, sfb_permuted, c = data
- M, K_half, L = a.shape
+ a, b, _, _, sfa_p, sfb_p, c = data
+ L = a.shape[2]
- # Get cached scale factors
- scales_a, scales_b = get_all_scales(sfa_permuted, sfb_permuted, L)
+ for l in range(L):
+ with torch.cuda.stream(_s[l]):
+ # Get result first
+ result = _mm(
+ a.select(2, l),
+ b.select(2, l).T,
+ sfa_p.select(-1, l).permute(2, 4, 0, 1, 3).flatten(),
+ sfb_p.select(-1, l).permute(2, 4, 0, 1, 3).flatten(),
+ bias=None,
+ out_dtype=torch.float16
+ )
- if L == 1:
- # L=1: Single GEMM, no streams needed
- result = torch._scaled_mm(
- a[:, :, 0],
- b[:, :, 0].transpose(0, 1),
- scales_a[0],
- scales_b[0],
- bias=None,
- out_dtype=torch.float16
- )
- # Direct copy using select (potentially faster than slice assignment)
- c.select(2, 0).select(1, 0).copy_(result.select(1, 0))
+ # Direct select on both sides - may avoid some indexing overhead
+ c.select(2, l).select(1, 0).copy_(result.select(1, 0))
- else:
- # L>1: Parallel streams with pre-cached scales
- for l_idx in range(L):
- stream = get_stream(l_idx)
- with torch.cuda.stream(stream):
- result = torch._scaled_mm(
- a[:, :, l_idx],
- b[:, :, l_idx].transpose(0, 1),
- scales_a[l_idx],
- scales_b[l_idx],
- bias=None,
- out_dtype=torch.float16
- )
- c.select(2, l_idx).select(1, 0).copy_(result.select(1, 0))
-
- # Efficient stream synchronization
- for l_idx in range(L):
- torch.cuda.current_stream().wait_stream(get_stream(l_idx))
-
torch.cuda.synchronize()
return c
scrolls · 111 diff lines total

Best evidence level for this revision: reported

JSON