|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 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 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from nemo.collections.speechlm2.parts.gc import GarbageCollectionManager |
| 18 | + |
| 19 | + |
| 20 | +def test_garbage_collection_manager_owns_step_state(monkeypatch): |
| 21 | + calls = [] |
| 22 | + |
| 23 | + class FakeGarbageCollection: |
| 24 | + def __init__(self, gc_every_steps): |
| 25 | + calls.append(("init", gc_every_steps)) |
| 26 | + |
| 27 | + def run(self, step_count): |
| 28 | + calls.append(("run", step_count)) |
| 29 | + |
| 30 | + import nemo_automodel.components.training.garbage_collection as gc_module |
| 31 | + |
| 32 | + monkeypatch.setattr(gc_module, "GarbageCollection", FakeGarbageCollection) |
| 33 | + manager = GarbageCollectionManager(gc_every_steps=10) |
| 34 | + |
| 35 | + manager.on_fit_start() |
| 36 | + manager.on_optimizer_step() |
| 37 | + manager.on_optimizer_step() |
| 38 | + |
| 39 | + assert calls == [("init", 10), ("run", 1), ("run", 2)] |
| 40 | + |
| 41 | + |
| 42 | +def test_garbage_collection_manager_is_noop_when_disabled(): |
| 43 | + manager = GarbageCollectionManager(gc_every_steps=None) |
| 44 | + |
| 45 | + manager.on_fit_start() |
| 46 | + manager.on_optimizer_step() |
| 47 | + |
| 48 | + assert manager._collector is None |
| 49 | + assert manager._optimizer_step_count == 0 |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("value", [True, False, 0, -1, 1.5, "10"]) |
| 53 | +def test_garbage_collection_manager_rejects_invalid_interval(value): |
| 54 | + with pytest.raises(ValueError, match="gc_every_steps"): |
| 55 | + GarbageCollectionManager(gc_every_steps=value) |
0 commit comments