Level 1: Package code in a lightning component

Prereqs: You know basic Python.

Goal: In this guide you’ll learn to develop a Lightning component.

Why you need Lightning components

A Lightning component is a self-contained, modular machine-learning component that you can plug into your existing ML workflows. A Lightning component organizes arbitrary code so it can run on the cloud, manages its own infrastructure, cloud costs, networking and more. Connect components using your current workflow management tools or our next-generation reactive orchestrator.

Components run on the cloud or your laptop without code changes 🤯🤯.


Organizing your code into Lightning components offers these benefits:

Build systems not scripts

The Lightning structure forces best practices so you don’t have to be an expert production engineer. Although it feels like you’re writing a script, you are actually building a production-ready system.

Cost control

The component run-time has been optimized for cost management to support the largest machine-learning workloads. Lower your cloud bill with machines that shut down or spin up faster.

For beginners: Code like an expert

Lightning embeds the best practices of building production-ready full stack AI apps into your coding experience. You can write code like you normally do, and the Lightning structure ensures your code is implicitly production ready… even if you’re just doing research.

For experts: Scale with full control

if you know what you are doing, Lightning gives you full control to manage your own scaling logic, fault-tolerance and even pre-provisioning, all from Python.

Integrate into your current workflow tools

Lightning components are self-contained pieces of functionality. Add them to your current workflow tools to quickly fill in gaps in your ML workflow such as monitoring drift, training LLMs and more. You can (optionally) use the Lightning App to integrate components into a cohesive workflow.

Packaged code

Lightning apps bundles components into an app that runs in any environment. The same code will run on your laptop, or any cloud or private clusters. You don’t have to think about the cluster or know anything about the cloud.

Rapid iteration

Iterate through ideas in hours not months because you don’t have to learn a million other concepts that the components handle for you such as kubernetes, cost management, auto-scaling and more.

Modularity

Components are modular and inter-operable by design. Leverage our vibrant community of components so you don’t have to build each piece of the system yourself.


Install Lightning

First, install Lightning.

Pip
Macs, Apple Silicon (M1/M2/M3)
Windows
pip install lightning


Build your first component

A Lightning component organizes arbitrary code so it can run on the cloud, manages its own infrastructure, cloud costs, networking and more

Run one of these components!

Hello world
Hello GPU world
PyTorch & ⚡⚡⚡ Trainer (1+ cloud GPUs)
Train PyTorch (cloud GPU)
Train PyTorch (32 cloud GPUs)
Deploy a model on cloud GPUs
Run a model script
XGBoost
Streamlit demo
# app.py
from lightning.app import LightningWork, LightningApp


class YourComponent(LightningWork):
   def run(self):
      print('RUN ANY PYTHON CODE HERE')



component = YourComponent()
app = LightningApp(component)


Key features

You now know enough to build a self-contained component that runs any Python code on the cloud that can be connected to form a powerful Lightning app. Here are a few key features available to super-charge your work:

15+ accelerators
Auto-stop idle machines
Auto-timeout submitted work
Use spot machines (~70% discount)
Work with massive datasets
Mount cloud storage
Use a custom container
# app.py
from lightning.app import LightningWork, LightningApp, CloudCompute


class YourComponent(LightningWork):
   def run(self):
      print('RUN ANY PYTHON CODE HERE')


# custom accelerators
compute = CloudCompute('gpu')
component = YourComponent(cloud_compute=compute)
app = LightningApp(component)

# OTHER ACCELERATORS:
# compute = CloudCompute('default')          # 1 CPU
# compute = CloudCompute('cpu-medium')       # 8 CPUs
# compute = CloudCompute('gpu')              # 1 T4 GPU
# compute = CloudCompute('gpu-fast-multi')   # 4 V100 GPU
# compute = CloudCompute('p4d.24xlarge')     # AWS instance name (8 A100 GPU)
# compute = ...


Next: Explore real component implementations

In this section we introduced components. Let’s explore real component implementations in-depth.