ref(upstream): FULL TREE — Deep-Spark xllm (1470) + ds_vllm csrc/models (703)

Replaces cherry-picked upstream_ref with complete source trees.

xllm/ — Iluvatar official C++ inference engine (15MB, 1470 files)
  Complete: kernels → layers → models → runtime → scheduler → api
  Excluded: .git, binary images, third_party submodule checkouts

ds_vllm/ — Iluvatar official vllm fork (8MB, 703 files)
  Included: csrc/ (ALL CUDA kernels), fused_moe/, qwen3_5 model, _custom_ops
  Excluded: tests, benchmarks, docs, examples (not needed for reference)

Critical call chains now fully traceable:
  MoE: moe_topk_softmax_kernels.cuh → ixformer.h → fused_moe.cpp → layer
  GDN: qwen3_gated_delta_net_base.cpp → qwen3_5_gated_delta_net.cpp
  Attention: ixformer.h → xllm_paged_attention → attention.cpp
This commit is contained in:
EX Engine
2026-08-10 02:53:54 +00:00
parent 9e4fb3712f
commit 002f9879b2
2179 changed files with 494021 additions and 79 deletions

View File

@@ -0,0 +1,216 @@
import os
import platform
from typing import Optional
def get_cxx_abi() -> bool:
try:
import torch
return torch.compiled_with_cxx11_abi()
except ImportError:
return False
def get_python_include_path() -> Optional[str]:
try:
from sysconfig import get_paths
return get_paths()["include"]
except ImportError:
return None
def get_torch_root_path() -> Optional[str]:
try:
import torch
import os
return os.path.dirname(os.path.abspath(torch.__file__))
except ImportError:
return None
def get_torch_mlu_root_path() -> Optional[str]:
try:
import torch_mlu
import os
return os.path.dirname(os.path.abspath(torch_mlu.__file__))
except ImportError:
return None
def get_ixformer_root_path() -> Optional[str]:
try:
import ixformer
import os
return os.path.dirname(os.path.abspath(ixformer.__file__))
except ImportError:
return None
def get_cuda_root_path() -> Optional[str]:
try:
import torch
from torch.utils.cpp_extension import CUDA_HOME
if CUDA_HOME is None:
raise RuntimeError(
"PyTorch was not built with CUDA, or nvcc is not in PATH. "
"Please set CUDA_TOOLKIT_ROOT_DIR manually."
)
return CUDA_HOME
except ImportError:
return None
def get_torch_musa_root_path() -> Optional[str]:
try:
import torch_musa
import os
return os.path.dirname(os.path.abspath(torch_musa.__file__))
except ImportError:
return None
def prepend_path_env(var_name: str, path: str, sep: str = os.pathsep) -> None:
"""Prepend a path into a path env var without duplicates."""
if not path:
return
current = os.getenv(var_name, "")
entries = [item for item in current.split(sep) if item]
if path in entries:
entries = [item for item in entries if item != path]
entries.insert(0, path)
os.environ[var_name] = sep.join(entries)
def set_npu_torch_ld_library_path() -> None:
"""Only for NPU flow: ensure torch runtime libraries are discoverable."""
torch_root = os.getenv("PYTORCH_INSTALL_PATH") or get_torch_root_path() or ""
if not torch_root:
return
# Order keeps current behavior: torch.libs > torch > torch/lib
for path in (f"{torch_root}.libs", torch_root, os.path.join(torch_root, "lib")):
if os.path.isdir(path):
prepend_path_env("LD_LIBRARY_PATH", path)
def set_common_envs() -> None:
os.environ["PYTHON_INCLUDE_PATH"] = get_python_include_path() or ""
torch_root = get_torch_root_path() or ""
os.environ["PYTHON_LIB_PATH"] = torch_root
os.environ["LIBTORCH_ROOT"] = torch_root
os.environ["PYTORCH_INSTALL_PATH"] = torch_root
def set_npu_envs() -> None:
PYTORCH_NPU_INSTALL_PATH = os.getenv("PYTORCH_NPU_INSTALL_PATH")
if not PYTORCH_NPU_INSTALL_PATH:
os.environ["PYTORCH_NPU_INSTALL_PATH"] = "/usr/local/libtorch_npu"
set_common_envs()
set_npu_torch_ld_library_path()
NPU_TOOLKIT_HOME = os.getenv("NPU_TOOLKIT_HOME")
if not NPU_TOOLKIT_HOME:
os.environ["NPU_TOOLKIT_HOME"] = "/usr/local/Ascend/ascend-toolkit/latest"
NPU_TOOLKIT_HOME = "/usr/local/Ascend/ascend-toolkit/latest"
LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH", "")
arch = platform.machine()
LD_LIBRARY_PATH = NPU_TOOLKIT_HOME+"/lib64" + ":" + \
NPU_TOOLKIT_HOME+"/lib64/plugin/opskernel" + ":" + \
NPU_TOOLKIT_HOME+"/lib64/plugin/nnengine" + ":" + \
NPU_TOOLKIT_HOME+"/opp/built-in/op_impl/ai_core/tbe/op_tiling/lib/linux/"+arch + ":" + \
NPU_TOOLKIT_HOME+"/opp/vendors/xllm/op_api/lib" + ":" + \
NPU_TOOLKIT_HOME+"/tools/aml/lib64" + ":" + \
NPU_TOOLKIT_HOME+"/tools/aml/lib64/plugin" + ":" + \
LD_LIBRARY_PATH
os.environ["LD_LIBRARY_PATH"] = LD_LIBRARY_PATH
PYTHONPATH = os.getenv("PYTHONPATH", "")
PYTHONPATH = NPU_TOOLKIT_HOME+"/python/site-packages" + ":" + \
NPU_TOOLKIT_HOME+"/opp/built-in/op_impl/ai_core/tbe" + ":" + \
PYTHONPATH
os.environ["PYTHONPATH"] = PYTHONPATH
PATH = os.getenv("PATH", "")
PATH = NPU_TOOLKIT_HOME+"/bin" + ":" + \
NPU_TOOLKIT_HOME+"/compiler/ccec_compiler/bin" + ":" + \
NPU_TOOLKIT_HOME+"/tools/ccec_compiler/bin" + ":" + \
PATH
os.environ["PATH"] = PATH
os.environ["ASCEND_AICPU_PATH"] = NPU_TOOLKIT_HOME
os.environ["ASCEND_OPP_PATH"] = NPU_TOOLKIT_HOME+"/opp"
os.environ["TOOLCHAIN_HOME"] = NPU_TOOLKIT_HOME+"/toolkit"
os.environ["NPU_HOME_PATH"] = NPU_TOOLKIT_HOME
ATB_PATH = os.getenv("ATB_PATH")
if not ATB_PATH:
os.environ["ATB_PATH"] = "/usr/local/Ascend/nnal/atb"
ATB_PATH = "/usr/local/Ascend/nnal/atb"
cxx_abi = "1" if get_cxx_abi() else "0"
ATB_HOME_PATH = os.path.join(ATB_PATH, "latest", "atb", "cxx_abi_" + cxx_abi)
os.environ["ATB_HOME_PATH"] = ATB_HOME_PATH
LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH", "")
LD_LIBRARY_PATH = ATB_HOME_PATH+"/lib" + ":" + \
ATB_HOME_PATH+"/examples" + ":" + \
ATB_HOME_PATH+"/tests/atbopstest" + ":" + \
LD_LIBRARY_PATH
os.environ["LD_LIBRARY_PATH"] = LD_LIBRARY_PATH
PATH = os.getenv("PATH", "")
PATH = ATB_HOME_PATH+"/bin" + ":" + PATH
os.environ["PATH"] = PATH
os.environ["ATB_STREAM_SYNC_EVERY_KERNEL_ENABLE"] = "0"
os.environ["ATB_STREAM_SYNC_EVERY_RUNNER_ENABLE"] = "0"
os.environ["ATB_STREAM_SYNC_EVERY_OPERATION_ENABLE"] = "0"
os.environ["ATB_OPSRUNNER_SETUP_CACHE_ENABLE"] = "1"
os.environ["ATB_OPSRUNNER_KERNEL_CACHE_TYPE"] = "3"
os.environ["ATB_OPSRUNNER_KERNEL_CACHE_LOCAL_COUNT"] = "1"
os.environ["ATB_OPSRUNNER_KERNEL_CACHE_GLOABL_COUNT"] = "5"
os.environ["ATB_OPSRUNNER_KERNEL_CACHE_TILING_SIZE"] = "10240"
os.environ["ATB_WORKSPACE_MEM_ALLOC_ALG_TYPE"] = "1"
os.environ["ATB_WORKSPACE_MEM_ALLOC_GLOBAL"] = "0"
os.environ["ATB_COMPARE_TILING_EVERY_KERNEL"] = "0"
os.environ["ATB_HOST_TILING_BUFFER_BLOCK_NUM"] = "128"
os.environ["ATB_DEVICE_TILING_BUFFER_BLOCK_NUM"] = "32"
os.environ["ATB_SHARE_MEMORY_NAME_SUFFIX"] = ""
os.environ["ATB_LAUNCH_KERNEL_WITH_TILING"] = "1"
os.environ["ATB_MATMUL_SHUFFLE_K_ENABLE"] = "1"
os.environ["ATB_RUNNER_POOL_SIZE"] = "64"
os.environ["ASDOPS_HOME_PATH"] = ATB_HOME_PATH
os.environ["ASDOPS_MATMUL_PP_FLAG"] = "1"
os.environ["ASDOPS_LOG_LEVEL"] = "ERROR"
os.environ["ASDOPS_LOG_TO_STDOUT"] = "0"
os.environ["ASDOPS_LOG_TO_FILE"] = "1"
os.environ["ASDOPS_LOG_TO_FILE_FLUSH"] = "0"
os.environ["ASDOPS_LOG_TO_BOOST_TYPE"] = "atb"
os.environ["ASDOPS_LOG_PATH"] = "~"
os.environ["ASDOPS_TILING_PARSE_CACHE_DISABLE"] = "0"
os.environ["LCCL_DETERMINISTIC"] = "0"
os.environ["LCCL_PARALLEL"] = "0"
def set_mlu_envs() -> None:
set_common_envs()
os.environ["PYTORCH_MLU_INSTALL_PATH"] = get_torch_mlu_root_path() or ""
def set_cuda_envs() -> None:
set_common_envs()
os.environ["CUDA_TOOLKIT_ROOT_DIR"] = get_cuda_root_path() or ""
def set_ilu_envs() -> None:
set_common_envs()
os.environ["IXFORMER_INSTALL_PATH"] = get_ixformer_root_path() or ""
def set_musa_envs() -> None:
set_common_envs()
os.environ["PYTORCH_MUSA_INSTALL_PATH"] = get_torch_musa_root_path() or ""
import torch_musa
from torch_musa.utils.musa_extension import MUSA_HOME
os.environ["TORCH_MUSA_PYTHONPATH"] = torch_musa.core.cmake_prefix_path
os.environ["MUSA_TOOLKIT_ROOT_DIR"] = MUSA_HOME
os.environ["MKL_DIR"] = "/opt/intel/oneapi/mkl/lib/cmake/mkl"
os.environ["MKLROOT"] = "/opt/intel/oneapi/mkl"
os.environ["TorchMusa_DIR"] = torch_musa.core.cmake_prefix_path + "/TorchMusa"
os.environ["MUSAMAPPING_PATH"] = MUSA_HOME + "/tools/musamapping"

