fix: 添加对C++操作不可用时的弱引用张量回退处理

This commit is contained in:
2026-02-11 00:17:14 +08:00
parent 43a2ed2f47
commit 5534f2fa41

View File

@@ -728,9 +728,14 @@ def weak_ref_tensor(tensor: Any) -> Any:
Create a weak reference to a tensor. Create a weak reference to a tensor.
The new tensor will share the same data as the original tensor, The new tensor will share the same data as the original tensor,
but will not keep the original tensor alive. but will not keep the original tensor alive.
Falls back to returning the tensor as-is if the C++ op is unavailable.
""" """
if isinstance(tensor, torch.Tensor): if isinstance(tensor, torch.Tensor):
return torch.ops._C_ascend.weak_ref_tensor(tensor) if hasattr(torch.ops, '_C_ascend') and hasattr(
torch.ops._C_ascend, 'weak_ref_tensor'):
return torch.ops._C_ascend.weak_ref_tensor(tensor)
# Fallback: return tensor directly (no weak ref, minor memory overhead)
return tensor
else: else:
return tensor return tensor