Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

5.7 KB
Raw Blame
"""Load-time warm-up of the dense GEMM paths.

The first call of a dense product at a row count class not seen before in a process (more exactly: a key (n, k, rows rounded up to 256,
row pitch, out dtype) missing from the dense-GEMM tune cache, see hgemm.cu dtune) screens every rocBLAS solution on the live operands
and costs 0.15-4 s inside one request. This runs each dense Linear shape once per row class a request can produce, so the screening
happens at load. With a populated tune cache file the warm-up is a few seconds of ordinary GEMMs; on a cold cache it pays the screening.
Switch: EXL3_WARM_DENSE=0 turns it off. Results are unchanged: tuned winners are bit-exact with the untuned path.
"""
from __future__ import annotations
import os, time
import torch

SMALL_ROWS = (1, 2, 3, 4, 5, 6, 7, 8, 16, 64, 255)   # untuned paths (decode, verify, short prompts): first-use of hipBLASLt / kernels


def _dense_linears(model):
    from ..modules.linear import Linear
    seen = {}
    for m in model:
        if not isinstance(m, Linear) or ".experts." in m.key or m.out_features == 0 or m.out_features > 65536:   # lm_head: only the last rows reach it
            continue
        inner = type(m.inner).__name__
        seen.setdefault((m.in_features, m.out_features, inner, str(m.out_dtype)), m)
    return list(seen.values())


def seed_dense_tune(seed=None, path=None) -> int:
    """Append the shipped tuned solutions (dense_gemm_tune_seed.txt: Qwen shapes at row classes 4352..8192, the classes
    a merged last prefill chunk reaches) to the dense-GEMM tune file for the keys it does not hold yet. Call before the first
    dense GEMM of the process (the C++ tuner reads the file once). Returns the number of lines added."""
    from pathlib import Path
    seed = Path(seed) if seed else Path(__file__).with_name("dense_gemm_tune_seed.txt")
    if not seed.exists():
        return 0
    if path is None:
        path = os.environ.get("EXL3_DENSE_GEMM_TUNE_FILE") or os.path.join(os.environ.get("HOME", "/tmp"), ".cache/exllamav3/dense_gemm_tune.txt")
    path = Path(path)
    key = lambda ln: tuple(ln.split()[:6])
    have = set()
    if path.exists():
        have = {key(l) for l in path.read_text().splitlines() if len(l.split()) == 7}
    new = [l for l in seed.read_text().splitlines() if len(l.split()) == 7 and key(l) not in have]
    if new:
        path.parent.mkdir(parents=True, exist_ok=True)
        with open(path, "a") as f:
            f.write("\n".join(new) + "\n")
    return len(new)


@torch.inference_mode()
def warm_dense_gemm(models, max_rows: int = 4096, log=print) -> dict:
    """models: iterable of Model (target, draft). max_rows: largest prefill chunk in tokens. Returns stats."""
    if os.environ.get("EXL3_WARM_DENSE", "1") == "0":
        return {"skipped": True}
    from ..modules.quant.exl3 import row_pad_pitch
    t0 = time.perf_counter()
    mem0 = torch.cuda.memory_allocated()
    torch.cuda.reset_peak_memory_stats()
    calls = 0
    # A cold tune cache (no shipped seed for this library version) pays a rocBLAS screening per key, minutes in total.
    # Past the budget the rest of the warm-up runs untuned (bit-exact, same output); the first request at a class that
    # is still missing tunes it then. EXL3_WARM_TUNE_BUDGET_S=0 never defers.
    budget = float(os.environ.get("EXL3_WARM_TUNE_BUDGET_S", "90"))
    tune_env = os.environ.get("EXL3_DENSE_GEMM_TUNE")
    deferred = False
    nclass = (max_rows + 255) // 256
    try:
        for model in models:
            if model is None:
                continue
            for lin in _dense_linears(model):
                k = lin.in_features
                # The MTP input projection runs per stream: tokens x hc_count rows (4 x 4096 = class 64).
                mult = 4 if lin.key.endswith("mtp.fc_hidden") else 1
                rows_list = list(SMALL_ROWS) + [256 * c for c in range(1, nclass * mult + 1)]
                for rows in rows_list:
                    # unpadded input (contiguous), and the row-padded input a producer gives (only above PAD_MIN_ROWS, and only
                    # for activations of at most one prefill chunk: the per-stream MTP rows are never producer-padded)
                    pitches = [0]
                    if rows <= max_rows:
                        p = row_pad_pitch(lin, k, rows, {})
                        if p:
                            pitches.append(p)
                    for pitch in pitches:
                        buf = torch.randn((rows, pitch or k), device=lin.device, dtype=torch.half).mul_(0.1)
                        x = buf[:, :k] if pitch else buf
                        lin.forward(x, {})
                        calls += 1
                        if budget > 0 and not deferred and rows > 255 and time.perf_counter() - t0 > budget:
                            deferred = True
                            os.environ["EXL3_DENSE_GEMM_TUNE"] = "0"
                            log(f"warm_dense_gemm: tune budget {budget:.0f} s used, remaining classes tune at first use")
                        del buf, x
                torch.cuda.synchronize()
        torch.cuda.synchronize()
    finally:   # an error in a forward must not leave the tune switched off for the rest of the process
        if deferred:
            if tune_env is None:
                os.environ.pop("EXL3_DENSE_GEMM_TUNE", None)
            else:
                os.environ["EXL3_DENSE_GEMM_TUNE"] = tune_env
    peak = torch.cuda.max_memory_allocated() - mem0
    torch.cuda.empty_cache()
    st = {"seconds": time.perf_counter() - t0, "calls": calls, "deferred": deferred, "peak_extra_mib": peak / 2**20}
    log(f"warm_dense_gemm: {calls} calls in {st['seconds']:.1f} s, transient peak {st['peak_extra_mib']:.0f} MiB")
    return st