-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Run this with | ||
# | ||
# python -m lightning_lite.cli examples/01_lite_launch/launcher_cli.py --devices 2 --precision bf16 | ||
|
||
import torch.distributed | ||
|
||
from lightning_lite import LightningLite | ||
|
||
|
||
if __name__ == "__main__": | ||
lite = LightningLite() | ||
print("launched", lite.global_rank) | ||
assert torch.distributed.is_initialized() | ||
lite.barrier() | ||
print("end") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import torch.distributed | ||
|
||
from lightning_lite import LightningLite | ||
|
||
|
||
class Lite(LightningLite): | ||
def run(self): | ||
print("launched", self.global_rank) | ||
assert torch.distributed.is_initialized() | ||
self.barrier() | ||
|
||
|
||
if __name__ == "__main__": | ||
lite = Lite(accelerator="cpu", devices=2, strategy="ddp") | ||
lite.run() | ||
print("after run", lite.global_rank) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import torch.distributed | ||
|
||
from lightning_lite import LightningLite | ||
|
||
|
||
if __name__ == "__main__": | ||
lite = LightningLite(accelerator="cpu", devices=2, strategy="ddp") | ||
lite.launch() | ||
print("launched", lite.global_rank) | ||
assert torch.distributed.is_initialized() | ||
lite.barrier() | ||
print("end") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import torch.distributed | ||
|
||
from lightning_lite import LightningLite | ||
|
||
|
||
def run(lite): | ||
print("launched", lite.global_rank) | ||
assert torch.distributed.is_initialized() | ||
lite.barrier() | ||
print("end") | ||
|
||
|
||
if __name__ == "__main__": | ||
lite = LightningLite(accelerator="cpu", devices=2, strategy="ddp") | ||
lite.launch(run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import torch.distributed | ||
|
||
from lightning_lite import LightningLite | ||
|
||
|
||
def run(lite): | ||
print("launched", lite.global_rank) | ||
assert torch.distributed.is_initialized() | ||
|
||
|
||
if __name__ == "__main__": | ||
lite = LightningLite(accelerator="cpu", devices=2, strategy="ddp_notebook") | ||
lite.launch(run) | ||
print("main process joins", lite.global_rank) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
from argparse import ArgumentParser | ||
import torch.distributed.run as torchrun | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser() | ||
parser.add_argument("script", type=str) | ||
parser.add_argument("--accelerator", type=str, default="cpu", choices=("cpu", "cuda", "mps", "tpu", "auto")) | ||
# TODO: note for some accelerators/strategies, torchrun won't make sense (e.g. dp) | ||
# TODO: should we include spawn? | ||
parser.add_argument("--strategy", type=str, default=None, choices=(None, "ddp", "dp", "deepspeed")) | ||
parser.add_argument("--devices", type=str, default="1") | ||
parser.add_argument("--num-nodes", type=int, default=1) | ||
parser.add_argument("--node-rank", type=int, default=0) | ||
parser.add_argument("--main-address", type=str, default="127.0.0.1") | ||
parser.add_argument("--main-port", type=int, default=29400) | ||
parser.add_argument("--precision", type=str, default="32", choices=("32", "16", "bf16")) | ||
args = parser.parse_args() | ||
|
||
os.environ["LT_ACCELERATOR"] = str(args.accelerator) | ||
if args.strategy: | ||
os.environ["LT_STRATEGY"] = str(args.strategy) | ||
os.environ["LT_DEVICES"] = str(args.devices) | ||
os.environ["LT_NUM_NODES"] = str(args.num_nodes) | ||
os.environ["LT_PRECISION"] = str(args.precision) | ||
|
||
num_devices = int(args.devices) # TODO: count them | ||
|
||
torchrun_args = [] | ||
torchrun_args.extend(["--nproc_per_node", str(num_devices)]) | ||
torchrun_args.extend(["--nnodes", str(args.num_nodes)]) | ||
torchrun_args.extend(["--node_rank", str(args.node_rank)]) | ||
torchrun_args.extend(["--master_addr", args.main_address]) | ||
torchrun_args.extend(["--master_port", str(args.main_port)]) | ||
torchrun_args.append(args.script) | ||
|
||
os.environ.setdefault("OMP_NUM_THREADS", str(max(1, os.cpu_count() // num_devices))) | ||
|
||
torchrun.main(torchrun_args) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters