Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / core / shard / ops / parallel_outer.py: 97%
37 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« 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 Outer operator.
17"""
19from typing import Tuple
21from hyper_parallel.core.dtensor.layout import Layout
22from .parallel_ops import DistributedOp
25# Preserve Torch keyword names for calls forwarded by __torch_function__.
26# pylint: disable-next=redefined-builtin
27def _normalize_outer_args(input, vec2):
28 return (input, vec2), {}
31def _get_alias_shard_set(dim_alias):
32 if isinstance(dim_alias, str):
33 return {dim_alias} if dim_alias != "None" else set()
34 return set(dim_alias)
37class OuterDistributedOp(DistributedOp):
38 """Distributed implementation for torch.outer."""
40 def preprocess(self, args: tuple, kwargs: dict) -> tuple:
41 """
42 Preprocess arguments for Outer operator.
44 Args:
45 args (tuple): Input arguments (input, vec2).
46 kwargs (dict): Keyword arguments (unused for outer).
48 Returns:
49 tuple: (local_args, local_kwargs, cache_values) where local_args contains
50 local tensors for input and vec2, and cache_values contains their layouts.
51 """
52 args, kwargs = _normalize_outer_args(*args, **kwargs)
53 input_tensor, vec2_tensor = args[0], args[1]
54 local_args = (input_tensor.to_local(), vec2_tensor.to_local())
55 local_kwargs = {}
56 cache_values = [input_tensor.layout, vec2_tensor.layout]
57 return local_args, local_kwargs, cache_values
59 def infer_layout(self, cache_values: list) -> Tuple[tuple, None]:
60 """
61 Infer output layout for Outer operator.
63 PyTorch semantics:
64 - Computes the outer product of two 1-D tensors.
65 - If input is of size N and vec2 is of size M, the output is of size (N, M).
66 - Input tensors must be 1-D.
68 Rules:
69 1. Inputs must not have Partial status.
70 2. Exactly two input layouts are required, both must be non-None.
71 3. Both inputs must be exactly 1-D.
72 4. The two inputs cannot be sharded along the same device mesh dimension.
73 5. Output dim 0 inherits the sharding of input; output dim 1 inherits the sharding of vec2.
75 Args:
76 cache_values (list): [input_layout, vec2_layout]
78 Returns:
79 tuple: ((output_layout,), None)
81 Raises:
82 ValueError: If any rule above is violated.
83 """
84 layout1, layout2 = cache_values[0], cache_values[1]
86 if layout1 is None or layout2 is None:
87 raise ValueError(
88 f"For {self.op_name}, both inputs should be DTensors with valid layouts, "
89 f"but got layout1={layout1}, layout2={layout2}."
90 )
92 if not self._allow_partial_inputs:
93 self._check_partial_inputs([layout1, layout2])
95 alias_map1 = layout1.alias_tensor_map
96 alias_map2 = layout2.alias_tensor_map
98 if len(alias_map1) != 1 or len(alias_map2) != 1:
99 raise ValueError(
100 f"For {self.op_name}, both inputs should be exactly 1-D tensors, "
101 f"but got {len(alias_map1)}-D and {len(alias_map2)}-D."
102 )
104 dim0_alias = alias_map1[0]
105 dim1_alias = alias_map2[0]
107 set1 = _get_alias_shard_set(dim0_alias)
108 set2 = _get_alias_shard_set(dim1_alias)
110 if set1.intersection(set2):
111 raise ValueError(
112 f"For {self.op_name}, the two inputs should not be sharded on the "
113 f"same device mesh dimension, "
114 f"but got conflict on mesh dimension(s): {set1.intersection(set2)}."
115 )
117 output_alias_map = (dim0_alias, dim1_alias)
119 output_layout = Layout(
120 mesh_shape=layout1.mesh_shape,
121 alias_name=layout1.alias_name,
122 rank_list=layout1.rank_list,
123 )
124 output_layout = output_layout(*output_alias_map)
126 return ((output_layout,), None)