pytorch
module_error
ai_generated
true
RuntimeError: Tracing failed: Recursive trace of function 'forward' is not allowed
ID: pytorch/torchscript-recursive-trace
85%Fix Rate
87%Confidence
1Evidence
2023-11-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| pytorch>=2.0.0 | active | — | — | — |
Root Cause
TorchScript tracing detected a recursive call to the traced function, which is not supported in graph-based tracing; the model uses dynamic control flow or self-referential layers.
generic中文
TorchScript 跟踪检测到对已跟踪函数的递归调用,这在基于图的跟踪中不受支持;模型使用了动态控制流或自引用层。
Official Documentation
https://pytorch.org/docs/stable/jit.html#tracingWorkarounds
-
90% success Refactor the model to avoid recursion: replace recursive calls with iterative loops using `torch.jit.script` and explicit type annotations. Example: change a recursive RNN to use `torch.nn.RNN` or a loop over time steps.
Refactor the model to avoid recursion: replace recursive calls with iterative loops using `torch.jit.script` and explicit type annotations. Example: change a recursive RNN to use `torch.nn.RNN` or a loop over time steps.
-
80% success Use `torch.jit.script` with `@torch.jit.script` decorator and annotate all tensor types. If recursion is unavoidable, use `torch.jit.ignore` on the recursive helper function.
Use `torch.jit.script` with `@torch.jit.script` decorator and annotate all tensor types. If recursion is unavoidable, use `torch.jit.ignore` on the recursive helper function.
-
75% success Export the model using ONNX instead of TorchScript, as ONNX may handle certain recursive patterns via explicit unrolling.
Export the model using ONNX instead of TorchScript, as ONNX may handle certain recursive patterns via explicit unrolling.
中文步骤
重构模型以避免递归:使用 `torch.jit.script` 和显式类型注释,用迭代循环替换递归调用。例如:将递归 RNN 改为使用 `torch.nn.RNN` 或时间步循环。
使用带有 `@torch.jit.script` 装饰器的 `torch.jit.script` 并注释所有张量类型。如果递归不可避免,则在递归辅助函数上使用 `torch.jit.ignore`。
使用 ONNX 而不是 TorchScript 导出模型,因为 ONNX 可能通过显式展开处理某些递归模式。
Dead Ends
Common approaches that don't work:
-
90% fail
Suppressing the error does not fix the underlying issue; the traced model may produce incorrect results.
-
85% fail
Tracing unrolls loops but still cannot handle recursive calls within the traced function.
-
70% fail
Script may fail with the same recursion error if the model is not scriptable; it requires explicit type annotations.