|
| 1 | +# Copyright The PyTorch Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import math |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from pytorch_lightning import Trainer |
| 20 | +from pytorch_lightning.callbacks import GradientAccumulationScheduler |
| 21 | +from pytorch_lightning.utilities.exceptions import MisconfigurationException |
| 22 | +from tests.helpers import BoringModel |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize("accumulate_grad_batches", (1, 2, 3)) |
| 26 | +def test_trainer_accumulate_grad_batches_zero_grad(tmpdir, accumulate_grad_batches): |
| 27 | + with patch("torch.optim.SGD.zero_grad") as sgd_zero_grad: |
| 28 | + model = BoringModel() |
| 29 | + trainer = Trainer( |
| 30 | + default_root_dir=tmpdir, |
| 31 | + limit_train_batches=20, |
| 32 | + limit_val_batches=1, |
| 33 | + max_epochs=1, |
| 34 | + weights_summary=None, |
| 35 | + accumulate_grad_batches=accumulate_grad_batches, |
| 36 | + ) |
| 37 | + assert trainer.accumulate_grad_batches == accumulate_grad_batches |
| 38 | + trainer.fit(model) |
| 39 | + |
| 40 | + assert sum(isinstance(cb, GradientAccumulationScheduler) for cb in trainer.callbacks) == 1 |
| 41 | + assert sgd_zero_grad.call_count == math.ceil(trainer.limit_train_batches / accumulate_grad_batches) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + ["accumulate_grad_batches", "expected_call_count"], |
| 46 | + [ |
| 47 | + ({1: 2, 3: 4}, 10 + 5 + 5 + 3), |
| 48 | + ({0: 2, 2: 1}, 5 + 5 + 10 + 10), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_trainer_accumulate_grad_batches_dict_zero_grad(tmpdir, accumulate_grad_batches, expected_call_count): |
| 52 | + with patch("torch.optim.SGD.zero_grad") as sgd_zero_grad: |
| 53 | + model = BoringModel() |
| 54 | + trainer = Trainer( |
| 55 | + default_root_dir=tmpdir, |
| 56 | + limit_train_batches=10, |
| 57 | + limit_val_batches=1, |
| 58 | + max_epochs=4, |
| 59 | + weights_summary=None, |
| 60 | + accumulate_grad_batches=accumulate_grad_batches, |
| 61 | + ) |
| 62 | + assert trainer.accumulate_grad_batches == accumulate_grad_batches.get(0, 1) |
| 63 | + trainer.fit(model) |
| 64 | + |
| 65 | + assert sum(isinstance(cb, GradientAccumulationScheduler) for cb in trainer.callbacks) == 1 |
| 66 | + assert sgd_zero_grad.call_count == expected_call_count |
| 67 | + |
| 68 | + |
| 69 | +def test_trainer_accumulate_grad_batches_with_callback(tmpdir): |
| 70 | + with patch("torch.optim.SGD.zero_grad") as sgd_zero_grad: |
| 71 | + model = BoringModel() |
| 72 | + trainer = Trainer( |
| 73 | + default_root_dir=tmpdir, |
| 74 | + limit_train_batches=10, |
| 75 | + limit_val_batches=1, |
| 76 | + max_epochs=4, |
| 77 | + weights_summary=None, |
| 78 | + callbacks=[GradientAccumulationScheduler({1: 2, 3: 4})], |
| 79 | + ) |
| 80 | + assert trainer.accumulate_grad_batches == 1 |
| 81 | + trainer.fit(model) |
| 82 | + |
| 83 | + assert sum(isinstance(cb, GradientAccumulationScheduler) for cb in trainer.callbacks) == 1 |
| 84 | + assert sgd_zero_grad.call_count == 10 + 5 + 5 + 3 |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "scheduling", |
| 89 | + [ |
| 90 | + {1: 2, -3: 4}, |
| 91 | + {0: 2, "2": 1}, |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_invalid_keys_for_grad_accum_scheduler(scheduling): |
| 95 | + with pytest.raises(MisconfigurationException, match="Epoch should be an int"): |
| 96 | + _ = GradientAccumulationScheduler(scheduling=scheduling) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "scheduling", |
| 101 | + [ |
| 102 | + {1: 0, 3: 4}, |
| 103 | + {0: 2, 2: "2"}, |
| 104 | + ], |
| 105 | +) |
| 106 | +def test_invalid_values_for_grad_accum_scheduler(scheduling): |
| 107 | + with pytest.raises(MisconfigurationException, match="Accumulation factor should be an int"): |
| 108 | + _ = GradientAccumulationScheduler(scheduling=scheduling) |
0 commit comments