Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / distributed / mesh.py: 84%

193 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-12-05 05:12 +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"""Mesh domains and the distributed-setup container for AutoModels. 

16 

17Split out of the former ``components/distributed/infrastructure.py`` in 

18stage 7 (05 §15.11 step 3): the mesh topology objects are AutoModels-side 

19model-construction inputs, while process-group lifecycle helpers moved to 

20``hyper_parallel.trainer.runtime.distributed`` / 

21``hyper_parallel.trainer.runtime.logging``. 

22""" 

23 

24from __future__ import annotations 

25 

26__all__ = [ 

27 "DistributedSetup", 

28 "LogicalDeviceMesh", 

29 "MeshContext", 

30 "ParallelLayout", 

31 "ParallelTopologySpec", 

32 "infer_minimum_world_size", 

33 "resolve_parallel_layout", 

34] 

35 

36import inspect 

37import math 

38from dataclasses import dataclass, field 

39from typing import Any, Optional, Tuple, Union 

40 

41import torch.distributed as dist 

42 

43from hyper_parallel import init_device_mesh 

44 

45 

46def _compatible_parallel_degree(value: Any) -> Any: 

47 """Preserve the Trainer's historical minimum parallel degree of one.""" 

48 return max(1, value) 

49 

50 

51def _positive_world_size(value: Any) -> int: 

52 """Validate the process-group or inferred world size.""" 

53 if isinstance(value, bool) or not isinstance(value, int) or value < 1: 

54 raise ValueError(f"world_size must be a positive integer, got {value!r}") 

55 return value 

56 

57 

58@dataclass(frozen=True) 

59class ParallelTopologySpec: 

60 """Normalized parallel degrees shared by runtime and offline planning.""" 

61 

62 tp_size: int 

63 cp_size: int 

64 pp_size: int 

65 ep_size: int 

66 dp_shard_size: int 

67 edp_shard_size: int 

68 sequence_parallel: bool 

69 loss_parallel: bool 

70 

71 @classmethod 

72 def from_config(cls, config: Any) -> "ParallelTopologySpec": 

73 """Read parallel degrees using the Trainer's compatibility semantics.""" 

74 accelerator = config.accelerator 

75 fsdp_config = config.fsdp_config 

76 return cls( 

77 tp_size=_compatible_parallel_degree(accelerator.tp_size), 

78 cp_size=_compatible_parallel_degree(accelerator.cp_size), 

79 pp_size=_compatible_parallel_degree(accelerator.pp_size), 

80 ep_size=_compatible_parallel_degree(accelerator.ep_size), 

81 dp_shard_size=_compatible_parallel_degree(fsdp_config.dp_shard_size), 

82 edp_shard_size=_compatible_parallel_degree(fsdp_config.edp_shard_size), 

83 sequence_parallel=bool(accelerator.sequence_parallel), 

84 loss_parallel=bool(accelerator.loss_parallel), 

85 ) 

86 

87 

88@dataclass(frozen=True) 

89class ParallelLayout: 

90 """Resolved parallel topology independent of devices and process groups.""" 

91 

92 spec: ParallelTopologySpec 

93 world_size: int 

94 dp_size: int 

95 dp_replicate_size: int 

96 

97 @property 

98 def dense_world_size(self) -> int: 

99 """Return the rank count in one pipeline stage.""" 

100 return self.dp_size * self.spec.cp_size * self.spec.tp_size 

101 

102 

103def resolve_parallel_layout( 

104 spec: ParallelTopologySpec, world_size: int 

105) -> ParallelLayout: 

106 """Validate a world size and resolve its DP/FSDP/EP layout.""" 

107 world_size = _positive_world_size(world_size) 

108 non_dp_size = spec.tp_size * spec.cp_size * spec.pp_size 

109 if world_size % non_dp_size != 0: 

110 raise ValueError( 

111 f"world_size {world_size} is not divisible by non-DP size " 

112 f"{non_dp_size}" 

113 ) 

