|
5 | 5 | <h1 align="center">Mini-Inference Engine</h1> |
6 | 6 |
|
7 | 7 | <p align="center"> |
8 | | - <b>CUDA GEMM optimization tutorial and mini inference engine</b><br> |
9 | | - <i>From naive matrix multiplication to ~85% cuBLAS-class performance on the reference benchmark</i> |
| 8 | + <b>CUDA GEMM 优化教程与迷你推理引擎</b><br> |
| 9 | + <i>从朴素矩阵乘法到 ~85% cuBLAS 性能的 7 级渐进式优化路线</i> |
10 | 10 | </p> |
11 | 11 |
|
12 | 12 | <p align="center"> |
13 | | - <a href="README.md">English</a> · |
14 | 13 | <a href="README.zh-CN.md">简体中文</a> · |
15 | | - <a href="https://lessup.github.io/mini-inference-engine/">Online Docs</a> · |
16 | | - <a href="docs/en/QUICK_START.md">Quick Start</a> |
| 14 | + <a href="README.md">English</a> · |
| 15 | + <a href="https://lessup.github.io/mini-inference-engine/">在线文档</a> |
17 | 16 | </p> |
18 | 17 |
|
19 | 18 | <p align="center"> |
|
23 | 22 | <a href="https://github.com/LessUp/mini-inference-engine/actions/workflows/pages.yml"> |
24 | 23 | <img src="https://github.com/LessUp/mini-inference-engine/actions/workflows/pages.yml/badge.svg" alt="Docs"> |
25 | 24 | </a> |
26 | | - <a href="LICENSE"> |
27 | | - <img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"> |
28 | | - </a> |
29 | | - <img src="https://img.shields.io/badge/CUDA-11.0+-76B900?logo=nvidia&logoColor=white" alt="CUDA"> |
| 25 | + <img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License"> |
| 26 | + <img src="https://img.shields.io/badge/CUDA-12.x-76B900?logo=nvidia&logoColor=white" alt="CUDA"> |
30 | 27 | <img src="https://img.shields.io/badge/C%2B%2B-17-00599C?logo=c%2B%2B&logoColor=white" alt="C++17"> |
| 28 | + <img src="https://img.shields.io/badge/Docs-VitePress-16b1ff" alt="VitePress"> |
31 | 29 | </p> |
32 | 30 |
|
33 | 31 | --- |
34 | 32 |
|
35 | | -## What this repository contains |
| 33 | +## TL;DR — 面试官速览 |
| 34 | + |
| 35 | +| 技术领域 | 本项目覆盖 | 代码证据 | |
| 36 | +|----------|-----------|----------| |
| 37 | +| **CUDA 编程** | 7 级 GEMM 优化(Naive → Vectorized) | `src/*_gemm.cu` | |
| 38 | +| **内存优化** | 共享内存分块、双缓冲、寄存器分块 | `tiled_gemm.cu`, `double_buffer_gemm.cu` | |
| 39 | +| **性能调优** | AutoTuner 自动调参、Profiler 性能分析 | `include/autotuner.h`, `include/profiler.h` | |
| 40 | +| **系统设计** | 四层架构、RAII 资源管理、内存池 | `include/memory_pool.h`, `include/stream_manager.h` | |
| 41 | +| **工程实践** | CMake、GoogleTest、CI/CD、OpenSpec | `CMakePresets.json`, `.github/workflows/` | |
36 | 42 |
|
37 | | -Mini-Inference Engine is a compact CUDA/C++17 project for learning high-performance GEMM optimization in a realistic inference-engine setting. It keeps the scope intentionally small: matrix multiplication kernels, runtime utilities, benchmarks, tests, and bilingual documentation all live in one traceable codebase. |
| 43 | +--- |
38 | 44 |
|
39 | | -**Core value:** |
| 45 | +## 性能一览 |
40 | 46 |
|
41 | | -| Area | What to inspect | |
42 | | -| --- | --- | |
43 | | -| GEMM kernels | `src/naive_matmul.cu` through `src/vectorized_gemm.cu` show the optimization path. | |
44 | | -| Runtime components | `include/tensor.h`, `include/inference_engine.h`, `include/memory_pool.h`, and `include/stream_manager.h`. | |
45 | | -| Benchmarks | `benchmarks/benchmark.cpp`, `benchmarks/detailed_benchmark.cu`, and `benchmarks/mnist_demo.cpp`. | |
46 | | -| Specs | `openspec/specs/` defines requirements, architecture, API, data, and testing expectations. | |
47 | | -| Documentation | `docs/en/` and `docs/zh/` provide the tutorial, architecture, API, and tuning guides. | |
| 47 | +``` |
| 48 | +Performance vs cuBLAS (RTX 3080, 1024×1024) |
| 49 | +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 50 | +L1 Naive ████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10% |
| 51 | +L2 Tiled ████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 20% |
| 52 | +L3 Coalesced ██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 25% |
| 53 | +L4 Double Buf ████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 40% |
| 54 | +L5 Register ████████████████████████████████████████████░ 85% |
| 55 | +L6 Fused ██████████████████████████████████████████░░░ 80% |
| 56 | +L7 Vectorized █████████████████████████████████████████████ 89% |
| 57 | +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 58 | +``` |
48 | 59 |
|
49 | | -The headline performance number is hardware-specific. The project uses a conservative reference claim: the best optimized kernel reaches about **85% of cuBLAS-class throughput** on the documented RTX 3080 1024×1024 benchmark. |
| 60 | +> 性能数字基于 RTX 3080、1024×1024 矩阵场景。本项目采用保守口径,用于教学对比而非跨硬件承诺。 |
50 | 61 |
|
51 | 62 | --- |
52 | 63 |
|
53 | | -## Quick start |
| 64 | +## 与业界项目对比 |
54 | 65 |
|
55 | | -Requirements: CUDA Toolkit 11.0+, CMake 3.18+, a C++17 compiler, and an NVIDIA GPU with compute capability 7.0+. |
| 66 | +| 项目 | 定位 | 本项目差异 | |
| 67 | +|------|------|-----------| |
| 68 | +| **cuBLAS** | NVIDIA 官方 BLAS 库 | 本项目是教学版,逐级展示优化过程 | |
| 69 | +| **CUTLASS** | CUDA 模板库 | 本项目更简单,适合入门学习 | |
| 70 | +| **llama.cpp** | LLM 推理框架 | 本项目聚焦 GEMM 优化教学 | |
| 71 | +| **vLLM** | LLM 服务框架 | 本项目是底层 kernel 教学 | |
| 72 | + |
| 73 | +**推荐学习路径:** |
| 74 | +``` |
| 75 | +本项目 (GEMM 基础) → CUTLASS (进阶) → cuBLAS (生产) |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## 快速开始 |
| 81 | + |
| 82 | +**要求:** CUDA Toolkit 12.x、CMake 3.18+、C++17 编译器、NVIDIA GPU (SM 7.0+) |
56 | 83 |
|
57 | 84 | ```bash |
58 | 85 | git clone https://github.com/LessUp/mini-inference-engine.git |
59 | 86 | cd mini-inference-engine |
60 | 87 |
|
| 88 | +# Debug 构建 + 测试 |
61 | 89 | cmake --preset default |
62 | 90 | cmake --build --preset default |
63 | 91 | ctest --preset default --output-on-failure |
64 | 92 |
|
| 93 | +# Release 构建 + Benchmark |
65 | 94 | cmake --preset release |
66 | 95 | cmake --build --preset release |
67 | 96 | ./build-release/benchmark |
68 | 97 | ``` |
69 | 98 |
|
70 | | -GPU tests skip when no CUDA device is available, but building still requires a CUDA toolkit because the library is compiled as a CUDA project. |
| 99 | +--- |
| 100 | + |
| 101 | +## 核心架构 |
| 102 | + |
| 103 | +``` |
| 104 | +┌─────────────────────────────────────────────────────────────┐ |
| 105 | +│ Application Layer │ |
| 106 | +│ Benchmark / MNIST Demo / Tests │ |
| 107 | +└─────────────────────────────────────────────────────────────┘ |
| 108 | + │ |
| 109 | + ▼ |
| 110 | +┌─────────────────────────────────────────────────────────────┐ |
| 111 | +│ Engine Layer │ |
| 112 | +│ InferenceEngine / Tensor / AutoTuner / Profiler │ |
| 113 | +└─────────────────────────────────────────────────────────────┘ |
| 114 | + │ |
| 115 | + ▼ |
| 116 | +┌─────────────────────────────────────────────────────────────┐ |
| 117 | +│ Kernel Layer │ |
| 118 | +│ 7-Level GEMM / FP16 GEMM / Batch GEMM / cuBLAS │ |
| 119 | +└─────────────────────────────────────────────────────────────┘ |
| 120 | + │ |
| 121 | + ▼ |
| 122 | +┌─────────────────────────────────────────────────────────────┐ |
| 123 | +│ Infrastructure Layer │ |
| 124 | +│ MemoryPool / StreamManager / Logger / Config / Quantization│ |
| 125 | +└─────────────────────────────────────────────────────────────┘ |
| 126 | +``` |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 文档 |
| 131 | + |
| 132 | +| 主题 | 中文 | English | |
| 133 | +|------|------|---------| |
| 134 | +| 快速开始 | [快速入门](docs/zh/QUICK_START.md) | [Quick Start](docs/en/QUICK_START.md) | |
| 135 | +| 架构设计 | [架构](docs/zh/ARCHITECTURE.md) | [Architecture](docs/en/ARCHITECTURE.md) | |
| 136 | +| GEMM 优化 | [优化详解](docs/zh/GEMM_OPTIMIZATION.md) | [Optimization](docs/en/GEMM_OPTIMIZATION.md) | |
| 137 | +| 性能调优 | [调优指南](docs/zh/PERFORMANCE_TUNING.md) | [Tuning](docs/en/PERFORMANCE_TUNING.md) | |
| 138 | +| API 参考 | [API](docs/zh/API_REFERENCE.md) | [API](docs/en/API_REFERENCE.md) | |
| 139 | +| 学习路径 | [学习计划](docs/zh/guides/learning-path.md) | [Learning Path](docs/en/guides/learning-path.md) | |
| 140 | +| FAQ | [常见问题](docs/zh/guides/faq.md) | [FAQ](docs/en/guides/faq.md) | |
71 | 141 |
|
72 | 142 | --- |
73 | 143 |
|
74 | | -## Documentation map |
| 144 | +## 学术论文引用 |
75 | 145 |
|
76 | | -| Topic | English | 中文 | |
77 | | -| --- | --- | --- | |
78 | | -| Quick Start | [docs/en/QUICK_START.md](docs/en/QUICK_START.md) | [docs/zh/QUICK_START.md](docs/zh/QUICK_START.md) | |
79 | | -| Architecture | [docs/en/ARCHITECTURE.md](docs/en/ARCHITECTURE.md) | [docs/zh/ARCHITECTURE.md](docs/zh/ARCHITECTURE.md) | |
80 | | -| GEMM Optimization | [docs/en/GEMM_OPTIMIZATION.md](docs/en/GEMM_OPTIMIZATION.md) | [docs/zh/GEMM_OPTIMIZATION.md](docs/zh/GEMM_OPTIMIZATION.md) | |
81 | | -| Performance Tuning | [docs/en/PERFORMANCE_TUNING.md](docs/en/PERFORMANCE_TUNING.md) | [docs/zh/PERFORMANCE_TUNING.md](docs/zh/PERFORMANCE_TUNING.md) | |
82 | | -| API Reference | [docs/en/API_REFERENCE.md](docs/en/API_REFERENCE.md) | [docs/zh/API_REFERENCE.md](docs/zh/API_REFERENCE.md) | |
83 | | -| Development Guide | [docs/en/CONTRIBUTING.md](docs/en/CONTRIBUTING.md) | [docs/zh/CONTRIBUTING.md](docs/zh/CONTRIBUTING.md) | |
| 146 | +本项目的优化技术来自以下经典论文: |
| 147 | + |
| 148 | +```bibtex |
| 149 | +@article{volkov2009better, |
| 150 | + title={Better performance at lower occupancy}, |
| 151 | + author={Volkov, Vasily}, |
| 152 | + journal={GTC}, |
| 153 | + year={2009} |
| 154 | +} |
| 155 | +
|
| 156 | +@inproceedings{hong2009analytic, |
| 157 | + title={An analytical model for the GPU architecture}, |
| 158 | + author={Hong, Sunpyo and Kim, Hyesoon}, |
| 159 | + booktitle={ISPASS}, |
| 160 | + year={2009} |
| 161 | +} |
| 162 | +``` |
84 | 163 |
|
85 | 164 | --- |
86 | 165 |
|
87 | | -## Engineering workflow |
| 166 | +## 工程规范 |
| 167 | + |
| 168 | +- **规格驱动**:`openspec/specs/**` 作为唯一事实源 |
| 169 | +- **构建系统**:显式源列表,禁止 globbing |
| 170 | +- **代码风格**:`.clang-format` (Google-based, 4-space) |
| 171 | +- **测试分类**:`tests_host` (无 GPU) + `tests_gpu` (需要 GPU) |
| 172 | +- **分支策略**:`master` 唯一长期分支 |
| 173 | + |
| 174 | +详见 [AGENTS.md](AGENTS.md)。 |
| 175 | + |
| 176 | +--- |
88 | 177 |
|
89 | | -- Source of truth: `openspec/specs/**`. |
90 | | -- Build system: explicit source lists in `CMakeLists.txt`; do not use recursive globbing for source files. |
91 | | -- Formatting: `.clang-format` with Google-based 4-space style. |
92 | | -- Tests: `tests_host` covers utilities that do not require a GPU device; `tests_gpu` covers CUDA runtime/kernel behavior. Configuring and compiling the project still requires a CUDA Toolkit. |
93 | | -- Branching: keep `master` as the only long-lived branch; use short-lived branches/worktrees for changes and delete them after merge. |
| 178 | +## License |
94 | 179 |
|
95 | | -See [AGENTS.md](AGENTS.md) for the full project-specific AI and engineering workflow. |
| 180 | +[MIT](LICENSE) © 2024-present |
0 commit comments