mirror of
https://github.com/deepseek-ai/DeepInfer.git
synced 2026-11-01 01:06:23 +08:00
The bring-up plan for a card that cannot build TileLang is to run the model on `tile_kernels.torch` -- pure-torch implementations of nearly every kernel the forward pass calls -- and replace the hot ones with Triton afterwards. That plan was blocked at the import: `import tile_kernels.torch` dragged in the whole compiled stack. Three eager imports were responsible, and each is now lazy or optional: * the top-level `__init__` re-exported every compiled kernel; it resolves them on access * `quant/__init__` did the same for 13 modules, and `import quant.common` runs it first * `quant/common.py` mixed host-side config dataclasses with `@T.macro` fragments; the dataclasses now import without TileLang, the macros are behind a guard, and its three dtype-handle members resolve TileLang when called rather than when imported `config.py` also only needed TileLang for three constants, now literals. One thing that looks like it should work and does not: a module-wide `from __future__ import annotations`. It also stringifies the annotations inside `@T.macro` bodies, which TileLang's macro parser evaluates itself, and it fails there. Only the four host-side annotations are quoted, individually. The patches are idempotent and reversible, and `verify_tile_kernels_patch.py` proves the surface is unchanged where TileLang is installed -- the kernel modules' wildcard imports of `quant.common` still see the macros, `from pkg import sub` still works, config knobs still take effect. The other half of the question, whether the references are numerically trustworthy, is answered by `compare_with_cuda.py`: on the recipe the engine actually uses on Hopper, per_token_cast, cast_back, swiglu_forward_and_per_token_cast and engram_hash are bitwise identical, and apply_rotary differs by one bf16 ULP on one element in 65536. The quantisation chain being bitwise equal is what makes a torch-first bring-up safe rather than approximate; a cast that were merely close would change tokens downstream. Not yet done, and recorded in the doc: MHC and the MoE gate still need their own comparison before anyone assumes they match, and the routing layer that points deepinfer's 25 call sites at these references does not exist yet.
81 lines
2.6 KiB
Python
Executable File
81 lines
2.6 KiB
Python
Executable File
"""tile_kernels: TileLang kernels for DeepSeek inference, plus pure-torch references.
|
|
|
|
Import is lazy on purpose. `tile_kernels.torch` is a pure-torch reference implementation of
|
|
every kernel here, and it has no TileLang dependency of its own -- but the eager
|
|
`from . import (config, quant, ...)` below used to pull the whole compiled stack in, so on a
|
|
host without TileLang even `import tile_kernels.torch` failed. Making the submodules lazy
|
|
means the reference path is usable wherever torch is, which is what a bring-up on a new
|
|
accelerator needs before any kernel has been ported.
|
|
|
|
Everything the eager form exported is still available; it is just resolved on first access.
|
|
`import tile_kernels; tile_kernels.quant.per_token_cast(...)` and
|
|
`from tile_kernels.quant import per_token_cast` both behave as before.
|
|
"""
|
|
|
|
import importlib
|
|
|
|
# Submodules that were imported eagerly before. Resolving one of these imports it.
|
|
_SUBMODULES = frozenset(
|
|
{
|
|
"config",
|
|
"utils",
|
|
"engram",
|
|
"mhc",
|
|
"modeling",
|
|
"moe",
|
|
"quant",
|
|
"rand",
|
|
"transform",
|
|
"torch",
|
|
"testing",
|
|
}
|
|
)
|
|
|
|
# Names re-exported from .config by the eager form.
|
|
_CONFIG_EXPORTS = frozenset(
|
|
{
|
|
"get_amax_clamp_for_quant",
|
|
"get_deterministic_algorithms",
|
|
"get_device_num_sms",
|
|
"get_num_ai_cores",
|
|
"get_num_cube_cores",
|
|
"get_num_sms",
|
|
"get_num_vec_cores",
|
|
"get_pdl",
|
|
"get_token_alignment",
|
|
"get_max_smem_per_sm",
|
|
"get_max_ub_per_vector_core",
|
|
"set_amax_clamp_for_quant",
|
|
"set_num_sms",
|
|
"set_pdl",
|
|
"set_token_alignment",
|
|
"use_deterministic_algorithms",
|
|
}
|
|
)
|
|
|
|
|
|
def __getattr__(name: str):
|
|
if name in _SUBMODULES:
|
|
module = importlib.import_module(f".{name}", __name__)
|
|
globals()[name] = module
|
|
return module
|
|
if name in _CONFIG_EXPORTS:
|
|
value = getattr(importlib.import_module(".config", __name__), name)
|
|
globals()[name] = value
|
|
return value
|
|
if name.startswith("__") and name.endswith("__"):
|
|
raise AttributeError(name)
|
|
try:
|
|
version = importlib.import_module("._version", __name__)
|
|
except ImportError:
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from None
|
|
if hasattr(version, name):
|
|
value = getattr(version, name)
|
|
globals()[name] = value
|
|
return value
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
|
|
|
|
def __dir__() -> list[str]:
|
|
return sorted(set(globals()) | _SUBMODULES | _CONFIG_EXPORTS)
|