33 Commits
Author SHA1 Message Date
wuyouqi1 b7bd974841 fix: cut patch_getenv overhead from 3x to ~1x and gate it by TORCH_NPU_LOGS
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!47484 merge personal/fix-patch-getenv-3x-overhead-master into master

fix: cut patch_getenv overhead from 3x to ~1x and gate it by TORCH_NPU_LOGS

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】> **如有社区issue,请关联issue链接**\
> **请勿携带内部流程信息(需求链接、问题单、内部issue等)**


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


关联issue:https://gitcode.com/Ascend/pytorch/issues/5090(Fixes #5090)


# 【修改方案】


`torch_npu/utils/patch_getenv.py` 使全进程 `os.getenv`/`os.environ.get` 开销达
~3.3x。根因:保存的原生 `os.getenv` 调用时重新解析 `environ.get`,再次进入被
patch 的实例属性(嵌套);且 `key in os.environ` 预检查本身即一次完整查找——
一次调用共 3 次真实查找。


修复:


1. 两个 patch 点共用一个 `_patched_getenv`,直读 `os.environ[key]`
   (KeyError 即未命中):恰好 1 次真实查找,构造上无嵌套
2. 仅当日志可观测时安装:`TORCH_NPU_LOGS`/`TORCH_LOGS` 已设置,或 import 时
   `torch_npu.env` logger 已可见 INFO。patch_getenv 早于
   `_add_logging_module()` 配置 logger,import 时 `isEnabledFor` 不可信,故按
   原始环境变量门控(与 ACL log 门禁做法一致);钩子为私有
   `_install()`,不进入公开 API 兼容性检查
3. UT:`test/utils/test_patch_getenv.py` 6 用例(门控/包装语义/单次查找计数);
   `test_torch_npu_init.py` 快照断言按门控分支


# 【资料变更】


不涉及


# 【接口变更】


不涉及。`os.getenv`/`os.environ.get` 返回值行为完全不变(含 bytes key 抛
TypeError 对齐),仅日志按需安装;默认配置不安装(原生直通)。


# 【功能验证】


| 场景 | 真实查找/次 | 耗时(py3.14) |
|---|---|---|
| native | 1 | 597 ns (1.00x) |
| 修复前 | 3 | 1974 ns (3.31x) |
| 修复后(已安装) | 1 | 626 ns (1.05x) |
| 修复后(默认不装) | 1 | 1.00x |


(py3.11 修复前 3.39x;纯 CPython 复现,与硬件/CANN 无关)


语义回归(命中/未命中/default/空串/kwargs/bytes key)、门控矩阵(默认不装,
`TORCH_NPU_LOGS`/`TORCH_LOGS`/预先配置 INFO 均安装)、端到端日志去重:全部
PASS。`test_api_compatibility` 复检通过(公开 callable 为空)。