View File

@@ -0,0 +1,389 @@
import os
import sys
import platform
import subprocess
import sysconfig
import io
import shlex
from pathlib import Path
from typing import Optional
# get cpu architecture
def get_cpu_arch() -> str:
arch = platform.machine()
if "x86" in arch or "amd64" in arch:
return "x86"
elif "arm" in arch or "aarch64" in arch:
return "arm"
else:
raise ValueError(f"❌ Unsupported architecture: {arch}")
# get device type
def get_device_type() -> str:
import torch
if torch.cuda.is_available():
try:
import ixformer
return "ilu"
except ImportError:
return "cuda"
try:
import torch_musa
if torch.musa.is_available():
return "musa"
except ImportError:
pass
try:
import torch_mlu
if torch.mlu.is_available():
return "mlu"
except ImportError:
pass
try:
import torch_npu
if torch.npu.is_available():
return "npu"
except ImportError:
pass
print("❌ Unsupported device type, please check what device you are using.")
exit(1)
def get_base_dir() -> str:
helper_path = Path(__file__).resolve()
for parent in helper_path.parents:
if all((parent / marker).exists() for marker in ("setup.py", "version.txt", "CMakeLists.txt")):
return str(parent)
fallback_index = min(2, len(helper_path.parents) - 1)
return str(helper_path.parents[fallback_index])
def _join_path(*paths: str) -> str:
return os.path.join(get_base_dir(), *paths)
# return the python version as a string like "310" or "311" etc
def get_python_version() -> str:
return sysconfig.get_python_version().replace(".", "")
def get_torch_version(device: str) -> Optional[str]:
try:
import torch
if device == "cuda":
return torch.__version__
return torch.__version__.split('+')[0]
except ImportError:
return None
def get_version() -> str:
# first read from environment variable
version: Optional[str] = os.getenv("XLLM_VERSION")
if not version:
# then read from version file
with open(_join_path("version.txt"), "r") as f:
version = f.read().strip()
# strip the leading 'v' if present
if version and version.startswith("v"):
version = version[1:]
if not version:
raise RuntimeError("❌ Unable to find version string.")
version_suffix = os.getenv("XLLM_VERSION_SUFFIX")
if version_suffix:
version += version_suffix
return version
def read_readme() -> str:
p = _join_path("README.md")
if os.path.isfile(p):
return io.open(p, "r", encoding="utf-8").read()
else:
return ""
def get_cmake_dir() -> str:
plat_name = sysconfig.get_platform()
python_version = get_python_version()
dir_name = f"cmake.{plat_name}-{sys.implementation.name}-{python_version}"
cmake_dir = os.path.join(get_base_dir(), "build", dir_name)
os.makedirs(cmake_dir, exist_ok=True)
return cmake_dir
def check_and_install_pre_commit() -> None:
# check if .git is a directory
if not os.path.isdir(".git"):
return
if not os.path.exists(".git/hooks/pre-commit"):
ok, _, _ = _run_command(["pre-commit", "install"], check=True)
if not ok:
print("❌ Run 'pre-commit install' failed. Please install pre-commit: pip install pre-commit")
exit(1)
def _run_command(
args: list[str],
cwd: Optional[str] = None,
check: bool = True,
input_text: Optional[str] = None,
passthrough_output: bool = False,
) -> tuple[bool, str, str]:
try:
if passthrough_output:
result = subprocess.run(
args,
cwd=cwd,
check=False,
input=input_text,
text=True,
)
else:
result = subprocess.run(
args,
cwd=cwd,
check=False,
input=input_text,
capture_output=True,
text=True,
)
except OSError as e:
return False, "", str(e)
if passthrough_output:
if check and result.returncode != 0:
return False, "", f"exit code {result.returncode}"
return result.returncode == 0, "", ""
if check and result.returncode != 0:
return False, result.stdout.strip(), (result.stderr or result.stdout).strip()
return result.returncode == 0, result.stdout.strip(), (result.stderr or "").strip()
def _print_manual_check_commands(commands: list[str]) -> None:
print("🔎 You can run these commands to inspect manually:")
for cmd in commands:
print(f" {cmd}")
def _run_shell_command(
command: str,
cwd: Optional[str] = None,
check: bool = True,
passthrough_output: bool = False,
) -> bool:
ok, _, err = _run_command(
shlex.split(command),
cwd=cwd,
check=check,
passthrough_output=passthrough_output,
)
if not ok:
print(f"❌ Run shell command '{command}' failed: {err}")
return False
return True
def _run_git_command(repo_root: str, args: list[str]) -> tuple[bool, str]:
ok, output, err = _run_command(["git"] + args, cwd=repo_root, check=True)
if not ok and "No such file or directory" in err:
print(f"❌ Failed to run git command in {repo_root}: git {' '.join(args)}")
print(f" {err}")
return False, ""
if not ok:
print(f"❌ Git command failed in {repo_root}: git {' '.join(args)}")
if err:
print(f" {err}")
return False, ""
return True, output
def _collect_submodule_init_issues(repo_root: str) -> dict[str, str]:
ok, output = _run_git_command(repo_root, ["submodule", "status"])
if not ok:
print("❌ Failed to inspect submodule status.")
_print_manual_check_commands([
f"cd {repo_root}",
"git submodule status",
"git submodule update --init --recursive",
])
exit(1)
issues: dict[str, str] = {}
for line in output.splitlines():
if not line:
continue
state = line[0]
content = line[1:].strip()
parts = content.split()
if len(parts) < 2:
continue
path = parts[1]
commit = parts[0]
if state == "-":
issues[path] = f"uninitialized (expected commit starts with {commit})"
elif state == "+":
issues[path] = f"commit mismatch (checked-out commit starts with {commit})"
elif state == "U":
issues[path] = "merge conflict"
return issues
def _is_dependency_installed(required_files: list[str]) -> bool:
normalized_files = [
os.path.abspath(os.path.expanduser(file_path))
for file_path in required_files
]
return all(os.path.isfile(file_path) for file_path in normalized_files)
def _get_required_dependency_files() -> dict[str, list[str]]:
install_prefix = "/usr/local/yalantinglibs"
return {
"yalantinglibs": [
os.path.join(
install_prefix,
"lib",
"cmake",
"yalantinglibs",
"config.cmake",
),
],
}
def _collect_missing_dependencies(
dependency_files: dict[str, list[str]],
) -> dict[str, list[str]]:
missing: dict[str, list[str]] = {}
for name, required_files in dependency_files.items():
normalized_files = [
os.path.abspath(os.path.expanduser(file_path))
for file_path in required_files
]
if not _is_dependency_installed(normalized_files):
missing[name] = normalized_files
return missing
def _export_cmake_prefix_paths(prefix_paths: list[str]) -> None:
existing = os.environ.get("CMAKE_PREFIX_PATH", "")
merged_paths: list[str] = []
for path in existing.split(os.pathsep):
if not path:
continue
normalized_path = os.path.abspath(os.path.expanduser(path))
if normalized_path and normalized_path not in merged_paths:
merged_paths.append(normalized_path)
for path in prefix_paths:
if not path:
continue
normalized_path = os.path.abspath(os.path.expanduser(path))
if normalized_path and normalized_path not in merged_paths:
merged_paths.append(normalized_path)
if not merged_paths:
return
os.environ["CMAKE_PREFIX_PATH"] = os.pathsep.join(merged_paths)
print(f"✅ Export CMAKE_PREFIX_PATH to environment: {os.environ['CMAKE_PREFIX_PATH']}")
def _run_dependencies_script_or_exit(script_path: str) -> None:
if not _run_shell_command(
"sh third_party/dependencies.sh",
cwd=script_path,
passthrough_output=True,
):
print("❌ Run shell command 'sh third_party/dependencies.sh' failed!")
_print_manual_check_commands([
f"cd {script_path}",
"sh third_party/dependencies.sh",
])
exit(1)
def _validate_submodules_or_exit(repo_root: str) -> None:
issues = _collect_submodule_init_issues(repo_root)
if issues:
print("❌ Submodule commit check failed. Repositories not correctly initialized:")
for path in sorted(issues):
print(f" - {path}: {issues[path]}")
print("\nPlease align submodules and try again:")
print(" git submodule update --init --recursive [-f|--force]")
exit(1)
def _ensure_prebuild_dependencies_installed(script_path: str) -> None:
dependency_files = _get_required_dependency_files()
missing_dependencies = _collect_missing_dependencies(dependency_files)
if missing_dependencies:
missing_names = ", ".join(sorted(missing_dependencies))
print(f" Missing third-party dependencies: {missing_names}. Running dependencies.sh ...")
_run_dependencies_script_or_exit(script_path)
missing_dependencies = _collect_missing_dependencies(dependency_files)
if missing_dependencies:
print("❌ Some third-party dependencies are still missing after running dependencies.sh:")
manual_commands = [f"cd {script_path}", "sh third_party/dependencies.sh"]
for name in sorted(missing_dependencies):
print(f" - {name}")
for file_path in missing_dependencies[name]:
print(f" missing file: {file_path}")
manual_commands.append(f"test -f {file_path}")
_print_manual_check_commands(manual_commands)
exit(1)
_export_cmake_prefix_paths(["/usr/local/yalantinglibs"])
def _get_cmake_cache_path() -> str:
plat_name = sysconfig.get_platform()
dir_name = f"cmake.{plat_name}-{sys.implementation.name}-{get_python_version()}"
return os.path.join(get_base_dir(), "build", dir_name, "CMakeCache.txt")
def _get_xllm_ops_marker_path() -> str:
ascend_home = os.getenv("ASCEND_HOME_PATH", "/usr/local/Ascend/ascend-toolkit/latest")
opp_root = os.path.join(ascend_home, "opp")
return os.path.join(opp_root, "vendors", "xllm", ".xllm_ops_git_head")
def _clear_xllm_ops_cache_git_head(cache_path: str) -> bool:
if not os.path.isfile(cache_path):
return False
cache_prefix = "XLLM_OPS_GIT_HEAD_CACHED:"
with open(cache_path, "r", encoding="utf-8") as cache_file:
old_lines = cache_file.readlines()
new_lines = [line for line in old_lines if not line.startswith(cache_prefix)]
if new_lines == old_lines:
return False
temp_file_path = f"{cache_path}.tmp"
with open(temp_file_path, "w", encoding="utf-8") as cache_file:
cache_file.writelines(new_lines)
os.replace(temp_file_path, cache_path)
return True
def _ensure_xllm_ops_rebuild_on_missing_marker() -> None:
marker_path = _get_xllm_ops_marker_path()
if os.path.isfile(marker_path):
return
cmake_cache_path = _get_cmake_cache_path()
if _clear_xllm_ops_cache_git_head(cmake_cache_path):
print("✅ Cleared XLLM_OPS_GIT_HEAD_CACHED from CMake cache to trigger xllm_ops rebuild.")
return
def pre_build() -> None:
script_path = get_base_dir()
_validate_submodules_or_exit(script_path)
_ensure_prebuild_dependencies_installed(script_path)
_ensure_xllm_ops_rebuild_on_missing_marker()