checkpoint_path = str(self.checkpoints_path / "checkpoint_{}.ckpt") torch.save(checkpoint_1, str(checkpoint_path).format("1")) torch.save(checkpoint_2, str(checkpoint_path).format("2")) class ModelDeploy(L.LightningWork): def __init__(self, ckpt_path, *args, **kwargs): super().__init__() self.ckpt_path = ckpt_path def run(self): ckpts = os.listdir(self.ckpt_path) checkpoint_1 = torch.load(os.path.join(self.ckpt_path, ckpts[0])) checkpoint_2 = torch.load(os.path.join(self.ckpt_path, ckpts[1])) print(f"Loaded checkpoint_1: {checkpoint_1}") print(f"Loaded checkpoint_2: {checkpoint_2}") class LitApp(L.LightningFlow): def __init__(self): super().__init__() self.train = ModelTraining() self.deploy = ModelDeploy(ckpt_path=self.train.checkpoints_path) def run(self): self.train.run() self.deploy.run() app = L.LightningApp(LitApp())
import os from pathlib import Path import lightning as L from examples.components.python.component_tracer import PLTracerPythonScript class RootFlow(L.LightningFlow): def __init__(self): super().__init__() script_path = Path(__file__).parent / "pl_script.py" self.tracer_python_script = PLTracerPythonScript(script_path) def run(self): assert os.getenv("GLOBAL_RANK", "0") == "0" if not self.tracer_python_script.has_started: self.tracer_python_script.run() if self.tracer_python_script.has_succeeded: self._exit("tracer script succeed") if self.tracer_python_script.has_failed: self._exit("tracer script failed") app = L.LightningApp(RootFlow())
import lightning as L # Step 1: Subclass LightningFlow component to define the app flow. class HelloWorld(L.LightningFlow): # Step 2: Add the app logic to the LightningFlow run method to # ``print("Hello World!")`. # The LightningApp executes the run method of the main LightningFlow # within an infinite loop. def run(self): print("Hello World!") # Step 3: Initialize a LightningApp with the LightningFlow you defined (in step 1) app = L.LightningApp(HelloWorld())
import argparse import lightning as L class Work(L.LightningWork): def __init__(self, cloud_compute): super().__init__(cloud_compute=cloud_compute) def run(self): pass class Flow(L.LightningFlow): def __init__(self, cloud_compute): super().__init__() self.work = Work(cloud_compute) def run(self): assert self.work.cloud_compute.name == "gpu", self.work.cloud_compute.name self._exit() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--use_gpu", action="store_true", default=False, help="Whether to use GPU in the cloud") hparams = parser.parse_args() app = L.LightningApp(Flow(L.CloudCompute("gpu" if hparams.use_gpu else "cpu")))
import lightning as L from lightning.app.utilities.app_helpers import pretty_state class Work(L.LightningWork): def __init__(self): super().__init__(cache_calls=False) # Attributes are registered automatically in the state. self.counter = 0 def run(self): # Incrementing an attribute gets reflected in the `Flow` state. self.counter += 1 class Flow(L.LightningFlow): def __init__(self): super().__init__() self.w = Work() def run(self): if self.w.has_started: print(f"State: {pretty_state(self.state)} \n") self.w.run() app = L.LightningApp(Flow())
import lightning as L from lightning.app import frontend class YourComponent(L.LightningFlow): def __init__(self): super().__init__() self.message_to_print = "Hello World!" self.should_print = False def configure_layout(self): return frontend.StaticWebFrontend(Path(__file__).parent / "ui/dist") class HelloLitReact(L.LightningFlow): def __init__(self): super().__init__() self.counter = 0 self.react_ui = YourComponent() def run(self): if self.react_ui.should_print: print(f"{self.counter}: {self.react_ui.message_to_print}") self.counter += 1 def configure_layout(self): return [{"name": "React UI", "content": self.react_ui}] app = L.LightningApp(HelloLitReact())
import lightning as L class HourLongWork(L.LightningWork): def __init__(self): super().__init__(cache_calls=False) self.progress = 0.0 def run(self): self.progress = 0.0 for _ in range(3600): self.progress += 1.0 / 3600 sleep(1) class RootFlow(L.LightningFlow): def __init__(self, child_work: L.LightningWork): super().__init__() self.child_work = child_work def run(self): # prints the progress from the child work print(round(self.child_work.progress, 4)) self.child_work.run() if self.child_work.counter == 1.0: print("1 hour later!") app = L.LightningApp(RootFlow(HourLongWork()))
class ScheduledDAG(L.LightningFlow): def __init__(self, dag_cls, **dag_kwargs): super().__init__() self.dags = List() self._dag_cls = dag_cls self.dag_kwargs = dag_kwargs def run(self): """Example of scheduling an infinite number of DAG runs continuously.""" # Step 1: Every minute, create and launch a new DAG. if self.schedule("* * * * *"): print("Launching a new DAG") self.dags.append(self._dag_cls(**self.dag_kwargs)) for dag in self.dags: if not dag.has_completed: dag.run() app = L.LightningApp( ScheduledDAG( DAG, models=[ "svm.SVR", "linear_model.LinearRegression", "tree.DecisionTreeRegressor", ], ), )
self.dict["src_w"].run() if self.dict["src_w"].has_succeeded: # create dynamically the dst_w at runtime if "dst_w" not in self.dict: self.dict["dst_w"] = DestinationFileAndServeWork( script_path=os.path.join(os.path.dirname(__file__), "scripts/serve.py"), port=1111, parallel=False, # runs until killed. cloud_compute=L.CloudCompute(), raise_exception=True, ) # the flow passes the file from one work to another. self.dict["dst_w"].run(self.dict["src_w"].boring_path) self._exit("Boring App End") def configure_layout(self): return { "name": "Boring Tab", "content": self.dict["dst_w"].url + "/file" if "dst_w" in self.dict else "" } app = L.LightningApp(BoringApp())
from docs.quickstart.app_02 import HourLongWork import lightning as L class RootFlow(L.LightningFlow): def __init__(self, child_work_1: L.LightningWork, child_work_2: L.LightningWork): super().__init__() self.child_work_1 = child_work_1 self.child_work_2 = child_work_2 def run(self): print(round(self.child_work_1.progress, 4), round(self.child_work_2.progress, 4)) self.child_work_1.run() self.child_work_2.run() if self.child_work_1.progress == 1.0: print("1 hour later `child_work_1` started!") if self.child_work_2.progress == 1.0: print("1 hour later `child_work_2` started!") app = L.LightningApp( RootFlow(HourLongWork(parallel=True), HourLongWork(parallel=True)))
script_path=self.script_path, data_dir=self.data_dir, cloud_compute=L.CloudCompute("cpu"), ) self.ws[work_name] = objective_work if not self.ws[work_name].has_started: trial = self._study.ask(ObjectiveWork.distributions()) self.ws[work_name].run(trial_id=trial._trial_id, **trial.params) if self.ws[work_name].metric and not self.ws[ work_name].has_told_study: self._study.tell(self.ws[work_name].trial_id, self.ws[work_name].metric) self.ws[work_name].has_told_study = True has_told_study.append(self.ws[work_name].has_told_study) if all(has_told_study): self.num_trials += self.simultaneous_trials if __name__ == "__main__": app = L.LightningApp( RootHPOFlow( script_path=str(Path(__file__).parent / "pl_script.py"), data_dir="data/hymenoptera_data_version_0", total_trials=6, simultaneous_trials=2, ))
@staticmethod def send_message(message): logger.info(f"Sending message: {message}") def run(self): pass class RootComponent(L.LightningFlow): def __init__(self): super().__init__() self.pickle_checker = PickleChecker() self.slack = Slack() self.counter = 3 def run(self): if self.counter > 0: logger.info(f"Running the app {self.counter}") image_str = b"it is not a pickle" if self.pickle_checker.run(image_str): self.slack.send_message("It's a pickle!") else: self.slack.send_message("It's not a pickle!") self.counter -= 1 else: self._exit("Pickle or Not End") app = L.LightningApp(RootComponent())
super().__init__() self.aas = Word("a") self.bbs = Word("b") self.counter = 0 def run(self): now = datetime.now() now = now.strftime("%H:%M:%S") log = {"time": now, "a": self.aas.repeats, "b": self.bbs.repeats} print(log) self.aas.run() self.bbs.run() sleep(2.0) self.counter += 1 def configure_layout(self): tab1 = {"name": "Tab_1", "content": self.aas} tab2 = {"name": "Tab_2", "content": self.bbs} tab3 = { "name": "Tab_3", "content": "https://tensorboard.dev/experiment/8m1aX0gcQ7aEmH0J7kbBtg/#scalars" } return [tab1, tab2, tab3] app = L.LightningApp(V0App())
"resnet18", ) # Init an image classifier with resnet26 backbone self.train_work_2 = ImageClassifierTrainWork( max_epochs, "resnet26", ) # Init the serving component self.server_work = ImageClassifierServeWork() def run(self): # running both `train_work_1` and `train_work_2` in parallel and asynchronously. self.train_work_1.run(self.data_dir) self.train_work_2.run(self.data_dir) # run serve_work only when both `best_model_score` are available. if self.train_work_1.best_model_score and self.train_work_2.best_model_score: # serve only the best model between `train_work_1` and `train_work_2`. self.server_work.run( self.train_work_1.best_model_path if self.train_work_1.best_model_score < self.train_work_2. best_model_score else self.train_work_2.best_model_path) # Step 4: download a dataset to your local directory under `/data` download_data("https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip", "./data") # Initialize your Lightning app with 5 epochs app = L.LightningApp(RootFlow(5, "./data/hymenoptera_data"))
def run(self): pass def configure_layout(self): return [ dict(name="one", content=self.c21), dict(name="two", content=self.c22), ] class Root(L.LightningFlow): def __init__(self): super().__init__() self.c1 = C1() self.c2 = C2() def run(self): sleep(10) self._exit("Layout End") def configure_layout(self): return [ dict(name="one", content=self.c1.c11), dict(name="two", content=self.c2), dict(name="three", content="https://lightning.ai"), ] app = L.LightningApp(Root())
torch.distributed.init_process_group( backend="gloo", init_method=f"tcp://{main_address}:{main_port}", world_size=world_size, rank=rank) gathered = [torch.zeros(1) for _ in range(world_size)] torch.distributed.all_gather(gathered, torch.tensor([rank]).float()) print(gathered) class MultiNodeDemo(L.LightningFlow): def __init__(self): super().__init__() self.work0 = Work() self.work1 = Work() def run(self): self.work0.run(init=True) if self.work0.internal_ip: self.work0.run(main_address=self.work0.internal_ip, main_port=self.work0.port, world_size=2, rank=0) self.work1.run(main_address=self.work0.internal_ip, main_port=self.work0.port, world_size=2, rank=1) app = L.LightningApp(MultiNodeDemo())