submission 230431
HayatoFujihara · python · License unknown
Kernel source · 126 lines ↓holds 1 record
Use it
Vendorable · source mirrored · license unknownView source →
No package. Vendor the mirrored source: 126 lines, June 9 Researcher Reciprocity License v1.0.
grayscale_v2_11.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-grayscale-v2-230431?include=source"interfacepython
Compatibility
measured onNVIDIA A100
declared hardwareNVIDIA A100
architecturessm_80
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:3b97344869d77963393133da2c6e71070c5fdf5a71c9e62243d7e6c39619f5d0
license declaredunknown
license concludedunknown
authorsHayatoFujihara
imported2026-08-15
Techniques
Extracted from the mirrored source by pattern, never inferred. Each row cites its line.
vector-width = ld.global.v4
"ld.global.v4.f32 {%0, %1, %2, %3}, [%4];"Kernel source
grayscale_v2_11.py126 lines
import torch
from torch.utils.cpp_extension import load_inline
from task import input_t, output_t
# =============================================================================
# Inline CUDA: 入出力両方 Inline PTX 版 RGB to Grayscale
# =============================================================================
# 目標: 2.39ms → 2.38ms (1位: 2389.197μs = 2.389ms)
#
# 変更点:
# - 入力: ld.global.v4.f32 (継続)
# - 出力: st.global.v4.f32 (新規追加)
# - 完全に PTX レベルでメモリアクセスを制御
#
# PTX 命令:
# ld.global.v4.f32 {%0, %1, %2, %3}, [%4]; - 128-bit ロード
# st.global.v4.f32 [%0], {%1, %2, %3, %4}; - 128-bit ストア
cuda_src = """
#include <cuda_runtime.h>
// Inline PTX による float4 ロード
__device__ __forceinline__ void load_float4_ptx(
const float* ptr,
float& r0, float& r1, float& r2, float& r3
) {
asm volatile(
"ld.global.v4.f32 {%0, %1, %2, %3}, [%4];"
: "=f"(r0), "=f"(r1), "=f"(r2), "=f"(r3)
: "l"(ptr)
);
}
// Inline PTX による float4 ストア
__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"
);
}
// RGB to Grayscale カーネル(入出力両方 PTX 版)
__global__ void grayscale_kernel(
const float* __restrict__ input,
float* __restrict__ output,
int n_pixels
) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int pixel_base = tid * 4;
if (pixel_base >= n_pixels) return;
int base = pixel_base * 3;
const float* ptr = input + base;
// PTX で 128-bit ロード × 3 回
float d0_x, d0_y, d0_z, d0_w; // [R0, G0, B0, R1]
float d1_x, d1_y, d1_z, d1_w; // [G1, B1, R2, G2]
float d2_x, d2_y, d2_z, d2_w; // [B2, R3, G3, B3]
load_float4_ptx(ptr, d0_x, d0_y, d0_z, d0_w);
load_float4_ptx(ptr + 4, d1_x, d1_y, d1_z, d1_w);
load_float4_ptx(ptr + 8, d2_x, d2_y, d2_z, d2_w);
// Grayscale 計算
float gray_x = d0_x * 0.2989f + d0_y * 0.5870f + d0_z * 0.1140f; // Pixel 0
float gray_y = d0_w * 0.2989f + d1_x * 0.5870f + d1_y * 0.1140f; // Pixel 1
float gray_z = d1_z * 0.2989f + d1_w * 0.5870f + d2_x * 0.1140f; // Pixel 2
float gray_w = d2_y * 0.2989f + d2_z * 0.5870f + d2_w * 0.1140f; // Pixel 3
// PTX で 128-bit ストア
store_float4_ptx(output + pixel_base, gray_x, gray_y, gray_z, gray_w);
}
torch::Tensor rgb_to_grayscale(torch::Tensor input, torch::Tensor output) {
int n_pixels = output.numel();
int threads = 256;
int pixels_per_block = threads * 4;
int blocks = (n_pixels + pixels_per_block - 1) / pixels_per_block;
grayscale_kernel<<<blocks, threads>>>(
input.data_ptr<float>(),
output.data_ptr<float>(),
n_pixels
);
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_full_ptx',
cuda_sources=[cuda_src],
cpp_sources=[cpp_src],
functions=['rgb_to_grayscale'],
extra_cuda_cflags=['-O3', '--use_fast_math'],
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 · 126 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 230430.
⋯ 2 unchanged linesfrom task import input_t, output_t# =============================================================================- # Inline CUDA: float4 ベクトルロード版 RGB to Grayscale (Phase 3: Inline PTX)+ # Inline CUDA: 入出力両方 Inline PTX 版 RGB to Grayscale# =============================================================================- # 目標: 2.47ms (Triton) → 2.38ms+ # 目標: 2.39ms → 2.38ms (1位: 2389.197μs = 2.389ms)#- # Phase 3 変更点:- # - Inline PTX で ld.global.v4.f32 を明示的に使用- # - LDG.128 命令を完全に強制- # - コンパイラの最適化判断を完全にバイパス+ # 変更点:+ # - 入力: ld.global.v4.f32 (継続)+ # - 出力: st.global.v4.f32 (新規追加)+ # - 完全に PTX レベルでメモリアクセスを制御## PTX 命令:- # ld.global.v4.f32 {%0, %1, %2, %3}, [%4];- # - 128-bit (16 bytes) 一括ロード- # - 4 つの float を同時に取得+ # ld.global.v4.f32 {%0, %1, %2, %3}, [%4]; - 128-bit ロード+ # st.global.v4.f32 [%0], {%1, %2, %3, %4}; - 128-bit ストアcuda_src = """#include <cuda_runtime.h>⋯ 10 unchanged lines);}- // RGB to Grayscale カーネル(Inline PTX 版)+ // Inline PTX による float4 ストア+ __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"+ );+ }++ // RGB to Grayscale カーネル(入出力両方 PTX 版)__global__ void grayscale_kernel(const float* __restrict__ input,float* __restrict__ output,⋯ 17 unchanged linesload_float4_ptx(ptr + 8, d2_x, d2_y, d2_z, d2_w);// Grayscale 計算- float4 gray;- gray.x = d0_x * 0.2989f + d0_y * 0.5870f + d0_z * 0.1140f; // Pixel 0- gray.y = d0_w * 0.2989f + d1_x * 0.5870f + d1_y * 0.1140f; // Pixel 1- gray.z = d1_z * 0.2989f + d1_w * 0.5870f + d2_x * 0.1140f; // Pixel 2- gray.w = d2_y * 0.2989f + d2_z * 0.5870f + d2_w * 0.1140f; // Pixel 3+ float gray_x = d0_x * 0.2989f + d0_y * 0.5870f + d0_z * 0.1140f; // Pixel 0+ float gray_y = d0_w * 0.2989f + d1_x * 0.5870f + d1_y * 0.1140f; // Pixel 1+ float gray_z = d1_z * 0.2989f + d1_w * 0.5870f + d2_x * 0.1140f; // Pixel 2+ float gray_w = d2_y * 0.2989f + d2_z * 0.5870f + d2_w * 0.1140f; // Pixel 3- // float4 で出力- *reinterpret_cast<float4*>(output + pixel_base) = gray;+ // PTX で 128-bit ストア+ store_float4_ptx(output + pixel_base, gray_x, gray_y, gray_z, gray_w);}torch::Tensor rgb_to_grayscale(torch::Tensor input, torch::Tensor output) {⋯ 25 unchanged linesglobal _moduleif _module is None:_module = load_inline(- name='grayscale_cuda_float4_ptx',+ name='grayscale_cuda_full_ptx',cuda_sources=[cuda_src],cpp_sources=[cpp_src],functions=['rgb_to_grayscale'],
scrolls · 80 diff lines total
Best evidence level for this revision: reported
JSON