pytorch
runtime_error
ai_generated
true
运行时错误:torch.compile:函数 'forward' 的重新编译次数达到上限(100 次)。考虑使用 dynamic=True 或减少张量形状变化。
RuntimeError: torch.compile: Recompilation limit reached (100 recompilations) for function 'forward'. Consider using dynamic=True or reducing tensor shape variability.
ID: pytorch/compile-recompilation-limit
90%修复率
84%置信度
1证据数
2024-04-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| torch>=2.0 | active | — | — | — |
| torch<=2.5.1 | active | — | — | — |
根因分析
torch.compile 的守卫机制在张量形状变化时触发重新编译,经过 100 次重新编译后停止,以防止无限循环或性能下降。
English
torch.compile's guard mechanism triggers recompilation when tensor shapes change, and after 100 recompilations, it stops to prevent infinite loops or performance degradation.
官方文档
https://pytorch.org/docs/stable/compile.html解决方案
-
Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.
-
Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length.
无效尝试
常见但无效的做法:
-
Disabling torch.compile entirely to avoid recompilation
60% 失败
This removes the performance benefits of compilation, which may be critical for large models.
-
Setting torch._dynamo.config.recompile_limit = 0 to disable the limit
90% 失败
This can lead to infinite recompilation loops, causing severe performance degradation or crashes.