114 dp_size = world_size // non_dp_size 

115 fsdp_data_parallel_size = dp_size * spec.cp_size 

116 if fsdp_data_parallel_size % spec.dp_shard_size != 0: 

117 raise ValueError( 

118 "DP+CP size " 

119 f"{fsdp_data_parallel_size} is not divisible by FSDP shard size " 

120 f"{spec.dp_shard_size}" 

121 ) 

122 dense_world_size = dp_size * spec.cp_size * spec.tp_size 

123 if dense_world_size % spec.ep_size != 0: 

124 raise ValueError( 

125 f"expert domain size ({dense_world_size}) must be divisible by " 

126 f"ep_size ({spec.ep_size})" 

127 ) 

128 expert_data_parallel_size = dense_world_size // spec.ep_size 

129 if expert_data_parallel_size % spec.edp_shard_size != 0: 

130 raise ValueError( 

131 f"expert data-parallel size ({expert_data_parallel_size}) must be " 

132 f"divisible by edp_shard_size ({spec.edp_shard_size})" 

133 ) 

134 return ParallelLayout( 

135 spec=spec, 

136 world_size=world_size, 

137 dp_size=dp_size, 

138 dp_replicate_size=fsdp_data_parallel_size // spec.dp_shard_size, 

139 ) 

140 

141 

142def infer_minimum_world_size(spec: ParallelTopologySpec) -> int: 

143 """Return the smallest world size satisfying every parallel contract.""" 

144 dense_world_size = math.lcm( 

145 spec.tp_size * spec.cp_size, 

146 spec.ep_size, 

147 spec.tp_size * spec.dp_shard_size, 

148 spec.ep_size * spec.edp_shard_size, 

149 ) 

150 return dense_world_size * spec.pp_size 

151 

152 

153@dataclass(frozen=True) 

154class LogicalDeviceMesh: 

155 """Process-group-free subset of the DeviceMesh planning interface.""" 

156 

157 mesh_dim_names: tuple[str, ...] 

158 mesh_shape: tuple[int, ...] 

159 

160 @property 

161 def shape(self) -> tuple[int, ...]: 

162 """Return the logical mesh shape.""" 

163 return self.mesh_shape 

164 

165 def __getitem__( 

166 self, mesh_dim_names: Union[str, Tuple[str, ...]] 

167 ) -> "LogicalDeviceMesh": 

168 """Return a logical submesh selected by dimension name.""" 

169 names = ( 

170 (mesh_dim_names,) 

171 if isinstance(mesh_dim_names, str) 

172 else tuple(mesh_dim_names) 

173 ) 

174 missing = tuple(name for name in names if name not in self.mesh_dim_names) 

175 if missing: 

176 raise KeyError(f"logical mesh has no dimensions {missing!r}") 

177 shape_by_name = dict(zip(self.mesh_dim_names, self.mesh_shape)) 

178 return LogicalDeviceMesh( 

179 mesh_dim_names=names, 

180 mesh_shape=tuple(shape_by_name[name] for name in names), 

181 ) 

182 

183 def size(self, mesh_dim: Optional[Union[int, str]] = None) -> int: 

184 """Return the total or selected logical dimension size.""" 

185 if mesh_dim is None: 

186 return math.prod(self.mesh_shape) 

187 index = ( 

188 self.mesh_dim_names.index(mesh_dim) 

189 if isinstance(mesh_dim, str) 

190 else mesh_dim 

191 ) 

192 return self.mesh_shape[index] 

193 

194 

195def _init_topology_mesh(mesh_kwargs: dict[str, Any]) -> Any: 

196 """Create a mesh while preserving compatibility with lightweight test fakes.""" 

197 supported_arguments = inspect.signature(init_device_mesh).parameters 

198 filtered_kwargs = { 

199 name: value 

200 for name, value in mesh_kwargs.items() 

201 if name in supported_arguments 

202 } 

