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

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

16Distributed implementation for Argsort operator. 

17""" 

18 

19import copy 

20from typing import Tuple 

21 

22from .parallel_ops import DistributedOp 

23 

24 

25def _normalize_argsort_args(x, dim=-1, descending=False, stable=False): 

26 """Normalize torch.argsort arguments to (args, kwargs). 

27 

28 torch.argsort(input, dim=-1, descending=False, *, stable=False) 

29 Only `stable` is keyword-only; all other params are positional. 

30 """ 

31 return (x, dim, descending), {'stable': stable} 

32 

33 

34class ArgsortDistributedOp(DistributedOp): 

35 """Distributed implementation for torch.argsort.""" 

36 

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

38 """ 

39 Preprocess arguments for Argsort operator. 

40 

41 Args: 

42 args (tuple): Input arguments, first element is the input tensor. 

43 kwargs (dict): Keyword arguments (dim, descending, stable). 

44 

45 Returns: 

46 tuple: (local_args, local_kwargs, cache_values) 

47 """ 

48 args, kwargs = _normalize_argsort_args(*args, **kwargs) 

49 input_tensor = args[0] 

50 dim = args[1] 

51 descending = args[2] 

52 stable = kwargs['stable'] 

53 

54 local_args = (input_tensor.to_local(),) 

55 local_kwargs = {'dim': dim, 'descending': descending, 'stable': stable} 

56 

57 cache_values = [input_tensor.layout, dim] 

58 return local_args, local_kwargs, cache_values 

59 

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

61 """ 

62 Infer output layout for Argsort operator. 

63 

64 Rules: 

65 1. Input must not have Partial status. 

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

67 3. The sort dimension must not be sharded. 

68 4. Output layout is identical to the input layout. 

69 

70 Args: 

71 cache_values (list): [input_layout, dim] where dim is the sort dimension. 

72 

73 Returns: 

74 tuple: ((output_layout,), None) 

75 

76 Raises: 

77 ValueError: If input has Partial status, dim is out of range, or the sort 

78 dimension is sharded. 

79 """ 

80 layout = cache_values[0] 

81 dim = cache_values[1] 

82 

83 if not self._allow_partial_inputs: 

84 self._check_partial_inputs([layout]) 

85 

86 if not isinstance(dim, int): 

87 raise ValueError( 

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

89 ) 

90 

91 alias_map = layout.alias_tensor_map 

92 ndim = len(alias_map) 

93 

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

95 raise ValueError( 

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

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

98 ) 

99 

100 if dim < 0: 

101 dim += ndim 

102 

103 if alias_map[dim] != "None": 

104 raise ValueError( 

105 f"For {self.op_name}, sorting along a sharded dimension " 

106 f"(dim {dim} mapped to {alias_map[dim]}) is not supported. " 

107 f"Please redistribute the tensor to Replicate on this dimension before sorting." 

108 ) 

109 

110 return ((copy.deepcopy(layout),), None)