开发者指南
开发环境配置、代码规范以及贡献指南。
目录
开发环境
前置要求
| 工具 | 最低版本 | 推荐版本 |
|---|---|---|
| CUDA Toolkit | 11.0 | 12.0+ |
| CMake | 3.18 | 3.25+ |
| GCC | 9.4 | 11+ |
| Clang | 10 | 14+ |
| Python | 3.8 | 3.10+ |
IDE 配置
VS Code
推荐扩展:
ms-vscode.cpptools— C/C++ 扩展llvm-vs-code-extensions.vscode-clangd— Clangd 语言服务器NVIDIA.nsight-vscode-edition— CUDA 支持
class="highlight">
1
2
3
4
5
6
7
// .vscode/settings.json
{
"cmake.configureSettings": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}
CLion
class="highlight">1
2
Settings → Build, Execution, Deployment → Toolchains
→ Add → CUDA (设置 CUDA 路径)
构建系统
Debug 构建
class="highlight">1
2
3
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON
make -j$(nproc)
Release 构建(带调试信息)
class="highlight">1
2
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
make -j$(nproc)
Sanitizer 构建
class="highlight">1
2
3
cmake .. -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-fsanitize=address,undefined"
make -j$(nproc)
交叉编译
class="highlight">1
2
3
4
5
# 指定架构
cmake .. -DCUDA_ARCH="75;80;86"
# 所有常见架构
cmake .. -DCUDA_ARCH="70;75;80;86;89;90"
测试
运行所有测试
class="highlight">1
ctest --output-on-failure
运行特定测试
class="highlight">1
./tests/tiny_llm_test --gtest_filter="W8A16MatmulTest.*"
调试 CUDA Kernel
class="highlight">1
2
3
4
5
6
7
8
# 启用 CUDA launch blocking
CUDA_LAUNCH_BLOCKING=1 ./tests/tiny_llm_test
# CUDA memcheck
cuda-memcheck ./tests/tiny_llm_test
# Compute sanitizer (CUDA 11.1+)
compute-sanitizer --tool memcheck ./tests/tiny_llm_test
性能分析
class="highlight">1
2
3
4
5
# Nsight Compute
ncu -o profile.ncu-rep ./test_kernel
# Nsight Systems
nsys profile -o profile.qdrep ./test_app
代码规范
C++ 代码规范
我们遵循 Google C++ 代码规范并做适当修改:
规则 约定 命名 类用 CamelCase,函数/变量用 snake_case 成员 私有成员尾部下划线: member_ 常量 kCamelCase 用于常量,SCREAMING_SNAKE 用于宏 缩进 4 空格(不使用 Tab) 行长度 100 字符
示例
class="highlight">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class MyClass {
public:
explicit MyClass(int size);
void doSomething(const std::string& input);
int getSize() const { return size_; }
private:
int size_;
std::vector<float> data_;
};
namespace tiny_llm {
constexpr int kDefaultGroupSize = 128;
Result<float> computeValue(int input) {
if (input < 0) {
return Result<float>::err("Negative input");
}
return Result<float>::ok(std::sqrt(input));
}
} // namespace tiny_llm
代码格式化
使用项目提供的 .clang-format 文件:
class="highlight">1
2
3
4
5
6
# 格式化单个文件
clang-format -i src/myfile.cpp
# 格式化所有源文件
find src tests kernels -name "*.cpp" -o -name "*.h" -o -name "*.cu" -o -name "*.cuh" \
| xargs clang-format -i
贡献指南
工作流程
- Fork & Clone class="highlight">
1
2
git clone https://github.com/your-username/tiny-llm.git
cd tiny-llm
- 创建分支 class="highlight">
1
git checkout -b feature/my-feature
- 进行修改
- 编写代码
- 添加测试
- 更新文档
- 提交 class="highlight">
1
git commit -m "feat: add new feature"
- 推送并创建 PR class="highlight">
1
git push origin feature/my-feature
然后在 GitHub 上创建 Pull Request。
Commit 消息格式
遵循 Conventional Commits 规范:
class="highlight">1
2
3
4
5
<type>(<scope>): <description>
[optional body]
[optional footer]
类型:
feat: 新功能 fix: Bug 修复 docs: 文档变更 style: 代码格式调整 refactor: 代码重构 perf: 性能优化 test: 测试相关 ci: CI/CD 变更 chore: 维护任务
示例:
class="highlight">