trunk/56c382923d5efe129737693860b2a5b85b618cc3: Support functools.partial mask_mod serialization (#180999)
Closure-captured tensors in partials now serialize correctly in PyTorch
PyTorch has merged a critical fix (PR #180999) addressing serialization of functools.partial callables used within the BlockMask._flatten() method. The core issue: when a mask_mod callable is traced with functools.partial, its bound state (func, args, keywords) is stored outside the standard __closure__ attribute. This hidden state meant that during serialization, the traced callable would reuse stale tensor values instead of properly replaying with new bound tensors. The fix handles functools.partial in the same serialization path by explicitly extracting its bound func, args, and keywords, then reconstructing the partial during the unflatten step. This ensures the original closure equality behavior remains unchanged.
The PR includes focused regression tests covering simple partials, recursive partials, and wrapper equality on stripped partials. The change was authored with assistance from Claude and Codex, and approved by PyTorch contributor aorenste. This fix resolves GitHub issue #179825, which likely manifested as incorrect behavior in models using partial-based mask modifications during trace replay. For developers working with PyTorch's attention mechanisms or custom masking, this ensures more reliable serialization of complex callable patterns.
- Fixes serialization of functools.partial callables in BlockMask._flatten()
- Extracts bound func/args/kwargs for proper replay with new tensors
- Adds regression tests for simple, recursive, and equality-checked partials
Why It Matters
Ensures reliable serialization of complex callable patterns in PyTorch, critical for attention masking reproducibility.