Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert generator in Sampler back to lazy construction #63646

Closed
wants to merge 8 commits into from

Conversation

ejguan
Copy link
Contributor

@ejguan ejguan commented Aug 20, 2021

Fixes #63609

Stack from ghstack:

Differential Revision: D30451774

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Aug 20, 2021

🔗 Helpful links

💊 CI failures summary and remediations

As of commit d506308 (more details on the Dr. CI page):


  • 1/1 failures introduced in this PR

1 failure not recognized by patterns:

Job Step Action
GitHub Actions linux-bionic-py3.6-clang9 / test (default, 1, 2, linux.2xlarge) Unknown 🔁 rerun

This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@ejguan ejguan linked an issue Aug 20, 2021 that may be closed by this pull request
@ejguan ejguan requested a review from vfdev-5 August 20, 2021 14:34
@ejguan
Copy link
Contributor Author

ejguan commented Aug 20, 2021

@ejguan has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

@ejguan looks good to me. Thanks. I left a nit comment.

torch/utils/data/sampler.py Outdated Show resolved Hide resolved
@ejguan ejguan changed the title Convert Sampler back to lazily construction Convert generator in Sampler back to lazily construction Aug 20, 2021
@ejguan ejguan changed the title Convert generator in Sampler back to lazily construction Convert generator in Sampler back to lazy construction Aug 20, 2021
@ejguan ejguan added this to In Progress in torch.utils.data via automation Aug 20, 2021
@ejguan ejguan added the module: dataloader Related to torch.utils.data.DataLoader and Sampler label Aug 20, 2021
@ejguan
Copy link
Contributor Author

ejguan commented Aug 20, 2021

@ejguan has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

else:
yield from torch.randperm(n, generator=self.generator).tolist()
yield from torch.randperm(n, generator=self._gen).tolist()
self._gen = None
Copy link
Contributor

Choose a reason for hiding this comment

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

This will badly impact all currently running iterators, consider creating two individual iterators in test and yielding them in random order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emmm, you are right. So, we have to create iter-local generator. But, then it goes back to the problem that we can not serialize the state of this iter-local generator anymore. Then, when we resume from snapshotting, we can not really fast forward the state of generator, have to iterate over it to the certain amount of iterations.

Copy link
Contributor

@VitalyFedyunin VitalyFedyunin left a comment

Choose a reason for hiding this comment

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

Not going to work as intended for two or more iterators created from same object.

 a = Sampler()
 i1 = iter(a)
 i2 = iter(a)

@VitalyFedyunin
Copy link
Contributor

Return new generator from __iter__ to keep states shared

class Sampler():
    def __init__(self):
        self.iterators = {}
        pass

    def __iter__(self):
        # Everything is by pointer here
        state_dict = {}
        generator = iter(self.generator(state_dict))
        self.iterators[generator] = state_dict
        return generator

    def generator(self, state_dict):
        state_dict['gen'] = len(self.iterators)
        for i in range(10):
            yield i

s = Sampler()
i = iter(s)
i2 = iter(s)

print(i)
print(i2)

for d in i:
    print(d)

print(s.iterators)

@ejguan
Copy link
Contributor Author

ejguan commented Aug 20, 2021

Return new generator from __iter__ to keep states shared

Yeah, that's also something in my mind. Will update PR.

ejguan added a commit that referenced this pull request Aug 20, 2021
ghstack-source-id: 81dde49f141f887850c1f2c87e68d35337e585a3
Pull Request resolved: #63646
ejguan added a commit that referenced this pull request Aug 20, 2021
ghstack-source-id: 17692bbbb9a7b9922e17426a543b2d0f6b59e1bf
Pull Request resolved: #63646
@ejguan
Copy link
Contributor Author

ejguan commented Aug 20, 2021

@ejguan has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Fixes #63609


- Revert #63026 
  - Sampler is expected to be re-seeded if user specify seed before each epoch
  - Can not attach generator to self with `__iter__` because multiple iterators will ruin the use case
- Add tests to prevent the same case for different Samplers



Differential Revision: [D30451774](https://our.internmc.facebook.com/intern/diff/D30451774)

[ghstack-poisoned]
Fixes #63609


- Revert #63026 
  - Sampler is expected to be re-seeded if user specify seed before each epoch
  - Can not attach generator to self with `__iter__` because multiple iterators will ruin the use case
- Add tests to prevent the same case for different Samplers
- Keep same functionality introduced in #63026 that user can serialize the Sampler.



Differential Revision: [D30451774](https://our.internmc.facebook.com/intern/diff/D30451774)

[ghstack-poisoned]
@ejguan
Copy link
Contributor Author

ejguan commented Sep 29, 2021

@ejguan has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Fixes #63609


- Revert #63026 
  - Sampler is expected to be re-seeded if user specify seed before each epoch
  - Can not attach generator to self with `__iter__` because multiple iterators will ruin the use case
- Add tests to prevent the same case for different Samplers


Differential Revision: [D30451774](https://our.internmc.facebook.com/intern/diff/D30451774)

[ghstack-poisoned]
@ejguan
Copy link
Contributor Author

ejguan commented Sep 29, 2021

I split the original PR into two PRs.
This PR would revert Sampler back to previous state that the RNG is created lazily.

The another PR provide a new feature for potential users to serialize the iter-local generator.

@ejguan
Copy link
Contributor Author

ejguan commented Sep 29, 2021

@ejguan has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@NivekT NivekT left a comment

Choose a reason for hiding this comment

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

LGTM

torch.utils.data automation moved this from In Progress to Done Sep 30, 2021
ejguan added a commit to ejguan/pytorch that referenced this pull request Sep 30, 2021
Summary:
Pull Request resolved: pytorch#63646

Fixes pytorch#63609

Test Plan: Imported from OSS

Reviewed By: NivekT

Differential Revision: D30451774

Pulled By: ejguan

fbshipit-source-id: 550d77494326446d1a42b5da0559e0d384c47413
@facebook-github-bot facebook-github-bot deleted the gh/ejguan/81/head branch October 4, 2021 14:24
malfet pushed a commit that referenced this pull request Oct 8, 2021
Summary:
Pull Request resolved: #63646

Fixes #63609

Test Plan: Imported from OSS

Reviewed By: NivekT

Differential Revision: D30451774

Pulled By: ejguan

fbshipit-source-id: 550d77494326446d1a42b5da0559e0d384c47413
maglimedia pushed a commit to gtedemo/demo-codespace-pytorch that referenced this pull request Oct 9, 2021
ghstack-source-id: bf4b2badb725d51e23b570f8c43e903fc6d9bb71
Pull Request resolved: pytorch/pytorch#63646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla signed module: dataloader Related to torch.utils.data.DataLoader and Sampler
Projects
Development

Successfully merging this pull request may close these issues.

Sampler should be seeded lazily
5 participants