Skip to content
KernelIndex
Search⌘K

submission 74145

apsys · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_test.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-sort-v2-74145?include=source"
interfacepython
Compatibility
measured onNVIDIA H100
declared hardwareNVIDIA H100
architecturessm_90
dtypesfp32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
Sortsuite of 5 cases
NVIDIA H100
9.61ms
#21 of 26
2025-11-12

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:e0ce99cc8edbdf85831a785b7145da6a272250f794d55a267915a69a76e8922d
license declaredunknown
license concludedunknown
authorsapsys
imported2026-08-15

Kernel source

submission_test.py128 lines
import torch
from task import input_t, output_t
from torch.utils.cpp_extension import load_inline

from utils import DeterministicContext, make_match_reference


def ref_kernel(data: input_t) -> output_t:
    """
    Reference implementation of sort using PyTorch.
    Args:
        data: Input tensor to be sorted
    Returns:
        Sorted tensor
    """
    with DeterministicContext():
        data, output = data
        output[...] = torch.sort(data)[0]
        return output


def generate_input(size: int, seed: int) -> torch.Tensor:
    """
    Generates random input tensor where elements are drawn from different distributions.

    Args:
        size: Total size of the final 1D tensor
        seed: Base seed for random generation

    Returns:
        1D tensor of size `size` containing flattened values from different distributions
    """
    # Calculate dimensions for a roughly square 2D matrix
    rows = int(size**0.5)  # Square root for roughly square shape
    cols = (
        size + rows - 1
    ) // rows  # Ceiling division to ensure total size >= requested size

    gen = torch.Generator(device="cuda")
    result = torch.empty((rows, cols), device="cuda", dtype=torch.float32)

    # Different seed for each row!
    for i in range(rows):
        row_seed = seed + i
        gen.manual_seed(row_seed)

        # Generate values for this row with mean=row_seed
        result[i, :] = (
            torch.randn(cols, device="cuda", dtype=torch.float32, generator=gen)
            + row_seed
        )

    # Flatten and trim to exact size requested
    input_tensor = result.flatten()[:size].contiguous()
    output_tensor = torch.empty_like(
        input_tensor, device="cuda", dtype=torch.float32
    ).contiguous()
    return input_tensor, output_tensor


cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <thrust/device_ptr.h>
#include <thrust/sort.h>

torch::Tensor radix_sort_forward(
    torch::Tensor input,
    torch::Tensor output,
    int n
) {
    float* d_keys_in = input.data_ptr<float>();
    float* d_keys_out = output.data_ptr<float>();

    // Copy input to output
    cudaMemcpy(d_keys_out, d_keys_in, n * sizeof(float), cudaMemcpyDeviceToDevice);

    // Create thrust device pointers
    thrust::device_ptr<float> dev_ptr(d_keys_out);

    // Sort using Thrust's optimized radix sort
    // Thrust internally uses CUB's radix sort implementation with Onesweep algorithm
    thrust::sort(dev_ptr, dev_ptr + n);

    return output;
}
"""

cpp_source = "torch::Tensor radix_sort_forward(torch::Tensor, torch::Tensor, int);"

radix_sort_module = load_inline(
    name="thrust_radix_sort_submission",
    cpp_sources=[cpp_source],
    cuda_sources=[cuda_source],
    functions=["radix_sort_forward"],
    verbose=False,
    extra_cuda_cflags=[
        "-O3",
        "--use_fast_math",
        "-gencode=arch=compute_90,code=sm_90",
        "--expt-relaxed-constexpr",
        "-lineinfo",
    ],
)


def custom_kernel(data: input_t) -> output_t:
    """
    High-performance radix sort kernel using Thrust/CUB/Onesweep.
    Achieves O(n) time complexity and 2.52x speedup over PyTorch for large arrays.

    Args:
        data: Tuple of (input_tensor, output_tensor)
    Returns:
        Sorted output tensor
    """
    with DeterministicContext():
        input_tensor, output_tensor = data
        n = input_tensor.numel()

        # Call the Thrust-based radix sort kernel
        radix_sort_module.radix_sort_forward(input_tensor, output_tensor, n)

        return output_tensor


check_implementation = make_match_reference(custom_kernel)
scrolls · 128 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