Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / core / activation_memory / policy.py: 100%

7 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"""Checkpoint policy enum shared by the activation-checkpoint implementations.""" 

16import enum 

17 

18 

19class CheckpointPolicy(enum.Enum): 

20 """ 

21 Enum for specifying the policy for checkpointing during backpropagation. 

22 

23 This enum extends PyTorch's selective activation checkpointing policies 

24 by introducing a SWAP-based strategy, which allows activation tensors 

25 to be offloaded during the forward pass and loaded back before backward 

26 computation. 

27 

28 For PyTorch native policies (SAVE / RECOMPUTE semantics and MUST vs PREFER), 

29 see: https://docs.pytorch.org/docs/2.6/checkpoint.html#torch.utils.checkpoint.CheckpointPolicy 

30 

31 Additional policy: 

32 

33 - ``MUST_SWAP``: The operation's output is offloaded to host memory during the 

34 forward pass and loaded back asynchronously before backward computation. The backward 

35 pass reuses the loaded activations without recomputation. 

36 

37 This policy must be used together with :class:`SwapManager` to coordinate 

38 asynchronous offload/load and stream synchronization. 

39 

40 .. note:: 

41 ``MUST_SWAP`` is typically applied to operations that are either 

42 computationally expensive or have large memory footprints. Note that 

43 swapping very small outputs may introduce additional overhead and 

44 reduce the effectiveness of asynchronous copy. 

45 """ 

46 MUST_SAVE = 0 

47 PREFER_SAVE = 1 

48 MUST_RECOMPUTE = 2 

49 PREFER_RECOMPUTE = 3 

50 

51 # Offload during forward, reload before backward. Requires SwapManager. 

52 MUST_SWAP = 4