PyTorch Dynamo Fixes Dictionary Key Ordering Bug After Pop/Reinsert
A subtle dict ordering mismatch in PyTorch's compiler now behaves like standard Python.
PyTorch’s Dynamo compiler tracks instance-dict mutations via an insertion-ordered dictionary keyed by attribute name. When a key was popped (marked as deleted) and later re-added, Dynamo reused the original slot instead of appending the key at the end, causing obj.__dict__ ordering to diverge from standard CPython behavior. This bug affected any Python code relying on dict insertion order after pop+reinsert operations—a pattern common in dynamic attribute tracking.
The fix, authored with AI assistance, drops the stale DeletedVariable entry before performing the re-store, so the key correctly re-inserts at the end of the ordering. It specifically targets INSTANCE_DICT mutations in Dynamo’s store_instance_dict_attr. Tests pass against CPython’s “splittable” dict behavior test and PyTorch’s own dictionary tests. This ensures that PyTorch-compiled code maintains consistent, predictable dictionary semantics, critical for reproducibility in ML models and data pipelines.
- Bug occurred in PyTorch Dynamo where pop+reinsert of a dict key did not update insertion order correctly.
- Dynamo reused the old slot for the re-inserted key instead of appending it to the end, breaking CPython dict order guarantees.
- Fix drops the stale DeletedVariable entry; tests pass including CPython's test_splittable_pop and PyTorch's own dict tests.
Why It Matters
Ensures PyTorch's JIT compiler produces correct, Python-standard dict ordering for reliable model execution.