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

Cast on host instead of IPU when using precision=16 #13880

Merged
merged 19 commits into from
Jul 28, 2022
Merged

Cast on host instead of IPU when using precision=16 #13880

merged 19 commits into from
Jul 28, 2022

Conversation

hmellor
Copy link
Contributor

@hmellor hmellor commented Jul 27, 2022

What does this PR do?

Removes the cast to half from the forward pass and adds it to the host side pre-processing.

In overloading batch_to_device it also prevents data.to(device), which is not currently necessary for IPU.

Fixes #13828

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 minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the 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 github-actions bot added the pl Generic label for PyTorch Lightning package label Jul 27, 2022
src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
src/pytorch_lightning/strategies/ipu.py Show resolved Hide resolved
src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
@awaelchli awaelchli added strategy: ipu (removed) bug Something isn't working labels Jul 27, 2022
src/pytorch_lightning/strategies/ipu.py Show resolved Hide resolved
src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
Comment on lines +257 to +259
# We don't call `super().batch_to_device` because `data.to(device)` is not
# currently necessary for IPUs. The movement of data from host<->IPU is
# currently handled by PopTorch.
Copy link
Contributor

Choose a reason for hiding this comment

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

if that's the case, we need to do a follow-up for users to let them know that some of the hooks won't work with IPUs, like transfer_batch_to_device, ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this something you could do? I don't know the codebase well enough to know all the implications.

@codecov
Copy link

codecov bot commented Jul 28, 2022

Codecov Report

Merging #13880 (b9ca3d9) into master (b37e466) will increase coverage by 26%.
The diff coverage is 8%.

❗ Current head b9ca3d9 differs from pull request most recent head daf9949. Consider uploading reports for the commit daf9949 to get more accurate results

@@            Coverage Diff            @@
##           master   #13880     +/-   ##
=========================================
+ Coverage      49%      75%    +26%     
=========================================
  Files         332      332             
  Lines       26010    26810    +800     
=========================================
+ Hits        12728    20078   +7350     
+ Misses      13282     6732   -6550     

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

Thank you for working on this!

src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
tests/tests_pytorch/deprecated_api/test_remove_2-0.py Outdated Show resolved Hide resolved
tests/tests_pytorch/accelerators/test_ipu.py Outdated Show resolved Hide resolved
@carmocca carmocca added this to the pl:1.6.x milestone Jul 28, 2022
@carmocca carmocca requested a review from rohitgr7 July 28, 2022 17:28
@mergify mergify bot added the ready PRs ready to be merged label Jul 28, 2022
@carmocca carmocca enabled auto-merge (squash) July 28, 2022 17:31
Copy link
Contributor

@rohitgr7 rohitgr7 left a comment

Choose a reason for hiding this comment

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

LGTM

# This override is necessary because the cast must occur before the data
# is moved to the device to prevent wasteful host->device copies.
if self.precision_plugin.precision in (PrecisionType.MIXED, PrecisionType.HALF):
batch = apply_to_collection(batch, (FloatTensor, torch.cuda.FloatTensor), function=Tensor.half)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
batch = apply_to_collection(batch, (FloatTensor, torch.cuda.FloatTensor), function=Tensor.half)
batch = apply_to_collection(batch, Tensor, function=Tensor.half)

if we need to move all kinds of tensors for IPUs

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the change that caused #13983?

Copy link
Member

Choose a reason for hiding this comment

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

that's part of it, but before e.g. DoubleTensors, BfloatTensors etc. wouldn't have been cast as well.

tests/tests_pytorch/deprecated_api/test_remove_1-8.py Outdated Show resolved Hide resolved
src/pytorch_lightning/strategies/ipu.py Outdated Show resolved Hide resolved
auto-merge was automatically disabled July 28, 2022 18:42

Head branch was pushed to by a user without write access

@rohitgr7 rohitgr7 enabled auto-merge (squash) July 28, 2022 18:46
@rohitgr7 rohitgr7 merged commit 07b39c2 into Lightning-AI:master Jul 28, 2022
@hmellor hmellor deleted the cast-before-move branch August 26, 2022 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pl Generic label for PyTorch Lightning package ready PRs ready to be merged strategy: ipu (removed)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cast happens after copy in mixed precision training on IPU
5 participants