203 return init_device_mesh(**filtered_kwargs) 

204 

205 

206@dataclass 

207class MeshContext: 

208 """Runtime topology and the mesh domains consumed by dual-mode Trainer.""" 

209 device_mesh: Any = None 

210 fsdp_non_moe_mesh: Any = None 

211 fsdp_moe_mesh: Any = None 

212 dp_size: int = 1 

213 dp_replicate_size: int = 1 

214 dp_shard_size: int = 1 

215 edp_shard_size: int = 1 

216 tp_size: int = 1 

217 cp_size: int = 1 

218 pp_size: int = 1 

219 ep_size: int = 1 

220 dp_rank: int = 0 

221 tp_rank: int = 0 

222 cp_rank: int = 0 

223 pp_rank: int = 0 

224 ep_rank: int = 0 

225 sequence_parallel: bool = False 

226 loss_parallel: bool = False 

227 pp_mesh: Any = None 

228 

229 @classmethod 

230 def from_layout(cls, layout: ParallelLayout) -> "MeshContext": 

231 """Create common mesh metadata from a resolved parallel layout.""" 

232 spec = layout.spec 

233 return cls( 

234 dp_size=layout.dp_size, 

235 dp_replicate_size=layout.dp_replicate_size, 

236 dp_shard_size=spec.dp_shard_size, 

237 edp_shard_size=spec.edp_shard_size, 

238 tp_size=spec.tp_size, 

239 cp_size=spec.cp_size, 

240 pp_size=spec.pp_size, 

241 ep_size=spec.ep_size, 

242 sequence_parallel=spec.sequence_parallel, 

243 loss_parallel=spec.loss_parallel, 

244 ) 

245 

246 @property 

247 def pp_enabled(self) -> bool: 

248 """Whether pipeline parallelism is enabled (pp_size > 1).""" 

249 return self.pp_size > 1 

250 

251 @property 

252 def dp_cp_mesh(self) -> Any: 

253 """DP+CP joint mesh for all-reduce. 

254 

255 Mirrors the derivation in ``FinetuneRecipe.setup()``: take the DP 

256 dimension together with CP (if present) and flatten to 1-D. 

257 """ 

258 if self.device_mesh is None: 

259 return None 

260 dim_names = self.device_mesh.mesh_dim_names or () 

261 selected_dims = tuple(dim_name for dim_name in ("dp", "cp") if dim_name in dim_names) 

262 if not selected_dims: 

263 selected_dims = tuple( 

264 dim_name 

265 for dim_name in ("dp_replicate", "dp_shard", "cp") 

266 if dim_name in dim_names 

267 ) 

268 if not selected_dims: 

269 return None 

270 sub = ( 

271 self.device_mesh[selected_dims[0]] 

272 if len(selected_dims) == 1 

273 else self.device_mesh[selected_dims] 

274 ) 

275 if sub.ndim > 1: 

276 sub = sub.flatten("dp_cp") 

277 return sub 

278 

279 @property 

280 def cp_mesh(self) -> Any: 

281 """CP submesh for CP utilities (e.g. shard_batch_for_cp). 

282 

283 Returns ``None`` when context parallelism is disabled (cp_size == 1) 

284 or no DeviceMesh is available. 

285 """ 

286 if self.device_mesh is None: 

287 return None 

288 dim_names = self.device_mesh.mesh_dim_names or () 

289 if "cp" in dim_names: 

290 return self.device_mesh["cp"] 

291 return None 

292 

293 def build_meshs(self, device_type: str, world_size: int) -> None: 

