Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / trainer / callbacks / garbage_collection_callback.py: 44%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-10-25 05:45 +0800

1# Copyright 2026 Huawei Technologies Co., Ltd 

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"""Periodic Python and accelerator garbage collection callback.""" 

16 

17import gc 

18from typing import Any 

19 

20from hyper_parallel.trainer.runtime.device import empty_cache, get_device_type 

21 

22from .base import Callback, TrainerState 

23 

24 

25class GarbageCollectionCallback(Callback): 

26 """Run Python GC and device cache cleanup on independent step cadences.""" 

27 

28 def __init__(self, trainer: Any) -> None: 

29 """Read cleanup intervals from the Trainer configuration. 

30 

31 Args: 

32 trainer: Trainer that owns the callback lifecycle. 

33 """ 

34 super().__init__(trainer) 

35 training_config = trainer.config.training 

36 self.gc_steps = training_config.gc_steps 

37 self.empty_cache_steps = training_config.empty_cache_steps 

38 

39 def on_step_end(self, state: TrainerState, **kwargs: Any) -> None: 

40 """Run configured cleanup operations after an optimizer step.""" 

41 del kwargs 

42 if self.gc_steps > 0 and state.global_step % self.gc_steps == 0: 

43 gc.collect() 

44 if ( 

45 self.empty_cache_steps > 0 

46 and state.global_step % self.empty_cache_steps == 0 

47 and get_device_type() != "cpu" 

48 ): 

49 empty_cache()