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-10-25 05:45 +0800
« 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 Cumsum operator.
17"""
19import copy
20from typing import Tuple
22from .parallel_ops import DistributedOp
25def _normalize_cumsum_args(x, dim, dtype=None):
26 return (x, dim), {'dtype': dtype}
29class CumsumDistributedOp(DistributedOp):
30 """Distributed implementation for torch.cumsum."""
32 def preprocess(self, args: tuple, kwargs: dict) -> tuple:
33 """
34 Preprocess arguments for Cumsum operator.
36 Args:
37 args (tuple): Input arguments, first element is the input tensor.
38 kwargs (dict): Keyword arguments (dim, dtype).
40 Returns:
41 tuple: (local_args, local_kwargs, cache_values)
42 """
43 args, kwargs = _normalize_cumsum_args(*args, **kwargs)
44 input_tensor = args[0]
45 dim = args[1]
46 dtype = kwargs['dtype']
48 local_input = input_tensor.to_local()
49 local_args = (local_input, dim)
50 local_kwargs = {}
51 if dtype is not None:
52 local_kwargs['dtype'] = dtype
54 cache_values = [input_tensor.layout, dim]
55 return local_args, local_kwargs, cache_values
57 def infer_layout(self, cache_values: list) -> Tuple[tuple, None]: # pylint: disable=W0221
58 """
59 Infer output layout for Cumsum operator.
61 Rules:
62 1. Input must not have Partial status.
63 2. dim must be an integer within the valid range [-ndim, ndim-1].
64 3. The cumsum dimension must not be sharded, including StridedShard mappings.
65 4. Output layout is identical to the input layout.
67 Args:
68 cache_values (list): [input_layout, dim]
70 Returns:
71 tuple: ((output_layout,), None)
73 Raises:
74 ValueError: If input has Partial status, dim is not an int, dim is out of range,
75 or the cumsum dimension is sharded.
76 """
77 layout = cache_values[0]
78 dim = cache_values[1]
80 if not self._allow_partial_inputs:
81 self._check_partial_inputs([layout])
83 if not isinstance(dim, int):
84 raise ValueError(
85 f"For {self.op_name}, dimension should be int, but got {type(dim)}"
86 )
88 alias_map = layout.alias_tensor_map
89 ndim = len(alias_map)
90 if dim < -ndim or dim >= ndim:
91 raise ValueError(
92 f"For {self.op_name}, dimension out of range "
93 f"(expected to be in range of [{-ndim}, {ndim - 1}], but got {dim})"
94 )
95 if dim < 0:
96 dim += ndim
98 mapping = alias_map[dim]
99 if mapping != "None":
100 raise ValueError(
101 f"For {self.op_name}, cumsum along a sharded dimension "
102 f"(dim {dim} mapped to {mapping}) is not supported."
103 )
105 return ((copy.deepcopy(layout),), None)