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

43 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-08-21 04:29 +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"""Storage interfaces for checkpoint save and load.""" 

16import abc 

17from dataclasses import dataclass 

18from pathlib import Path 

19from typing import Optional, Union 

20 

21from hyper_parallel.core.distributed_checkpoint.metadata import Metadata, MetadataIndex 

22from hyper_parallel.core.distributed_checkpoint.planner import LoadPlan, LoadPlanner, SavePlan, SavePlanner 

23 

24METADATA_FILE_NAME = ".metadata" 

25 

26 

27@dataclass 

28class StorageInfo: 

29 """ 

30 Storage information for a single logical item. 

31 

32 Torch-aligned: matches torch.distributed.checkpoint.filesystem._StorageInfo. 

33 

34 Attributes: 

35 relative_path: Path relative to checkpoint root. 

36 offset: Byte offset within the file. 

37 length: Byte length of the data (best-effort, may be -1 for tensors). 

38 tensor_key: Optional physical key used inside a tensor container file. 

39 """ 

40 relative_path: str 

41 offset: int 

42 length: int 

43 tensor_key: Optional[str] = None 

44 

45 

46@dataclass 

47class WriteResult: 

48 """ 

49 Result of writing a single logical item. 

50 

51 Torch-aligned: contains the metadata index and storage information. 

52 """ 

53 index: MetadataIndex 

54 storage_data: StorageInfo 

55 

56 

57class StorageWriter(abc.ABC): 

58 """ 

59 Abstract base class for storage writers. 

60 

61 Defines the interface for writing checkpoint data to storage backends. 

62 """ 

63 

64 @abc.abstractmethod 

65 def initialize_writer(self, checkpoint_id: Optional[Union[Path, str]] = None) -> None: 

66 """ 

67 Initialize storage writer with optional new checkpoint directory. 

68 

69 Args: 

70 checkpoint_id (Optional[Union[Path, str]]): The ID/path of the checkpoint directory. 

71 If None, uses the previously configured checkpoint directory. Default None. 

72 """ 

73 

74 @abc.abstractmethod 

75 def configure_writer(self, is_coordinator: bool, **kwargs) -> None: 

76 """ 

77 Configure storage writer with coordinator and rank information. 

78 

79 Args: 

80 is_coordinator (bool): Whether this rank is the coordinator rank. 

81 **kwargs: Additional keyword arguments (e.g., rank, use_collectives). 

82 """ 

83 

84 @abc.abstractmethod 

85 def optimize_local_plan(self, plan: SavePlan) -> SavePlan: 

86 """ 

87 Optimize local plan for storage-specific optimizations. 

88 

89 Args: 

90 plan (SavePlan): The local save plan to optimize. 

91 

92 Returns: 

93 SavePlan: The optimized local save plan. 

94 """ 

95 

96 @abc.abstractmethod 

97 def optimize_global_plan(self, plans: list[SavePlan]) -> list[SavePlan]: 

98 """ 

99 Optimize global plan from all local plans. 

100 

101 Args: 

102 plans (list[SavePlan]): List of local save plans from all ranks. 

103 

104 Returns: 

105 list[SavePlan]: List of optimized global save plans. 

106 """ 

107 

108 @abc.abstractmethod 

109 def execute_write(self, plan: SavePlan, planner: SavePlanner) -> list[WriteResult]: 

110 """ 

111 Execute write operation to storage and return write results. 

112 

113 Args: 

114 plan (SavePlan): The save plan to execute. 

115 planner (SavePlanner): The save planner instance for accessing tensor data. 

116 

117 Returns: 

118 list[WriteResult]: List of write results containing storage information for each written item. 

119 """ 

120 

121 @abc.abstractmethod 

122 def finalize_checkpoint(self, metadata: Metadata, results: list[list[WriteResult]]) -> None: 

123 """ 

124 Finalize checkpoint writing and complete metadata. 

125 

126 Args: 

127 metadata (Metadata): The checkpoint metadata to finalize. 

128 results (list[list[WriteResult]]): List of write results from all ranks, 

129 where each inner list contains WriteResults from one rank. 

130 """ 

131 

132 

133class StorageReader(abc.ABC): 

134 """ 

135 Abstract base class for storage readers. 

136 

137 Defines the interface for reading checkpoint data from storage backends. 

138 """ 

139 

140 @abc.abstractmethod 

141 def initialize_reader(self, checkpoint_id: Optional[Union[Path, str]] = None) -> None: 

142 """ 

143 Initialize storage reader with optional new checkpoint directory. 

144 

145 Args: 

146 checkpoint_id (Optional[Union[Path, str]]): The ID/path of the checkpoint directory. 

147 If None, uses the previously configured checkpoint directory. Default None. 

148 """ 

149 

150 @abc.abstractmethod 

151 def load_metadata(self, **kwargs) -> Metadata: 

152 """ 

153 Load checkpoint metadata from storage. 

154 

155 Args: 

156 **kwargs: Additional keyword arguments (e.g., rank for rank-local metadata). 

157 

158 Returns: 

159 Metadata: The loaded checkpoint metadata. 

160 """ 

161 

162 @abc.abstractmethod 

163 def configure_reader(self, metadata: Metadata, is_coordinator: bool, **kwargs) -> None: 

164 """ 

165 Configure storage reader with metadata and coordinator information. 

166 

167 Args: 

168 metadata (Metadata): The checkpoint metadata. 

169 is_coordinator (bool): Whether this rank is the coordinator rank. 

170 **kwargs: Additional keyword arguments (e.g., rank, use_collectives). 

171 """ 

172 

173 @abc.abstractmethod 

174 def optimize_local_plan(self, plan: LoadPlan) -> LoadPlan: 

175 """ 

176 Optimize local plan for storage-specific optimizations. 

177 

178 Args: 

179 plan (LoadPlan): The local load plan to optimize. 

180 

181 Returns: 

182 LoadPlan: The optimized local load plan. 

183 """ 

184 

185 @abc.abstractmethod 

186 def optimize_global_plan(self, plans: list[LoadPlan]) -> list[LoadPlan]: 

187 """ 

188 Optimize global plan from all local plans. 

189 

190 Args: 

191 plans (list[LoadPlan]): List of local load plans from all ranks. 

192 

193 Returns: 

194 list[LoadPlan]: List of optimized global load plans. 

195 """ 

196 

197 @abc.abstractmethod 

198 def execute_read(self, plan: LoadPlan, planner: LoadPlanner) -> None: 

199 """ 

200 Execute read operation from storage according to the load plan. 

201 

202 Args: 

203 plan (LoadPlan): The load plan to execute. 

204 planner (LoadPlanner): The load planner instance for applying loaded data. 

205 """