Skip to content
KernelIndex
Search⌘K

submission 103289

Venkat Raman · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_hybrid_ultra.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-nvfp4-gemv-103289?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
55.5µs
#287 of 678
2025-11-25

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:187469f51d09f0c1f603aec4ef64ea8ed684b3ce9acb07b538490aff69a6cfab
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 Ultra-Optimized Hybrid Kernel

Kernel source

submission_hybrid_ultra.py241 lines
"""NVFP4 GEMV Ultra-Optimized Hybrid Kernel

Key optimizations:
1. Skip to_blocked() by using pre-permuted scale factors directly
2. Use torch._scaled_mm for L=1 (Tensor Cores)
3. Use CuTe DSL for L>1 (efficient batching)

The pre-permuted scale factors (sfa_permuted, sfb_permuted) are already in a
layout that can be converted to blocked format with a simple permute + reshape,
saving ~43 μs per call compared to the full to_blocked() computation.
"""

import torch
from task import input_t, output_t

# ============================================================================
# TORCH._SCALED_MM PATH (for L=1) - ULTRA OPTIMIZED
# ============================================================================

def permuted_to_blocked(permuted_tensor, l_idx=0):
    """
    Convert pre-permuted scale factor to blocked format for torch._scaled_mm.
    
    Input: [32, 4, rest_m, 4, rest_k, L] (sfa_permuted/sfb_permuted format)
    Output: Flattened blocked tensor compatible with torch._scaled_mm
    
    This is ~10x faster than to_blocked() because the data is already pre-arranged.
    """
    # Extract the L slice and convert to blocked format
    # [32, 4, rest_m, 4, rest_k] -> [rest_m, rest_k, 32, 4, 4] -> [-1, 32, 16]
    tensor = permuted_tensor[:, :, :, :, :, l_idx]  # [32, 4, rest_m, 4, rest_k]
    return tensor.permute(2, 4, 0, 1, 3).reshape(-1, 32, 16).flatten().contiguous()


def torch_scaled_mm_kernel_ultra(a, b, sfa_permuted, sfb_permuted, c):
    """Ultra-fast path for L=1 using pre-computed blocked scale factors."""
    # Convert pre-permuted to blocked format (fast path)
    scale_a = permuted_to_blocked(sfa_permuted, 0)
    scale_b = permuted_to_blocked(sfb_permuted, 0)
    
    result = torch._scaled_mm(
        a[:, :, 0],
        b[:, :, 0].transpose(0, 1),
        scale_a,
        scale_b,
        bias=None,
        out_dtype=torch.float16,
    )
    c[:, 0, 0] = result[:, 0]
    return c


# ============================================================================
# CUTE DSL PATH (for L>1) - unchanged from hybrid_best
# ============================================================================

import cutlass
import cutlass.cute as cute
from cutlass.cute.runtime import make_ptr
import cutlass.utils.blockscaled_layout as blockscaled_utils

mma_tiler_mnk = (128, 1, 64)
ab_dtype = cutlass.Float4E2M1FN
sf_dtype = cutlass.Float8E4M3FN
c_dtype = cutlass.Float16
sf_vec_size = 16
threads_per_cta = 128


