概述

本章节包含各种子模块接口,如随机数、内存管理等。

表1 torch_npu.npu API

API名称

说明

(beta)torch_npu.npu.get_npu_overflow_flag

检测NPU计算过程中是否有数值溢出。

(beta)torch_npu.npu.clear_npu_overflow_flag

对NPU芯片溢出检测为进行清零。

torch_npu.npu.matmul.allow_hf32

功能和调用方式与torch.backends.cuda.matmul.allow_tf32类似。

torch_npu.npu.conv.allow_hf32

功能和调用方式与torch.backends.cudnn.allow_tf32类似。

(beta)torch_npu.npu.set_option

详细使用参见《PyTorch 训练模型迁移调优指南》中的“设置算子编译选项”章节

(beta)torch_npu.npu.config.allow_internal_format

是否使用私有格式,设置为True时允许使用私有格式,设置为False时,不允许申请任何私有格式的tensor,避免了适配层出现私有格式流通。

(beta)torch_npu.npu.stress_detect

提供硬件精度在线检测接口,供模型调用。

(beta)torch_npu.npu.stop_device

停止对应device上的计算,对于没有执行的计算进行清除,后续在此device上执行计算会报错。

(beta)torch_npu.npu.restart_device

恢复对应device上的状态,后续在此device上进行计算可以继续进行计算执行。

(beta)torch_npu.npu.check_uce_in_memory

提供故障内存地址类型检测接口,供MindCluster进行故障恢复策略的决策。其功能是在出现UCE片上内存故障时,判断故障内存地址类型。

torch_npu.npu.SyncLaunchStream

创建一条同步下发NPUStream,在该流上下发的任务不再使用taskqueue异步下发。

(beta)torch_npu.npu.utils.is_support_inf_nan

判断当前使用的溢出检测模式,True为INF_NAN模式,False为饱和模式。

(beta)torch_npu.npu.utils.npu_check_overflow

检测梯度是否溢出,INF_NAN模式下检测输入Tensor是否溢出;饱和模式检查硬件溢出标志位判断。

表2 amp API

API名称

说明

(beta)torch_npu.npu.get_amp_supported_dtype

获取NPU设备支持的数据类型,可能设备支持不止一种数据类型。

(beta)torch_npu.npu.is_autocast_enabled

确认autocast是否可用。

(beta)torch_npu.npu.set_autocast_enabled

是否在设备上使能AMP。

(beta)torch_npu.npu.get_autocast_dtype

在amp场景获取设备支持的数据类型,该dtype由torch_npu.npu.set_autocast_dtype设置或者默认数据类型torch.float16。

(beta)torch_npu.npu.set_autocast_dtype

设置设备在AMP场景支持的数据类型。

表3 Random Number Generator API

API接口

说明

beta)torch_npu.npu.get_rng_state

Torch_npu提供随机数相关的部分接口,具体可参考torch.cuda

beta)torch_npu.npu.set_rng_state

beta)torch_npu.npu.get_rng_state_all

beta)torch_npu.npu.set_rng_state_all

beta)torch_npu.npu.manual_seed

beta)torch_npu.npu.manual_seed_all

beta)torch_npu.npu.seed

beta)torch_npu.npu.seed_all

beta)torch_npu.npu.initial_seed

表4 torch_npu API

API名称

说明

(beta)torch_npu.npu.set_dump

传入配置文件来配置dump参数。

(beta)torch_npu.npu.init_dump

初始化dump配置。

(beta)torch_npu.npu.finalize_dump

结束dump。

(beta)torch_npu.npu.set_compile_mode

设置是否开启二进制。

(beta)torch_npu.npu.is_jit_compile_false

确认算子计算是否采用的二进制,如果是二进制计算,返回True,否则返回False。

(beta)torch_npu.npu.set_mm_bmm_format_nd

设置线性module里面的mm和bmm算子是否用ND格式。

(beta)torch_npu.npu.get_mm_bmm_format_nd

确认线性module里面的mm和bmm算子是否有使能ND格式,如果使能了ND,返回True,否则,返回False。

NPU Device

torch_npu提供设备相关的部分接口。

表5 Memory management API

API接口

说明

beta)torch_npu.npu.caching_allocator_alloc

Torch_npu提供内存管理相关的部分接口,具体可参考torch.cuda

beta)torch_npu.npu.caching_allocator_delete

beta)torch_npu.npu.set_per_process_memory_fraction

beta)torch_npu.npu.empty_cache

beta)torch_npu.npu.memory_stats

beta)torch_npu.npu.memory_stats_as_nested_dict

beta)torch_npu.npu.reset_accumulated_memory_stats

beta)torch_npu.npu.reset_peak_memory_stats

beta)torch_npu.npu.reset_max_memory_allocated

beta)torch_npu.npu.reset_max_memory_cached

beta)torch_npu.npu.memory_allocated

beta)torch_npu.npu.max_memory_allocated

beta)torch_npu.npu.memory_reserved

beta)torch_npu.npu.max_memory_reserved

beta)torch_npu.npu.memory_cached

beta)torch_npu.npu.max_memory_cached

beta)torch_npu.npu.memory_snapshot

beta)torch_npu.npu.memory_summary

torch.npu.npu.NPUPluggableAllocator

该接口涉及高危操作,使用请参考torch.npu.npu.NPUPluggableAllocator

torch.npu.npu.change_current_allocator

该接口涉及高危操作,使用请参考torch.npu.npu.change_current_allocator

表6 aoe API

API名称

说明

(beta)torch_npu.npu.set_aoe

AOE调优使能。

表7 Profiler API

API名称

说明

torch_npu.npu.mstx

打点接口。

torch_npu.npu.mstx.mark

标记瞬时事件。

torch_npu.npu.mstx.range_start

标识打点开始。

torch_npu.npu.mstx.range_end

标识打点结束。

torch_npu.npu.mstx.mstx_range

range装饰器,用来采集被装饰函数的range执行耗时。

表8 torch_npu Storage API

API接口

说明

torch_npu.npu.BoolStorage

功能和调用方式与torch.Storage相同,具体请参考https://pytorch.org/docs/stable/storage.html#

torch_npu.npu.ByteStorage

torch_npu.npu.ShortStorage

torch_npu.npu.LongStorage

torch_npu.npu.IntStorage

torch_npu.npu.HalfStorage

torch_npu.npu.CharStorage

torch_npu.npu.DoubleStorage

torch_npu.npu.FloatStorage

表9 alnn API

API名称

说明

(beta)torch_npu.npu.aclnn.version

查询aclnn版本信息。

torch_npu.npu.aclnn.allow_hf32

设置conv算子是否支持hf32,一个属性值,对aclnn的allow_hf32属性的设置和查询。

表10 NPU Tensor API(beta)

PyTorch原生API名称

NPU形式名称

是否支持

参考链接

torch.cuda.DoubleTensor

torch_npu.npu.DoubleTensor

https://pytorch.org/docs/stable/tensors.html#data-types

torch.cuda.ShortTensor

torch_npu.npu.ShortTensor

torch.cuda.CharTensor

torch_npu.npu.CharTensor

torch.cuda.ByteTensor

torch_npu.npu.ByteTensor

torch.cuda.FloatTensor

torch_npu.npu.FloatTensor

torch.cuda.HalfTensor

torch_npu.npu.HalfTensor

torch.cuda.IntTensor

torch_npu.npu.IntTensor

torch.cuda.BoolTensor

torch_npu.npu.BoolTensor

torch.cuda.LongTensor

torch_npu.npu.LongTensor