-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Typing for accelerators and plugins #7022
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9d7d440
Add typings for evaluation_loop.py
ethanwharris f3d5b54
Fix PEP
ethanwharris 7b9f13b
Fix some tests
ethanwharris 8d84fb4
Run pre-commit
ethanwharris 9b29428
Apply suggestions from code review
Borda 4f47b8b
Merge branch 'master' into docs/evaluation_loop_typing
carmocca 03bde1a
Update setup.cfg
ethanwharris ad960bc
Fix some mypy issues
ethanwharris cfdbad7
Updates
ethanwharris e21ad93
Fix
ethanwharris 42b60f9
Fix typing for accelerators and plugins
carmocca 72d28a2
Merge branch 'master' into typing-accelerators-plugins
carmocca fdf0f0e
pre-commit
carmocca 177f604
Fix mypy
carmocca 75e499e
Fix typing
carmocca f949e33
Fix typing
carmocca 04ff62e
Fix typing
carmocca 49fe989
Duplicate import
carmocca ab32167
Fix typing
carmocca 74d4376
Fix typing
carmocca 429c61e
Merge branch 'master' into typing-accelerators-plugins
carmocca f40252c
Bad merge
carmocca 0216d2a
Undo some changes
carmocca b539786
Undo forward references
carmocca 4c93cf4
Address comment
carmocca a1b1247
Forward reference OSS
carmocca c677107
Forward reference GradScaler
carmocca e80cf44
Minor changes
carmocca 6c14757
Update pytorch_lightning/accelerators/accelerator.py
carmocca e005c44
flake8
carmocca 8dd9460
Update pytorch_lightning/plugins/precision/apex_amp.py
carmocca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
|
||
from torch.optim import Optimizer | ||
|
||
import pytorch_lightning as pl | ||
from pytorch_lightning.accelerators.accelerator import Accelerator | ||
from pytorch_lightning.plugins.precision import MixedPrecisionPlugin | ||
from pytorch_lightning.plugins.training_type.single_tpu import SingleTPUPlugin | ||
|
@@ -26,10 +27,9 @@ | |
import torch_xla.core.xla_model as xm | ||
from torch_xla._patched_functions import clip_grad_norm_ | ||
|
||
# rename to mock in a test | ||
xla_clip_grad_norm_ = clip_grad_norm_ | ||
|
||
import pytorch_lightning as pl | ||
|
||
|
||
class TPUAccelerator(Accelerator): | ||
""" Accelerator for TPU devices. """ | ||
|
@@ -59,19 +59,16 @@ def clip_gradients( | |
self, | ||
optimizer: Optimizer, | ||
clip_val: Union[float, int], | ||
norm_type: float = 2.0, | ||
gradient_clip_algorithm: GradClipAlgorithmType = GradClipAlgorithmType.NORM | ||
gradient_clip_algorithm: GradClipAlgorithmType = GradClipAlgorithmType.NORM, | ||
) -> None: | ||
assert gradient_clip_algorithm is GradClipAlgorithmType.NORM, \ | ||
assert gradient_clip_algorithm == GradClipAlgorithmType.NORM, \ | ||
"Only NORM gradient clipping is supported on TPU for now" | ||
|
||
model = self.lightning_module | ||
parameters = model.parameters() | ||
|
||
grad_clip_val = float(clip_val) | ||
if grad_clip_val <= 0: | ||
return | ||
|
||
max_norm = grad_clip_val | ||
parameters = self.model.parameters() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this equivalent to what we had before? |
||
norm_type = 2.0 | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
xla_clip_grad_norm_(parameters, max_norm, norm_type) | ||
xla_clip_grad_norm_(parameters, grad_clip_val, norm_type) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.