2022-03-25 10:24:52 +00:00
|
|
|
# Copyright The Lightning AI 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.
|
|
|
|
import torch
|
|
|
|
from jsonargparse import lazy_instance
|
2023-02-10 10:30:42 +00:00
|
|
|
from lightning.pytorch import LightningModule
|
|
|
|
from lightning.pytorch.cli import LightningCLI
|
|
|
|
from lightning.pytorch.demos.mnist_datamodule import MNISTDataModule
|
ruff: replace isort with ruff +TPU (#17684)
* ruff: replace isort with ruff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing & imports
* lines in warning test
* docs
* fix enum import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing
* import
* fix lines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* type ClusterEnvironment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-26 15:54:55 +00:00
|
|
|
from lightning_habana import HPUPrecisionPlugin
|
|
|
|
from torch.nn import functional as F
|
2022-03-25 10:24:52 +00:00
|
|
|
|
|
|
|
|
2022-06-15 12:53:51 +00:00
|
|
|
class LitClassifier(LightningModule):
|
2022-03-25 10:24:52 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.l1 = torch.nn.Linear(28 * 28, 10)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
return torch.relu(self.l1(x.view(x.size(0), -1)))
|
|
|
|
|
|
|
|
def training_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
2023-05-05 09:34:40 +00:00
|
|
|
return F.cross_entropy(self(x), y)
|
2022-03-25 10:24:52 +00:00
|
|
|
|
|
|
|
def validation_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
|
|
|
probs = self(x)
|
|
|
|
acc = self.accuracy(probs, y)
|
|
|
|
self.log("val_acc", acc)
|
|
|
|
|
|
|
|
def test_step(self, batch, batch_idx):
|
|
|
|
x, y = batch
|
|
|
|
logits = self(x)
|
|
|
|
acc = self.accuracy(logits, y)
|
|
|
|
self.log("test_acc", acc)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def accuracy(logits, y):
|
2023-05-05 09:34:40 +00:00
|
|
|
return torch.sum(torch.eq(torch.argmax(logits, -1), y).to(torch.float32)) / len(y)
|
2022-03-25 10:24:52 +00:00
|
|
|
|
|
|
|
def configure_optimizers(self):
|
|
|
|
return torch.optim.Adam(self.parameters(), lr=0.02)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli = LightningCLI(
|
|
|
|
LitClassifier,
|
|
|
|
MNISTDataModule,
|
|
|
|
trainer_defaults={
|
|
|
|
"accelerator": "hpu",
|
|
|
|
"devices": 1,
|
|
|
|
"max_epochs": 1,
|
2023-02-17 16:58:14 +00:00
|
|
|
"plugins": lazy_instance(HPUPrecisionPlugin, precision="16-mixed"),
|
2022-03-25 10:24:52 +00:00
|
|
|
},
|
|
|
|
run=False,
|
2022-12-19 22:24:25 +00:00
|
|
|
save_config_kwargs={"overwrite": True},
|
2022-03-25 10:24:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Run the model ⚡
|
|
|
|
cli.trainer.fit(cli.model, datamodule=cli.datamodule)
|
|
|
|
cli.trainer.validate(cli.model, datamodule=cli.datamodule)
|
|
|
|
cli.trainer.test(cli.model, datamodule=cli.datamodule)
|