import torch
import torch.nn as nn
import torch.nn.functional as F
def module_fn(
x: torch.Tensor,
conv1_weight: nn.Parameter,
conv1_bias: nn.Parameter,
conv2_weight: nn.Parameter,
conv2_bias: nn.Parameter,
fc1_weight: nn.Parameter,
fc1_bias: nn.Parameter,
fc2_weight: nn.Parameter,
fc2_bias: nn.Parameter,
fc3_weight: nn.Parameter,
fc3_bias: nn.Parameter,
) -> torch.Tensor:
"""
Implements a LeNet-5 architecture with ReLU activation.
Args:
x (torch.Tensor): The input tensor, shape (batch_size, 1, 32, 32)
conv1_weight (nn.Parameter): Parameters for first conv layer
conv1_bias (nn.Parameter): Parameters for first conv layer
conv2_weight (nn.Parameter): Parameters for second conv layer
conv2_bias (nn.Parameter): Parameters for second conv layer
fc1_weight (nn.Parameter): Parameters for first FC layer
fc1_bias (nn.Parameter): Parameters for first FC layer
fc2_weight (nn.Parameter): Parameters for second FC layer
fc3_weight (nn.Parameter): Parameters for third FC layer
fc3_bias (nn.Parameter): Parameters for third FC layer
Returns:
torch.Tensor: The output tensor, shape (batch_size, num_classes)
"""
# First convolutional layer with ReLU activation and max pooling
x = F.conv2d(x, conv1_weight, conv1_bias, stride=1)
x = F.relu(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
# Second convolutional layer with ReLU activation and max pooling
x = F.conv2d(x, conv2_weight, conv2_bias, stride=1)
x = F.relu(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
# Flatten the output for the fully connected layers
x = x.view(-1, 16 * 5 * 5)
# First fully connected layer with ReLU activation
x = F.linear(x, fc1_weight, fc1_bias)
x = F.relu(x)
# Second fully connected layer with ReLU activation
x = F.linear(x, fc2_weight, fc2_bias)
x = F.relu(x)
# Final fully connected layer
x = F.linear(x, fc3_weight, fc3_bias)
return x
class Model(nn.Module):
def __init__(self, num_classes):
"""
LeNet-5 architecture implementation in PyTorch.
:param num_classes: The number of output classes.
"""
super(Model, self).__init__()
# Extract parameters from convolutional layers
conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1)
self.conv1_weight = nn.Parameter(conv1.weight.data.clone())
self.conv1_bias = nn.Parameter(conv1.bias.data.clone())
conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1)
self.conv2_weight = nn.Parameter(conv2.weight.data.clone())
self.conv2_bias = nn.Parameter(conv2.bias.data.clone())
# Extract parameters from fully connected layers
fc1 = nn.Linear(in_features=16 * 5 * 5, out_features=120)
self.fc1_weight = nn.Parameter(fc1.weight.data.clone())
self.fc1_bias = nn.Parameter(fc1.bias.data.clone())
fc2 = nn.Linear(in_features=120, out_features=84)
self.fc2_weight = nn.Parameter(fc2.weight.data.clone())
self.fc2_bias = nn.Parameter(fc2.bias.data.clone())
fc3 = nn.Linear(in_features=84, out_features=num_classes)
self.fc3_weight = nn.Parameter(fc3.weight.data.clone())
self.fc3_bias = nn.Parameter(fc3.bias.data.clone())
def forward(self, x, fn=module_fn):
return fn(
x,
self.conv1_weight,
self.conv1_bias,
self.conv2_weight,
self.conv2_bias,
self.fc1_weight,
self.fc1_bias,
self.fc2_weight,
self.fc2_bias,
self.fc3_weight,
self.fc3_bias,
)
# Test code for the LeNet-5 model
batch_size = 1
num_classes = 10
def get_inputs():
return [torch.randn(batch_size, 1, 32, 32)]
def get_init_inputs():
return [num_classes]
import torch
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self, num_classes):
"""
LeNet-5 architecture implementation in PyTorch.
:param num_classes: The number of output classes.
"""
super(Model, self).__init__()
# Convolutional layers
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1)
# Fully connected layers
self.fc1 = nn.Linear(in_features=16*5*5, out_features=120)
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=num_classes)
def forward(self, x):
"""
Forward pass of the LeNet-5 model.
:param x: The input tensor, shape (batch_size, 1, 32, 32)
:return: The output tensor, shape (batch_size, num_classes)
"""
# First convolutional layer with ReLU activation and max pooling
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, kernel_size=2, stride=2)
# Second convolutional layer with ReLU activation and max pooling
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, kernel_size=2, stride=2)
# Flatten the output for the fully connected layers
x = x.view(-1, 16*5*5)
# First fully connected layer with ReLU activation
x = F.relu(self.fc1(x))
# Second fully connected layer with ReLU activation
x = F.relu(self.fc2(x))
# Final fully connected layer
x = self.fc3(x)
return x
# Test code for the LeNet-5 model
batch_size = 1
num_classes = 10
def get_inputs():
return [torch.randn(batch_size, 1, 32, 32)]
def get_init_inputs():
return [num_classes]
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cublas_v2.h>
#include <ATen/cuda/CUDAContext.h>
#include <ATen/cuda/CUDAUtils.h>
#include <cfloat>
// Fused kernel: Applies ReLU and then performs 2D max pooling in one pass.
// Workload is evenly distributed using a grid-stride loop.
__global__ void fused_relu_pool_kernel(
const float* __restrict__ input,
float* __restrict__ output,
int batch, int channels,
int height, int width,
int pool_h, int pool_w, int stride
) {
int out_h = (height - pool_h) / stride + 1;
int out_w = (width - pool_w) / stride + 1;
int total = batch * channels * out_h * out_w;
for (int idx = blockIdx.x * blockDim.x + threadIdx.x; idx < total; idx += blockDim.x * gridDim.x) {
int tmp = idx;
int w = tmp % out_w; tmp /= out_w;
int h = tmp % out_h; tmp /= out_h;
int c = tmp % channels; tmp /= channels;
int b = tmp;
int in_row_start = h * stride;
int in_col_start = w * stride;
// Initialize to 0 since with ReLU negatives become 0.
float max_val = 0.0f;
for (int i = 0; i < pool_h; i++) {
for (int j = 0; j < pool_w; j++) {
int in_row = in_row_start + i;
int in_col = in_col_start + j;
float val = input[(b * channels + c) * height * width + in_row * width + in_col];
// Apply ReLU inline
float relu_val = fmaxf(val, 0.0f);
if (relu_val > max_val) {
max_val = relu_val;
}
}
}
output[idx] = max_val;
}
}
// Simple flattening kernel using a grid-stride loop
__global__ void flatten_kernel(const float* __restrict__ input, float* __restrict__ output, int total) {
for (int idx = blockIdx.x * blockDim.x + threadIdx.x; idx < total; idx += blockDim.x * gridDim.x) {
output[idx] = input[idx];
}
}
// Forward function for the LeNet-5 network that uses the fused ReLU+Pool kernel
// to better distribute workloads evenly and reduce kernel launch overhead.
torch::Tensor forward(
torch::Tensor x,
torch::Tensor conv1_weight, torch::Tensor conv1_bias,
torch::Tensor conv2_weight, torch::Tensor conv2_bias,
torch::Tensor fc1_weight, torch::Tensor fc1_bias,
torch::Tensor fc2_weight, torch::Tensor fc2_bias,
torch::Tensor fc3_weight, torch::Tensor fc3_bias
) {
// Move all inputs to CUDA
x = x.to(torch::kCUDA);
conv1_weight = conv1_weight.to(torch::kCUDA);
conv1_bias = conv1_bias.to(torch::kCUDA);
conv2_weight = conv2_weight.to(torch::kCUDA);
conv2_bias = conv2_bias.to(torch::kCUDA);
fc1_weight = fc1_weight.to(torch::kCUDA);
fc1_bias = fc1_bias.to(torch::kCUDA);
fc2_weight = fc2_weight.to(torch::kCUDA);
fc2_bias = fc2_bias.to(torch::kCUDA);
fc3_weight = fc3_weight.to(torch::kCUDA);
fc3_bias = fc3_bias.to(torch::kCUDA);
// First Convolutional Layer
auto conv1 = torch::conv2d(x, conv1_weight, conv1_bias, {1, 1});
// Instead of launching separate ReLU and max_pool kernels, we fuse them.
int B = conv1.size(0);
int C = conv1.size(1);
int H = conv1.size(2);
int W = conv1.size(3);
int pool_h = 2, pool_w = 2, stride = 2;
int out_h = (H - pool_h) / stride + 1;
int out_w = (W - pool_w) / stride + 1;
auto pool1 = torch::empty({B, C, out_h, out_w}, conv1.options());
int total_pool1 = B * C * out_h * out_w;
int threads = 256;
int blocks = (total_pool1 + threads - 1) / threads;
fused_relu_pool_kernel<<<blocks, threads>>>(
conv1.data_ptr<float>(), pool1.data_ptr<float>(), B, C, H, W, pool_h, pool_w, stride);
// Second Convolutional Layer
auto conv2 = torch::conv2d(pool1, conv2_weight, conv2_bias, {1, 1});
B = conv2.size(0);
C = conv2.size(1);
H = conv2.size(2);
W = conv2.size(3);
out_h = (H - pool_h) / stride + 1;
out_w = (W - pool_w) / stride + 1;
auto pool2 = torch::empty({B, C, out_h, out_w}, conv2.options());
int total_pool2 = B * C * out_h * out_w;
blocks = (total_pool2 + threads - 1) / threads;
fused_relu_pool_kernel<<<blocks, threads>>>(
conv2.data_ptr<float>(), pool2.data_ptr<float>(), B, C, H, W, pool_h, pool_w, stride);
// Flatten the output
auto flat = pool2.view({pool2.size(0), -1});
// Fully connected layers are computed using torch::linear which are highly optimized (e.g., via cuBLAS)
auto fc1 = torch::linear(flat, fc1_weight, fc1_bias);
fc1 = torch::relu(fc1);
auto fc2 = torch::linear(fc1, fc2_weight, fc2_bias);
fc2 = torch::relu(fc2);
auto fc3 = torch::linear(fc2, fc3_weight, fc3_bias);
return fc3;
}
// PyBind11 module definition
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &forward, "LeNet-5 forward pass with fused ReLU and pooling");
}
Metric | Value | Unit | Variance | Samples |
---|
Rule | Description |
---|
Operation / Metric | Value | Unit |
---|---|---|
aten::conv2d | ||
CPU Time | 1007107.07 | μs |
Device Time | 268320.65 | μs |
Self CPU Time | 42727.48 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::convolution | ||
CPU Time | 964379.59 | μs |
Device Time | 268320.65 | μs |
Self CPU Time | 53321.88 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::_convolution | ||
CPU Time | 911057.71 | μs |
Device Time | 268320.65 | μs |
Self CPU Time | 110018.17 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::cudnn_convolution | ||
CPU Time | 576657.48 | μs |
Device Time | 188101.13 | μs |
Self CPU Time | 393488.23 | μs |
Self Device Time | 188101.13 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::linear | ||
CPU Time | 688650.33 | μs |
Device Time | 149675.68 | μs |
Self CPU Time | 62318.81 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::zero_ | ||
CPU Time | 111202.35 | μs |
Device Time | 952686.33 | μs |
Self CPU Time | 27017.18 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::fill_ | ||
CPU Time | 84186.41 | μs |
Device Time | 952686.33 | μs |
Self CPU Time | 33320.88 | μs |
Self Device Time | 952686.33 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
void at::native::vectorized_elementwise_kernel<4, at::native::FillFunctor<int>, at::detail::Array<char*, 1> >(int, at::native::FillFunctor<int>, at::detail::Array<char*, 1>) | ||
CPU Time | 0.00 | μs |
Device Time | 952686.33 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 952686.33 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
45312 warnings generated when compiling for host. Suppressed 45346 warnings (45299 in non-user code, 47 NOLINT). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.