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:
project6-dev
2026-08-11 02:31:56 +00:00
parent a8b16da5da
commit 87a19d2d00
250 changed files with 76690 additions and 0 deletions

View File

@@ -0,0 +1 @@
from .timer import Benchmark, BenchmarkTimer

View File

@@ -0,0 +1,69 @@
import time
from collections import namedtuple, OrderedDict
from typing import List, Dict, Any
import tabulate
import torch
import torch.distributed as dist
DeviceTime = namedtuple("DeviceTime", ["cpu", "gpu"])
class Functor:
def __init__(self, fn, *args, **kwargs):
self.fn = fn
self.args = args
self.kwargs = kwargs
def __call__(self):
return self.fn(*self.args, **self.kwargs)
def cuda_timeit(fn: Functor, dist_barrier=False) -> DeviceTime:
torch.cuda.synchronize()
start = torch.cuda.Event(enable_timing=True)
stop = torch.cuda.Event(enable_timing=True)
start.record(torch.cuda.current_stream())
t0 = time.time()
fn()
t1 = time.time()
stop.record(torch.cuda.current_stream())
torch.cuda.synchronize()
if dist_barrier:
dist.barrier()
gpu_time = start.elapsed_time(stop)
cpu_time = t1 - t0
return DeviceTime(cpu_time, gpu_time)
def cuda_benchmark(fn: Functor, num_repeated=10, num_warmup=1, dist_barrier=False) -> DeviceTime:
[fn() for _ in range(num_warmup)]
times = [cuda_timeit(fn, dist_barrier=dist_barrier) for _ in range(num_repeated)]
times.sort(key=lambda t: t.gpu)
if num_repeated >= 10:
times = times[3:-3]
avg_gpu_time = sum([t.gpu for t in times]) / len(times)
avg_cpu_time = sum([t.cpu for t in times]) / len(times)
return DeviceTime(avg_cpu_time * 1000, avg_gpu_time)
def show_benchmark_results(times: List[DeviceTime], extra_info: Dict[Any, List]=None):
data = extra_info or OrderedDict()
if len(times) != 0:
cpu_times = [round(t.cpu, 6) for t in times]
gpu_times = [round(t.gpu, 6) for t in times]
data["CPU Time(ms)"] = cpu_times
data["GPU Time(ms)"] = gpu_times
print(tabulate.tabulate(extra_info, headers=data.keys()))

View File

@@ -0,0 +1,130 @@
import time
from collections import OrderedDict
from typing import Callable
from tabulate import tabulate
from tqdm import tqdm
import torch
class BenchmarkTimer:
def __init__(self):
self.reset()
def reset(self):
self.start_time = None
self.end_time = None
self.running_times = []
def __enter__(self):
self.start_time = time.perf_counter()
def __exit__(self, exc_type, exc_val, exc_tb):
self.end_time = time.perf_counter()
self.running_times.append(self.end_time - self.start_time)
class Benchmark:
def __init__(
self,
warmup: int = None,
number: int = 100,
timer=None,
description: str = None,
show_progress: bool = False,
fn_desc_key: str = "fn_desc",
sync: bool = True,
):
if warmup is None:
warmup = int(number // 100) + 10
self.warmup = warmup
self.number = number
self.description = description
self.show_progress = show_progress
self.fn_desc_key = fn_desc_key
self.sync = sync
if timer is None:
timer = BenchmarkTimer()
self.timer = timer
self.reset()
def reset(self):
self.results = OrderedDict()
self._run_index = 0
self._fn_name = None
def run(self, fn, *args, **kwargs):
self._run_index += 1
if self.fn_desc_key in kwargs:
self.set_fn_name(kwargs[self.fn_desc_key])
kwargs.pop(self.fn_desc_key)
key = self._get_fn_key(fn)
# warmup
self._run_fn(False, fn, *args, **kwargs)
# get running times
results = self._run_fn(True, fn, *args, **kwargs)
self.results[key] = results
return results
def set_fn_name(self, name):
self._fn_name = name
def _run_fn(self, benchmark: bool, fn: Callable, *args, **kwargs):
self.timer.reset()
n = self.number if benchmark else self.warmup
if self.show_progress and benchmark:
progress = tqdm(range(n), desc=self._get_fn_key(fn))
else:
progress = range(n)
torch.cuda.synchronize()
for _ in progress:
with self.timer:
fn(*args, **kwargs)
if self.sync:
torch.cuda.synchronize()
return self.timer.running_times
def _get_fn_key(self, fn: Callable):
if self._fn_name is not None:
return self._fn_name
if hasattr(fn, "__name__"):
fn_name = fn.__name__
else:
fn_name = str(fn)
return f"{fn_name}_{self._run_index}"
def render(self) -> str:
head = [""] + list(self.results.keys())
total = ["Total (s)"] + [sum(times) for times in self.results.values()]
mean = ["Mean (s)"] + [_t / self.number for _t in total[1:]]
min_ = ["Min (s)"] + [min(times) for times in self.results.values()]
max_ = ["Max (s)"] + [max(times) for times in self.results.values()]
count = ["Count"] + [len(list(times)) for times in self.results.values()]
return tabulate(
headers=head,
tabular_data=[total, mean, min_, max_, count],
numalign="right",
)
def print_caption(self):
if self.description is not None:
caption = "\n" + "=" * 60 + "\n"
caption += f"= {self.description}" + "\n"
caption += "=" * 60 + "\n"
print(caption)
def print(self):
print(self.render())