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¶
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
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
--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.