Developer Tools

PyTorch Dynamo fixes set() keyword arg bug causing silent errors

A subtle bug in PyTorch's Dynamo compiler silently accepted invalid set() calls, returning empty sets instead of errors.

Deep Dive

PyTorch's Dynamo compiler, which accelerates PyTorch models by tracing and compiling Python code, had a subtle compatibility bug in its handling of set() and frozenset() constructors. In CPython, calling set(a=1) (keyword argument) or set().__init__(a=1) directly raises a TypeError because these constructors do not accept keyword arguments. However, under Dynamo, the keyword argument check was placed inside an early-return block that only executed when no positional arguments were supplied. This meant that set(a=1) with zero positional args bypassed the check entirely and silently created an empty set, breaking Python's language contract. The bug also affected frozenset and subclass constructors, potentially leading to hard-to-debug silent failures in Dynamo-compiled code.

The fix, submitted as PR #189051 by guilhermeleobas, simply moves the keyword argument validation (raising TypeError) before the positional argument count logic in both the call_set and call_frozenset functions. This reuses the existing error handling path already used for the case of more than one positional argument – no new error routing was needed. The change ensures that set() and frozenset() constructors behave identically to CPython in all cases: valid constructions (set(), set(iterable), and the >1-positional-args TypeError) remain unchanged, while invalid keyword arguments now correctly raise TypeError. The fix was tested against CPython 3.13's test_set.py (623 tests passed) and Dynamo's own test_sets.py, all passing with no new issues. Interestingly, the PR was authored with the help of an AI assistant, highlighting the growing role of AI in code maintenance.

Key Points
  • Bug: Dynamo silently returned empty set for set(a=1) instead of raising TypeError, diverging from CPython behavior.
  • Fix: Moved keyword argument check before positional count logic in call_set and call_frozenset (PR #189051).
  • Validation: Passed 623 CPython tests + Dynamo test_sets.py; no behavior change for valid set() calls.

Why It Matters

Ensures PyTorch Dynamo faithfully preserves Python semantics, preventing silent data corruption in compiled models.

📬 Get the top 10 AI stories daily