"""Counsel's GDN replay kernel for vLLM, used by `counsel-serve-vllm --mode copy`. Stock vLLM keeps 1 + num_speculative_tokens recurrent-state slots per request for the gated linear-attention (GDN) layers, so long copy drafts (128-512 tokens) do not fit next to a 27B model on one GPU. The replay kernel keeps one slot per request and rolls back a rejected draft tail by replaying the accepted tokens with the stock arithmetic, so outputs are bit-identical to plain decoding. VLLM_GDN_REPLAY_CHUNKED=1 verifies the draft window with the chunked prefill kernel. The patched code is inert unless VLLM_GDN_REPLAY=1 (which only --mode copy sets). `counsel-install-kernels` patches the installed vLLM 0.32.0 in place (needs the `patch` tool). """ import importlib.metadata import importlib.util import os import shutil import subprocess import sys VLLM_VERSION = "0.32.0" HERE = os.path.dirname(os.path.abspath(__file__)) def install(argv=None) -> None: spec = importlib.util.find_spec("vllm") if spec is None: sys.exit("vLLM is not installed in this environment") version = importlib.metadata.version("vllm") if version != VLLM_VERSION: sys.exit(f"the GDN replay kernel is built for vLLM {VLLM_VERSION}; this environment has {version}") root = os.path.dirname(spec.origin) site = os.path.dirname(root) utils = os.path.join(root, "model_executor", "layers", "mamba", "mamba_utils.py") if "GDN_REPLAY_PATCH" not in open(utils).read(): diff = os.path.join(HERE, "gdn_replay.diff") for extra in (["--dry-run"], []): # check the whole patch applies before touching anything r = subprocess.run(["patch", "-p0", "--forward", "--batch", *extra, "-i", diff], cwd=site, capture_output=True, text=True) if r.returncode: sys.exit(f"could not patch vLLM in {site}:\n{r.stdout}{r.stderr}") shutil.copy(os.path.join(HERE, "gdn_replay.py"), os.path.join(root, "model_executor", "layers", "mamba", "ops", "gdn_replay.py")) print(f"GDN replay kernel installed in vLLM {version} at {root}")