PyTorch Dynamo 修复了对预创建生成器的集合操作以提升 CPython 测试兼容性
英文摘要
This PR addresses two issues in PyTorch Dynamo when using pre-existing generator objects in set methods. First, it introduces a test-only config (`enable_compile_time_generator_exhaustion`) that exhausts the generator at compile time and guards its identity with an ID_MATCH guard, avoiding a graph break. Second, it fixes set polyfills (union, difference, intersection, symmetric_difference) that previously iterated non-set operands multiple times, silently dropping elements from one-shot iterables; they now materialize operands exactly once. The `set_isdisjoint` loop order was also corrected to match CPython's lazy, short-circuiting behavior. These changes remove six expected-failure sentinels from the CPython test suite under `PYTORCH_TEST_WITH_DYNAMO=1`.
中文摘要
该 PR 解决了 PyTorch Dynamo 在集合方法中使用预创建生成器对象的两个问题。首先,引入了一个仅用于测试的配置(`enable_compile_time_generator_exhaustion`),在编译时消耗生成器并通过 ID_MATCH 守卫其标识,从而避免图断。其次,修复了集合多态方法(union、difference、intersection、symmetric_difference)之前多次迭代非集合操作数的问题,这会导致一次性可迭代对象的元素静默丢失;现在它们会提前精确地物化一次操作数。`set_isdisjoint` 的循环顺序也已修正,以匹配 CPython 的惰性短路行为。这些更改移除了 CPython 测试套件在 `PYTORCH_TEST_WITH_DYNAMO=1` 下的六个预期失败标记。
关键要点
Dynamo now supports pre-existing generators in set operations through compile-time exhaustion with ID_MATCH guard (test-only config).
Dynamo 现在通过编译时消耗和 ID_MATCH 守卫支持在集合操作中使用预创建生成器(测试专用配置)。
Set polyfills materialize one-shot iterables exactly once, preventing silent element loss when generators are used as operands.
集合多态方法精确地物化一次性可迭代对象一次,避免了生成器作为操作数时元素的静默丢失。
set_isdisjoint loop order was fixed to match CPython's lazy short-circuiting, while removing six CPython test expected-failure sentinels.
修正了 set_isdisjoint 的循环顺序以匹配 CPython 的惰性短路行为,并移除了六个 CPython 测试预期失败标记。