开发者
资源
文档评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

tensor_tiled_mma

矩阵乘法累加接口,计算A*B+C。TiledMma策略中设定的计算尺寸必须与矩阵D/A/B/C形状一致。

接口定义

template<typename TiledMma,

typename dtypeD, typename LayoutD,

typename dtypeA, typename LayoutA,

typename dtypeB, typename LayoutB,

typename dtypeC, typename LayoutC>

void tensor_tiled_mma(TiledMma mma, Tensor<dtypeD, LayoutD> D, Tensor<dtypeA, LayoutA> A, Tensor<dtypeB, LayoutB> B, Tensor<dtypeC, LayoutC> C);

模板参数

表1 模板参数定义

参数名

类型

描述

TiledMma

typename

mma tile策略类型

dtypeD, dtypeA, dtypeB, dtypeC

typename

D,A,B,C精度类型

LayoutD, LayoutA, LayoutB, LayoutC

typename

D,A,B,C布局类型

参数

表2 参数定义

参数名

类型

描述

输入/输出

mma

TiledMma

mma tile策略

输入

D

Tensor<dtypeD, LayoutD>

矩阵对象D

输入

A

Tensor<dtypeA, LayoutA>

矩阵对象A

输入

B

Tensor<dtypeB, LayoutB>

矩阵对象B

输入

C

Tensor<dtypeC, LayoutC>

矩阵对象C

输入

返回值

  • void

示例

 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
31
32
33
34
35
36
37
38
39
40
41
42
#include "stdlib.h"
#include "kupl.h"
using namespace kupl::tensor;

int main()
{
    constexpr int MATRIX_M  = 32;
    constexpr int MATRIX_N  = 16;
    constexpr int MATRIX_K = 512;
    double *data_a = (double *)malloc(sizeof(double) * MATRIX_M * MATRIX_K);
    double *data_b = (double *)malloc(sizeof(double) * MATRIX_K * MATRIX_N);
    double *data_c = (double *)malloc(sizeof(double) * MATRIX_M * MATRIX_N);

    auto shape_a = make_shape(Int<32>{}, Int<512>{});
    auto shape_b = make_shape(Int<512>{}, Int<16>{});
    auto shape_c = make_shape(Int<32>{}, Int<16>{});

    auto stride_a = make_stride(Int<1>{}, Int<32>{});
    auto stride_b = make_stride(Int<16>{}, Int<1>{});
    auto stride_c = make_stride(Int<16>{}, Int<1>{});

    auto layout_a = make_layout(shape_a, stride_a);
    auto layout_b = make_layout(shape_b, stride_b);
    auto layout_c = make_layout(shape_c, stride_c);

    auto mma_atom_shape = make_shape(Int<1>{}, Int<1>{}, Int<1>{});
    auto tiled_mma = make_tiled_mma(Ops<MMA_32x16x512_F64F64F64>{}, mma_atom_shape);
    auto store_atom_shape = make_shape(Int<1>{}, Int<1>{});
    auto tile_store = make_tiled_store(Ops<STORE_32x16_F64>{}, store_atom_shape);

    auto tensor_a = make_tensor(data_a, layout_a);
    auto tensor_b = make_tensor(data_b, layout_b);
    auto tensor_c = make_tensor(data_c, layout_c);

    tensor_tiled_mma(tiled_mma, tensor_c, tensor_a, tensor_b, tensor_c);
    tensor_tiled_store(tile_store, tensor_c);

    free(data_a);
    free(data_b);
    free(data_c);
    return 0;
}

上述示例演示了mma流程,tensor_tiled_mma接收Tensor对象进行矩阵乘法计算。