Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / parallelism_invariant / row_parallel_linear / collectives.py: 30%

27 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"""Transport-only HCCL with a fixed Triton tree at the receiving rank.""" 

16from typing import Any 

17 

18import torch 

19import torch.distributed as dist 

20 

21from .reduction import fixed_tree 

22 

23 

24def combine_partials(partial: torch.Tensor, group: Any, world: int, rank: int, *, 

25 scatter: bool, transport: str) -> torch.Tensor: 

26 """Combine FP32 rank subtrees; HCCL never performs the forward SUM.""" 

27 if world == 1: 

28 return partial 

29 rows, columns = partial.shape 

30 if transport == "all_gather": 

31 received = torch.empty((world * rows, columns), dtype=torch.float32, device=partial.device) 

32 dist.all_gather_into_tensor(received, partial.contiguous(), group=group) 

33 root = fixed_tree(received.view(world, rows, columns)) 

34 return root.narrow(0, rank * (rows // world), rows // world) if scatter else root 

35 received = torch.empty((world, rows // world, columns), dtype=torch.float32, device=partial.device) 

36 dist.all_to_all_single(received.view(-1), partial.contiguous().view(-1), group=group) 

37 owned = fixed_tree(received) 

38 if scatter: 

39 return owned 

40 result = torch.empty_like(partial) 

41 dist.all_gather_into_tensor(result, owned.contiguous(), group=group) 

42 return result 

43 

44 

45def gather_sequence_gradient(gradient: torch.Tensor, group: Any, world: int, 

46 sequence_dim: int) -> torch.Tensor: 

47 """Gather disjoint SP gradient rows, without a floating-point reduction.""" 

48 ordered = gradient.movedim(sequence_dim, 0).contiguous() 

49 shape = (ordered.shape[0] * world, *ordered.shape[1:]) 

50 received = torch.empty(shape, dtype=gradient.dtype, device=gradient.device) 

51 dist.all_gather_into_tensor(received, ordered, group=group) 

52 return received.movedim(0, sequence_dim)