9_Tall_skinny_matrix_multiplication_
• unrolled_matmul_kernel_base
import torch
import torch.nn as nn
import torch.nn.functional as F
def module_fn(A, B):
"""
Performs a single matrix multiplication (C = A * B) where one of the matrices is tall and skinny (M >> N or N >> M).
Args:
A (torch.Tensor): Input matrix of shape (M, K) or (K, M) where M >> N or N >> M.
B (torch.Tensor): Input matrix of shape (K, N) or (N, K) where M >> N or N >> M.
Returns:
torch.Tensor: Output matrix of shape (M, N) or (N, M)
"""
return torch.matmul(A, B)
class Model(nn.Module):
"""
Simple model that performs a single matrix multiplication (C = A * B) where one of the matrices is tall and skinny (M >> N or N >> M)
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, A, B, fn=module_fn):
return fn(A, B)
M = 16384
N = 16
def get_inputs():
A = torch.randn(M, N)
B = torch.randn(N, M)
return [A, B]
def get_init_inputs():
return [] # No special initialization inputs needed
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Simple model that performs a single matrix multiplication (C = A * B) where one of the matrices is tall and skinny (M >> N or N >> M)
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, A, B):
"""
Performs the matrix multiplication.
Args:
A (torch.Tensor): Input matrix of shape (M, K) or (K, M) where M >> N or N >> M.
B (torch.Tensor): Input matrix of shape (K, N) or (N, K) where M >> N or N >> M.
Returns:
torch.Tensor: Output matrix of shape (M, N) or (N, M)
"""
return torch.matmul(A, B)
M = 16384
N = 16
def get_inputs():
A = torch.randn(M, N)
B = torch.randn(N, M)
return [A, B]
def get_init_inputs():
return [] # No special initialization inputs needed
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define BLOCK_SIZE 16
#define ELEMENTS_PER_THREAD 4
__constant__ int const_dims[6]; // M, N, K, lda, ldb, ldc
__constant__ bool const_trans[2]; // transA, transB
__device__ float get_element(const float* __restrict__ matrix, int row, int col, int ld, bool transpose) {
return transpose ? matrix[col * ld + row] : matrix[row * ld + col];
}
__global__ void matmul_kernel_unrolled(const float* __restrict__ A,
const float* __restrict__ B,
float* __restrict__ C) {
const int M = const_dims[0];
const int N = const_dims[1];
const int K = const_dims[2];
const int lda = const_dims[3];
const int ldb = const_dims[4];
const int ldc = const_dims[5];
const bool transA = const_trans[0];
const bool transB = const_trans[1];
int block_row = blockIdx.y * (BLOCK_SIZE * ELEMENTS_PER_THREAD);
int block_col = blockIdx.x * BLOCK_SIZE;
int thread_row = threadIdx.y;
int thread_col = threadIdx.x;
__shared__ float As[ELEMENTS_PER_THREAD][BLOCK_SIZE][BLOCK_SIZE];
__shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
float C_values[ELEMENTS_PER_THREAD] = {0.0f};
for (int t = 0; t < (K + BLOCK_SIZE - 1) / BLOCK_SIZE; ++t) {
if (t * BLOCK_SIZE + thread_row < K && block_col + thread_col < N) {
Bs[thread_row][thread_col] = get_element(B,
t * BLOCK_SIZE + thread_row,
block_col + thread_col,
ldb, transB);
} else {
Bs[thread_row][thread_col] = 0.0f;
}
#pragma unroll
for (int e = 0; e < ELEMENTS_PER_THREAD; ++e) {
int row = block_row + e * BLOCK_SIZE + thread_row;
if (row < M && t * BLOCK_SIZE + thread_col < K) {
As[e][thread_row][thread_col] = get_element(A,
row,
t * BLOCK_SIZE + thread_col,
lda, transA);
} else {
As[e][thread_row][thread_col] = 0.0f;
}
}
__syncthreads();
#pragma unroll
for (int e = 0; e < ELEMENTS_PER_THREAD; ++e) {
#pragma unroll
for (int k = 0; k < BLOCK_SIZE; ++k) {
C_values[e] += As[e][thread_row][k] * Bs[k][thread_col];
}
}
__syncthreads();
}
#pragma unroll
for (int e = 0; e < ELEMENTS_PER_THREAD; ++e) {
int row = block_row + e * BLOCK_SIZE + thread_row;
int col = block_col + thread_col;
if (row < M && col < N) {
C[row * ldc + col] = C_values[e];
}
}
}
torch::Tensor matmul_cuda(torch::Tensor A, torch::Tensor B) {
if (!A.is_cuda() || !B.is_cuda()) {
throw std::invalid_argument("Input tensors must be on CUDA devices");
}
int dims[6];
dims[0] = A.size(0); // M
dims[1] = B.size(1); // N
dims[2] = A.size(1); // K
dims[3] = A.stride(0); // lda
dims[4] = B.stride(0); // ldb
dims[5] = B.size(1); // ldc
bool trans[2] = {false, false};
// Copy configuration to constant memory
cudaMemcpyToSymbol(const_dims, dims, sizeof(dims));
cudaMemcpyToSymbol(const_trans, trans, sizeof(trans));
auto C = torch::empty({dims[0], dims[1]}, A.options());
dim3 blockDim(BLOCK_SIZE, BLOCK_SIZE);
dim3 gridDim((dims[1] + BLOCK_SIZE - 1) / BLOCK_SIZE,
(dims[0] + (BLOCK_SIZE * ELEMENTS_PER_THREAD) - 1) / (BLOCK_SIZE * ELEMENTS_PER_THREAD));
matmul_kernel_unrolled<<<gridDim, blockDim>>>(
A.data_ptr<float>(),
B.data_ptr<float>(),
C.data_ptr<float>());
return C;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &matmul_cuda, "Matrix multiplication with loop unrolling (CUDA)");
}
Metric | Value | Unit | Variance | Samples |
---|---|---|---|---|
Executed Ipc Active | 3.020 | inst/cycle | 0.000 | 5 |
Executed Ipc Elapsed | 3.010 | inst/cycle | 0.000 | 5 |
Issue Slots Busy | 75.526 | % | 0.002 | 5 |
Issued Ipc Active | 3.020 | inst/cycle | 0.000 | 5 |
SM Busy | 75.526 | % | 0.002 | 5 |
Memory Throughput | 1358857757837.058 | byte/second | 556563172630536576.000 | 5 |
Mem Busy | 94.194 | % | 0.001 | 5 |
Max Bandwidth | 70.818 | % | 0.001 | 5 |
L1/TEX Hit Rate | 38.480 | % | 0.000 | 5 |
L2 Hit Rate | 99.064 | % | 0.000 | 5 |
Mem Pipes Busy | 61.802 | % | 0.001 | 5 |
Warp Cycles Per Issued Instruction | 14.704 | cycle | 0.000 | 5 |
Warp Cycles Per Executed Instruction | 14.704 | cycle | 0.000 | 5 |
Avg. Active Threads Per Warp | 32.000 | 0.000 | 5 | |
Avg. Not Predicated Off Threads Per Warp | 30.910 | 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 | 6.000 | block | 0.000 | 5 |
Block Limit Shared Mem | 16.000 | block | 0.000 | 5 |
Block Limit Warps | 8.000 | block | 0.000 | 5 |
Theoretical Active Warps per SM | 48.000 | warp | 0.000 | 5 |
Theoretical Occupancy | 75.000 | % | 0.000 | 5 |
Achieved Occupancy | 69.792 | % | 0.000 | 5 |
Achieved Active Warps Per SM | 44.664 | warp | 0.000 | 5 |
Rule | Description |
---|---|
INF HighPipeUtilization | ALU is the highest-utilized pipeline (34.2%) 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 (75.0%) is limited by the number of required registers. 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 | 227313.71 | μs |
Device Time | 79.84 | μs |
Self CPU Time | 44.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::_to_copy | ||
CPU Time | 227269.54 | μs |
Device Time | 79.84 | μs |
Self CPU Time | 125.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::empty_strided | ||
CPU Time | 226652.30 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 114.02 | μ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 |
cudaDeviceGetStreamPriorityRange | ||
CPU Time | 226348.98 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 226348.98 | μ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 |
cudaMemcpyToSymbol | ||
CPU Time | 3093765.99 | μs |
Device Time | 20574.30 | μs |
Self CPU Time | 3093765.99 | μs |
Self Device Time | 20574.30 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
matmul_kernel_unrolled(float const*, float const*, float*) | ||
CPU Time | 0.00 | μs |
Device Time | 2806469.98 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 2806469.98 | μ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 | 45713.36 | μs |
Device Time | 326142.15 | μs |
Self CPU Time | 7929.20 | μ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 | 37786.04 | μs |
Device Time | 326142.15 | μs |
Self CPU Time | 11343.16 | μs |
Self Device Time | 326142.15 | μ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 | 326221.67 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 326221.67 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
45287 warnings generated when compiling for host. Suppressed 45322 warnings (45275 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.