MUTLASS 代码使用指南
目录
概述
MUTLASS (MUSA Templates for Linear Algebra Subroutines) 是一个用于在 MUSA 架构上实现高性能矩阵乘法运算的 C++ 模板库。本文档详细介绍如何在代码中使用 MUTLASS。
- 纯头文件库:MUTLASS 是 header-only 库,只需包含头文件即可使用
- 模板元编程:使用 C++ 模板实现零开销抽象
- 分层设计:从底层原子操作到高级 API 的多层抽象
- 架构优化:针对 MUSA 架构(Quyuan/MP22, MP31)优化
环境配置
1. 包含路径设置
需要将 MUTLASS 的 include/ 目录添加到包含路径:
# CMakeLists.txt
target_include_directories(target PRIVATE
${MUTLASS_DIR}/include
)
或者使用编译器选项:
mcc -I/path/to/mutlass/include your_code.mu
2. 编译要求
- C++ 标准:C++17 或更高
- 编译器:MCC 4.3.4+
- 文件扩展名:
.mu文件自动启用 MUSA 扩展,.cpp/.cc文件需要-x musa选项
# .mu 文件
mcc code.mu -std=c++17
# .cpp 文件
mcc -x musa code.cpp -std=c++17
3. 链接库
如果使用 MUTLASS Library(预编译的 kernel 实例),需要链接:
target_link_libraries(target PRIVATE
mutlass_lib
musart
musa_driver
)
完整项目构建与运行
本节详细介绍如何编译和运行 MUTLASS 项目的所有组件,包括 Examples、Tools、Tests 和 Experimental 功能。建议新用户先阅读本节,快速上手项目。
1. 完整项目构建
1.1 清理和准备
# 进入项目目录
cd /home/test/mutlass
# 清理旧的构建(如果存在)
rm -rf build
# 创建新的构建目录
mkdir build && cd build
1.2 CMake 配置
完整配置(推荐用于开发):
S4000将-DMUTLASS_MCC_ARCHS=31设为-DMUTLASS_MCC_ARCHS=22
cmake .. \
-DMUTLASS_MCC_ARCHS=31 \
-DMUTLASS_ENABLE_EXAMPLES=ON \
-DMUTLASS_ENABLE_TESTS=ON \
-DMUTLASS_ENABLE_PROFILER=ON \
-DMUTLASS_ENABLE_LIBRARY=ON \
-DMUTLASS_ENABLE_EXPERIMENTAL=ON
警告修复:(可忽略)将cmake/googletest.cmake中line48-54替换为
if(NOT googletest_POPULATED)
if (MSVC)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
FetchContent_MakeAvailable(googletest)
endif()
最小配置(快速验证):
cmake .. \
-DMUTLASS_MCC_ARCHS=31 \
-DMUTLASS_ENABLE_EXAMPLES=ON \
-DMUTLASS_ENABLE_PROFILER=ON
多架构配置:
cmake .. \
-DMUTLASS_MCC_ARCHS="22;31" \
-DMUTLASS_ENABLE_EXAMPLES=ON \
-DMUTLASS_ENABLE_PROFILER=ON
1.3 编译组件
1.3.1整体编译
# 编译所有目标
make -j$(nproc)
# 使用较少并行数避免依赖问题
make -j4
1.3.2分步骤编译(推荐)
make mutlass_library_objs -j1
make mutlass_library -j1 # Library(会自动编译 mutlass_library_objs)
make mutlass_library_static -j$(nproc) # 编译静态库
make mutlass_examples -j$(nproc) # Examples
make mutlass_profiler -j$(nproc) # Profiler,耗时较长
make mutlass_test_unit -j$(nproc) # Tests
make mutlass_experimental -j$(nproc) # Experimental
说明:
make mutlass_library会自动构建大量子目标,包括:- 各种架构的 GEMM kernel(如
mutlass_library_gemm_mp22_sgemm_simt、mutlass_library_gemm_mp31_void_s128x64x64gemm_f16_tensorop等) - 各种数据类型组合(sgemm, f16, e4m3, e5m2 等)
- 各种 kernel 类型(simt, tensorop 等)
- 每个 kernel 都有对应的
*_objs(对象文件)、共享库(.so)和静态库(.a)
- 各种架构的 GEMM kernel(如
此时核心组件编译完成
2. 设置运行环境
运行 Profiler 或依赖 Library 的可执行程序前,需设置动态库路径:
# 在 build 目录下设置库路径(必需)
export LD_LIBRARY_PATH=$PWD/tools/library:$LD_LIBRARY_PATH
# 或使用绝对路径
export LD_LIBRARY_PATH=/home/test/mutlass/build/tools/library:$LD_LIBRARY_PATH
# 验证
echo $LD_LIBRARY_PATH
说明:Examples(如 00_basic_gemm)不依赖 libmutlass.so,可直接运行;Profiler 必须设置 LD_LIBRARY_PATH 才能找到 libmutlass.so。
3. 运行 Examples(示例代码)
3.1 运行单个示例
# 基础 GEMM 示例
./examples/00_basic_gemm/00_basic_gemm --m=2048 --n=2048 --k=1024
# Quyuan 架构集体构建器示例(需要 MP22 架构)
./examples/01_quyuan_gemm_with_collective_builder/01_collective_builder \
--m=2048 --n=2048 --k=1024 --l=2
# MP31 FP8 GEMM 示例(需要 MP31 架构)
./examples/02_mp31_fp8_gemm_with_collective_builder/02_mp31_fp8_gemm
# MP31 FP8 缩放 GEMM 示例
./examples/03_mp31_fp8_scaling_gemm/03_fp8_scaling_gemm
3.2 运行所有示例
# 运行所有示例测试(会自动编译并运行)
make test_examples
4. 运行 Profiler(性能分析工具)
4.1 编译 Profiler
cd /home/test/mutlass/build
# 编译 Profiler(需要先编译 Library)
make mutlass_profiler -j$(nproc)
4.2 运行 Profiler
基础 GEMM 性能测试:
./tools/profiler/mutlass_profiler \
--operation=Gemm \
--op_class=simt \
--m=4096 \
--n=4096 \
--k=1024 \
--cta_m=128 \
--cta_n=128
TensorOp GEMM 性能测试:
./tools/profiler/mutlass_profiler \
--operation=Gemm \
--op_class=tensorop \
--m=4096 \
--n=4096 \
--k=1024
FP16 GEMM 性能测试:
./tools/profiler/mutlass_profiler \
--operation=Gemm \
--op_class=tensorop \
--m=4096 \
--n=4096 \
--k=1024 \
--A=f16:row \
--B=f16:column \
--C=f16:column \
--D=f16:column
查看帮助信息:
# 通用帮助
./tools/profiler/mutlass_profiler --help
# GEMM 操作帮助
./tools/profiler/mutlass_profiler --operation=Gemm --help
Profiler 输出示例:
=============================
Problem ID: 1
Provider: MUTLASS
OperationKind: gemm
Operation: mutlass_mp22_simt_sgemm_f32_f32_f32_f32_f32_128x128x4_tnn_align2
Status: Success
Verification: ON
Disposition: Passed
reference_device: Passed
muBLAS: Not run
muDNN: Not run
Arguments: --gemm_kind=universal --m=4096 --n=4096 --k=1024 --A=f32:row --B=f32:column --C=f32:column --D=f32:column \
--alpha=1 --beta=0 --batch_count=1 --op_class=simt --accum=f32 --cta_m=128 --cta_n=128 --cta_k=4 --cluster_m=1 \
--cluster_n=1 --cluster_k=1 --stages=2 --inst_m=1 --inst_n=1 --inst_k=1 --min_cc=22 --max_cc=1024
Bytes: 100663296 bytes
FLOPs: 34393292800 flops
FLOPs/Byte: 341
Runtime: 2.92589 ms
Memory: 32.0416 GiB/s
Math: 11754.8 GFLOP/s
5. 运行单元测试(Unit Tests)
5.1 编译单元测试
cd /home/test/mutlass/build
# 编译所有单元测试
make test_unit -j$(nproc)
# 或编译特定测试
make test_unit_mute_core
make test_unit_gemm
5.2 运行单元测试
# 运行所有单元测试(会自动编译并运行)
make test_unit
# 运行特定测试组
make test_unit_mute_core
make test_unit_gemm_device
预期输出示例:
[==========] Running 33 tests from 1 test suite.
[----------] Global test environment set-up.
[ RUN ] MuTe_core.Tuple
[ OK ] MuTe_core.Tuple (0 ms)
...
[----------] 33 tests from MuTe_core (7 ms total)
[==========] 33 tests from 1 test suite ran. (7 ms total)
[ PASSED ] 33 tests.
6. 运行 Experimental(实验性功能)
6.1 编译 Experimental
cd /home/test/mutlass/build
# 编译所有实验性功能
make mutlass_experimental -j$(nproc)
包含的实验性功能:
mp31_fmha_fwd: MP31 架构的 Fused Multi-Head Attention (FMHA)mp31_paged_fmha_fwd: MP31 架构的 Paged FMHAmp31_mla_decode: MP31 架构的 Multi-Layer Attention (MLA)
6.2 运行 FMHA 测试
# 运行所有 FMHA 测试
make test_examples_mp31_fmha_fwd
# 运行特定测试用例
make test_examples_mp31_fmha_fwd_test_causal_00_128_128
make test_examples_mp31_fmha_fwd_test_non_causal_00_128_128
make test_examples_mp31_fmha_fwd_test_varlen_causal_00_128_128
6.3 运行 Paged FMHA 测试
# 运行所有 Paged FMHA 测试
make test_examples_mp31_paged_fmha_fwd
# 运行特定测试用例
make test_examples_mp31_paged_fmha_fwd_test_causal_00_128_128
make test_examples_mp31_paged_fmha_fwd_test_non_causal_00_128_128
6.4 运行 MLA 测试
# 运行 MLA 测试
make test_examples_mp31_mla_decode
注意:实验性功能可能不稳定,API 可能会变化。
7. 运行所有测试(一键测试)
cd /home/test/mutlass/build
# 运行所有测试(包括 examples, tests, experimental)
make test_all
这会依次运行:
test_examples- 所有示例测试test_unit- 所有单元测试test_experimental- 所有实验性功能测试
8. 验证构建完整性
8.1 检查编译目标
cd /home/test/mutlass/build
# 查看所有可用目标
make help | grep -E '^\.\.\. (test_|mutlass_)' | head -50
# 统计目标数量
make help | grep -E '^\.\.\. (test_|mutlass_)' | wc -l
8.2 检查生成的文件
# 检查可执行文件
find examples -type f -executable
find tools -type f -executable
find experimental -type f -executable
# 检查库文件
ls -lh tools/library/libmutlass*.so | head -10
# 检查库文件数量
ls tools/library/libmutlass*.so | wc -l
8.3 验证库依赖
# 检查 profiler 的依赖
ldd tools/profiler/mutlass_profiler | grep mutlass
# 检查示例的依赖
ldd examples/00_basic_gemm/00_basic_gemm | grep musa