Repository navigation
Fix deepcopy bottleneck in state_dict() - #8387
Merged
Merged
Conversation
yuxin00j
force-pushed
the
fix-deepcopy-state-dict
branch
5 times, most recently
from
August 4, 2026 05:51
2ebcc1a to
ec9bb04
Compare
This commit implements explicit state_dict serialization across the IterableDataset iterators, replacing the previous fallback logic that relied on copy.deepcopy() of python generators, which raised Pickling errors.
yuxin00j
force-pushed
the
fix-deepcopy-state-dict
branch
from
August 4, 2026 05:53
ec9bb04 to
a28ff3e
Compare
deepcopy bottleneck in IterableDataset.state_dict() during shuffledeepcopy bottleneck in state_dict()
zhixiangli
reviewed
Aug 4, 2026
…with TypeError on unsupported types
zhixiangli
reviewed
Aug 19, 2026
| return set_seed_recursively(ex_iterable) | ||
|
|
||
|
|
||
| _PRIMITIVE_TYPES = (int, float, bool, str, bytes, type(None)) |
| """Fast acyclic copy of state_dict data structures, bypassing slow pickle/deepcopy introspection.""" | ||
| # Use type() instead of isinstance() for 2x faster type checks | ||
| state_type = type(state) | ||
| if state_type in _PRIMITIVE_TYPES: |
There was a problem hiding this comment.
nit: perform this primitive type check last, before the else statement.
| return state | ||
| elif state_type is list: | ||
| return [ | ||
| v if type(v) in _PRIMITIVE_TYPES else _fast_copy(v) |
There was a problem hiding this comment.
nit: simplify to [_fast_copy(v) for v in state]; the same applies to dict and tuple.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #8393
This PR significantly resolves a major CPU bottleneck in
IterableDatasetdataloading whenbuffer_sizeandmax_buffer_input_shardsare > 1.The Bug:
Currently,
_BaseExamplesIterable.state_dict()falls back to performing a full recursivecopy.deepcopy(self._state_dict).In nested iterator chains (like
BufferShuffledExamplesIterablewrappingCyclingMultiSourcesExamplesIterablewrapping multipleArrowExamplesIterableshards), thestate_dict()is queried and updated on every yielded row.Because
copy.deepcopyrelies on Python'spicklemechanics for object introspection and memoization, it incurs significant overhead. Worse,pickleoften crashes when it encounters unpicklable objects like generators. Whenmbisis scaled up and the dictionary size grows, the deepcopy completely blocks the CPU, hiding underneath the shuffle buffer processing.The Fix:
We replaced the expensive
deepcopy()logic with a dynamic, lightweight_fast_copy()helper directly inside_BaseExamplesIterable.state_dict().Because dataset
state_dicts strictly consist of standard collections and primitive state objects (and importantly, contain no circular references),_fast_copy()manually recursively walksdictandliststructures. For any other object, it natively returns the object (or uses its.copy()method if available), bypassingpickleanddeepcopyentirely.This allows us to dynamically serialize state without explicitly overriding
state_dictin every single dataset subclass, restoring maintainability and drastically improving speed.Impact:
We measured workloads using a high number of concurrent shards (
max_buffer_input_shards=10):This single PR shaves ~38 seconds of pure CPU overhead off the workload, generating a 36% overall speedup, entirely restoring the expected linear scaling of shuffle buffers when reading across many shards!