submission 233908
HayatoFujihara · python · License unknown
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 193 lines, June 9 Researcher Reciprocity License v1.0.
grayscale_v2_16.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-grayscale-v2-233908?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
Reported · How evidence levels are derived →
Source and license
sourceavailable
revision digestsha256:01c2829996e4a2faf1d9ea067364e96bf4ff9f1cda165a8cfef976f782dc9bb5
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-memory
void* 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_16.py193 lines
import torch
from torch.utils.cpp_extension import load_inline
from task import input_t, output_t
# =============================================================================
# Inline CUDA: 最小限最適化版 (TMA + LDS.128)
# =============================================================================
# 目標: 598.336μs以下(B200 1位)
#
# v2_15 (598.976μs) からの最適化:
# 1. 境界チェック削除: n_pixelsは1024の倍数なので不要
# 2. コンパイラフラグ強化: -Xptxas=-O3 追加
#
# 注意: インターリーブは逆効果だったため、v2_15の計算順序を維持
# =============================================================================
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;
// =============================================================================
// 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)
);
}
__device__ __forceinline__ void mbarrier_wait(uint64_t* mbar, int phase) {
int done = 0;
while (!done) {
asm volatile(
"{"
".reg .pred p;"
"mbarrier.try_wait.parity.shared.b64 p, [%1], %2;"
"selp.b32 %0, 1, 0, p;"
"}"
: "=r"(done)
: "r"((uint32_t)__cvta_generic_to_shared(mbar)), "r"(phase)
);
}
}
__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"
);
}
// =============================================================================
// 最小限最適化カーネル(v2_15の構造維持、境界チェックのみ削除)
// =============================================================================
__global__ __launch_bounds__(256, 4) void grayscale_minimal_opt_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;
// 【最適化】境界チェック削除(n_pixelsは1024の倍数)
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);
}
mbarrier_wait(&mbar, 0);
// v2_15と同じ順序を維持(コンパイラに最適化を任せる)
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_minimal_opt_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_minimal_opt',
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 · 193 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 233822.
Best evidence level for this revision: reported
JSON