Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / functional / moe_token_permute.py: 100%
4 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"""NPU MoE token permutation function."""
17import torch # pylint: disable=forbidden-backend-import
18import torch_npu
21def moe_token_permute(
22 tokens: torch.Tensor,
23 indices: torch.Tensor,
24) -> tuple[torch.Tensor, torch.Tensor]:
25 """Reorder tokens from token-major to expert-major order.
27 Args:
28 tokens: Token tensor whose first dimension is the token dimension.
29 indices: Selected expert indices with shape ``[num_tokens, top_k]``.
31 Returns:
32 A tuple containing expert-major tokens and the sorting indices needed
33 by :func:`moe_token_unpermute`.
34 """
35 return torch_npu.npu_moe_token_permute(tokens, indices)