CUDA GEMM 优化教程与迷你推理引擎
从朴素矩阵乘法到 ~85% cuBLAS 性能的 7 级渐进式优化路线
| 技术领域 | 本项目覆盖 | 代码证据 |
|---|---|---|
| CUDA 编程 | 7 级 GEMM 优化(Naive → Vectorized) | src/*_gemm.cu |
| 内存优化 | 共享内存分块、双缓冲、寄存器分块 | tiled_gemm.cu, double_buffer_gemm.cu |
| 性能调优 | AutoTuner 自动调参、Profiler 性能分析 | include/autotuner.h, include/profiler.h |
| 系统设计 | 四层架构、RAII 资源管理、内存池 | include/memory_pool.h, include/stream_manager.h |
| 工程实践 | CMake、GoogleTest、CI/CD、OpenSpec | CMakePresets.json, .github/workflows/ |
Performance vs cuBLAS (RTX 3080, 1024×1024)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
L1 Naive ████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10%
L2 Tiled ████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 20%
L3 Coalesced ██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 25%
L4 Double Buf ████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 40%
L5 Register ████████████████████████████████████████████░ 85%
L6 Fused ██████████████████████████████████████████░░░ 80%
L7 Vectorized █████████████████████████████████████████████ 89%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
性能数字基于 RTX 3080、1024×1024 矩阵场景。本项目采用保守口径,用于教学对比而非跨硬件承诺。
| 项目 | 定位 | 本项目差异 |
|---|---|---|
| cuBLAS | NVIDIA 官方 BLAS 库 | 本项目是教学版,逐级展示优化过程 |
| CUTLASS | CUDA 模板库 | 本项目更简单,适合入门学习 |
| llama.cpp | LLM 推理框架 | 本项目聚焦 GEMM 优化教学 |
| vLLM | LLM 服务框架 | 本项目是底层 kernel 教学 |
推荐学习路径:
本项目 (GEMM 基础) → CUTLASS (进阶) → cuBLAS (生产)
要求: CUDA Toolkit 12.x、CMake 3.18+、C++17 编译器、NVIDIA GPU (SM 7.0+)
git clone https://github.com/LessUp/mini-inference-engine.git
cd mini-inference-engine
# Debug 构建 + 测试
cmake --preset default
cmake --build --preset default
ctest --preset default --output-on-failure
# Release 构建 + Benchmark
cmake --preset release
cmake --build --preset release
./build-release/benchmark┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
│ Benchmark / MNIST Demo / Tests │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Engine Layer │
│ InferenceEngine / Tensor / AutoTuner / Profiler │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Kernel Layer │
│ 7-Level GEMM / FP16 GEMM / Batch GEMM / cuBLAS │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Infrastructure Layer │
│ MemoryPool / StreamManager / Logger / Config / Quantization│
└─────────────────────────────────────────────────────────────┘
| 主题 | 中文 | English |
|---|---|---|
| 快速开始 | 快速入门 | Quick Start |
| 架构设计 | 架构 | Architecture |
| GEMM 优化 | 优化详解 | Optimization |
| 性能调优 | 调优指南 | Tuning |
| API 参考 | API | API |
| 学习路径 | 学习计划 | Learning Path |
| FAQ | 常见问题 | FAQ |
本项目的优化技术来自以下经典论文:
@article{volkov2009better,
title={Better performance at lower occupancy},
author={Volkov, Vasily},
journal={GTC},
year={2009}
}
@inproceedings{hong2009analytic,
title={An analytical model for the GPU architecture},
author={Hong, Sunpyo and Kim, Hyesoon},
booktitle={ISPASS},
year={2009}
}- 规格驱动:
openspec/specs/**作为唯一事实源 - 构建系统:显式源列表,禁止 globbing
- 代码风格:
.clang-format(Google-based, 4-space) - 测试分类:
tests_host(无 GPU) +tests_gpu(需要 GPU) - 分支策略:
master唯一长期分支
详见 AGENTS.md。
MIT © 2024-present