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:
0
ixformer_sdk/utils/__init__.py
Normal file
0
ixformer_sdk/utils/__init__.py
Normal file
1
ixformer_sdk/utils/benchmark/__init__.py
Normal file
1
ixformer_sdk/utils/benchmark/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .timer import Benchmark, BenchmarkTimer
|
||||
69
ixformer_sdk/utils/benchmark/cuda_benchmark.py
Normal file
69
ixformer_sdk/utils/benchmark/cuda_benchmark.py
Normal 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()))
|
||||
130
ixformer_sdk/utils/benchmark/timer.py
Normal file
130
ixformer_sdk/utils/benchmark/timer.py
Normal 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())
|
||||
227
ixformer_sdk/utils/object.py
Normal file
227
ixformer_sdk/utils/object.py
Normal file
@@ -0,0 +1,227 @@
|
||||
import inspect
|
||||
from typing import Any, Callable, Dict, Mapping, Union
|
||||
|
||||
__all__ = [
|
||||
"isfunction",
|
||||
"iscallable",
|
||||
"get_obj_name",
|
||||
"isimmutable_var",
|
||||
"get_self_from",
|
||||
"get_obj_funcs",
|
||||
"recurse_getattr",
|
||||
"recurse_find_by_key",
|
||||
"set_value_by_cascasde_key",
|
||||
"flatten_container",
|
||||
"flatten_dict",
|
||||
"get_func_argspec",
|
||||
"get_obj_attr",
|
||||
"get_namedtuple_fields",
|
||||
"get_namedtuple_defaults",
|
||||
"isnamedtuple",
|
||||
"namedtype_to_dict",
|
||||
]
|
||||
|
||||
|
||||
def isfunction(f):
|
||||
return (
|
||||
inspect.isfunction(f) or inspect.ismethod(f) or inspect.isbuiltin(f)
|
||||
) and not inspect.isclass(f)
|
||||
|
||||
|
||||
def iscallable(fn) -> bool:
|
||||
return any(
|
||||
[
|
||||
callable(fn),
|
||||
inspect.isfunction(fn),
|
||||
inspect.ismethod(fn),
|
||||
inspect.isbuiltin(fn),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def get_obj_name(obj, containe_module=False):
|
||||
mod_name = None
|
||||
if inspect.isclass(obj):
|
||||
obj_name = obj.__name__
|
||||
if hasattr(obj, "__module__") and containe_module:
|
||||
mod_name = obj.__module__
|
||||
elif hasattr(obj, "__name__"):
|
||||
obj_name = obj.__name__
|
||||
elif hasattr(obj, "__class__"):
|
||||
obj_name = obj.__class__.__name__
|
||||
if hasattr(obj.__class__, "__module__") and containe_module:
|
||||
mod_name = obj.__class__.__module__
|
||||
else:
|
||||
obj_name = str(obj)
|
||||
|
||||
if containe_module and mod_name is None:
|
||||
if hasattr(obj, "__module__"):
|
||||
mod_name = obj.__module__
|
||||
|
||||
if mod_name is None:
|
||||
return obj_name
|
||||
else:
|
||||
return f"{mod_name}.{obj_name}"
|
||||
|
||||
|
||||
def isimmutable_var(var):
|
||||
if var is None:
|
||||
return True
|
||||
|
||||
if inspect.isclass(var):
|
||||
var_cls = var
|
||||
else:
|
||||
var_cls = type(var)
|
||||
|
||||
return var_cls in [int, float, tuple, str, None]
|
||||
|
||||
|
||||
def get_self_from(obj):
|
||||
if hasattr(obj, "__self__"):
|
||||
return obj.__self__
|
||||
raise AttributeError(f"Not found attribute `self` in {obj}.")
|
||||
|
||||
|
||||
def get_obj_funcs(obj) -> Dict[str, Callable]:
|
||||
attrs = dir(obj)
|
||||
funcs = dict()
|
||||
for attr in attrs:
|
||||
fn = getattr(obj, attr)
|
||||
if iscallable(fn):
|
||||
funcs[attr] = fn
|
||||
|
||||
return funcs
|
||||
|
||||
|
||||
def recurse_find_by_key(container: dict, key: Union[str, list], default=None):
|
||||
if isinstance(key, str):
|
||||
key = key.split(".")
|
||||
|
||||
if not isinstance(key, (tuple, list)):
|
||||
raise RuntimeError(f"Please give the type str or list, but get ({type(key)}).")
|
||||
|
||||
value = default
|
||||
_cnt = container
|
||||
for k in key:
|
||||
if k not in _cnt:
|
||||
return default
|
||||
value = _cnt[k]
|
||||
_cnt = value
|
||||
if _cnt is None:
|
||||
return default
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def set_value_by_cascasde_key(container: dict, key: str, value: Any):
|
||||
if isinstance(key, str):
|
||||
key = key.split(".")
|
||||
|
||||
if not isinstance(key, (tuple, list)):
|
||||
raise RuntimeError(f"Please give the type str or list, but get ({type(key)}).")
|
||||
|
||||
_cnt = container
|
||||
for k in key[:-1]:
|
||||
if k not in _cnt:
|
||||
_cnt[k] = dict()
|
||||
_cnt = _cnt[k]
|
||||
_cnt[key[-1]] = value
|
||||
return container
|
||||
|
||||
|
||||
def flatten_dict(d: dict, preffix="", out=None):
|
||||
if out is None:
|
||||
out = dict()
|
||||
for k, v in d.items():
|
||||
if isinstance(v, Mapping):
|
||||
flatten_dict(v, f"{preffix}{k}.", out)
|
||||
else:
|
||||
out[preffix + k] = v
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def flatten_container(container: Union[list, dict]):
|
||||
outs = []
|
||||
|
||||
def _flatten_list(cnt: list):
|
||||
for item in cnt:
|
||||
if isinstance(item, (tuple, list)):
|
||||
_flatten_list(item)
|
||||
elif isinstance(item, Mapping):
|
||||
_flatten_dict(item)
|
||||
else:
|
||||
outs.append(item)
|
||||
|
||||
def _flatten_dict(cnt: Dict):
|
||||
for key, item in cnt.items():
|
||||
if isinstance(item, (tuple, list)):
|
||||
_flatten_list(item)
|
||||
elif isinstance(item, Mapping):
|
||||
_flatten_dict(item)
|
||||
else:
|
||||
outs.append(item)
|
||||
|
||||
if isinstance(container, (tuple, list)):
|
||||
_flatten_list(container)
|
||||
elif isinstance(container, dict):
|
||||
_flatten_dict(container)
|
||||
else:
|
||||
outs.append(container)
|
||||
|
||||
return outs
|
||||
|
||||
|
||||
def get_func_argspec(func) -> inspect.FullArgSpec:
|
||||
return inspect.getfullargspec(func)
|
||||
|
||||
|
||||
def get_obj_attr(obj, attr, default=None):
|
||||
if isinstance(obj, Mapping):
|
||||
return obj.get(attr, default)
|
||||
return getattr(obj, attr, default)
|
||||
|
||||
|
||||
def isnamedtuple(obj):
|
||||
if not inspect.isclass(obj) or not issubclass(obj, tuple):
|
||||
return False
|
||||
|
||||
if hasattr(obj, "_fields") and hasattr(obj, "_replace"):
|
||||
if (
|
||||
hasattr(obj._replace, "__module__")
|
||||
and obj._replace.__module__ == "collections"
|
||||
):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def get_namedtuple_fields(t):
|
||||
if not inspect.isclass(t):
|
||||
t = type(t)
|
||||
|
||||
if not isnamedtuple(t):
|
||||
raise RuntimeError(f"{t} is not a namedtuple object")
|
||||
|
||||
return t._fields
|
||||
|
||||
|
||||
def get_namedtuple_defaults(t) -> dict:
|
||||
return t._field_defaults
|
||||
|
||||
|
||||
def namedtype_to_dict(t):
|
||||
return t._asdict()
|
||||
|
||||
|
||||
def recurse_getattr(obj, attr: str, sep="."):
|
||||
attrs = attr.split(sep)
|
||||
idx = 0
|
||||
cur_obj = obj
|
||||
while idx < len(attrs):
|
||||
cur_obj = getattr(cur_obj, attrs[idx])
|
||||
idx += 1
|
||||
|
||||
if cur_obj == obj:
|
||||
return None
|
||||
return cur_obj
|
||||
16
ixformer_sdk/utils/seed.py
Normal file
16
ixformer_sdk/utils/seed.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import random
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def manual_seed(seed=41):
|
||||
random.seed(seed)
|
||||
np.random.seed(seed)
|
||||
try:
|
||||
import torch
|
||||
|
||||
torch.manual_seed(seed)
|
||||
torch.cuda.manual_seed(seed)
|
||||
torch.cuda.manual_seed_all(seed)
|
||||
except:
|
||||
pass
|
||||
Reference in New Issue
Block a user