本地 whl 实测(conda wyq_py10,torch_npu 2.7.1 whl):`test_patch_getenv.py`
6 用例全过;`test_torch_npu_init.py` 16 用例全过(test_14 按 #5230 skip)。


**test_14 临时 skip**:`test_torch_npu_init.py::test_14_dtensor_strategies_are_
registered_without_dynamo` 在 v2.7.1 HEAD 上确定性失败,与本 MR 无关——
!44496 的 flex_attention 补丁在 import 时经
`from torch.nn.attention import flex_attention` 拉入 `torch._dynamo`
(torch 2.7.x),破坏 #2788 的延迟导入契约。详见
https://gitcode.com/Ascend/pytorch/issues/5230,待修复后恢复用例。


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


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


See merge request: Ascend/pytorch!47484
2026-10-08 11:45:34 +08:00
wuyouqi1 b8d20d53ed fix(asd): 修正 matmul_checksum UT SupportedDevices 设备名(Ascend910C → Ascend910_93)
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!47478 merge personal/fix-checksum-ut-soc-master into master

fix(asd): 修正 matmul_checksum UT SupportedDevices 设备名(Ascend910C → Ascend910_93)

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

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

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

1. `test/npu/test_matmul_checksum.py` 中 4 个 abft 用例的 `@SupportedDevices(['Ascend910B', 'Ascend910C'])` 修正为 `@SupportedDevices(['Ascend910B', 'Ascend910_93'])`。
2. 原因:torch_npu SoC 名称表中不存在 `Ascend910C`(A3 实际设备名为 `Ascend910_93xx`,`get_device_name` 返回,`SupportedDevices` 匹配名为 `Ascend910_93`),原写法导致 A3 机器上 4 个 abft 用例被误 skip(skip 理由为"设备不在支持列表",而非算子不可用)。
3. 与 op-plugin 仓 `_npu_matmul_abft_verify` 算子 UT 的设备门控写法保持一致。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

- 纯 UT 门控修正,不改变任何运行时行为。
- A2(Ascend910B)实测:`python test/npu/test_matmul_checksum.py -v` → 11/11 通过(行为不变)。
- A3(Ascend910_93):修复前 4 个 abft 用例因设备名不匹配被误 skip;修复后不再被设备门控拦截,按算子可用性探测正常判定(与 A2 行为一致)。

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

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


See merge request: Ascend/pytorch!47478
2026-09-30 16:40:56 +08:00
wuyouqi1 ea07320357 fix(core): skip internal-format repr probe for symbolic tensors in npu_patches
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46788 merge fix/fake-tensor-repr-segv-master-pyguard into master

fix(core): skip internal-format repr probe for symbolic tensors in npu_patches

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

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

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

关联 issue:https://gitcode.com/Ascend/pytorch/issues/4744

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

!46582 已为 `get_npu_format` 增加 C++ 侧 `dynamic_cast` 守卫(FakeTensor 触达内核时抛可捕获异常而非 SIGSEGV)。但 master 上的 `Tensor.__repr__` 内部格式猴补丁位于 `torch_npu/_init/patches/npu_patches.py` 的 `apply_npu_internal_format_repr_patch`(发布分支位于 `torch_npu/utils/tensor_methods.py`),前述 PR 误判 master 已无该补丁,未带 Python 侧守卫。本 PR 补齐:

1. `torch_npu/_init/patches/npu_patches.py`:`_npu_internal_format_repr` 增加 `_is_symbolic_tensor` 守卫,FakeTensor 跳过 `get_npu_format` 探测(与发布分支修复 !46578 系列的 Python 侧守卫一致)。C++ 守卫合入后该探测对符号张量仅是每次 repr 多一次抛/捕异常,跳过更干净,并保留纵深防御。
2. `test/npu/test_tensor.py`:补齐 repr 路径 UT(自发布分支修复移植):
   - `test_faketensor_repr`:`repr(FakeTensor)` 正常输出,不触发 `get_npu_format` 探测
   - `test_faketensor_repr_under_dispatch_logging`:dispatch logging 场景对 FakeTensor 求 repr 走通(修复前的 SIGSEGV 路径)
   - 既有 `test_get_npu_format_faketensor_guarded(_no_dispatch)` 继续看护 C++ 路径

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

- 新增 UT:`test_faketensor_repr`、`test_faketensor_repr_under_dispatch_logging`(与发布分支 !46578 系列同内容,已在其分支 CI 验证通过)
- 回归:真实 NPU 张量(含 NZ 私有格式)`repr` 走内部格式 `.cpu()` 路径的行为不变;`_is_symbolic_tensor` 仅对 FakeTensor 短路,普通 npu 张量不受影响
- 依赖 CI 门禁完成构建与全量 UT 验证

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

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


See merge request: Ascend/pytorch!46788
2026-09-22 15:37:17 +08:00
wuyouqi1 85d554a0a4 [sync] PR-45502: test: un-skip test_single_reshape_copy_to_contiguous cases (issue 4356)
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46545 merge fix/narrow-aclnn-nc1hwc0-3d-fallback-master into master

[sync] PR-45502: test: un-skip test_single_reshape_copy_to_contiguous cases (issue 4356)

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

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

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

关联 issue:https://gitcode.com/Ascend/pytorch/issues/4356

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间是如何交互的,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

本 MR 为 !45502(已合入 v2.7.1:`test: un-skip test_single_reshape_copy_to_contiguous cases (issue 4356)`)向 **master** 分支的同步合入,代码内容与 v2.7.1 合入版本一致。

1. 背景:issue 4356 中 `allow_internal_format=True` 时,非视图 3D NC1HWC0 张量经 `npu_format_cast(src, NCHW)`(如 `to("cpu")` 触发)在 aclnn 路径报 161001 且毒化进程,导致 `test_single_reshape_copy_to_contiguous` 类 4 个用例被 `@unittest.skip` 临时跳过。
2. 该问题的根因在 CANN 侧(`aclnnNpuFormatCast` 对 3D NC1HWC0 → NCHW 的支持性判定与执行不一致),**CANN 已在 9.2.0 修复**;torch_npu 侧无需 workaround。
3. 本 MR 仅恢复测试看护:解除 `test/trans_contiguous/test_single_reshape_copy_to_contiguous.py` 中 4 个用例的 `@unittest.skip`(view/unsqueeze/flatten/narrow,issue 4356)。
4. 头注释补充说明:设置 `allow_internal_format = True` 走真实内部格式路径时,3D NC1HWC0 在 aclnn 路径上的正确转换需要 CANN >= 9.2.0。


5. 相对 v2.7.1 合入版本的差异:额外删除了该测试文件中解除 skip 后不再使用的 `import unittest`。master 分支的 `.lintrunner.toml` 启用了 flake8 检查(发布分支中该项被注释),机器人同步 MR !46509 因 F401('unittest' imported but unused)CI 失败;本 MR 基于最新 master 重新提交并已修复该 lint 问题(本地 `flake8 --config=.flake8` 验证通过)。


# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

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

与 !45502(v2.7.1)验证结论一致,本 MR 为同一变更的分支同步:

1. 环境:910B2 / CANN 9.2.0 / torch_npu(v2.7.1 构建)。
2. 默认配置(`allow_internal_format` 保持注释,走 aclnn 降级路径):`python test_single_reshape_copy_to_contiguous.py -v SingleViewCopyToContiguous`,5 个用例全部通过。
3. 解开 `torch.npu.config.allow_internal_format = True`(CANN 9.2.0 修复生效后):整类用例通过,`test_narrow_at_first_axis_copy` 的 `[fp16, NC1HWC0, [20,16,16]]` item(原 161001 失败点)恢复正常。
4. 本 MR 不修改框架代码,无新增 UT;由恢复的 4 个用例看护原失败路径。

5. **Lint 自验证**:`python -m flake8 --config=.flake8 test/trans_contiguous/test_single_reshape_copy_to_contiguous.py` 通过(修复机器人 MR !46509 的 F401 失败项)。



See merge request: Ascend/pytorch!46545
2026-09-20 17:15:55 +08:00
wuyouqi1 d700685e9e [master] fix(core): guard get_npu_format against symbolic tensors to avoid segfault
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46582 merge fix/fake-tensor-repr-segv-master into master

[master] fix(core): guard get_npu_format against symbolic tensors to avoid segfault

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->
https://gitcode.com/Ascend/pytorch/issues/4744
# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

1. `torch_npu/csrc/aten/common/FormatCastKernelNpu.cpp`:`get_npu_format` 改用 `dynamic_cast<NPUStorageImpl*>` 判空 + `TORCH_CHECK`,替代原 `NPUBridge::GetNpuStorageImpl` 的无校验 `static_cast`
2. `test/npu/test_tensor.py`:新增 2 个 UT 看护(dispatch 生效与 no_dispatch 两条路径)

**根因**:FakeTensor 经 `_make_subclass(device_for_backend_keys='npu')` 创建,impl 级 `device_opt_` 被改写为 npu(`torch_check_npu` 放行)但底层是普通 `c10::StorageImpl`(无 `npu_desc_` 成员),`get_npu_format` 无条件 `static_cast` 后读 `npu_desc_` 越界 → SIGSEGV(try/except 无法捕获)。Python dispatch 被禁用(no_dispatch 嵌套调用)时 op 直达 C++ 内核,仅注册 meta/fake 实现无法拦截,故必须 C++ 层兜底。master 上 v2.7.1 修复所防护的 `Tensor.__repr__` 猴补已在上游移除,故本分支仅需 C++ 侧守卫与 UT。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

已在 v2.7.1 分支完成同等 C++ 改动的完整验证(MR !46180),本分支为同内容同步:
- UT:`test_get_npu_format_faketensor_guarded`(dispatch 生效时抛可捕获异常)/ `test_get_npu_format_faketensor_guarded_no_dispatch`(no_dispatch 嵌套路径,旧 C++ 上实测 SIGSEGV,判别性成立)
- 无回归:真实 NPU 张量(含 NZ 私有格式、视图、save/load、autocast、autograd 矩阵)`get_npu_format` 行为不变;npu 设备 storage 全局工厂 `SetStorageImplCreate(PrivateUse1, make_npu_storage_impl)` 保证所有显式构造点均有 NPUStorageImpl
- 依赖 CI 门禁完成构建与全量 UT 验证

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

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


See merge request: Ascend/pytorch!46582
2026-09-20 16:41:48 +08:00
wuyouqi1 a956f7551c docs(alloc): 完善PYTORCH_ALLOC_CONF环境变量资料
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46413 merge docs/pytorch-alloc-conf into master

docs(alloc): 完善PYTORCH_ALLOC_CONF环境变量资料

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

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

- [x] 需求 https://gitcode.com/Ascend/pytorch/issues/4691
- [ ] 问题单
- [ ] issue/工单
- [ ] 重构优化
- [x] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

**背景与依据**:PyTorch社区自v2.10.0版本起正式发布环境变量`PYTORCH_ALLOC_CONF`,并在官方文档中定版为主变量(社区PR pytorch/pytorch#167659),`PYTORCH_CUDA_ALLOC_CONF`仅作为向后兼容的别名保留。该变量是社区面向全部加速器后端(CUDA/ROCm/XPU等)提供的设备无关(device-agnostic)缓存分配器统一配置入口。为与PyTorch社区命名及行为保持一致,TorchNPU自26.2.0版本起引入同名环境变量`PYTORCH_ALLOC_CONF`,作为配置NPU缓存分配器的推荐优先入口(与`PYTORCH_NPU_ALLOC_CONF`互斥,同时配置会报错并退出程序),因此资料约束表述为"该变量适用于TorchNPU 26.2.0且PyTorch 2.10.0及以上版本"。

1. 功能描述章节整体重构:开头补充社区背景——PyTorch社区自2.10.0版本起正式发布PYTORCH_ALLOC_CONF,作为面向全部加速器后端的缓存分配器统一配置入口,设备专属变量名(如PYTORCH_CUDA_ALLOC_CONF)仅为向后兼容别名;随后说明TorchNPU自26.2.0版本起为保持与社区命名一致引入同名变量、建议优先使用;与原生PyTorch的参数差异以NOTE提示框简要说明(torch_npu在原生参数基础上还支持NPU专属参数);作用对象、互斥约束等信息分别由功能描述、使用约束章节单一来源呈现,避免重复。
2. 使用约束章节措辞调整:明确"该变量适用于TorchNPU 26.2.0且PyTorch 2.10.0及以上版本,建议优先使用此变量","该变量与PYTORCH_NPU_ALLOC_CONF互斥,同时配置会报错并退出程序"。
3. "支持的型号"章节移除跨页链接,直接以term标签列出支持的型号(Atlas 训练系列产品、Atlas A2 训练系列产品、Atlas A3 训练系列产品、Atlas 推理系列产品)。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

涉及。本PR为纯资料更新,仅修改docs/zh/api/environment_variable/memory_management/PYTORCH_ALLOC_CONF.md,补充与原生PyTorch的差别说明、调整使用约束措辞、内联支持型号列表。

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

不涉及代码逻辑变更,纯资料修改。已自检Markdown格式、term标签用法与同目录其他环境变量文档保持一致,章节内链接仅保留同目录文档相对链接。

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

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


See merge request: Ascend/pytorch!46413
2026-09-18 14:30:43 +08:00
wuyouqi1 f74ee4311d fix(test): skip float_status UTs on Ascend950 and clarify FORCE_OVERFLOW_CHECK doc SOC support
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46182 merge fix/force-overflow-check-a5-master into master

fix(test): skip float_status UTs on Ascend950 and clarify FORCE_OVERFLOW_CHECK doc SOC support

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

# 【合入来源】
> **如有社区issue,请关联issue链接**> **请勿携带内部流程信息(需求链接、问题单、内部issue等)**

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/4374
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】

测试发现 <term>Ascend 950DT</term> 上 `npu_get_float_status` / `npu_alloc_float_status` / `npu_clear_float_status` 算子不支持(op-plugin 文档产品支持表未包含 950),需调整 MR !44962 引入的 UT 与文档:

1. `test/npu/test_check_over_flow.py`:新增 `FLOAT_STATUS_DEVICES`(Ascend910B/910C/910_93,float_status 算子文档支持范围与 CI 机器池的交集);`test_check_over_flow` / `test_float_status_detection` / `test_npu_check_overflow_register_path` 三个用例统一使用 `@SupportedDevices(FLOAT_STATUS_DEVICES)` 过滤,Ascend950 上跳过(寄存器检测路径依赖的 float_status 算子不支持);环境变量负向用例(子进程,不触算子)保持 `NON_SATURATION_DEVICES` 不变。
2. `docs/zh/api/environment_variable/op_execution/FORCE_OVERFLOW_CHECK.md`:将"值为1"条目精简为开关语义,接口可用性说明独立成段;示例标注为 <term>Atlas A2 训练系列产品</term>/<term>Atlas A3 训练系列产品</term> 上的示例;NOTICE 块(移至示例后)说明 <term>Ascend 950DT</term> 不支持使用 `get_npu_overflow_flag` 等溢出检测接口、开启该开关后可通过 plog 中 `aclrtSetStreamOverflowSwitch` 相关日志(`ASCEND_GLOBAL_LOG_LEVEL=1`)确认开关已生效;删除"针对 Atlas 训练/推理系列及 Atlas 200I/500 A2 仅支持饱和模式、该环境变量不生效"的冗余说明;"支持的型号"表保留 <term>Ascend 950DT</term>(环境变量本身在 950DT 上生效)。

# 【资料变更】

`docs/zh/api/environment_variable/op_execution/FORCE_OVERFLOW_CHECK.md`(随本 MR 一并修改,见修改方案第 2 点)。

# 【接口变更】

无。

# 【功能验证】

- Ascend910B2 + CANN 9.2.0:`python test/npu/test_check_over_flow.py -v` 6/6 通过,确认非 950 设备覆盖不受影响;
- Ascend950:三个依赖 float_status 算子的用例按 `FLOAT_STATUS_DEVICES` 过滤跳过,环境变量负向用例(子进程,不触算子)正常执行;
- plog 验证(CANN 9.2.0):`ASCEND_GLOBAL_LOG_LEVEL=1` 时可观察到 `aclrtSetStreamOverflowSwitchImpl: successfully execute rtSetStreamOverflowSwitch, flag is 1` INFO 日志,确认文档中开关状态确认方式有效。

See merge request: Ascend/pytorch!46182
2026-09-16 18:08:10 +08:00
wuyouqi1 c01b3d31a2 [sync] PR-38861: feat(asd): dispatch matmul_checksum to V-ABFT op and extend dtype support to bf16/fp32
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46403 merge feat/pta-matmul-checksum-master into master

[sync] PR-38861: feat(asd): dispatch matmul_checksum to V-ABFT op and extend dtype support to bf16/fp32

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

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

- [x] 需求 https://gitcode.com/Ascend/pytorch/issues/4730
- [ ] 问题单
- [ ] issue/工单 
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间是如何交互的,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

本 MR 为 !38861(已合入 v2.7.1:`feat(asd): dispatch matmul_checksum to V-ABFT op and extend dtype support to bf16/fp32`)向 **master** 分支的同步合入,代码内容与 v2.7.1 合入版本一致(cherry-pick,仅按本分支基线消解 `torch_npu/asd/checksum.py` 的上下文差异)。

将 ASD 框架的 matmul checksum 校验从纯 PyTorch 实现升级为「V-ABFT 算子优先 + PyTorch 回退」两级分发,并将支持的 dtype 由 BF16 扩展到 BF16/FP32。修改 `torch_npu/asd/checksum.py` 与 `torch_npu/asd/asd.py`,对外接口 `torch_npu.matmul_checksum(a, b, c)` 的签名、返回值(NPU 侧 bool 标量张量)与懒加载导出均不变。

1. **分发器 `_matmul_checksum`**:输入满足算子契约(2D、bf16/fp32、a/b/c shape 一致)且环境可用时,走 op-plugin 注册的内部算子 `torch.ops.npu._npu_matmul_abft_verify`(V-ABFT 分块校验和算子,方差自适应阈值;不暴露公开 API);否则回落原纯 PyTorch 实现 `_matmul_checksum_py`(完整保留)。3D batched、a/b 混合精度、c 精度低于 a/b 等不满足算子契约的场景自动走回退路径。
2. **可用性探测 `_npu_matmul_abft_verify_available`**:一次微型真实调用探测并缓存(进程内一次)——内部算子未注册(op-plugin 未编入该算子)或 `aclnnMatmulAbftVerify` 符号无法解析(EXEC_NPU_CMD 运行期 dlopen/dlsym,如未安装 vendors 算子包)均判定为不可用,自动回落纯 PyTorch 路径,不改变现有行为。
3. **语义桥接 `_matmul_checksum_abft`**:checksumWeight 取全 1 向量(算子文档推荐配置);e_max 按 dtype 取算子侧推荐值(BF16=0.001,FP32=0.00002);C 矩阵在 python 侧统一 `to(torch.float32)`(算子要求 C 为 float32);comp_row 位流按固定布局转换为 bool,M 非 8 倍数时用缓存掩码屏蔽未定义的 padding 位。
4. **dtype 支持扩展(BF16 → BF16/FP32)**:`_check_matmul_checksum_inputs` 的 dtype 校验由 bf16-only 放宽为 bfloat16/float32。asd.py hook 注入门禁由「a、b 均为 bf16」放宽为「a、b ∈ {bfloat16, float32}」,参数扫描标志 `matmul_with_bf16` 相应更名为 `matmul_dtype_supported`(FP32 模型的 checksum 联动不再被跳过)。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及(本 MR 未修改文档。「checksum 联动仅支持 BF16」的资料同步已在 master 分支单独提交:MR !46144(docs/en 与 docs/zh 的 NPU_ASD_CONFIG.md 及 feature_value_detection.md,BF16 → BF16/FP32),与本 MR 代码行为一致。

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

`torch_npu.matmul_checksum(a, b, c) -> Tensor`(bool 标量)签名与返回值不变。行为变化:
1. 支持的 dtype 由 BF16 扩展为 BF16/FP32(独立调用与 asd.py hook 注入路径一致);
2. 内部实现优先分发至内部算子 `npu._npu_matmul_abft_verify`(不暴露公开 API;依赖 CANN vendors 算子包提供 `aclnnMatmulAbftVerify` 符号),环境缺失或输入不满足算子契约(3D、c 精度低于 a/b 等)时自动回落原纯 PyTorch 实现,调用方无需感知。

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

与 !38861(v2.7.1)验证结论一致,本 MR 为同一变更的分支同步:

测试环境:Atlas A2(910B2)、CANN 9.2.0 + vendors 算子包(libcust_opapi.so 提供 aclnnMatmulAbftVerify)、torch 2.12.0 + 含 _npu_matmul_abft_verify 注册的 torch_npu wheel。

1. **功能分发(20/20 通过)**:BF16/FP32 无检/SDC 检出、M%8∈{1,4,7,0} 边界、多 segment、MOE gate shape、强制回退后行为不变、3D/混合 dtype/c 低精度回退、非连续输入、CPU 张量 TypeError、缓存生效。
2. **ASD hook 冒烟**:checksum_enable 下 bf16/fp32 2D matmul 与 Tensor.matmul 正常注入,3D 注入后走 py 回退;matmul 计算结果不变,干净数据标志位保持 False。
3. **误检率**:gaussian/uniform/scaled × BF16/FP32 × 6 个 LLM shape + 4 个 MOE gate shape(FP32),每组合 1000 次干净 matmul → 误检 0/1000。
4. **检出率**:bit-flip 故障注入(bit1-3 × BF16/FP32)→ 100% 检出。
5. **性能**:checksum 相对 matmul 的增量开销由纯 PyTorch 路径的 BF16≈883% / FP32≈635% 降至 BF16≈310% / FP32≈186%(同 shape 集平均);FP32 大 M(≥1024)场景 16-34%。

UT 说明:随本 MR 同步新增仓内 UT `test/npu/test_matmul_checksum.py`(11 例):
- 输入校验(CPU 张量 TypeError,不依赖算子,所有环境执行);
- py 回退与分发契约(强制回退后的无检/检出、3D/混合 dtype/c 低精度回退、`_abft_path_applicable` 判定);
- 算子路径(干净数据无误检 + 腐化数据检出 + 非连续 b + bf16 输入/fp32 输出混合 dtype,含 M%8∈{1,4,7,0} 边界与多 segment)。
算子路径用例按环境可用性门控:`aclnnMatmulAbftVerify` 经 vendors 算子包分发而非基础 CANN 包,标准 CI 环境无该算子时自动 skip(实测无算子环境 7 pass + 4 skip,有算子环境 11/11 通过)。

![image.png](https://raw.gitcode.com/user-images/assets/7404318/c5aa22ce-2fc4-40b0-b7d4-95f859c98fd8/image.png 'image.png')

![image.png](https://raw.gitcode.com/user-images/assets/7404318/cbb7e785-bcf8-4458-97a1-bc4946d4563f/image.png 'image.png')


See merge request: Ascend/pytorch!46403
2026-09-16 16:23:31 +08:00
wuyouqi1 162d0576e8 docs(asd): update checksum linkage dtype support to bf16/fp32
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!46144 merge docs/asd-checksum-dtype into master

docs(asd): update checksum linkage dtype support to bf16/fp32

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

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

- [x] 需求 https://gitcode.com/Ascend/pytorch/issues/4730
- [ ] 问题单
- [ ] issue/工单
- [ ] 重构优化
- [x] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间是如何交互的,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

ASD 特征值检测的 checksum 联动功能已扩展支持 FP32(对应代码合入 `torch_npu/asd/checksum.py` 与 `torch_npu/asd/asd.py`:matmul checksum 支持 BF16/FP32,`with_checksum` 自动注入 hook 的 dtype 门禁由 BF16 放宽为 BF16/FP32)。同步更新文档中“使用约束”的相关描述:

1. `docs/zh/api/environment_variable/eigenvalue_detection/NPU_ASD_CONFIG.md`:“checksum联动仅支持**BF16**的数据类型”→“仅支持**BF16**或**FP32**的数据类型”。
2. `docs/en/api/environment_variable/NPU_ASD_CONFIG.md`:同步英文表述(only **BF16** → only **BF16** and **FP32**)。
3. `docs/zh/developer_notes/fault_diagnosis/feature_value_detection.md` 与 `docs/en/developer_notes/framework_feature_guide_pytorch/feature_value_detection.md`:约束说明中的同一句同步更新(该文档被 NPU_ASD_CONFIG 环境变量页超链接引用,保持口径一致)。

FP16 无算子侧推荐 e_max,维持不支持,表述保留“仅/only”。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

涉及:本 PR 即资料修改,共 4 个文档文件、每文件 1 行(checksum 联动支持的 dtype 约束 BF16 → BF16/FP32),中英文一致。

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及(无代码改动;`torch_npu.matmul_checksum` 的 dtype 支持扩展由代码 PR 单独合入,本 PR 仅同步资料)。

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

纯文档修改。自验:
1. 中英文 4 个文件已全量检索,无遗漏的“checksum联动仅支持BF16”类表述(`grep -rn "checksum" docs/zh docs/en` 复核)。
2. 修改仅限“使用约束/约束说明”小节的 1 行措辞,不改变文档结构与超链接目标。
3. 与代码行为一致性:`torch_npu/asd/asd.py` 中 `_MATMUL_CHECKSUM_DTYPES = (torch.bfloat16, torch.float32)`,`torch_npu/asd/checksum.py` 校验 bf16/fp32、fp16 TypeError。

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

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


See merge request: Ascend/pytorch!46144
2026-09-16 16:02:55 +08:00
wuyouqi1 e9e5c8e2ee Release 2.14.0
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45390 merge feat/v2.14.0-release into master

Release 2.14.0

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

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

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/1923
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

社区演进和版本升级,参考 !40367(Release 2.13.0),进行 2.14 切分支与出包适配,依赖切换至 torch 2.14.0(stable whl/cpu),并预置 2.15 开发线 requirements 轮转。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

涉及:SUPPORT.md / SUPPORT.en.md、SECURITYNOTE.md 版本信息随本 PR 同步更新

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

`bash ci/build.sh --python=3.10 --torch=2.14.0` 版本校验通过;torch 2.14.0+cpu / torchvision 0.29.0+cpu 已在 whl/cpu stable 通道确认可获取。

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

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


See merge request: Ascend/pytorch!45390
2026-09-04 15:24:31 +08:00
wuyouqi1 c09f0dc50f fix: return base torch.Event instance from _EventProxy
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45313 merge fix/transfer-to-npu-event-proxy-base-instance into master

fix: return base torch.Event instance from _EventProxy

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

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

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/4433
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

1. torch 2.13 起 torch.Stream.record_event 在 C++ 侧以精确类型匹配(Py_TYPE(event) == THPEventClass)校验 event 入参,拒绝子类实例;transfer_to_npu 将 torch.Event 替换为子类代理 _EventProxy,传入 record_event 时校验失败报错。
2. 修改 _EventProxy.__new__:完成 cuda→npu 入参改写后返回基类 torch.Event 实例,使其通过精确类型校验;device 改写逻辑不变,_TorchTypeProxyMeta.__instancecheck__ 保证 isinstance 语义兼容。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

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

环境:Atlas 910B2 / CANN 9.2.0 / torch 2.13.0+cpu / torch_npu 2.13.0rc2 / Python 3.10。使用修改后文件替换 wheel 内同名文件验证:
1. s.record_event(e) 正常执行,返回原生 torch.Event
2. isinstance(e, torch.Event) 为 True
3. torch.Event(device='cuda') 仍正确改写为 npu
4. 带参构造(enable_timing=True)+ record + synchronize 正常
5. 不使用 transfer_to_npu 的场景不受影响

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

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


See merge request: Ascend/pytorch!45313
2026-08-29 15:56:49 +08:00
wuyouqi1 38c1f4290f fix: downgrade internal-format targets to base format on the aclnn path when ALLOW_INTERNAL_FORMAT=disable [master]
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45500 merge fix/aclnn-format-cast-forbid-internal-format-master into master

fix: downgrade internal-format targets to base format on the aclnn path when ALLOW_INTERNAL_FORMAT=disable [master]

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】
- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/4389
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
allow_internal_format=False 时,npu_format_cast 走 aclnn 路径(7302fba05 引入)未检查该开关:目标为内部格式(FRACTAL_NZ/NC1HWC0/FRACTAL_Z)时直接创建私有格式 tensor,绕过了 aclop 分配层守卫(TensorFactories.cpp unsafe_empty_with_format),与 aclop 路径行为不一致,导致 test_allow_internal_format_false_with_npu_format_cast 类用例失败。

修复:在 MaybeUseAclnnNpuFormatCast 的 IsAclnnFormatCastSupported 分支入口增加降级守卫——allow_internal_format=False 且目标为非 base 格式时,将目标降级为 GetBaseFormat 并 TORCH_WARN_ONCE,与 aclop 路径行为对齐。

关键设计:cast_into_existing 豁免——目标格式等于 src tensor 自身格式时不降级(tensor 重载 npu_format_cast_(self, Tensor src) 中该值恒为 True,属预期:该重载语义为将 src 数据写入已存在 tensor,目标格式是既成事实;若降级会破坏 nz_dst.copy_(nd_src) 后 dst 保持 NZ 布局的既有行为)。int 重载经 same-format 早退后该值恒为 False,守卫正常生效。950 的 IsAclnnOnly 分支不经此守卫,不受影响。

注:此前 !39022 曾在 MaybeUseAclnnNpuFormatCast 入口无条件降级(无 cast_into_existing 豁免),因破坏既有行为被 !39259 回退。本 MR 为带豁免的修正版,同步自 v2.7.1 MR !45183。

# 【资料变更】
op-plugin 仓 MR !5771(allow_internal_format 影响范围说明)

# 【接口变更】
不涉及

# 【功能验证】
UT:test/custom_ops/test_npu_format_cast.py TestNpuFormatCastForbidInternalFormat(降级 4 组格式断言 + base 目标不受影响 + 开关开启对照 + copy_ 保持已存在 NZ tensor 格式)

See merge request: Ascend/pytorch!45500
2026-08-29 11:57:14 +08:00
wuyouqi1 741077401b feat: support overflow switch on non-saturation mode via FORCE_OVERFLOW_CHECK
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45378 merge feat/force-overflow-check-master into master

feat: support overflow switch on non-saturation mode via FORCE_OVERFLOW_CHECK

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

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

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

1. AclInterface.cpp: AclrtCreateStreamWithConfig 内联 FORCE_OVERFLOW_CHECK 判断(env=1 且 CANN>=9.1.0 时非饱和 INF_NAN 模式也开启 aclrtSetStreamOverflowSwitch 溢出检测开关,版本不满足时 LOGW 一次并回退原逻辑),为 Inf/NaN 问题提供不影响执行时序的异步定位能力。
2. torch_npu/npu/utils.py: 新增 is_force_overflow_check() 查询接口(仅 npu.utils 命名空间);get_npu_overflow_flag / npu_check_overflow / clear_npu_overflow_flag 三个接口在 force 开启时改走溢出状态标志检测路径。
3. test: test_check_over_flow.py 扩展寄存器级、utils 层及子进程负向环境变量用例;torch_npu_schema.json 登记新 API。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

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

环境:Ascend910B2 + CANN 9.2.0(非饱和 INF_NAN 模式)。
1. 手工能力验证:FORCE_OVERFLOW_CHECK=1 下 fp16 溢出(65504+65504)经溢出状态标志检出(get_npu_overflow_flag=True);npu_check_overflow 检出后自动清除标志(残留=False);正常运算不误报(False)。
2. UT:test/npu/test_check_over_flow.py 6 用例全绿(含 force 生效检出、默认行为回归、版本不匹配告警、非法取值容错)。
3. 性能对比:mm(4096x4096) fp16 开关开启 0.463ms/iter vs 关闭 0.455ms/iter,开销约 +1.8%。
4. 该改动已先合入 v2.7.1 分支(MR !44962,合入 commit b7bb55832),本 MR 为向其他分支的同步。

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

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


See merge request: Ascend/pytorch!45378
2026-08-29 09:16:00 +08:00
wuyouqi1 8d8805334a docs:add FORCE_OVERFLOW_CHECK env var doc
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45308 merge docs/force-overflow-check-master into master

docs:add FORCE_OVERFLOW_CHECK env var doc

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

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

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

新增docs/zh/api/environment_variable/op_execution/FORCE_OVERFLOW_CHECK.md描述环境变量功能。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

新增docs/zh/api/environment_variable/op_execution/FORCE_OVERFLOW_CHECK.md。

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

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

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

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


See merge request: Ascend/pytorch!45308
2026-08-27 16:48:11 +08:00
wuyouqi1 aad3743121 fix: [910B/910_93] fall back to aclop for view tensors in format cast
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45056 merge master-format-cast-view-fallback into master

fix: [910B/910_93] fall back to aclop for view tensors in format cast

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】
- [x] 问题单

# 【修改方案】
1. MaybeUseAclnnNpuFormatCast 非 aclnn-only 分支(910B/910_93)中,新增文件内 IsViewTensor 判定(张量逻辑形状 sizes 与存储 desc 的 base_sizes 不一致即为 view),将原 ND->NZ N×1 列向量窄守卫泛化为通用 sizes 守卫:view(reshape/transpose/slice)直接回退 aclop,恢复 PR 前行为。
2. 非连续 internal 守卫保持原样;同形异构 stride view(shape 一致、stride 不一致)经实测 aclnn 可正确且位级保值处理,不改变其原有路径。
3. aclnn-only(950)分支保持现状,不做未经验证的改动。
4. 回退安全性: aclop 路径尺寸由 torch_npu 侧确定性公式计算(check_size_nonnegative / GuessStorageFormat / GetMemorySize),无垃圾值输入来源。

# 【资料变更】
不涉及

# 【接口变更】
不涉及

# 【功能验证】
- 10×10 格式矩阵(view 场景,100 组合): 修复前 35 个 OOM 组合修复后全部回退 aclop 成功;28 个 aclnn 正常组合不受影响。
- 同形异构 stride view 修复前环境实测: 走 aclnn 正确处理,data_ptr 共享存储,数值位级一致(max_err=0),故守卫仅按 sizes 判定、不改变 stride 场景行为。
- 新增 UT(test_npu_format_cast.py Group 8,5 个用例): reshape / transpose / as_strided 三类 view,覆盖回退行为与数值正确性,适配 910B/910_93。

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


See merge request: Ascend/pytorch!45056
2026-08-21 15:37:48 +08:00
wuyouqi1 e74d0331fe feat: adapt FRACTAL_NZ D2H/printing and serialization UT for Ascend950
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!44320 merge feat/nz2nd-serialization-master into master

feat: adapt FRACTAL_NZ D2H/printing and serialization UT for Ascend950

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

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

- [x] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2629
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
同步 #2629(A5/Ascend950 上 FRACTAL_NZ 私有格式 tensor 的 D2H、打印、序列化适配)至本分支,与 v2.7.1 实现(MR !40289)保持一致。本 PR 内容:

1. `torch_npu/utils/tensor_methods.py`:新增 `_add_repr_patch()`。私有格式(FRACTAL_NZ 系列)tensor 在 `Tensor.__repr__` 中会命中 `_tensor_str` 内部格式守卫(cat/stack),导致打印/D2H 失败;patch 在私有格式时先 `.cpu()` 触发 D2H + format cast,再走原 `__repr__`。在 `_add_tensor_methods()` 末尾注册。
2. `torch_npu/csrc/aten/common/FormatCastKernelNpu.cpp`:`IsAclnnOnly()` 路径下,未显式指定 `customize_dtype` 时,对 `element_size() >= 4` 的类型(fp32/int32)将 `customizeAcltype` 默认置为 `ACL_FLOAT16`(C0=16),与 aclop 路径的历史行为对齐。
3. `test/npu/test_serialization_format.py`:新增 Ascend950 专属用例 `TestSerializationFormatAscend950`(受 `@unittest.skipUnless(IS_ASCEND950, ...)` 门控),覆盖 ND save/load、按 dtype 的 FRACTAL_NZ round-trip、NZ tensor D2H/repr/print;原 `TestSerializationFormat` 在 Ascend950 上跳过。

> 说明:本分支不含 v2.7.1 MR !40289 中的 `__reduce_ex__` 序列化 patch(该 patch 在本分支不需要)。如该 patch 后续需要,将另行评估。

# 【资料变更】
不涉及

# 【接口变更】
不涉及

# 【功能验证】
测试场景:
- A2/A3(非 Ascend950):`TestSerializationFormat` 原用例照常执行,`TestSerializationFormatAscend950` 被 `skipUnless(IS_ASCEND950)` 跳过。
- A5/Ascend950:`TestSerializationFormat` 跳过,执行 `TestSerializationFormatAscend950`:fp16/bf16/int8/int32/int64 的 FRACTAL_NZ save/load round-trip 与 D2H/repr/print。

运行方式:`python test/npu/test_serialization_format.py -v`

新增/变更内容已适配 UT 用例(`test_serialization_format.py`)。本 PR 为跨版本同步,代码已验证在各目标分支干净 apply、无遗留依赖(不含 `_reduce_ex__`);具体执行结果以本分支 CI 流水线为准。

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

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

Fixes #2629
https://gitcode.com/Ascend/pytorch/issues/2629


See merge request: Ascend/pytorch!44320
2026-08-17 17:27:14 +08:00
wuyouqi1 97e976e2e1 fix: gate ACL log via TORCH_NPU_LOGS env var instead of logger level
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!43616 merge personal/master-fix-acl-log-init-order into master

fix: gate ACL log via TORCH_NPU_LOGS env var instead of logger level

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】
- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/3614
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】

PR-40113 的 `g_log_enabled` 通过 `call_once` 在首次 ACL 调用时缓存日志启用状态。但首次 ACL 调用发生在 `libtorch_npu.so` 被 `dlopen` 加载时的 C++ 全局变量初始化阶段(op-plugin 仓 `MlaPrologV3KernelNpuOpApi.cpp:34` 的 `is_cann_version_gte_required = IsGteCANNVersion(...)` → `AclsysGetCANNVersion` → `ACL_CALL_LOG` 宏),远早于 Python 侧 `LogContext::setLogs()` 处理 `TORCH_NPU_LOGS`。此时 `LogContext` 尚未收到任何 log 配置,logger 默认级别为 WARNING,`g_log_enabled` 被永久锁为 false,后续 `setLogs()` 更新级别也无法逆转。

`g_log_enabled` 改为从环境变量直接判断——`getenv("TORCH_NPU_LOGS") != nullptr`,与 `TORCH_NPU_LOGS_FILTER` 一致。环境变量在 `dlopen` 之前已可用,无初始化顺序依赖,`call_once` 首次缓存后热路径仅读 1 bool(与 PR-40113 完全等价)。

仅改 `AclCallDecorator.cpp`,`.h` / 宏 / 调用点不动。

# 【资料变更】

不涉及

# 【接口变更】

不涉及

# 【功能验证】
> 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\
> 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图
unset TORCH_NPU_LOGS_FILTER
unset TORCH_NPU_LOGS
case 1:
export TORCH_NPU_LOGS=acl
export TORCH_NPU_LOGS_FILTER=+aclmdlRICaptureGetInfo,+aclrtSetOpExecuteTimeOut,-aclmdlRICaptureGetInfo
case 2:
export TORCH_NPU_LOGS=acl
export TORCH_NPU_LOGS_FILTER=-aclmdlRICaptureGetInfo
case 3:
export TORCH_NPU_LOGS=acl
export TORCH_NPU_LOGS_FILTER=+aclmdlRICaptureGetInfo
case 4:
export TORCH_NPU_LOGS=acl

python test_acl_log_print.py
以上4个case验证均通过。
![image.png](https://raw.gitcode.com/user-images/assets/7404318/e4f5bdcd-dbb2-41d6-a84c-a4c209c88083/image.png 'image.png')


# 【CheckList】

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


See merge request: Ascend/pytorch!43616
2026-08-03 21:35:48 +08:00
wuyouqi1 f56ddc06ef Release 2.13.0
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!40367 merge feat/v2.14.0-bump into master

Release 2.13.0

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 [版本升级](https://gitcode.com/Ascend/pytorch/issues/1923)
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
社区演进和版本升级,参考 !35503(551d971f Release 2.11.0rc1 / v2.12.0rc1),将依赖切到 2.13.0:

- version.txt: 保持 2.13.0
- requirements.txt: torch==2.13.0
- test/requirements.txt: torchvision==0.28.0

# 【资料变更】
不涉及
# 【接口变更】
不涉及

See merge request: Ascend/pytorch!40367
2026-07-09 19:24:07 +08:00
wuyouqi1 47e43e8f97 [sync] PR-40113: fix: cache log gate inside ShouldLogAclApi (only .cpp)
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!40211 merge sync-pr40113-personal/v2.9.0-26.1.0-fix-once-v2-to-master into master

[sync] PR-40113: fix: cache log gate inside ShouldLogAclApi (only .cpp)

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/40113

### 2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2611

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[82a7567b](https://gitcode.com/Ascend/pytorch/commit/82a7567bc52afd25b7c39b6fd72593b3ac181271)|2026-07-06 14:27:15 +0800 CST|update: 更新文件 AclCallDecorator.cpp<br><br><br>Signed-off-by: wuyouqi1 <wuyouqi1@h-partners.com>|
|[b10db5d1](https://gitcode.com/Ascend/pytorch/commit/b10db5d10d54c2c89894dd2cc2beb46d940eee19)|2026-07-06 09:27:25 +0800 CST|fix(acl-log): wrap isACLGlobalLogOn in try-catch to prevent call_once throw<br><br>isACLGlobalLogOn may throw c10::Error when ASCEND_GLOBAL_LOG_LEVEL<br>is invalid. In old code this was silently caught by the macro's<br>try/catch. Now that InitFilter runs inside call_once, an uncaught<br>throw would prevent the once flag from being set, causing retry<br>on every ACL call -> crash.<br>|
|[50da757b](https://gitcode.com/Ascend/pytorch/commit/50da757b00e197656736ed09a56dd02e58023b70)|2026-07-04 21:07:33 +0800 CST|fix(acl-log): invert g_filter_disabled so TORCH_NPU_LOGS_FILTER enables filter<br><br>Setting TORCH_NPU_LOGS_FILTER now activates Query/Event suppression<br>(matches env name). Previously setting it disabled the filter - inverted<br>semantics. Default (unset) keeps filter off. Orthogonal to TPOT fix.<br>|
|[efe15557](https://gitcode.com/Ascend/pytorch/commit/efe15557c8b6fc6d6beb96f7a894754be6e84ee0)|2026-07-04 20:39:59 +0800 CST|perf(acl-log): cache log gate inside ShouldLogAclApi (only .cpp)<br><br>ACL_CALL_LOG uses ShouldLogAclApi as outer gate; original returns true by<br>default (only filters Query/Event), so the macro enters the ostringstream<br>block on every acl call -> TPOT regression.<br><br>Fold the log-level gate into ShouldLogAclApi via call_once: add g_log_enabled<br>resolved once (PTA acl-logger INFO OR ASCEND global ACL_INFO). Default off -><br>ShouldLogAclApi returns false -> macro short-circuits before ostringstream.<br><br>Only AclCallDecorator.cpp changed. Header macro + 84 call sites untouched.<br>|


See merge request: Ascend/pytorch!40211
2026-07-06 21:50:11 +08:00
wuyouqi1 d467a1364d fix: Fallback npu_format_cast to aclop for non-contig internal-format tensor
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



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

fix: Fallback npu_format_cast to aclop for non-contig internal-format tensor

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

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

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2605
- [ ] 重构优化
- [ ] 资料更新

 # 【修改方案】
  修复 aclnn 路径下,对携带内部格式(非 base 格式)的非连续张量执行 `npu_format_cast` 必然抛 `RuntimeError` 的问题。

  修改内容:
  1. 在 `MaybeUseAclnnNpuFormatCast` 的非 `IsAclnnOnly` 分支中、调用 `aclnnNpuFormatCastCalculateSizeAndFormat` 之前,增加判断:当 `src` 为非 base 格式且非连续时,返回 `useAclnn=false`。
  2. 调用方(`npu_format_cast_` / `_npu_format_cast` 等)在 `useAclnn=false` 时复用既有 aclop 路径 `format_cast_impl_out_npu`,该路径通过 `Identity` 算子 + `InputWithoutContiguous` 显式支持非连续 src,与切换 aclnn 之前的历史行为一致。
  3. 机型分流:guard 位于 `IsAclnnOnly()` 块之后,Ascend950(纯 aclnn、无 aclop)仍走 aclnn 并保留原抛错行为;Ascend910B / 910_93 走 aclop 兜底。
  4. `format_cast_impl_out_npu_aclnn` 入口的 `TORCH_CHECK` 保留,作为 950 的正确行为以及防御性兜底。

  # 【资料变更】
  不涉及

  # 【接口变更】
  不涉及

  # 【功能验证】
  测试场景:非连续内部格式张量(NZ,经 transpose 得到)执行 `npu_format_cast` 到 ND。
  新增/变更 UT(`test/custom_ops/test_npu_format_cast.py`):
  - `test_noncontiguous_with_internal_format_fallback`(`@SupportedDevices(['Ascend910B','Ascend910_93'])`):断言非连续 NZ → ND 成功,输出格式为 ND。
  - `test_noncontiguous_with_internal_format_raises`(`@SupportedDevices(['Ascend950'])`):断言非连续 NZ → ND 抛 `RuntimeError`。

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


See merge request: Ascend/pytorch!40190
2026-07-06 21:01:50 +08:00
wuyouqi1 f12fa5bf96 [sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!39703 merge sync-pr39678-revert/compat-impl-socname-routing-26.1.0-to-master into master

[sync] PR-39678: revert: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/39678

### 2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2561

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[8539c290](https://gitcode.com/Ascend/pytorch/commit/8539c290bd0035b66335b3e92ac28540de8c2805)|2026-06-30 21:08:41 +0800 CST|Revert "feat: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default"<br><br>This reverts commit acebf35339791ea6fcac6f116acdb242a1c09096.<br><br>Removes the _is_ascend950() SoC auto-detection and the SoC-based default-value assignment for TORCH_NPU_USE_COMPATIBLE_IMPL in torch_npu/__init__.py, and drops test_compatible_impl_soc_detect.py.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|


See merge request: Ascend/pytorch!39703
2026-06-30 23:23:07 +08:00
wuyouqi1 764c9005e2 fix: best-effort cleanup in CachingHostAllocator ExpandableSegment::map
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!37600 merge fix/caching-host-allocator-resource-leak into master

fix: best-effort cleanup in CachingHostAllocator ExpandableSegment::map

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

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

- [ ] 需求
- [x] 问题单
- [ ] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2479
- [ ] 重构优化
- [ ] 资料更新

`CachingHostAllocator.cpp` 中 `ExpandableSegment::map()` 异常路径物理内存句柄泄漏,长期运行累积可导致 NPU 内存耗尽。

# 【修改方案】

修改文件:`torch_npu/csrc/core/npu/CachingHostAllocator.cpp`

`ExpandableSegment::map()` 分两阶段为 pinned host 内存分配物理页并映射到虚拟地址空间。两个阶段在异常路径下均存在资源泄漏:

**泄漏点1 — `AclrtMallocPhysical` 返回非 OOM 错误:** 原仅 `ACL_ERROR_RT_MEMORY_ALLOCATION`(OOM) 时清理,其他错误码直接抛异常,`[begin, i)` 已分配句柄未释放。修复:改为 `status != ACL_SUCCESS` 统一清理 `[begin, i)`,OOM 优雅降级 return、其余清理后抛异常;并对失败槽 handle 防御性释放(`if (handle != nullptr) AclrtFreePhysical`)。

**泄漏点2 — `AclrtMapMem` 失败:** 原直接抛异常无回滚,`[begin, end)` 全部句柄未释放、`[begin, i)` 已映射段未 unmap。修复:先 unmap `[begin, i)`,再 free `[begin, end)`,`trimHandles()` 后抛异常。

**为何不自动回收:** map() 抛异常后 segment 对象仍存活、调用链无 try-catch;且失败时无 block 指向泄漏句柄,`empty_cache`/析构均够不到,必须 map() 内立即回滚。

# 【资料变更】

不涉及

# 【接口变更】

不涉及

# 【功能验证】

修改涉及 `ExpandableSegment::map()` 内部异常路径的错误处理逻辑,属于 C++ 层 pinned 内存分配器的防御性修复。正常路径与 OOM 降级行为不变,仅错误路径新增清理/回滚。

**正确性核查(对照 RTS 源码):** OOM 码 `207001` 归属正确(`DRV_ERROR_OUT_OF_MEMORY → RT_ERROR_DRV_OUT_MEMORY → 207001`);对「已分配未映射」handle 直接 `AclrtFreePhysical` 合法不漏;回滚范围互不重叠且回滚后 `handles_` 置 nullopt,结构上无重复释放。

**验证方法:** 正常路径由现有 pinned 内存 UT 覆盖;异常路径需真实硬件 + ACL 失败注入,Python 层无法直接 mock。

# 【CheckList】

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


See merge request: Ascend/pytorch!37600
2026-06-26 17:36:48 +08:00
wuyouqi1 d73fdd62e5 [sync] PR-38402: fix: gate CachingHostAllocator and NPUGraphsUtils with CheckCompatibleImpl to avoid perf regression
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!38808 merge sync-pr38402-fix/hasPrimaryContext-perf-gate-to-master into master

[sync] PR-38402: fix: gate CachingHostAllocator and NPUGraphsUtils with CheckCompatibleImpl to avoid perf regression

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/38402

### 2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2413

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[face8635](https://gitcode.com/Ascend/pytorch/commit/face86355b540d1963ddbbda78c15d1db8226f8d)|2026-06-16 22:07:19 +0800 CST|fix: hoist OptionalDeviceGuard out of compat gate and drop redundant comments<br><br>The OptionalDeviceGuard must outlive the allocation / pointer-attribute<br>calls (RAII), so declare it before the CheckCompatibleImpl() branch in<br>both allocate_host_memory and CachingHostAllocator_isPinned. Previously<br>it was scoped inside the if-block and was destroyed before the<br>allocation/query, defeating the guard on the compat=1 path.<br><br>Also drop the redundant inline comments added by the gate commit.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[0fbd934c](https://gitcode.com/Ascend/pytorch/commit/0fbd934cdfd62c8f1f62def14daa94d76e504bba)|2026-06-12 17:27:38 +0800 CST|fix: gate CachingHostAllocator and NPUGraphsUtils with CheckCompatibleImpl to avoid perf regression<br><br>Commit 3bf50b1b7 (feat: route hasPrimaryContext through ACL API when<br>TORCH_NPU_USE_COMPATIBLE_IMPL=1) replaced fast thread-local device<br>checks (GetLocalDevice < 0 / IsContextInitialized) with<br>getDeviceIndexWithPrimaryContext() + isDeviceCtxActive() that acquire<br>recursive_mutex on every call. This caused operator dispatch perf<br>regression even when TORCH_NPU_USE_COMPATIBLE_IMPL is NOT set.<br><br>Fix: gate the new CUDA-aligned path behind CheckCompatibleImpl().<br>When env var is not set (default), keep the original fast thread-local<br>path. Only when TORCH_NPU_USE_COMPATIBLE_IMPL=1, use the new<br>getDeviceIndexWithPrimaryContext + OptionalDeviceGuard path.<br><br>Affected files:<br>- CachingHostAllocator.cpp: allocate_host_memory<br>- CachingHostAllocator.h: CachingHostAllocator_isPinned<br>- NPUGraphsUtils.h: currentStreamCaptureStatus<br><br>NPUFunctions.cpp (isDeviceCtxActive gate + getDeviceIndexWithPrimaryContext)<br>is unchanged — only called through MaybeSetDevice/MaybeExchangeDevice<br>which are lower frequency and the gate logic is correct there.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|


See merge request: Ascend/pytorch!38808
2026-06-18 14:17:26 +08:00
wuyouqi1 951e46ca8f [sync] PR-38376: fix: remove the deprecated get_cann_version() in _optim.py to reduce import time
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!38508 merge sync-pr38376-fix/lazy-load-optim-get-cann-version-to-master into master

[sync] PR-38376: fix: remove the deprecated get_cann_version() in _optim.py to reduce import time

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/38376

### 2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2370

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[145499ed](https://gitcode.com/Ascend/pytorch/commit/145499ed5f0269813a74fa30788c4150adaae936)|2026-06-13 11:19:22 +0800 CST|perf: remove obsolete CANN 8.0 blacklist from _optim.py<br><br>torch_npu 2.7.1 requires CANN >= 8.2, so the CANN 8.0 blacklist<br>and cann_version check are no longer reachable. Removing them<br>eliminates the _get_cann_version() call from import path.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[30946b2a](https://gitcode.com/Ascend/pytorch/commit/30946b2ad16668acd04e5b73981eb0b67c2e0a63)|2026-06-12 16:25:50 +0800 CST|perf: simplify _optim.py to use C++ get_cann_version and remove obsolete CANN 8.0 blacklist<br><br>- Replace collect_env.get_cann_version() (os.walk over ASCEND_HOME_PATH,<br>  ~250ms) with torch_npu._C._get_cann_version() (C++ impl, ~0.014ms)<br>- Remove obsolete CANN 8.0.x blacklist entries (8.0.RC1/RC2, T1~T52)<br>- Remove global state (_cann_version, _device_name) in favor of local<br>  variables - no caching needed with <1ms C++ API calls<br>- Remove collect_env dependency entirely<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|


See merge request: Ascend/pytorch!38508
2026-06-16 09:17:01 +08:00
wuyouqi1 b778af9a96 [sync] PR-37189: fix: unskip test_reductions test case in test_multiprocessing_api.py
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!37685 merge sync-pr37189-fix/test_reductions_multiprocess-to-master into master

[sync] PR-37189: fix: unskip test_reductions test case in test_multiprocessing_api.py

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/37189

### 2. Original pull request related issue(s):

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[908d8d41](https://gitcode.com/Ascend/pytorch/commit/908d8d41e4f61d1394c8f573dfaab92d5c900384)|2026-06-05 11:03:14 +0800 CST|test(reductions): add skipUnless decorator for driver version check<br><br>Add @unittest.skipUnless to test_reductions to skip the test when<br>driver version < 25.3, as NPU IPC reduce/rebuild requires driver >= 25.3.<br><br>Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com><br>|
|[e00edd92](https://gitcode.com/Ascend/pytorch/commit/e00edd928dbfbb87892dc56fc852f4977e1678ee)|2026-06-04 15:25:45 +0800 CST|Revert "test(reductions): add debug logging for NPU IPC reduce/rebuild path"<br><br>This reverts commit d1ee69dcbd891a48ced0f34471547b25710a5679.<br>|
|[d1ee69dc](https://gitcode.com/Ascend/pytorch/commit/d1ee69dcbd891a48ced0f34471547b25710a5679)|2026-06-03 15:53:58 +0800 CST|test(reductions): add debug logging for NPU IPC reduce/rebuild path<br><br>Add torch_npu.multiprocessing.reductions debug logger to trace:<br>- _npu_reduce_tensor: which branch (NPU/meta/CPU), _share_npu_() return values, cache key format<br>- rebuild_npu_tensor: cache lookup key format, HIT/MISS, _new_shared_npu() result, rebuilt tensor data<br>- test worker: rebuild_fn args, rebuilt tensor data in child process<br><br>Purpose: verify cross-process IPC reduce→rebuild takes correct code path.<br><br>Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com><br>|
|[5f3fe781](https://gitcode.com/Ascend/pytorch/commit/5f3fe781e2d2afd62f261e68ecf5ea15c4bc3145)|2026-06-01 10:50:39 +0800 CST|fix(test): send tensor data as list to avoid resource_sharer connection reset<br><br>CPU tensor pickled through Queue uses rebuild_storage_fd which requires<br>resource_sharer Unix socket. Child process exits before parent can<br>establish connection. Convert tensor to plain list before sending.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[4fbab4e4](https://gitcode.com/Ascend/pytorch/commit/4fbab4e426da26946aebb3f714f5b2a1b416e944)|2026-06-01 10:37:09 +0800 CST|test(multiprocessing): use explicit reduce/rebuild in cross-process test<br><br>Address review comments:<br>- Worker now receives (rebuild_fn, args) from reduce_tensor, explicitly<br>  calls rebuild_fn(*args) to reconstruct tensor<br>- Main process puts reduced_npu handle instead of raw tensor<br>- Main process compares result with original npu_tensor instead of<br>  hardcoding expected values in worker<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[8b7be3c2](https://gitcode.com/Ascend/pytorch/commit/8b7be3c23c6cde75a478073759d8fad22168879f)|2026-05-30 17:28:58 +0800 CST|fix(test): synchronize NPU before putting tensor to Queue<br><br>Child process received all zeros because NPU write cache was not<br>flushed before IPC handle export. torch.npu.synchronize() ensures<br>data is written to physical memory before cross-process transfer.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[89953a94](https://gitcode.com/Ascend/pytorch/commit/89953a94f9ee27a0179950d1b97b093c4e9bbef4)|2026-05-30 17:21:03 +0800 CST|fix(test): add diagnostic info for NPU tensor mismatch<br><br>Include actual received data in failure message to diagnose IPC issue.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[ad27f741](https://gitcode.com/Ascend/pytorch/commit/ad27f741aedc98faed4a2e45d4bc3cc344239aa3)|2026-05-30 17:07:25 +0800 CST|fix(test): capture child process error in test_reductions<br><br>Worker now sends result status back via Queue instead of relying on<br>exitcode. This propagates actual error messages from the child process<br>for diagnosis.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[9b2a8888](https://gitcode.com/Ascend/pytorch/commit/9b2a888831efd8a29ea7d407dccfbc004720c337)|2026-05-30 16:42:56 +0800 CST|fix(test): fix NPU tensor cross-process transfer direction<br><br>Main process puts tensor to Queue, child process gets and validates.<br>Matches torch_npu multiprocessing test patterns. Use Queue instead of<br>SimpleQueue to avoid resource_sharer lifecycle issues.<br><br>Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com><br>|
|[db6b692b](https://gitcode.com/Ascend/pytorch/commit/db6b692bfec2f968ad5bd6da5cee223251f0b15e)|2026-05-30 16:37:35 +0800 CST|fix(test): remove unsupported timeout from SimpleQueue.get()<br><br>SimpleQueue.get() does not accept timeout parameter in Python 3.10.<br><br>Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com><br>|
|[c0820b36](https://gitcode.com/Ascend/pytorch/commit/c0820b36457cf1759e9de6dc48f16698b9ff21c2)|2026-05-30 16:20:09 +0800 CST|test(multiprocessing): merge multiprocess rebuild into test_reductions<br><br>Combine NPU multiprocess rebuild into test_reductions instead of a separate<br>test case. CPU tensor uses same-process rebuild, NPU tensor uses cross-process<br>rebuild via Queue.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[aedaa436](https://gitcode.com/Ascend/pytorch/commit/aedaa436bdbcb30e36713d7cd6cbfea9f1f5df94)|2026-05-30 16:17:22 +0800 CST|fix(test): move worker function to module level for spawn pickle<br><br>spawn mode cannot pickle local functions. Move _npu_tensor_worker to<br>module top level.<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|
|[0e37c8f8](https://gitcode.com/Ascend/pytorch/commit/0e37c8f8015060dcffbef58b9377cd380d7e7687)|2026-05-30 10:40:03 +0800 CST|test(multiprocessing): fix test_reductions and add multi-process NPU test<br><br>- Remove @unittest.skip decorator from test_reductions<br>- Remove unreliable same-process NPU tensor reconstruction assertion<br>  (IPC handles cannot be imported in the same process that exported them)<br>- Add test_reductions_multiprocess to validate cross-process NPU tensor<br>  sharing via Queue, aligned with PyTorch upstream CUDA test pattern<br><br>Co-Authored-By: Claude <noreply@anthropic.com><br>|


See merge request: Ascend/pytorch!37685
2026-06-05 15:51:17 +08:00
wuyouqi1 742a134905 [sync] PR-36922: test(npu): add primary context isolation tests
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!37657 merge sync-pr36922-pr_test_primary_ctx-to-master into master

[sync] PR-36922: test(npu): add primary context isolation tests

Created-by: ascend-ds-bot
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: 
### 1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/36922

### 2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2262

### 3. Original pull request related commit(s):
| Sha | Datetime | Message |
|---|---|---|
|[96d3670f](https://gitcode.com/Ascend/pytorch/commit/96d3670fbbd48ed0f8382e6574034fd4cbc0a5af)|2026-06-04 20:33:43 +0800 CST|test: remove debug prints and set ACL_OP_INIT_MODE=1 for CI compatibility<br><br>Remove diagnostic debug output from test_npu_primary_ctx.py.<br>Set ACL_OP_INIT_MODE=1 in __main__ block to prevent eager ACL op<br>initialization from implicitly creating device 0 primary context,<br>which is the root cause of CI failure (CI sets ACL_OP_INIT_MODE=0).<br><br>Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com><br>|
|[c1b3d598](https://gitcode.com/Ascend/pytorch/commit/c1b3d59814b4e399e2f1772bbddcebbd5176d51f)|2026-06-04 15:17:53 +0800 CST|Revert "test: add diagnostic test files for CI primary ctx failure"<br><br>This reverts commit eb1e36319452da71fdbedbeec22bb72d3e9fea1b.<br>|
|[78a9c425](https://gitcode.com/Ascend/pytorch/commit/78a9c425df0631f052b0c0520de812cd6b34118d)|2026-06-04 15:17:33 +0800 CST|Revert "test: pure ctypes test — only AclrtGetPrimaryCtxState, no torch_npu wrapper"<br><br>This reverts commit f0a4f1e267c380ea9d74bc4a42a9354b385c1b80.<br>|
|[f0a4f1e2](https://gitcode.com/Ascend/pytorch/commit/f0a4f1e267c380ea9d74bc4a42a9354b385c1b80)|2026-06-03 16:55:35 +0800 CST|test: pure ctypes test — only AclrtGetPrimaryCtxState, no torch_npu wrapper<br>|
|[eb1e3631](https://gitcode.com/Ascend/pytorch/commit/eb1e36319452da71fdbedbeec22bb72d3e9fea1b)|2026-06-03 15:10:00 +0800 CST|test: add diagnostic test files for CI primary ctx failure<br><br>- test_npu_primary_ctx_swap_order.py: swap hasPrimaryContext(1)/(0) check order<br>- test_npu_primary_ctx_ctypes.py: ctypes direct call AclrtGetPrimaryCtxState<br>|
|[ba9d8b6c](https://gitcode.com/Ascend/pytorch/commit/ba9d8b6c23c92df4f7c971d0059eb8a5fa709f93)|2026-06-02 14:55:15 +0800 CST|test(npu): add primary context isolation tests with debug diagnostics<br><br>Add test_npu_primary_ctx.py to verify NPU primary context isolation<br>across devices, with debug logging for CI environment diagnosis.<br><br>Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com><br>|


See merge request: Ascend/pytorch!37657
2026-06-05 14:24:47 +08:00
wuyouqi1 da8d764ca5 feat: route hasPrimaryContext through ACL API when TORCH_NPU_USE_COMPATIBLE_IMPL=1
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!36923 merge feat/hasPrimaryContext-api-consistency-master into master

feat: route hasPrimaryContext through ACL API when TORCH_NPU_USE_COMPATIBLE_IMPL=1

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->
  # 【合入来源】
  - [x] 需求
  - [ ] 问题单
  - [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2073
  - [ ] 重构优化
  - [ ] 资料更新

  # 【修改方案】
  1. isDeviceCtxActive() 增加 CheckCompatibleImpl() 门控:TORCH_NPU_USE_COMPATIBLE_IMPL=1 时走 hasPrimaryContext (ACL API), 否则走 used_devices 本地缓存。MaybeSetDevice / MaybeExchangeDevice 均调用 isDeviceCtxActive,连锁受益。

  2. 新增 c10_npu::getDeviceIndexWithPrimaryContext(): 对齐 c10::cuda::getDeviceIndexWithPrimaryContext,遍历 device,找到已有 primary context 的设备。

  3. CachingHostAllocator (allocate_host_memory / isPinned) 对齐 CUDA: 用 getDeviceIndexWithPrimaryContext() + OptionalDeviceGuard

  4. currentStreamCaptureStatus() 对齐 CUDA (CUDAGraphsUtils.cuh): 用 isDeviceCtxActive(current_device()) 替代 IsContextInitialized()。

  5. NPUHooksInterface::hasPrimaryContext / Python 绑定 _npu_hasPrimaryContext
     简化为直接调用 isDeviceCtxActive (门控已统一在此)。


  修改文件:
  - torch_npu/csrc/core/npu/NPUHooksInterface.cpp
  - torch_npu/csrc/core/npu/NPUFunctions.cpp
  - torch_npu/csrc/core/npu/NPUFunctions.h
  - torch_npu/csrc/core/npu/NPUGraphsUtils.h
  - torch_npu/csrc/core/npu/CachingHostAllocator.cpp
  - torch_npu/csrc/core/npu/CachingHostAllocator.h

  # 【资料变更】
  不涉及

  # 【接口变更】
  不涉及

  # 【功能验证】
- 单卡环境 + env unset(自动跳过,无需多卡)
- 多卡环境 + env unset(三个用例PASS)

```
(/root/my_envs/tmp_ffmh) [root@A5-PoD-A04-CPU3 0128]# export ASCEND_RT_VISIBLE_DEVICES=3
(/root/my_envs/tmp_ffmh) [root@A5-PoD-A04-CPU3 0128]# python test_npu_primary_ctx.py 
sss
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK (skipped=3)
(/root/my_envs/tmp_ffmh) [root@A5-PoD-A04-CPU3 0128]# unset ASCEND_RT_VISIBLE_DEVICES
(/root/my_envs/tmp_ffmh) [root@A5-PoD-A04-CPU3 0128]# python test_npu_primary_ctx.py 
...
----------------------------------------------------------------------
Ran 3 tests in 30.733s

OK
```

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


See merge request: Ascend/pytorch!36923
2026-05-28 11:52:59 +08:00
wuyouqi1 940bc04890 feat: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!36752 merge sync-pr36613-master into master

feat: detect Ascend950 SoC for TORCH_NPU_USE_COMPATIBLE_IMPL default

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

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

  - [x] 需求
  - [ ] 问题单
  - [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2091
  - [ ] 重构优化
  - [ ] 资料更新

  # 【修改方案】

  `TORCH_NPU_USE_COMPATIBLE_IMPL` 环境变量默认值根据 SoC 型号自动检测:

  1. 新增 `_is_ascend950()` 函数,通过 Python ctypes 直接调用 CANN 库 `libascendcl.so` 的 `aclrtGetSocName()` 接口获取 SoC 名称,判断是否以 `"Ascend950"` 开头
  2. 仅在环境变量 `TORCH_NPU_USE_COMPATIBLE_IMPL` **未设置**时触发检测:
     - Ascend950:默认值 `"1"`(启用 ACL API 路径,与 GPU 行为对齐)
     - 其他型号:默认值 `"0"`(保持缓存路径,原有行为不变)
  3. 用户显式设置环境变量时,保持用户值不变,不做检测覆盖

  修改文件:
  - `torch_npu/__init__.py` — SoC 检测 + 环境变量默认值
  - `test/test_compatible_impl_soc_detect.py`(新增)— 4 个测试用例

  # 【资料变更】

  不涉及。

  # 【接口变更】

  不涉及。`TORCH_NPU_USE_COMPATIBLE_IMPL` 环境变量接口不变,仅调整未设置时的默认值逻辑。无新增/变更客户面 API。

  # 【功能验证】

  测试场景:

  1. **Ascend950 机器,环境变量未设置**:`CheckCompatibleImpl()` 返回 `true`
  2. **非 Ascend950 机器(如 Ascend910B),环境变量未设置**:`CheckCompatibleImpl()` 返回 `false`
  3. **任意机器,用户显式设置 `TORCH_NPU_USE_COMPATIBLE_IMPL=1`**:用户值优先
  4. **无 CANN 环境**:ctypes 加载失败,`_is_ascend950()` 返回 `False`,默认 `"0"`,不抛异常

  自动化测试:`python3 test/test_compatible_impl_soc_detect.py`
  - A2场景:
  ```
  (wyq) [root@localhost HPC]# python test_compatible_impl_soc_detect.py -v
test_compatible_impl_default_matches_soc (__main__.TestCompatibleImplSocDetect) ... skipped 'TORCH_NPU_USE_COMPATIBLE_IMPL was pre-set in environment'
test_compatible_impl_respects_user_value (__main__.TestCompatibleImplSocDetect) ... ok
test_is_ascend950_matches_cpp_soc_version (__main__.TestCompatibleImplSocDetect) ... ok
test_is_ascend950_returns_bool (__main__.TestCompatibleImplSocDetect) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK (skipped=1)
  ```
  - A5场景:
  ```
  (/root/my_envs/tmp_ffmh) [root@A5-PoD-A04-CPU3 0128]# python test_compatible_impl_soc_detect.py -v 
test_compatible_impl_default_matches_soc (__main__.TestCompatibleImplSocDetect) ... skipped 'TORCH_NPU_USE_COMPATIBLE_IMPL was pre-set in environment'
test_compatible_impl_respects_user_value (__main__.TestCompatibleImplSocDetect) ... ok
test_is_ascend950_matches_cpp_soc_version (__main__.TestCompatibleImplSocDetect) ... ok
test_is_ascend950_returns_bool (__main__.TestCompatibleImplSocDetect) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK (skipped=1)
  ```
  # 【CheckList】

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

See merge request: Ascend/pytorch!36752
2026-05-27 11:04:10 +08:00
weixin_47897441 4e8486ef72 feat: add force mode and affinity mask logging for CPU affinity binding
Co-authored-by: weixin_47897441<wuyouqi1@h-partners.com>



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

feat: add force mode and affinity mask logging for CPU affinity binding

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

  # 【合入来源】

  > 容器核隔离场景下 CPU_AFFINITY_CONF 绑核失效问题修复

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

  ---

  # 【修改方案】

  **文件**:`torch_npu/csrc/core/npu/NPUAffinityController.cpp`

  1. **新增 `force:0/1` 选项**:`force:0`(默认)检测冲突时跳过绑核;`force:1` 跳过冲突检测,强制绑核(适用于容器核隔离场景)。
  2. **增强冲突日志**:冲突时输出 WARNING 日志,含请求核列表和冲突核列表。
  3. **修复 off-by-one**:冲突检测循环条件由 `i < end` 修正为 `i <= end`。

  ---

  # 【接口变更】

  不涉及。`CPU_AFFINITY_CONF` 新增可选字段 `force:0/1`,向后兼容。

  ---

  使用 `taskset -c` 模拟容器 cgroup 核限制,4 个用例全部通过:

  | 用例 | 配置 | taskset | 预期 | 结果 |
  |------|------|---------|------|------|
  | 1: force:0 默认范围冲突 | `mode:2,force:0,npu0:0-3` | 0-7 | 其他设备默认范围冲突,跳过绑核,mask=ff | PASS |
  | 2: force:1 强制绑核 | `mode:2,force:1,npu0:0-3` | 0-7 | 跳过冲突检测,强制绑核,mask=f | PASS |
  | 3: off-by-one 修复 | `mode:2,force:0,npu0:0-8` | 0-7 | 核8冲突检测(i<=end),跳过绑核,mask=ff | PASS |
  | 4: WARNING 日志验证 | `mode:2,force:0,npu0:0-3` | 0-7 | 输出含冲突原因和 force:1 建议的 WARNING | PASS |

  ---

  # 【CheckList】

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

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

See merge request: Ascend/pytorch!34480
2026-04-27 20:34:08 +08:00
weixin_47897441 4af07350e2 fix: handle zero tensor src to avoid null pointer in aclrtMemcpy
Co-authored-by: weixin_47897441<wuyouqi1@h-partners.com>



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

fix: handle zero tensor src to avoid null pointer in aclrtMemcpy

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

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

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列

1. 在 `torch_npu/csrc/aten/ops/op_api/CopyKernelOpApi.cpp` 的 `copy_()` 函数中添加 `src._is_zerotensor()` 检查
2. 当 src 为 ZeroTensor(惰性零张量,`data_ptr()` 为 null)时,直接调用 `self.zero_()` 而不走 `aclrtMemcpy`,避免 null 指针传入触发 error code 100000
3. 与 PyTorch 原生 `aten/src/ATen/native/Copy.cpp` 中的 `_is_zerotensor()` 处理保持一致

根因:`_efficientzerotensor` 算子 NPU 未实现回退 CPU,CPU 侧使用 `ZeroTensorAllocator` 创建惰性零张量不分配实际内存,`data_ptr()` 为 null,`copy_()` 直接调用 `aclrtMemcpy` 时传入 null src 指针报错。

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写不涉及

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写不涉及

不涉及

# 【功能验证】
> 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤

1. 测试用例:`test_linearize_composition_grad_npu_float32`
2. 测试命令:`python test_eager_transforms.py -v -k test_linearize_composition_grad_npu_float32`
3. 验证结果:PASSED(修复前报 RuntimeError: aclrtMemcpy error code 100000)

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

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


See merge request: Ascend/pytorch!33381
2026-04-13 15:16:48 +08:00
weixin_47897441 9df03631be fix(hccl): rename HCCL_COMM_HCCL_QOS_CONFIG_NOT_SET to HCCL_COMM_QOS_CONFIG_NOT_SET
Co-authored-by: weixin_47897441<wuyouqi1@h-partners.com>



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

fix(hccl): rename HCCL_COMM_HCCL_QOS_CONFIG_NOT_SET to HCCL_COMM_QOS_CONFIG_NOT_SET

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

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

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

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

- Rename constant HCCL_COMM_HCCL_QOS_CONFIG_NOT_SET to HCCL_COMM_QOS_CONFIG_NOT_SET
- Remove redundant HCCL prefix for consistency with other config constants

# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

# 【接口变更】
> 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及

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

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

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


See merge request: Ascend/pytorch!33134
2026-04-08 09:31:24 +08:00
weixin_47897441 cf61a0b80a [fix]add torch_npu._C._npu_hasPrimaryContext
Co-authored-by: weixin_47897441<wuyouqi1@h-partners.com>



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

[fix]add torch_npu._C._npu_hasPrimaryContext

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

# 【合入来源】
- [ ] 需求
- [ ] 问题单
- [x] issue/工单 社区issue需适配_npu_hasPrimaryContext
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
1、 新增hasPrimaryContext接口,用于查询指定设备的主上下文是否已激活,对齐CUDA能力,支持上下文状态感知的资源管理。
2、在ACL接口层封装aclrtGetPrimaryCtxState,C++层实现hasPrimaryContext函数,并通过Python扩展暴露_npu_hasPrimaryContext,同时注册到Dynamo跟踪规则以支持图模式调用。

# 【资料变更】
> 不涉及

# 【接口变更】
> 不涉及

# 【功能验证】
python test_cuda_primary_ctx.py -v -k test_copy 自验证通过
![image.png](https://raw.gitcode.com/user-images/assets/7404318/b33b6582-f6bc-4373-93c2-468d65fc1335/image.png 'image.png')
# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

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

See merge request: Ascend/pytorch!32454
2026-03-27 17:35:04 +08:00
weixin_47897441 7ecdce1eb3 [fix]add getMemoryFraction attribute for torch._C
Co-authored-by: weixin_47897441<wuyouqi1@h-partners.com>



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

[fix]add getMemoryFraction attribute for torch._C

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

# 【合入来源】
> (如有)请关联需求文档/issue链接

- [ ] 需求
- [ ] 问题单
- [x] issue/工单 : torch._C不支持_npu_getMemoryFraction
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
1、NPUCachingAllocator中新增getMemoryFraction方法及虚函数声明,Module中注册_npu_getMemoryFraction绑定;
2、在torch_npu.npu和torch_npu.npu.memory中实现并暴露get_per_process_memory_fraction接口,支持指定设备,调用底层 C++ 接口;
3、新增该接口的测试用例;
4、将该接口及底层Cpp绑定加入Dynamo追踪规则,完成适配。

# 【资料变更】
>  不涉及

# 【接口变更】
> 否

# 【功能验证】
> UT用例测试通过:python test_npu.py -v -k test_get_per_process_memory_fraction
![image.png](https://raw.gitcode.com/user-images/assets/7404318/dd7cc124-b3b1-47d1-ab15-ad3798b33115/image.png 'image.png')
# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

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


See merge request: Ascend/pytorch!30619
2026-03-12 19:44:32 +08:00