Level 2: Explore real component implementations

Audience: Users who want to deeply understand what is possible with Lightning components.

Prereqs: You must have finished level 1.


Real component examples

Use this guide to understand what is happening in each type of component. These are a few prototypical components. Since each component organizes Python, you can build virtually infinite components for any use-case you can think of.


Ex: PyTorch + Lightning Trainer

This example shows how to train PyTorch with the Lightning trainer on your machine or cloud GPUs without code changes.

import Lightning
We're using a demo LightningModule
Move your training code here (usually your main.py)
Pass your component to the multi-node executor (it works on CPU or single GPUs also)
Select the number of machines (nodes). Here we choose 4.
Choose from over 15+ machine types. This one has 4 v100 GPUs.
Initialize the App object that executes the component logic.
# app.py
from lightning import Trainer
from lightning.app import LightningWork, LightningApp, CloudCompute
from lightning.app.components import LightningTrainerMultiNode
from lightning.pytorch.demos.boring_classes import BoringModel


class LightningTrainerDistributed(LightningWork):
    def run(self):
        model = BoringModel()
        trainer = Trainer(max_epochs=10, strategy="ddp")
        trainer.fit(model)

# 8 GPUs: (2 nodes of 4 x v100)
component = LightningTrainerMultiNode(
    LightningTrainerDistributed,
    num_nodes=4,
    cloud_compute=CloudCompute("gpu-fast-multi"), # 4 x v100
)
app = LightningApp(component)


Ex: Deploy a PyTorch API endpoint

This example shows how to deploy PyTorch and create an API

Shortcut to list dependencies without a requirements.txt file.
Import one of our serving components (high-performance ones are available on the enterprise tiers)
Define the setup function to load your favorite pretrained models and do any kind of pre-processing.
Define the predict function which is called when the endpoint is hit.
Initialize the server and define the type of cloud machine to use.
# !pip install torchvision
from lightning.app import LightningApp, CloudCompute
from lightning.app.components.serve import PythonServer, Image, Number
import base64, io, torchvision, torch
from PIL import Image as PILImage


class PyTorchServer(PythonServer):
    def setup(self):
        self._model = torchvision.models.resnet18(pretrained=True)
        self._device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self._model.to(self._device)

    def predict(self, request):
        image = base64.b64decode(request.image.encode("utf-8"))
        image = PILImage.open(io.BytesIO(image))
        transforms = torchvision.transforms.Compose([
            torchvision.transforms.Resize(224),
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])
        image = transforms(image)
        image = image.to(self._device)
        prediction = self._model(image.unsqueeze(0))
        return {"prediction": prediction.argmax().item()}


component = PyTorchServer(
   input_type=Image, output_type=Number, cloud_compute=CloudCompute('gpu')
)
app = LightningApp(component)


Next: Save on cloud costs

Let’s review key lightning features to help you run components more efficiently on the cloud so you can save on cloud costs.