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
This commit is contained in:
170
ixformer_sdk/train/functions/residual_bias_ln.py
Normal file
170
ixformer_sdk/train/functions/residual_bias_ln.py
Normal file
@@ -0,0 +1,170 @@
|
||||
from typing import Union
|
||||
|
||||
import ixformer._C as ops
|
||||
import torch
|
||||
from torch.autograd.function import Function, FunctionCtx
|
||||
|
||||
__all__ = ["residual_bias_ln"]
|
||||
|
||||
|
||||
class ResidualBiasLnFunction(Function):
|
||||
@staticmethod
|
||||
def forward(
|
||||
ctx,
|
||||
input: torch.Tensor,
|
||||
residual: torch.Tensor,
|
||||
bias: torch.Tensor,
|
||||
ln_weight: torch.Tensor,
|
||||
ln_bias: torch.Tensor,
|
||||
output: torch.Tensor,
|
||||
alpha=1,
|
||||
is_post_ln=True,
|
||||
):
|
||||
norm_size = ln_weight.size(-1)
|
||||
|
||||
mean_size = input.numel() // norm_size
|
||||
input_hat = torch.empty_like(input)
|
||||
rstd = torch.empty([mean_size], dtype=input.dtype, device=input.device)
|
||||
if bias is not None:
|
||||
ops.train.add_residual_bias_ln_training_forward(
|
||||
input,
|
||||
residual,
|
||||
bias,
|
||||
ln_weight,
|
||||
ln_bias,
|
||||
alpha,
|
||||
is_post_ln,
|
||||
output,
|
||||
input_hat,
|
||||
rstd,
|
||||
)
|
||||
else:
|
||||
ops.train.add_residual_bias_ln_training_forward(
|
||||
input,
|
||||
residual,
|
||||
ln_weight,
|
||||
ln_bias,
|
||||
alpha,
|
||||
is_post_ln,
|
||||
output,
|
||||
input_hat,
|
||||
rstd,
|
||||
)
|
||||
ctx.norm_size = norm_size
|
||||
ctx.has_bias = bias is not None
|
||||
ctx.alpha = alpha
|
||||
ctx.save_for_backward(input_hat, rstd, ln_weight)
|
||||
|
||||
return output
|
||||
|
||||
@staticmethod
|
||||
def backward(ctx: FunctionCtx, grad_output):
|
||||
input_hat, rstd_data, ln_weight = ctx.saved_tensors
|
||||
|
||||
grad_input = torch.empty_like(input_hat)
|
||||
grad_residual = torch.empty_like(input_hat)
|
||||
grad_ln_weight = torch.empty_like(ln_weight)
|
||||
grad_ln_bias = torch.empty_like(ln_weight)
|
||||
|
||||
if ctx.has_bias:
|
||||
grad_bias = torch.empty_like(ln_weight)
|
||||
ops.train.add_residual_bias_ln_backward(
|
||||
input_hat,
|
||||
rstd_data,
|
||||
ln_weight,
|
||||
grad_output,
|
||||
grad_ln_weight,
|
||||
grad_ln_bias,
|
||||
grad_input,
|
||||
grad_residual,
|
||||
grad_bias,
|
||||
ctx.alpha,
|
||||
)
|
||||
return (
|
||||
grad_input,
|
||||
grad_residual,
|
||||
grad_bias,
|
||||
grad_ln_weight,
|
||||
grad_ln_bias,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
else:
|
||||
ops.train.add_residual_bias_ln_backward(
|
||||
input_hat,
|
||||
rstd_data,
|
||||
ln_weight,
|
||||
grad_output,
|
||||
grad_ln_weight,
|
||||
grad_ln_bias,
|
||||
grad_input,
|
||||
grad_residual,
|
||||
ctx.alpha,
|
||||
)
|
||||
return (
|
||||
grad_input,
|
||||
grad_residual,
|
||||
None,
|
||||
grad_ln_weight,
|
||||
grad_ln_bias,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
def residual_bias_ln(
|
||||
input: torch.Tensor,
|
||||
residual: torch.Tensor,
|
||||
bias: torch.Tensor,
|
||||
ln_weight: torch.Tensor,
|
||||
ln_bias: torch.Tensor,
|
||||
alpha=1,
|
||||
is_post_ln=True,
|
||||
output: torch.Tensor = None,
|
||||
training: bool = False,
|
||||
):
|
||||
"""
|
||||
等价实现:
|
||||
input = residual.float() * alpha + input.float() + bias.float()
|
||||
output = torch.nn.functional.layer_norm(
|
||||
input, [input.shape[-1]], ln_weight.float(), ln_bias.float(), eps=1e-5)
|
||||
|
||||
参数说明:
|
||||
Args:
|
||||
input: shape:[batch_count * seq_len, hidden_size],dtype:[torch.half]
|
||||
residual: shape:[batch_count * seq_len, hidden_size],dtype:[torch.half]
|
||||
bias: shape:[hidden_size],dtype:[torch.half]
|
||||
ln_weight:shape:[hidden_size],,dtype:[torch.half]
|
||||
ln_bias:shape:[hidden_size],,dtype:[torch.half]
|
||||
alpha: float
|
||||
is_post_ln: bool, 是否应用layernorm 后处理
|
||||
return:
|
||||
output: shape:[batch_count * seq_len, hidden_size],dtype:[torch.half]
|
||||
"""
|
||||
if alpha is None:
|
||||
alpha = 1
|
||||
if not is_post_ln:
|
||||
raise NotImplementedError()
|
||||
if ln_weight is None or ln_bias is None:
|
||||
raise NotImplementedError()
|
||||
|
||||
if output is None:
|
||||
output = torch.empty_like(input)
|
||||
if not training:
|
||||
if bias is not None:
|
||||
ops.infer.add_residual_bias_ln_forward(
|
||||
input, residual, bias, ln_weight, ln_bias, alpha, is_post_ln, output
|
||||
)
|
||||
else:
|
||||
ops.infer.add_residual_bias_ln_forward(
|
||||
input, residual, ln_weight, ln_bias, alpha, is_post_ln, output
|
||||
)
|
||||
return output
|
||||
else:
|
||||
return ResidualBiasLnFunction.apply(
|
||||
input, residual, bias, ln_weight, ln_bias, output, alpha, is_post_ln
|
||||
)
|
||||
Reference in New Issue
Block a user