|
| 1 | +from distutils.version import LooseVersion |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | + |
| 6 | +from tests.base import EvalModelTemplate |
| 7 | +from tests.base.datamodules import TrialMNISTDataModule |
| 8 | +from tests.base.models import ParityModuleRNN, BasicGAN |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("modelclass", [ |
| 12 | + EvalModelTemplate, |
| 13 | + ParityModuleRNN, |
| 14 | + BasicGAN, |
| 15 | +]) |
| 16 | +def test_torchscript_input_output(modelclass): |
| 17 | + """ Test that scripted LightningModule forward works. """ |
| 18 | + model = modelclass() |
| 19 | + script = model.to_torchscript() |
| 20 | + assert isinstance(script, torch.jit.ScriptModule) |
| 21 | + model.eval() |
| 22 | + model_output = model(model.example_input_array) |
| 23 | + script_output = script(model.example_input_array) |
| 24 | + assert torch.allclose(script_output, model_output) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("device", [ |
| 28 | + torch.device("cpu"), |
| 29 | + torch.device("cuda", 0) |
| 30 | +]) |
| 31 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires GPU machine") |
| 32 | +def test_torchscript_device(device): |
| 33 | + """ Test that scripted module is on the correct device. """ |
| 34 | + model = EvalModelTemplate().to(device) |
| 35 | + script = model.to_torchscript() |
| 36 | + assert next(script.parameters()).device == device |
| 37 | + script_output = script(model.example_input_array.to(device)) |
| 38 | + assert script_output.device == device |
| 39 | + |
| 40 | + |
| 41 | +def test_torchscript_retain_training_state(): |
| 42 | + """ Test that torchscript export does not alter the training mode of original model. """ |
| 43 | + model = EvalModelTemplate() |
| 44 | + model.train(True) |
| 45 | + script = model.to_torchscript() |
| 46 | + assert model.training |
| 47 | + assert not script.training |
| 48 | + model.train(False) |
| 49 | + _ = model.to_torchscript() |
| 50 | + assert not model.training |
| 51 | + assert not script.training |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("modelclass", [ |
| 55 | + EvalModelTemplate, |
| 56 | + ParityModuleRNN, |
| 57 | + BasicGAN, |
| 58 | +]) |
| 59 | +def test_torchscript_properties(modelclass): |
| 60 | + """ Test that scripted LightningModule has unnecessary methods removed. """ |
| 61 | + model = modelclass() |
| 62 | + model.datamodule = TrialMNISTDataModule() |
| 63 | + script = model.to_torchscript() |
| 64 | + assert not hasattr(script, "datamodule") |
| 65 | + assert not hasattr(model, "batch_size") or hasattr(script, "batch_size") |
| 66 | + assert not hasattr(model, "learning_rate") or hasattr(script, "learning_rate") |
| 67 | + |
| 68 | + if LooseVersion(torch.__version__) >= LooseVersion("1.4.0"): |
| 69 | + # only on torch >= 1.4 do these unused methods get removed |
| 70 | + assert not callable(getattr(script, "training_step", None)) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("modelclass", [ |
| 74 | + EvalModelTemplate, |
| 75 | + ParityModuleRNN, |
| 76 | + BasicGAN, |
| 77 | +]) |
| 78 | +@pytest.mark.skipif( |
| 79 | + LooseVersion(torch.__version__) < LooseVersion("1.5.0"), |
| 80 | + reason="torch.save/load has bug loading script modules on torch <= 1.4", |
| 81 | +) |
| 82 | +def test_torchscript_save_load(tmpdir, modelclass): |
| 83 | + """ Test that scripted LightningModules is correctly saved and can be loaded. """ |
| 84 | + model = modelclass() |
| 85 | + output_file = str(tmpdir / "model.pt") |
| 86 | + script = model.to_torchscript(file_path=output_file) |
| 87 | + loaded_script = torch.jit.load(output_file) |
| 88 | + assert torch.allclose(next(script.parameters()), next(loaded_script.parameters())) |
0 commit comments