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

14 statements  

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

1# Copyright 2025 Bytedance Ltd. and/or its affiliates 

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 

15from typing import TYPE_CHECKING, Any, Dict, List 

16 

17from hyper_parallel.trainer.state import TrainerState 

18 

19 

20if TYPE_CHECKING: 

21 from hyper_parallel.trainer.base import BaseTrainer 

22 

23 

24class Callback: 

25 def __init__(self, trainer: "BaseTrainer") -> None: 

26 """Bind the callback to its owning trainer.""" 

27 self.trainer = trainer 

28 self.mesh = trainer.mesh 

29 

30 def on_step_begin(self, state: TrainerState, micro_batches: List[Dict[str, Any]] = None, **kwargs: Any) -> None: 

31 """Hook invoked at the start of each training step.""" 

32 

33 def on_step_end( 

34 self, state: TrainerState, loss: float, loss_dict: Dict[str, float], grad_norm: float, **kwargs: Any 

35 ) -> None: 

36 """Hook invoked at the end of each training step.""" 

37 

38 def on_micro_step_begin(self, state: TrainerState, micro_batch: Dict[str, Any], **kwargs: Any) -> None: 

39 """Hook invoked at the start of each gradient-accumulation micro step.""" 

40 

41 def on_micro_step_end(self, state: TrainerState, **kwargs: Any) -> None: 

42 """Hook invoked at the end of each gradient-accumulation micro step.""" 

43 

44 def on_epoch_begin(self, state: TrainerState, **kwargs: Any) -> None: 

45 """Hook invoked at the start of each epoch.""" 

46 

47 def on_epoch_end(self, state: TrainerState, **kwargs: Any) -> None: 

48 """Hook invoked at the end of each epoch.""" 

49 

50 def on_train_begin(self, state: TrainerState, **kwargs: Any) -> None: 

51 """Hook invoked at the start of training.""" 

52 

53 def on_train_end(self, state: TrainerState, **kwargs: Any) -> None: 

54 """Hook invoked at the end of training."""