-
Notifications
You must be signed in to change notification settings - Fork 3.5k
IPU Integration #7735
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
Closed
IPU Integration #7735
Changes from 12 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
f75f445
Initial changes
a4a60c2
Merge branch 'master' into wip/acc
dc9744b
Add broken example for now
931bb74
Fix reference
9b18baf
Merge branch 'master' into wip/acc
c617f02
Fix format
522a81f
Code runs
0c00360
Fixes
30c1370
Merge branch 'master' into wip/acc
adbdb2a
Clear up files
3e733af
Add tests, helpers, fixes
a51f23e
Small cleanups
be7de87
Refactors based on review
83c8a79
Swap to special tests
a6018e5
Add special tests
0e71bbe
Add source
6e38bd1
Cleanups
526383f
Add logic to attach/detach model from devices
e18039c
Fixes for tests
2e43fee
Fixes for tests
53d31a0
Move earlier
6241432
Cleanups
d249a13
Add check for nvcc
d08cf39
Add tests, cleanups
7469744
Fix errors
f474c5b
fix
e178d5f
Try condition
c704920
Add missing annotation
c54a216
Clearer
2ea1766
Clearer message
751f0ea
Fix variable
87e4c8a
Merge branch 'master' into wip/acc
61d2014
Cleanups
d76f491
Merge branch 'master' into wip/acc
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from pprint import pprint | ||
|
||
import torch | ||
from torch.nn import functional as F | ||
|
||
import pytorch_lightning as pl | ||
from pl_examples.basic_examples.mnist_datamodule import MNISTDataModule | ||
|
||
|
||
class LitClassifier(pl.LightningModule): | ||
|
||
def __init__( | ||
self, | ||
hidden_dim: int = 128, | ||
learning_rate: float = 0.0001, | ||
): | ||
super().__init__() | ||
self.save_hyperparameters() | ||
|
||
self.l1 = torch.nn.Linear(28 * 28, self.hparams.hidden_dim) | ||
self.l2 = torch.nn.Linear(self.hparams.hidden_dim, 10) | ||
|
||
def forward(self, x): | ||
x = x.view(x.size(0), -1) | ||
x = torch.relu(self.l1(x)) | ||
x = torch.relu(self.l2(x)) | ||
return x | ||
|
||
def training_step(self, batch, batch_idx): | ||
x, y = batch | ||
y_hat = self(x) | ||
loss = F.cross_entropy(y_hat, y) | ||
return loss | ||
|
||
def validation_step(self, batch, batch_idx): | ||
x, y = batch | ||
logits = self(x) | ||
acc = self.accuracy(logits, y) | ||
return acc | ||
|
||
def test_step(self, batch, batch_idx): | ||
x, y = batch | ||
logits = self(x) | ||
acc = self.accuracy(logits, y) | ||
return acc | ||
|
||
def accuracy(self, logits, y): | ||
# todo (sean): currently IPU poptorch doesn't implicit convert bools to tensor | ||
# hence we use an explicit calculation for accuracy here. Once fixed in poptorch | ||
# we can use the accuracy metric. | ||
acc = torch.sum(torch.eq(torch.argmax(logits, -1), y).to(torch.float32)) / len(y) | ||
return acc | ||
|
||
def validation_epoch_end(self, outputs) -> None: | ||
self.log('val_acc', torch.stack(outputs).mean(), prog_bar=True) | ||
|
||
def test_epoch_end(self, outputs) -> None: | ||
self.log('test_acc', torch.stack(outputs).mean()) | ||
|
||
def configure_optimizers(self): | ||
return torch.optim.Adam(self.parameters(), lr=self.hparams.learning_rate) | ||
|
||
|
||
if __name__ == '__main__': | ||
dm = MNISTDataModule(batch_size=32) | ||
|
||
model = LitClassifier() | ||
|
||
trainer = pl.Trainer(max_epochs=2, ipu_cores=8) | ||
|
||
trainer.fit(model, datamodule=dm) | ||
|
||
result = trainer.test(model, datamodule=dm) | ||
pprint(result) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright The PyTorch Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from collections import Callable | ||
|
||
from torch.optim import Optimizer | ||
|
||
from pytorch_lightning.accelerators.accelerator import Accelerator | ||
from pytorch_lightning.utilities.exceptions import MisconfigurationException | ||
|
||
|
||
class IPUAccelerator(Accelerator): | ||
|
||
def setup_optimizers(self, trainer): | ||
super().setup_optimizers(trainer) | ||
|
||
if len(self.optimizers) > 1: | ||
raise MisconfigurationException("IPUs currently only support one optimizer.") | ||
|
||
def optimizer_step(self, optimizer: Optimizer, opt_idx: int, lambda_closure: Callable, **kwargs): | ||
# Optimizer step is handled by the IPU accelerator. | ||
lambda_closure() |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
|
||
from torch import Tensor | ||
|
||
from pytorch_lightning.plugins.precision.precision_plugin import PrecisionPlugin | ||
|
||
|
||
class IPUPrecisionPlugin(PrecisionPlugin): | ||
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. @awaelchli said before the next boilerplate precision plugin we should refactor to have the |
||
|
||
def __init__(self, precision: int) -> None: | ||
super().__init__() | ||
self.precision = precision | ||
|
||
def backward( | ||
self, | ||
closure_loss: Tensor, | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> Tensor: | ||
# IPU internally manages bwd step. | ||
return closure_loss | ||
|
||
def clip_gradients(self, *args, **kwargs) -> None: | ||
pass |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to clear up why this is here, not in the step itself (the step functions are jitted, and the outputs are collated from all devices, so mean averaging etc cannot happen within the functions)