2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Runs either `.fit()` or `.test()` on a single node across multiple gpus."""
|
2020-12-05 20:09:47 +00:00
|
|
|
import os
|
2020-10-01 13:25:33 +00:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2021-01-15 00:32:41 +00:00
|
|
|
from pytorch_lightning import seed_everything, Trainer
|
2021-02-23 22:08:46 +00:00
|
|
|
from tests.helpers.datamodules import ClassifDataModule
|
|
|
|
from tests.helpers.simple_models import ClassificationModel
|
2021-01-15 00:32:41 +00:00
|
|
|
|
2020-10-01 13:25:33 +00:00
|
|
|
|
|
|
|
def main():
|
2021-04-19 13:50:31 +00:00
|
|
|
seed_everything(4321)
|
2020-12-05 20:09:47 +00:00
|
|
|
|
2020-10-01 13:25:33 +00:00
|
|
|
parser = ArgumentParser(add_help=False)
|
|
|
|
parser = Trainer.add_argparse_args(parser)
|
2021-07-26 11:37:35 +00:00
|
|
|
parser.add_argument("--trainer_method", default="fit")
|
|
|
|
parser.add_argument("--tmpdir")
|
|
|
|
parser.add_argument("--workdir")
|
2020-10-01 13:25:33 +00:00
|
|
|
parser.set_defaults(gpus=2)
|
2020-12-09 08:18:23 +00:00
|
|
|
parser.set_defaults(accelerator="ddp")
|
2020-10-01 13:25:33 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-02-23 22:08:46 +00:00
|
|
|
dm = ClassifDataModule()
|
|
|
|
model = ClassificationModel()
|
2020-10-01 13:25:33 +00:00
|
|
|
trainer = Trainer.from_argparse_args(args)
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
if args.trainer_method == "fit":
|
2021-02-23 22:08:46 +00:00
|
|
|
trainer.fit(model, datamodule=dm)
|
|
|
|
result = None
|
2021-07-26 11:37:35 +00:00
|
|
|
elif args.trainer_method == "test":
|
2021-02-23 22:08:46 +00:00
|
|
|
result = trainer.test(model, datamodule=dm)
|
2021-07-26 11:37:35 +00:00
|
|
|
elif args.trainer_method == "fit_test":
|
2021-02-23 22:08:46 +00:00
|
|
|
trainer.fit(model, datamodule=dm)
|
|
|
|
result = trainer.test(model, datamodule=dm)
|
|
|
|
else:
|
2021-07-26 11:37:35 +00:00
|
|
|
raise ValueError(f"Unsupported: {args.trainer_method}")
|
2021-02-23 22:08:46 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
result_ext = {"status": "complete", "method": args.trainer_method, "result": result}
|
|
|
|
file_path = os.path.join(args.tmpdir, "ddp.result")
|
2021-02-23 22:08:46 +00:00
|
|
|
torch.save(result_ext, file_path)
|
2020-10-01 13:25:33 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
if __name__ == "__main__":
|
2020-10-01 13:25:33 +00:00
|
|
|
main()
|