"""Mutation-friendly substrate for KernelBench level-2 problem 44.""" CUDA_SOURCE = r""" #include __global__ void l2p44_convtranspose2d_multiply_global_avg_mean_kernel( const float* x, float* out, const float* weight, const float* bias, int batches, int input_height, int input_width, int output_height, int output_width, long long output_elements) { long long output_index = (long long)blockIdx.x * blockDim.x + threadIdx.x; if (output_index >= output_elements) return; int output_channel = (int)(output_index % 128); int batch = (int)(output_index / 128); long long output_spatial = (long long)output_height * output_width; // Sum the transposed convolution over its complete spatial plane. This // uses linearity to avoid materializing the 256x256 intermediate: every // input/weight product is included exactly when its scattered output // coordinate lies inside the ConvTranspose2d result. float spatial_sum = bias[output_channel] * (float)output_spatial; for (int input_channel = 0; input_channel < 64; input_channel++) { for (int input_y = 0; input_y < input_height; input_y++) { for (int input_x = 0; input_x < input_width; input_x++) { long long input_index = (((long long)batch * 64 + input_channel) * input_height + input_y) * input_width + input_x; float input_value = x[input_index]; for (int kernel_y = 0; kernel_y < 3; kernel_y++) { int output_y = input_y * 2 - 1 + kernel_y; if (output_y < 0 || output_y >= output_height) continue; for (int kernel_x = 0; kernel_x < 3; kernel_x++) { int output_x = input_x * 2 - 1 + kernel_x; if (output_x < 0 || output_x >= output_width) continue; // ConvTranspose2d weight layout for groups=1 is // [input_channel, output_channel, kernel_y, kernel_x]. long long weight_index = (((long long)input_channel * 128 + output_channel) * 3 + kernel_y) * 3 + kernel_x; spatial_sum += input_value * weight[weight_index]; } } } } } // forward(): multiply by 0.5, mean over H/W with keepdim=True, then // mean over the resulting 1x1 dimensions (also with keepdim=True). float multiplier = 0.5f; float first_global_mean = (spatial_sum * multiplier) / (float)output_spatial; float second_global_mean = first_global_mean / 1.0f; out[output_index] = second_global_mean; } """ def register(K, torch, ctypes): # Reproduce the module created from get_init_inputs(). ConvTranspose2d's # default bias=True, groups=1, and dilation=1 are all part of the model. # Freezing a deterministic ordinary initialization makes the parameter # state shared by the reference and device-only implementation. with torch.random.fork_rng(devices=[]): torch.manual_seed(0) convolution = torch.nn.ConvTranspose2d( 64, 128, 3, stride=2, padding=1, output_padding=1, ) frozen_weight = convolution.weight.detach().cpu().contiguous() frozen_bias = convolution.bias.detach().cpu().contiguous() parameter_cache = {} def parameters_for(x): key = (x.device.type, x.device.index, x.dtype) parameters = parameter_cache.get(key) if parameters is None: parameters = ( frozen_weight.to(device=x.device, dtype=x.dtype), frozen_bias.to(device=x.device, dtype=x.dtype), ) parameter_cache[key] = parameters return parameters def ref(x): weight, bias = parameters_for(x) value = torch.nn.functional.conv_transpose2d( x, weight, bias=bias, stride=2, padding=1, output_padding=1, groups=1, dilation=1, ) value = value * 0.5 value = torch.mean(value, dim=(2, 3), keepdim=True) value = torch.mean(value, dim=(2, 3), keepdim=True) return value def launch(inputs, torch_module): x = inputs[0] batches, input_channels, input_height, input_width = x.shape if input_channels != 64: raise ValueError("level-2 problem 44 requires exactly 64 input channels") # (I-1)*2 - 2*1 + (3-1) + output_padding(1) + 1 = 2*I. output_height = input_height * 2 output_width = input_width * 2 out = torch_module.empty( (batches, 128, 1, 1), device=x.device, dtype=x.dtype, ) weight, bias = parameters_for(x) # Keep cached tensors alive through the asynchronous raw CUDA launch. out._substrate_keepalive = (weight, bias) output_elements = out.numel() args = [ (ctypes.c_void_p, weight.data_ptr()), (ctypes.c_void_p, bias.data_ptr()), (ctypes.c_int, batches), (ctypes.c_int, input_height), (ctypes.c_int, input_width), (ctypes.c_int, output_height), (ctypes.c_int, output_width), (ctypes.c_longlong, output_elements), ] grid = ((output_elements + 255) // 256, 1, 1) return out, grid, (256, 1, 1), args def suites(): def t0_original(trial): torch.manual_seed(1000 + trial) return [torch.rand(16, 64, 128, 128)] def t1_signed(trial): torch.manual_seed(2000 + trial) return [torch.randn(2, 64, 7, 11)] def t2_misaligned(trial): torch.manual_seed(3000 + trial) return [torch.rand(3, 64, 9, 13)] def t3_positive_spikes(trial): torch.manual_seed(4000 + trial) x = torch.rand(2, 64, 8, 15) x[:, :, 0, 0] = 20.0 x[:, :, 4, 7] = 10.0 x[:, :, -1, -1] = 40.0 return [x] return [ ("T0_original", 5, t0_original), ("T1_signed", 1, t1_signed), ("T2_misaligned", 1, t2_misaligned), ("T3_positive_spikes", 1, t3_positive_spikes), ] def probe(torch_module): torch_module.manual_seed(0) return [ ("p_aligned", [torch_module.rand(1, 64, 8, 16)]), ("p_misaligned", [torch_module.randn(2, 64, 5, 7)]), ] K.PROBLEMS["L2p44_convtranspose2d_multip"] = dict( kb_file="44_ConvTranspose2d_Multiply_GlobalAvgPool_GlobalAvgPool_Mean.py", cuda=CUDA_SOURCE, kernel_name="l2p44_convtranspose2d_multiply_global_avg_mean_kernel", ref=ref, launch=launch, suites=suites, probe=probe, )