294 """Build the device, dense-FSDP and expert-FSDP mesh domains. 

295 

296 The device mesh preserves the existing (dp, cp, tp) rank order. Dense 

297 FSDP flattens the DP+CP domain and reshapes it according to the 

298 configured shard size, while expert FSDP derives an (edp, ep) 

299 topology from the flattened device domain. 

300 

301 Args: 

302 device_type: Device type accepted by init_device_mesh. 

303 world_size: Number of ranks in the process group. 

304 

305 Raises: 

306 ValueError: If the configured topology cannot cover world_size or 

307 cannot form the requested expert mesh. 

308 """ 

309 dense_world_size = self.dp_size * self.cp_size * self.tp_size 

310 expected_world_size = dense_world_size * self.pp_size 

311 if expected_world_size != world_size: 

312 raise ValueError( 

313 "dp_size * cp_size * tp_size * pp_size " 

314 f"must equal world_size ({expected_world_size} != {world_size})" 

315 ) 

316 fsdp_data_parallel_size = self.dp_size * self.cp_size 

317 configured_fsdp_size = self.dp_replicate_size * self.dp_shard_size 

318 if configured_fsdp_size != fsdp_data_parallel_size: 

319 raise ValueError( 

320 "dp_replicate_size * dp_shard_size must equal dp_size * cp_size " 

321 f"({configured_fsdp_size} != {fsdp_data_parallel_size})" 

322 ) 

323 

324 init_backend = dist.is_initialized() 

325 device_mesh_kwargs = { 

326 "device_type": device_type, 

327 "mesh_shape": (self.dp_size, self.cp_size, self.tp_size), 

328 "mesh_dim_names": ("dp", "cp", "tp"), 

329 "init_backend": init_backend, 

330 } 

331 self.device_mesh = self._stage_mesh(device_mesh_kwargs) 

332 

333 dense_mesh_kwargs = { 

334 "device_type": device_type, 

335 "mesh_shape": ( 

336 self.dp_replicate_size, 

337 self.dp_shard_size, 

338 self.tp_size, 

339 ), 

340 "mesh_dim_names": ("fsdp_replicate", "fsdp_shard", "tp"), 

341 "init_backend": init_backend, 

342 } 

343 self.fsdp_non_moe_mesh = self._stage_mesh(dense_mesh_kwargs) 

344 _validate_dense_tp_rank_layout(self.device_mesh, self.fsdp_non_moe_mesh) 

345 self._build_expert_parallel_mesh(device_type, init_backend) 

346 

347 def _stage_mesh(self, kwargs: dict[str, Any]) -> Any: 

348 """Create every stage's group in the same order before selecting the local tile.""" 

349 if not kwargs["init_backend"] or self.pp_size == 1: 

350 return _init_topology_mesh(kwargs) 

351 names = kwargs["mesh_dim_names"] 

352 root = _init_topology_mesh({ 

353 **kwargs, "mesh_shape": (self.pp_size, *kwargs["mesh_shape"]), "mesh_dim_names": ("pp", *names), 

354 }) 

355 if self.pp_mesh is None: 

356 self.pp_mesh = root["pp"] 

357 return root[names] 

358 

359 def _build_expert_parallel_mesh(self, device_type: str, init_backend: bool) -> None: 

360 """Build the expert-parallel FSDP mesh when expert parallelism is enabled.""" 

361 self.fsdp_moe_mesh = None 

362 if self.ep_size <= 1: 

363 return 

364 expert_domain_size = math.prod(self.device_mesh.mesh_shape) 

365 if expert_domain_size % self.ep_size != 0: 

366 raise ValueError( 

367 f"expert domain size ({expert_domain_size}) must be divisible by " 

368 f"ep_size ({self.ep_size})" 

369 ) 

370 edp_size = expert_domain_size // self.ep_size 

371 if edp_size % self.edp_shard_size != 0: 

372 raise ValueError( 

373 f"expert data-parallel size ({edp_size}) must be divisible by " 

374 f"edp_shard_size ({self.edp_shard_size})" 

375 ) 

376 edp_replicate_size = edp_size // self.edp_shard_size 

