Skip to content
KernelIndex
Search⌘K

submission 234126

HayatoFujihara · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

grayscale_v2_17.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-grayscale-v2-234126?include=source"
interfacepython
Compatibility
measured onNVIDIA B200
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
RGB to grayscalesuite of 6 cases
NVIDIA B200
597.4µs
#3 of 84
2025-12-29

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:47622fa379e6d1f6fe291c0da39f1b5196c48aafa2a734af89784ea269202513
license declaredunknown
license concludedunknown
authorsHayatoFujihara
imported2026-08-15

Techniques

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

async-copy__device__ __forceinline__ void cp_async_bulk(
mbarrier__device__ __forceinline__ void mbarrier_init(uint64_t* mbar, int arrival_count) {
shared-memoryvoid* smem_ptr,
tma"cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes [%0], [%1], %2, [%3];"
vector-width = st.global.v4"st.global.v4.f32 [%0], {%1, %2, %3, %4};"

Kernel source

grayscale_v2_17.py197 lines
import torch
from torch.utils.cpp_extension import load_inline
from task import input_t, output_t

# =============================================================================
# Inline CUDA: suspendTimeHint版 (TMA + LDS.128)
# =============================================================================
# 目標: 598.336μs以下(B200 1位)
#
# v2_16 (598.411μs) からの最適化:
#   - mbarrier.try_wait.parity に suspendTimeHint を追加
#   - TMA転送完了までの推定待機時間をヒントとして指定
#   - 待機中に他ワープの実行を促進
#
# ベース: v2_16(境界チェック削除 + -Xptxas=-O3)
# =============================================================================

cuda_src = """
#include <cuda_runtime.h>

// =============================================================================
// 定数定義
// =============================================================================
constexpr int PIXELS_PER_TILE   = 1024;
constexpr int BYTES_PER_TILE    = 12288;
constexpr int FLOATS_PER_TILE   = 3072;
constexpr int THREADS_PER_BLOCK = 256;
constexpr int PIXELS_PER_THREAD = 4;

// TMA転送の推定待機時間(ナノ秒)
// 12KB転送 @ ~2TB/s帯域 ≈ 6ns、余裕を持って100ns
constexpr uint32_t SUSPEND_TIME_HINT = 100;

// =============================================================================
// PTXヘルパー関数
// =============================================================================

__device__ __forceinline__ void mbarrier_init(uint64_t* mbar, int arrival_count) {
    asm volatile(
        "mbarrier.init.shared.b64 [%0], %1;"
        :: "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(arrival_count)
    );
}

__device__ __forceinline__ void mbarrier_arrive_expect_tx(uint64_t* mbar, int tx_bytes) {
    asm volatile(
        "mbarrier.arrive.expect_tx.shared.b64 _, [%0], %1;"
        :: "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(tx_bytes)
    );
}

// 【最適化】suspendTimeHintを追加したmbarrier_wait
__device__ __forceinline__ void mbarrier_wait_with_hint(uint64_t* mbar, int phase, uint32_t suspend_hint) {
    int done = 0;
    while (!done) {
        asm volatile(
            "{"
            ".reg .pred p;"
            "mbarrier.try_wait.parity.shared::cta.b64 p, [%1], %2, %3;"
            "selp.b32 %0, 1, 0, p;"
            "}"
            : "=r"(done)
            : "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(phase), "r"(suspend_hint)
        );
    }
}

__device__ __forceinline__ void cp_async_bulk(
    void* smem_ptr,
    const void* gmem_ptr,
    int bytes,
    uint64_t* mbar
) {
    asm volatile(
        "cp.async.bulk.shared::cta.global.mbarrier::complete_tx::bytes [%0], [%1], %2, [%3];"
        :: "r"((uint32_t)__cvta_generic_to_shared(smem_ptr)),
           "l"(gmem_ptr),
           "r"(bytes),
           "r"((uint32_t)__cvta_generic_to_shared(mbar))
    );
}

__device__ __forceinline__ void load_shared_float4(
    uint32_t smem_addr,
    float& v0, float& v1, float& v2, float& v3
) {
    asm volatile(
        "ld.shared.v4.f32 {%0, %1, %2, %3}, [%4];"
        : "=f"(v0), "=f"(v1), "=f"(v2), "=f"(v3)
        : "r"(smem_addr)
    );
}

__device__ __forceinline__ void store_float4_ptx(
    float* ptr, float v0, float v1, float v2, float v3
) {
    asm volatile(
        "st.global.v4.f32 [%0], {%1, %2, %3, %4};"
        :: "l"(ptr), "f"(v0), "f"(v1), "f"(v2), "f"(v3) : "memory"
    );
}

// =============================================================================
// suspendTimeHint版カーネル
// =============================================================================
__global__ __launch_bounds__(256, 4) void grayscale_suspend_hint_kernel(
    const float* __restrict__ input,
    float* __restrict__ output
) {
    __shared__ __align__(128) float smem[FLOATS_PER_TILE];
    __shared__ __align__(8) uint64_t mbar;

    const int tid = threadIdx.x;
    const int tile_idx = blockIdx.x;
    const int pixel_base = tile_idx * PIXELS_PER_TILE + tid * PIXELS_PER_THREAD;

    if (tid == 0) {
        mbarrier_init(&mbar, 1);
    }
    __syncthreads();

    if (tid == 0) {
        const float* src = input + tile_idx * PIXELS_PER_TILE * 3;
        mbarrier_arrive_expect_tx(&mbar, BYTES_PER_TILE);
        cp_async_bulk(smem, src, BYTES_PER_TILE, &mbar);
    }

    // 【最適化】suspendTimeHintでTMA転送中の待機を効率化
    mbarrier_wait_with_hint(&mbar, 0, SUSPEND_TIME_HINT);

    uint32_t smem_addr = (uint32_t)__cvta_generic_to_shared(&smem[tid * 12]);

    float s0_x, s0_y, s0_z, s0_w;
    float s1_x, s1_y, s1_z, s1_w;
    float s2_x, s2_y, s2_z, s2_w;

    load_shared_float4(smem_addr,      s0_x, s0_y, s0_z, s0_w);
    load_shared_float4(smem_addr + 16, s1_x, s1_y, s1_z, s1_w);
    load_shared_float4(smem_addr + 32, s2_x, s2_y, s2_z, s2_w);

    float gray0 = fmaf(s0_x, 0.2989f, fmaf(s0_y, 0.5870f, s0_z * 0.1140f));
    float gray1 = fmaf(s0_w, 0.2989f, fmaf(s1_x, 0.5870f, s1_y * 0.1140f));
    float gray2 = fmaf(s1_z, 0.2989f, fmaf(s1_w, 0.5870f, s2_x * 0.1140f));
    float gray3 = fmaf(s2_y, 0.2989f, fmaf(s2_z, 0.5870f, s2_w * 0.1140f));

    store_float4_ptx(output + pixel_base, gray0, gray1, gray2, gray3);
}

torch::Tensor rgb_to_grayscale(torch::Tensor input, torch::Tensor output) {
    int n_pixels = output.numel();
    int blocks = n_pixels / PIXELS_PER_TILE;

    grayscale_suspend_hint_kernel<<<blocks, THREADS_PER_BLOCK>>>(
        input.data_ptr<float>(),
        output.data_ptr<float>()
    );

    return output;
}
"""

cpp_src = """
#include <torch/extension.h>

torch::Tensor rgb_to_grayscale(torch::Tensor input, torch::Tensor output);
"""

_module = None

def _get_module():
    global _module
    if _module is None:
        _module = load_inline(
            name='grayscale_cuda_suspend_hint',
            cuda_sources=[cuda_src],
            cpp_sources=[cpp_src],
            functions=['rgb_to_grayscale'],
            extra_cuda_cflags=[
                '-O3',
                '--use_fast_math',
                '-std=c++17',
                '-arch=sm_90',
                '-Xptxas=-O3',
            ],
            verbose=False
        )
    return _module


def custom_kernel(data: input_t) -> output_t:
    input_tensor, output_tensor = data

    module = _get_module()
    module.rgb_to_grayscale(input_tensor, output_tensor)

    return output_tensor
scrolls · 197 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 234048.

⋯ 2 unchanged lines
from task import input_t, output_t
# =============================================================================
- # Inline CUDA: 最小限最適化版 (TMA + LDS.128)
+ # Inline CUDA: suspendTimeHint版 (TMA + LDS.128)
# =============================================================================
# 目標: 598.336μs以下(B200 1位)
#
- # v2_15 (598.976μs) からの最適化:
- # 1. 境界チェック削除: n_pixelsは1024の倍数なので不要
- # 2. コンパイラフラグ強化: -Xptxas=-O3 追加
+ # v2_16 (598.411μs) からの最適化:
+ # - mbarrier.try_wait.parity に suspendTimeHint を追加
+ # - TMA転送完了までの推定待機時間をヒントとして指定
+ # - 待機中に他ワープの実行を促進
#
- # 注意: インターリーブは逆効果だったため、v2_15の計算順序を維持
+ # ベース: v2_16(境界チェック削除 + -Xptxas=-O3)
# =============================================================================
cuda_src = """
⋯ 8 unchanged lines
constexpr int THREADS_PER_BLOCK = 256;
constexpr int PIXELS_PER_THREAD = 4;
+ // TMA転送の推定待機時間(ナノ秒)
+ // 12KB転送 @ ~2TB/s帯域 ≈ 6ns、余裕を持って100ns
+ constexpr uint32_t SUSPEND_TIME_HINT = 100;
+
// =============================================================================
// PTXヘルパー関数
// =============================================================================
⋯ 12 unchanged lines
);
}
- __device__ __forceinline__ void mbarrier_wait(uint64_t* mbar, int phase) {
+ // 【最適化】suspendTimeHintを追加したmbarrier_wait
+ __device__ __forceinline__ void mbarrier_wait_with_hint(uint64_t* mbar, int phase, uint32_t suspend_hint) {
int done = 0;
while (!done) {
asm volatile(
"{"
".reg .pred p;"
- "mbarrier.try_wait.parity.shared.b64 p, [%1], %2;"
+ "mbarrier.try_wait.parity.shared::cta.b64 p, [%1], %2, %3;"
"selp.b32 %0, 1, 0, p;"
"}"
: "=r"(done)
- : "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(phase)
+ : "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(phase), "r"(suspend_hint)
);
}
}
⋯ 34 unchanged lines
}
// =============================================================================
- // 最小限最適化カーネル(v2_15の構造維持、境界チェックのみ削除)
+ // suspendTimeHint版カーネル
// =============================================================================
- __global__ __launch_bounds__(256, 4) void grayscale_minimal_opt_kernel(
+ __global__ __launch_bounds__(256, 4) void grayscale_suspend_hint_kernel(
const float* __restrict__ input,
float* __restrict__ output
) {
⋯ 4 unchanged lines
const int tile_idx = blockIdx.x;
const int pixel_base = tile_idx * PIXELS_PER_TILE + tid * PIXELS_PER_THREAD;
- // 【最適化】境界チェック削除(n_pixelsは1024の倍数)
-
if (tid == 0) {
mbarrier_init(&mbar, 1);
}
⋯ 5 unchanged lines
cp_async_bulk(smem, src, BYTES_PER_TILE, &mbar);
}
- mbarrier_wait(&mbar, 0);
+ // 【最適化】suspendTimeHintでTMA転送中の待機を効率化
+ mbarrier_wait_with_hint(&mbar, 0, SUSPEND_TIME_HINT);
- // v2_15と同じ順序を維持(コンパイラに最適化を任せる)
uint32_t smem_addr = (uint32_t)__cvta_generic_to_shared(&smem[tid * 12]);
float s0_x, s0_y, s0_z, s0_w;
⋯ 16 unchanged lines
int n_pixels = output.numel();
int blocks = n_pixels / PIXELS_PER_TILE;
- grayscale_minimal_opt_kernel<<<blocks, THREADS_PER_BLOCK>>>(
+ grayscale_suspend_hint_kernel<<<blocks, THREADS_PER_BLOCK>>>(
input.data_ptr<float>(),
output.data_ptr<float>()
);
⋯ 14 unchanged lines
global _module
if _module is None:
_module = load_inline(
- name='grayscale_cuda_minimal_opt',
+ name='grayscale_cuda_suspend_hint',
cuda_sources=[cuda_src],
cpp_sources=[cpp_src],
functions=['rgb_to_grayscale'],
scrolls · 106 diff lines total

Best evidence level for this revision: reported

JSON