cutlass::gemm::device:: Gemm 模板·

1
2
3
4
5
6
7
8
9
10
using RowMajor = cutlass::layout::RowMajor;

using Gemm = cutlass::gemm::device::Gemm<
float, RowMajor, // A:数据类型、布局
float, RowMajor, // B:数据类型、布局
float, RowMajor, // C/D:数据类型、布局
float, // 累加及计算类型
cutlass::arch::OpClassSimt, // 运算类别:SIMT
cutlass::arch::Sm70 // 架构标签
>;

CUTLASS GEMM 的通用模板形式

1
2
3
4
5
6
7
8
9
10
11
cutlass::gemm::device::Gemm<
ElementA,
LayoutA,
ElementB,
LayoutB,
ElementC,
LayoutC,
ElementAccumulator,
OperatorClass,
ArchTag
>
模板参数 当前代码中的值 含义
ElementA float 矩阵 A 的元素类型
LayoutA RowMajor A 的存储布局
ElementB float 矩阵 B 的元素类型
LayoutB RowMajor B 的存储布局
ElementC float 矩阵 C、D 的元素类型
LayoutC RowMajor C、D 的存储布局
ElementAccumulator float 累加器类型
OperatorClass OpClassSimt 使用 CUDA Core 的 SIMT 运算
ArchTag Sm70 CUTLASS 内核架构标签

参数构造、初始化及执行代码

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
26
27
28
29
30
// 1. 定义 GEMM 参数
Gemm::Arguments arguments{
{M, N, K},
{dA.ptr, K},
{dB.ptr, N},
{dC.ptr, N},
{dD.ptr, N},
{alpha, beta}
};

// 2. 检查参数是否满足内核要求
CUTLASS_CHECK(Gemm::can_implement(arguments));

// 3. 分配工作空间
std::size_t workspace_bytes =
Gemm::get_workspace_size(arguments);

DeviceBuffer<unsigned char> workspace(workspace_bytes);

// 4. 创建 GEMM 对象
Gemm gemm;

// 5. 初始化
CUTLASS_CHECK(gemm.initialize(arguments, workspace.ptr));

// 6. 执行
CUTLASS_CHECK(gemm.run());

// 7. 等待 GPU 完成
CUDA_CHECK(cudaDeviceSynchronize());

对应的数学运算是:

DM×N=αAM×KBK×N+βCM×ND_{M\times N}=\alpha A_{M\times K}B_{K\times N}+\beta C_{M\times N}

当前模板使用 OpClassSimt,因此走的是 SIMT FP32 路径。虽然编译命令使用 -arch=sm_89,面向 RTX 4060 Mobile,但不会因此自动启用 Tensor Core。另外,这只是 device::Gemm 的简化模板实例化。CUTLASS 2.x 还支持通过后续模板参数配置 Threadblock Shape、Warp Shape、Instruction Shape、Epilogue 等,这些在原代码中均使用默认值。