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

35 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 Cumsum operator. 

17""" 

18 

19import copy 

20from typing import Tuple 

21 

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_cumsum_args(input, dim, dtype=None): 

28 return (input, dim), {'dtype': dtype} 

29 

30 

31class CumsumDistributedOp(DistributedOp): 

32 """Distributed implementation for torch.cumsum.""" 

33 

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

35 """ 

36 Preprocess arguments for Cumsum operator. 

37 

38 Args: 

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

40 kwargs (dict): Keyword arguments (dim, dtype). 

41 

42 Returns: 

43 tuple: (local_args, local_kwargs, cache_values) 

44 """ 

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

46 input_tensor = args[0] 

47 dim = args[1] 

48 dtype = kwargs['dtype'] 

49 

50 local_input = input_tensor.to_local() 

51 local_args = (local_input, dim) 

52 local_kwargs = {} 

53 if dtype is not None: 

54 local_kwargs['dtype'] = dtype 

55 

56 cache_values = [input_tensor.layout, dim] 

57 return local_args, local_kwargs, cache_values 

58 

59 def infer_layout(self, cache_values: list) -> Tuple[tuple, None]: # pylint: disable=W0221 

60 """ 

61 Infer output layout for Cumsum operator. 

62 

63 Rules: 

64 1. Input must not have Partial status. 

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

66 3. The cumsum dimension must not be sharded, including StridedShard mappings. 

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

68 

69 Args: 

70 cache_values (list): [input_layout, dim] 

71 

72 Returns: 

73 tuple: ((output_layout,), None) 

74 

75 Raises: 

76 ValueError: If input has Partial status, dim is not an int, dim is out of range, 

77 or the cumsum dimension is sharded. 

78 """ 

79 layout = cache_values[0] 

80 dim = cache_values[1] 

81 

82 if not self._allow_partial_inputs: 

83 self._check_partial_inputs([layout]) 

84 

85 if not isinstance(dim, int): 

86 raise ValueError( 

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

88 ) 

89 

90 alias_map = layout.alias_tensor_map 

91 ndim = len(alias_map) 

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

93 raise ValueError( 

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

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

96 ) 

97 if dim < 0: 

98 dim += ndim 

99 

100 mapping = alias_map[dim] 

101 if mapping != "None": 

102 raise ValueError( 

103 f"For {self.op_name}, cumsum along a sharded dimension " 

104 f"(dim {dim} mapped to {mapping}) is not supported." 

105 ) 

106 

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