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

- **ID:** `pytorch/fx-graph-tracing-non-tensor`
- **领域:** pytorch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| torch>=1.8.0 | active | — | — |
| torch>=2.0.0 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — FX tracer executes the code symbolically; exceptions are propagated as tracer errors. (90% 失败率)
- **** — This flag doesn't exist; FX always traces shapes. (95% 失败率)
