PyTorch Dynamo 增加对 collections.deque.__init__ 的支持
英文摘要
PyTorch Dynamo previously lacked support for collections.deque.__init__, causing graph breaks when re-initializing an existing deque. This commit adds handling in DequeVariable.call_method by mirroring CPython's deque_init: it resets maxlen, clears the deque, and extends it with the provided iterable. The implementation also validates maxlen as a non-negative integer, matching CPython's error behavior. This change removes an expected-failure sentinel for CPython 3.13's test_basics, which now passes. The fix improves Dynamo's coverage of Python built-in types, reducing friction for tracing code that uses deques.
中文摘要
PyTorch Dynamo 之前不支持 collections.deque.__init__,导致重新初始化现有 deque 时发生图断(graph break)。该提交在 DequeVariable.call_method 中添加了处理逻辑,通过模拟 CPython 的 deque_init:重置 maxlen、清空 deque 并用提供的可迭代对象扩展。同时验证 maxlen 为非负整数,以匹配 CPython 的错误行为。此更改移除了 CPython 3.13 中 test_basics 的预期失败标记,现在测试通过。此修复增强了 Dynamo 对 Python 内置类型的覆盖,减少了跟踪使用 deque 的代码时的障碍。
关键要点
collections.deque.__init__ was unsupported in Dynamo, causing graph breaks
Dynamo 原本不支持 collections.deque.__init__,导致图断
New handling mirrors CPython deque_init: resets maxlen, clears, and extends from iterable
新增逻辑模拟 CPython 的 deque_init:重置 maxlen、清空并用迭代器扩展
Maxlen validation enforces non-negative integer, raising correct errors
maxlen 验证要求非负整数,抛出正确异常
Removes expected-failure sentinel for CPython 3.13 test_basics, which now passes
移除 CPython 3.13 的 test_basics 预期失败标记,测试通过
Improves Dynamo compatibility for Python code using deques
提升 Dynamo 对使用 deque 的 Python 代码的兼容性