pytorch runtime_error ai_generated true

RuntimeError:FX追踪失败:符号追踪期间'NoneType'对象没有属性'shape'

RuntimeError: FX tracing failed: 'NoneType' object has no attribute 'shape' during symbolic tracing

ID: pytorch/fx-graph-tracing-non-tensor

其他格式: JSON · Markdown 中文 · English
78%修复率
83%置信度
1证据数
2023-11-20首次发现

版本兼容性

版本状态引入弃用备注
torch>=1.8.0 active
torch>=2.0.0 active

根因分析

在torch.fx符号追踪期间,函数或方法返回了None而不是张量,导致追踪器在访问.shape属性时失败。

English

During torch.fx symbolic tracing, a function or method returned None instead of a tensor, causing the tracer to fail when accessing .shape attribute.

generic

官方文档

https://pytorch.org/docs/stable/fx.html#limitations

解决方案

  1. Ensure all functions return tensors, use torch.zeros(1) as placeholder for None cases: if x is None: return torch.zeros(1)
  2. Decorate the problematic function with @torch.fx.wrap to skip tracing that part
  3. Use torch.jit.trace instead of torch.fx.symbolic_trace for models with dynamic control flow

无效尝试

常见但无效的做法:

  1. 90% 失败

    FX tracer executes the code symbolically; exceptions are propagated as tracer errors.

  2. 95% 失败

    This flag doesn't exist; FX always traces shapes.