|
| 1 | +:orphan: |
| 2 | + |
| 3 | +MUSA training (Advanced) |
| 4 | +======================== |
| 5 | +**Audience:** Users looking to train models on MooreThreads device using MUSA accelerator. |
| 6 | + |
| 7 | +.. warning:: This is an :ref:`experimental <versioning:Experimental API>` feature. |
| 8 | + |
| 9 | +---- |
| 10 | + |
| 11 | +MUSAAccelerator Overview |
| 12 | +-------------------- |
| 13 | +torch_musa is an extended Python package based on PyTorch that enables full utilization of MooreThreads graphics cards' |
| 14 | +super computing power. Combined with PyTorch, users can take advantage of the strong power of MooreThreads graphics cards |
| 15 | +through torch_musa. |
| 16 | + |
| 17 | +PyTorch Lightning automatically finds these weights and ties them after the modules are moved to the |
| 18 | +MUSA device under the hood. It will ensure that the weights among the modules are shared but not copied |
| 19 | +independently. |
| 20 | + |
| 21 | + |
| 22 | +Example: |
| 23 | + |
| 24 | +.. code-block:: python |
| 25 | + import torch, torch.nn as nn, torch.utils.data as data, torchvision as tv, torch.nn.functional as F |
| 26 | + import pytorch_lightning as L |
| 27 | +
|
| 28 | + # Step 1: Define a LightningModule |
| 29 | + class LitAutoEncoder(L.LightningModule): |
| 30 | + def __init__(self): |
| 31 | + super().__init__() |
| 32 | + self.encoder = nn.Sequential(nn.Linear(28 * 28, 128), nn.ReLU(), nn.Linear(128, 3)) |
| 33 | + self.decoder = nn.Sequential(nn.Linear(3, 128), nn.ReLU(), nn.Linear(128, 28 * 28)) |
| 34 | +
|
| 35 | + def forward(self, x): |
| 36 | + # in lightning, forward defines the prediction/inference actions |
| 37 | + embedding = self.encoder(x) |
| 38 | + return embedding |
| 39 | +
|
| 40 | + def training_step(self, batch, batch_idx): |
| 41 | + # training_step defines the train loop. It is independent of forward |
| 42 | + x, _ = batch |
| 43 | + x = x.view(x.size(0), -1) |
| 44 | + z = self.encoder(x) |
| 45 | + x_hat = self.decoder(z) |
| 46 | + loss = F.mse_loss(x_hat, x) |
| 47 | + self.log("train_loss", loss) |
| 48 | + return loss |
| 49 | +
|
| 50 | + def configure_optimizers(self): |
| 51 | + optimizer = torch.optim.Adam(self.parameters(), lr=1e-3) |
| 52 | + return optimizer |
| 53 | +
|
| 54 | + def main(): |
| 55 | + # ------------------- |
| 56 | + # Step 2: Define data |
| 57 | + # ------------------- |
| 58 | + dataset = tv.datasets.MNIST(".", download=True, transform=tv.transforms.ToTensor()) |
| 59 | + train, val = data.random_split(dataset, [55000, 5000]) |
| 60 | +
|
| 61 | + # ------------------- |
| 62 | + # Step 3: Train |
| 63 | + # ------------------- |
| 64 | + autoencoder = LitAutoEncoder() |
| 65 | + # we also support accelerator="auto" or accelerator="musa" |
| 66 | + trainer = L.Trainer(accelerator="gpu") |
| 67 | + trainer.fit(autoencoder, data.DataLoader(train), data.DataLoader(val)) |
| 68 | +
|
| 69 | + if __name__ == '__main__': |
| 70 | +
|
| 71 | + main() |
| 72 | +---- |
| 73 | + |
| 74 | +MUSA |
| 75 | +---- |
| 76 | +MUSA is the library that interfaces PyTorch with the MooreThreads graphics cards. |
| 77 | +For more information check out `MUSA <https://github.com/MooreThreads/torch_musa>`_. |
0 commit comments