Convolution⚓︎
类型定位⚓︎
卷积或 stencil 类 kernel 的关键特征是:相邻输出会重叠访问输入窗口,因此不同线程之间天然共享大量输入数据。这使得 shared memory staging 往往比在 elementwise 模式中的收益更明显。
优化方法⚓︎
- Basic 版本让每个线程负责一个输出元素,并直接从 global memory 读取整个窗口。
- 将 block 覆盖的输入窗口搬入 shared memory,减少相邻输出的重复读取。
- 将卷积核系数放入 constant memory,使同一时刻所有线程读取同一 tap 时可以利用广播机制。
- 让单线程计算多个相邻输出,提高输入 tile 的复用率。
题目索引⚓︎
convolution_1dconvolution_3d
convolution_1d⚓︎
convolution_1d 对一维输入序列执行 valid convolution。若输入为 x,卷积核为 w,kernel size 为 K,则输出长度为 N - K + 1,每个输出元素为:
y_i = \sum_{j=0}^{K-1} x_{i+j} w_j
这个公式说明相邻输出窗口会共享大部分输入元素。例如 y_i 和 y_{i+1} 的输入窗口只相差左右边界各一个元素。Basic 版本直接重复读取窗口,Advanced 版本用 shared memory 保存输入 tile,并把卷积核系数放入 constant memory。
convolution_1d 实现
convolution_1d_basic
#include <cuda_runtime.h>
constexpr int kBlockSize = 256;
constexpr int kThreadOutputs = 4;
constexpr int kMaxKernelSize = 4096;
__constant__ float c_kernel[kMaxKernelSize];
__global__ void convolution_1d_basic_kernel(const float *input,
const float *kernel,
float *output,
int input_size,
int kernel_size) {
int output_idx = blockIdx.x * blockDim.x + threadIdx.x;
int output_size = input_size - kernel_size + 1;
if (output_idx < output_size) {
float sum = 0.0f;
for (int j = 0; j < kernel_size; ++j) {
sum += input[output_idx + j] * kernel[j];
}
output[output_idx] = sum;
}
}
void launch_convolution_1d_basic(const float *input,
const float *kernel,
float *output,
int input_size,
int kernel_size) {
if (kernel_size <= 0 || kernel_size > input_size) {
return;
}
int output_size = input_size - kernel_size + 1;
int blocks = (output_size + kBlockSize - 1) / kBlockSize;
convolution_1d_basic_kernel<<<blocks, kBlockSize>>>(input, kernel, output, input_size, kernel_size);
}
convolution_1d_advanced
#include <cuda_runtime.h>
constexpr int kBlockSize = 256;
constexpr int kThreadOutputs = 4;
constexpr int kMaxKernelSize = 4096;
__constant__ float c_kernel[kMaxKernelSize];
__global__ void convolution_1d_advanced_kernel(const float *input,
float *output,
int input_size,
int kernel_size) {
int output_size = input_size - kernel_size + 1;
int outputs_per_block = blockDim.x * kThreadOutputs;
int block_output_start = blockIdx.x * outputs_per_block;
extern __shared__ float shared_input[];
// 相邻输出共享大部分输入窗口,先把整块输入窗口搬入 shared memory 减少重复 global load。
int tile_input_size = min(outputs_per_block + kernel_size - 1, input_size - block_output_start);
for (int t = threadIdx.x; t < tile_input_size; t += blockDim.x) {
shared_input[t] = input[block_output_start + t];
}
__syncthreads();
float acc[kThreadOutputs] = {0.0f, 0.0f, 0.0f, 0.0f};
for (int j = 0; j < kernel_size; ++j) {
float k = c_kernel[j];
#pragma unroll
for (int o = 0; o < kThreadOutputs; ++o) {
// 让同一轮 o 内的连续线程写连续输出,避免 thread coarsening 破坏合并访存。
int out_idx = block_output_start + o * blockDim.x + threadIdx.x;
if (out_idx < output_size) {
acc[o] += shared_input[o * blockDim.x + threadIdx.x + j] * k;
}
}
}
#pragma unroll
for (int o = 0; o < kThreadOutputs; ++o) {
int out_idx = block_output_start + o * blockDim.x + threadIdx.x;
if (out_idx < output_size) {
output[out_idx] = acc[o];
}
}
}
void launch_convolution_1d_advanced(const float *input,
const float *kernel,
float *output,
int input_size,
int kernel_size) {
if (kernel_size <= 0 || kernel_size > input_size || kernel_size > kMaxKernelSize) {
return;
}
int output_size = input_size - kernel_size + 1;
int outputs_per_block = kBlockSize * kThreadOutputs;
int blocks = (output_size + outputs_per_block - 1) / outputs_per_block;
cudaMemcpyToSymbol(c_kernel, kernel, kernel_size * sizeof(float), 0, cudaMemcpyHostToDevice);
// shared tile 覆盖本 block 的输出窗口以及卷积右侧 halo。
size_t shared_bytes = static_cast<size_t>(outputs_per_block + kernel_size - 1) * sizeof(float);
convolution_1d_advanced_kernel<<<blocks, kBlockSize, shared_bytes>>>(input, output, input_size, kernel_size);
}
convolution_3d⚓︎
convolution_3d 操作涉及对三个维度 (x, y, z) 同时应用空间卷积:
Y_{x,y,z} = \sum_{d} \sum_{r} \sum_{c} X_{x+c, y+r, z+d} \cdot W_{d, r, c}
convolution_3d 实现
convolution_3d_basic
#include <cuda_runtime.h>
__global__ void convolution_3d_basic_kernel(const float *input,
float *output,
const float *filter,
int width,
int height,
int depth,
int filter_size) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int z = blockIdx.z * blockDim.z + threadIdx.z;
if (x < width && y < height && z < depth) {
float sum = 0.0f;
int filter_half = filter_size / 2;
int filter_area = filter_size * filter_size;
for (int d = -filter_half; d <= filter_half; ++d) {
for (int r = -filter_half; r <= filter_half; ++r) {
for (int c = -filter_half; c <= filter_half; ++c) {
int cur_z = z + d;
int cur_y = y + r;
int cur_x = x + c;
if (cur_z >= 0 && cur_z < depth && cur_y >= 0 && cur_y < height && cur_x >= 0 && cur_x < width) {
int input_idx = cur_z * width * height + cur_y * width + cur_x;
int filter_z = d + filter_half;
int filter_y = r + filter_half;
int filter_x = c + filter_half;
int filter_idx = filter_z * filter_area + filter_y * filter_size + filter_x;
sum += input[input_idx] * filter[filter_idx];
}
}
}
}
output[z * width * height + y * width + x] = sum;
}
}
void launch_convolution_3d_basic(const float *input,
float *output,
const float *filter,
int width,
int height,
int depth,
int filter_size) {
if (width <= 0 || height <= 0 || depth <= 0 || filter_size <= 0 || filter_size % 2 == 0) {
return;
}
dim3 threads(8, 8, 4);
dim3 blocks((width + threads.x - 1) / threads.x,
(height + threads.y - 1) / threads.y,
(depth + threads.z - 1) / threads.z);
convolution_3d_basic_kernel<<<blocks, threads>>>(input, output, filter, width, height, depth, filter_size);
}
边界与局限⚓︎
当 kernel size 很大时,constant memory 不一定适合;当输入窗口与输出 tile 的关系复杂时,shared memory 大小和 occupancy 会形成新的约束。