@cute.kernel
def cute_kernel(
    mA_mkl: cute.Tensor,
    mB_nkl: cute.Tensor,
    mSFA_mkl: cute.Tensor,
    mSFB_nkl: cute.Tensor,
    mC_mnl: cute.Tensor,
):
    bidx, bidy, bidz = cute.arch.block_idx()
    tidx, _, _ = cute.arch.thread_idx()

    gA_mkl = cute.local_tile(
        mA_mkl, cute.slice_(mma_tiler_mnk, (None, 0, None)), (None, None, None)
    )
    gSFA_mkl = cute.local_tile(
        mSFA_mkl, cute.slice_(mma_tiler_mnk, (None, 0, None)), (None, None, None)
    )
    gB_nkl = cute.local_tile(
        mB_nkl, cute.slice_(mma_tiler_mnk, (0, None, None)), (None, None, None)
    )
    gSFB_nkl = cute.local_tile(
        mSFB_nkl, cute.slice_(mma_tiler_mnk, (0, None, None)), (None, None, None)
    )
    gC_mnl = cute.local_tile(
        mC_mnl, cute.slice_(mma_tiler_mnk, (None, None, 0)), (None, None, None)
    )

    tCgC = gC_mnl[tidx, None, bidx, bidy, bidz]
    tCgC = cute.make_tensor(tCgC.iterator, 1)
    res = cute.zeros_like(tCgC, cutlass.Float32)

    k_tile_cnt = gA_mkl.layout[3].shape
    for k_tile in range(k_tile_cnt):
        tAgA = gA_mkl[tidx, None, bidx, k_tile, bidz]
        tBgB = gB_nkl[0, None, bidy, k_tile, bidz]
        tAgSFA = gSFA_mkl[tidx, None, bidx, k_tile, bidz]
        tBgSFB = gSFB_nkl[0, None, bidy, k_tile, bidz]

        tArA = cute.make_rmem_tensor_like(tAgA, cutlass.Float32)
        tBrB = cute.make_rmem_tensor_like(tBgB, cutlass.Float32)
        tArSFA = cute.make_rmem_tensor_like(tAgSFA, cutlass.Float32)
        tBrSFB = cute.make_rmem_tensor_like(tBgSFB, cutlass.Float32)

        a_val = tAgA.load().to(cutlass.Float32)
        b_val = tBgB.load().to(cutlass.Float32)
        sfa_val = tAgSFA.load().to(cutlass.Float32)
        sfb_val = tBgSFB.load().to(cutlass.Float32)

        tArA.store(a_val)
        tBrB.store(b_val)
        tArSFA.store(sfa_val)
        tBrSFB.store(sfb_val)

        for i in cutlass.range_constexpr(mma_tiler_mnk[2]):
            res += tArA[i] * tArSFA[i] * tBrB[i] * tBrSFB[i]

    tCgC.store(res.to(cutlass.Float16))
    return


@cute.jit
def cute_jit_kernel(
    a_ptr: cute.Pointer,
    b_ptr: cute.Pointer,
    sfa_ptr: cute.Pointer,
    sfb_ptr: cute.Pointer,
    c_ptr: cute.Pointer,
    problem_size: tuple,
):
    m, _, k, l = problem_size
    
    a_tensor = cute.make_tensor(
        a_ptr,
        cute.make_layout(
            (m, cute.assume(k, 32), l),
            stride=(cute.assume(k, 32), 1, cute.assume(m * k, 32)),
        ),
    )
    
    n_padded_128 = 128
    b_tensor = cute.make_tensor(
        b_ptr,
        cute.make_layout(
            (n_padded_128, cute.assume(k, 32), l),
            stride=(cute.assume(k, 32), 1, cute.assume(n_padded_128 * k, 32)),
        ),
    )
    
    c_tensor = cute.make_tensor(
        c_ptr, cute.make_layout((cute.assume(m, 32), 1, l), stride=(1, 1, m))
    )
    
    sfa_layout = blockscaled_utils.tile_atom_to_shape_SF(a_tensor.shape, sf_vec_size)
    sfa_tensor = cute.make_tensor(sfa_ptr, sfa_layout)

    sfb_layout = blockscaled_utils.tile_atom_to_shape_SF(b_tensor.shape, sf_vec_size)
    sfb_tensor = cute.make_tensor(sfb_ptr, sfb_layout)

    grid = (
        cute.ceil_div(c_tensor.shape[0], 128),
        1,
        c_tensor.shape[2],
    )

    cute_kernel(a_tensor, b_tensor, sfa_tensor, sfb_tensor, c_tensor).launch(
        grid=grid,
        block=[threads_per_cta, 1, 1],
        cluster=(1, 1, 1),
    )
    return


_compiled_kernel_cache = None


