Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / core / shard / ops / parallel_unbind.py: 97%

34 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""" 

16Distributed implementation for Unbind operator. 

17""" 

18 

19from typing import Tuple 

20 

21from hyper_parallel.core.dtensor.layout import Layout 

22from .parallel_ops import DistributedOp 

23 

24 

25# Preserve Torch keyword names for calls forwarded by __torch_function__. 

26# pylint: disable-next=redefined-builtin 

27def _normalize_unbind_args(input, dim=0): 

28 return (input, dim), {} 

29 

30 

31class UnbindDistributedOp(DistributedOp): 

32 """Distributed implementation for Unbind operator.""" 

33 

34 def preprocess(self, args: tuple, kwargs: dict) -> tuple: 

35 """ 

36 Preprocess arguments for Unbind operator. 

37 

38 Args: 

39 args (tuple): Input arguments (input_tensor, dim). 

40 kwargs (dict): Keyword arguments (empty for this operator). 

41 

42 Returns: 

43 tuple: (local_args, local_kwargs, cache_values) 

44 """ 

45 args, kwargs = _normalize_unbind_args(*args, **kwargs) 

46 input_tensor, dim = args 

47 

48 local_args = (input_tensor.to_local(), dim) 

49 local_kwargs = {} 

50 cache_values = [input_tensor.layout, tuple(input_tensor.shape), dim] 

51 return local_args, local_kwargs, cache_values 

52 

53 # pylint: disable=W0237 

54 def infer_layout(self, cache_values: list) -> Tuple[tuple, None]: 

55 """ 

56 Infer output layouts for Unbind operator. 

57 

58 Rules: 

59 1. Input must not have Partial status. 

60 2. dim must be an integer within the valid range [-ndim, ndim-1]. 

61 3. The dimension to unbind must not be sharded. 

62 4. Output layout removes the mapping for the unbound dimension; 

63 all output tensors share the same layout. 

64 

65 Args: 

66 cache_values (list): [input_layout, input_shape, dim] 

67 

68 Returns: 

69 tuple: ((output_layouts_tuple,), None) 

70 

71 Raises: 

72 ValueError: If any rule above is violated. 

73 """ 

74 layout = cache_values[0] 

75 shape = cache_values[1] 

76 dim = cache_values[2] 

77 

78 if not self._allow_partial_inputs: 

79 self._check_partial_inputs([layout]) 

80 

81 alias_tensor_map = layout.alias_tensor_map 

82 ndim = len(shape) 

83 

84 if not isinstance(dim, int): 

85 raise ValueError( 

86 f"For {self.op_name}, dimension should be int, but got {type(dim)}" 

87 ) 

88 

89 if dim < -ndim or dim >= ndim: 

90 raise ValueError( 

91 f"For {self.op_name}, dimension out of range " 

92 f"(expected to be in range of [{-ndim}, {ndim - 1}], but got {dim})" 

93 ) 

94 

95 if dim < 0: 

96 dim += ndim 

97 

98 # Check if the dimension to unbind is sharded. 

99 # alias_tensor_map returns "None" for replicated dimensions. 

100 if alias_tensor_map[dim] != "None": 

101 raise ValueError( 

102 f"For {self.op_name}, the dimension {dim} is sharded " 

103 f"(mapped to {alias_tensor_map[dim]}). " 

104 f"Unbinding a sharded dimension is not supported. " 

105 f"Please redistribute the tensor to replicate this dimension first." 

106 ) 

107 

108 # Construct output layout: remove the mapping for the unbound dimension 

109 out_alias_map = alias_tensor_map[:dim] + alias_tensor_map[dim + 1:] 

110 

111 base_layout = Layout( 

112 mesh_shape=layout.mesh_shape, 

113 alias_name=layout.alias_name, 

114 rank_list=layout.rank_list 

115 ) 

116 out_layout = base_layout(*out_alias_map) 

117 

118 num_outputs = shape[dim] 

119 return ((out_layout,) * num_outputs, None)