Files
project_6/ixformer_sdk/inference/functions/groupnorm.py
project6-dev 87a19d2d00 feat(CRITICAL): 从 GitHub 扫描搬运 ixformer SDK + xllm 完整 GDN/MoE 代码
来源:
  1. Chranos/ixformer (GitHub) → ixformer_sdk/ (230 files, 70K lines)
     - inference/functions/vllm.py: vllm_moe_topk_softmax 完整实现 (2033 lines)
     - inference/functions/moe.py: MoE ops 完整实现 (1380 lines)
     - contrib/vllm_flash_attn/: FA2 Python 接口 (1018 lines)
     - contrib/tgi/fused_moe.py: TGI fused MoE (429 lines)
     - csrc/include/ixformer/: C++ kernel headers + cmake

  2. Deep-Spark/xllm (GitHub) → upstream_ref/xllm_latest/ (+15 files)
     - npu_torch/qwen3_5_decoder_layer_impl.cpp/.h
     - npu_torch/qwen3_5_gated_delta_net.cpp/.h
     - npu_torch/qwen3_next_*.cpp/.h (6 files)
     - npu_torch/attention.cpp/.h + fused_moe.cpp/.h + CMakeLists.txt
     - models/llm/qwen3_5.h + qwen3_5_mtp.h + qwen3_next.h
     - models/vlm/qwen3_5.h

调用链完整性:
  ixformer_sdk/inference/functions/vllm.py
    → ops.infer.moe_topk_softmax() (C++ 层)
    → 这就是 base 镜像 libixformer.so 里的实现

  upstream_ref/xllm_latest/core/layers/ilu/fused_moe.cpp
    → ixformer::infer::topk_softmax() (直接 C++ 调用)
    → ixformer::infer::group_gemm() → 完整 7-step MoE pipeline
2026-08-11 02:32:06 +00:00

121 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import List, Tuple, Union
import ixformer._C as ops
import torch
from torch.autograd.function import Function, FunctionCtx
import ixformer
__all__ = [
"group_norm",
"ref_group_norm",
"ref_fused_group_norm_silu",
"fused_group_norm_silu",
"ref_fused_group_norm_silu_nhwc",
"fused_group_norm_silu_nhwc"
]
def is_channels_last(ten):
return torch._prims_common.suggest_memory_format(ten) == torch.channels_last
def ref_group_norm(input, num_groups, weight, bias, eps):
output = torch.nn.functional.group_norm(input, num_groups, weight, bias, eps)
return output
#group_norm官方接口如果input是nhwc(channel_last),输出则不是channel_last,而是nchw如果input是nchw那么输出也是nchw
def group_norm(
input: torch.Tensor,
num_groups: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float = 1e-05,
):
"""
Args:
input: (n,c,h,w) or (n,c,h) or (n,h,w,c) torch.float16
"contiguous_format":(n,c,h,w) or (n,c,h) "channels_last": (n,h,w,c)
num_groups: int
weight: (c) torch.float16
bias: (c) torch.float16
eps: float
Returns:
Tensor: (n,c,h,w) torch.float16
"""
is_nhwc=is_channels_last(input)
out = ops.infer.groupnorm(input, num_groups, weight, bias, eps, is_nhwc, 0)
if is_nhwc:
out=out.permute(0,3,1,2).contiguous()
return out
def ref_fused_group_norm_silu_nhwc(
input: torch.Tensor,
num_groups: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float = 1e-05,
act_type:int = 0
):
output = torch.nn.functional.group_norm(input.permute(0,3,1,2).contiguous(), num_groups, weight, bias, eps)
if act_type:
output = output * torch.sigmoid(output)
output = output.permute(0,2,3,1).contiguous()
return output
#为了减少permute/contiguous,新接口支持输入输出都是nhwc的融合silu
def fused_group_norm_silu_nhwc(
input: torch.Tensor,
num_groups: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float = 1e-05,
act_type:int = 0
):
"""
Args:
input: (n,h,w,c) torch.float16
num_groups: int
weight: (c) torch.float16
bias: (c) torch.float16
eps: float
act_type: int
0 or 1,if act_type=1, silu
Returns:
Tensor: (n,h,w,c) torch.float16
"""
out = ops.infer.groupnorm(input, num_groups, weight, bias, eps, True, act_type)
return out
def ref_fused_group_norm_silu(
input: torch.Tensor,
num_groups: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float = 1e-05,
):
output = torch.nn.functional.group_norm(input, num_groups, weight, bias, eps)
output = output * torch.sigmoid(output)
return output
def fused_group_norm_silu(
input: torch.Tensor,
num_groups: int,
weight: torch.Tensor,
bias: torch.Tensor,
eps: float = 1e-05,
):
"""
Args:
input: (n,c,h,w) or (n,c,h) torch.float16
num_groups: int
weight: (c) torch.float16
bias: (c) torch.float16
eps: float
Returns:
output: (n,c,h,w) or (n,c,h) torch.float16
"""
out = ops.infer.groupnorm(input, num_groups, weight, bias, eps, False, 1)
return out