Files
TorchNPU/docs/zh/api/native_api/deterministic_computing_apis.md
T
ascend-robot c9c95daf42 【docs】Modify version 26.1.0 simultaneously
Co-authored-by: l60114168<liuyaxiong6@h-partners.com>



# message auto-generated for no-merge-commit merge:
!43055 merge master into master

【docs】Modify version 26.1.0 simultaneously

Created-by: lyx324521
Commit-by: l60114168
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
https://gitcode.com/Ascend/pytorch/issues/3327
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [x] 资料更新

# 【修改方案】
26.1.0修改同步主干

# 【资料变更】
涉及

# 【接口变更】
不涉及

# 【功能验证】
> 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\
> 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!43055
2026-07-29 09:20:23 +08:00

4.6 KiB

确定性计算API清单

简介

在使用PyTorch框架进行训练时,部分算子在计算时可能存在随机性,若需要输出结果排除随机性,则需要设置确定性计算开关。在开启确定性计算时,当使用相同的输入在相同的硬件和软件上执行相同的操作,输出的结果每次都是相同的。

Note

  • 确定性计算设置方法都必须与待设置的网络、算子等在同一个主进程,部分模型脚本中main()与训练网络并不在一个进程中。
  • 当前同一线程中只能设置一次确定性状态,多次设置以第一次有效设置为准,后续设置不会生效。
    有效设置:在设置确定性状态后,必须实际执行至少一次算子,使其任务下发。仅仅设置而未执行任何算子,只会开启确定性标志,但不会真正影响算子行为。
    解决方案:
    1. 暂不推荐在同一线程中多次设置确定性。
    2. 该问题在开启和关闭确定性开关的情况下均存在,在后续版本中会解决该问题。

使用方法

确定性计算的用法和效果具体可参考相应官方文档torch.use_deterministic_algorithms,本小节仅介绍开启确定性计算的方法。

Caution

开启确定性开关可能会导致性能下降。

  1. 开启确定性计算开关:

    torch.use_deterministic_algorithms(True)
    
  2. 验证设置是否成功。

    1. 执行如下命令查询接口设置:

      torch.are_deterministic_algorithms_enabled()
      
    2. 回显示例如下:

      print(torch.are_deterministic_algorithms_enabled())
      

    执行训练时,当接口返回值为True时,表示当前已开启确定性计算开关,返回值为False则表示未开启。

API清单

当使用Atlas A2 训练系列产品/Atlas A2 推理系列产品或Atlas A3 训练系列产品/Atlas A3 推理系列产品时,表1所示API计算存在随机性,开启确定性计算开关可以保持计算结果的确定性。

表 1 API列表

API
torch.nn.functional.batch_norm
torch.nn.functional.binary_cross_entropy
torch.nn.functional.ctc_loss
torch.cumsum
torch.dot
torch.matmul
torch.nn.functional.embedding
torch.nn.functional.nll_loss
torch.nn.functional.prelu
torch.mean
torch.nn.functional.adaptive_avg_pool2d
torch.nn.functional.avg_pool2d
torch.nn.functional.binary_cross_entropy_with_logits
torch.nn.functional.mse_loss
torch.addbmm
torch.Tensor.addbmm_
torch.addmv
torch.Tensor.addmv_
torch.nn.functional.l1_loss
torch.nn.functional.smooth_l1_loss
torch.addmm
torch.Tensor.addmm
torch.mm
torch.bmm
torch.nn.functional.layer_norm
Tensor.put_
torch.Tensor.index_put
torch.Tensor.index_put_
torch.ops.aten.convolution_backward
torch.nn.NLLLoss2d
torch.nn.functional.fold
torch.nn.functional.unfold
torch.nn.functional.kl_div
torch.nn.functional.multilabel_margin_loss
torch.std_mean
torch.std
torch.var_mean
torch.var
torch.sum
torch.nn.functional.interpolate
torch.nn.functional.soft_margin_loss
torch.trace
torch.mv

当使用Ascend 950DT时,表2所示API计算存在随机性,开启确定性计算开关可以保持计算结果的确定性。

表 2 API列表

API
torch.Tensor.put_
torch.max_unpool2d
torch.max_unpool3d
torch._unique
torch._unique2
torch.scatter_add
torch.Tensor.scatter_add_
torch.scatter
torch.Tensor.scatter_
torch.index_add
torch.bincount
torch.index_copy
torch.index_copy_
torch.Tensor.index_put_
torch.linalg.vector_norm
torch.ops.aten.upsample_bicubic2d 反向
torch.ops.aten.upsample_bilinear2d 反向
torch.ops.aten.upsample_linear1d 反向
torch.ops.aten._upsample_bilinear2d_aa 反向
torch.ops.aten._upsample_bicubic2d_aa 反向
torch.nn.functional.adaptive_max_pool3d 反向
torch.nn.functional.batch_norm
torch.Tensor.coalesce
torch.grid_sampler_2d 反向
torch.grid_sampler_3d 反向