2020-10-13 20:47:23 +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.
|
|
|
|
import os
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
2021-02-09 10:10:52 +00:00
|
|
|
from tests.helpers import BoringModel
|
2021-03-02 09:36:01 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
2020-10-13 20:47:23 +00:00
|
|
|
|
|
|
|
|
2021-01-08 21:13:12 +00:00
|
|
|
def test_model_torch_save(tmpdir):
|
2020-10-13 20:47:23 +00:00
|
|
|
"""Test to ensure torch save does not fail for model and trainer."""
|
2020-11-24 00:23:12 +00:00
|
|
|
model = BoringModel()
|
2020-10-13 20:47:23 +00:00
|
|
|
num_epochs = 1
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, max_epochs=num_epochs)
|
|
|
|
temp_path = os.path.join(tmpdir, "temp.pt")
|
2020-10-13 20:47:23 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
# Ensure these do not fail
|
|
|
|
torch.save(trainer.model, temp_path)
|
|
|
|
torch.save(trainer, temp_path)
|
2020-12-01 00:09:46 +00:00
|
|
|
trainer = torch.load(temp_path)
|
2020-10-13 20:47:23 +00:00
|
|
|
|
|
|
|
|
2021-11-16 04:36:47 +00:00
|
|
|
@RunIf(skip_windows=True, skip_49370=True)
|
2020-10-13 20:47:23 +00:00
|
|
|
def test_model_torch_save_ddp_cpu(tmpdir):
|
|
|
|
"""Test to ensure torch save does not fail for model and trainer using cpu ddp."""
|
2020-11-24 00:23:12 +00:00
|
|
|
model = BoringModel()
|
2020-10-13 20:47:23 +00:00
|
|
|
num_epochs = 1
|
|
|
|
trainer = Trainer(
|
2022-01-12 05:47:01 +00:00
|
|
|
default_root_dir=tmpdir, max_epochs=num_epochs, strategy="ddp_spawn", accelerator="cpu", devices=2, logger=False
|
2020-10-13 20:47:23 +00:00
|
|
|
)
|
2021-07-26 11:37:35 +00:00
|
|
|
temp_path = os.path.join(tmpdir, "temp.pt")
|
2020-10-13 20:47:23 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
# Ensure these do not fail
|
|
|
|
torch.save(trainer.model, temp_path)
|
|
|
|
torch.save(trainer, temp_path)
|
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=2)
|
2020-10-13 20:47:23 +00:00
|
|
|
def test_model_torch_save_ddp_cuda(tmpdir):
|
|
|
|
"""Test to ensure torch save does not fail for model and trainer using gpu ddp."""
|
2020-11-24 00:23:12 +00:00
|
|
|
model = BoringModel()
|
2020-10-13 20:47:23 +00:00
|
|
|
num_epochs = 1
|
2022-01-12 05:47:01 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir, max_epochs=num_epochs, strategy="ddp_spawn", accelerator="gpu", devices=2
|
|
|
|
)
|
2021-07-26 11:37:35 +00:00
|
|
|
temp_path = os.path.join(tmpdir, "temp.pt")
|
2020-10-13 20:47:23 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
# Ensure these do not fail
|
|
|
|
torch.save(trainer.model, temp_path)
|
|
|
|
torch.save(trainer, temp_path)
|