matmul(dst, a, b, m, k, n, init_l1out=True)
参数名称 |
输入/输出 |
含义 |
---|---|---|
dst |
输出 |
矩阵相乘结果操作数的起始element, 支持的数据类型请参考表2,Tensor的scope为L1OUT。 结果张量格式:[N1, M, N0],N = N1 * N0
|
a |
输入 |
源操作数,左矩阵Tensor,支持的数据类型请参考表2, Tensor的scope为L1。 输入张量格式:[K1, M, K0], K = K1 * K0
|
b |
输入 |
源操作数,右矩阵Tensor,支持的数据类型请参考表2, Tensor的scope为L1。 输入张量格式:[K1, N, K0], K = K1 * K0
|
m |
输入 |
左矩阵有效Height,范围:[1, 4096],支持的数据类型为:立即数(int)。 注意:m可以不是16的倍数。 |
k |
输入 |
左矩阵有效Width、右矩阵有效Height,支持的数据类型为:立即数(int)。 当输入张量a的数据类型为float16时,范围:[1, 16384] 当输入张量a的数据类型为int8时,范围:[1, 32768] 注意:k可以不是16的倍数。 |
n |
输入 |
右矩阵有效Width,范围:[1, 4096],支持的数据类型为:立即数(int)。 注意:n可以不是16的倍数。 |
init_l1out |
输入 |
表示dst是否需要初始化。支持的数据类型为:bool类型。默认值为True。
|
Atlas 200/300/500 推理产品
Atlas 训练系列产品
无
from tbe import tik tik_instance = tik.Tik() # 定义tensor a_gm = tik_instance.Tensor("int8", [2, 32, 32], name='a_gm', scope=tik.scope_gm) b_gm = tik_instance.Tensor("int8", [2, 160, 32], name='b_gm', scope=tik.scope_gm) # 由于matmul矩阵计算m=30,fixpipe会将dst_l1out中无效数据删除,因此dst_gm的shape在m方向上设置为30即可 dst_gm = tik_instance.Tensor("int32", [10, 30, 16], name='dst_gm', scope=tik.scope_gm) a_l1 = tik_instance.Tensor("int8", [2, 32, 32], name='a_l1', scope=tik.scope_cbuf) b_l1 = tik_instance.Tensor("int8", [2, 160, 32], name='b_l1', scope=tik.scope_cbuf) dst_l1out = tik_instance.Tensor("int32", [10, 32, 16], name='dst_l1out', scope=tik.scope_cbuf_out) # 将数据搬至源操作数 tik_instance.data_move(a_l1, a_gm, 0, 1, 64, 0, 0) tik_instance.data_move(b_l1, b_gm, 0, 1, 320, 0, 0) # 进行matmul操作,mkn分别为30,64,160,dst_l1out的shape在m维度向上16对齐取值至32 tik_instance.matmul(dst_l1out, a_l1, b_l1, 30, 64, 160) # 将数据搬移至dst_gm,其中burst_len = 30*16*dst_l1out_dtype_size//32 = 60 tik_instance.fixpipe(dst_gm, dst_l1out, 10, 60, 0, 0, extend_params={"relu": True}) tik_instance.BuildCCE(kernel_name="matmul", inputs=[a_gm, b_gm], outputs=[dst_gm])
结果示例:
输入数据: a_l1 = [[[-1, -1, -1, ..., -1, -1, -1] ... [-1, -1, -1, ..., -1, -1, -1]] [[-1, -1, -1, ..., -1, -1, -1] ... [-1, -1, -1, ..., -1, -1, -1]]] b_l1 = [[[1, 1, 1, ..., 1, 1, 1] ... [1, 1, 1, ..., 1, 1, 1]] [[1, 1, 1, ..., 1, 1, 1] ... [1, 1, 1, ..., 1, 1, 1]]] 输出数据: dst_gm = [[[0, 0, 0, ..., 0, 0, 0] ... [0, 0, 0, ..., 0, 0, 0]] ... [[0, 0, 0, ..., 0, 0, 0] ... [0, 0, 0, ..., 0, 0, 0]]]
from tbe import tik tik_instance = tik.Tik() # 定义tensor a_gm = tik_instance.Tensor("float16", [4, 32, 16], name='a_gm', scope=tik.scope_gm) b_gm = tik_instance.Tensor("float16", [4, 160, 16], name='b_gm', scope=tik.scope_gm) element_wise_add_gm = tik_instance.Tensor("float32", [10, 32, 16], name='element_wise_add_gm', scope=tik.scope_gm) # 由于matmul矩阵计算m=30,fixpipe会将dst_l1out中无效数据删除,因此dst_gm的shape在m方向上设置为30即可 dst_gm = tik_instance.Tensor("float32", [10, 30, 16], name='dst_gm', scope=tik.scope_gm) a_l1 = tik_instance.Tensor("float16", [4, 32, 16], name='a_l1', scope=tik.scope_cbuf) b_l1 = tik_instance.Tensor("float16", [4, 160, 16], name='b_l1', scope=tik.scope_cbuf) element_wise_add = tik_instance.Tensor("float32", [10, 32, 16], name='element_wise_add', scope=tik.scope_cbuf) dst_l1out = tik_instance.Tensor("float32", [10, 32, 16], name='dst_l1out', scope=tik.scope_cbuf_out) # 将数据搬至源操作数 tik_instance.data_move(a_l1, a_gm, 0, 1, 128, 0, 0) tik_instance.data_move(b_l1, b_gm, 0, 1, 640, 0, 0) tik_instance.data_move(element_wise_add, element_wise_add_gm, 0, 1, 640, 0, 0) # 进行matmul操作,mkn分别为30,64,160,dst_l1out的shape在m维度向上16对齐取值至32 tik_instance.matmul(dst_l1out, a_l1, b_l1, 30, 64, 160) # 将数据搬移至dst_gm,其中burst_len = 30*16*dst_l1out_dtype_size//32 = 60 tik_instance.fixpipe(dst_gm, dst_l1out, 10, 60, 0, 0, extend_params={"element-wise-add": element_wise_add}) tik_instance.BuildCCE(kernel_name="matmul", inputs=[a_gm, b_gm, element_wise_add_gm], outputs=[dst_gm])
结果示例:
输入数据: a_l1 = [[[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]] ... [[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]]] b_l1 = [[[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]] ... [[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]]] element_wise_add = [[[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]] ... [[1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0] ... [1.0, 1.0, 1.0, ..., 1.0, 1.0, 1.0]]] 输出数据: dst_gm = [[[65.0, 65.0, 65.0, ..., 65.0, 65.0, 65.0] ... [65.0, 65.0, 65.0, ..., 65.0, 65.0, 65.0]] ... [[65.0, 65.0, 65.0, ..., 65.0, 65.0, 65.0] ... [65.0, 65.0, 65.0, ..., 65.0, 65.0, 65.0]]]