48_Conv3d_Scaling_Tanh_Multiply_Sigmoid
• constant_mem_optimization_base
import torch
import torch.nn as nn
import torch.nn.functional as F
def module_fn(
x: torch.Tensor,
conv_weight: torch.Tensor,
conv_bias: torch.Tensor,
scaling_factor: torch.Tensor,
bias: torch.Tensor,
) -> torch.Tensor:
"""
Applies 3D convolution, scaling, tanh, bias multiplication and sigmoid.
Args:
x (torch.Tensor): Input tensor of shape (batch_size, in_channels, depth, height, width)
conv_weight (torch.Tensor): 3D convolution weight tensor
conv_bias (torch.Tensor): 3D convolution bias tensor
scaling_factor (torch.Tensor): Scaling factor tensor of shape (out_channels, 1, 1, 1)
bias (torch.Tensor): Bias tensor of shape (out_channels, 1, 1, 1)
Returns:
torch.Tensor: Output tensor after applying convolution, scaling, tanh, bias and sigmoid
"""
x = F.conv3d(x, conv_weight, bias=conv_bias)
x = x * scaling_factor
x = torch.tanh(x)
x = x * bias
x = torch.sigmoid(x)
return x
class Model(nn.Module):
"""
Model that performs a 3D convolution, scales the output, applies tanh, multiplies by a scaling factor, and applies sigmoid.
"""
def __init__(
self, in_channels, out_channels, kernel_size, scaling_factor, bias_shape
):
super(Model, self).__init__()
conv = nn.Conv3d(in_channels, out_channels, kernel_size)
self.conv_weight = nn.Parameter(conv.weight)
self.conv_bias = nn.Parameter(
conv.bias
+ torch.randn(
conv.bias.shape, device=conv.bias.device, dtype=conv.bias.dtype
)
* 0.02
)
self.scaling_factor = nn.Parameter(torch.randn(bias_shape) * 0.02)
self.bias = nn.Parameter(torch.randn(bias_shape) * 0.02)
def forward(self, x, fn=module_fn):
return fn(x, self.conv_weight, self.conv_bias, self.scaling_factor, self.bias)
batch_size = 128
in_channels = 3
out_channels = 16
depth, height, width = 16, 32, 32
kernel_size = 3
scaling_factor = 2
bias_shape = (out_channels, 1, 1, 1)
def get_inputs():
return [torch.randn(batch_size, in_channels, depth, height, width)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, scaling_factor, bias_shape]
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Model that performs a 3D convolution, scales the output, applies tanh, multiplies by a scaling factor, and applies sigmoid.
"""
def __init__(self, in_channels, out_channels, kernel_size, scaling_factor, bias_shape):
super(Model, self).__init__()
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size)
self.conv.bias = nn.Parameter(self.conv.bias + torch.randn(self.conv.bias.shape, device=self.conv.bias.device, dtype=self.conv.bias.dtype) * 0.02)
self.scaling_factor = nn.Parameter(torch.randn(bias_shape) * 0.02)
self.bias = nn.Parameter(torch.randn(bias_shape) * 0.02)
def forward(self, x):
x = self.conv(x)
x = x * self.scaling_factor
x = torch.tanh(x)
x = x * self.bias
x = torch.sigmoid(x)
return x
batch_size = 128
in_channels = 3
out_channels = 16
depth, height, width = 16, 32, 32
kernel_size = 3
scaling_factor = 2
bias_shape = (out_channels, 1, 1, 1)
def get_inputs():
return [torch.randn(batch_size, in_channels, depth, height, width)]
def get_init_inputs():
return [in_channels, out_channels, kernel_size, scaling_factor, bias_shape]
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <vector>
#include <math.h>
// Constant memory declarations
__constant__ float c_scaling[8192];
__constant__ float c_bias[8192];
template <typename scalar_t>
__global__ void conv3d_constant_mem_kernel(
const scalar_t* __restrict__ output,
scalar_t* __restrict__ result,
const int batch_size,
const int out_channels,
const int depth,
const int height,
const int width) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int total_elements = batch_size * out_channels * depth * height * width;
if (idx < total_elements) {
const int c = (idx / (width * height * depth)) % out_channels;
scalar_t val = output[idx];
val *= c_scaling[c];
val = tanh(val);
val *= c_bias[c];
if (std::is_same<scalar_t, float>::value) {
val = 1.0f / (1.0f + __expf(-val));
} else {
val = 1.0 / (1.0 + exp(-val));
}
result[idx] = val;
}
}
torch::Tensor forward(
torch::Tensor x,
torch::Tensor conv_weight,
torch::Tensor conv_bias,
torch::Tensor scaling_factor,
torch::Tensor bias) {
auto conv_out = torch::conv3d(x, conv_weight, conv_bias);
const int out_channels = conv_out.size(1);
if (out_channels > 8192) {
AT_ERROR("out_channels exceeds constant memory capacity");
}
// Copy parameters to constant memory
cudaMemcpyToSymbol(c_scaling, scaling_factor.data_ptr<float>(), out_channels * sizeof(float));
cudaMemcpyToSymbol(c_bias, bias.data_ptr<float>(), out_channels * sizeof(float));
auto result = torch::empty_like(conv_out);
const int total_elements = conv_out.numel();
const int threads = 256;
const int blocks = (total_elements + threads - 1) / threads;
AT_DISPATCH_FLOATING_TYPES(conv_out.scalar_type(), "conv3d_constant_mem", ([&] {
conv3d_constant_mem_kernel<scalar_t><<<blocks, threads>>>(
conv_out.data_ptr<scalar_t>(),
result.data_ptr<scalar_t>(),
conv_out.size(0),
out_channels,
conv_out.size(2),
conv_out.size(3),
conv_out.size(4)
);
}));
return result;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &forward, "Conv3d with constant memory optimization");
}
Metric | Value | Unit | Variance | Samples |
---|---|---|---|---|
Executed Ipc Active | 3.238 | inst/cycle | 0.000 | 5 |
Executed Ipc Elapsed | 3.170 | inst/cycle | 0.000 | 5 |
Issue Slots Busy | 81.006 | % | 0.022 | 5 |
Issued Ipc Active | 3.238 | inst/cycle | 0.000 | 5 |
SM Busy | 81.006 | % | 0.022 | 5 |
Memory Throughput | 1644842218153.616 | byte/second | 2401827829664609792.000 | 5 |
Mem Busy | 27.204 | % | 0.002 | 5 |
Max Bandwidth | 49.080 | % | 0.002 | 5 |
L1/TEX Hit Rate | 0.000 | % | 0.000 | 5 |
L2 Hit Rate | 49.900 | % | 0.023 | 5 |
Mem Pipes Busy | 53.522 | % | 0.002 | 5 |
Warp Cycles Per Issued Instruction | 15.252 | cycle | 0.000 | 5 |
Warp Cycles Per Executed Instruction | 15.252 | cycle | 0.000 | 5 |
Avg. Active Threads Per Warp | 32.000 | 0.000 | 5 | |
Avg. Not Predicated Off Threads Per Warp | 28.650 | 0.000 | 5 | |
Max Active Clusters | 0.000 | cluster | 0.000 | 5 |
Max Cluster Size | 8.000 | block | 0.000 | 5 |
Overall GPU Occupancy | 0.000 | % | 0.000 | 5 |
Cluster Occupancy | 0.000 | % | 0.000 | 5 |
Block Limit SM | 32.000 | block | 0.000 | 5 |
Block Limit Registers | 16.000 | block | 0.000 | 5 |
Block Limit Shared Mem | 32.000 | block | 0.000 | 5 |
Block Limit Warps | 8.000 | block | 0.000 | 5 |
Theoretical Active Warps per SM | 64.000 | warp | 0.000 | 5 |
Theoretical Occupancy | 100.000 | % | 0.000 | 5 |
Achieved Occupancy | 78.214 | % | 0.000 | 5 |
Achieved Active Warps Per SM | 50.060 | warp | 0.000 | 5 |
Rule | Description |
---|---|
INF HighPipeUtilization | ALU is the highest-utilized pipeline (54.5%) based on active cycles, taking into account the rates of its different instructions. It executes integer and logic operations. It is well-utilized, but should not be a bottleneck. |
WRN Occupancy | This kernel's theoretical occupancy is not impacted by any block limit. The difference between calculated theoretical (100.0%) and measured achieved occupancy (78.2%) can be the result of warp scheduling overheads or workload imbalances during the kernel execution. Load imbalances can occur between warps within a block as well as across blocks of the same kernel. See the CUDA Best Practices Guide (https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#occupancy) for more details on optimizing occupancy. |
Operation / Metric | Value | Unit |
---|---|---|
aten::to | ||
CPU Time | 596163.26 | μs |
Device Time | 2760.82 | μs |
Self CPU Time | 59.93 | μ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::empty_strided | ||
CPU Time | 620034.45 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 22982.41 | μ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::conv3d | ||
CPU Time | 427204.36 | μs |
Device Time | 4285297.49 | μs |
Self CPU Time | 15759.10 | μ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 | 411445.27 | μs |
Device Time | 4285297.49 | μs |
Self CPU Time | 19676.71 | μ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 | 391768.55 | μs |
Device Time | 4285297.49 | μs |
Self CPU Time | 37879.12 | μ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 | 258515.04 | μs |
Device Time | 3719252.40 | μs |
Self CPU Time | 186450.07 | μs |
Self Device Time | 3719252.40 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
sm80_xmma_fprop_implicit_gemm_indexed_f32f32_f32f32_f32_nchwkcrs_nchw_tilesize32x32x8_stage3_warpsize1x2x1_g1_ffma_aligna4_alignc4_execute_kernel__5x_cudnn | ||
CPU Time | 0.00 | μs |
Device Time | 3719250.73 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 3719250.73 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
cudaLaunchKernel | ||
CPU Time | 3956820.89 | μs |
Device Time | 69667.52 | μs |
Self CPU Time | 3956820.89 | μs |
Self Device Time | 69667.52 | μ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 | 3545671.03 | μs |
Device Time | 482748.26 | μs |
Self CPU Time | 17644.05 | μ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 | 3528028.53 | μs |
Device Time | 482748.26 | μs |
Self CPU Time | 26859.15 | μs |
Self Device Time | 482748.26 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
45289 warnings generated when compiling for host. Suppressed 45325 warnings (45278 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.