def compile_cute_kernel():
    global _compiled_kernel_cache
    if _compiled_kernel_cache is not None:
        return _compiled_kernel_cache

    a_ptr = make_ptr(ab_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
    b_ptr = make_ptr(ab_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
    c_ptr = make_ptr(c_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
    sfa_ptr = make_ptr(sf_dtype, 0, cute.AddressSpace.gmem, assumed_align=32)
    sfb_ptr = make_ptr(sf_dtype, 0, cute.AddressSpace.gmem, assumed_align=32)

    _compiled_kernel_cache = cute.compile(
        cute_jit_kernel, a_ptr, b_ptr, sfa_ptr, sfb_ptr, c_ptr, (0, 0, 0, 0)
    )
    return _compiled_kernel_cache


def cute_dsl_kernel(a, b, sfa_permuted, sfb_permuted, c):
    compiled_func = compile_cute_kernel()
    m, k, l = a.shape
    k = k * 2
    n = 1

    a_ptr = make_ptr(ab_dtype, a.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
    b_ptr = make_ptr(ab_dtype, b.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
    c_ptr = make_ptr(c_dtype, c.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
    sfa_ptr = make_ptr(sf_dtype, sfa_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32)
    sfb_ptr = make_ptr(sf_dtype, sfb_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32)

    compiled_func(a_ptr, b_ptr, sfa_ptr, sfb_ptr, c_ptr, (m, n, k, l))
    return c


# ============================================================================
# HYBRID KERNEL
# ============================================================================

def custom_kernel(data: input_t) -> output_t:
    """
    Ultra-optimized hybrid NVFP4 GEMV kernel.
    
    - L=1: torch._scaled_mm with fast pre-permuted scale conversion
    - L>1: CuTe DSL (efficient batching)
    """
    a, b, sfa, sfb, sfa_permuted, sfb_permuted, c = data
    
    _, _, L = a.shape
    
    if L == 1:
        return torch_scaled_mm_kernel_ultra(a, b, sfa_permuted, sfb_permuted, c)
    else:
        return cute_dsl_kernel(a, b, sfa_permuted, sfb_permuted, c)


__all__ = ["custom_kernel"]

scrolls · 241 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 101184.

- """NVFP4 GEMV using CUTLASS CuTe DSL
+ """NVFP4 GEMV Ultra-Optimized Hybrid Kernel
- High-performance FP4 block-scaled GEMV using CUTLASS 4.3's CuTe Python API.
- Target: <50μs (near winners' 18-20μs performance).
+ Key optimizations:
+ 1. Skip to_blocked() by using pre-permuted scale factors directly
+ 2. Use torch._scaled_mm for L=1 (Tensor Cores)
+ 3. Use CuTe DSL for L>1 (efficient batching)
+
+ The pre-permuted scale factors (sfa_permuted, sfb_permuted) are already in a
+ layout that can be converted to blocked format with a simple permute + reshape,
+ saving ~43 μs per call compared to the full to_blocked() computation.
"""
import torch
from task import input_t, output_t
+ # ============================================================================
+ # TORCH._SCALED_MM PATH (for L=1) - ULTRA OPTIMIZED
+ # ============================================================================
+
+ def permuted_to_blocked(permuted_tensor, l_idx=0):
+ """
+ Convert pre-permuted scale factor to blocked format for torch._scaled_mm.
+
+ Input: [32, 4, rest_m, 4, rest_k, L] (sfa_permuted/sfb_permuted format)
+ Output: Flattened blocked tensor compatible with torch._scaled_mm
+
+ This is ~10x faster than to_blocked() because the data is already pre-arranged.
+ """
+ # Extract the L slice and convert to blocked format
+ # [32, 4, rest_m, 4, rest_k] -> [rest_m, rest_k, 32, 4, 4] -> [-1, 32, 16]
+ tensor = permuted_tensor[:, :, :, :, :, l_idx] # [32, 4, rest_m, 4, rest_k]
+ return tensor.permute(2, 4, 0, 1, 3).reshape(-1, 32, 16).flatten().contiguous()
+
+
+ def torch_scaled_mm_kernel_ultra(a, b, sfa_permuted, sfb_permuted, c):
+ """Ultra-fast path for L=1 using pre-computed blocked scale factors."""
+ # Convert pre-permuted to blocked format (fast path)
+ scale_a = permuted_to_blocked(sfa_permuted, 0)
+ scale_b = permuted_to_blocked(sfb_permuted, 0)
+
+ result = torch._scaled_mm(
+ a[:, :, 0],
+ b[:, :, 0].transpose(0, 1),
+ scale_a,
+ scale_b,
+ bias=None,
+ out_dtype=torch.float16,
+ )
+ c[:, 0, 0] = result[:, 0]
+ return c
+
+
+ # ============================================================================
+ # CUTE DSL PATH (for L>1) - unchanged from hybrid_best
+ # ============================================================================
+
import cutlass
import cutlass.cute as cute
from cutlass.cute.runtime import make_ptr
import cutlass.utils.blockscaled_layout as blockscaled_utils
- # Kernel configuration parameters
- mma_tiler_mnk = (128, 1, 64) # Tile sizes for M, N, K dimensions
- ab_dtype = cutlass.Float4E2M1FN # FP4 data type for A and B
- sf_dtype = cutlass.Float8E4M3FN # FP8 data type for scale factors
- c_dtype = cutlass.Float16 # FP16 output type
- sf_vec_size = 16 # Scale factor block size (16 elements share one scale)
- threads_per_cta = 128 # Number of threads per CUDA thread block
+ mma_tiler_mnk = (128, 1, 64)
+ ab_dtype = cutlass.Float4E2M1FN
+ sf_dtype = cutlass.Float8E4M3FN
+ c_dtype = cutlass.Float16
+ sf_vec_size = 16
+ threads_per_cta = 128
- # Helper function for ceiling division
- def ceil_div(a, b):
- return (a + b - 1) // b
-
-
- # The CuTe reference implementation for NVFP4 block-scaled GEMV
@cute.kernel
- def kernel(
+ def cute_kernel(
mA_mkl: cute.Tensor,
mB_nkl: cute.Tensor,
mSFA_mkl: cute.Tensor,
mSFB_nkl: cute.Tensor,
mC_mnl: cute.Tensor,
):
- # Get CUDA block and thread indices
bidx, bidy, bidz = cute.arch.block_idx()
tidx, _, _ = cute.arch.thread_idx()
- # Extract the local tile for input matrix A (shape: [block_M, block_K, rest_M, rest_K, rest_L])
gA_mkl = cute.local_tile(
mA_mkl, cute.slice_(mma_tiler_mnk, (None, 0, None)), (None, None, None)
)
- # Extract the local tile for scale factor tensor for A (same shape as gA_mkl)
- # Here, block_M = (32, 4); block_K = (16, 4)
gSFA_mkl = cute.local_tile(
mSFA_mkl, cute.slice_(mma_tiler_mnk, (None, 0, None)), (None, None, None)
)
- # Extract the local tile for input matrix B (shape: [block_N, block_K, rest_N, rest_K, rest_L])
gB_nkl = cute.local_tile(
mB_nkl, cute.slice_(mma_tiler_mnk, (0, None, None)), (None, None, None)
)
- # Extract the local tile for scale factor tensor for B (same shape as gB_nkl)
gSFB_nkl = cute.local_tile(
mSFB_nkl, cute.slice_(mma_tiler_mnk, (0, None, None)), (None, None, None)
)
- # Extract the local tile for output matrix C (shape: [block_M, block_N, rest_M, rest_N, rest_L])
gC_mnl = cute.local_tile(
mC_mnl, cute.slice_(mma_tiler_mnk, (None, None, 0)), (None, None, None)
)
- # Select output element corresponding to this thread and block indices
tCgC = gC_mnl[tidx, None, bidx, bidy, bidz]
tCgC = cute.make_tensor(tCgC.iterator, 1)
res = cute.zeros_like(tCgC, cutlass.Float32)
- # Get the number of k tiles (depth dimension) for the reduction loop
k_tile_cnt = gA_mkl.layout[3].shape
for k_tile in range(k_tile_cnt):
tAgA = gA_mkl[tidx, None, bidx, k_tile, bidz]
⋯ 6 unchanged lines
tArSFA = cute.make_rmem_tensor_like(tAgSFA, cutlass.Float32)
tBrSFB = cute.make_rmem_tensor_like(tBgSFB, cutlass.Float32)
- # Load NVFP4 or FP8 values from global memory
- a_val_nvfp4 = tAgA.load()
- b_val_nvfp4 = tBgB.load()
- sfa_val_fp8 = tAgSFA.load()
- sfb_val_fp8 = tBgSFB.load()
+ a_val = tAgA.load().to(cutlass.Float32)
+ b_val = tBgB.load().to(cutlass.Float32)
+ sfa_val = tAgSFA.load().to(cutlass.Float32)
+ sfb_val = tBgSFB.load().to(cutlass.Float32)
- # Convert loaded values to float32 for computation (FFMA)
- a_val = a_val_nvfp4.to(cutlass.Float32)
- b_val = b_val_nvfp4.to(cutlass.Float32)
- sfa_val = sfa_val_fp8.to(cutlass.Float32)
- sfb_val = sfb_val_fp8.to(cutlass.Float32)
-
- # Store the converted values to RMEM CuTe tensors
tArA.store(a_val)
tBrB.store(b_val)
tArSFA.store(sfa_val)
tBrSFB.store(sfb_val)
- # Iterate over SF vector tiles and compute the scale&matmul accumulation
for i in cutlass.range_constexpr(mma_tiler_mnk[2]):
res += tArA[i] * tArSFA[i] * tBrB[i] * tBrSFB[i]
- # Store the final float16 result back to global memory
tCgC.store(res.to(cutlass.Float16))
return
@cute.jit
- def my_kernel(
+ def cute_jit_kernel(
a_ptr: cute.Pointer,
b_ptr: cute.Pointer,
sfa_ptr: cute.Pointer,
⋯ 1 unchanged lines
c_ptr: cute.Pointer,
problem_size: tuple,
):
- """
- Host-side JIT function to prepare tensors and launch GPU kernel.
- """
m, _, k, l = problem_size
- # Create CuTe Tensor via pointer and problem size.
+
a_tensor = cute.make_tensor(
a_ptr,
cute.make_layout(
⋯ 1 unchanged lines
stride=(cute.assume(k, 32), 1, cute.assume(m * k, 32)),
),
)
- # We use n=128 to create the torch tensor to do fp4 computation via torch._scaled_mm
- # then copy torch tensor to cute tensor for cute customize kernel computation
- # therefore we need to ensure b_tensor has the right stride with this 128 padded size on n.
+
n_padded_128 = 128
b_tensor = cute.make_tensor(
b_ptr,
⋯ 2 unchanged lines
stride=(cute.assume(k, 32), 1, cute.assume(n_padded_128 * k, 32)),
),
)
+
c_tensor = cute.make_tensor(
c_ptr, cute.make_layout((cute.assume(m, 32), 1, l), stride=(1, 1, m))
)
- # Convert scale factor tensors to MMA layout
- # The layout matches Tensor Core requirements: (((32, 4), REST_M), ((SF_K, 4), REST_K), (1, REST_L))
+
sfa_layout = blockscaled_utils.tile_atom_to_shape_SF(a_tensor.shape, sf_vec_size)
sfa_tensor = cute.make_tensor(sfa_ptr, sfa_layout)
sfb_layout = blockscaled_utils.tile_atom_to_shape_SF(b_tensor.shape, sf_vec_size)
sfb_tensor = cute.make_tensor(sfb_ptr, sfb_layout)
- # Compute grid dimensions
- # Grid is (M_blocks, 1, L) where:
- # - M_blocks = ceil(M / 128) to cover all output rows
- # - L = batch size
grid = (
cute.ceil_div(c_tensor.shape[0], 128),
1,
c_tensor.shape[2],
)
- # Launch the CUDA kernel
- kernel(a_tensor, b_tensor, sfa_tensor, sfb_tensor, c_tensor).launch(
+ cute_kernel(a_tensor, b_tensor, sfa_tensor, sfb_tensor, c_tensor).launch(
grid=grid,
block=[threads_per_cta, 1, 1],
cluster=(1, 1, 1),
⋯ 1 unchanged lines
return
- # Global cache for compiled kernel
_compiled_kernel_cache = None
- # This function is used to compile the kernel once and cache it and then allow users to
- # run the kernel multiple times to get more accurate timing results.
- def compile_kernel():
- """
- Compile the kernel once and cache it.
- This should be called before any timing measurements.
-
- Returns:
- The compiled kernel function
- """
+ def compile_cute_kernel():
global _compiled_kernel_cache
-
if _compiled_kernel_cache is not None:
return _compiled_kernel_cache
- # Create CuTe pointers for A/B/C/SFA/SFB via torch tensor data pointer
a_ptr = make_ptr(ab_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
b_ptr = make_ptr(ab_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
c_ptr = make_ptr(c_dtype, 0, cute.AddressSpace.gmem, assumed_align=16)
sfa_ptr = make_ptr(sf_dtype, 0, cute.AddressSpace.gmem, assumed_align=32)
sfb_ptr = make_ptr(sf_dtype, 0, cute.AddressSpace.gmem, assumed_align=32)
- # Compile the kernel
_compiled_kernel_cache = cute.compile(
- my_kernel, a_ptr, b_ptr, sfa_ptr, sfb_ptr, c_ptr, (0, 0, 0, 0)
+ cute_jit_kernel, a_ptr, b_ptr, sfa_ptr, sfb_ptr, c_ptr, (0, 0, 0, 0)
)
-
return _compiled_kernel_cache
- def custom_kernel(data: input_t) -> output_t:
- """
- Execute the block-scaled GEMV kernel.
-
- This is the main entry point called by the evaluation framework.
- It converts PyTorch tensors to CuTe tensors, launches the kernel,
- and returns the result.
-
- Args:
- data: Tuple of (a, b, sfa_cpu, sfb_cpu, sfa_permuted, sfb_permuted, c) PyTorch tensors
- a: [m, k, l] - Input matrix in float4e2m1fn
- b: [1, k, l] - Input vector in float4e2m1fn
- sfa_cpu: [m, k, l] - Scale factors in float8_e4m3fn
- sfb_cpu: [1, k, l] - Scale factors in float8_e4m3fn
- sfa_permuted: [32, 4, rest_m, 4, rest_k, l] - Scale factors in float8_e4m3fn
- sfb_permuted: [32, 4, rest_n, 4, rest_k, l] - Scale factors in float8_e4m3fn
- c: [m, 1, l] - Output vector in float16
-
- Returns:
- Output tensor c with computed GEMV results
- """
- a, b, _, _, sfa_permuted, sfb_permuted, c = data
-
- # Ensure kernel is compiled (will use cached version if available)
- # To avoid the compilation overhead, we compile the kernel once and cache it.
- compiled_func = compile_kernel()
-
- # Get dimensions from MxKxL layout
+ def cute_dsl_kernel(a, b, sfa_permuted, sfb_permuted, c):
+ compiled_func = compile_cute_kernel()
m, k, l = a.shape
- # Torch use e2m1_x2 data type, thus k is halved
k = k * 2
- # GEMV N dimension is always 1
n = 1
- # Create CuTe pointers for A/B/C/SFA/SFB via torch tensor data pointer
a_ptr = make_ptr(ab_dtype, a.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
b_ptr = make_ptr(ab_dtype, b.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
c_ptr = make_ptr(c_dtype, c.data_ptr(), cute.AddressSpace.gmem, assumed_align=16)
- sfa_ptr = make_ptr(
- sf_dtype, sfa_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32
- )
- sfb_ptr = make_ptr(
- sf_dtype, sfb_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32
- )
+ sfa_ptr = make_ptr(sf_dtype, sfa_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32)
+ sfb_ptr = make_ptr(sf_dtype, sfb_permuted.data_ptr(), cute.AddressSpace.gmem, assumed_align=32)
- # Execute the compiled kernel
compiled_func(a_ptr, b_ptr, sfa_ptr, sfb_ptr, c_ptr, (m, n, k, l))
-
return c
+ # ============================================================================
+ # HYBRID KERNEL
+ # ============================================================================
+
+ def custom_kernel(data: input_t) -> output_t:
+ """
+ Ultra-optimized hybrid NVFP4 GEMV kernel.
+
+ - L=1: torch._scaled_mm with fast pre-permuted scale conversion
+ - L>1: CuTe DSL (efficient batching)
+ """
+ a, b, sfa, sfb, sfa_permuted, sfb_permuted, c = data
+
+ _, _, L = a.shape
+
+ if L == 1:
+ return torch_scaled_mm_kernel_ultra(a, b, sfa_permuted, sfb_permuted, c)
+ else:
+ return cute_dsl_kernel(a, b, sfa_permuted, sfb_permuted, c)
+
+
__all__ = ["custom_kernel"]
+
scrolls · 345 diff lines total

Best evidence level for this revision: reported

JSON