# Why is 'sampler.set\_epoch(epoch)' needed for DistributedSampler?

**URL:** <https://discuss.pytorch.org/t/why-is-sampler-set-epoch-epoch-needed-for-distributedsampler/149672>\
**Category:** distributed\
**Created:** [April 20, 2022, 5:22pm UTC](https://discuss.pytorch.org/t/why-is-sampler-set-epoch-epoch-needed-for-distributedsampler/149672 "2022-04-20T17:22:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mesllo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mesllo/32/48260_2.png) [@mesllo](https://discuss.pytorch.org/u/mesllo)\
**Post date:** [April 20, 2022, 5:22pm UTC](https://discuss.pytorch.org/t/why-is-sampler-set-epoch-epoch-needed-for-distributedsampler/149672/1 "2022-04-20T17:22:51Z")

</div>

I’ve seen various examples using `DistributedDataParallel` where some implement the `DistributedSampler` **and** also set `sampler.set_epoch(epoch)` for every epoch in the train loop, and some that just skip this entirely. Why is this, and is it really needed for the distributed training to execute correctly?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 21, 2022, 5:02am UTC](https://discuss.pytorch.org/t/why-is-sampler-set-epoch-epoch-needed-for-distributedsampler/149672/2 "2022-04-21T05:02:41Z")

</div>

Based on the [docs](https://pytorch.org/docs/stable/data.html#torch.utils.data.distributed.DistributedSampler) it’s necessary to use `set_epoch` to guarantee a different shuffling order:

> In distributed mode, calling the `set_epoch()` method at the beginning of each epoch **before** creating the `DataLoader` iterator is necessary to make shuffling work properly across multiple epochs. Otherwise, the same ordering will be always used.
