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

14 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"""All local leaves in one native Level 3 BMM submission.""" 

16import torch 

17 

18from .plan import PikPlan 

19from .reduction import fixed_tree 

20 

21 

22def batched_leaves(x: torch.Tensor, weight: torch.Tensor, *, plan: PikPlan, 

23 local_leaves: int) -> torch.Tensor: 

24 """Return BF16 [L,M,N] leaves; strided views avoid explicit input/weight packing.""" 

25 rows, local_k = x.shape 

26 outputs = weight.shape[0] 

27 leaf_k = local_k // local_leaves 

28 left = x.reshape(rows, local_leaves, leaf_k).permute(1, 0, 2) 

29 right = weight.reshape(outputs, local_leaves, leaf_k).permute(1, 2, 0) 

30 if plan.leaf_layout == "packed": 

31 left, right = left.contiguous(), right.contiguous() 

32 return torch.bmm(left, right) 

33 

34 

35def local_gemm(x: torch.Tensor, weight: torch.Tensor, *, plan: PikPlan, 

36 local_leaves: int) -> torch.Tensor: 

37 """Compute one FP32 subtree without rounding at the local-rank boundary.""" 

38 return fixed_tree(batched_leaves(x, weight, plan=plan, local_leaves=local_leaves))