lightning/pl_examples/basic_examples/gpu_template.py

53 lines
1.3 KiB
Python
Raw Normal View History

2019-10-05 18:13:32 +00:00
"""
Runs a model on a single node across multiple gpus.
2019-10-05 18:13:32 +00:00
"""
import os
from argparse import ArgumentParser
from pytorch_lightning import Trainer, seed_everything
from pl_examples.models.lightning_template import LightningTemplateModel
2019-10-05 18:13:32 +00:00
seed_everything(234)
2019-10-05 18:13:32 +00:00
def main(args):
""" Main training routine specific for this project. """
2019-10-05 18:13:32 +00:00
# ------------------------
# 1 INIT LIGHTNING MODEL
# ------------------------
model = LightningTemplateModel(**vars(args))
2019-10-05 18:13:32 +00:00
# ------------------------
# 2 INIT TRAINER
# ------------------------
trainer = Trainer.from_argparse_args(args)
2019-10-05 18:13:32 +00:00
# ------------------------
# 3 START TRAINING
# ------------------------
trainer.fit(model)
def run_cli():
2019-10-05 18:13:32 +00:00
# ------------------------
# TRAINING ARGUMENTS
# ------------------------
# these are project-wide arguments
root_dir = os.path.dirname(os.path.realpath(__file__))
parent_parser = ArgumentParser(add_help=False)
# each LightningModule defines arguments relevant to it
parser = LightningTemplateModel.add_model_specific_args(parent_parser, root_dir)
parser = Trainer.add_argparse_args(parser)
parser.set_defaults(gpus=2)
args = parser.parse_args()
2019-10-05 18:13:32 +00:00
# ---------------------
# RUN TRAINING
# ---------------------
main(args)
if __name__ == '__main__':
run_cli()