提交记录/
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
共 2 个文件变更+36-1
Mtest/npu/test_tensor.py+19-0
@@ -352,6 +352,25 @@ class TestTensor(TestCase):
352 x = torch.randn(4, 10, device=device, dtype=torch.float32)352 x = torch.randn(4, 10, device=device, dtype=torch.float32)
353 return mode.from_tensor(x)353 return mode.from_tensor(x)
354 354 
355+ def test_faketensor_repr(self, device="npu"):
356+ # A FakeTensor claims device npu but has no NPUStorageImpl; the
357+ # __repr__ patch must skip the get_npu_format probe for it.
358+ faketensor = self._make_fake_npu_tensor(device)
359+ self.assertIn("FakeTensor", repr(faketensor))
360+ 
361+ def test_faketensor_repr_under_dispatch_logging(self, device="npu"):
362+ # Dispatch logging formats FakeTensor args via repr(); the nested
363+ # get_npu_format probe used to segfault before the C++ guard.
364+ import logging
365+ from torch._subclasses.fake_tensor import FakeTensorMode
366+ try:
367+ torch._logging.set_logs(all=logging.DEBUG)
368+ with FakeTensorMode(allow_non_fake_inputs=True):
369+ x = torch.randn(4, 10, device=device, dtype=torch.float32)
370+ self.assertIn("FakeTensor", repr(x.softmax(-1)))
371+ finally:
372+ torch._logging.set_logs(all=logging.WARNING)
373+ 
355 def test_get_npu_format_faketensor_guarded(self, device="npu"):374 def test_get_npu_format_faketensor_guarded(self, device="npu"):
356 faketensor = self._make_fake_npu_tensor(device)375 faketensor = self._make_fake_npu_tensor(device)
357 with self.assertRaises(Exception):376 with self.assertRaises(Exception):
Mtorch_npu/_init/patches/npu_patches.py+17-1

评论