377 expert_mesh_shape = ( 

378 (edp_replicate_size, self.edp_shard_size, self.ep_size) 

379 if edp_replicate_size > 1 

380 else (self.edp_shard_size, self.ep_size) 

381 ) 

382 expert_mesh_names = ( 

383 ("edp_replicate", "edp_shard", "ep") 

384 if edp_replicate_size > 1 

385 else ("edp_shard", "ep") 

386 ) 

387 expert_mesh_kwargs = { 

388 "device_type": device_type, 

389 "mesh_shape": expert_mesh_shape, 

390 "mesh_dim_names": expert_mesh_names, 

391 "init_backend": init_backend, 

392 } 

393 self.fsdp_moe_mesh = self._stage_mesh(expert_mesh_kwargs) 

394 

395 

396def _validate_dense_tp_rank_layout(device_mesh: Any, fsdp_non_moe_mesh: Any) -> None: 

397 """Ensure both TP child meshes describe the same groups and local rank.""" 

398 if not hasattr(device_mesh, "rank_list") or not hasattr(fsdp_non_moe_mesh, "rank_list"): 

399 return 

400 device_tp_mesh = device_mesh["tp"] 

401 fsdp_tp_mesh = fsdp_non_moe_mesh["tp"] 

402 if tuple(device_tp_mesh.rank_list) != tuple(fsdp_tp_mesh.rank_list): 

403 raise ValueError("device_mesh['tp'] and fsdp_non_moe_mesh['tp'] rank groups differ") 

404 if device_tp_mesh.get_local_rank() != fsdp_tp_mesh.get_local_rank(): 

405 raise ValueError("device_mesh['tp'] and fsdp_non_moe_mesh['tp'] local ranks differ") 

406 

407 

408@dataclass 

409class DistributedSetup: 

410 """Unified distributed configuration container. 

411 

412 Following design doc 06 §3. 

413 """ 

414 mesh_context: MeshContext = field(default_factory=MeshContext) 

415 strategy_config: Any = None 

416 # Normalized ``{match: ModuleShardingSpec}`` mapping (or None) — the 

417 # Trainer desugars YAML PlanOverride entries via 

418 # entries_to_plan_overrides (when-filtering against the built mesh 

419 # included) BEFORE the AutoModels build pipeline runs; AutoModels never 

420 # sees the raw Trainer YAML DTO list (05 §15.2.6). 

421 plan_overrides: Any = None 

422 # Normalized module replacement rules (tuple[ModuleReplacementSpec]) 

423 # desugared Trainer-side via entries_to_module_replacements. 

424 module_replacements: Any = None 

425 module_replacement_requests: Any = None 

426 # Non-forward method patches installed after module replacement and before planning. 

427 method_replacements: Any = None 

428 # TrainingConfig.training.low_precision. Kept on the setup so model 

429 # construction and sharding consume one resolved policy object. 

430 low_precision_config: Any = None 

431 fp32_main_params: bool = False 

432 pipeline_config: Any = None 

433 pipeline_chunk_index: int = 0 

434 

435 @classmethod 

436 def from_config( 

437 cls, config: Any, mesh_context: MeshContext, *, strategy_enabled: bool 

438 ) -> "DistributedSetup": 

439 """Assemble model-build configuration independently of mesh execution. 

440 

441 Args: 

442 config: Resolved training configuration. 

443 mesh_context: Topology prepared by the caller. 

444 strategy_enabled: Whether to retain the configured FSDP strategy. 

445 

446 Returns: 

447 Setup with common model-build policies populated. 

448 """ 

449 return cls( 

450 mesh_context=mesh_context, 

451 strategy_config=config.fsdp_config if strategy_enabled else None, 

452 low_precision_config=getattr( 

453 getattr(config, "training", None), "low_precision", None 

454 ), 

455 fp32_main_params=config.optimizer.fp32_main_params, 

456 pipeline_config=getattr(config, "pipeline", None), 

457 )