Evolve a model into an ML system¶
Required background: Basic Python familiarity and complete the Build and Train a Model guide.
Goal: We’ll walk you through the two key steps to build your first Lightning App from your existing PyTorch Lightning scripts.
Training and beyond¶
With PyTorch Lightning, we abstracted distributed training and hardware, by organizing PyTorch code. With Lightning Apps, we unified the local and cloud experience while abstracting infrastructure.
By using PyTorch Lightning and Lightning Apps together, a completely new world of possibilities emerges.
1. Write an App to run the train.py script¶
This article continues where the Build and Train a Model guide finished.
Create an additional file app.py
in the pl_project
folder as follows:
pl_project/
app.py
train.py
requirements.txt
Inside the app.py
file, add the following code.
import lightning as L
from lightning.app.components import TracerPythonScript
class RootFlow(L.LightningFlow):
def __init__(self):
super().__init__()
self.runner = TracerPythonScript(
"train.py",
cloud_compute=L.CloudCompute("gpu"),
)
def run(self):
self.runner.run()
app = L.LightningApp(RootFlow())
This App runs the PyTorch Lightning script contained in the train.py
file using the powerful TracerPythonScript
component. This is really worth checking out!
2. Run the train.py file locally or in the cloud¶
First, go to the pl_folder
folder from the local terminal and install the requirements.
cd pl_folder
pip install -r requirements.txt
To run your app, copy the following command to your local terminal:
lightning run app app.py
Simply add --cloud
to run this application in the cloud with a GPU machine 🤯
lightning run app app.py --cloud
Congratulations! Now, you know how to run a PyTorch Lightning script with Lightning Apps.
Lightning Apps can make your ML system way more powerful, keep reading to learn how.