基于昇腾NPU的矩阵向量乘优化策略OA
Optimization Strategies for Matrix-Vector Multiplication Based on Ascend NPU
通用矩阵向量乘法(GEMV)是大语言模型解码阶段的核心运算,其性能直接决定模型推理效率.然而,现有神经网络处理单元(NPU)大多通过调用通用矩阵乘法(GEMM)接口实现GEMV运算,在小规模维度场景下适配性不佳,硬件资源利用率低.该文基于昇腾910B NPU,围绕GEMV算子的硬件适配与性能优化开展研究;结合昇腾910B AI Core计算内核的硬件特征,分别基于矩阵运算单元(AIC)与向量运算单元(AIV)设计了适配对应计算单元的GEMV基础实现方案.针对AIC的硬件数据排布约束,将GEMV运算等价转化为转置矩阵与向量相乘,以提升结果写回效率;针对AIV的访存特征,根据不同数据排布设计差异化计算流程,保障内存访问和计算指令的连续性.在此基础上,进一步提出了通用优化策略:采用矩形分块机制维持访存连续性、减少指令发射次数,并通过二次任务切分缓解计算负载不均衡问题.此外,设计算子融合接口,支持GEMV与逐元素(Elementwise)算子融合运算,在AIV单元上完成融合计算后的性能损耗可控制在2%以内.实验结果表明,在单精度浮点、半精度浮点与8位整型3类数据精度场景下,该文优化实现的GEMV算子性能分别为昇腾CANN平台原生GEMM接口的3.38倍、2.94倍与23.95倍(含反量化开销),可为NPU平台下大模型的高效部署与推理提供有力支撑.
General Matrix-Vector Multiplication(GEMV)is a core operation in the decoding phase of large lan-guage models,and its performance directly determines the inference efficiency of the models.However,existing Neural Processing Units(NPUs)mostly implement GEMV operations by invoking General Matrix Multiplication(GEMM)interfaces,which suffer from poor adaptability and low hardware resource utilization in small-scale dimen-sion scenarios.This paper conducts research on hardware adaptation and performance optimization of GEMV opera-tors based on the Ascend 910 B NPU.By leveraging the hardware features of the Ascend 910 B AI Core compute kernels,this paper designs fundamental GEMV implementation schemes adapted to the corresponding compute units,respectively based on the matrix operation unit(AIC)and the vector operation unit(AIV).To meet the hard-ware data layout requirements of AIC,this paper equivalently transforms the GEMV operation into matrix-transpose-vector multiplication to improve result write-back efficiency.To address the memory access characteristics of the AIV,differentiated computation flows are designed according to different data layouts to ensure the continuity of memory access and compute instructions.On this basis,this paper further proposes general optimization strategies:adopting a rectangular tiling mechanism to maintain memory access continuity and reduce instruction dispatch over-head,and employing secondary task partitioning to alleviate computational load imbalance.Additionally,this paper designs an operator fusion interface that supports the fusion of GEMV with elementwise operators,with performance overhead kept within 2%when fusion computation is completed on the AIV unit.Experimental results show that under three data precision scenarios—single-precision floating-point,half-precision floating-point,and 8-bit integer,the performance of the GEMV implemented and optimized in this study reaches 3.38 times,2.94 times,and 23.95 times(including dequantization)that of the default GEMM interface of the Ascend CANN platform,respectively.This provides strong support for efficient deployment and inference of large models on NPU platforms.
陆璐;古钟书
华南理工大学 计算机科学与工程学院,广东 广州 510006华南理工大学 计算机科学与工程学院,广东 广州 510006
信息技术与安全科学
通用矩阵向量乘法昇腾NPU算子融合
general matrix-vector multiplicationAscend NPUoperator fusion
《华南理工大学学报(自然科学版)》 2026 (8)
1-13,13
广东省自然科学基金项目(2024A1515010204)南方电网科学研究院项目(1500002024030103XA00063) Supported by the Natural Science Foundation of Guangdong Province(2024A1515010204)
评论