Dataset Viewer
Auto-converted to Parquet
problem_id
stringlengths
21
23
task_group
stringclasses
1 value
task_type
stringclasses
1 value
prompt
stringlengths
3.82k
26.5k
verification_info
stringlengths
661
23.9k
metadata
stringlengths
29
120
kernelbench_level_1_100
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ A model that computes Hinge Loss for binary classification tasks. Parameters: None """ def __init__(self): super(Model, self).__init__() def forward(self, predictions, targets): return torch.mean(torch.clamp(1 - predictions * targets, min=0)) batch_size = 128 input_shape = (1,) dim = 1 def get_inputs(): return [torch.randn(batch_size, *input_shape), torch.randint(0, 2, (batch_size, 1)).float() * 2 - 1] def get_init_inputs(): return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n A model that computes Hinge Loss for binary classification tasks.\n\n Parameters:\n None\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n\n def forward(self, predictions, targets):\n return torch.mean(torch.clamp(1 - predictions * targets, min=0))\n\nbatch_size = 128\ninput_shape = (1,)\ndim = 1\n\ndef get_inputs():\n return [torch.randn(batch_size, *input_shape), torch.randint(0, 2, (batch_size, 1)).float() * 2 - 1]\n\ndef get_init_inputs():\n return []", "mean_runtime_torch": 0.0704, "mean_runtime_torch_compile": 0.094}
{"level": 1, "name": "100_HingeLoss"}
kernelbench_level_1_10
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Performs 3D tensor-matrix multiplication. """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs 3D tensor-matrix multiplication. Args: A (torch.Tensor): Input 3D tensor of shape (N, M, K). B (torch.Tensor): Input matrix of shape (K, L). Returns: torch.Tensor: Output tensor of shape (N, M, L), resulting from the multiplication of A and B along the last dimension of A. """ return torch.matmul(A, B) N = 16 M = 1024 K = 2048 L = 768 def get_inputs(): A = torch.randn(N, M, K) B = torch.randn(K, L) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Performs 3D tensor-matrix multiplication.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A, B):\n \"\"\"\n Performs 3D tensor-matrix multiplication.\n\n Args:\n A (torch.Tensor): Input 3D tensor of shape (N, M, K).\n B (torch.Tensor): Input matrix of shape (K, L).\n\n Returns:\n torch.Tensor: Output tensor of shape (N, M, L), resulting from the multiplication of A and B along the last dimension of A.\n \"\"\"\n return torch.matmul(A, B)\n\nN = 16\nM = 1024\nK = 2048\nL = 768\n\ndef get_inputs():\n A = torch.randn(N, M, K)\n B = torch.randn(K, L)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 1.24, "mean_runtime_torch_compile": 1.23}
{"level": 1, "name": "10_3D_tensor_matrix_multiplication"}
kernelbench_level_1_11
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Performs 4D tensor-matrix multiplication: C[b, i, j, k] = sum_l A[b, i, j, l] * B[l, k] Args: A (torch.Tensor): Input 4D tensor of shape (b, i, j, l) B (torch.Tensor): Input matrix of shape (l, k) Returns: torch.Tensor: Output 4D tensor of shape (b, i, j, k) """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs the 4D tensor-matrix multiplication. Args: A (torch.Tensor): Input 4D tensor of shape (b, i, j, l) B (torch.Tensor): Input matrix of shape (l, k) Returns: torch.Tensor: Output 4D tensor of shape (b, i, j, k) """ return torch.einsum("bijl,lk->bijk", A, B) # Test code b = 16 i = 256 j = 512 l = 256 k = 768 def get_inputs(): A = torch.randn(b, i, j, l) B = torch.randn(l, k) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Performs 4D tensor-matrix multiplication: \n C[b, i, j, k] = sum_l A[b, i, j, l] * B[l, k]\n\n Args:\n A (torch.Tensor): Input 4D tensor of shape (b, i, j, l)\n B (torch.Tensor): Input matrix of shape (l, k)\n\n Returns:\n torch.Tensor: Output 4D tensor of shape (b, i, j, k)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n\n def forward(self, A, B):\n \"\"\"\n Performs the 4D tensor-matrix multiplication.\n\n Args:\n A (torch.Tensor): Input 4D tensor of shape (b, i, j, l)\n B (torch.Tensor): Input matrix of shape (l, k)\n\n Returns:\n torch.Tensor: Output 4D tensor of shape (b, i, j, k)\n \"\"\"\n return torch.einsum(\"bijl,lk->bijk\", A, B)\n\n# Test code\nb = 16\ni = 256\nj = 512\nl = 256\nk = 768\n\ndef get_inputs():\n A = torch.randn(b, i, j, l)\n B = torch.randn(l, k)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 28.3, "mean_runtime_torch_compile": 28.8}
{"level": 1, "name": "11_4D_tensor_matrix_multiplication"}
kernelbench_level_1_12
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a matrix multiplication of a diagonal matrix with another matrix. C = diag(A) * B """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs the matrix multiplication. Args: A (torch.Tensor): A 1D tensor representing the diagonal of the diagonal matrix. Shape: (N,). B (torch.Tensor): A 2D tensor representing the second matrix. Shape: (N, M). Returns: torch.Tensor: The result of the matrix multiplication. Shape: (N, M). """ return torch.diag(A) @ B M = 4096 N = 4096 def get_inputs(): A = torch.randn(N) B = torch.randn(N, M) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a matrix multiplication of a diagonal matrix with another matrix.\n C = diag(A) * B\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A, B):\n \"\"\"\n Performs the matrix multiplication.\n\n Args:\n A (torch.Tensor): A 1D tensor representing the diagonal of the diagonal matrix. Shape: (N,).\n B (torch.Tensor): A 2D tensor representing the second matrix. Shape: (N, M).\n\n Returns:\n torch.Tensor: The result of the matrix multiplication. Shape: (N, M).\n \"\"\"\n return torch.diag(A) @ B\n\nM = 4096\nN = 4096\n\ndef get_inputs():\n A = torch.randn(N)\n B = torch.randn(N, M)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 2.52, "mean_runtime_torch_compile": 2.6}
{"level": 1, "name": "12_Matmul_with_diagonal_matrices_"}
kernelbench_level_1_13
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single matrix multiplication (C = A * B) with A and B being symmetric matrices. """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs matrix multiplication of two symmetric matrices. Args: A (torch.Tensor): Input matrix A, shape (N, N), symmetric. B (torch.Tensor): Input matrix B, shape (N, N), symmetric. Returns: torch.Tensor: Output matrix C, shape (N, N). """ return torch.matmul(A, B) N = 4096 def get_inputs(): """ Generates a pair of random symmetric matrices for testing. Returns: list: List containing two symmetric tensors A and B. """ A = torch.randn(N, N) A = (A + A.T) / 2 # Ensure symmetry B = torch.randn(N, N) B = (B + B.T) / 2 # Ensure symmetry return [A, B] def get_init_inputs(): """ No specific initialization inputs needed for this model. Returns: list: Empty list. """ return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single matrix multiplication (C = A * B) with A and B being symmetric matrices.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A, B):\n \"\"\"\n Performs matrix multiplication of two symmetric matrices.\n\n Args:\n A (torch.Tensor): Input matrix A, shape (N, N), symmetric.\n B (torch.Tensor): Input matrix B, shape (N, N), symmetric.\n\n Returns:\n torch.Tensor: Output matrix C, shape (N, N).\n \"\"\"\n return torch.matmul(A, B)\n\nN = 4096\n\ndef get_inputs():\n \"\"\"\n Generates a pair of random symmetric matrices for testing.\n\n Returns:\n list: List containing two symmetric tensors A and B.\n \"\"\"\n A = torch.randn(N, N)\n A = (A + A.T) / 2 # Ensure symmetry\n B = torch.randn(N, N)\n B = (B + B.T) / 2 # Ensure symmetry\n return [A, B]\n\ndef get_init_inputs():\n \"\"\"\n No specific initialization inputs needed for this model.\n\n Returns:\n list: Empty list.\n \"\"\"\n return []", "mean_runtime_torch": 3.18, "mean_runtime_torch_compile": 3.26}
{"level": 1, "name": "13_Matmul_for_symmetric_matrices"}
kernelbench_level_1_14
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs matrix multiplication (C = A * B) for upper triangular matrices. """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs matrix multiplication for upper triangular matrices. Args: A (torch.Tensor): Upper triangular matrix of shape (N, N). B (torch.Tensor): Upper triangular matrix of shape (N, N). Returns: torch.Tensor: The product of A and B, also an upper triangular matrix of shape (N, N). """ return torch.triu(torch.matmul(A, B)) N = 4096 def get_inputs(): """ Generates upper triangular matrices for testing. Returns: list: A list containing two upper triangular matrices of shape (N, N). """ A = torch.triu(torch.randn(N, N)) B = torch.triu(torch.randn(N, N)) return [A, B] def get_init_inputs(): """ No specific initialization inputs are needed for this model. Returns: list: An empty list. """ return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs matrix multiplication (C = A * B) for upper triangular matrices.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A, B):\n \"\"\"\n Performs matrix multiplication for upper triangular matrices.\n\n Args:\n A (torch.Tensor): Upper triangular matrix of shape (N, N).\n B (torch.Tensor): Upper triangular matrix of shape (N, N).\n\n Returns:\n torch.Tensor: The product of A and B, also an upper triangular matrix of shape (N, N).\n \"\"\"\n return torch.triu(torch.matmul(A, B))\n\nN = 4096\n\ndef get_inputs():\n \"\"\"\n Generates upper triangular matrices for testing.\n\n Returns:\n list: A list containing two upper triangular matrices of shape (N, N).\n \"\"\"\n A = torch.triu(torch.randn(N, N))\n B = torch.triu(torch.randn(N, N))\n return [A, B]\n\ndef get_init_inputs():\n \"\"\"\n No specific initialization inputs are needed for this model.\n\n Returns:\n list: An empty list.\n \"\"\"\n return []", "mean_runtime_torch": 2.65, "mean_runtime_torch_compile": 2.71}
{"level": 1, "name": "14_Matmul_for_upper_triangular_matrices"}
kernelbench_level_1_15
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a matrix multiplication (C = A * B) where A and B are lower triangular matrices. """ def __init__(self): super(Model, self).__init__() def forward(self, A, B): """ Performs matrix multiplication of lower triangular matrices A and B. Args: A (torch.Tensor): Lower triangular matrix of shape (N, N). B (torch.Tensor): Lower triangular matrix of shape (N, N). Returns: torch.Tensor: The result of matrix multiplication C of shape (N, N). """ return torch.tril(torch.matmul(A, B)) M = 4096 def get_inputs(): A = torch.randn(M, M) B = torch.randn(M, M) A = torch.tril(A) B = torch.tril(B) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a matrix multiplication (C = A * B) where A and B are lower triangular matrices. \n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A, B):\n \"\"\"\n Performs matrix multiplication of lower triangular matrices A and B.\n\n Args:\n A (torch.Tensor): Lower triangular matrix of shape (N, N).\n B (torch.Tensor): Lower triangular matrix of shape (N, N).\n\n Returns:\n torch.Tensor: The result of matrix multiplication C of shape (N, N).\n \"\"\"\n return torch.tril(torch.matmul(A, B))\n\nM = 4096\n\ndef get_inputs():\n A = torch.randn(M, M)\n B = torch.randn(M, M)\n A = torch.tril(A)\n B = torch.tril(B)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 2.69, "mean_runtime_torch_compile": 2.64}
{"level": 1, "name": "15_Matmul_for_lower_triangular_matrices"}
kernelbench_level_1_16
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single matrix multiplication (C = A * B) """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs matrix multiplication. Args: A: Input tensor of shape (M, K). B: Input tensor of shape (K, N). Returns: Output tensor of shape (M, N). """ return torch.matmul(A.T, B) M = 1024 K = 4096 N = 2048 def get_inputs(): A = torch.randn(K, M) B = torch.randn(K, N) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single matrix multiplication (C = A * B)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs matrix multiplication.\n\n Args:\n A: Input tensor of shape (M, K).\n B: Input tensor of shape (K, N).\n\n Returns:\n Output tensor of shape (M, N).\n \"\"\"\n return torch.matmul(A.T, B)\n\nM = 1024\nK = 4096\nN = 2048\n\ndef get_inputs():\n A = torch.randn(K, M)\n B = torch.randn(K, N)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.378, "mean_runtime_torch_compile": 0.42}
{"level": 1, "name": "16_Matmul_with_transposed_A"}
kernelbench_level_1_17
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single matrix multiplication (C = A * B) """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs matrix multiplication. Args: A: Input tensor of shape (M, K). B: Input tensor of shape (K, N). Returns: Output tensor of shape (M, N). """ return torch.matmul(A, B.T) M = 1024 K = 4096 N = 2048 def get_inputs(): A = torch.randn(M, K) B = torch.randn(N, K) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single matrix multiplication (C = A * B)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs matrix multiplication.\n\n Args:\n A: Input tensor of shape (M, K).\n B: Input tensor of shape (K, N).\n\n Returns:\n Output tensor of shape (M, N).\n \"\"\"\n return torch.matmul(A, B.T)\n\nM = 1024\nK = 4096\nN = 2048\n\ndef get_inputs():\n A = torch.randn(M, K)\n B = torch.randn(N, K)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.528, "mean_runtime_torch_compile": 0.567}
{"level": 1, "name": "17_Matmul_with_transposed_B"}
kernelbench_level_1_18
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single matrix multiplication (C = A * B) """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs matrix multiplication. Args: A: Input tensor of shape (M, K). B: Input tensor of shape (K, N). Returns: Output tensor of shape (M, N). """ return torch.matmul(A.T, B.T) M = 1024 K = 4096 N = 2048 def get_inputs(): A = torch.randn(K, M) B = torch.randn(N, K) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single matrix multiplication (C = A * B)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs matrix multiplication.\n\n Args:\n A: Input tensor of shape (M, K).\n B: Input tensor of shape (K, N).\n\n Returns:\n Output tensor of shape (M, N).\n \"\"\"\n return torch.matmul(A.T, B.T)\n\nM = 1024\nK = 4096\nN = 2048\n\ndef get_inputs():\n A = torch.randn(K, M)\n B = torch.randn(N, K)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.384, "mean_runtime_torch_compile": 0.44}
{"level": 1, "name": "18_Matmul_with_transposed_both"}
kernelbench_level_1_19
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a ReLU activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies ReLU activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with ReLU applied, same shape as input. """ return torch.relu(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a ReLU activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies ReLU activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with ReLU applied, same shape as input.\n \"\"\"\n return torch.relu(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0283, "mean_runtime_torch_compile": 0.0875}
{"level": 1, "name": "19_ReLU"}
kernelbench_level_1_1
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single square matrix multiplication (C = A * B) """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs the matrix multiplication. Args: A (torch.Tensor): Input matrix A of shape (N, N). B (torch.Tensor): Input matrix B of shape (N, N). Returns: torch.Tensor: Output matrix C of shape (N, N). """ return torch.matmul(A, B) N = 2048 def get_inputs(): A = torch.randn(N, N) B = torch.randn(N, N) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single square matrix multiplication (C = A * B)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs the matrix multiplication.\n\n Args:\n A (torch.Tensor): Input matrix A of shape (N, N).\n B (torch.Tensor): Input matrix B of shape (N, N).\n\n Returns:\n torch.Tensor: Output matrix C of shape (N, N).\n \"\"\"\n return torch.matmul(A, B)\n\nN = 2048\n\ndef get_inputs():\n A = torch.randn(N, N)\n B = torch.randn(N, N)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.411, "mean_runtime_torch_compile": 0.457}
{"level": 1, "name": "1_Square_matrix_multiplication_"}
kernelbench_level_1_20
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a LeakyReLU activation. """ def __init__(self, negative_slope: float = 0.01): """ Initializes the LeakyReLU module. Args: negative_slope (float, optional): The negative slope of the activation function. Defaults to 0.01. """ super(Model, self).__init__() self.negative_slope = negative_slope def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies LeakyReLU activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with LeakyReLU applied, same shape as input. """ return torch.nn.functional.leaky_relu(x, negative_slope=self.negative_slope) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a LeakyReLU activation.\n \"\"\"\n def __init__(self, negative_slope: float = 0.01):\n \"\"\"\n Initializes the LeakyReLU module.\n\n Args:\n negative_slope (float, optional): The negative slope of the activation function. Defaults to 0.01.\n \"\"\"\n super(Model, self).__init__()\n self.negative_slope = negative_slope\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies LeakyReLU activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with LeakyReLU applied, same shape as input.\n \"\"\"\n return torch.nn.functional.leaky_relu(x, negative_slope=self.negative_slope)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0306, "mean_runtime_torch_compile": 0.0894}
{"level": 1, "name": "20_LeakyReLU"}
kernelbench_level_1_21
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Sigmoid activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Sigmoid activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with Sigmoid applied, same shape as input. """ return torch.sigmoid(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Sigmoid activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Sigmoid activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with Sigmoid applied, same shape as input.\n \"\"\"\n return torch.sigmoid(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0274, "mean_runtime_torch_compile": 0.161}
{"level": 1, "name": "21_Sigmoid"}
kernelbench_level_1_22
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Tanh activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Tanh activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with Tanh applied, same shape as input. """ return torch.tanh(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Tanh activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Tanh activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with Tanh applied, same shape as input.\n \"\"\"\n return torch.tanh(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0277, "mean_runtime_torch_compile": 0.0868}
{"level": 1, "name": "22_Tanh"}
kernelbench_level_1_23
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Softmax activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Softmax activation to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, num_features). Returns: torch.Tensor: Output tensor with Softmax applied, same shape as input. """ return torch.softmax(x, dim=1) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Softmax activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Softmax activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, num_features).\n\n Returns:\n torch.Tensor: Output tensor with Softmax applied, same shape as input.\n \"\"\"\n return torch.softmax(x, dim=1)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0277, "mean_runtime_torch_compile": 0.142}
{"level": 1, "name": "23_Softmax"}
kernelbench_level_1_24
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a LogSoftmax activation. """ def __init__(self, dim: int = 1): super(Model, self).__init__() self.dim = dim def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies LogSoftmax activation to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, dim). Returns: torch.Tensor: Output tensor with LogSoftmax applied, same shape as input. """ return torch.log_softmax(x, dim=self.dim) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a LogSoftmax activation.\n \"\"\"\n def __init__(self, dim: int = 1):\n super(Model, self).__init__()\n self.dim = dim\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies LogSoftmax activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, dim).\n\n Returns:\n torch.Tensor: Output tensor with LogSoftmax applied, same shape as input.\n \"\"\"\n return torch.log_softmax(x, dim=self.dim)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0258, "mean_runtime_torch_compile": 0.158}
{"level": 1, "name": "24_LogSoftmax"}
kernelbench_level_1_25
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Swish activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Swish activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with Swish applied, same shape as input. """ return x * torch.sigmoid(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Swish activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Swish activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with Swish applied, same shape as input.\n \"\"\"\n return x * torch.sigmoid(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0408, "mean_runtime_torch_compile": 0.101}
{"level": 1, "name": "25_Swish"}
kernelbench_level_1_26
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a GELU activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies GELU activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with GELU applied, same shape as input. """ return torch.nn.functional.gelu(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a GELU activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies GELU activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with GELU applied, same shape as input.\n \"\"\"\n return torch.nn.functional.gelu(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0292, "mean_runtime_torch_compile": 0.0876}
{"level": 1, "name": "26_GELU_"}
kernelbench_level_1_27
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a SELU activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies SELU activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with SELU applied, same shape as input. """ return torch.selu(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a SELU activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies SELU activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with SELU applied, same shape as input.\n \"\"\"\n return torch.selu(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0281, "mean_runtime_torch_compile": 0.0901}
{"level": 1, "name": "27_SELU_"}
kernelbench_level_1_28
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a HardSigmoid activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies HardSigmoid activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with HardSigmoid applied, same shape as input. """ return torch.nn.functional.hardsigmoid(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a HardSigmoid activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies HardSigmoid activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with HardSigmoid applied, same shape as input.\n \"\"\"\n return torch.nn.functional.hardsigmoid(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0261, "mean_runtime_torch_compile": 0.0931}
{"level": 1, "name": "28_HardSigmoid"}
kernelbench_level_1_29
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Softplus activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Softplus activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with Softplus applied, same shape as input. """ return torch.nn.functional.softplus(x) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Softplus activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Softplus activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with Softplus applied, same shape as input.\n \"\"\"\n return torch.nn.functional.softplus(x)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0291, "mean_runtime_torch_compile": 0.0862}
{"level": 1, "name": "29_Softplus"}
kernelbench_level_1_2
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a single matrix multiplication (C = A * B) """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs matrix multiplication. Args: A: Input tensor of shape (M, K). B: Input tensor of shape (K, N). Returns: Output tensor of shape (M, N). """ return torch.matmul(A, B) M = 1024 K = 4096 N = 2048 def get_inputs(): A = torch.randn(M, K) B = torch.randn(K, N) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a single matrix multiplication (C = A * B)\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs matrix multiplication.\n\n Args:\n A: Input tensor of shape (M, K).\n B: Input tensor of shape (K, N).\n\n Returns:\n Output tensor of shape (M, N).\n \"\"\"\n return torch.matmul(A, B)\n\nM = 1024\nK = 4096\nN = 2048\n\ndef get_inputs():\n A = torch.randn(M, K)\n B = torch.randn(K, N)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.374, "mean_runtime_torch_compile": 0.417}
{"level": 1, "name": "2_Standard_matrix_multiplication_"}
kernelbench_level_1_30
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs a Softsign activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Softsign activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with Softsign applied, same shape as input. """ return x / (1 + torch.abs(x)) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a Softsign activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Softsign activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with Softsign applied, same shape as input.\n \"\"\"\n return x / (1 + torch.abs(x))\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0549, "mean_runtime_torch_compile": 0.0865}
{"level": 1, "name": "30_Softsign"}
kernelbench_level_1_31
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): """ Simple model that performs an ELU activation. """ def __init__(self, alpha: float = 1.0): """ Initializes the ELU model. Args: alpha (float, optional): The alpha parameter for the ELU function. Defaults to 1.0. """ super(Model, self).__init__() self.alpha = alpha def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies ELU activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with ELU applied, same shape as input. """ return F.elu(x, alpha=self.alpha) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [1.0] # Provide alpha value for initialization ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs an ELU activation.\n \"\"\"\n def __init__(self, alpha: float = 1.0):\n \"\"\"\n Initializes the ELU model.\n\n Args:\n alpha (float, optional): The alpha parameter for the ELU function. Defaults to 1.0.\n \"\"\"\n super(Model, self).__init__()\n self.alpha = alpha\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies ELU activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with ELU applied, same shape as input.\n \"\"\"\n return F.elu(x, alpha=self.alpha)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [1.0] # Provide alpha value for initialization", "mean_runtime_torch": 0.0324, "mean_runtime_torch_compile": 0.0913}
{"level": 1, "name": "31_ELU"}
kernelbench_level_1_32
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): """ Simple model that performs a HardTanh activation. """ def __init__(self): super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies HardTanh activation to the input tensor. Args: x (torch.Tensor): Input tensor of any shape. Returns: torch.Tensor: Output tensor with HardTanh applied, same shape as input. """ return F.hardtanh(x, min_val=-1., max_val=1.) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs a HardTanh activation.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies HardTanh activation to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of any shape.\n\n Returns:\n torch.Tensor: Output tensor with HardTanh applied, same shape as input.\n \"\"\"\n return F.hardtanh(x, min_val=-1., max_val=1.)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.0376, "mean_runtime_torch_compile": 0.0883}
{"level": 1, "name": "32_HardTanh"}
kernelbench_level_1_33
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs Batch Normalization. """ def __init__(self, num_features: int): """ Initializes the BatchNorm layer. Args: num_features (int): Number of features in the input tensor. """ super(Model, self).__init__() self.bn = nn.BatchNorm2d(num_features=num_features) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Batch Normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, num_features, *). Returns: torch.Tensor: Output tensor with Batch Normalization applied, same shape as input. """ return self.bn(x) batch_size = 16 features = 64 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [features] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs Batch Normalization.\n \"\"\"\n def __init__(self, num_features: int):\n \"\"\"\n Initializes the BatchNorm layer.\n\n Args:\n num_features (int): Number of features in the input tensor.\n \"\"\"\n super(Model, self).__init__()\n self.bn = nn.BatchNorm2d(num_features=num_features)\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Batch Normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, num_features, *).\n\n Returns:\n torch.Tensor: Output tensor with Batch Normalization applied, same shape as input.\n \"\"\"\n return self.bn(x)\n\nbatch_size = 16\nfeatures = 64\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return [features]", "mean_runtime_torch": 1.28, "mean_runtime_torch_compile": 1.33}
{"level": 1, "name": "33_BatchNorm"}
kernelbench_level_1_34
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs Instance Normalization. """ def __init__(self, num_features: int): """ Initializes the InstanceNorm layer. Args: num_features (int): Number of features in the input tensor. """ super(Model, self).__init__() self.inorm = nn.InstanceNorm2d(num_features=num_features) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Instance Normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, num_features, height, width). Returns: torch.Tensor: Output tensor with Instance Normalization applied, same shape as input. """ return self.inorm(x) batch_size = 16 features = 64 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [features] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs Instance Normalization.\n \"\"\"\n def __init__(self, num_features: int):\n \"\"\"\n Initializes the InstanceNorm layer.\n\n Args:\n num_features (int): Number of features in the input tensor.\n \"\"\"\n super(Model, self).__init__()\n self.inorm = nn.InstanceNorm2d(num_features=num_features)\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Instance Normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, num_features, height, width).\n\n Returns:\n torch.Tensor: Output tensor with Instance Normalization applied, same shape as input.\n \"\"\"\n return self.inorm(x)\n\nbatch_size = 16\nfeatures = 64\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return [features]", "mean_runtime_torch": 1.27, "mean_runtime_torch_compile": 1.15}
{"level": 1, "name": "34_InstanceNorm"}
kernelbench_level_1_35
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs Group Normalization. """ def __init__(self, num_features: int, num_groups: int): """ Initializes the GroupNorm layer. Args: num_features (int): Number of features in the input tensor. num_groups (int): Number of groups to divide the channels into. """ super(Model, self).__init__() self.gn = nn.GroupNorm(num_groups=num_groups, num_channels=num_features) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Group Normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, num_features, *). Returns: torch.Tensor: Output tensor with Group Normalization applied, same shape as input. """ return self.gn(x) batch_size = 16 features = 64 num_groups = 8 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [features, num_groups] # num_features ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs Group Normalization.\n \"\"\"\n def __init__(self, num_features: int, num_groups: int):\n \"\"\"\n Initializes the GroupNorm layer.\n\n Args:\n num_features (int): Number of features in the input tensor.\n num_groups (int): Number of groups to divide the channels into.\n \"\"\"\n super(Model, self).__init__()\n self.gn = nn.GroupNorm(num_groups=num_groups, num_channels=num_features)\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Group Normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, num_features, *).\n\n Returns:\n torch.Tensor: Output tensor with Group Normalization applied, same shape as input.\n \"\"\"\n return self.gn(x)\n\nbatch_size = 16\nfeatures = 64\nnum_groups = 8\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return [features, num_groups] # num_features", "mean_runtime_torch": 1.25, "mean_runtime_torch_compile": 1.27}
{"level": 1, "name": "35_GroupNorm_"}
kernelbench_level_1_36
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs RMS Normalization. """ def __init__(self, num_features: int, eps: float = 1e-5): """ Initializes the RMSNorm layer. Args: num_features (int): Number of features in the input tensor. eps (float, optional): A small value added to the denominator to avoid division by zero. Defaults to 1e-5. """ super(Model, self).__init__() self.num_features = num_features self.eps = eps def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies RMS Normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (batch_size, num_features, *). Returns: torch.Tensor: Output tensor with RMS Normalization applied, same shape as input. """ # Calculate the RMS along the feature dimension rms = torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + self.eps) # Normalize the input by dividing by the RMS return x / rms batch_size = 16 features = 64 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [features] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs RMS Normalization.\n \"\"\"\n def __init__(self, num_features: int, eps: float = 1e-5):\n \"\"\"\n Initializes the RMSNorm layer.\n\n Args:\n num_features (int): Number of features in the input tensor.\n eps (float, optional): A small value added to the denominator to avoid division by zero. Defaults to 1e-5.\n \"\"\"\n super(Model, self).__init__()\n self.num_features = num_features\n self.eps = eps\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies RMS Normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (batch_size, num_features, *).\n\n Returns:\n torch.Tensor: Output tensor with RMS Normalization applied, same shape as input.\n \"\"\"\n # Calculate the RMS along the feature dimension\n rms = torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + self.eps)\n\n # Normalize the input by dividing by the RMS\n return x / rms\n\nbatch_size = 16\nfeatures = 64\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return [features]", "mean_runtime_torch": 2.06, "mean_runtime_torch_compile": 1.26}
{"level": 1, "name": "36_RMSNorm_"}
kernelbench_level_1_37
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs Frobenius norm normalization. """ def __init__(self): """ Initializes the Frobenius norm normalization layer. """ super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Frobenius norm normalization to the input tensor. Args: x (torch.Tensor): Input tensor of arbitrary shape. Returns: torch.Tensor: Output tensor with Frobenius norm normalization applied, same shape as input. """ norm = torch.norm(x, p='fro') return x / norm batch_size = 16 features = 64 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs Frobenius norm normalization.\n \"\"\"\n def __init__(self):\n \"\"\"\n Initializes the Frobenius norm normalization layer.\n \"\"\"\n super(Model, self).__init__()\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Frobenius norm normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of arbitrary shape.\n\n Returns:\n torch.Tensor: Output tensor with Frobenius norm normalization applied, same shape as input.\n \"\"\"\n norm = torch.norm(x, p='fro')\n return x / norm\n\nbatch_size = 16\nfeatures = 64\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return []", "mean_runtime_torch": 1.22, "mean_runtime_torch_compile": 1.27}
{"level": 1, "name": "37_FrobeniusNorm_"}
kernelbench_level_1_38
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs L1 normalization. """ def __init__(self): """ Initializes the L1 normalization layer. """ super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies L1 normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (..., dim, ...). Returns: torch.Tensor: Output tensor with L1 normalization applied, same shape as input. """ return x / torch.sum(torch.abs(x), dim=1, keepdim=True) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs L1 normalization.\n \"\"\"\n def __init__(self):\n \"\"\"\n Initializes the L1 normalization layer.\n \"\"\"\n super(Model, self).__init__()\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies L1 normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (..., dim, ...).\n\n Returns:\n torch.Tensor: Output tensor with L1 normalization applied, same shape as input.\n \"\"\"\n return x / torch.sum(torch.abs(x), dim=1, keepdim=True)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return []", "mean_runtime_torch": 0.0603, "mean_runtime_torch_compile": 0.116}
{"level": 1, "name": "38_L1Norm_"}
kernelbench_level_1_39
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs L2 normalization. """ def __init__(self): """ Initializes the L2Norm layer. Args: dim (int): Dimension along which to normalize. """ super(Model, self).__init__() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies L2 normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (*, dim, *). Returns: torch.Tensor: Output tensor with L2 normalization applied, same shape as input. """ return x / torch.norm(x, p=2, dim=1, keepdim=True) batch_size = 16 dim = 16384 def get_inputs(): x = torch.randn(batch_size, dim) return [x] def get_init_inputs(): return [] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs L2 normalization.\n \"\"\"\n def __init__(self):\n \"\"\"\n Initializes the L2Norm layer.\n\n Args:\n dim (int): Dimension along which to normalize.\n \"\"\"\n super(Model, self).__init__()\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies L2 normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (*, dim, *).\n\n Returns:\n torch.Tensor: Output tensor with L2 normalization applied, same shape as input.\n \"\"\"\n return x / torch.norm(x, p=2, dim=1, keepdim=True)\n\nbatch_size = 16\ndim = 16384\n\ndef get_inputs():\n x = torch.randn(batch_size, dim)\n return [x]\n\ndef get_init_inputs():\n return []", "mean_runtime_torch": 0.0549, "mean_runtime_torch_compile": 0.122}
{"level": 1, "name": "39_L2Norm_"}
kernelbench_level_1_3
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Performs batched matrix multiplication (C = A * B) where A, B, and C have the same batch dimension. """ def __init__(self): super(Model, self).__init__() def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: """ Performs batched matrix multiplication. Args: A: Input tensor of shape (batch_size, m, k). B: Input tensor of shape (batch_size, k, n). Returns: C: Output tensor of shape (batch_size, m, n). """ return torch.bmm(A, B) batch_size = 128 m = 128 k = 256 n = 512 def get_inputs(): A = torch.randn(batch_size, m, k) B = torch.randn(batch_size, k, n) return [A, B] def get_init_inputs(): return [] # No special initialization inputs needed ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Performs batched matrix multiplication (C = A * B) where A, B, and C have the same batch dimension.\n \"\"\"\n def __init__(self):\n super(Model, self).__init__()\n \n def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Performs batched matrix multiplication.\n\n Args:\n A: Input tensor of shape (batch_size, m, k).\n B: Input tensor of shape (batch_size, k, n).\n\n Returns:\n C: Output tensor of shape (batch_size, m, n).\n \"\"\"\n return torch.bmm(A, B)\n\nbatch_size = 128\nm = 128\nk = 256\nn = 512\n\ndef get_inputs():\n A = torch.randn(batch_size, m, k)\n B = torch.randn(batch_size, k, n)\n return [A, B]\n\ndef get_init_inputs():\n return [] # No special initialization inputs needed", "mean_runtime_torch": 0.168, "mean_runtime_torch_compile": 0.208}
{"level": 1, "name": "3_Batched_matrix_multiplication"}
kernelbench_level_1_40
Kernels
kernelbench
You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination. Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: ``` import torch import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() def forward(self, a, b): return a + b def get_inputs(): # randomly generate input tensors based on the model architecture a = torch.randn(1, 128).cuda() b = torch.randn(1, 128).cuda() return [a, b] def get_init_inputs(): # randomly generate tensors required for initialization based on the model architecture return [] ``` The example new arch with custom CUDA kernels looks like this: ``` import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.cpp_extension import load_inline # Define the custom CUDA kernel for element-wise addition elementwise_add_source = """ #include <torch/extension.h> #include <cuda_runtime.h> __global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if (idx < size) { out[idx] = a[idx] + b[idx]; } } torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) { auto size = a.numel(); auto out = torch::zeros_like(a); const int block_size = 256; const int num_blocks = (size + block_size - 1) / block_size; elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size); return out; } """ elementwise_add_cpp_source = ( "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);" ) # Compile the inline CUDA code for element-wise addition elementwise_add = load_inline( name="elementwise_add", cpp_sources=elementwise_add_cpp_source, cuda_sources=elementwise_add_source, functions=["elementwise_add_cuda"], verbose=True, extra_cflags=[""], extra_ldflags=[""], ) class ModelNew(nn.Module): def __init__(self) -> None: super().__init__() self.elementwise_add = elementwise_add def forward(self, a, b): return self.elementwise_add.elementwise_add_cuda(a, b) ``` You are given the following architecture: ``` import torch import torch.nn as nn class Model(nn.Module): """ Simple model that performs Layer Normalization. """ def __init__(self, normalized_shape: tuple): """ Initializes the LayerNorm layer. Args: normalized_shape (tuple): Shape of the input tensor to be normalized. """ super(Model, self).__init__() self.ln = nn.LayerNorm(normalized_shape=normalized_shape) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Applies Layer Normalization to the input tensor. Args: x (torch.Tensor): Input tensor of shape (*, normalized_shape). Returns: torch.Tensor: Output tensor with Layer Normalization applied, same shape as input. """ return self.ln(x) batch_size = 16 features = 64 dim1 = 256 dim2 = 256 def get_inputs(): x = torch.randn(batch_size, features, dim1, dim2) return [x] def get_init_inputs(): return [(features, dim1, dim2)] ``` Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code!
{"reference_arch": "import torch\nimport torch.nn as nn\n\nclass Model(nn.Module):\n \"\"\"\n Simple model that performs Layer Normalization.\n \"\"\"\n def __init__(self, normalized_shape: tuple):\n \"\"\"\n Initializes the LayerNorm layer.\n\n Args:\n normalized_shape (tuple): Shape of the input tensor to be normalized.\n \"\"\"\n super(Model, self).__init__()\n self.ln = nn.LayerNorm(normalized_shape=normalized_shape)\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n \"\"\"\n Applies Layer Normalization to the input tensor.\n\n Args:\n x (torch.Tensor): Input tensor of shape (*, normalized_shape).\n\n Returns:\n torch.Tensor: Output tensor with Layer Normalization applied, same shape as input.\n \"\"\"\n return self.ln(x)\n\nbatch_size = 16\nfeatures = 64\ndim1 = 256\ndim2 = 256\n\ndef get_inputs():\n x = torch.randn(batch_size, features, dim1, dim2)\n return [x]\n\ndef get_init_inputs():\n return [(features, dim1, dim2)]", "mean_runtime_torch": 6.18, "mean_runtime_torch_compile": 1.6}
{"level": 1, "name": "40_LayerNorm"}
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
0