Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / tools / dryrun / patches / trainer / distributed.py: 86%

44 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"""Training-flow distributed and FSDP patches for FakeTensor dry-run.""" 

16 

17from contextlib import contextmanager 

18from typing import Any, Iterator 

19 

20import torch 

21import torch.distributed as dist 

22from torch._subclasses.fake_tensor import unset_fake_temporarily 

23 

24from hyper_parallel.core.dtensor.device_mesh import DeviceMesh 

25from hyper_parallel.core.fully_shard import api as fully_shard_api 

26from hyper_parallel.tools.dryrun.simulation import is_device_runtime_simulated 

27 

28 

29def install_distributed_patches() -> None: 

30 """Short-circuit Python-object collectives while device simulation is active.""" 

31 if getattr(dist, "_dryrun_object_collectives_patched", False): 

32 return 

33 original_all_gather_object = dist.all_gather_object 

34 original_broadcast_object_list = dist.broadcast_object_list 

35 

36 def simulated_all_gather_object( 

37 object_list: list[Any], 

38 obj: Any, 

39 group: Any = None, 

40 ) -> None: 

41 """Populate every slot with ``obj`` instead of serializing via ``.numpy()``.""" 

42 if is_device_runtime_simulated(): 

43 del group 

44 for index, _ in enumerate(object_list): 

45 object_list[index] = obj 

46 return 

47 original_all_gather_object(object_list, obj, group=group) 

48 

49 def simulated_broadcast_object_list( 

50 object_list: list[Any], 

51 *args: Any, 

52 **kwargs: Any, 

53 ) -> None: 

54 """Keep ``object_list`` unchanged without serializing via FakeTensors.""" 

55 if is_device_runtime_simulated(): 

56 return 

57 original_broadcast_object_list(object_list, *args, **kwargs) 

58 

59 dist.all_gather_object = simulated_all_gather_object 

60 dist.broadcast_object_list = simulated_broadcast_object_list 

61 dist._dryrun_object_collectives_patched = True 

62 

63 

64@contextmanager 

65def fsdp_device_context(simulation_device: torch.device) -> Iterator[None]: 

66 """Temporarily resolve FSDP mesh device lookup and host mesh concatenation. 

67 

68 Args: 

69 simulation_device: Logical device where FakeTensor FSDP storage resides. 

70 """ 

71 original_device_resolver = fully_shard_api._get_device_from_mesh 

72 original_concatenate = DeviceMesh.concatenate 

73 

74 def _resolve_device(mesh: Any) -> torch.device: 

75 del mesh 

76 return simulation_device 

77 

78 def _concatenate_meshes(meshes: Any) -> DeviceMesh: 

79 # Mesh rank maps are host metadata and must never become FakeTensors. 

80 with unset_fake_temporarily(): 

81 return original_concatenate(meshes) 

82 

83 fully_shard_api._get_device_from_mesh = _resolve_device 

84 DeviceMesh.concatenate = staticmethod(_concatenate_meshes) 

85 try: 

86 yield 

87 finally: 

88 fully_shard_api._get_device_from_mesh = original_device_resolver 

89 DeviceMesh.concatenate = staticmethod(original_concatenate) 

90 

91 

92__all__ = [ 

93 "fsdp_device_context", 

94 "install_distributed_patches", 

95]