PyTorch has added support for serializing functools.partial objects, which resolves a previous issue where traced mask_mod callables reused stale values. This enhancement ensures that bound tensors within functools.partial objects are correctly handled during serialization and deserialization, allowing for accurate replay with new bound tensors.
| Attribute | Value |
|---|---|
| Released by | PyTorch |
| Release date | |
| What it is | Support for serializing functools.partial objects. |
| Who it is for | Developers using PyTorch for machine learning models. |
| Where to get it | PyTorch GitHub repository (trunk branch). |
| Price | Free and open-source. |
- PyTorch now supports serialization of
functools.partialobjects. - This update addresses issues with stale values in traced
mask_modcallables. - The change ensures correct handling of bound tensors during serialization.
- It reconstructs
functools.partialduring unflattening. - The original closure equality behavior remains unchanged.
- PyTorch’s new feature enables proper serialization of
functools.partialobjects. - This prevents traced callables from using outdated tensor values.
- The update extracts and reconstructs bound function, arguments, and keywords.
- It maintains consistent closure equality behavior.
- The fix includes regressions for simple and recursive partials.
What is functools.partial serialization support?
functools.partial serialization support in PyTorch allows functools.partial objects to be correctly saved and loaded, ensuring their internal state is preserved.
Previously, functools.partial stored its bound state outside the closure, leading to hidden tensors and stale values during tracing [Source event]. This update addresses this by enabling proper serialization and deserialization of these objects [Source event].
What is new vs the previous version?
The new version of PyTorch now correctly serializes functools.partial objects, which was not fully supported before.
| Feature | Previous Version | New Version |
|---|---|---|
functools.partial serialization |
Bound state hidden, reused stale values [Source event]. | Bound func/args/keywords extracted and reconstructed [Source event]. |
| Traced callable replay | Reused stale values for bound tensors [Source event]. | Replayed with new bound tensors [Source event]. |
| Closure equality behavior | Not yet disclosed. | Original behavior unchanged [Source event]. |
How does functools.partial serialization work?
functools.partial serialization works by extracting its bound function, arguments, and keywords, then reconstructing it during deserialization.
BlockMask._flatten()lifts closure-captured values into pytree leaves [Source event].- This allows traced
mask_modcallables to be replayed with new bound tensors [Source event]. - The serialization path extracts the bound function, arguments, and keywords from
functools.partial[Source event]. - During unflattening, the
functools.partialobject is reconstructed [Source event]. - The original closure equality behavior remains unchanged [Source event].
Benchmarks and evidence
The update includes focused regressions to validate the fix for simple partials, recursive partials, and wrapper equality on stripped partials [Source event].
| Test Case | Result | Source |
|---|---|---|
| Simple partials | Regression tests added | Source event |
| Recursive partials | Regression tests added | Source event |
| Wrapper equality on stripped partials | Regression tests added | Source event |
Who should care?
Builders
Builders developing machine learning models with PyTorch should care about this update.
This fix ensures that models using functools.partial will serialize and deserialize correctly, preventing unexpected behavior [Source event]. It simplifies the process of saving and loading complex model components.
How to use this feature today
To use this feature, developers should update their PyTorch installation to the latest trunk branch version.
The functionality is integrated into PyTorch’s serialization mechanisms, so no explicit API calls are required beyond standard saving and loading procedures.
Risks, limits, and myths
- Myth: This update completely changes how all Python functions are serialized.
- Reality: This update specifically targets
functools.partialobjects within PyTorch’s serialization context [Source event].
- Reality: This update specifically targets
- Risk: Compatibility issues with older serialized models.
- Mitigation: The update aims for backward compatibility by preserving original closure equality behavior [Source event].
- Limit: The fix primarily addresses issues related to bound tensors in
mask_modcallables [Source event].- Context: It may not resolve all possible serialization complexities for arbitrary Python objects.
FAQ
- What problem does this PyTorch update solve?
This PyTorch update solves the problem of
functools.partialobjects reusing stale values in tracedmask_modcallables during serialization [Source event]. - When was this PyTorch feature released?
This PyTorch feature was released on [Source event].
- Which part of PyTorch benefits from this serialization support?
The
BlockMask._flatten()method and tracedmask_modcallables benefit from this serialization support [Source event]. - Does this change affect how PyTorch handles closures?
No, this change keeps the original closure equality behavior unchanged [Source event].
- What kind of tests were added for this feature?
Focused regression tests were added for simple partials, recursive partials, and wrapper equality on stripped partials [Source event].
- Is this update available in a stable PyTorch release?
This update is available in the PyTorch trunk branch [Source event].
- Who authored this PyTorch update?
This PyTorch update was authored with Claude and Codex [Source event].
- What is
functools.partialin Python?functools.partialallows for partial application of a function, creating a new callable with some arguments pre-filled.
Glossary
functools.partial- A Python function from the
functoolsmodule that allows for partial application of a function, creating a new callable with some arguments pre-filled. - Serialization
- The process of converting an object into a format that can be stored or transmitted and reconstructed later.
- Traced callable
- A function or method that has been processed by a tracing mechanism, often to optimize execution or analyze behavior.
- Bound tensor
- A tensor that is associated with or captured by a function or object’s state.
- Closure
- A function object that remembers values from its enclosing lexical scope even if that scope is no longer present.