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

Add custom dataloader support with Lite #10279

Merged
merged 22 commits into from
Nov 1, 2021

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Oct 31, 2021

What does this PR do?

This PR enables the following Custom DataLoader not to crash.

This would work

class CustomDataLoader(DataLoader):
    def __init__(self, value: int, *args, **kwargs):
        super().__init__(range(value), *args, **kwargs)
class LiteWithCustomDataLoader(LightningLite):
    def run(self):
        # This doesn't fail as the context manager would save all the arguments provided
        # to the dataloaders.
        dataloader = CustomDataLoader(2, batch_size=2)
        self.setup_dataloaders(dataloader)

LiteWithCustomDataLoader().run()

Would we failed as the value has been captured.

dataloader = CustomDataLoader(2, batch_size=2)
lite_dataloader = lite.setup_dataloaders(dataloader)

This would enable to remove this ugliness from PyTorch Geometric: https://github.com/pyg-team/pytorch_geometric/blob/master/torch_geometric/loader/dataloader.py#L74 if they use LightningLite.

This would be added to Lightning in another PR.

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

@github-actions
Copy link
Contributor

Build Error! No Linked Issue found. Please link an issue or mention it in the body using #<issue_id>

@tchaton tchaton self-assigned this Oct 31, 2021
@github-actions
Copy link
Contributor

Build Error! No Linked Issue found. Please link an issue or mention it in the body using #<issue_id>

@tchaton tchaton added this to the v1.5 milestone Oct 31, 2021
Copy link
Member

@ethanwharris ethanwharris left a comment

Choose a reason for hiding this comment

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

LGTM, small comment

pytorch_lightning/lite/wrappers.py Show resolved Hide resolved
@mergify mergify bot added the ready PRs ready to be merged label Nov 1, 2021
@awaelchli awaelchli added feature Is an improvement or enhancement fabric lightning.fabric.Fabric labels Nov 1, 2021
Copy link
Contributor

@kaushikb11 kaushikb11 left a comment

Choose a reason for hiding this comment

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

LGTM!

@tchaton tchaton enabled auto-merge (squash) November 1, 2021 17:57
@tchaton tchaton merged commit facaff9 into master Nov 1, 2021
@tchaton tchaton deleted the make_lite_dataloader_an_iterator branch November 1, 2021 18:33
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

pytorch_lightning/lite/lite.py Show resolved Hide resolved
pytorch_lightning/lite/lite.py Show resolved Hide resolved
pytorch_lightning/lite/lite.py Show resolved Hide resolved
pytorch_lightning/lite/wrappers.py Show resolved Hide resolved
pytorch_lightning/lite/wrappers.py Show resolved Hide resolved
pytorch_lightning/lite/wrappers.py Show resolved Hide resolved
pytorch_lightning/lite/wrappers.py Show resolved Hide resolved
super().__init__(**dl_kwargs)
super().__init__()
self.__dict__.update(getattr(dataloader, "__dict__", {}))
self._dataloader = dataloader
self._device = device

@property
def device(self) -> Optional[torch.device]:
return self._device

def __iter__(self) -> Union[Iterator[Any], Generator[Any, None, None]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this get possibly overridden by the __dict__.update above?

Copy link
Contributor

Choose a reason for hiding this comment

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

the __* methods don't get affected by direct update to __dict__

carmocca added a commit that referenced this pull request Nov 5, 2021
carmocca added a commit that referenced this pull request Nov 5, 2021
@carmocca carmocca mentioned this pull request Nov 5, 2021
10 tasks
@awaelchli
Copy link
Contributor

@carmocca We can accumulate these smaller things like typing in #10297 and make separate PRs where needed as discussed in #10329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fabric lightning.fabric.Fabric feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants