torch_npu接口列表【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills本章节包含常用自定义接口包括创建tensor及计算类操作。共计144个接口正式接口 79 个Beta 接口 65 个。正式接口API名称说明torch_npu.empty_with_swapped_memory申请一个device信息为NPU且实际内存在host侧的Tensor。torch_npu.erase_streamTensor通过record_stream在内存池上添加的已被stream使用的标记后可以通过该接口移除该标记。torch.npu.get_device_limit通过该接口获取指定Device上的Device资源限制。torch.npu.get_stream_limit通过该接口获取指定Stream的Device资源限制。torch_npu.matmul_checksum提供基于原生torch.matmul和Tensor.matmul接口的AIcore错误硬件故障检测接口。内部执行矩阵计算结果校验过程校验误差和实时计算的校验门限进行对比判断校验误差是否超越门限若超越则认为发生了AIcore错误。torch_npu.npu_advance_step_flashattn在NPU上实现vLLM库中advance_step_flashattn的功能在每个生成步骤中原地更新input_tokensinput_positionsseq_lens和slot_mapping。torch_npu.npu_all_gather_base_mmTP切分场景下融合allgather和matmul实现通信和计算流水并行。torch_npu.npu_alltoallv_gmmMoEMixture of Experts混合专家模型网络中完成路由专家AlltoAllv、Permute、GroupedMatMul融合并实现与共享专家MatMul并行融合先通信后计算。torch_npu.npu_anti_quant对张量x进行反量化操作即将整数恢复为浮点数。torch_npu.npu_convert_weight_to_int4pack将int32类型的输入tensor打包为int4存放每8个int4数据通过一个int32数据承载并进行交叠排放。torch_npu.npu_cross_entropy_loss计算输入input和标签target之间的交叉熵损失。此API将原生CrossEntropyLoss中的log_softmax和nll_loss融合降低计算时使用的内存。torch_npu.npu_dequant_swiglu_quant对张量x做dequant反量化swiglu激活quant量化操作同时支持分组。torch_npu.npu_dynamic_block_quant对输入张量通过给定的row_block_size和col_block_size将输入划分成多个数据块以数据块为基本粒度进行量化。在每个块中先计算出当前块对应的量化参数scale并根据scale对输入进行量化。输...torch_npu.npu_dynamic_quant对输入的张量进行pertoken对称动态量化。torch_npu.npu_dynamic_quant_asymmetric对输入的张量进行动态非对称量化。支持pertoken、pertensor和MoEMixture of Experts混合专家模型场景。torch_npu.npu_fast_gelu快速高斯误差线性单元激活函数Fast Gaussian Error Linear Units activation function对输入的每个元素计算FastGelu的前向结果。torch_npu.npu_ffnFFN算子提供MoeFFN和FFN的计算功能。在没有专家分组expert_tokens为空时是FFN有专家分组expert_tokens不为空时是MoeFFN。torch_npu.npu_fused_infer_attention_score适配增量和全量推理场景的FlashAttention算子既可以支持全量计算场景PromptFlashAttention也可支持增量计算场景IncreFlashAttention。当query矩阵的S为1进入Incre...torch_npu.npu_fused_infer_attention_score_v2适配增量全量推理场景的FlashAttention算子既可以支持全量计算场景PromptFlashAttention也可支持增量计算场景IncreFlashAttention。当不涉及system prefix、左pad...torch_npu.npu_fusion_attention实现“Transformer Attention Score”的融合计算实现的计算公式如下torch_npu.npu_gather_sparse_index—torch_npu.npu_gelu计算高斯误差线性单元的激活函数。torch_npu.npu_gelu_mul当输入Tensor的尾轴为32B对齐场景时使用该API可对输入Tensor进行GELU与MUL结合的复合计算操作以提高算子性能若尾轴为非32B对齐场景时建议走小算子拼接逻辑即按照下述公式分步拼接计算。torch_npu.npu_gmm_alltoallvMoE网络中完成路由专家GroupedMatMul、AlltoAllv融合并实现与共享专家MatMul并行融合先计算后通信。torch_npu.npu_group_norm_silu计算输入张量input按组归一化的结果包括张量out、均值meanOut、标准差的倒数rstdOut以及silu的输出。torch_npu.npu_group_norm_swish计算输入input的组归一化结果y均值mean标准差的倒数rstd以及swish的输出。torch_npu.npu_group_quant对输入的张量进行分组量化操作。torch_npu.npu_grouped_matmulnpu_grouped_matmul是一种对多个矩阵乘法matmul操作进行分组计算的高效方法。该API实现了对多个矩阵乘法操作的批量处理通过将具有相同形状或相似形状的矩阵乘法操作组合在一起减少内存访问开销和计算资源的浪费...torch_npu.npu_grouped_matmul_swiglu_quant_v2npu_grouped_matmul_swiglu_quant_v2是一种融合分组矩阵乘法GroupedMatmul、SwiGLU混合激活函数、量化quant的计算方法。该方法适用于需要对矩阵乘法结果进行SwiGLU激活函...torch_npu.npu_incre_flash_attention增量FA实现。torch_npu.npu_interleave_rope针对单输入x进行旋转位置编码。torch_npu-npu_kv_quant_sparse_flash_attentionkv_quant_sparse_flash_attention在sparse_flash_attention的基础上支持了[Per-Token-Head-Tile-128量化]输入。随着大模型上下文长度的增加Sparse A...torch_npu.npu_kv_rmsnorm_rope_cache融合了MLAMulti-head Latent Attention结构中RMSNorm归一化计算与RoPERotary Position Embedding位置编码以及更新KVCache的ScatterUpdate操作。torch_npu-npu_lightning_indexerlightning_indexer基于一系列操作得到每一个token对应的Top-$k$个位置。torch_npu.npu_mla_prolog推理场景下Multi-Head Latent AttentionMLA前处理的计算。torch_npu.npu_mla_prolog_v2推理场景下Multi-Head Latent AttentionMLA前处理的计算。主要计算过程分为五路torch_npu.npu_mm_all_reduce_baseTP切分场景下实现mm和all_reduce的融合融合算子内部实现计算和通信流水并行。torch_npu.npu_mm_reduce_scatter_baseTP切分场景下实现matmul和reduce_scatter的融合融合算子内部实现计算和通信流水并行。支持perchannelpertoken量化。torch_npu.npu_moe_compute_expert_tokensMoEMixture of Experts混合专家模型计算中通过二分查找的方式查找每个专家处理的最后一行的位置。torch_npu.npu_moe_distribute_combine先进行reduce_scatterv通信再进行alltoallv通信最后将接收的数据整合乘权重再相加。需与torch_npu.npu_moe_distribute_dispatch配套使用相当于按npu_moe_distri...torch_npu.npu_moe_distribute_combine_add_rms_norm需与torch_npu.npu_moe_distribute_dispatch_v2配套使用相当于按npu_moe_distribute_dispatch_v2算子收集数据的路径原路返回后对数据进行add_rms_norm操作。torch_npu.npu_moe_distribute_combine_v2需与torch_npu.npu_moe_distribute_dispatch_v2配套使用相当于按npu_moe_distribute_dispatch_v2算子收集数据的路径原路返回。torch_npu.npu_moe_distribute_dispatch需与torch_npu.npu_moe_distribute_combine配套使用完成MoE的并行部署下的token dispatch与combine。对token数据先进行quant量化可选再进行EPExpert Par...torch_npu.npu_moe_distribute_dispatch_v2需与torch_npu.npu_moe_distribute_combine_v2或torch_npu.npu_moe_distribute_combine_add_rms_norm配套使用完成MoE的并行部署下的token dis...torch_npu.npu_moe_finalize_routing在MoE计算的最后合并MoE FFN(Feedforward Neural Network)的输出结果。torch_npu.npu_moe_gating_top_kMoE计算中对输入x做Sigmoid/SoftMax计算对计算结果分组进行排序最后根据分组排序的结果选取前k个专家。torch_npu.npu_moe_gating_top_k_softmaxMoE计算中对输入x做Softmax计算再做topk操作。torch_npu.npu_moe_init_routingMoE的routing计算根据torch_npu.npu_moe_gating_top_k_softmax的计算结果做routing处理。torch_npu.npu_moe_init_routing_v2MoEMixture of Experts的routing计算根据torch_npu.npu_moe_gating_top_k_softmax的计算结果做routing处理支持不量化和动态量化模式。torch_npu.npu_moe_re_routingMoE网络中进行AlltoAll操作从其他卡上拿到需要算的token后将token按照专家顺序重新排列。torch_npu.npu_moe_update_expert本API支持负载均衡和专家剪枝功能。经过映射后的专家表和mask可传入Moe层进行数据分发和处理。torch_npu.npu_prefetch提供网络weight预取功能将需要预取的权重搬到L2 Cache中。尤其在做较大Tensor的MatMul计算且需要搬移到L2 Cache的操作时可通过该接口提前预取权重适当提高模型性能具体效果取决于用户采用的并行方式...torch_npu.npu_prompt_flash_attention全量FA实现。torch_npu.npu_quant_lightning_indexerQuantLightningIndexer是推理场景下SparseFlashAttentionSFA前处理的计算选出关键的稀疏token并对输入query和key进行量化实现存8算8获取最大收益。torch_npu.npu_quant_matmul完成量化的矩阵乘计算最小支持输入维度为2维最大支持输入维度为6维。torch_npu.npu_quant_matmul_reduce_sum完成量化的分组矩阵计算然后所有组的矩阵计算结果相加后输出。torch_npu.npu_quant_scatter先将updates进行量化然后将updates中的值按指定的轴axis和索引indices更新input中的值并将结果保存到输出tensorinput本身的数据不变。torch_npu.npu_quant_scatter_先将updates进行量化然后将updates中的值按指定的轴axis和索引indices更新input中的值input中的数据被改变。torch_npu.npu_quantize对输入的张量进行量化处理。torch_npu.npu_recurrent_gated_delta_rule该接口实现了变步长Recurrent Gated Delta RuleRGDR的计算逻辑是Transformer线性注意力机制的关键算子之一。通过引入门控机制与递归更新策略RGDR能够在保持线性时间复杂度的同时有效捕捉长距离...torch_npu.npu_rotary_mul实现Rotary Position Embedding (RoPE) 旋转位置编码通过对输入特征进行二维平面旋转注入位置信息。torch_npu.npu_scaled_masked_softmax计算输入张量x缩放并按照mask遮蔽后的Softmax结果。torch_npu.npu_scatter_nd_update将updates中的值按指定的索引indices更新input中的值并将结果保存到输出tensorinput本身的数据不变。torch_npu.npu_scatter_nd_update_将updates中的值按指定的索引indices更新input中的值并将结果保存到输出tensorinput中的数据被改变。torch_npu.npu_scatter_pa_kv_cache更新KVCache中指定位置的key和value。torch_npu.npu_sim_exponential_根据参数lambd生成指数分布随机数并原地填充至输入张量input。torch_npu.npu_sparse_flash_attentionsparse_flash_attentionSFA是针对大序列长度推理场景的高效注意力计算模块该模块通过“只计算关键部分”大幅减少计算量然而会引入大量的离散访存造成数据搬运时间增加进而影响整体性能。torch_npu.npu_sparse_lightning_indexer_grad_kl_loss该接口实现了npu_lightning_indexer的反向功能并融合了Loss的计算。npu_lightning_indexer用于筛选Attention的query与key间最高内在联系的Top-k项存放在spars...torch_npu.npu_swiglu_quant在swiglu激活函数后添加quant操作实现输入x的SwiGluQuant计算支持int8或int4量化输出支持MoE场景和非MoE场景group_index为空支持分组量化支持动态/静态量化。torch_npu.npu_top_k_top_p对原始输入logits进行top-k和top-p采样过滤。torch_npu.npu_top_k_top_p_sample根据输入词频logits、top_k/top_p采样参数、随机采样权重分布q进行topK-topP-Sample采样计算输出每个batch的最大词频logits_select_idx以及topK-topP采样...torch_npu.npu_trans_quant_param完成量化计算参数scale数据类型的转换将float32数据按照bit位存储进一个int64数据里。torch_npu.npu_transpose_batchmatmul完成张量input与张量weight的矩阵乘计算。仅支持三维的Tensor传入。Tensor支持转置转置序列根据传入的数列进行变更。perm_x1代表张量input的转置序列perm_x2代表张量weight的转置...torch_npu.npu_weight_quant_batchmatmul该接口用于实现矩阵乘计算中weight输入和输出的量化操作支持pertensor、perchannel、pergroup多场景量化。torch.npu.reset_stream_limit调用torch.npu.set_stream_limit接口设置指定Stream的Device资源限制后可调用本接口重置指定Stream的Device资源限制恢复默认配置此时可通过torch.npu.get_stream_...torch_npu.scatter_update将tensor updates中的值按指定的轴axis和索引indices更新tensor data中的值并将结果保存到输出tensordata本身的数据不变。torch_npu.scatter_update_将tensor updates中的值按指定的轴axis和索引indices更新tensor data中的值并将结果保存到输出tensordata本身的数据被改变。torch.npu.set_device_limit设置一个进程上指定device执行算子时所使用的cube和vector核数。torch.npu.set_stream_limit设置指定Stream的Device资源限制。Beta 接口API名称说明betatorch_npu._npu_dropout不使用种子seed进行dropout结果计数。betatorch_npu.copy_memory_从src拷贝元素到self张量并原地返回self张量。betatorch_npu.empty_with_format返回一个填充未初始化数据的张量。betatorch_npu.fast_gelu快速高斯误差线性单元激活函数Fast Gaussian Error Linear Units activation function对输入的每个元素计算FastGelu。支持FakeTensor模式。betatorch_npu.npu_alloc_float_status申请一个专门用于存储浮点运算状态标志的Tensor。该Tensor用于后续记录计算过程中的溢出状态。betatorch_npu.npu_anchor_response_flags在单个特征图中生成锚点的响应标志即标识哪些锚点需要参与训练或推理。betatorch_npu.npu_apply_adam获取adam优化器的计算结果。betatorch_npu.npu_batch_nms以批量处理方式对每个类别的检测框进行非极大值抑制Non-Maximum SuppressionNMS从而去除冗余检测框输出保留下来的检测框及其对应的类别和得分。betatorch_npu.npu_bert_apply_adam针对bert模型获取adam优化器的计算结果。betatorch_npu.npu_bmmV2将矩阵“a”乘以矩阵“b”生成“a*b”。支持FakeTensor模式。betatorch_npu.npu_bounding_box_decode根据rois和deltas生成标注框。自定义Faster R-CNN算子。betatorch_npu.npu_bounding_box_encode计算标注框和ground truth真值框之间的坐标变化。自定义Faster R-CNN算子。betatorch_npu.npu_broadcast返回self张量的新视图其单维度扩展结果连续。张量也可以扩展更多维度新的维度添加在最前面。betatorch_npu.npu_ciou应用基于NPU的CIoU操作。在DIoU的基础上增加了penalty term并propose CIoU。betatorch_npu.npu_clear_float_status清除溢出检测相关标志位。betatorch_npu.npu_confusion_transpose同时执行reshape与transpose运算。betatorch_npu.npu_conv2d在由多个输入平面组成的输入图像上应用一个2D卷积。betatorch_npu.npu_conv3d在由多个输入平面组成的输入图像上应用一个3D卷积。betatorch_npu.npu_conv_transpose2d在由多个输入平面组成的输入图像上应用一个2D转置卷积算子有时这个过程也被称为“反卷积”。betatorch_npu.npu_convolution在由多个输入平面组成的输入图像上应用一个2D或3D卷积。betatorch_npu.npu_convolution_transpose在由多个输入平面组成的输入图像上应用一个2D或3D转置卷积算子有时这个过程也被称为“反卷积”。betatorch_npu.npu_deformable_conv2d使用预期输入计算变形卷积deformable convolution的输出。betatorch_npu.npu_diou应用基于NPU的DIoU操作。考虑到目标之间距离以及距离和范围的重叠率不同目标或边界需趋于稳定。betatorch_npu.npu_dropout_with_add_softmax实现axpy_v2、softmax_v2、drop_out_domask_v3功能。即betatorch_npu.npu_dtype_cast执行张量数据类型dtype转换。支持FakeTensor模式。betatorch_npu.npu_format_cast修改input的数据格式为目标格式修改后的新张量不会替换原有张量。betatorch_npu.npu_format_cast_原地修改input的数据格式为目标格式。betatorch_npu.npu_fused_attention_score实现“Transformer attention score”的融合计算逻辑主要将matmul、transpose、add、softmax、dropout、batchmatmul、permute等计算进行了融合。betatorch_npu.npu_get_float_status获取溢出检测结果。betatorch_npu.npu_giou首先计算两个框的最小封闭面积和IoU然后计算封闭区域中不属于两个框的封闭面积的比例最后从IoU中减去这个比例得到GIoU。betatorch_npu.npu_grid_assign_positive执行position-sensitive的候选区域池化梯度计算。betatorch_npu.npu_gru计算DynamicGRUV2。betatorch_npu.npu_indexing以begin为起始索引end为结束索引strides为步长对输入张量进行切片。betatorch_npu.npu_iou根据ground-truth和预测区域计算交并比IoU或前景交叉比IoF。betatorch_npu.npu_layer_norm_eval对层归一化结果进行计算。与torch.nn.functional.layer_norm相同优化NPU设备实现。betatorch_npu.npu_linear将矩阵“a”乘以矩阵“b”生成“a*b”。betatorch_npu.npu_lstm计算DynamicRNN。betatorch_npu.npu_max使用dim对最大结果进行计算。类似于torch.max优化NPU设备实现。betatorch_npu.npu_min使用dim对最小结果进行计算。类似于torch.min优化NPU设备实现。betatorch_npu.npu_mish按元素计算self的双曲正切。betatorch_npu.npu_mla_prolog_v3推理场景下Multi-Head Latent Attention前处理的计算操作。该算子实现四条并行的计算路径betatorch_npu.npu_multi_head_attention实现Transformer模块中的MultiHeadAttention计算逻辑。betatorch_npu.npu_nms_rotated按分数降序选择旋转标注框的子集。betatorch_npu.npu_nms_v4按分数降序选择标注框的子集。betatorch_npu.npu_nms_with_mask生成值0或1用于nms算子确定有效位。betatorch_npu.npu_one_hot返回一个one-hot张量。input中index表示的位置采用on_value值而其他所有位置采用off_value的值。betatorch_npu.npu_pad填充张量。betatorch_npu.npu_ps_roi_pooling执行Position Sensitive ROI Pooling。betatorch_npu.npu_ptiou根据ground-truth和预测区域计算交并比IoU或前景交叉比IoF。betatorch_npu.npu_random_choice_with_mask获取非零元素的index混洗后输出。betatorch_npu.npu_reshapereshape张量。仅更改张量shape其数据不变。betatorch_npu.npu_rms_normRmsNorm算子是大模型常用的归一化操作相比LayerNorm算子其去掉了减去均值的部分。betatorch_npu.npu_roi_align从特征图中获取ROI特征矩阵。自定义Faster R-CNN算子。betatorch_npu.npu_rotated_iou计算旋转框的IoU。betatorch_npu.npu_rotated_overlaps计算旋转框的重叠面积。betatorch_npu.npu_sign_bits_pack将float类型的输入打包为uint8类型。每8个浮点数打包为一个uint8数值-1.0编码为二进制位01.0编码为二进制位1并按小端序进行打包。betatorch_npu.npu_sign_bits_unpack将uint8类型的输入拆包为float类型。将uint8数值中的8个二进制位解码为8个浮点数0解码为-1.01解码为1.0并以小端序进行返回。betatorch_npu.npu_silu计算self的Swish。Swish是一种激活函数计算公式为 x * sigmoid(x) 。betatorch_npu.npu_slice从张量中提取切片。betatorch_npu.npu_softmax_cross_entropy_with_logits计算softmax的交叉熵损失。betatorch_npu.npu_sort_v2沿给定维度对输入张量元素进行升序排序不返回索引。若dim未设置则选择输入的最后一个维度。如果descending为True则元素将按值降序排序。betatorch_npu.npu_swigluSwish门控线性单元激活函数实现张量input的swiglu计算。betatorch_npu.npu_transpose返回原始张量视图其维度已permute结果连续。支持FakeTensor模式。betatorch_npu.npu_yolo_boxes_encode根据YOLO的锚点框anchor box和真值框ground-truth box生成标注框。自定义mmdetection算子。betatorch_npu.one_用1填充self张量。【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考