Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / platform / torch / activation_checkpoint / checkpoint_wrapper.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-07-06 05:41 +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"""Activation checkpoint wrapper implementation for PyTorch.""" 

16from typing import Any, Callable, Union 

17 

18from torch import nn 

19 

20from hyper_parallel.platform.torch.activation_checkpoint.activation_swap import ActivationWrapper 

21 

22 

23class CheckpointWrapper(ActivationWrapper): 

24 """Wrap a module with activation checkpointing.""" 

25 

26 def __init__( 

27 self, 

28 mod: Union[nn.Module, Callable], 

29 **checkpoint_kwargs: Any, 

30 ): 

31 super().__init__(mod) 

32 self.checkpoint_kwargs = checkpoint_kwargs 

33 

34 def _do_checkpoint(self, wrapped_module: Any, *args: Any, **kwargs: Any) -> Any: 

35 # Checkpoint may save inputs before the wrapped module's pre-hook runs. 

36 group_name = getattr(self, "_swap_group_name", None) 

37 if group_name is not None: 

38 from hyper_parallel.core.activation_checkpoint.swap import SwapManager # pylint: disable=C0415 

39 SwapManager().set_current_group_name(group_name) 

40 

41 from hyper_parallel.core.activation_checkpoint.activation_checkpoint import checkpoint # pylint: disable=C0415 

42 return checkpoint( 

43 wrapped_module, 

44 *args, 

45 **self.checkpoint_kwargs, 

46 **kwargs, 

47 ) 

48 

49 def forward(self, *args: Any, **kwargs: Any) -> Any: 

50 return self._do_checkpoint(self._wrapped_module, *args, **kwargs) 

51 

52 

53def ckpt_wrapper(module: Union[nn.Module, Callable], **checkpoint_kwargs: Any) -> CheckpointWrapper: 

54 """Wrap *module* with activation checkpointing.""" 

55 return CheckpointWrapper(module, **checkpoint_kwargs)