Skip to content

Conversation

@aelavender
Copy link
Contributor

@aelavender aelavender commented Nov 18, 2025

Partially fixes #1513

StatefulDistributedSampler has epoch-dependent behaviour (the sampling order depends on epoch), so we need to save and restore the correct epoch.

When used with StatefulDataLoader this is mostly an edge case, as users are expected to explicitly call set_epoch before each epoch. But the order matters here: if the user forgets to call set_epoch() or calls __iter__() before set_epoch(), things will be incorrect. By putting the epoch in state we can avoid these mistakes.

See also #1516.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 18, 2025
@aelavender aelavender changed the title [WIP] StatefulDistributedSampler add epoch to state_dict [WIP][SDL] StatefulDistributedSampler add epoch to state_dict Nov 18, 2025
@aelavender aelavender changed the title [WIP][SDL] StatefulDistributedSampler add epoch to state_dict [SDL] StatefulDistributedSampler add epoch to state_dict Nov 19, 2025
@aelavender aelavender marked this pull request as ready for review November 19, 2025 02:10
@meta-codesync
Copy link

meta-codesync bot commented Nov 19, 2025

@aelavender has imported this pull request. If you are a Meta employee, you can view this in D87398440.

self.next_yielded = None

def __iter__(self):
print(f"Calling __iter__... {self.yielded=} {self.next_yielded=}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip the print ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, will remove

def load_state_dict(self, state_dict: Dict[str, Any]) -> None:
if self._EPOCH not in state_dict:
raise ValueError("Invalid state_dict")
self.set_epoch(state_dict[self._EPOCH])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If users are used to calling set_epoch in a certain in the main loop, then doing self.set_epoch(..) on load_state_dict might interfere with that ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user is trying to set_epoch to something other than what was previously saved, then they are likely going to get repeated indices in the current epoch, which seems like broken behaviour. Perhaps we should warn in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug with loading state for SamplerWrapper + StatefulDistributedSampler

2 participants