commit 4ade8136ad2dbbcacb47ddc92bebba9606950992 Author: project6-dev Date: Wed Aug 12 03:17:31 2026 +0000 wudixzy stack docker build test: 2615-line qwen3_5.py + 12 prebuilt .so + 251-line patch_ops.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..faa0a98 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM git.modelhub.org.cn:9443/enginex-iluvatar/bi100-3.2.3-x86-ubuntu20.04-py3.10-poc-llm-infer:v1.2.3 +RUN mkdir -p /workspace +WORKDIR /workspace/ +# Copy all our engine patches +COPY ./qwen3_6_scripts /workspace/qwen3_6_scripts +COPY ./computility-run.yaml /workspace/computility-run.yaml +# Make patch script executable and run it +RUN chmod +x /workspace/qwen3_6_scripts/patch_ops.sh && \ + bash /workspace/qwen3_6_scripts/patch_ops.sh 2>&1 | tee /workspace/patch_ops.log ; \ + echo "[Dockerfile] patch_ops exit code: $?" diff --git a/computility-run.yaml b/computility-run.yaml new file mode 100644 index 0000000..41351f4 --- /dev/null +++ b/computility-run.yaml @@ -0,0 +1,44 @@ +concurrency: 1 +command: + - python3 + - -m + - vllm.entrypoints.openai.api_server + - --model + - /model + - --served-model-name + - llm + - --max-model-len + - '262144' + - --gpu-memory-utilization + - '0.9' + - --trust-remote-code + - -tp + - '4' + - --max-num-seqs + - '1' + - --disable-log-requests + - --disable-frontend-multiprocessing + - --max-num-batched-tokens + - '8192' + - --enable-chunked-prefill + - --max-seq-len-to-capture + - '32768' + - --enable-auto-tool-choice + - --tool-call-parser + - qwen3_coder + - --reasoning-parser + - qwen3 + - --enable-prefix-caching +env: + - name: VLLM_ENGINE_ITERATION_TIMEOUT_S + value: 3600 + - name: BI100_MOE_COREX_DIRECT_ROUTED + value: 1 + - name: BI100_GDN_COREX_PACKED_DECODE + value: 1 + - name: BI100_HYBRID_KV_ACCOUNTING + value: full_attention + - name: BI100_GDN_CACHE_POLICY + value: admission64 + - name: BI100_GDN_RESTORE_MODE + value: hybrid64 diff --git a/qwen3_6_scripts/api_server.py b/qwen3_6_scripts/api_server.py new file mode 100644 index 0000000..fa5ef30 --- /dev/null +++ b/qwen3_6_scripts/api_server.py @@ -0,0 +1,1080 @@ +import asyncio +import importlib +import inspect +import multiprocessing +import os +import regex as re +import signal +import socket +import sys +import tempfile +import time +from argparse import Namespace +from contextlib import asynccontextmanager +from functools import partial +from http import HTTPStatus +from typing import AsyncIterator, Set + + +def _bi100_field(value, name): + if isinstance(value, dict): + return value.get(name) + return getattr(value, name, None) + + +def _bi100_scalar(value): + return getattr(value, "value", value) + + +def _bi100_tool_choice_kind(value): + value = _bi100_scalar(value) + if value is None: + return "unset" + if isinstance(value, str): + return value if value in ("none", "auto", "required") else "other" + function = _bi100_field(value, "function") + if function is not None and isinstance( + _bi100_field(function, "name"), str): + return "named" + return "other" + + +def _bi100_image_source_kind(value): + if not isinstance(value, str): + return "other" + prefix = value[:8].lower() + if prefix.startswith("data:"): + return "data" + if prefix.startswith(("http://", "https://")): + return "remote" + return "other" + + +def _bi100_chat_4xx_reason(message): + if message == "messages must contain at least one message": + return "empty_messages" + if (isinstance(message, str) + and message.startswith("top_p must be in (0, 1], got ")): + return "invalid_top_p" + if (isinstance(message, str) + and message.startswith("max_tokens must be at least 1, got ")): + return "invalid_max_tokens" + if (isinstance(message, str) + and message.startswith("This model's maximum context length is ") + and "tokens. However, you requested " in message): + return "context_length_exceeded" + if (isinstance(message, str) and message.startswith("n=") + and " exceeds max_num_seqs=" in message): + return "n_exceeds_max_num_seqs" + if message == 'tool_choice = "required" is not supported!': + return "unsupported_tool_choice_required" + if (isinstance(message, str) + and message.startswith('"auto" tool choice requires ')): + return "tool_parser_unavailable" + if message == "Tool call arguments are not valid JSON.": + return "invalid_tool_arguments_json" + if (isinstance(message, str) + and message.startswith("Tool call arguments must ")): + return "invalid_tool_arguments_type" + if (isinstance(message, str) + and ( + (message.startswith("At most ") + and " image(s) may be provided in one request." in message) + or (message.startswith("You set image=") + and "items in the same prompt." in message))): + return "image_count_limit" + if message == "Unknown model type: qwen3_5_moe": + return "image_model_type_unsupported" + return "unclassified_chat_error" + + +def _bi100_chat_request_shape(request): + messages = _bi100_field(request, "messages") + if not isinstance(messages, (list, tuple)): + messages = () + tools = _bi100_field(request, "tools") + if not isinstance(tools, (list, tuple)): + tools = () + + system_count = 0 + system_part_message_count = 0 + system_text_part_count = 0 + system_other_part_count = 0 + tool_message_count = 0 + assistant_tool_message_count = 0 + image_count = 0 + image_data_count = 0 + image_remote_count = 0 + image_other_count = 0 + for message in messages: + role = _bi100_scalar(_bi100_field(message, "role")) + if role == "system": + system_count += 1 + elif role == "tool": + tool_message_count += 1 + elif (role == "assistant" + and _bi100_field(message, "tool_calls")): + assistant_tool_message_count += 1 + content = _bi100_field(message, "content") + if not isinstance(content, (list, tuple)): + continue + if role == "system": + system_part_message_count += 1 + for part in content: + part_type = _bi100_scalar(_bi100_field(part, "type")) + if role == "system": + if part_type == "text": + system_text_part_count += 1 + else: + system_other_part_count += 1 + if part_type in ("image", "image_url"): + image_count += 1 + image_url = _bi100_field(part, "image_url") + source_kind = _bi100_image_source_kind( + _bi100_field(image_url, "url")) + if source_kind == "data": + image_data_count += 1 + elif source_kind == "remote": + image_remote_count += 1 + else: + image_other_count += 1 + + strict_false_count = 0 + strict_true_count = 0 + for tool in tools: + function = _bi100_field(tool, "function") + strict = _bi100_field(function, "strict") + if strict is False: + strict_false_count += 1 + elif strict is True: + strict_true_count += 1 + + n = _bi100_field(request, "n") + return { + "message_count": len(messages), + "system_count": system_count, + "system_part_message_count": system_part_message_count, + "system_text_part_count": system_text_part_count, + "system_other_part_count": system_other_part_count, + "tool_count": len(tools), + "tool_message_count": tool_message_count, + "assistant_tool_message_count": assistant_tool_message_count, + "strict_false_count": strict_false_count, + "strict_true_count": strict_true_count, + "tool_choice_kind": _bi100_tool_choice_kind( + _bi100_field(request, "tool_choice")), + "image_count": image_count, + "image_data_count": image_data_count, + "image_remote_count": image_remote_count, + "image_other_count": image_other_count, + "has_image": image_count > 0, + "stream": bool(_bi100_field(request, "stream")), + "n": n if isinstance(n, int) else None, + } + + +def _bi100_validation_message_reason(error, tool_choice_kind): + if not isinstance(error, dict): + return None + + messages = [] + context = error.get("ctx") + if isinstance(context, dict): + context_error = context.get("error") + if isinstance(context_error, ValueError): + messages.append(str(context_error)) + + message = error.get("msg") + if isinstance(message, str): + if message.startswith("Value error, "): + message = message.removeprefix("Value error, ") + messages.append(message) + + for message in messages: + if message == "Tool call arguments are not valid JSON.": + return "invalid_tool_arguments_json" + if message in ( + "Tool call arguments must decode to a JSON object.", + "Tool call arguments must be a JSON object or a " + "JSON-encoded object string."): + return "invalid_tool_arguments_type" + if message == ( + "`tool_choice` must be a named tool, \"auto\", or \"none\"."): + if tool_choice_kind == "required": + return "unsupported_tool_choice_required" + return "request_validation_tool_choice" + return None + + +def _bi100_validation_reason(errors, request_shape=None): + categories = set() + message_categories = set() + tool_choice_kind = ( + request_shape.get("tool_choice_kind") + if isinstance(request_shape, dict) else None + ) + validation_errors = errors if isinstance(errors, (list, tuple)) else () + for error in validation_errors: + if not isinstance(error, dict): + continue + message_category = _bi100_validation_message_reason( + error, tool_choice_kind) + if message_category is not None: + message_categories.add(message_category) + location = error.get("loc") + if not isinstance(location, (list, tuple)): + continue + fields = [ + value for value in location + if isinstance(value, str) + and value not in ("body", "query", "path") + ] + if not fields: + continue + field = fields[0] + descendants = set(fields[1:]) + if field == "messages": + if "tool_call_id" in descendants: + categories.add("request_validation_message_tool_call_id") + elif "tool_calls" in descendants: + categories.add("request_validation_message_tool_calls") + elif "content" in descendants: + categories.add("request_validation_message_content") + elif "role" in descendants: + categories.add("request_validation_message_role") + else: + categories.add("request_validation_messages") + elif field == "tools": + if "strict" in descendants: + categories.add("request_validation_tool_strict") + elif "parameters" in descendants: + categories.add("request_validation_tool_parameters") + else: + categories.add("request_validation_tools") + elif field in ("tool_choice", "parallel_tool_calls"): + categories.add("request_validation_tool_choice") + elif field == "response_format": + categories.add("request_validation_response_format") + elif field in ("stream", "stream_options"): + categories.add("request_validation_streaming") + elif field in ("n", "max_tokens", "min_tokens", "stop"): + categories.add("request_validation_generation") + elif field in ( + "temperature", "top_p", "top_k", "frequency_penalty", + "presence_penalty", "repetition_penalty", "seed"): + categories.add("request_validation_sampling") + elif field == "model": + categories.add("request_validation_model") + else: + categories.add("request_validation_other") + + priority = ( + "request_validation_tool_strict", + "request_validation_tool_parameters", + "request_validation_tool_choice", + "request_validation_message_tool_call_id", + "request_validation_message_tool_calls", + "request_validation_message_content", + "request_validation_message_role", + "request_validation_messages", + "request_validation_tools", + "request_validation_response_format", + "request_validation_streaming", + "request_validation_generation", + "request_validation_sampling", + "request_validation_model", + "request_validation_other", + ) + for category in priority: + if category in categories: + return category + message_priority = ( + "invalid_tool_arguments_json", + "invalid_tool_arguments_type", + "unsupported_tool_choice_required", + "request_validation_tool_choice", + ) + for category in message_priority: + if category in message_categories: + return category + return "request_validation_unknown" + + +def _bi100_validation_identifier(value): + if not isinstance(value, str) or not value or len(value) > 64: + return "unknown" + if not value.isascii(): + return "unknown" + if not all(character.isalnum() or character in "._-" + for character in value): + return "unknown" + return value + + +def _bi100_validation_diagnostics(errors): + if not isinstance(errors, (list, tuple)): + return "unknown", "unknown" + try: + error_count = len(errors) + except Exception: + return "unknown", "unknown" + if error_count > 1: + return "multiple", "multiple" + if error_count == 0: + return "unknown", "unknown" + + try: + error = errors[0] + if not isinstance(error, dict): + return "unknown", "unknown" + location = error.get("loc") + validation_type = _bi100_validation_identifier(error.get("type")) + if not isinstance(location, (list, tuple)): + return "unknown", validation_type + if not location: + return "root", validation_type + index = 0 + if location[0] in ("body", "query", "path", "header", "cookie"): + index = 1 + if index >= len(location): + return "root", validation_type + field = location[index] + if field in ("__root__", "root"): + return "root", validation_type + return _bi100_validation_identifier(field), validation_type + except Exception: + return "unknown", "unknown" + + +def _bi100_safe_validation_errors(exc): + try: + errors = exc.errors() + if not isinstance(errors, (list, tuple)): + return () + return tuple(errors) + except Exception: + return () + + +def _bi100_startup_trace(message: str) -> None: + if os.getenv("BI100_EXECUTOR_STARTUP_DEBUG") == "1": + stamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + print(f"[BI100 STARTUP] {stamp} pid={os.getpid()} {message}", + file=sys.stderr, flush=True) + + +_bi100_startup_trace("api_server stdlib imports complete; loading runtime dependencies") + +import uvloop +from fastapi import APIRouter, FastAPI, Request +from fastapi.exceptions import RequestValidationError +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import JSONResponse, Response, StreamingResponse +from starlette.datastructures import State +from starlette.routing import Mount +from typing_extensions import assert_never + +import vllm.envs as envs +from vllm.config import ModelConfig +from vllm.engine.arg_utils import AsyncEngineArgs +from vllm.engine.async_llm_engine import AsyncLLMEngine +from vllm.engine.multiprocessing.client import MQLLMEngineClient +from vllm.engine.multiprocessing.engine import run_mp_engine +from vllm.engine.protocol import EngineClient +from vllm.entrypoints.launcher import serve_http +from vllm.entrypoints.logger import RequestLogger +from vllm.entrypoints.openai.cli_args import (make_arg_parser, + validate_parsed_serve_args) +# yapf conflicts with isort for this block +# yapf: disable +from vllm.entrypoints.openai.protocol import (ChatCompletionRequest, + ChatCompletionResponse, + CompletionRequest, + CompletionResponse, + DetokenizeRequest, + DetokenizeResponse, + EmbeddingRequest, + EmbeddingResponse, ErrorResponse, + LoadLoraAdapterRequest, + TokenizeRequest, + TokenizeResponse, + UnloadLoraAdapterRequest) +# yapf: enable +from vllm.entrypoints.openai.serving_chat import OpenAIServingChat +from vllm.entrypoints.openai.serving_completion import OpenAIServingCompletion +from vllm.entrypoints.openai.serving_embedding import OpenAIServingEmbedding +from vllm.entrypoints.openai.serving_engine import BaseModelPath +from vllm.entrypoints.openai.serving_tokenization import ( + OpenAIServingTokenization) +from vllm.entrypoints.openai.tool_parsers import ToolParserManager +from vllm.reasoning import ReasoningParserManager +from vllm.logger import init_logger +from vllm.usage.usage_lib import UsageContext +from vllm.utils import FlexibleArgumentParser, get_open_zmq_ipc_path +from vllm.version import __version__ as VLLM_VERSION + +TIMEOUT_KEEP_ALIVE = 5 # seconds + +prometheus_multiproc_dir: tempfile.TemporaryDirectory + +# Cannot use __name__ (https://github.com/vllm-project/vllm/pull/4765) +logger = init_logger('vllm.entrypoints.openai.api_server') + +_running_tasks: Set[asyncio.Task] = set() + +_bi100_startup_trace("api_server runtime imports complete") + + +def _bi100_log_chat_4xx(request, error) -> None: + code = getattr(error, "code", None) + if not isinstance(code, int) or not 400 <= code < 500: + return + shape = _bi100_chat_request_shape(request) + reason = _bi100_chat_4xx_reason(getattr(error, "message", None)) + logger.warning( + "[BI100 4XX] endpoint=chat code=%d reason=%s messages=%d " + "systems=%d system_part_msgs=%d system_text_parts=%d " + "system_other_parts=%d tools=%d tool_msgs=%d " + "assistant_tool_msgs=%d strict_false=%d strict_true=%d choice=%s " + "images=%d image_data=%d image_remote=%d image_other=%d " + "stream=%d n=%s", + code, + reason, + shape["message_count"], + shape["system_count"], + shape["system_part_message_count"], + shape["system_text_part_count"], + shape["system_other_part_count"], + shape["tool_count"], + shape["tool_message_count"], + shape["assistant_tool_message_count"], + shape["strict_false_count"], + shape["strict_true_count"], + shape["tool_choice_kind"], + shape["image_count"], + shape["image_data_count"], + shape["image_remote_count"], + shape["image_other_count"], + int(shape["stream"]), + shape["n"] if shape["n"] is not None else "unset", + ) + + +def _bi100_log_request_validation_4xx(raw_request, exc) -> None: + validation_errors = () + validation_field = "unknown" + validation_type = "unknown" + try: + validation_errors = _bi100_safe_validation_errors(exc) + validation_field, validation_type = ( + _bi100_validation_diagnostics(validation_errors) + ) + body = getattr(exc, "body", None) + url = getattr(raw_request, "url", None) + path = getattr(url, "path", "") + is_chat_request = ( + isinstance(path, str) + and path.endswith("/v1/chat/completions") + and isinstance(body, dict) + ) + shape = ( + _bi100_chat_request_shape(body) if is_chat_request else None + ) + reason = _bi100_validation_reason(validation_errors, shape) + if shape is not None: + if (reason == "request_validation_tools" + and shape["strict_true_count"]): + reason = "request_validation_tool_strict" + logger.warning( + "[BI100 4XX] endpoint=request_validation code=400 reason=%s " + "messages=%d systems=%d system_part_msgs=%d " + "system_text_parts=%d system_other_parts=%d tools=%d " + "tool_msgs=%d assistant_tool_msgs=%d strict_false=%d " + "strict_true=%d choice=%s images=%d image_data=%d " + "image_remote=%d image_other=%d stream=%d n=%s errors=%d " + "validation_field=%s validation_type=%s", + reason, + shape["message_count"], + shape["system_count"], + shape["system_part_message_count"], + shape["system_text_part_count"], + shape["system_other_part_count"], + shape["tool_count"], + shape["tool_message_count"], + shape["assistant_tool_message_count"], + shape["strict_false_count"], + shape["strict_true_count"], + shape["tool_choice_kind"], + shape["image_count"], + shape["image_data_count"], + shape["image_remote_count"], + shape["image_other_count"], + int(shape["stream"]), + shape["n"] if shape["n"] is not None else "unset", + len(validation_errors), + validation_field, + validation_type, + ) + else: + logger.warning( + "[BI100 4XX] endpoint=request_validation code=400 reason=%s " + "errors=%d validation_field=%s validation_type=%s", + reason, + len(validation_errors), + validation_field, + validation_type, + ) + return + except Exception: + pass + + try: + logger.warning( + "[BI100 4XX] endpoint=request_validation code=400 " + "reason=request_validation_unknown errors=%d " + "validation_field=%s validation_type=%s", + len(validation_errors), + validation_field, + validation_type, + ) + except Exception: + pass + + +@asynccontextmanager +async def lifespan(app: FastAPI): + try: + if app.state.log_stats: + engine_client: EngineClient = app.state.engine_client + + async def _force_log(): + while True: + await asyncio.sleep(10.) + await engine_client.do_log_stats() + + task = asyncio.create_task(_force_log()) + _running_tasks.add(task) + task.add_done_callback(_running_tasks.remove) + else: + task = None + try: + yield + finally: + if task is not None: + task.cancel() + finally: + # Ensure app state including engine ref is gc'd + del app.state + + +@asynccontextmanager +async def build_async_engine_client( + args: Namespace) -> AsyncIterator[EngineClient]: + + _bi100_startup_trace("building AsyncEngineArgs") + # Context manager to handle engine_client lifecycle + # Ensures everything is shutdown and cleaned up on error/exit + engine_args = AsyncEngineArgs.from_cli_args(args) + + _bi100_startup_trace("entering engine client construction") + async with build_async_engine_client_from_engine_args( + engine_args, args.disable_frontend_multiprocessing) as engine: + _bi100_startup_trace("engine client construction completed") + yield engine + + +@asynccontextmanager +async def build_async_engine_client_from_engine_args( + engine_args: AsyncEngineArgs, + disable_frontend_multiprocessing: bool = False, +) -> AsyncIterator[EngineClient]: + """ + Create EngineClient, either: + - in-process using the AsyncLLMEngine Directly + - multiprocess using AsyncLLMEngine RPC + + Returns the Client or None if the creation failed. + """ + + # Fall back + # TODO: fill out feature matrix. + if (MQLLMEngineClient.is_unsupported_config(engine_args) + or disable_frontend_multiprocessing): + engine_config = engine_args.create_engine_config() + uses_ray = getattr(AsyncLLMEngine._get_executor_cls(engine_config), + "uses_ray", False) + + build_engine = partial(AsyncLLMEngine.from_engine_args, + engine_args=engine_args, + engine_config=engine_config, + usage_context=UsageContext.OPENAI_API_SERVER) + if uses_ray: + # Must run in main thread with ray for its signal handlers to work + engine_client = build_engine() + else: + engine_client = await asyncio.get_running_loop().run_in_executor( + None, build_engine) + + yield engine_client + return + + # Otherwise, use the multiprocessing AsyncLLMEngine. + else: + if "PROMETHEUS_MULTIPROC_DIR" not in os.environ: + # Make TemporaryDirectory for prometheus multiprocessing + # Note: global TemporaryDirectory will be automatically + # cleaned up upon exit. + global prometheus_multiproc_dir + prometheus_multiproc_dir = tempfile.TemporaryDirectory() + os.environ[ + "PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir.name + else: + logger.warning( + "Found PROMETHEUS_MULTIPROC_DIR was set by user. " + "This directory must be wiped between vLLM runs or " + "you will find inaccurate metrics. Unset the variable " + "and vLLM will properly handle cleanup.") + + # Select random path for IPC. + ipc_path = get_open_zmq_ipc_path() + logger.info("Multiprocessing frontend to use %s for IPC Path.", + ipc_path) + + # Start RPCServer in separate process (holds the LLMEngine). + # the current process might have CUDA context, + # so we need to spawn a new process + context = multiprocessing.get_context("spawn") + + engine_process = context.Process(target=run_mp_engine, + args=(engine_args, + UsageContext.OPENAI_API_SERVER, + ipc_path)) + engine_process.start() + logger.info("Started engine process with PID %d", engine_process.pid) + + # Build RPCClient, which conforms to EngineClient Protocol. + # NOTE: Actually, this is not true yet. We still need to support + # embedding models via RPC (see TODO above) + engine_config = engine_args.create_engine_config() + mp_engine_client = MQLLMEngineClient(ipc_path, engine_config) + + try: + while True: + try: + await mp_engine_client.setup() + break + except TimeoutError: + if not engine_process.is_alive(): + raise RuntimeError( + "Engine process failed to start") from None + + yield mp_engine_client # type: ignore[misc] + finally: + # Ensure rpc server process was terminated + engine_process.terminate() + + # Close all open connections to the backend + mp_engine_client.close() + + # Wait for engine process to join + engine_process.join(4) + if engine_process.exitcode is None: + # Kill if taking longer than 5 seconds to stop + engine_process.kill() + + # Lazy import for prometheus multiprocessing. + # We need to set PROMETHEUS_MULTIPROC_DIR environment variable + # before prometheus_client is imported. + # See https://prometheus.github.io/client_python/multiprocess/ + from prometheus_client import multiprocess + multiprocess.mark_process_dead(engine_process.pid) + + +router = APIRouter() + + +def mount_metrics(app: FastAPI): + # Lazy import for prometheus multiprocessing. + # We need to set PROMETHEUS_MULTIPROC_DIR environment variable + # before prometheus_client is imported. + # See https://prometheus.github.io/client_python/multiprocess/ + from prometheus_client import (CollectorRegistry, make_asgi_app, + multiprocess) + + prometheus_multiproc_dir_path = os.getenv("PROMETHEUS_MULTIPROC_DIR", None) + if prometheus_multiproc_dir_path is not None: + logger.info("vLLM to use %s as PROMETHEUS_MULTIPROC_DIR", + prometheus_multiproc_dir_path) + registry = CollectorRegistry() + multiprocess.MultiProcessCollector(registry) + + # Add prometheus asgi middleware to route /metrics requests + metrics_route = Mount("/metrics", make_asgi_app(registry=registry)) + else: + # Add prometheus asgi middleware to route /metrics requests + metrics_route = Mount("/metrics", make_asgi_app()) + + # Workaround for 307 Redirect for /metrics + metrics_route.path_regex = re.compile("^/metrics(?P.*)$") + app.routes.append(metrics_route) + + +def chat(request: Request) -> OpenAIServingChat: + return request.app.state.openai_serving_chat + + +def completion(request: Request) -> OpenAIServingCompletion: + return request.app.state.openai_serving_completion + + +def tokenization(request: Request) -> OpenAIServingTokenization: + return request.app.state.openai_serving_tokenization + + +def embedding(request: Request) -> OpenAIServingEmbedding: + return request.app.state.openai_serving_embedding + + +def engine_client(request: Request) -> EngineClient: + return request.app.state.engine_client + + +@router.get("/health") +async def health(raw_request: Request) -> Response: + """Health check.""" + await engine_client(raw_request).check_health() + return Response(status_code=200) + + +@router.post("/tokenize") +async def tokenize(request: TokenizeRequest, raw_request: Request): + generator = await tokenization(raw_request).create_tokenize(request) + if isinstance(generator, ErrorResponse): + return JSONResponse(content=generator.model_dump(), + status_code=generator.code) + elif isinstance(generator, TokenizeResponse): + return JSONResponse(content=generator.model_dump()) + + assert_never(generator) + + +@router.post("/detokenize") +async def detokenize(request: DetokenizeRequest, raw_request: Request): + generator = await tokenization(raw_request).create_detokenize(request) + if isinstance(generator, ErrorResponse): + return JSONResponse(content=generator.model_dump(), + status_code=generator.code) + elif isinstance(generator, DetokenizeResponse): + return JSONResponse(content=generator.model_dump()) + + assert_never(generator) + + +@router.get("/v1/models") +async def show_available_models(raw_request: Request): + models = await completion(raw_request).show_available_models() + return JSONResponse(content=models.model_dump()) + + +@router.get("/version") +async def show_version(): + ver = {"version": VLLM_VERSION} + return JSONResponse(content=ver) + + +@router.post("/v1/chat/completions") +async def create_chat_completion(request: ChatCompletionRequest, + raw_request: Request): + + generator = await chat(raw_request).create_chat_completion( + request, raw_request) + + if isinstance(generator, ErrorResponse): + _bi100_log_chat_4xx(request, generator) + return JSONResponse(content=generator.model_dump(), + status_code=generator.code) + + elif isinstance(generator, ChatCompletionResponse): + return JSONResponse(content=generator.model_dump()) + + return StreamingResponse(content=generator, media_type="text/event-stream") + + +@router.post("/v1/completions") +async def create_completion(request: CompletionRequest, raw_request: Request): + generator = await completion(raw_request).create_completion( + request, raw_request) + if isinstance(generator, ErrorResponse): + return JSONResponse(content=generator.model_dump(), + status_code=generator.code) + elif isinstance(generator, CompletionResponse): + return JSONResponse(content=generator.model_dump()) + + return StreamingResponse(content=generator, media_type="text/event-stream") + + +@router.post("/v1/embeddings") +async def create_embedding(request: EmbeddingRequest, raw_request: Request): + generator = await embedding(raw_request).create_embedding( + request, raw_request) + if isinstance(generator, ErrorResponse): + return JSONResponse(content=generator.model_dump(), + status_code=generator.code) + elif isinstance(generator, EmbeddingResponse): + return JSONResponse(content=generator.model_dump()) + + assert_never(generator) + + +if envs.VLLM_TORCH_PROFILER_DIR: + logger.warning( + "Torch Profiler is enabled in the API server. This should ONLY be " + "used for local development!") + + @router.post("/start_profile") + async def start_profile(raw_request: Request): + logger.info("Starting profiler...") + await engine_client(raw_request).start_profile() + logger.info("Profiler started.") + return Response(status_code=200) + + @router.post("/stop_profile") + async def stop_profile(raw_request: Request): + logger.info("Stopping profiler...") + await engine_client(raw_request).stop_profile() + logger.info("Profiler stopped.") + return Response(status_code=200) + + +if envs.VLLM_ALLOW_RUNTIME_LORA_UPDATING: + logger.warning( + "Lora dynamic loading & unloading is enabled in the API server. " + "This should ONLY be used for local development!") + + @router.post("/v1/load_lora_adapter") + async def load_lora_adapter(request: LoadLoraAdapterRequest, + raw_request: Request): + response = await chat(raw_request).load_lora_adapter(request) + if isinstance(response, ErrorResponse): + return JSONResponse(content=response.model_dump(), + status_code=response.code) + + response = await completion(raw_request).load_lora_adapter(request) + if isinstance(response, ErrorResponse): + return JSONResponse(content=response.model_dump(), + status_code=response.code) + + return Response(status_code=200, content=response) + + @router.post("/v1/unload_lora_adapter") + async def unload_lora_adapter(request: UnloadLoraAdapterRequest, + raw_request: Request): + response = await chat(raw_request).unload_lora_adapter(request) + if isinstance(response, ErrorResponse): + return JSONResponse(content=response.model_dump(), + status_code=response.code) + + response = await completion(raw_request).unload_lora_adapter(request) + if isinstance(response, ErrorResponse): + return JSONResponse(content=response.model_dump(), + status_code=response.code) + + return Response(status_code=200, content=response) + + +def build_app(args: Namespace) -> FastAPI: + if args.disable_fastapi_docs: + app = FastAPI(openapi_url=None, + docs_url=None, + redoc_url=None, + lifespan=lifespan) + else: + app = FastAPI(lifespan=lifespan) + app.include_router(router) + app.root_path = args.root_path + + mount_metrics(app) + + app.add_middleware( + CORSMiddleware, + allow_origins=args.allowed_origins, + allow_credentials=args.allow_credentials, + allow_methods=args.allowed_methods, + allow_headers=args.allowed_headers, + ) + + @app.exception_handler(RequestValidationError) + async def validation_exception_handler(raw_request, exc): + _bi100_log_request_validation_4xx(raw_request, exc) + chat = app.state.openai_serving_chat + err = chat.create_error_response(message=str(exc)) + return JSONResponse(err.model_dump(), + status_code=HTTPStatus.BAD_REQUEST) + + if token := envs.VLLM_API_KEY or args.api_key: + + @app.middleware("http") + async def authentication(request: Request, call_next): + root_path = "" if args.root_path is None else args.root_path + if request.method == "OPTIONS": + return await call_next(request) + if not request.url.path.startswith(f"{root_path}/v1"): + return await call_next(request) + if request.headers.get("Authorization") != "Bearer " + token: + return JSONResponse(content={"error": "Unauthorized"}, + status_code=401) + return await call_next(request) + + for middleware in args.middleware: + module_path, object_name = middleware.rsplit(".", 1) + imported = getattr(importlib.import_module(module_path), object_name) + if inspect.isclass(imported): + app.add_middleware(imported) + elif inspect.iscoroutinefunction(imported): + app.middleware("http")(imported) + else: + raise ValueError(f"Invalid middleware {middleware}. " + f"Must be a function or a class.") + + return app + + +def init_app_state( + engine_client: EngineClient, + model_config: ModelConfig, + state: State, + args: Namespace, +) -> None: + if args.served_model_name is not None: + served_model_names = args.served_model_name + else: + served_model_names = [args.model] + + if args.disable_log_requests: + request_logger = None + else: + request_logger = RequestLogger(max_log_len=args.max_log_len) + + base_model_paths = [ + BaseModelPath(name=name, model_path=args.model) + for name in served_model_names + ] + + state.engine_client = engine_client + state.log_stats = not args.disable_log_stats + + state.openai_serving_chat = OpenAIServingChat( + engine_client, + model_config, + base_model_paths, + args.response_role, + lora_modules=args.lora_modules, + prompt_adapters=args.prompt_adapters, + request_logger=request_logger, + chat_template=args.chat_template, + return_tokens_as_token_ids=args.return_tokens_as_token_ids, + enable_auto_tools=args.enable_auto_tool_choice, + tool_parser=args.tool_call_parser, + reasoning_parser=getattr(args, 'reasoning_parser', None)) + state.openai_serving_completion = OpenAIServingCompletion( + engine_client, + model_config, + base_model_paths, + lora_modules=args.lora_modules, + prompt_adapters=args.prompt_adapters, + request_logger=request_logger, + return_tokens_as_token_ids=args.return_tokens_as_token_ids, + ) + state.openai_serving_embedding = OpenAIServingEmbedding( + engine_client, + model_config, + base_model_paths, + request_logger=request_logger, + ) + state.openai_serving_tokenization = OpenAIServingTokenization( + engine_client, + model_config, + base_model_paths, + lora_modules=args.lora_modules, + request_logger=request_logger, + chat_template=args.chat_template, + ) + + +async def run_server(args, **uvicorn_kwargs) -> None: + _bi100_startup_trace("run_server entered") + logger.info("vLLM API server version %s", VLLM_VERSION) + logger.info("args: %s", args) + + if args.tool_parser_plugin and len(args.tool_parser_plugin) > 3: + ToolParserManager.import_tool_parser(args.tool_parser_plugin) + + valide_tool_parses = ToolParserManager.tool_parsers.keys() + if args.enable_auto_tool_choice \ + and args.tool_call_parser not in valide_tool_parses: + raise KeyError(f"invalid tool call parser: {args.tool_call_parser} " + f"(chose from {{ {','.join(valide_tool_parses)} }})") + + reasoning_parser = getattr(args, 'reasoning_parser', None) + if reasoning_parser: + valid_reasoning = ReasoningParserManager.list_registered() + if reasoning_parser not in valid_reasoning: + raise KeyError( + f"invalid reasoning parser: {reasoning_parser} " + f"(chose from {{ {','.join(valid_reasoning)} }})") + + # workaround to make sure that we bind the port before the engine is set up. + # This avoids race conditions with ray. + # see https://github.com/vllm-project/vllm/issues/8204 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind(("", args.port)) + + def signal_handler(*_) -> None: + # Interrupt server on sigterm while initializing + raise KeyboardInterrupt("terminated") + + signal.signal(signal.SIGTERM, signal_handler) + + _bi100_startup_trace("starting engine client context") + async with build_async_engine_client(args) as engine_client: + _bi100_startup_trace("building FastAPI application") + app = build_app(args) + + _bi100_startup_trace("requesting model config from engine") + model_config = await engine_client.get_model_config() + _bi100_startup_trace("model config received; initializing app state") + init_app_state(engine_client, model_config, app.state, args) + + _bi100_startup_trace("starting HTTP server") + shutdown_task = await serve_http( + app, + host=args.host, + port=args.port, + log_level=args.uvicorn_log_level, + timeout_keep_alive=TIMEOUT_KEEP_ALIVE, + ssl_keyfile=args.ssl_keyfile, + ssl_certfile=args.ssl_certfile, + ssl_ca_certs=args.ssl_ca_certs, + ssl_cert_reqs=args.ssl_cert_reqs, + fd=sock.fileno(), + **uvicorn_kwargs, + ) + + # NB: Await server shutdown only after the backend context is exited + await shutdown_task + + +if __name__ == "__main__": + _bi100_startup_trace("api_server __main__ entered") + # NOTE(simon): + # This section should be in sync with vllm/scripts.py for CLI entrypoints. + parser = FlexibleArgumentParser( + description="vLLM OpenAI-Compatible RESTful API server.") + parser = make_arg_parser(parser) + args = parser.parse_args() + validate_parsed_serve_args(args) + _bi100_startup_trace( + f"arguments parsed model={args.model} tp={args.tensor_parallel_size} " + f"max_model_len={args.max_model_len}") + + uvloop.run(run_server(args)) diff --git a/qwen3_6_scripts/bi100_env.py b/qwen3_6_scripts/bi100_env.py new file mode 100644 index 0000000..468eb49 --- /dev/null +++ b/qwen3_6_scripts/bi100_env.py @@ -0,0 +1,26 @@ +import os + + +def env_bool(name: str, default: bool = False) -> bool: + raw = os.getenv(name) + if raw is None: + return default + if raw in ("1", "true", "True", "yes", "YES", "on", "ON"): + return True + if raw in ("0", "false", "False", "no", "NO", "off", "OFF"): + return False + raise RuntimeError(f"{name} must be boolean, got {raw!r}") + + +def env_int(name: str, default: int, min_value: int, max_value: int) -> int: + raw = os.getenv(name) + if raw is None: + return default + try: + value = int(raw) + except ValueError as exc: + raise RuntimeError(f"{name} must be int, got {raw!r}") from exc + if not (min_value <= value <= max_value): + raise RuntimeError( + f"{name}={value} outside [{min_value}, {max_value}]") + return value diff --git a/qwen3_6_scripts/bi100_profile.py b/qwen3_6_scripts/bi100_profile.py new file mode 100644 index 0000000..9869702 --- /dev/null +++ b/qwen3_6_scripts/bi100_profile.py @@ -0,0 +1,237 @@ +import contextlib +import fnmatch +import functools +import json +import os +import re +import threading +import time + +from vllm.logger import init_logger + +logger = init_logger(__name__) +_EVENT_SCHEMA = "bi100-profile-event-v1" +_EVENT_VERSION = 1 +_NAME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_.-]{0,63}$") +_FILTER_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_.*?-]{0,63}$") + + +def _strict_bool(name: str, default: str = "0") -> bool: + value = os.getenv(name, default).strip() + if value not in {"0", "1"}: + raise RuntimeError(f"{name} must be exactly 0 or 1, got {value!r}") + return value == "1" + + +_ENABLED = _strict_bool("BI100_PROFILE") +_INCLUDE_STARTUP = _strict_bool("BI100_PROFILE_INCLUDE_STARTUP") +_MODE = os.getenv("BI100_PROFILE_MODE", "sync").strip().lower() +_FILTERS = tuple( + item.strip() + for item in os.getenv("BI100_PROFILE_FILTER", "").split(",") + if item.strip() +) +if _ENABLED and _MODE not in {"sync", "event"}: + raise RuntimeError(f"unsupported BI100_PROFILE_MODE={_MODE!r}") +if _ENABLED and any(_FILTER_RE.fullmatch(pattern) is None + for pattern in _FILTERS): + raise RuntimeError("BI100_PROFILE_FILTER contains an invalid pattern") + +_EVENT_RECORDS = [] +_COUNTERS = {} +_LOCK = threading.Lock() +_FORWARD_INDEX = 0 +_LAST_FLUSH_NS = None +_ACTIVE_FORWARD_TOKEN = None +_NEXT_FORWARD_TOKEN = 0 + + +def _enabled_for(name: str) -> bool: + return (_ENABLED + and (not _FILTERS + or any(fnmatch.fnmatchcase(name, pattern) + for pattern in _FILTERS))) + + +def _skip_startup() -> bool: + return (not _INCLUDE_STARTUP + and os.getenv("BI100_IN_STARTUP_PROFILE") == "1") + + +def bi100_profile_event_enabled() -> bool: + return _ENABLED and _MODE == "event" and not _skip_startup() + + +def _begin_profile_forward(): + global _ACTIVE_FORWARD_TOKEN, _NEXT_FORWARD_TOKEN + if not bi100_profile_event_enabled(): + return None + with _LOCK: + _EVENT_RECORDS.clear() + _COUNTERS.clear() + token = _NEXT_FORWARD_TOKEN + _NEXT_FORWARD_TOKEN += 1 + _ACTIVE_FORWARD_TOKEN = token + return token + + +def _abort_profile_forward(token) -> None: + global _ACTIVE_FORWARD_TOKEN + if token is None: + return + with _LOCK: + if _ACTIVE_FORWARD_TOKEN != token: + return + _EVENT_RECORDS.clear() + _COUNTERS.clear() + _ACTIVE_FORWARD_TOKEN = None + + +def bi100_profile_transaction(function): + """Keep one top-level model forward isolated from failed forwards.""" + @functools.wraps(function) + def wrapped(*args, **kwargs): + token = _begin_profile_forward() + if token is None: + return function(*args, **kwargs) + try: + result = function(*args, **kwargs) + except BaseException: + _abort_profile_forward(token) + raise + with _LOCK: + was_flushed = _ACTIVE_FORWARD_TOKEN != token + if not was_flushed: + _abort_profile_forward(token) + raise RuntimeError( + "BI100 profile transaction completed without a flush") + return result + + return wrapped + + +def _normalize_metadata(metadata): + normalized = {} + for key, value in metadata.items(): + if not isinstance(key, str) or _NAME_RE.fullmatch(key) is None: + raise TypeError("profile metadata keys must be bounded names") + if isinstance(value, bool): + normalized[key] = value + elif isinstance(value, int) and not isinstance(value, bool): + normalized[key] = value + elif isinstance(value, str) and len(value) <= 64: + normalized[key] = value + else: + raise TypeError( + "profile metadata values must be bool, int, or short strings") + return normalized + + +def bi100_profile_count(name: str, **metadata) -> None: + """Record privacy-safe path metadata for the current model forward.""" + if not bi100_profile_event_enabled() or not _enabled_for(name): + return + if not isinstance(name, str) or _NAME_RE.fullmatch(name) is None: + raise TypeError("profile counter name must be a bounded name") + normalized = _normalize_metadata(metadata) + encoded = json.dumps( + {"name": name, **normalized}, sort_keys=True, separators=(",", ":")) + with _LOCK: + _COUNTERS[encoded] = _COUNTERS.get(encoded, 0) + 1 + + +@contextlib.contextmanager +def bi100_timer(name: str): + if not _enabled_for(name) or _skip_startup(): + yield + return + import torch + + if _MODE == "event": + started = torch.cuda.Event(enable_timing=True) + finished = torch.cuda.Event(enable_timing=True) + host_started_ns = time.monotonic_ns() + started.record() + try: + yield + finally: + finished.record() + with _LOCK: + _EVENT_RECORDS.append( + (name, started, finished, host_started_ns)) + return + + torch.cuda.synchronize() + t0 = time.perf_counter() + try: + yield + finally: + torch.cuda.synchronize() + logger.info("[BI100_PROFILE] %s %.3f ms", name, + (time.perf_counter() - t0) * 1000) + + +def bi100_profile_flush(*, tp_rank, **metadata): + """Synchronize once and emit one aggregate event record per model forward.""" + global _ACTIVE_FORWARD_TOKEN, _FORWARD_INDEX, _LAST_FLUSH_NS + if not bi100_profile_event_enabled(): + return None + if (not isinstance(tp_rank, int) or isinstance(tp_rank, bool) + or not 0 <= tp_rank < 256): + raise TypeError("profile TP rank must be an integer in [0, 255]") + normalized_metadata = _normalize_metadata(metadata) + + with _LOCK: + records = list(_EVENT_RECORDS) + counters = dict(_COUNTERS) + _EVENT_RECORDS.clear() + _COUNTERS.clear() + _ACTIVE_FORWARD_TOKEN = None + if not records: + return None + + import torch + + torch.cuda.synchronize() + flushed_ns = time.monotonic_ns() + regions = {} + model_started_ns = [] + for name, started, finished, host_started_ns in records: + stats = regions.setdefault(name, {"count": 0, "total_ms": 0.0}) + stats["count"] += 1 + stats["total_ms"] += float(started.elapsed_time(finished)) + if name == "model.forward": + model_started_ns.append(host_started_ns) + + counter_rows = [] + for encoded, count in sorted(counters.items()): + row = json.loads(encoded) + row["count"] = count + counter_rows.append(row) + + first_model_started_ns = ( + min(model_started_ns) if model_started_ns else None) + payload = { + "schema": _EVENT_SCHEMA, + "version": _EVENT_VERSION, + "tp_rank": tp_rank, + "forward_index": _FORWARD_INDEX, + "metadata": normalized_metadata, + "event_count": len(records), + "model_forward_event_count": len(model_started_ns), + "regions": regions, + "counters": counter_rows, + "host_model_start_to_flush_ms": ( + (flushed_ns - first_model_started_ns) / 1_000_000 + if first_model_started_ns is not None else None), + "host_gap_since_previous_flush_ms": ( + (first_model_started_ns - _LAST_FLUSH_NS) / 1_000_000 + if first_model_started_ns is not None + and _LAST_FLUSH_NS is not None + else None), + } + _FORWARD_INDEX += 1 + _LAST_FLUSH_NS = flushed_ns + logger.info("[BI100_PROFILE_EVENT] %s", + json.dumps(payload, sort_keys=True, separators=(",", ":"))) + return payload diff --git a/qwen3_6_scripts/block_major_kv_cache.py b/qwen3_6_scripts/block_major_kv_cache.py new file mode 100644 index 0000000..80eb4d2 --- /dev/null +++ b/qwen3_6_scripts/block_major_kv_cache.py @@ -0,0 +1,398 @@ +from __future__ import annotations + +import os +import time +from collections.abc import Mapping + +import torch + +from vllm.logger import init_logger + + +logger = init_logger(__name__) + +ENABLE_ENV = "BI100_BLOCK_MAJOR_CPU_KV" +TRACE_ENV = "BI100_BLOCK_MAJOR_CPU_KV_TRACE" +CPU_OFFLOAD_ENV = "BI100_CPU_KV_OFFLOAD" +HYBRID_ACCOUNTING_ENV = "BI100_HYBRID_KV_ACCOUNTING" +NUM_ATTENTION_LAYERS = 10 +KV_PLANES = 2 +ELEMENTS_PER_PLANE_BLOCK = 4096 +STAGING_BLOCKS = 512 +STAGING_BUFFER_COUNT = 2 +BYTES_PER_BLOCK = ( + NUM_ATTENTION_LAYERS * KV_PLANES * ELEMENTS_PER_PLANE_BLOCK * 2 +) +GPU_STAGING_BYTES = STAGING_BLOCKS * STAGING_BUFFER_COUNT * BYTES_PER_BLOCK + + +def _strict_binary_selector( + name: str, + environ: Mapping[str, str] | None = None, +) -> bool: + source = os.environ if environ is None else environ + raw = source.get(name, "0") + if raw == "0": + return False + if raw == "1": + return True + raise RuntimeError(f"{name} must be exactly '0' or '1', got {raw!r}") + + +def block_major_cpu_kv_enabled( + environ: Mapping[str, str] | None = None, +) -> bool: + return _strict_binary_selector(ENABLE_ENV, environ) + + +def block_major_cpu_kv_trace_enabled( + environ: Mapping[str, str] | None = None, +) -> bool: + return _strict_binary_selector(TRACE_ENV, environ) + + +def _require_block_major_runtime( + environ: Mapping[str, str] | None = None, +) -> None: + source = os.environ if environ is None else environ + if source.get(CPU_OFFLOAD_ENV, "0") != "1": + raise RuntimeError( + f"{ENABLE_ENV}=1 requires {CPU_OFFLOAD_ENV}=1") + if source.get(HYBRID_ACCOUNTING_ENV, "legacy40") != "full_attention": + raise RuntimeError( + f"{ENABLE_ENV}=1 requires " + f"{HYBRID_ACCOUNTING_ENV}=full_attention") + + +def reserve_block_major_gpu_blocks( + num_gpu_blocks: int, + cache_block_size: int, + environ: Mapping[str, str] | None = None, +) -> int: + if (not isinstance(num_gpu_blocks, int) + or isinstance(num_gpu_blocks, bool) + or num_gpu_blocks < 0): + raise ValueError("num_gpu_blocks must be a non-negative integer") + if not block_major_cpu_kv_enabled(environ): + return num_gpu_blocks + + _require_block_major_runtime(environ) + if cache_block_size != BYTES_PER_BLOCK: + raise RuntimeError( + f"{ENABLE_ENV}=1 requires cache block size " + f"{BYTES_PER_BLOCK}, got {cache_block_size}") + reserved_blocks = ( + GPU_STAGING_BYTES + cache_block_size - 1 + ) // cache_block_size + remaining_blocks = num_gpu_blocks - reserved_blocks + if remaining_blocks <= 0: + raise RuntimeError( + "block-major GPU staging leaves no usable GPU KV blocks") + logger.info( + "[BI100 BLOCK KV] capacity reserve blocks=%d bytes=%d " + "profiled_blocks=%d usable_blocks=%d", + reserved_blocks, + GPU_STAGING_BYTES, + num_gpu_blocks, + remaining_blocks, + ) + return remaining_blocks + + +def validate_block_mapping( + mapping: torch.Tensor, + source_limit: int, + destination_limit: int, +) -> tuple[torch.Tensor, torch.Tensor]: + if not isinstance(mapping, torch.Tensor): + raise TypeError("block mapping must be a torch.Tensor") + if mapping.device.type != "cpu": + raise ValueError("block mapping must be on CPU") + if mapping.dtype != torch.int64: + raise ValueError("block mapping must use torch.int64") + if not mapping.is_contiguous(): + raise ValueError("block mapping must be contiguous") + if mapping.dim() != 2 or mapping.shape[1] != 2: + raise ValueError("block mapping must have shape [N, 2]") + if source_limit <= 0 or destination_limit <= 0: + raise ValueError("block mapping limits must be positive") + + sources: set[int] = set() + destinations: set[int] = set() + for row, pair in enumerate(mapping.tolist()): + source, destination = pair + if not 0 <= source < source_limit: + raise ValueError( + f"source block out of range at row {row}: {source}") + if not 0 <= destination < destination_limit: + raise ValueError( + f"destination block out of range at row {row}: " + f"{destination}") + if source in sources: + raise ValueError(f"duplicate source block: {source}") + if destination in destinations: + raise ValueError(f"duplicate destination block: {destination}") + sources.add(source) + destinations.add(destination) + + return mapping[:, 0].contiguous(), mapping[:, 1].contiguous() + + +class BlockMajorCpuKVCache: + + def __init__( + self, + gpu_cache: list[torch.Tensor], + num_cpu_blocks: int, + pin_memory: bool, + ) -> None: + self._validate_gpu_cache(gpu_cache) + if block_major_cpu_kv_enabled(): + _require_block_major_runtime() + if num_cpu_blocks <= 0: + raise RuntimeError( + f"{ENABLE_ENV}=1 requires a positive CPU block count") + if not pin_memory: + raise RuntimeError( + f"{ENABLE_ENV}=1 requires pinned CPU memory") + + try: + from vllm import corex_block_major_kv_transfer as extension + except ImportError as exc: + raise RuntimeError( + "block-major CoreX extension is unavailable") from exc + + self.extension = extension + self.gpu_cache = gpu_cache + self.device = gpu_cache[0].device + self.dtype = gpu_cache[0].dtype + self.num_gpu_blocks = gpu_cache[0].shape[1] + self.num_cpu_blocks = num_cpu_blocks + self.trace_enabled = block_major_cpu_kv_trace_enabled() + + self.cpu_pool = torch.zeros( + ( + num_cpu_blocks, + NUM_ATTENTION_LAYERS, + KV_PLANES, + ELEMENTS_PER_PLANE_BLOCK, + ), + dtype=self.dtype, + device="cpu", + pin_memory=True, + ) + if not self.cpu_pool.is_pinned(): + raise RuntimeError("block-major CPU pool is not pinned") + + # Preserve the public CacheEngine shape without allocating a second + # layer-major CPU cache. Transfer methods use cpu_pool directly. + self.layer_views = [ + self.cpu_pool[:, layer, :, :].permute(1, 0, 2) + for layer in range(NUM_ATTENTION_LAYERS) + ] + self.cpu_staging = [ + torch.empty( + ( + STAGING_BLOCKS, + NUM_ATTENTION_LAYERS, + KV_PLANES, + ELEMENTS_PER_PLANE_BLOCK, + ), + dtype=self.dtype, + device="cpu", + pin_memory=True, + ) + for _ in range(STAGING_BUFFER_COUNT) + ] + if not all(staging.is_pinned() for staging in self.cpu_staging): + raise RuntimeError("block-major CPU staging is not pinned") + + with torch.cuda.device(self.device): + self.gpu_staging = [ + torch.empty_like(staging, device=self.device) + for staging in self.cpu_staging + ] + self.events = [ + torch.cuda.Event(enable_timing=False) + for _ in range(STAGING_BUFFER_COUNT) + ] + self.error_flag = torch.zeros( + 1, dtype=torch.int32, device=self.device) + + logger.info( + "[BI100 BLOCK KV] enabled device=%s gpu_blocks=%d cpu_blocks=%d " + "layers=%d block_bytes=%d staging_blocks=%d staging_buffers=%d", + self.device, + self.num_gpu_blocks, + self.num_cpu_blocks, + NUM_ATTENTION_LAYERS, + BYTES_PER_BLOCK, + STAGING_BLOCKS, + STAGING_BUFFER_COUNT, + ) + + @staticmethod + def _validate_gpu_cache(gpu_cache: list[torch.Tensor]) -> None: + if len(gpu_cache) != NUM_ATTENTION_LAYERS: + raise RuntimeError( + f"{ENABLE_ENV}=1 requires exactly " + f"{NUM_ATTENTION_LAYERS} GPU attention caches, got " + f"{len(gpu_cache)}") + first = gpu_cache[0] + if first.device.type != "cuda": + raise RuntimeError("block-major GPU cache must be on CUDA") + if first.dtype != torch.float16: + raise RuntimeError("block-major GPU cache must use float16") + if (first.dim() != 3 or first.shape[0] != KV_PLANES + or first.shape[2] != ELEMENTS_PER_PLANE_BLOCK): + raise RuntimeError( + "block-major GPU cache must have shape [2, blocks, 4096]") + if not first.is_contiguous(): + raise RuntimeError("block-major GPU cache must be contiguous") + + for layer, tensor in enumerate(gpu_cache): + if tensor.device != first.device: + raise RuntimeError( + f"GPU cache layer {layer} is on a different device") + if tensor.dtype != first.dtype or tensor.shape != first.shape: + raise RuntimeError( + f"GPU cache layer {layer} has inconsistent geometry") + if not tensor.is_contiguous(): + raise RuntimeError( + f"GPU cache layer {layer} is not contiguous") + + def _to_gpu_ids(self, block_ids: torch.Tensor) -> torch.Tensor: + return block_ids.to( + device=self.device, + dtype=torch.int32, + non_blocking=False, + ) + + @staticmethod + def _chunks( + source: torch.Tensor, + destination: torch.Tensor, + gpu_ids: torch.Tensor, + ): + for start in range(0, source.numel(), STAGING_BLOCKS): + end = min(start + STAGING_BLOCKS, source.numel()) + yield ( + source[start:end], + destination[start:end], + gpu_ids[start:end], + end - start, + ) + + def _begin(self) -> None: + self.error_flag.zero_() + + def _finish( + self, + direction: str, + block_count: int, + started: float | None, + ) -> None: + # check_error performs the final stream synchronization. This also + # makes every staging slot safe to reuse in the next CacheEngine call. + self.extension.check_error(self.error_flag) + if started is not None: + elapsed_ms = (time.perf_counter() - started) * 1000.0 + logger.info( + "[BI100 BLOCK KV TRACE] direction=%s blocks=%d bytes=%d " + "elapsed_ms=%.3f", + direction, + block_count, + block_count * BYTES_PER_BLOCK, + elapsed_ms, + ) + + def swap_out(self, mapping: torch.Tensor) -> None: + started = time.perf_counter() if self.trace_enabled else None + source_gpu, destination_cpu = validate_block_mapping( + mapping, + source_limit=self.num_gpu_blocks, + destination_limit=self.num_cpu_blocks, + ) + block_count = source_gpu.numel() + if block_count == 0: + return + source_gpu_ids = self._to_gpu_ids(source_gpu) + + self._begin() + pending: tuple[int, torch.Tensor, int] | None = None + for index, (_, destination, gpu_ids, count) in enumerate( + self._chunks( + source_gpu, destination_cpu, source_gpu_ids)): + slot = index % STAGING_BUFFER_COUNT + self.extension.pack( + self.gpu_cache, + gpu_ids, + self.gpu_staging[slot], + self.error_flag, + count, + ) + self.cpu_staging[slot][:count].copy_( + self.gpu_staging[slot][:count], + non_blocking=True, + ) + self.events[slot].record() + if pending is not None: + pending_slot, pending_destination, pending_count = pending + self.events[pending_slot].synchronize() + self.extension.cpu_scatter( + self.cpu_staging[pending_slot], + self.cpu_pool, + pending_destination, + pending_count, + ) + pending = (slot, destination, count) + + if pending is not None: + pending_slot, pending_destination, pending_count = pending + self.events[pending_slot].synchronize() + self.extension.cpu_scatter( + self.cpu_staging[pending_slot], + self.cpu_pool, + pending_destination, + pending_count, + ) + self._finish("d2h", block_count, started) + + def swap_in(self, mapping: torch.Tensor) -> None: + started = time.perf_counter() if self.trace_enabled else None + source_cpu, destination_gpu = validate_block_mapping( + mapping, + source_limit=self.num_cpu_blocks, + destination_limit=self.num_gpu_blocks, + ) + block_count = source_cpu.numel() + if block_count == 0: + return + destination_gpu_ids = self._to_gpu_ids(destination_gpu) + + self._begin() + for index, (source, _, gpu_ids, count) in enumerate( + self._chunks( + source_cpu, destination_gpu, destination_gpu_ids)): + slot = index % STAGING_BUFFER_COUNT + if index >= STAGING_BUFFER_COUNT: + self.events[slot].synchronize() + self.extension.cpu_gather( + self.cpu_pool, + source, + self.cpu_staging[slot], + count, + ) + self.gpu_staging[slot][:count].copy_( + self.cpu_staging[slot][:count], + non_blocking=True, + ) + self.extension.scatter( + self.gpu_staging[slot], + gpu_ids, + self.gpu_cache, + self.error_flag, + count, + ) + self.events[slot].record() + self._finish("h2d", block_count, started) diff --git a/qwen3_6_scripts/build_corex_attn_head_rms_norm.sh b/qwen3_6_scripts/build_corex_attn_head_rms_norm.sh new file mode 100755 index 0000000..9802a2a --- /dev/null +++ b/qwen3_6_scripts/build_corex_attn_head_rms_norm.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_attn_head_rms_norm.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_attn_head_rms_norm.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" \ + --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check \ + -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_attn_head_rms_norm \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" \ + -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_attn_head_rms_norm.cu" \ + -L"${TORCH_ROOT}/lib" \ + -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" \ + -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart \ + -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX attention head RMSNorm extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_fused_paged_prefill_split4.sh b/qwen3_6_scripts/build_corex_fused_paged_prefill_split4.sh new file mode 100644 index 0000000..0ed3a63 --- /dev/null +++ b/qwen3_6_scripts/build_corex_fused_paged_prefill_split4.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_fused_paged_prefill_split4.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_fused_paged_prefill_split4.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_fused_paged_prefill \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_fused_paged_prefill_split4.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcublas -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX split4 fused paged-prefill extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_gdn_beta_decay.sh b/qwen3_6_scripts/build_corex_gdn_beta_decay.sh new file mode 100644 index 0000000..fc92dac --- /dev/null +++ b/qwen3_6_scripts/build_corex_gdn_beta_decay.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_gdn_beta_decay.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_gdn_beta_decay.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" \ + --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check \ + -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_gdn_beta_decay \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" \ + -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_gdn_beta_decay.cu" \ + -L"${TORCH_ROOT}/lib" \ + -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" \ + -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart \ + -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX GDN beta/decay extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_gdn_causal_conv.sh b/qwen3_6_scripts/build_corex_gdn_causal_conv.sh new file mode 100755 index 0000000..4a6eded --- /dev/null +++ b/qwen3_6_scripts/build_corex_gdn_causal_conv.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_gdn_causal_conv.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_gdn_causal_conv.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" \ + --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check \ + -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_gdn_causal_conv \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" \ + -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_gdn_causal_conv.cu" \ + -L"${TORCH_ROOT}/lib" \ + -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" \ + -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart \ + -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX GDN causal conv extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_gdn_gated_norm.sh b/qwen3_6_scripts/build_corex_gdn_gated_norm.sh new file mode 100755 index 0000000..aca1c2b --- /dev/null +++ b/qwen3_6_scripts/build_corex_gdn_gated_norm.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_gdn_gated_norm.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_gdn_gated_norm.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" \ + --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check \ + -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_gdn_gated_norm \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" \ + -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_gdn_gated_norm.cu" \ + -L"${TORCH_ROOT}/lib" \ + -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" \ + -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart \ + -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX GDN gated norm extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_gdn_packed_decode.sh b/qwen3_6_scripts/build_corex_gdn_packed_decode.sh new file mode 100755 index 0000000..f3f34dd --- /dev/null +++ b/qwen3_6_scripts/build_corex_gdn_packed_decode.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_gdn_packed_decode.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_gdn_packed_decode.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_gdn_packed_decode \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_gdn_packed_decode.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX GDN packed decode extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_gdn_qk_map.sh b/qwen3_6_scripts/build_corex_gdn_qk_map.sh new file mode 100644 index 0000000..8a971ec --- /dev/null +++ b/qwen3_6_scripts/build_corex_gdn_qk_map.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_gdn_qk_map.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_gdn_qk_map.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_gdn_qk_map \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_gdn_qk_map.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX GDN q/k map extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_moe_direct_routed.sh b/qwen3_6_scripts/build_corex_moe_direct_routed.sh new file mode 100755 index 0000000..c487c0b --- /dev/null +++ b/qwen3_6_scripts/build_corex_moe_direct_routed.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_moe_direct_routed.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_moe_direct_routed.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_moe_direct_routed \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_moe_direct_routed.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX direct routed-expert extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_moe_exact_reduce.sh b/qwen3_6_scripts/build_corex_moe_exact_reduce.sh new file mode 100755 index 0000000..55e4edf --- /dev/null +++ b/qwen3_6_scripts/build_corex_moe_exact_reduce.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_moe_exact_reduce.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_moe_exact_reduce.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_moe_exact_reduce \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_moe_exact_reduce.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX MoE exact reduce extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_moe_weight_gather.sh b/qwen3_6_scripts/build_corex_moe_weight_gather.sh new file mode 100755 index 0000000..f01b785 --- /dev/null +++ b/qwen3_6_scripts/build_corex_moe_weight_gather.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_moe_weight_gather.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_moe_weight_gather.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_moe_weight_gather \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_moe_weight_gather.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX MoE selected-weight gather extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/build_corex_paged_kv_gather.sh b/qwen3_6_scripts/build_corex_paged_kv_gather.sh new file mode 100644 index 0000000..09c8c88 --- /dev/null +++ b/qwen3_6_scripts/build_corex_paged_kv_gather.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: build_corex_paged_kv_gather.sh VLLM_ROOT} +COREX_ROOT=${COREX_ROOT:-/usr/local/corex-3.2.3} +TORCH_ROOT=${TORCH_ROOT:-${COREX_ROOT}/lib64/python3/dist-packages/torch} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT=${VLLM_ROOT}/corex_paged_kv_gather.so + +"${COREX_ROOT}/bin/clang++" \ + -std=c++17 -O3 -shared -fPIC \ + --cuda-path="${COREX_ROOT}" --cuda-gpu-arch=ivcore10 \ + --no-cuda-version-check -D_GLIBCXX_USE_CXX11_ABI=0 \ + -DTORCH_EXTENSION_NAME=corex_paged_kv_gather \ + -DTORCH_API_INCLUDE_EXTENSION_H \ + -I"${TORCH_ROOT}/include" \ + -I"${TORCH_ROOT}/include/torch/csrc/api/include" \ + -I"${TORCH_ROOT}/include/TH" -I"${TORCH_ROOT}/include/THC" \ + -I/usr/local/include/python3.10 \ + "${SCRIPT_DIR}/corex_paged_kv_gather.cu" \ + -L"${TORCH_ROOT}/lib" -L"${COREX_ROOT}/lib64" \ + -Wl,-rpath,"${TORCH_ROOT}/lib" -Wl,-rpath,"${COREX_ROOT}/lib64" \ + -ltorch_python -ltorch_cuda -ltorch_cpu -ltorch \ + -lc10_cuda -lc10 -lcudart -o "${OUTPUT}" + +test -s "${OUTPUT}" +printf '[ok] CoreX paged K/V gather extension %s\n' "${OUTPUT}" diff --git a/qwen3_6_scripts/chat_utils.py b/qwen3_6_scripts/chat_utils.py new file mode 100644 index 0000000..007c1e8 --- /dev/null +++ b/qwen3_6_scripts/chat_utils.py @@ -0,0 +1,617 @@ +import asyncio +import codecs +import json +from abc import ABC, abstractmethod +from collections import defaultdict +from functools import lru_cache, partial +from pathlib import Path +from typing import (Any, Awaitable, Dict, Generic, Iterable, List, Literal, + Mapping, Optional, Tuple, TypeVar, Union, cast) + +# yapf conflicts with isort for this block +# yapf: disable +from openai.types.chat import (ChatCompletionAssistantMessageParam, + ChatCompletionContentPartImageParam) +from openai.types.chat import ( + ChatCompletionContentPartParam as OpenAIChatCompletionContentPartParam) +from openai.types.chat import (ChatCompletionContentPartRefusalParam, + ChatCompletionContentPartTextParam) +from openai.types.chat import ( + ChatCompletionMessageParam as OpenAIChatCompletionMessageParam) +from openai.types.chat import (ChatCompletionMessageToolCallParam, + ChatCompletionToolMessageParam) +# yapf: enable +# pydantic needs the TypedDict from typing_extensions +from pydantic import ConfigDict +from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast +from typing_extensions import Required, TypeAlias, TypedDict + +from vllm.config import ModelConfig +from vllm.logger import init_logger +from vllm.multimodal import MultiModalDataDict +from vllm.multimodal.utils import (async_get_and_parse_audio, + async_get_and_parse_image, + get_and_parse_audio, get_and_parse_image) +from vllm.transformers_utils.tokenizer import AnyTokenizer, MistralTokenizer + +logger = init_logger(__name__) + + +class AudioURL(TypedDict, total=False): + url: Required[str] + """ + Either a URL of the audio or a data URL with base64 encoded audio data. + """ + + +class ChatCompletionContentPartAudioParam(TypedDict, total=False): + audio_url: Required[AudioURL] + + type: Required[Literal["audio_url"]] + """The type of the content part.""" + + +class CustomChatCompletionContentPartParam(TypedDict, total=False): + __pydantic_config__ = ConfigDict(extra="allow") # type: ignore + + type: Required[str] + """The type of the content part.""" + + +ChatCompletionContentPartParam: TypeAlias = Union[ + OpenAIChatCompletionContentPartParam, ChatCompletionContentPartAudioParam, + ChatCompletionContentPartRefusalParam, + CustomChatCompletionContentPartParam] + + +class CustomChatCompletionMessageParam(TypedDict, total=False): + """Enables custom roles in the Chat Completion API.""" + role: Required[str] + """The role of the message's author.""" + + content: Union[str, List[ChatCompletionContentPartParam]] + """The contents of the message.""" + + name: str + """An optional name for the participant. + + Provides the model information to differentiate between participants of the + same role. + """ + + tool_call_id: Optional[str] + """Tool call that this message is responding to.""" + + tool_calls: Optional[Iterable[ChatCompletionMessageToolCallParam]] + """The tool calls generated by the model, such as function calls.""" + + reasoning_content: Optional[str] + """Reasoning / thinking content for assistant messages (vLLM extension). + When present in a previous assistant turn, it is rendered as + ... before the main content so the model sees its own + chain-of-thought in subsequent turns.""" + + +ChatCompletionMessageParam = Union[OpenAIChatCompletionMessageParam, + CustomChatCompletionMessageParam] + + +# TODO: Make fields ReadOnly once mypy supports it +class ConversationMessage(TypedDict, total=False): + role: Required[str] + """The role of the message's author.""" + + content: Optional[str] + """The contents of the message""" + + tool_call_id: Optional[str] + """Tool call that this message is responding to.""" + + name: Optional[str] + """The name of the function to call""" + + tool_calls: Optional[Iterable[ChatCompletionMessageToolCallParam]] + """The tool calls generated by the model, such as function calls.""" + + reasoning_content: Optional[str] + """Reasoning / thinking content for assistant messages. + Passed directly to the chat template (Qwen3 reads message.reasoning_content + natively) instead of being manually wrapped in ....""" + + +ModalityStr = Literal["image", "audio", "video"] +_T = TypeVar("_T") + + +class BaseMultiModalItemTracker(ABC, Generic[_T]): + """ + Tracks multi-modal items in a given request and ensures that the number + of multi-modal items in a given request does not exceed the configured + maximum per prompt. + """ + + def __init__(self, model_config: ModelConfig, tokenizer: AnyTokenizer): + super().__init__() + + self._model_config = model_config + self._tokenizer = tokenizer + self._allowed_items = (model_config.multimodal_config.limit_per_prompt + if model_config.multimodal_config else {}) + self._consumed_items = {k: 0 for k in self._allowed_items} + + self._items: List[_T] = [] + + @staticmethod + @lru_cache(maxsize=None) + def _cached_token_str(tokenizer: AnyTokenizer, token_index: int) -> str: + return tokenizer.decode(token_index) + + def _placeholder_str(self, modality: ModalityStr, + current_count: int) -> Optional[str]: + # TODO: Let user specify how to insert image tokens into prompt + # (similar to chat template) + hf_config = self._model_config.hf_config + model_type = hf_config.model_type + + if modality == "image": + if model_type == "phi3_v": + # Workaround since this token is not defined in the tokenizer + return f"<|image_{current_count}|>" + if model_type == "minicpmv": + return "(./)" + if model_type in ("blip-2", "chatglm", "fuyu", "paligemma", + "pixtral"): + # These models do not use image tokens in the prompt + return None + if model_type == "qwen": + return f"Picture {current_count}: " + if model_type.startswith("llava"): + return self._cached_token_str(self._tokenizer, + hf_config.image_token_index) + if model_type in ("chameleon", "internvl_chat", "NVLM_D"): + return "" + if model_type == "mllama": + return "<|image|>" + if model_type in ("qwen2_vl", "qwen2_5_vl", "qwen3_5", + "qwen3_5_moe"): + return "<|vision_start|><|image_pad|><|vision_end|>" + if model_type == "molmo": + return "" + + raise TypeError(f"Unknown model type: {model_type}") + elif modality == "audio": + if model_type == "ultravox": + return "<|reserved_special_token_0|>" + raise TypeError(f"Unknown model type: {model_type}") + elif modality == "video": + if model_type in ("qwen2_vl","qwen2_5_vl"): + return "<|vision_start|><|video_pad|><|vision_end|>" + raise TypeError(f"Unknown model type: {model_type}") + else: + raise TypeError(f"Unknown modality: {modality}") + + @staticmethod + def _combine(items: List[MultiModalDataDict]) -> MultiModalDataDict: + mm_lists: Mapping[str, List[object]] = defaultdict(list) + + # Merge all the multi-modal items + for single_mm_data in items: + for mm_key, mm_item in single_mm_data.items(): + if isinstance(mm_item, list): + mm_lists[mm_key].extend(mm_item) + else: + mm_lists[mm_key].append(mm_item) + + # Unpack any single item lists for models that don't expect multiple. + return { + mm_key: mm_list[0] if len(mm_list) == 1 else mm_list + for mm_key, mm_list in mm_lists.items() + } + + def add(self, modality: ModalityStr, item: _T) -> Optional[str]: + """ + Add a multi-modal item to the current prompt and returns the + placeholder string to use, if any. + """ + allowed_count = self._allowed_items.get(modality, 1) + current_count = self._consumed_items.get(modality, 0) + 1 + if current_count > allowed_count: + raise ValueError( + f"At most {allowed_count} {modality}(s) may be provided in " + "one request.") + + self._consumed_items[modality] = current_count + self._items.append(item) + + return self._placeholder_str(modality, current_count) + + @abstractmethod + def create_parser(self) -> "BaseMultiModalContentParser": + raise NotImplementedError + + +class MultiModalItemTracker(BaseMultiModalItemTracker[MultiModalDataDict]): + + def all_mm_data(self) -> Optional[MultiModalDataDict]: + return self._combine(self._items) if self._items else None + + def create_parser(self) -> "BaseMultiModalContentParser": + return MultiModalContentParser(self) + + +class AsyncMultiModalItemTracker( + BaseMultiModalItemTracker[Awaitable[MultiModalDataDict]]): + + async def all_mm_data(self) -> Optional[MultiModalDataDict]: + if self._items: + items = await asyncio.gather(*self._items) + return self._combine(items) + + return None + + def create_parser(self) -> "BaseMultiModalContentParser": + return AsyncMultiModalContentParser(self) + + +class BaseMultiModalContentParser(ABC): + + def __init__(self) -> None: + super().__init__() + + # multimodal placeholder_string : count + self._placeholder_counts: Dict[str, int] = defaultdict(lambda: 0) + + def _add_placeholder(self, placeholder: Optional[str]): + if placeholder: + self._placeholder_counts[placeholder] += 1 + + def mm_placeholder_counts(self) -> Dict[str, int]: + return dict(self._placeholder_counts) + + @abstractmethod + def parse_image(self, image_url: str) -> None: + raise NotImplementedError + + @abstractmethod + def parse_audio(self, audio_url: str) -> None: + raise NotImplementedError + + +class MultiModalContentParser(BaseMultiModalContentParser): + + def __init__(self, tracker: MultiModalItemTracker) -> None: + super().__init__() + + self._tracker = tracker + + def parse_image(self, image_url: str) -> None: + image = get_and_parse_image(image_url) + + placeholder = self._tracker.add("image", image) + self._add_placeholder(placeholder) + + def parse_audio(self, audio_url: str) -> None: + audio = get_and_parse_audio(audio_url) + + placeholder = self._tracker.add("audio", audio) + self._add_placeholder(placeholder) + + +class AsyncMultiModalContentParser(BaseMultiModalContentParser): + + def __init__(self, tracker: AsyncMultiModalItemTracker) -> None: + super().__init__() + + self._tracker = tracker + + def parse_image(self, image_url: str) -> None: + image_coro = async_get_and_parse_image(image_url) + + placeholder = self._tracker.add("image", image_coro) + self._add_placeholder(placeholder) + + def parse_audio(self, audio_url: str) -> None: + audio_coro = async_get_and_parse_audio(audio_url) + + placeholder = self._tracker.add("audio", audio_coro) + self._add_placeholder(placeholder) + + +def validate_chat_template(chat_template: Optional[Union[Path, str]]): + """Raises if the provided chat template appears invalid.""" + if chat_template is None: + return + + elif isinstance(chat_template, Path) and not chat_template.exists(): + raise FileNotFoundError( + "the supplied chat template path doesn't exist") + + elif isinstance(chat_template, str): + JINJA_CHARS = "{}\n" + if not any(c in chat_template + for c in JINJA_CHARS) and not Path(chat_template).exists(): + raise ValueError( + f"The supplied chat template string ({chat_template}) " + f"appears path-like, but doesn't exist!") + + else: + raise TypeError( + f"{type(chat_template)} is not a valid chat template type") + + +def load_chat_template( + chat_template: Optional[Union[Path, str]]) -> Optional[str]: + if chat_template is None: + return None + try: + with open(chat_template, "r") as f: + resolved_chat_template = f.read() + except OSError as e: + if isinstance(chat_template, Path): + raise + + JINJA_CHARS = "{}\n" + if not any(c in chat_template for c in JINJA_CHARS): + msg = (f"The supplied chat template ({chat_template}) " + f"looks like a file path, but it failed to be " + f"opened. Reason: {e}") + raise ValueError(msg) from e + + # If opening a file fails, set chat template to be args to + # ensure we decode so our escape are interpreted correctly + resolved_chat_template = codecs.decode(chat_template, "unicode_escape") + + logger.info("Using supplied chat template:\n%s", resolved_chat_template) + return resolved_chat_template + + +# TODO: Let user specify how to insert multimodal tokens into prompt +# (similar to chat template) +def _get_full_multimodal_text_prompt(placeholder_counts: Dict[str, int], + text_prompt: str) -> str: + """Combine multimodal prompts for a multimodal language model.""" + + # Look through the text prompt to check for missing placeholders + missing_placeholders: List[str] = [] + for placeholder in placeholder_counts: + + # For any existing placeholder in the text prompt, we leave it as is + placeholder_counts[placeholder] -= text_prompt.count(placeholder) + + if placeholder_counts[placeholder] < 0: + raise ValueError( + f"Found more '{placeholder}' placeholders in input prompt than " + "actual multimodal data items.") + + missing_placeholders.extend([placeholder] * + placeholder_counts[placeholder]) + + # NOTE: For now we always add missing placeholders at the front of + # the prompt. This may change to be customizable in the future. + return "\n".join(missing_placeholders + [text_prompt]) + + +# No need to validate using Pydantic again +_TextParser = partial(cast, ChatCompletionContentPartTextParam) +_ImageParser = partial(cast, ChatCompletionContentPartImageParam) +_AudioParser = partial(cast, ChatCompletionContentPartAudioParam) +_RefusalParser = partial(cast, ChatCompletionContentPartRefusalParam) +MODEL_KEEP_MULTI_MODAL_CONTENT = {'mllama'} + + +def _parse_chat_message_content_parts( + role: str, + parts: Iterable[ChatCompletionContentPartParam], + mm_tracker: BaseMultiModalItemTracker, +) -> List[ConversationMessage]: + texts: List[str] = [] + + mm_parser = mm_tracker.create_parser() + keep_multimodal_content = \ + mm_tracker._model_config.hf_config.model_type in \ + MODEL_KEEP_MULTI_MODAL_CONTENT + + has_image = False + for part in parts: + part_type = part["type"] + if part_type == "text": + text = _TextParser(part)["text"] + texts.append(text) + elif part_type == "image_url": + image_url = _ImageParser(part)["image_url"] + + if image_url.get("detail", "auto") != "auto": + logger.warning( + "'image_url.detail' is currently not supported and " + "will be ignored.") + + mm_parser.parse_image(image_url["url"]) + has_image = True + elif part_type == "audio_url": + audio_url = _AudioParser(part)["audio_url"] + + mm_parser.parse_audio(audio_url["url"]) + elif part_type == "refusal": + text = _RefusalParser(part)["refusal"] + texts.append(text) + else: + raise NotImplementedError(f"Unknown part type: {part_type}") + + text_prompt = "\n".join(texts) + if keep_multimodal_content: + text_prompt = "\n".join(texts) + role_content = [{'type': 'text', 'text': text_prompt}] + + if has_image: + role_content = [{'type': 'image'}] + role_content + return [ConversationMessage(role=role, + content=role_content)] # type: ignore + else: + mm_placeholder_counts = mm_parser.mm_placeholder_counts() + if mm_placeholder_counts: + text_prompt = _get_full_multimodal_text_prompt( + mm_placeholder_counts, text_prompt) + return [ConversationMessage(role=role, content=text_prompt)] + + +# No need to validate using Pydantic again +_AssistantParser = partial(cast, ChatCompletionAssistantMessageParam) +_ToolParser = partial(cast, ChatCompletionToolMessageParam) + + +def _parse_chat_message_content( + message: ChatCompletionMessageParam, + mm_tracker: BaseMultiModalItemTracker, +) -> List[ConversationMessage]: + role = message["role"] + content = message.get("content") + + if content is None: + content = [] + elif isinstance(content, str): + content = [ + ChatCompletionContentPartTextParam(type="text", text=content) + ] + + result = _parse_chat_message_content_parts( + role, + content, # type: ignore + mm_tracker, + ) + + for result_msg in result: + if role == 'assistant': + parsed_msg = _AssistantParser(message) + + if "tool_calls" in parsed_msg: + result_msg["tool_calls"] = list(parsed_msg["tool_calls"]) + + # Pass reasoning content as a dedicated field so the chat template + # can render it natively (Qwen3: message.reasoning_content branch). + # Accept both "reasoning" (new vllm) and "reasoning_content" (ours). + reasoning = (message.get("reasoning") # type: ignore[arg-type] + or message.get("reasoning_content")) # type: ignore[arg-type] + if reasoning and isinstance(reasoning, str): + result_msg["reasoning_content"] = reasoning + + elif role == "tool": + parsed_msg = _ToolParser(message) + if "tool_call_id" in parsed_msg: + result_msg["tool_call_id"] = parsed_msg["tool_call_id"] + + if "name" in message and isinstance(message["name"], str): + result_msg["name"] = message["name"] + + return result + + +def _postprocess_messages(messages: List[ConversationMessage]) -> None: + # per the Transformers docs & maintainers, tool call arguments in + # assistant-role messages with tool_calls need to be dicts not JSON str - + # this is how tool-use chat templates will expect them moving forwards + # so, for messages that have tool_calls, parse the string (which we get + # from openAI format) to dict + for message in messages: + if (message["role"] == "assistant" and "tool_calls" in message + and message["tool_calls"] is not None): + if not isinstance(message["tool_calls"], list): + message["tool_calls"] = list(message["tool_calls"]) + + for item in message["tool_calls"]: + arguments = item["function"]["arguments"] + if isinstance(arguments, str): + try: + arguments = json.loads(arguments) + except json.JSONDecodeError as exc: + raise ValueError( + "Tool call arguments are not valid JSON.") from exc + elif not isinstance(arguments, dict): + raise TypeError( + "Tool call arguments must be a JSON object or a " + "JSON-encoded object string.") + if not isinstance(arguments, dict): + raise TypeError( + "Tool call arguments must decode to a JSON object.") + item["function"]["arguments"] = arguments + + +def parse_chat_messages( + messages: List[ChatCompletionMessageParam], + model_config: ModelConfig, + tokenizer: AnyTokenizer, +) -> Tuple[List[ConversationMessage], Optional[MultiModalDataDict]]: + conversation: List[ConversationMessage] = [] + mm_tracker = MultiModalItemTracker(model_config, tokenizer) + + for msg in messages: + sub_messages = _parse_chat_message_content(msg, mm_tracker) + + conversation.extend(sub_messages) + + _postprocess_messages(conversation) + + return conversation, mm_tracker.all_mm_data() + + +def parse_chat_messages_futures( + messages: List[ChatCompletionMessageParam], + model_config: ModelConfig, + tokenizer: AnyTokenizer, +) -> Tuple[List[ConversationMessage], Awaitable[Optional[MultiModalDataDict]]]: + conversation: List[ConversationMessage] = [] + mm_tracker = AsyncMultiModalItemTracker(model_config, tokenizer) + + for msg in messages: + sub_messages = _parse_chat_message_content(msg, mm_tracker) + + conversation.extend(sub_messages) + + _postprocess_messages(conversation) + + return conversation, mm_tracker.all_mm_data() + + +def apply_hf_chat_template( + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], + conversation: List[ConversationMessage], + chat_template: Optional[str], + *, + tokenize: bool = False, # Different from HF's default + **kwargs: Any, +) -> str: + if chat_template is None and tokenizer.chat_template is None: + raise ValueError( + "As of transformers v4.44, default chat template is no longer " + "allowed, so you must provide a chat template if the tokenizer " + "does not define one.") + + return tokenizer.apply_chat_template( + conversation=conversation, # type: ignore[arg-type] + chat_template=chat_template, + tokenize=tokenize, + **kwargs, + ) + + +def apply_mistral_chat_template( + tokenizer: MistralTokenizer, + messages: List[ChatCompletionMessageParam], + chat_template: Optional[str] = None, + **kwargs: Any, +) -> List[int]: + if chat_template is not None: + logger.warning( + "'chat_template' cannot be overridden for mistral tokenizer.") + if "add_generation_prompt" in kwargs: + logger.warning( + "'add_generation_prompt' is not supported for mistral tokenizer, " + "so it will be ignored.") + if "continue_final_message" in kwargs: + logger.warning( + "'continue_final_message' is not supported for mistral tokenizer, " + "so it will be ignored.") + + return tokenizer.apply_chat_template( + messages=messages, + **kwargs, + ) diff --git a/qwen3_6_scripts/cli_args.py b/qwen3_6_scripts/cli_args.py new file mode 100644 index 0000000..292b6da --- /dev/null +++ b/qwen3_6_scripts/cli_args.py @@ -0,0 +1,261 @@ +""" +This file contains the command line arguments for the vLLM's +OpenAI-compatible server. It is kept in a separate file for documentation +purposes. +""" + +import argparse +import json +import ssl +from typing import List, Optional, Sequence, Union + +from vllm.engine.arg_utils import AsyncEngineArgs, nullable_str +from vllm.entrypoints.chat_utils import validate_chat_template +from vllm.entrypoints.openai.serving_engine import (LoRAModulePath, + PromptAdapterPath) +from vllm.entrypoints.openai.tool_parsers import ToolParserManager +from vllm.utils import FlexibleArgumentParser + + +class LoRAParserAction(argparse.Action): + + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Optional[Union[str, Sequence[str]]], + option_string: Optional[str] = None, + ): + if values is None: + values = [] + if isinstance(values, str): + raise TypeError("Expected values to be a list") + + lora_list: List[LoRAModulePath] = [] + for item in values: + if item in [None, '']: # Skip if item is None or empty string + continue + if '=' in item and ',' not in item: # Old format: name=path + name, path = item.split('=') + lora_list.append(LoRAModulePath(name, path)) + else: # Assume JSON format + try: + lora_dict = json.loads(item) + lora = LoRAModulePath(**lora_dict) + lora_list.append(lora) + except json.JSONDecodeError: + parser.error( + f"Invalid JSON format for --lora-modules: {item}") + except TypeError as e: + parser.error( + f"Invalid fields for --lora-modules: {item} - {str(e)}" + ) + setattr(namespace, self.dest, lora_list) + + +class PromptAdapterParserAction(argparse.Action): + + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Optional[Union[str, Sequence[str]]], + option_string: Optional[str] = None, + ): + if values is None: + values = [] + if isinstance(values, str): + raise TypeError("Expected values to be a list") + + adapter_list: List[PromptAdapterPath] = [] + for item in values: + name, path = item.split('=') + adapter_list.append(PromptAdapterPath(name, path)) + setattr(namespace, self.dest, adapter_list) + + +def make_arg_parser(parser: FlexibleArgumentParser) -> FlexibleArgumentParser: + parser.add_argument("--host", + type=nullable_str, + default=None, + help="host name") + parser.add_argument("--port", type=int, default=8000, help="port number") + parser.add_argument( + "--uvicorn-log-level", + type=str, + default="info", + choices=['debug', 'info', 'warning', 'error', 'critical', 'trace'], + help="log level for uvicorn") + parser.add_argument("--allow-credentials", + action="store_true", + help="allow credentials") + parser.add_argument("--allowed-origins", + type=json.loads, + default=["*"], + help="allowed origins") + parser.add_argument("--allowed-methods", + type=json.loads, + default=["*"], + help="allowed methods") + parser.add_argument("--allowed-headers", + type=json.loads, + default=["*"], + help="allowed headers") + parser.add_argument("--api-key", + type=nullable_str, + default=None, + help="If provided, the server will require this key " + "to be presented in the header.") + parser.add_argument( + "--lora-modules", + type=nullable_str, + default=None, + nargs='+', + action=LoRAParserAction, + help="LoRA module configurations in either 'name=path' format" + "or JSON format. " + "Example (old format): 'name=path' " + "Example (new format): " + "'{\"name\": \"name\", \"local_path\": \"path\", " + "\"base_model_name\": \"id\"}'") + parser.add_argument( + "--prompt-adapters", + type=nullable_str, + default=None, + nargs='+', + action=PromptAdapterParserAction, + help="Prompt adapter configurations in the format name=path. " + "Multiple adapters can be specified.") + parser.add_argument("--chat-template", + type=nullable_str, + default=None, + help="The file path to the chat template, " + "or the template in single-line form " + "for the specified model") + parser.add_argument("--response-role", + type=nullable_str, + default="assistant", + help="The role name to return if " + "`request.add_generation_prompt=true`.") + parser.add_argument("--ssl-keyfile", + type=nullable_str, + default=None, + help="The file path to the SSL key file") + parser.add_argument("--ssl-certfile", + type=nullable_str, + default=None, + help="The file path to the SSL cert file") + parser.add_argument("--ssl-ca-certs", + type=nullable_str, + default=None, + help="The CA certificates file") + parser.add_argument( + "--ssl-cert-reqs", + type=int, + default=int(ssl.CERT_NONE), + help="Whether client certificate is required (see stdlib ssl module's)" + ) + parser.add_argument( + "--root-path", + type=nullable_str, + default=None, + help="FastAPI root_path when app is behind a path based routing proxy") + parser.add_argument( + "--middleware", + type=nullable_str, + action="append", + default=[], + help="Additional ASGI middleware to apply to the app. " + "We accept multiple --middleware arguments. " + "The value should be an import path. " + "If a function is provided, vLLM will add it to the server " + "using @app.middleware('http'). " + "If a class is provided, vLLM will add it to the server " + "using app.add_middleware(). ") + parser.add_argument( + "--return-tokens-as-token-ids", + action="store_true", + help="When --max-logprobs is specified, represents single tokens as " + "strings of the form 'token_id:{token_id}' so that tokens that " + "are not JSON-encodable can be identified.") + parser.add_argument( + "--disable-frontend-multiprocessing", + action="store_true", + help="If specified, will run the OpenAI frontend server in the same " + "process as the model serving engine.") + + parser.add_argument( + "--enable-auto-tool-choice", + action="store_true", + default=False, + help= + "Enable auto tool choice for supported models. Use --tool-call-parser" + "to specify which parser to use") + + valid_tool_parsers = ToolParserManager.tool_parsers.keys() + parser.add_argument( + "--tool-call-parser", + type=str, + metavar="{" + ",".join(valid_tool_parsers) + "} or name registered in " + "--tool-parser-plugin", + default=None, + help= + "Select the tool call parser depending on the model that you're using." + " This is used to parse the model-generated tool call into OpenAI API " + "format. Required for --enable-auto-tool-choice.") + + parser.add_argument( + "--tool-parser-plugin", + type=str, + default="", + help= + "Special the tool parser plugin write to parse the model-generated tool" + " into OpenAI API format, the name register in this plugin can be used " + "in --tool-call-parser.") + + parser.add_argument( + "--reasoning-parser", + type=str, + default=None, + help= + "Select the reasoning parser to split ... content into " + "reasoning_content vs content in the response. " + "Supported: qwen3") + + parser = AsyncEngineArgs.add_cli_args(parser) + + parser.add_argument('--max-log-len', + type=int, + default=None, + help='Max number of prompt characters or prompt ' + 'ID numbers being printed in log.' + '\n\nDefault: Unlimited') + + parser.add_argument( + "--disable-fastapi-docs", + action='store_true', + default=False, + help="Disable FastAPI's OpenAPI schema, Swagger UI, and ReDoc endpoint" + ) + + return parser + + +def validate_parsed_serve_args(args: argparse.Namespace): + """Quick checks for model serve args that raise prior to loading.""" + if hasattr(args, "subparser") and args.subparser != "serve": + return + + # Ensure that the chat template is valid; raises if it likely isn't + validate_chat_template(args.chat_template) + + # Enable auto tool needs a tool call parser to be valid + if args.enable_auto_tool_choice and not args.tool_call_parser: + raise TypeError("Error: --enable-auto-tool-choice requires " + "--tool-call-parser") + + +def create_parser_for_docs() -> FlexibleArgumentParser: + parser_for_docs = FlexibleArgumentParser( + prog="-m vllm.entrypoints.openai.api_server") + return make_arg_parser(parser_for_docs) diff --git a/qwen3_6_scripts/corex_attn_head_rms_norm.cu b/qwen3_6_scripts/corex_attn_head_rms_norm.cu new file mode 100644 index 0000000..6021d1d --- /dev/null +++ b/qwen3_6_scripts/corex_attn_head_rms_norm.cu @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +#include + +namespace { + +constexpr int kHeadDim = 256; +constexpr int kThreads = 256; + +void check_half_matrix(const torch::Tensor& input, const char* name) { + TORCH_CHECK(input.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(input.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(input.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(input.dim() == 2 && input.size(1) == kHeadDim, + name, " must have shape (rows, 256)"); +} + +__global__ void prepare_kernel(const __half* input, float* converted, + float* squares, int rows) { + const int row = blockIdx.x; + const int column = threadIdx.x; + if (row >= rows || column >= kHeadDim) { + return; + } + const int offset = row * kHeadDim + column; + const float value = __half2float(input[offset]); + converted[offset] = value; + squares[offset] = __fmul_rn(value, value); +} + +__global__ void apply_inverse_kernel( + const float* input, const __half* weight, const float* inverse, + __half* output, int rows) { + const int row = blockIdx.x; + const int column = threadIdx.x; + if (row >= rows || column >= kHeadDim) { + return; + } + const int offset = row * kHeadDim + column; + const float scaled = __fmul_rn(input[offset], inverse[row]); + const float factor = __fadd_rn(1.0f, __half2float(weight[column])); + output[offset] = __float2half_rn(__fmul_rn(scaled, factor)); +} + +} // namespace + +std::vector prepare(const torch::Tensor& input) { + check_half_matrix(input, "input"); + auto float_options = input.options().dtype(torch::kFloat32); + auto converted = torch::empty(input.sizes(), float_options); + auto squares = torch::empty(input.sizes(), float_options); + const int rows = static_cast(input.size(0)); + prepare_kernel<<>>( + reinterpret_cast(input.data_ptr()), + converted.data_ptr(), squares.data_ptr(), rows); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return {converted, squares}; +} + +torch::Tensor apply_inverse(const torch::Tensor& input, + const torch::Tensor& weight, + const torch::Tensor& inverse) { + TORCH_CHECK(input.is_cuda() && weight.is_cuda() && inverse.is_cuda(), + "all tensors must be CUDA tensors"); + TORCH_CHECK(input.scalar_type() == torch::kFloat32, + "input must have dtype float32"); + TORCH_CHECK(weight.scalar_type() == torch::kFloat16, + "weight must have dtype float16"); + TORCH_CHECK(inverse.scalar_type() == torch::kFloat32, + "inverse must have dtype float32"); + TORCH_CHECK(input.is_contiguous() && weight.is_contiguous() + && inverse.is_contiguous(), + "all tensors must be contiguous"); + TORCH_CHECK(input.dim() == 2 && input.size(1) == kHeadDim, + "input must have shape (rows, 256)"); + TORCH_CHECK(weight.dim() == 1 && weight.size(0) == kHeadDim, + "weight must have shape (256,)"); + TORCH_CHECK(inverse.numel() == input.size(0), + "inverse must contain one value per row"); + auto output = torch::empty( + input.sizes(), input.options().dtype(torch::kFloat16)); + const int rows = static_cast(input.size(0)); + apply_inverse_kernel<<>>( + input.data_ptr(), + reinterpret_cast(weight.data_ptr()), + inverse.data_ptr(), + reinterpret_cast<__half*>(output.data_ptr()), rows); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("prepare", &prepare, + "Convert FP16 attention heads and compute exact squares"); + module.def("apply_inverse", &apply_inverse, + "Apply PyTorch-computed attention head RMSNorm inverse"); +} diff --git a/qwen3_6_scripts/corex_block_major_kv_transfer.cu b/qwen3_6_scripts/corex_block_major_kv_transfer.cu new file mode 100644 index 0000000..cac1f1c --- /dev/null +++ b/qwen3_6_scripts/corex_block_major_kv_transfer.cu @@ -0,0 +1,402 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +constexpr int kAttentionLayers = 10; +constexpr int kKvPlanes = 2; +constexpr int kElementsPerPlaneBlock = 4096; +constexpr int kElementsPerVector = 8; +constexpr int kVectorsPerPlaneBlock = + kElementsPerPlaneBlock / kElementsPerVector; +constexpr int kVectorsPerBlockMajorRow = + kAttentionLayers * kKvPlanes * kVectorsPerPlaneBlock; +constexpr int kThreads = 256; +constexpr int kMaxGridBlocks = 65535; + +using PackedVector = uint4; + +__device__ __forceinline__ const PackedVector* select_const_layer( + int layer, const PackedVector* layer0, const PackedVector* layer1, + const PackedVector* layer2, const PackedVector* layer3, + const PackedVector* layer4, const PackedVector* layer5, + const PackedVector* layer6, const PackedVector* layer7, + const PackedVector* layer8, const PackedVector* layer9) { + switch (layer) { + case 0: + return layer0; + case 1: + return layer1; + case 2: + return layer2; + case 3: + return layer3; + case 4: + return layer4; + case 5: + return layer5; + case 6: + return layer6; + case 7: + return layer7; + case 8: + return layer8; + default: + return layer9; + } +} + +__device__ __forceinline__ PackedVector* select_mutable_layer( + int layer, PackedVector* layer0, PackedVector* layer1, + PackedVector* layer2, PackedVector* layer3, PackedVector* layer4, + PackedVector* layer5, PackedVector* layer6, PackedVector* layer7, + PackedVector* layer8, PackedVector* layer9) { + switch (layer) { + case 0: + return layer0; + case 1: + return layer1; + case 2: + return layer2; + case 3: + return layer3; + case 4: + return layer4; + case 5: + return layer5; + case 6: + return layer6; + case 7: + return layer7; + case 8: + return layer8; + default: + return layer9; + } +} + +__global__ void pack_block_major_kernel( + const PackedVector* layer0, const PackedVector* layer1, + const PackedVector* layer2, const PackedVector* layer3, + const PackedVector* layer4, const PackedVector* layer5, + const PackedVector* layer6, const PackedVector* layer7, + const PackedVector* layer8, const PackedVector* layer9, + const int* source_blocks, PackedVector* staging, int* error_flag, + int count, int gpu_blocks) { + const int64_t total = + static_cast(count) * kVectorsPerBlockMajorRow; + for (int64_t linear = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + linear < total; + linear += static_cast(blockDim.x) * gridDim.x) { + int64_t cursor = linear; + const int feature_vector = cursor % kVectorsPerPlaneBlock; + cursor /= kVectorsPerPlaneBlock; + const int kv_plane = cursor % kKvPlanes; + cursor /= kKvPlanes; + const int layer = cursor % kAttentionLayers; + const int row = cursor / kAttentionLayers; + const int source_block = source_blocks[row]; + if (static_cast(source_block) >= + static_cast(gpu_blocks)) { + atomicExch(error_flag, 1); + continue; + } + const PackedVector* source = select_const_layer( + layer, layer0, layer1, layer2, layer3, layer4, layer5, layer6, + layer7, layer8, layer9); + const int64_t source_index = + ((static_cast(kv_plane) * gpu_blocks + source_block) + * kVectorsPerPlaneBlock) + + feature_vector; + staging[linear] = source[source_index]; + } +} + +__global__ void scatter_block_major_kernel( + const PackedVector* staging, const int* destination_blocks, + PackedVector* layer0, PackedVector* layer1, PackedVector* layer2, + PackedVector* layer3, PackedVector* layer4, PackedVector* layer5, + PackedVector* layer6, PackedVector* layer7, PackedVector* layer8, + PackedVector* layer9, int* error_flag, int count, int gpu_blocks) { + const int64_t total = + static_cast(count) * kVectorsPerBlockMajorRow; + for (int64_t linear = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + linear < total; + linear += static_cast(blockDim.x) * gridDim.x) { + int64_t cursor = linear; + const int feature_vector = cursor % kVectorsPerPlaneBlock; + cursor /= kVectorsPerPlaneBlock; + const int kv_plane = cursor % kKvPlanes; + cursor /= kKvPlanes; + const int layer = cursor % kAttentionLayers; + const int row = cursor / kAttentionLayers; + const int destination_block = destination_blocks[row]; + if (static_cast(destination_block) >= + static_cast(gpu_blocks)) { + atomicExch(error_flag, 1); + continue; + } + PackedVector* destination = select_mutable_layer( + layer, layer0, layer1, layer2, layer3, layer4, layer5, layer6, + layer7, layer8, layer9); + const int64_t destination_index = + ((static_cast(kv_plane) * gpu_blocks + destination_block) + * kVectorsPerPlaneBlock) + + feature_vector; + destination[destination_index] = staging[linear]; + } +} + +void check_gpu_layers(const std::vector& layers) { + TORCH_CHECK(layers.size() == kAttentionLayers, "expected exactly ", + kAttentionLayers, " GPU attention-layer tensors"); + const auto device = layers.front().device(); + const int64_t blocks = layers.front().size(1); + for (int layer = 0; layer < kAttentionLayers; ++layer) { + const auto& tensor = layers[layer]; + TORCH_CHECK(tensor.is_cuda(), "GPU layer ", layer, + " must be a CUDA tensor"); + TORCH_CHECK(tensor.device() == device, "GPU layer ", layer, + " is on a different device"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, "GPU layer ", + layer, " must use float16"); + TORCH_CHECK(tensor.is_contiguous(), "GPU layer ", layer, + " must be contiguous"); + TORCH_CHECK(tensor.dim() == 3 && tensor.size(0) == kKvPlanes && + tensor.size(1) == blocks && + tensor.size(2) == kElementsPerPlaneBlock, + "GPU layer ", layer, " must have shape [2, blocks, 4096]"); + TORCH_CHECK( + reinterpret_cast(tensor.data_ptr()) % + alignof(PackedVector) == + 0, + "GPU layer ", layer, " is not 16-byte aligned"); + } +} + +void check_gpu_transfer_args(const std::vector& layers, + const torch::Tensor& block_ids, + const torch::Tensor& staging, + const torch::Tensor& error_flag, + int64_t count) { + check_gpu_layers(layers); + TORCH_CHECK(block_ids.is_cuda(), "block_ids must be a CUDA tensor"); + TORCH_CHECK(block_ids.device() == layers.front().device(), + "block_ids must be on the cache device"); + TORCH_CHECK(block_ids.scalar_type() == torch::kInt32, + "block_ids must use int32"); + TORCH_CHECK(block_ids.dim() == 1 && block_ids.is_contiguous(), + "block_ids must be a contiguous one-dimensional tensor"); + TORCH_CHECK(count > 0 && count <= block_ids.numel(), + "count must be in [1, block_ids.numel()]"); + TORCH_CHECK(staging.is_cuda(), "staging must be a CUDA tensor"); + TORCH_CHECK(staging.device() == layers.front().device(), + "staging must be on the cache device"); + TORCH_CHECK(staging.scalar_type() == torch::kFloat16, + "staging must use float16"); + TORCH_CHECK(staging.is_contiguous(), "staging must be contiguous"); + TORCH_CHECK( + staging.dim() == 4 && staging.size(0) >= count && + staging.size(1) == kAttentionLayers && + staging.size(2) == kKvPlanes && + staging.size(3) == kElementsPerPlaneBlock, + "staging must have shape [capacity>=count, 10, 2, 4096]"); + TORCH_CHECK( + reinterpret_cast(staging.data_ptr()) % + alignof(PackedVector) == + 0, + "staging is not 16-byte aligned"); + TORCH_CHECK(error_flag.is_cuda(), + "error_flag must be a CUDA tensor"); + TORCH_CHECK(error_flag.device() == layers.front().device(), + "error_flag must be on the cache device"); + TORCH_CHECK(error_flag.scalar_type() == torch::kInt32, + "error_flag must use int32"); + TORCH_CHECK(error_flag.is_contiguous() && error_flag.numel() == 1, + "error_flag must be one contiguous int32 value"); +} + +int launch_blocks(int64_t count) { + const int64_t total = count * kVectorsPerBlockMajorRow; + return static_cast(std::min( + (total + kThreads - 1) / kThreads, kMaxGridBlocks)); +} + +void pack_block_major(const std::vector& layers, + const torch::Tensor& source_blocks, + torch::Tensor staging, torch::Tensor error_flag, + int64_t count) { + check_gpu_transfer_args( + layers, source_blocks, staging, error_flag, count); + const int blocks = static_cast(layers.front().size(1)); + pack_block_major_kernel<<>>( + reinterpret_cast( + layers[0].data_ptr()), + reinterpret_cast( + layers[1].data_ptr()), + reinterpret_cast( + layers[2].data_ptr()), + reinterpret_cast( + layers[3].data_ptr()), + reinterpret_cast( + layers[4].data_ptr()), + reinterpret_cast( + layers[5].data_ptr()), + reinterpret_cast( + layers[6].data_ptr()), + reinterpret_cast( + layers[7].data_ptr()), + reinterpret_cast( + layers[8].data_ptr()), + reinterpret_cast( + layers[9].data_ptr()), + source_blocks.data_ptr(), + reinterpret_cast(staging.data_ptr()), + error_flag.data_ptr(), static_cast(count), blocks); + C10_CUDA_KERNEL_LAUNCH_CHECK(); +} + +void scatter_block_major(const torch::Tensor& staging, + const torch::Tensor& destination_blocks, + const std::vector& layers, + torch::Tensor error_flag, + int64_t count) { + check_gpu_transfer_args( + layers, destination_blocks, staging, error_flag, count); + const int blocks = static_cast(layers.front().size(1)); + scatter_block_major_kernel<<>>( + reinterpret_cast( + staging.data_ptr()), + destination_blocks.data_ptr(), + reinterpret_cast(layers[0].data_ptr()), + reinterpret_cast(layers[1].data_ptr()), + reinterpret_cast(layers[2].data_ptr()), + reinterpret_cast(layers[3].data_ptr()), + reinterpret_cast(layers[4].data_ptr()), + reinterpret_cast(layers[5].data_ptr()), + reinterpret_cast(layers[6].data_ptr()), + reinterpret_cast(layers[7].data_ptr()), + reinterpret_cast(layers[8].data_ptr()), + reinterpret_cast(layers[9].data_ptr()), + error_flag.data_ptr(), static_cast(count), blocks); + C10_CUDA_KERNEL_LAUNCH_CHECK(); +} + +void check_transfer_error(const torch::Tensor& error_flag) { + TORCH_CHECK(error_flag.is_cuda(), + "error_flag must be a CUDA tensor"); + TORCH_CHECK(error_flag.scalar_type() == torch::kInt32, + "error_flag must use int32"); + TORCH_CHECK(error_flag.is_contiguous() && error_flag.numel() == 1, + "error_flag must be one contiguous int32 value"); + TORCH_CHECK(error_flag.item() == 0, + "GPU block mapping contains an out-of-range id"); +} + +void check_cpu_transfer_args(const torch::Tensor& pool, + const torch::Tensor& block_ids, + const torch::Tensor& staging, int64_t count) { + TORCH_CHECK(!pool.is_cuda() && !staging.is_cuda() && + !block_ids.is_cuda(), + "CPU gather/scatter tensors must be on CPU"); + TORCH_CHECK(pool.scalar_type() == torch::kFloat16 && + staging.scalar_type() == torch::kFloat16, + "CPU pool and staging must use float16"); + TORCH_CHECK(pool.is_contiguous() && staging.is_contiguous(), + "CPU pool and staging must be contiguous"); + TORCH_CHECK( + pool.dim() == 4 && pool.size(1) == kAttentionLayers && + pool.size(2) == kKvPlanes && + pool.size(3) == kElementsPerPlaneBlock, + "CPU pool must have shape [slots, 10, 2, 4096]"); + TORCH_CHECK( + staging.dim() == 4 && staging.size(0) >= count && + staging.size(1) == kAttentionLayers && + staging.size(2) == kKvPlanes && + staging.size(3) == kElementsPerPlaneBlock, + "CPU staging must have shape [capacity>=count, 10, 2, 4096]"); + TORCH_CHECK(block_ids.scalar_type() == torch::kInt64, + "CPU block_ids must use int64"); + TORCH_CHECK(block_ids.dim() == 1 && block_ids.is_contiguous(), + "CPU block_ids must be contiguous and one-dimensional"); + TORCH_CHECK(count > 0 && count <= block_ids.numel(), + "count must be in [1, block_ids.numel()]"); + + const int64_t* ids = block_ids.data_ptr(); + for (int64_t row = 0; row < count; ++row) { + TORCH_CHECK(ids[row] >= 0 && ids[row] < pool.size(0), + "CPU block id out of range at row ", row, ": ", ids[row]); + } +} + +void cpu_gather_rows(const torch::Tensor& pool, + const torch::Tensor& source_blocks, + torch::Tensor staging, int64_t count) { + check_cpu_transfer_args(pool, source_blocks, staging, count); + const int64_t row_elements = + kAttentionLayers * kKvPlanes * kElementsPerPlaneBlock; + const size_t row_bytes = + static_cast(row_elements) * sizeof(at::Half); + const char* source = reinterpret_cast( + pool.data_ptr()); + char* destination = + reinterpret_cast(staging.data_ptr()); + const int64_t* ids = source_blocks.data_ptr(); + at::parallel_for(0, count, 8, [&](int64_t begin, int64_t end) { + for (int64_t row = begin; row < end; ++row) { + std::memcpy(destination + row * row_bytes, + source + ids[row] * row_bytes, row_bytes); + } + }); +} + +void cpu_scatter_rows(const torch::Tensor& staging, + torch::Tensor pool, + const torch::Tensor& destination_blocks, + int64_t count) { + check_cpu_transfer_args(pool, destination_blocks, staging, count); + const int64_t row_elements = + kAttentionLayers * kKvPlanes * kElementsPerPlaneBlock; + const size_t row_bytes = + static_cast(row_elements) * sizeof(at::Half); + const char* source = reinterpret_cast( + staging.data_ptr()); + char* destination = + reinterpret_cast(pool.data_ptr()); + const int64_t* ids = destination_blocks.data_ptr(); + at::parallel_for(0, count, 8, [&](int64_t begin, int64_t end) { + for (int64_t row = begin; row < end; ++row) { + std::memcpy(destination + ids[row] * row_bytes, + source + row * row_bytes, row_bytes); + } + }); +} + +} // namespace + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("pack", &pack_block_major, + "Pack ten layer-major FP16 KV caches into block-major staging"); + module.def("scatter", &scatter_block_major, + "Scatter block-major FP16 staging into ten layer-major caches"); + module.def("check_error", &check_transfer_error, + "Fail fast after a bounds-safe asynchronous transfer"); + module.def("cpu_gather", &cpu_gather_rows, + "Gather block-major CPU pool rows into bounded staging"); + module.def("cpu_scatter", &cpu_scatter_rows, + "Scatter bounded staging rows into the block-major CPU pool"); +} diff --git a/qwen3_6_scripts/corex_fused_paged_prefill_split4.cu b/qwen3_6_scripts/corex_fused_paged_prefill_split4.cu new file mode 100644 index 0000000..bbd1250 --- /dev/null +++ b/qwen3_6_scripts/corex_fused_paged_prefill_split4.cu @@ -0,0 +1,494 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace { + +constexpr int kBlockSize = 16; +constexpr int kHeadDim = 256; +constexpr int kKeyPack = 8; +constexpr int kNumQueryHeads = 4; +constexpr int kNumKvHeads = 1; +constexpr int kTileTokens = 512; +constexpr int kSplitCount = 4; +constexpr int kGroupTokens = kSplitCount * kTileTokens; +constexpr int kThreads = 256; +constexpr int kMaxQueryTokens = 8192; +constexpr int kMaxSequenceTokens = 262144; + +void check_half_cuda_contiguous(const torch::Tensor& tensor, + const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); +} + +__global__ void convert_query_kernel(const __half* query, float* converted, + int query_len, float scale) { + const int64_t total = static_cast(query_len) + * kNumQueryHeads * kHeadDim; + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < total; + index += static_cast(blockDim.x) * gridDim.x) { + const int dim = index % kHeadDim; + const int query_index = + (index / kHeadDim) % query_len; + const int head = + index / (static_cast(kHeadDim) * query_len); + const int64_t source = + (static_cast(query_index) * kNumQueryHeads + head) + * kHeadDim + dim; + converted[index] = __half2float(query[source]) * scale; + } +} + +__global__ void gather_kv_group_kernel( + const __half* key_new, const __half* value_new, + const __half* key_cache, const __half* value_cache, + const int* block_table, float* key_tiles, float* value_tiles, + int context_len, int query_len, int group_start, int group_tokens, + int active_splits) { + constexpr int kElements = kTileTokens * kHeadDim; + const int64_t total = static_cast(active_splits) * kElements; + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < total; + index += static_cast(blockDim.x) * gridDim.x) { + const int split = index / kElements; + const int element = index - static_cast(split) * kElements; + const int token_offset = element / kHeadDim; + const int dim = element - token_offset * kHeadDim; + const int remaining_tokens = group_tokens - split * kTileTokens; + const int split_tokens = + remaining_tokens < kTileTokens ? remaining_tokens : kTileTokens; + const int logical_token = + group_start + split * kTileTokens + token_offset; + float key_value = 0.0f; + float value_value = 0.0f; + if (token_offset >= split_tokens) { + // The fixed 512-column GEMMs require zero-filled tail columns. + } else if (logical_token < context_len) { + const int logical_block = logical_token / kBlockSize; + const int block_offset = logical_token % kBlockSize; + const int physical_block = block_table[logical_block]; + const int64_t key_index = + (((static_cast(physical_block) * kNumKvHeads) + * (kHeadDim / kKeyPack) + dim / kKeyPack) + * kBlockSize + block_offset) * kKeyPack + dim % kKeyPack; + const int64_t value_index = + ((static_cast(physical_block) * kNumKvHeads) + * kHeadDim + dim) * kBlockSize + block_offset; + key_value = __half2float(key_cache[key_index]); + value_value = __half2float(value_cache[value_index]); + } else if (logical_token < context_len + query_len) { + const int query_index = logical_token - context_len; + const int64_t source = + static_cast(query_index) * kHeadDim + dim; + key_value = __half2float(key_new[source]); + value_value = __half2float(value_new[source]); + } + key_tiles[index] = key_value; + value_tiles[index] = value_value; + } +} + +__global__ void mask_group_scores_kernel( + float* scores, int query_len, int context_len, + int group_start, int group_tokens, int active_splits, + int rows, bool causal) { + const int64_t split_elements = + static_cast(rows) * kTileTokens; + const int64_t elements = active_splits * split_elements; + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < elements; + index += static_cast(blockDim.x) * gridDim.x) { + const int split = index / split_elements; + const int split_index = index - split * split_elements; + const int column = split_index % kTileTokens; + const int row = split_index / kTileTokens; + const int query_index = row % query_len; + const int remaining_tokens = group_tokens - split * kTileTokens; + const int split_tokens = + remaining_tokens < kTileTokens ? remaining_tokens : kTileTokens; + const int logical_token = + group_start + split * kTileTokens + column; + if (column >= split_tokens + || (causal && logical_token > context_len + query_index)) { + scores[index] = -std::numeric_limits::infinity(); + } + } +} + +__global__ void normalize_split_scores_kernel( + float* scores, float* corrections, float* running_max, + float* running_sum, int active_splits, int rows) { + const int row = blockIdx.x; + if (row >= rows) { + return; + } + + __shared__ float reduction[kThreads]; + __shared__ float state_max; + __shared__ float state_sum; + __shared__ float next_max; + __shared__ float correction; + + if (threadIdx.x == 0) { + state_max = running_max[row]; + state_sum = running_sum[row]; + } + __syncthreads(); + + for (int split = 0; split < active_splits; ++split) { + float* row_scores = + scores + (static_cast(split) * rows + row) * kTileTokens; + float local_max = -std::numeric_limits::infinity(); + for (int column = threadIdx.x; column < kTileTokens; + column += blockDim.x) { + local_max = fmaxf(local_max, row_scores[column]); + } + reduction[threadIdx.x] = local_max; + __syncthreads(); + for (int stride = kThreads / 2; stride > 0; stride /= 2) { + if (threadIdx.x < stride) { + reduction[threadIdx.x] = fmaxf( + reduction[threadIdx.x], reduction[threadIdx.x + stride]); + } + __syncthreads(); + } + if (threadIdx.x == 0) { + next_max = fmaxf(state_max, reduction[0]); + correction = + (state_max == -std::numeric_limits::infinity() + && next_max == -std::numeric_limits::infinity()) + ? 1.0f + : expf(state_max - next_max); + corrections[static_cast(split) * rows + row] = correction; + } + __syncthreads(); + + float local_sum = 0.0f; + for (int column = threadIdx.x; column < kTileTokens; + column += blockDim.x) { + const float score = row_scores[column]; + const float probability = + (score == -std::numeric_limits::infinity() + && next_max == -std::numeric_limits::infinity()) + ? 0.0f + : expf(score - next_max); + row_scores[column] = probability; + local_sum = __fadd_rn(local_sum, probability); + } + reduction[threadIdx.x] = local_sum; + __syncthreads(); + for (int stride = kThreads / 2; stride > 0; stride /= 2) { + if (threadIdx.x < stride) { + reduction[threadIdx.x] = __fadd_rn( + reduction[threadIdx.x], reduction[threadIdx.x + stride]); + } + __syncthreads(); + } + if (threadIdx.x == 0) { + state_sum = __fadd_rn( + __fmul_rn(state_sum, correction), reduction[0]); + state_max = next_max; + } + __syncthreads(); + } + + if (threadIdx.x == 0) { + running_max[row] = state_max; + running_sum[row] = state_sum; + } +} + +__global__ void merge_split_output_kernel( + float* running_output, const float* split_output, + const float* corrections, int active_splits, + int rows, int64_t output_elements) { + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < output_elements; + index += static_cast(blockDim.x) * gridDim.x) { + const int row = index / kHeadDim; + float value = running_output[index]; + for (int split = 0; split < active_splits; ++split) { + const int64_t row_index = + static_cast(split) * rows + row; + const int64_t output_index = + static_cast(split) * output_elements + index; + value = __fadd_rn( + __fmul_rn(value, corrections[row_index]), + split_output[output_index]); + } + running_output[index] = value; + } +} + +__global__ void accumulate_output_kernel( + float* running_output, const float* tile_output, + const float* correction, int64_t elements) { + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < elements; + index += static_cast(blockDim.x) * gridDim.x) { + const int row = index / kHeadDim; + const float scaled = + __fmul_rn(running_output[index], correction[row]); + running_output[index] = __fadd_rn(scaled, tile_output[index]); + } +} + +int launch_blocks(int64_t elements) { + const int64_t needed = (elements + kThreads - 1) / kThreads; + return static_cast(std::min(needed, 65535)); +} + +void check_cublas(cublasStatus_t status, const char* operation) { + TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS, operation, + " failed with cuBLAS status ", static_cast(status)); +} + +cublasStatus_t qk_batched( + cublasHandle_t handle, const float* key_tile, const float* query, + float* scores, int query_len) { + const float alpha = 1.0f; + const float beta = 0.0f; + return cublasSgemmStridedBatched( + handle, CUBLAS_OP_T, CUBLAS_OP_N, + kTileTokens, query_len, kHeadDim, + &alpha, key_tile, kHeadDim, 0, + query, kHeadDim, static_cast(query_len) * kHeadDim, + &beta, scores, kTileTokens, + static_cast(query_len) * kTileTokens, + kNumQueryHeads); +} + +cublasStatus_t pv_batched( + cublasHandle_t handle, const float* value_tile, const float* scores, + float* output, int query_len) { + const float alpha = 1.0f; + const float beta = 0.0f; + return cublasSgemmStridedBatched( + handle, CUBLAS_OP_N, CUBLAS_OP_N, + kHeadDim, query_len, kTileTokens, + &alpha, value_tile, kHeadDim, 0, + scores, kTileTokens, + static_cast(query_len) * kTileTokens, + &beta, output, kHeadDim, + static_cast(query_len) * kHeadDim, + kNumQueryHeads); +} + +} // namespace + +std::vector fused_paged_prefill_forward( + const torch::Tensor& query, const torch::Tensor& key_new, + const torch::Tensor& value_new, const torch::Tensor& key_cache, + const torch::Tensor& value_cache, const torch::Tensor& block_table, + int64_t context_len_arg, double scale_arg) { + check_half_cuda_contiguous(query, "query"); + check_half_cuda_contiguous(key_new, "key_new"); + check_half_cuda_contiguous(value_new, "value_new"); + check_half_cuda_contiguous(key_cache, "key_cache"); + check_half_cuda_contiguous(value_cache, "value_cache"); + TORCH_CHECK(block_table.is_cuda(), + "block_table must be a CUDA tensor"); + TORCH_CHECK(block_table.scalar_type() == torch::kInt32, + "block_table must have dtype int32"); + TORCH_CHECK(block_table.is_contiguous(), + "block_table must be contiguous"); + TORCH_CHECK(block_table.dim() == 1, + "block_table must be one-dimensional"); + TORCH_CHECK(query.dim() == 3 && query.size(1) == kNumQueryHeads + && query.size(2) == kHeadDim, + "query must have shape (Q, 4, 256)"); + TORCH_CHECK(key_new.dim() == 3 && key_new.size(1) == kNumKvHeads + && key_new.size(2) == kHeadDim, + "key_new must have shape (Q, 1, 256)"); + TORCH_CHECK(value_new.sizes() == key_new.sizes(), + "value_new must match key_new"); + TORCH_CHECK(key_new.size(0) == query.size(0), + "query, key_new, and value_new lengths must match"); + TORCH_CHECK(key_cache.dim() == 5 + && key_cache.size(1) == kNumKvHeads + && key_cache.size(2) == kHeadDim / kKeyPack + && key_cache.size(3) == kBlockSize + && key_cache.size(4) == kKeyPack, + "key_cache must have shape (N, 1, 32, 16, 8)"); + TORCH_CHECK(value_cache.dim() == 4 + && value_cache.size(1) == kNumKvHeads + && value_cache.size(2) == kHeadDim + && value_cache.size(3) == kBlockSize, + "value_cache must have shape (N, 1, 256, 16)"); + TORCH_CHECK(key_cache.size(0) == value_cache.size(0), + "key/value cache block counts must match"); + TORCH_CHECK(query.device() == key_new.device() + && query.device() == value_new.device() + && query.device() == key_cache.device() + && query.device() == value_cache.device() + && query.device() == block_table.device(), + "all tensors must use the same device"); + TORCH_CHECK(context_len_arg >= 0 + && context_len_arg <= kMaxSequenceTokens, + "context_len is out of range"); + TORCH_CHECK(context_len_arg % kBlockSize == 0, + "context_len must be block aligned"); + const int query_len = static_cast(query.size(0)); + const int context_len = static_cast(context_len_arg); + TORCH_CHECK(query_len > 0 && query_len <= kMaxQueryTokens, + "query length must be in [1, 8192]"); + TORCH_CHECK(context_len + query_len <= kMaxSequenceTokens, + "context_len + query_len exceeds 262144"); + const int required_blocks = + (context_len + kBlockSize - 1) / kBlockSize; + TORCH_CHECK(block_table.numel() >= required_blocks, + "block_table is too short for context_len"); + if (required_blocks > 0) { + auto active_blocks = block_table.narrow(0, 0, required_blocks); + const int minimum_block = active_blocks.min().item(); + const int maximum_block = active_blocks.max().item(); + TORCH_CHECK(minimum_block >= 0 + && maximum_block < key_cache.size(0), + "block_table contains an out-of-range physical block ID"); + } + TORCH_CHECK(std::isfinite(scale_arg) && scale_arg > 0.0, + "scale must be finite and positive"); + TORCH_CHECK(query_len <= std::numeric_limits::max() / kNumQueryHeads, + "query length overflows row count"); + + const int rows = kNumQueryHeads * query_len; + const int64_t output_elements = + static_cast(rows) * kHeadDim; + auto float_options = query.options().dtype(torch::kFloat32); + auto converted_query = torch::empty( + {kNumQueryHeads, query_len, kHeadDim}, float_options); + auto key_tiles = torch::empty( + {kSplitCount, kTileTokens, kHeadDim}, float_options); + auto value_tiles = torch::empty( + {kSplitCount, kTileTokens, kHeadDim}, float_options); + auto scores = torch::empty( + {kSplitCount, kNumQueryHeads, query_len, kTileTokens}, + float_options); + auto split_output = torch::empty( + {kSplitCount, kNumQueryHeads, query_len, kHeadDim}, + float_options); + auto running_max = torch::full( + {kNumQueryHeads, query_len}, + -std::numeric_limits::infinity(), float_options); + auto running_sum = torch::zeros( + {kNumQueryHeads, query_len}, float_options); + auto running_output = torch::zeros( + {kNumQueryHeads, query_len, kHeadDim}, float_options); + auto corrections = torch::empty( + {kSplitCount, kNumQueryHeads, query_len}, float_options); + + auto stream = at::cuda::getCurrentCUDAStream(); + convert_query_kernel<<>>( + reinterpret_cast(query.data_ptr()), + converted_query.data_ptr(), query_len, + static_cast(scale_arg)); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + + cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle(); + check_cublas(cublasSetStream(handle, stream), "cublasSetStream"); + const int64_t key_split_stride = + static_cast(kTileTokens) * kHeadDim; + const int64_t score_split_stride = + static_cast(rows) * kTileTokens; + const int64_t output_split_stride = output_elements; + const auto run_group = [&](int group_start, int group_tokens, + bool causal) { + const int active_splits = + (group_tokens + kTileTokens - 1) / kTileTokens; + TORCH_CHECK(active_splits > 0 && active_splits <= kSplitCount, + "invalid split count for paged-prefill group"); + constexpr int kGatherBlocks = 512; + gather_kv_group_kernel<<>>( + reinterpret_cast(key_new.data_ptr()), + reinterpret_cast(value_new.data_ptr()), + reinterpret_cast(key_cache.data_ptr()), + reinterpret_cast(value_cache.data_ptr()), + block_table.data_ptr(), key_tiles.data_ptr(), + value_tiles.data_ptr(), context_len, query_len, group_start, + group_tokens, active_splits); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + + for (int split = 0; split < active_splits; ++split) { + check_cublas(qk_batched( + handle, + key_tiles.data_ptr() + split * key_split_stride, + converted_query.data_ptr(), + scores.data_ptr() + split * score_split_stride, + query_len), "split4 paged prefill QK"); + } + + const bool needs_mask = + causal || group_tokens != active_splits * kTileTokens; + if (needs_mask) { + const int64_t score_elements = + static_cast(active_splits) * score_split_stride; + mask_group_scores_kernel<<< + launch_blocks(score_elements), kThreads, 0, stream>>>( + scores.data_ptr(), query_len, context_len, group_start, + group_tokens, active_splits, rows, causal); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + } + + normalize_split_scores_kernel<<>>( + scores.data_ptr(), corrections.data_ptr(), + running_max.data_ptr(), running_sum.data_ptr(), + active_splits, rows); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + + for (int split = 0; split < active_splits; ++split) { + check_cublas(pv_batched( + handle, + value_tiles.data_ptr() + split * key_split_stride, + scores.data_ptr() + split * score_split_stride, + split_output.data_ptr() + split * output_split_stride, + query_len), "split4 paged prefill PV"); + } + + merge_split_output_kernel<<< + launch_blocks(output_elements), kThreads, 0, stream>>>( + running_output.data_ptr(), split_output.data_ptr(), + corrections.data_ptr(), active_splits, rows, + output_elements); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + }; + for (int group_start = 0; group_start < context_len; + group_start += kGroupTokens) { + run_group(group_start, + std::min(kGroupTokens, context_len - group_start), false); + } + for (int key_start = 0; key_start < query_len; + key_start += kGroupTokens) { + run_group(context_len + key_start, + std::min(kGroupTokens, query_len - key_start), true); + } + + running_output.div_(running_sum.unsqueeze(-1)); + auto output = running_output.permute({1, 0, 2}) + .to(query.scalar_type()).contiguous(); + auto lse = (running_max + at::log(running_sum)) + .transpose(0, 1).contiguous(); + return {output, lse}; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("forward", &fused_paged_prefill_forward, + "Fixed-shape FP32 paged-prefill pipeline for cache-only context"); +} diff --git a/qwen3_6_scripts/corex_gdn_beta_decay.cu b/qwen3_6_scripts/corex_gdn_beta_decay.cu new file mode 100644 index 0000000..48e50dc --- /dev/null +++ b/qwen3_6_scripts/corex_gdn_beta_decay.cu @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +namespace { + +__global__ void beta_decay_kernel(const half* beta_input, + const half* decay_input, + const half* a_log, + const half* dt_bias, + float* output, int elements, + int heads) { + const int index = blockIdx.x * blockDim.x + threadIdx.x; + if (index >= elements) { + return; + } + const int head = index % heads; + + const float beta_value = __half2float(beta_input[index]); + const float beta_fp32 = 1.0f / (1.0f + expf(-beta_value)); + output[index] = __half2float(__float2half(beta_fp32)); + + const float x = (__half2float(decay_input[index]) + + __half2float(dt_bias[head])); + const float softplus = x > 20.0f ? x : log1pf(expf(x)); + output[elements + index] = expf( + -expf(__half2float(a_log[head])) * softplus); +} + +void check_half(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 2, name, " must have shape (batch, heads)"); +} + +void check_half_vector(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 1, name, " must have shape (heads)"); +} + +} // namespace + +torch::Tensor beta_decay(const torch::Tensor& beta_input, + const torch::Tensor& decay_input, + const torch::Tensor& a_log, + const torch::Tensor& dt_bias) { + check_half(beta_input, "beta_input"); + check_half(decay_input, "decay_input"); + check_half_vector(a_log, "a_log"); + check_half_vector(dt_bias, "dt_bias"); + TORCH_CHECK(beta_input.sizes() == decay_input.sizes(), + "beta_input and decay_input shapes must match"); + TORCH_CHECK(beta_input.size(1) == a_log.size(0) && + a_log.sizes() == dt_bias.sizes(), + "parameter heads must match input heads"); + + const int elements = static_cast(beta_input.numel()); + const int heads = static_cast(beta_input.size(1)); + torch::Tensor output = torch::empty( + {2, beta_input.size(0), beta_input.size(1)}, + beta_input.options().dtype(torch::kFloat32)); + constexpr int threads = 128; + const int blocks = (elements + threads - 1) / threads; + beta_decay_kernel<<>>( + reinterpret_cast(beta_input.data_ptr()), + reinterpret_cast(decay_input.data_ptr()), + reinterpret_cast(a_log.data_ptr()), + reinterpret_cast(dt_bias.data_ptr()), + output.data_ptr(), elements, heads); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("beta_decay", &beta_decay, + "Fused GDN beta sigmoid and decay factor"); +} diff --git a/qwen3_6_scripts/corex_gdn_causal_conv.cu b/qwen3_6_scripts/corex_gdn_causal_conv.cu new file mode 100644 index 0000000..6101d5c --- /dev/null +++ b/qwen3_6_scripts/corex_gdn_causal_conv.cu @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kStateLen = 3; +constexpr int kKernelSize = kStateLen + 1; +constexpr int kThreads = 256; + +__global__ void causal_conv_update_kernel( + float* state, const __half* hidden, const __half* weight, + __half* output, int channels) { + const int channel = blockIdx.x * blockDim.x + threadIdx.x; + const int batch = blockIdx.y; + if (channel >= channels) { + return; + } + + const int state_offset = (batch * channels + channel) * kStateLen; + const int vector_offset = batch * channels + channel; + const int weight_offset = channel * kKernelSize; + const __half current = hidden[vector_offset]; + const __half state0 = __float2half_rn(state[state_offset]); + const __half state1 = __float2half_rn(state[state_offset + 1]); + const __half state2 = __float2half_rn(state[state_offset + 2]); + + float value = __half2float(state0) * __half2float(weight[weight_offset]); + value += __half2float(state1) * __half2float(weight[weight_offset + 1]); + value += __half2float(state2) * __half2float(weight[weight_offset + 2]); + value += __half2float(current) * __half2float(weight[weight_offset + 3]); + + state[state_offset] = __half2float(state1); + state[state_offset + 1] = __half2float(state2); + state[state_offset + 2] = __half2float(current); + const __half convolved = __float2half_rn(value); + const float activation_input = __half2float(convolved); + output[vector_offset] = __float2half_rn( + activation_input / (1.0f + expf(-activation_input))); +} + +void check_half_cuda_contiguous(const torch::Tensor& tensor, + const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); +} + +} // namespace + +torch::Tensor causal_conv_update(torch::Tensor state, + const torch::Tensor& hidden, + const torch::Tensor& weight) { + TORCH_CHECK(state.is_cuda(), "state must be a CUDA tensor"); + TORCH_CHECK(state.scalar_type() == torch::kFloat32, + "state must have dtype float32"); + TORCH_CHECK(state.is_contiguous(), "state must be contiguous"); + check_half_cuda_contiguous(hidden, "hidden"); + check_half_cuda_contiguous(weight, "weight"); + TORCH_CHECK(state.dim() == 3 && state.size(2) == kStateLen, + "state must have shape (batch, channels, 3)"); + TORCH_CHECK(hidden.dim() == 3 && hidden.size(2) == 1 && + hidden.size(0) == state.size(0) && + hidden.size(1) == state.size(1), + "hidden must have shape (batch, channels, 1)"); + TORCH_CHECK(weight.dim() == 2 && weight.size(0) == state.size(1) && + weight.size(1) == kKernelSize, + "weight must have shape (channels, 4)"); + + auto output = torch::empty_like(hidden); + const int channels = static_cast(state.size(1)); + const dim3 blocks((channels + kThreads - 1) / kThreads, + static_cast(state.size(0))); + causal_conv_update_kernel<<>>( + state.data_ptr(), + reinterpret_cast(hidden.data_ptr()), + reinterpret_cast(weight.data_ptr()), + reinterpret_cast<__half*>(output.data_ptr()), channels); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("causal_conv_update", &causal_conv_update, + "Fused CoreX Gated DeltaNet causal convolution update"); +} diff --git a/qwen3_6_scripts/corex_gdn_gated_norm.cu b/qwen3_6_scripts/corex_gdn_gated_norm.cu new file mode 100644 index 0000000..aa8a035 --- /dev/null +++ b/qwen3_6_scripts/corex_gdn_gated_norm.cu @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kHeadDim = 128; + +__device__ __forceinline__ float silu(float value) { + return value / (1.0f + expf(-value)); +} + +__global__ void gated_rms_norm_inverse_kernel( + const float* input, const __half* gate, const __half* weight, + const float* inverse, __half* output, int rows) { + const int row = blockIdx.x; + const int column = threadIdx.x; + if (row >= rows || column >= kHeadDim) { + return; + } + const int offset = row * kHeadDim + column; + const float scaled = __fmul_rn(input[offset], inverse[row]); + const float normalized = __fmul_rn( + __half2float(weight[column]), scaled); + const float activated = silu(__half2float(gate[offset])); + output[offset] = __float2half_rn(__fmul_rn(normalized, activated)); +} + +void check_input(const torch::Tensor& input, const torch::Tensor& gate, + const torch::Tensor& weight, + const torch::Tensor& inverse) { + TORCH_CHECK(input.is_cuda() && gate.is_cuda() && weight.is_cuda() + && inverse.is_cuda(), + "all tensors must be CUDA tensors"); + TORCH_CHECK(input.scalar_type() == torch::kFloat32, + "input must have dtype float32"); + TORCH_CHECK(gate.scalar_type() == torch::kFloat16, + "gate must have dtype float16"); + TORCH_CHECK(weight.scalar_type() == torch::kFloat16, + "weight must have dtype float16"); + TORCH_CHECK(inverse.scalar_type() == torch::kFloat32, + "inverse must have dtype float32"); + TORCH_CHECK(input.is_contiguous() && gate.is_contiguous() + && weight.is_contiguous() && inverse.is_contiguous(), + "all tensors must be contiguous"); + TORCH_CHECK(input.dim() == 2 && input.size(1) == kHeadDim, + "input must have shape (rows, 128)"); + TORCH_CHECK(gate.sizes() == input.sizes(), + "gate must match input shape"); + TORCH_CHECK(weight.dim() == 1 && weight.size(0) == kHeadDim, + "weight must have shape (128,)"); + TORCH_CHECK(inverse.numel() == input.size(0), + "inverse must contain one value per row"); +} + +} // namespace + +torch::Tensor apply_inverse(const torch::Tensor& input, + const torch::Tensor& gate, + const torch::Tensor& weight, + const torch::Tensor& inverse) { + check_input(input, gate, weight, inverse); + auto output = torch::empty_like(gate); + const int rows = static_cast(input.size(0)); + gated_rms_norm_inverse_kernel<<< + rows, kHeadDim, 0, at::cuda::getCurrentCUDAStream()>>>( + input.data_ptr(), + reinterpret_cast(gate.data_ptr()), + reinterpret_cast(weight.data_ptr()), + inverse.data_ptr(), + reinterpret_cast<__half*>(output.data_ptr()), rows); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("apply_inverse", &apply_inverse, + "CoreX gated RMSNorm using a PyTorch-computed inverse"); +} diff --git a/qwen3_6_scripts/corex_gdn_packed_decode.cu b/qwen3_6_scripts/corex_gdn_packed_decode.cu new file mode 100644 index 0000000..140017d --- /dev/null +++ b/qwen3_6_scripts/corex_gdn_packed_decode.cu @@ -0,0 +1,165 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kKeyHeads = 4; +constexpr int kValueHeads = 8; +constexpr int kHeadDim = 128; +constexpr int kMixedDim = + (2 * kKeyHeads + kValueHeads) * kHeadDim; +constexpr float kQueryScale = 0.08838834764831845f; + +__global__ void gdn_packed_decode_kernel( + float* state, const half* mixed_qkv, const half* beta_input, + const half* decay_input, const half* a_log, const half* dt_bias, + float* output) { + const int batch_head = blockIdx.x; + const int column = threadIdx.x; + const int batch = batch_head / kValueHeads; + const int value_head = batch_head % kValueHeads; + const int key_head = value_head / (kValueHeads / kKeyHeads); + const int mixed_offset = batch * kMixedDim; + const int query_offset = mixed_offset + key_head * kHeadDim; + const int key_offset = + mixed_offset + kKeyHeads * kHeadDim + key_head * kHeadDim; + const int value_offset = mixed_offset + 2 * kKeyHeads * kHeadDim + + value_head * kHeadDim; + const int vector_offset = batch_head * kHeadDim; + const int state_offset = batch_head * kHeadDim * kHeadDim; + + __shared__ half norm_squares[kHeadDim * 2]; + __shared__ float normalized_query[kHeadDim]; + __shared__ float normalized_key[kHeadDim]; + const half raw_query = mixed_qkv[query_offset + column]; + const half raw_key = mixed_qkv[key_offset + column]; + norm_squares[column] = __hmul(raw_query, raw_query); + norm_squares[kHeadDim + column] = __hmul(raw_key, raw_key); + __syncthreads(); + + for (int stride = kHeadDim / 2; stride > 0; stride >>= 1) { + if (column < stride) { + norm_squares[column] = __hadd( + norm_squares[column], norm_squares[column + stride]); + norm_squares[kHeadDim + column] = __hadd( + norm_squares[kHeadDim + column], + norm_squares[kHeadDim + column + stride]); + } + __syncthreads(); + } + + const half epsilon = __float2half(1e-6f); + const half query_inverse = __float2half(rsqrtf(__half2float( + __hadd(norm_squares[0], epsilon)))); + const half key_inverse = __float2half(rsqrtf(__half2float( + __hadd(norm_squares[kHeadDim], epsilon)))); + normalized_query[column] = __half2float( + __hmul(raw_query, query_inverse)) * kQueryScale; + normalized_key[column] = __half2float(__hmul(raw_key, key_inverse)); + __syncthreads(); + + const int coefficient_offset = batch * kValueHeads + value_head; + const float beta_value = __half2float(beta_input[coefficient_offset]); + const float beta = __half2float(__float2half( + 1.0f / (1.0f + expf(-beta_value)))); + const float decay_x = __half2float(decay_input[coefficient_offset]) + + __half2float(dt_bias[value_head]); + const float softplus = + decay_x > 20.0f ? decay_x : log1pf(expf(decay_x)); + const float decay = expf( + -expf(__half2float(a_log[value_head])) * softplus); + + float memory = 0.0f; +#pragma unroll + for (int row = 0; row < kHeadDim; ++row) { + const int index = state_offset + row * kHeadDim + column; + const float decayed = state[index] * decay; + memory += normalized_key[row] * decayed; + } + + const float value = __half2float(mixed_qkv[value_offset + column]); + const float delta = (value - memory) * beta; + float result = 0.0f; +#pragma unroll + for (int row = 0; row < kHeadDim; ++row) { + const int index = state_offset + row * kHeadDim + column; + const float decayed = state[index] * decay; + const float updated = decayed + normalized_key[row] * delta; + state[index] = updated; + result += normalized_query[row] * updated; + } + output[vector_offset + column] = result; +} + +void check_half_matrix(const torch::Tensor& tensor, const char* name, + int64_t width) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 2 && tensor.size(1) == width, + name, " must have shape (batch, ", width, ")"); +} + +void check_half_vector(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 1 && tensor.size(0) == kValueHeads, + name, " must have shape (", kValueHeads, ")"); +} + +} // namespace + +torch::Tensor packed_decode(torch::Tensor state, + const torch::Tensor& mixed_qkv, + const torch::Tensor& beta_input, + const torch::Tensor& decay_input, + const torch::Tensor& a_log, + const torch::Tensor& dt_bias) { + TORCH_CHECK(state.is_cuda(), "state must be a CUDA tensor"); + TORCH_CHECK(state.scalar_type() == torch::kFloat32, + "state must have dtype float32"); + TORCH_CHECK(state.is_contiguous(), "state must be contiguous"); + TORCH_CHECK(state.dim() == 4 && state.size(0) == 1 + && state.size(1) == kValueHeads + && state.size(2) == kHeadDim + && state.size(3) == kHeadDim, + "state must have shape (1, 8, 128, 128)"); + check_half_matrix(mixed_qkv, "mixed_qkv", kMixedDim); + check_half_matrix(beta_input, "beta_input", kValueHeads); + check_half_matrix(decay_input, "decay_input", kValueHeads); + check_half_vector(a_log, "a_log"); + check_half_vector(dt_bias, "dt_bias"); + TORCH_CHECK(mixed_qkv.size(0) == 1 && beta_input.size(0) == 1 + && decay_input.size(0) == 1, + "packed decode only supports one sequence"); + TORCH_CHECK(state.device() == mixed_qkv.device() + && state.device() == beta_input.device() + && state.device() == decay_input.device() + && state.device() == a_log.device() + && state.device() == dt_bias.device(), + "all inputs must be on the same device"); + + torch::Tensor output = torch::empty( + {1, kValueHeads, kHeadDim}, state.options()); + gdn_packed_decode_kernel<<>>( + state.data_ptr(), + reinterpret_cast(mixed_qkv.data_ptr()), + reinterpret_cast(beta_input.data_ptr()), + reinterpret_cast(decay_input.data_ptr()), + reinterpret_cast(a_log.data_ptr()), + reinterpret_cast(dt_bias.data_ptr()), + output.data_ptr()); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("packed_decode", &packed_decode, + "Packed Qwen3.6 GDN single-token decode"); +} diff --git a/qwen3_6_scripts/corex_gdn_qk_map.cu b/qwen3_6_scripts/corex_gdn_qk_map.cu new file mode 100644 index 0000000..7d4d74f --- /dev/null +++ b/qwen3_6_scripts/corex_gdn_qk_map.cu @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kHeadDim = 128; +constexpr float kQueryScale = 0.08838834764831845f; + +__global__ void qk_map_kernel(const half* query, const half* key, + float* output, int batch, int key_heads, + int value_heads, int expand_ratio) { + const int elements = batch * value_heads * kHeadDim; + const int index = blockIdx.x * blockDim.x + threadIdx.x; + if (index >= elements) { + return; + } + const int dim = index % kHeadDim; + const int value_head_index = index / kHeadDim; + const int value_head = value_head_index % value_heads; + const int batch_index = value_head_index / value_heads; + const int key_head = value_head / expand_ratio; + const int source = ((batch_index * key_heads + key_head) * kHeadDim + dim); + output[index] = __half2float(query[source]) * kQueryScale; + output[elements + index] = __half2float(key[source]); +} + +void check_input(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 3 && tensor.size(2) == kHeadDim, + name, " must have shape (batch, key_heads, 128)"); +} + +} // namespace + +torch::Tensor qk_map(const torch::Tensor& query, + const torch::Tensor& key, + int64_t value_heads_arg) { + check_input(query, "query"); + check_input(key, "key"); + TORCH_CHECK(query.sizes() == key.sizes(), + "query and key shapes must match"); + const int batch = static_cast(query.size(0)); + const int key_heads = static_cast(query.size(1)); + const int value_heads = static_cast(value_heads_arg); + TORCH_CHECK(value_heads > 0 && value_heads % key_heads == 0, + "value_heads must be divisible by key_heads"); + + torch::Tensor output = torch::empty( + {2, batch, value_heads, kHeadDim}, + query.options().dtype(torch::kFloat32)); + const int elements = batch * value_heads * kHeadDim; + constexpr int threads = 256; + const int blocks = (elements + threads - 1) / threads; + qk_map_kernel<<>>( + reinterpret_cast(query.data_ptr()), + reinterpret_cast(key.data_ptr()), + output.data_ptr(), batch, key_heads, value_heads, + value_heads / key_heads); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("qk_map", &qk_map, + "Map normalized FP16 key heads to FP32 value heads"); +} diff --git a/qwen3_6_scripts/corex_moe_direct_routed.cu b/qwen3_6_scripts/corex_moe_direct_routed.cu new file mode 100644 index 0000000..37699d4 --- /dev/null +++ b/qwen3_6_scripts/corex_moe_direct_routed.cu @@ -0,0 +1,181 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kExperts = 256; +constexpr int kTopK = 8; +constexpr int kHidden = 2048; +constexpr int kIntermediate = 128; +constexpr int kW13Rows = 2 * kIntermediate; +constexpr int kThreads = 256; +constexpr int kWarpSize = 32; + +__device__ inline float warp_sum(float value) { +#pragma unroll + for (int offset = kWarpSize / 2; offset > 0; offset /= 2) { + value += __shfl_down_sync(0xffffffff, value, offset); + } + return value; +} + +__global__ void direct_w13_kernel( + const __half* input, const __half* w13, const int64_t* expert_ids, + __half* gate_up) { + const int warp = + (static_cast(blockIdx.x) * blockDim.x + threadIdx.x) / kWarpSize; + const int lane = threadIdx.x & (kWarpSize - 1); + if (warp >= kTopK * kW13Rows) { + return; + } + + const int slot = warp / kW13Rows; + const int local_row = warp - slot * kW13Rows; + const int64_t expert = expert_ids[slot]; + const int64_t weight_row = + (expert * kW13Rows + local_row) * static_cast(kHidden); + const __half2* input2 = reinterpret_cast(input); + const __half2* weight2 = + reinterpret_cast(w13 + weight_row); + float sum = 0.0f; + for (int index = lane; index < kHidden / 2; index += kWarpSize) { + const __half2 x = input2[index]; + const __half2 weight = weight2[index]; + sum = fmaf(__half2float(weight.x), __half2float(x.x), sum); + sum = fmaf(__half2float(weight.y), __half2float(x.y), sum); + } + sum = warp_sum(sum); + if (lane == 0) { + gate_up[warp] = __float2half_rn(sum); + } +} + +__global__ void direct_w2_reduce_kernel( + const __half* activated, const __half* w2, const int64_t* expert_ids, + const __half* weights, __half* output) { + const int warp = + (static_cast(blockIdx.x) * blockDim.x + threadIdx.x) / kWarpSize; + const int lane = threadIdx.x & (kWarpSize - 1); + if (warp >= kHidden) { + return; + } + + float weighted_sum = 0.0f; +#pragma unroll + for (int slot = 0; slot < kTopK; ++slot) { + const int64_t expert = expert_ids[slot]; + const int64_t weight_row = + (expert * kHidden + warp) * static_cast(kIntermediate); + const __half2* activation2 = reinterpret_cast( + activated + slot * kIntermediate); + const __half2* weight2 = + reinterpret_cast(w2 + weight_row); + float expert_sum = 0.0f; + for (int index = lane; index < kIntermediate / 2; + index += kWarpSize) { + const __half2 x = activation2[index]; + const __half2 weight = weight2[index]; + expert_sum = fmaf( + __half2float(weight.x), __half2float(x.x), expert_sum); + expert_sum = fmaf( + __half2float(weight.y), __half2float(x.y), expert_sum); + } + expert_sum = warp_sum(expert_sum); + if (lane == 0) { + const __half expert_half = __float2half_rn(expert_sum); + const __half product = __hmul(expert_half, weights[slot]); + weighted_sum += __half2float(product); + } + } + if (lane == 0) { + output[warp] = __float2half_rn(weighted_sum); + } +} + +void check_half_cuda(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); +} + +void check_ids(const torch::Tensor& expert_ids) { + TORCH_CHECK(expert_ids.is_cuda() && expert_ids.is_contiguous(), + "expert_ids must be a contiguous CUDA tensor"); + TORCH_CHECK(expert_ids.scalar_type() == torch::kInt64, + "expert_ids must have dtype int64"); + TORCH_CHECK(expert_ids.dim() == 1 && expert_ids.numel() == kTopK, + "expert_ids must have shape (8,)"); +} + +} // namespace + +torch::Tensor direct_w13(const torch::Tensor& input, + const torch::Tensor& w13, + const torch::Tensor& expert_ids) { + check_half_cuda(input, "input"); + check_half_cuda(w13, "w13"); + check_ids(expert_ids); + TORCH_CHECK(input.dim() == 2 && input.size(0) == 1 + && input.size(1) == kHidden, + "input must have shape (1, 2048)"); + TORCH_CHECK(w13.dim() == 3 && w13.size(0) == kExperts + && w13.size(1) == kW13Rows + && w13.size(2) == kHidden, + "w13 must have shape (256, 256, 2048)"); + + auto output = torch::empty({kTopK, kW13Rows}, input.options()); + constexpr int kWarpsPerBlock = kThreads / kWarpSize; + constexpr int kBlocks = + (kTopK * kW13Rows + kWarpsPerBlock - 1) / kWarpsPerBlock; + direct_w13_kernel<<>>( + reinterpret_cast(input.data_ptr()), + reinterpret_cast(w13.data_ptr()), + expert_ids.data_ptr(), + reinterpret_cast<__half*>(output.data_ptr())); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +torch::Tensor direct_w2_reduce(const torch::Tensor& activated, + const torch::Tensor& w2, + const torch::Tensor& expert_ids, + const torch::Tensor& weights) { + check_half_cuda(activated, "activated"); + check_half_cuda(w2, "w2"); + check_half_cuda(weights, "weights"); + check_ids(expert_ids); + TORCH_CHECK(activated.dim() == 2 && activated.size(0) == kTopK + && activated.size(1) == kIntermediate, + "activated must have shape (8, 128)"); + TORCH_CHECK(w2.dim() == 3 && w2.size(0) == kExperts + && w2.size(1) == kHidden + && w2.size(2) == kIntermediate, + "w2 must have shape (256, 2048, 128)"); + TORCH_CHECK(weights.dim() == 1 && weights.numel() == kTopK, + "weights must have shape (8,)"); + + auto output = torch::empty({1, kHidden}, activated.options()); + constexpr int kWarpsPerBlock = kThreads / kWarpSize; + constexpr int kBlocks = + (kHidden + kWarpsPerBlock - 1) / kWarpsPerBlock; + direct_w2_reduce_kernel<<>>( + reinterpret_cast(activated.data_ptr()), + reinterpret_cast(w2.data_ptr()), + expert_ids.data_ptr(), + reinterpret_cast(weights.data_ptr()), + reinterpret_cast<__half*>(output.data_ptr())); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("w13", &direct_w13, + "Direct selected-expert FP16 W13 matvec"); + module.def("w2_reduce", &direct_w2_reduce, + "Direct selected-expert W2 matvec and routed reduction"); +} diff --git a/qwen3_6_scripts/corex_moe_exact_reduce.cu b/qwen3_6_scripts/corex_moe_exact_reduce.cu new file mode 100644 index 0000000..05c3b7f --- /dev/null +++ b/qwen3_6_scripts/corex_moe_exact_reduce.cu @@ -0,0 +1,107 @@ +#include +#include +#include +#include + +namespace { + +constexpr int kTopK = 8; +constexpr int kThreads = 256; + +enum class Mode { kSerialFloat, kTreeFloat, kSerialHalf }; + +__global__ void exact_reduce_kernel(const __half* expert_output, + const __half* weights, + __half* output, int hidden, + Mode mode) { + const int column = blockIdx.x * blockDim.x + threadIdx.x; + if (column >= hidden) { + return; + } + __half products[kTopK]; +#pragma unroll + for (int expert = 0; expert < kTopK; ++expert) { + products[expert] = __hmul( + expert_output[expert * hidden + column], weights[expert]); + } + if (mode == Mode::kSerialHalf) { + __half sum = products[0]; +#pragma unroll + for (int expert = 1; expert < kTopK; ++expert) { + sum = __hadd(sum, products[expert]); + } + output[column] = sum; + return; + } + + float sum; + if (mode == Mode::kSerialFloat) { + sum = __half2float(products[0]); +#pragma unroll + for (int expert = 1; expert < kTopK; ++expert) { + sum += __half2float(products[expert]); + } + } else { + const float sum01 = __half2float(products[0]) + __half2float(products[1]); + const float sum23 = __half2float(products[2]) + __half2float(products[3]); + const float sum45 = __half2float(products[4]) + __half2float(products[5]); + const float sum67 = __half2float(products[6]) + __half2float(products[7]); + sum = (sum01 + sum23) + (sum45 + sum67); + } + output[column] = __float2half_rn(sum); +} + +void check_input(const torch::Tensor& expert_output, + const torch::Tensor& weights) { + TORCH_CHECK(expert_output.is_cuda() && weights.is_cuda(), + "inputs must be CUDA tensors"); + TORCH_CHECK(expert_output.scalar_type() == torch::kFloat16 + && weights.scalar_type() == torch::kFloat16, + "inputs must have dtype float16"); + TORCH_CHECK(expert_output.is_contiguous() && weights.is_contiguous(), + "inputs must be contiguous"); + TORCH_CHECK(expert_output.dim() == 2 + && expert_output.size(0) == kTopK, + "expert_output must have shape (8, hidden)"); + TORCH_CHECK(weights.dim() == 1 && weights.size(0) == kTopK, + "weights must have shape (8,)"); +} + +torch::Tensor launch(const torch::Tensor& expert_output, + const torch::Tensor& weights, Mode mode) { + check_input(expert_output, weights); + auto output = torch::empty( + {1, expert_output.size(1)}, expert_output.options()); + const int hidden = static_cast(expert_output.size(1)); + const int blocks = (hidden + kThreads - 1) / kThreads; + exact_reduce_kernel<<>>( + reinterpret_cast(expert_output.data_ptr()), + reinterpret_cast(weights.data_ptr()), + reinterpret_cast<__half*>(output.data_ptr()), hidden, mode); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return output; +} + +} // namespace + +torch::Tensor serial_float(const torch::Tensor& expert_output, + const torch::Tensor& weights) { + return launch(expert_output, weights, Mode::kSerialFloat); +} + +torch::Tensor tree_float(const torch::Tensor& expert_output, + const torch::Tensor& weights) { + return launch(expert_output, weights, Mode::kTreeFloat); +} + +torch::Tensor serial_half(const torch::Tensor& expert_output, + const torch::Tensor& weights) { + return launch(expert_output, weights, Mode::kSerialHalf); +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("serial_float", &serial_float); + module.def("tree_float", &tree_float); + module.def("serial_half", &serial_half); +} diff --git a/qwen3_6_scripts/corex_moe_weight_gather.cu b/qwen3_6_scripts/corex_moe_weight_gather.cu new file mode 100644 index 0000000..60ebc70 --- /dev/null +++ b/qwen3_6_scripts/corex_moe_weight_gather.cu @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +#include +#include + +namespace { + +constexpr int kTopK = 8; +constexpr int kThreads = 256; +constexpr int kGridX = 8; + +__global__ void selected_weight_gather_vec16_kernel( + const uint4* w13, const uint4* w2, const int64_t* expert_ids, + uint4* selected_w13, uint4* selected_w2, + int64_t w13_vecs_per_expert, int64_t w2_vecs_per_expert) { + const int segment = blockIdx.y; + const int slot = segment & (kTopK - 1); + const bool copy_w2 = segment >= kTopK; + const int64_t count = + copy_w2 ? w2_vecs_per_expert : w13_vecs_per_expert; + const uint4* source = copy_w2 ? w2 : w13; + uint4* output = copy_w2 ? selected_w2 : selected_w13; + const int64_t source_offset = expert_ids[slot] * count; + const int64_t output_offset = static_cast(slot) * count; + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < count; + index += static_cast(blockDim.x) * gridDim.x) { + output[output_offset + index] = source[source_offset + index]; + } +} + +void check_weight(const torch::Tensor& tensor, const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); + TORCH_CHECK(tensor.dim() == 3, name, " must be rank three"); + TORCH_CHECK(tensor.size(1) * tensor.size(2) % 8 == 0, + name, " expert slices must be divisible by 16 bytes"); +} + +} // namespace + +std::vector gather_selected_weights( + const torch::Tensor& w13, const torch::Tensor& w2, + const torch::Tensor& expert_ids) { + check_weight(w13, "w13"); + check_weight(w2, "w2"); + TORCH_CHECK(w13.device() == w2.device(), + "W13/W2 must be on the same device"); + TORCH_CHECK(w13.size(0) == w2.size(0), + "W13/W2 expert counts differ"); + TORCH_CHECK(w13.size(2) == w2.size(1), + "W13/W2 hidden dimensions differ"); + TORCH_CHECK(w13.size(1) == 2 * w2.size(2), + "W13/W2 intermediate dimensions differ"); + TORCH_CHECK(expert_ids.is_cuda() && expert_ids.is_contiguous(), + "expert_ids must be a contiguous CUDA tensor"); + TORCH_CHECK(expert_ids.device() == w13.device(), + "weights and expert_ids must be on the same device"); + TORCH_CHECK(expert_ids.scalar_type() == torch::kInt64, + "expert_ids must have dtype int64"); + TORCH_CHECK(expert_ids.dim() == 1 && expert_ids.numel() == kTopK, + "expert_ids must have shape (8,)"); + + auto selected_w13 = torch::empty( + {kTopK, w13.size(1), w13.size(2)}, w13.options()); + auto selected_w2 = torch::empty( + {kTopK, w2.size(1), w2.size(2)}, w2.options()); + const int64_t w13_vecs_per_expert = w13.size(1) * w13.size(2) / 8; + const int64_t w2_vecs_per_expert = w2.size(1) * w2.size(2) / 8; + const dim3 grid(kGridX, 2 * kTopK); + selected_weight_gather_vec16_kernel<<< + grid, kThreads, 0, at::cuda::getCurrentCUDAStream()>>>( + reinterpret_cast(w13.data_ptr()), + reinterpret_cast(w2.data_ptr()), + expert_ids.data_ptr(), + reinterpret_cast(selected_w13.data_ptr()), + reinterpret_cast(selected_w2.data_ptr()), + w13_vecs_per_expert, w2_vecs_per_expert); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return {selected_w13, selected_w2}; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("gather", &gather_selected_weights, + "Gather selected FP16 top-8 MoE weights with 16-byte loads"); +} diff --git a/qwen3_6_scripts/corex_paged_kv_gather.cu b/qwen3_6_scripts/corex_paged_kv_gather.cu new file mode 100644 index 0000000..e208727 --- /dev/null +++ b/qwen3_6_scripts/corex_paged_kv_gather.cu @@ -0,0 +1,118 @@ +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +constexpr int kThreads = 256; +constexpr int kSmallGridBlocks = 256; +constexpr int kSmallGridMaxSeqLen = 96 * 1024; + +__global__ void paged_kv_gather_kernel( + const __half* key_cache, const __half* value_cache, + const int* block_table, float* key_output, float* value_output, + int seq_len, int num_kv_heads, int head_size, int block_size, + int key_pack) { + const int64_t total = + static_cast(seq_len) * num_kv_heads * head_size; + for (int64_t index = + static_cast(blockIdx.x) * blockDim.x + threadIdx.x; + index < total; + index += static_cast(blockDim.x) * gridDim.x) { + const int dim = index % head_size; + const int token = (index / head_size) % seq_len; + const int kv_head = index / (static_cast(head_size) * seq_len); + const int logical_block = token / block_size; + const int block_offset = token % block_size; + const int physical_block = block_table[logical_block]; + + const int64_t key_index = + (((static_cast(physical_block) * num_kv_heads + kv_head) + * (head_size / key_pack) + dim / key_pack) + * block_size + block_offset) * key_pack + dim % key_pack; + const int64_t value_index = + ((static_cast(physical_block) * num_kv_heads + kv_head) + * head_size + dim) * block_size + block_offset; + const int64_t key_output_index = + (static_cast(kv_head) * head_size + dim) * seq_len + token; + const int64_t value_output_index = + (static_cast(kv_head) * seq_len + token) * head_size + dim; + + key_output[key_output_index] = __half2float(key_cache[key_index]); + value_output[value_output_index] = __half2float(value_cache[value_index]); + } +} + +void check_half_cuda_contiguous(const torch::Tensor& tensor, + const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); +} + +} // namespace + +std::vector gather_paged_kv( + const torch::Tensor& key_cache, const torch::Tensor& value_cache, + const torch::Tensor& block_table, int64_t seq_len) { + check_half_cuda_contiguous(key_cache, "key_cache"); + check_half_cuda_contiguous(value_cache, "value_cache"); + TORCH_CHECK(block_table.is_cuda(), "block_table must be a CUDA tensor"); + TORCH_CHECK(block_table.scalar_type() == torch::kInt32, + "block_table must have dtype int32"); + TORCH_CHECK(block_table.is_contiguous(), "block_table must be contiguous"); + TORCH_CHECK(key_cache.dim() == 5, + "key_cache must have shape (blocks, kv_heads, d/x, block, x)"); + TORCH_CHECK(value_cache.dim() == 4, + "value_cache must have shape (blocks, kv_heads, d, block)"); + TORCH_CHECK(block_table.dim() == 1, + "block_table must be a one-dimensional row"); + TORCH_CHECK(key_cache.size(0) == value_cache.size(0), + "key/value block counts differ"); + TORCH_CHECK(key_cache.size(1) == value_cache.size(1), + "key/value KV-head counts differ"); + TORCH_CHECK(key_cache.size(3) == value_cache.size(3), + "key/value block sizes differ"); + TORCH_CHECK(key_cache.size(2) * key_cache.size(4) == value_cache.size(2), + "key/value head sizes differ"); + TORCH_CHECK(seq_len > 0, "seq_len must be positive"); + + const int block_size = static_cast(value_cache.size(3)); + const int64_t required_blocks = (seq_len + block_size - 1) / block_size; + TORCH_CHECK(required_blocks <= block_table.numel(), + "block_table is too short for seq_len"); + const int num_kv_heads = static_cast(value_cache.size(1)); + const int head_size = static_cast(value_cache.size(2)); + const int key_pack = static_cast(key_cache.size(4)); + + auto output_options = key_cache.options().dtype(torch::kFloat32); + auto key_output = torch::empty( + {num_kv_heads, head_size, seq_len}, output_options); + auto value_output = torch::empty( + {num_kv_heads, seq_len, head_size}, output_options); + const int64_t total = seq_len * num_kv_heads * head_size; + const int grid_cap = + seq_len <= kSmallGridMaxSeqLen ? kSmallGridBlocks : 65535; + const int blocks = static_cast(std::min( + (total + kThreads - 1) / kThreads, grid_cap)); + paged_kv_gather_kernel<<>>( + reinterpret_cast(key_cache.data_ptr()), + reinterpret_cast(value_cache.data_ptr()), + block_table.data_ptr(), key_output.data_ptr(), + value_output.data_ptr(), static_cast(seq_len), + num_kv_heads, head_size, block_size, key_pack); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return {key_output, value_output}; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("gather", &gather_paged_kv, + "Gather paged FP16 K/V directly into FP32 attention layouts"); +} diff --git a/qwen3_6_scripts/corex_query_tiled_paged_prefill.cu b/qwen3_6_scripts/corex_query_tiled_paged_prefill.cu new file mode 100644 index 0000000..e94624b --- /dev/null +++ b/qwen3_6_scripts/corex_query_tiled_paged_prefill.cu @@ -0,0 +1,503 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace { + +constexpr int kBlockSize = 16; +constexpr int kHeadDim = 256; +constexpr int kKeyPack = 8; +constexpr int kNumQueryHeads = 4; +constexpr int kNumKvHeads = 1; +constexpr int kQueryTile = 16; +constexpr int kKeyTile = 16; +constexpr int kReductionTokens = 512; +constexpr int kKeyTilesPerReduction = kReductionTokens / kKeyTile; +constexpr int kPvReductionSplits = 4; +constexpr int kKeyTilesPerPvSplit = + kKeyTilesPerReduction / kPvReductionSplits; +constexpr int kMmaK = 16; +constexpr int kDimTiles = kHeadDim / kMmaK; +constexpr int kWarpSize = 64; +constexpr int kMaxQueryTokens = 8192; +constexpr int kMaxSequenceTokens = 262144; + +using namespace nvcuda; + +struct __align__(128) SharedStorage { + float matrix_tile[kQueryTile * kKeyTile]; + float scores[ + kKeyTilesPerReduction * kQueryTile * kKeyTile]; + float running_output[kQueryTile * kHeadDim]; + float partial_output[ + kPvReductionSplits * kQueryTile * kMmaK]; + float running_max[kQueryTile]; + float running_sum[kQueryTile]; + float correction[kQueryTile]; +}; + +void check_half_cuda_contiguous(const torch::Tensor& tensor, + const char* name) { + TORCH_CHECK(tensor.is_cuda(), name, " must be a CUDA tensor"); + TORCH_CHECK(tensor.scalar_type() == torch::kFloat16, + name, " must have dtype float16"); + TORCH_CHECK(tensor.is_contiguous(), name, " must be contiguous"); +} + +__device__ __forceinline__ float load_key( + const __half* key_new, const __half* key_cache, + const int* block_table, int logical_token, int context_len, int dim) { + if (logical_token < context_len) { + const int logical_block = logical_token / kBlockSize; + const int block_offset = logical_token % kBlockSize; + const int physical_block = block_table[logical_block]; + const int64_t index = + ((((static_cast(physical_block) * kNumKvHeads) + * (kHeadDim / kKeyPack) + dim / kKeyPack) + * kBlockSize + block_offset) * kKeyPack + dim % kKeyPack); + return __half2float(key_cache[index]); + } + const int query_index = logical_token - context_len; + return __half2float( + key_new[static_cast(query_index) * kHeadDim + dim]); +} + +__device__ __forceinline__ float load_value( + const __half* value_new, const __half* value_cache, + const int* block_table, int logical_token, int context_len, int dim) { + if (logical_token < context_len) { + const int logical_block = logical_token / kBlockSize; + const int block_offset = logical_token % kBlockSize; + const int physical_block = block_table[logical_block]; + const int64_t index = + ((static_cast(physical_block) * kNumKvHeads) + * kHeadDim + dim) * kBlockSize + block_offset; + return __half2float(value_cache[index]); + } + const int query_index = logical_token - context_len; + return __half2float( + value_new[static_cast(query_index) * kHeadDim + dim]); +} + +__global__ void query_tiled_paged_prefill_kernel( + const __half* query, const __half* key_new, const __half* value_new, + const __half* key_cache, const __half* value_cache, + const int* block_table, __half* output, float* lse, + int context_len, int query_len, float scale) { + __shared__ SharedStorage shared; + + const int lane = threadIdx.x; + const int query_tile_index = blockIdx.x / kNumQueryHeads; + const int query_head = blockIdx.x % kNumQueryHeads; + const int query_start = query_tile_index * kQueryTile; + const int active_rows = min(kQueryTile, query_len - query_start); + if (active_rows <= 0) { + return; + } + + wmma::fragment query_fragments[kDimTiles]; + +#pragma unroll + for (int dim_tile = 0; dim_tile < kDimTiles; ++dim_tile) { +#pragma unroll + for (int quarter = 0; quarter < 4; ++quarter) { + const int row = lane / 16 + quarter * 4; + const int column = lane % 16; + float value = 0.0f; + if (row < active_rows) { + const int query_index = query_start + row; + const int dim = dim_tile * kMmaK + column; + const int64_t source = + (static_cast(query_index) * kNumQueryHeads + + query_head) * kHeadDim + dim; + value = __half2float(query[source]) * scale; + } + const int offset = + wmma::CoordToOffset<32, wmma::layout_t::mem_row_major>( + row, column); + shared.matrix_tile[offset] = value; + } + __syncthreads(); + wmma::load_matrix_sync( + query_fragments[dim_tile], shared.matrix_tile, 0); + __syncthreads(); + } + + for (int index = lane; index < kQueryTile * kHeadDim; + index += kWarpSize) { + shared.running_output[index] = 0.0f; + } + if (lane < kQueryTile) { + shared.running_max[lane] = -std::numeric_limits::infinity(); + shared.running_sum[lane] = 0.0f; + shared.correction[lane] = 1.0f; + } + __syncthreads(); + + const int last_query = min(query_start + kQueryTile, query_len); + + // Preserve the installed reference's 512-token reduction boundaries: + // paged context and current causal K/V are separate phases. + for (int phase = 0; phase < 2; ++phase) { + const int phase_base = phase == 0 ? 0 : context_len; + const int phase_tokens = phase == 0 ? context_len : last_query; + for (int group_start = 0; group_start < phase_tokens; + group_start += kReductionTokens) { + const int group_tokens = + min(kReductionTokens, phase_tokens - group_start); + const int group_key_tiles = + (group_tokens + kKeyTile - 1) / kKeyTile; + + for (int key_tile_in_group = 0; + key_tile_in_group < group_key_tiles; + ++key_tile_in_group) { + const int local_key_start = + group_start + key_tile_in_group * kKeyTile; + const int logical_key_start = phase_base + local_key_start; + wmma::fragment + score_fragment; + wmma::fill_fragment(score_fragment, 0.0f); + +#pragma unroll + for (int dim_tile = 0; dim_tile < kDimTiles; ++dim_tile) { +#pragma unroll + for (int quarter = 0; quarter < 4; ++quarter) { + const int row = lane / 16 + quarter * 4; + const int column = lane % 16; + const int logical_token = logical_key_start + column; + const int dim = dim_tile * kMmaK + row; + const float value = + local_key_start + column < phase_tokens + ? load_key(key_new, key_cache, block_table, + logical_token, context_len, dim) + : 0.0f; + const int offset = + wmma::CoordToOffset< + 32, wmma::layout_t::mem_col_major>( + row, column); + shared.matrix_tile[offset] = value; + } + __syncthreads(); + wmma::fragment key_fragment; + wmma::load_matrix_sync( + key_fragment, shared.matrix_tile, 0); + wmma::mma_sync( + score_fragment, + query_fragments[dim_tile], + key_fragment, + score_fragment); + __syncthreads(); + } + + float* score_tile = + shared.scores + + key_tile_in_group * kQueryTile * kKeyTile; + wmma::store_matrix_sync( + score_tile, score_fragment, 0, wmma::mem_row_major); + __syncthreads(); + } + + if (lane < kQueryTile) { + const int row = lane; + if (row >= active_rows) { + shared.correction[row] = 1.0f; + for (int key_offset = 0; key_offset < group_tokens; + ++key_offset) { + const int key_tile = key_offset / kKeyTile; + const int column = key_offset % kKeyTile; + shared.scores[ + key_tile * kQueryTile * kKeyTile + + row * kKeyTile + column] = 0.0f; + } + } else { + const int absolute_query = + context_len + query_start + row; + float block_max = + -std::numeric_limits::infinity(); + for (int key_offset = 0; key_offset < group_tokens; + ++key_offset) { + const int key_tile = key_offset / kKeyTile; + const int column = key_offset % kKeyTile; + const int score_index = + key_tile * kQueryTile * kKeyTile + + row * kKeyTile + column; + const int logical_key = + phase_base + group_start + key_offset; + if (logical_key <= absolute_query) { + block_max = fmaxf( + block_max, shared.scores[score_index]); + } else { + shared.scores[score_index] = + -std::numeric_limits::infinity(); + } + } + const float old_max = shared.running_max[row]; + const float new_max = fmaxf(old_max, block_max); + const float correction = + old_max == -std::numeric_limits::infinity() + ? 0.0f + : expf(old_max - new_max); + float group_sum = 0.0f; + for (int key_offset = 0; key_offset < group_tokens; + ++key_offset) { + const int key_tile = key_offset / kKeyTile; + const int column = key_offset % kKeyTile; + const int score_index = + key_tile * kQueryTile * kKeyTile + + row * kKeyTile + column; + const float score = shared.scores[score_index]; + const float probability = + score == -std::numeric_limits::infinity() + ? 0.0f + : expf(score - new_max); + shared.scores[score_index] = probability; + group_sum += probability; + } + shared.running_sum[row] = + shared.running_sum[row] * correction + group_sum; + shared.running_max[row] = new_max; + shared.correction[row] = correction; + } + for (int key_offset = group_tokens; + key_offset < group_key_tiles * kKeyTile; + ++key_offset) { + const int key_tile = key_offset / kKeyTile; + const int column = key_offset % kKeyTile; + shared.scores[ + key_tile * kQueryTile * kKeyTile + + row * kKeyTile + column] = 0.0f; + } + } + __syncthreads(); + + for (int index = lane; + index < active_rows * kHeadDim; + index += kWarpSize) { + const int row = index / kHeadDim; + shared.running_output[index] *= shared.correction[row]; + } + __syncthreads(); + +#pragma unroll + for (int dim_tile = 0; dim_tile < kDimTiles; ++dim_tile) { + // CoreX's reference matmul reduces a 512-token K dimension + // hierarchically. Preserve that numerical shape with four fixed, + // contiguous 128-token partials and a deterministic binary merge. +#pragma unroll + for (int split = 0; split < kPvReductionSplits; ++split) { + wmma::fragment + output_fragment; + wmma::fill_fragment(output_fragment, 0.0f); + const int split_start = split * kKeyTilesPerPvSplit; + const int split_end = + min(group_key_tiles, split_start + kKeyTilesPerPvSplit); + for (int key_tile_in_group = split_start; + key_tile_in_group < split_end; + ++key_tile_in_group) { + const int local_key_start = + group_start + key_tile_in_group * kKeyTile; + const int logical_key_start = phase_base + local_key_start; + const float* score_tile = + shared.scores + + key_tile_in_group * kQueryTile * kKeyTile; + wmma::fragment probability_fragment; + wmma::load_matrix_sync( + probability_fragment, score_tile, 0); + +#pragma unroll + for (int quarter = 0; quarter < 4; ++quarter) { + const int row = lane / 16 + quarter * 4; + const int column = lane % 16; + const int logical_token = logical_key_start + row; + const int dim = dim_tile * kMmaK + column; + const float value = + local_key_start + row < phase_tokens + ? load_value(value_new, value_cache, block_table, + logical_token, context_len, dim) + : 0.0f; + const int offset = + wmma::CoordToOffset< + 32, wmma::layout_t::mem_row_major>( + row, column); + shared.matrix_tile[offset] = value; + } + __syncthreads(); + + wmma::fragment value_fragment; + wmma::load_matrix_sync( + value_fragment, shared.matrix_tile, 0); + wmma::mma_sync( + output_fragment, + probability_fragment, + value_fragment, + output_fragment); + __syncthreads(); + } + + wmma::store_matrix_sync( + shared.partial_output + + split * kQueryTile * kMmaK, + output_fragment, + 0, + wmma::mem_row_major); + __syncthreads(); + } + +#pragma unroll + for (int quarter = 0; quarter < 4; ++quarter) { + const int row = lane / 16 + quarter * 4; + const int column = lane % 16; + if (row < active_rows) { + const int output_index = + row * kHeadDim + dim_tile * kMmaK + column; + const int tile_index = row * kMmaK + column; + const int partial_stride = kQueryTile * kMmaK; + const float left = __fadd_rn( + shared.partial_output[tile_index], + shared.partial_output[partial_stride + tile_index]); + const float right = __fadd_rn( + shared.partial_output[2 * partial_stride + tile_index], + shared.partial_output[3 * partial_stride + tile_index]); + shared.running_output[output_index] = __fadd_rn( + shared.running_output[output_index], + __fadd_rn(left, right)); + } + } + __syncthreads(); + } + } + } + + for (int index = lane; index < active_rows * kHeadDim; + index += kWarpSize) { + const int row = index / kHeadDim; + const int dim = index % kHeadDim; + const int query_index = query_start + row; + const int64_t destination = + (static_cast(query_index) * kNumQueryHeads + + query_head) * kHeadDim + dim; + output[destination] = __float2half_rn( + shared.running_output[index] / shared.running_sum[row]); + } + if (lane < active_rows) { + const int query_index = query_start + lane; + lse[static_cast(query_index) * kNumQueryHeads + + query_head] = + shared.running_max[lane] + logf(shared.running_sum[lane]); + } +} + +} // namespace + +std::vector query_tiled_paged_prefill_forward( + const torch::Tensor& query, const torch::Tensor& key_new, + const torch::Tensor& value_new, const torch::Tensor& key_cache, + const torch::Tensor& value_cache, const torch::Tensor& block_table, + int64_t context_len_arg, double scale_arg) { + check_half_cuda_contiguous(query, "query"); + check_half_cuda_contiguous(key_new, "key_new"); + check_half_cuda_contiguous(value_new, "value_new"); + check_half_cuda_contiguous(key_cache, "key_cache"); + check_half_cuda_contiguous(value_cache, "value_cache"); + TORCH_CHECK(block_table.is_cuda(), + "block_table must be a CUDA tensor"); + TORCH_CHECK(block_table.scalar_type() == torch::kInt32, + "block_table must have dtype int32"); + TORCH_CHECK(block_table.is_contiguous(), + "block_table must be contiguous"); + TORCH_CHECK(block_table.dim() == 1, + "block_table must be one-dimensional"); + TORCH_CHECK(query.dim() == 3 && query.size(1) == kNumQueryHeads + && query.size(2) == kHeadDim, + "query must have shape (Q, 4, 256)"); + TORCH_CHECK(key_new.dim() == 3 && key_new.size(1) == kNumKvHeads + && key_new.size(2) == kHeadDim, + "key_new must have shape (Q, 1, 256)"); + TORCH_CHECK(value_new.sizes() == key_new.sizes(), + "value_new must match key_new"); + TORCH_CHECK(key_new.size(0) == query.size(0), + "query, key_new, and value_new lengths must match"); + TORCH_CHECK(key_cache.dim() == 5 + && key_cache.size(1) == kNumKvHeads + && key_cache.size(2) == kHeadDim / kKeyPack + && key_cache.size(3) == kBlockSize + && key_cache.size(4) == kKeyPack, + "key_cache must have shape (N, 1, 32, 16, 8)"); + TORCH_CHECK(value_cache.dim() == 4 + && value_cache.size(1) == kNumKvHeads + && value_cache.size(2) == kHeadDim + && value_cache.size(3) == kBlockSize, + "value_cache must have shape (N, 1, 256, 16)"); + TORCH_CHECK(key_cache.size(0) == value_cache.size(0), + "key/value cache block counts must match"); + TORCH_CHECK(query.device() == key_new.device() + && query.device() == value_new.device() + && query.device() == key_cache.device() + && query.device() == value_cache.device() + && query.device() == block_table.device(), + "all tensors must use the same device"); + TORCH_CHECK(context_len_arg >= 0 + && context_len_arg <= kMaxSequenceTokens, + "context_len is out of range"); + TORCH_CHECK(context_len_arg % kBlockSize == 0, + "context_len must be block aligned"); + const int query_len = static_cast(query.size(0)); + const int context_len = static_cast(context_len_arg); + TORCH_CHECK(query_len > 0 && query_len <= kMaxQueryTokens, + "query length must be in [1, 8192]"); + TORCH_CHECK(context_len + query_len <= kMaxSequenceTokens, + "context_len + query_len exceeds 262144"); + const int required_blocks = context_len / kBlockSize; + TORCH_CHECK(block_table.numel() >= required_blocks, + "block_table is too short for context_len"); + if (required_blocks > 0) { + auto active_blocks = block_table.narrow(0, 0, required_blocks); + const int minimum_block = active_blocks.min().item(); + const int maximum_block = active_blocks.max().item(); + TORCH_CHECK(minimum_block >= 0 + && maximum_block < key_cache.size(0), + "block_table contains an out-of-range physical block ID"); + } + TORCH_CHECK(std::isfinite(scale_arg) && scale_arg > 0.0, + "scale must be finite and positive"); + + auto output = torch::empty_like(query); + auto lse = torch::empty( + {query_len, kNumQueryHeads}, + query.options().dtype(torch::kFloat32)); + const int query_tiles = + (query_len + kQueryTile - 1) / kQueryTile; + const int blocks = query_tiles * kNumQueryHeads; + auto stream = at::cuda::getCurrentCUDAStream(); + query_tiled_paged_prefill_kernel<<< + blocks, kWarpSize, 0, stream>>>( + reinterpret_cast(query.data_ptr()), + reinterpret_cast(key_new.data_ptr()), + reinterpret_cast(value_new.data_ptr()), + reinterpret_cast(key_cache.data_ptr()), + reinterpret_cast(value_cache.data_ptr()), + block_table.data_ptr(), + reinterpret_cast<__half*>(output.data_ptr()), + lse.data_ptr(), context_len, query_len, + static_cast(scale_arg)); + C10_CUDA_KERNEL_LAUNCH_CHECK(); + return {output, lse}; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { + module.def("forward", &query_tiled_paged_prefill_forward, + "Fixed BI100 query-tiled paged-prefill forward"); +} diff --git a/qwen3_6_scripts/gdn_prefix.py b/qwen3_6_scripts/gdn_prefix.py new file mode 100644 index 0000000..3eccfe1 --- /dev/null +++ b/qwen3_6_scripts/gdn_prefix.py @@ -0,0 +1,291 @@ +"""Shared GDN prefix-state cache contracts for the BI100 runtime.""" + +from __future__ import annotations + +import os +from collections import OrderedDict +from dataclasses import dataclass +from typing import Iterable, List, Optional, Sequence, Tuple + + +GdnPrefixKey = Tuple[int, bytes] +GdnCapturePoint = Tuple[int, GdnPrefixKey] + +_VALID_POLICIES = {"fine32", "admission64", "off"} +GDN_KERNEL_CHUNK_TOKENS = 64 +GDN_DIRECT_MIN_REPLAY_TOKENS = 2 + +_VALID_RESTORE_MODES = {"direct", "hybrid64", "chunk64", "aligned"} + + +def _env_choice(name: str, default: str, choices: set[str]) -> str: + value = os.getenv(name, default).strip().lower() + if value not in choices: + allowed = ", ".join(sorted(choices)) + raise RuntimeError(f"invalid {name}={value!r}; expected one of: {allowed}") + return value + + +def gdn_cache_policy_from_env() -> str: + return _env_choice("BI100_GDN_CACHE_POLICY", "fine32", _VALID_POLICIES) + + +def gdn_restore_mode_from_env() -> str: + return _env_choice( + "BI100_GDN_RESTORE_MODE", "direct", _VALID_RESTORE_MODES) + + +def gdn_restore_alignment(restore_mode: str, block_size: int, + scheduler_chunk_tokens: int) -> int: + """Return the content boundary required by a restore mode.""" + if block_size <= 0: + raise ValueError("block_size must be positive") + if restore_mode == "direct": + return block_size + if restore_mode in {"hybrid64", "chunk64"}: + alignment = GDN_KERNEL_CHUNK_TOKENS + elif restore_mode == "aligned": + alignment = scheduler_chunk_tokens + else: + raise ValueError(f"unknown GDN restore mode: {restore_mode}") + if alignment <= 0 or alignment % block_size != 0: + raise ValueError( + f"{restore_mode} GDN restore requires a positive alignment " + f"divisible by block_size={block_size}; got {alignment}") + return alignment + + +def make_prefix_key(block_count: int, digest: bytes) -> GdnPrefixKey: + if block_count <= 0: + raise ValueError("GDN prefix key requires at least one complete block") + if not isinstance(digest, bytes) or len(digest) != 32: + raise ValueError("GDN prefix digest must be exactly 32 bytes") + return block_count, digest + + +def keys_from_block_hashes(block_hashes: Sequence[bytes]) -> List[GdnPrefixKey]: + return [make_prefix_key(i + 1, digest) + for i, digest in enumerate(block_hashes)] + + +def strict_prefix_block_count(token_count: int, block_size: int) -> int: + if block_size <= 0: + raise ValueError("block_size must be positive") + if token_count <= 1: + return 0 + return (token_count - 1) // block_size + + +def key_at_strict_boundary(block_hashes: Sequence[bytes], token_count: int, + block_size: int) -> Optional[GdnPrefixKey]: + block_count = min( + len(block_hashes), strict_prefix_block_count(token_count, block_size)) + if block_count <= 0: + return None + return make_prefix_key(block_count, block_hashes[block_count - 1]) + + +def final_capture_key( + block_hashes: Sequence[bytes], prompt_tokens: int, block_size: int, + restore_mode: str, replay_alignment: int) -> Optional[GdnPrefixKey]: + if restore_mode in {"direct", "hybrid64"}: + block_count = min( + len(block_hashes), strict_prefix_block_count( + prompt_tokens, block_size)) + if (block_count > 0 + and prompt_tokens - block_count * block_size + < GDN_DIRECT_MIN_REPLAY_TOKENS): + block_count -= 1 + if block_count <= 0: + return None + return make_prefix_key(block_count, block_hashes[block_count - 1]) + if restore_mode not in {"chunk64", "aligned"}: + raise ValueError(f"unknown GDN restore mode: {restore_mode}") + if (replay_alignment <= 0 or replay_alignment % block_size != 0 + or prompt_tokens <= 1): + return None + boundary_tokens = ((prompt_tokens - 1) // replay_alignment + * replay_alignment) + block_count = min(len(block_hashes), boundary_tokens // block_size) + if block_count <= 0: + return None + return make_prefix_key(block_count, block_hashes[block_count - 1]) + + +def restore_key_is_eligible( + key: GdnPrefixKey, prompt_tokens: int, block_size: int, + restore_mode: str, replay_alignment: int, + direct_final_key: Optional[GdnPrefixKey] = None) -> bool: + """Return whether restoring ``key`` preserves the execution contract.""" + make_prefix_key(*key) + if block_size <= 0: + raise ValueError("block_size must be positive") + boundary_tokens = key[0] * block_size + remaining_tokens = prompt_tokens - boundary_tokens + if remaining_tokens <= 0: + return False + if restore_mode == "direct": + return remaining_tokens >= GDN_DIRECT_MIN_REPLAY_TOKENS + if restore_mode == "hybrid64": + if direct_final_key is not None: + make_prefix_key(*direct_final_key) + return (remaining_tokens >= GDN_DIRECT_MIN_REPLAY_TOKENS + and replay_alignment > 0 + and (boundary_tokens % replay_alignment == 0 + or key == direct_final_key)) + if restore_mode not in {"chunk64", "aligned"}: + raise ValueError(f"unknown GDN restore mode: {restore_mode}") + return (replay_alignment > 0 + and boundary_tokens % replay_alignment == 0) + + +def capture_points_for_step( + targets: Iterable[GdnPrefixKey], physical_context_tokens: int, + logical_end_tokens: int, block_size: int) -> Tuple[GdnCapturePoint, ...]: + if physical_context_tokens < 0 or logical_end_tokens < 0: + raise ValueError("token positions must be non-negative") + if logical_end_tokens <= physical_context_tokens: + return () + selected = {} + for key in targets: + make_prefix_key(*key) + boundary_tokens = key[0] * block_size + if physical_context_tokens < boundary_tokens <= logical_end_tokens: + selected[boundary_tokens - physical_context_tokens] = key + points = tuple(sorted(selected.items())) + if len(points) > 2: + raise ValueError("at most two GDN capture points are allowed per step") + return points + + +def cap_prefill_end_at_capture_boundary( + logical_start_tokens: int, logical_end_tokens: int, + targets: Iterable[GdnPrefixKey], block_size: int) -> int: + """Stop a physical prefill step at its earliest pending capture boundary.""" + if logical_start_tokens < 0 or logical_end_tokens < 0: + raise ValueError("token positions must be non-negative") + if logical_end_tokens < logical_start_tokens: + raise ValueError("logical end must not precede logical start") + if block_size <= 0: + raise ValueError("block_size must be positive") + + capped_end = logical_end_tokens + for key in targets: + make_prefix_key(*key) + boundary_tokens = key[0] * block_size + if logical_start_tokens < boundary_tokens < capped_end: + capped_end = boundary_tokens + return capped_end + + +def canonical_direct_segment_offsets( + block_hashes: Sequence[bytes], physical_context_tokens: int, + logical_end_tokens: int, block_size: int, + scheduler_chunk_tokens: int) -> Tuple[int, ...]: + """Reproduce cold fine32/direct segment boundaries after fast-forward.""" + if physical_context_tokens < 0 or logical_end_tokens < 0: + raise ValueError("token positions must be non-negative") + if block_size <= 0 or scheduler_chunk_tokens <= 0: + raise ValueError("block and scheduler chunk sizes must be positive") + if scheduler_chunk_tokens % block_size != 0: + raise ValueError("scheduler chunk size must be divisible by block size") + if logical_end_tokens <= physical_context_tokens: + return () + + boundaries = set() + step_ends = list(range(scheduler_chunk_tokens, logical_end_tokens, + scheduler_chunk_tokens)) + for step_end in (*step_ends, logical_end_tokens): + key = final_capture_key(block_hashes, step_end, block_size, + "direct", block_size) + if key is not None: + boundaries.add(key[0] * block_size) + boundaries.update(step_ends) + return tuple( + boundary - physical_context_tokens + for boundary in sorted(boundaries) + if physical_context_tokens < boundary < logical_end_tokens) + + +@dataclass(frozen=True) +class GdnCachePlan: + restore_key: Optional[GdnPrefixKey] = None + capture_points: Tuple[GdnCapturePoint, ...] = () + evict_keys: Tuple[GdnPrefixKey, ...] = () + + +class GdnPrefixStatePolicy: + """Scheduler-owned state index with deterministic worker actions.""" + + def __init__(self, policy: str) -> None: + if policy not in _VALID_POLICIES: + raise ValueError(f"unknown GDN cache policy: {policy}") + self.policy = policy + self.capacity = {"fine32": 32, "admission64": 64, "off": 0}[policy] + self._resident: OrderedDict[GdnPrefixKey, None] = OrderedDict() + + def __len__(self) -> int: + return len(self._resident) + + def resident_keys(self) -> Tuple[GdnPrefixKey, ...]: + return tuple(self._resident) + + def contains(self, key: GdnPrefixKey) -> bool: + return key in self._resident + + def should_capture_final(self, key: GdnPrefixKey) -> bool: + """Return whether a final state must be materialized on this request.""" + make_prefix_key(*key) + if self.policy == "off": + return False + if self.policy == "admission64": + return key not in self._resident + return True + + def select_restore( + self, live_prefix_keys: Sequence[GdnPrefixKey], + max_blocks: int) -> Optional[GdnPrefixKey]: + if self.capacity == 0 or max_blocks <= 0: + return None + best = None + for key in live_prefix_keys[:max_blocks]: + if key in self._resident: + best = key + if best is not None: + self._resident.move_to_end(best) + return best + + def repeated_branch_candidate( + self, live_prefix_keys: Sequence[GdnPrefixKey], + max_blocks: int) -> Optional[GdnPrefixKey]: + """Return a repeated raw-KV branch that lacks recurrent state. + + A live KV hit proves that the content occurred in an earlier request; + the current request is therefore the second or later occurrence. + """ + if (self.policy != "admission64" or max_blocks <= 0 + or not live_prefix_keys): + return None + candidate = live_prefix_keys[min(len(live_prefix_keys), max_blocks) - 1] + if candidate in self._resident: + return None + return candidate + + def admit(self, keys: Iterable[GdnPrefixKey]) -> Tuple[GdnPrefixKey, ...]: + evicted: List[GdnPrefixKey] = [] + if self.capacity == 0: + return () + for key in keys: + make_prefix_key(*key) + if key in self._resident: + self._resident.move_to_end(key) + else: + self._resident[key] = None + while len(self._resident) > self.capacity: + evicted_key, _ = self._resident.popitem(last=False) + evicted.append(evicted_key) + return tuple(evicted) + + def forget(self, keys: Iterable[GdnPrefixKey]) -> None: + for key in keys: + self._resident.pop(key, None) diff --git a/qwen3_6_scripts/install_prebuilt_corex.sh b/qwen3_6_scripts/install_prebuilt_corex.sh new file mode 100755 index 0000000..aca8052 --- /dev/null +++ b/qwen3_6_scripts/install_prebuilt_corex.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +set -euo pipefail + +VLLM_ROOT=${1:?usage: install_prebuilt_corex.sh VLLM_ROOT} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BUNDLE_DIR=${SCRIPT_DIR}/prebuilt/corex-3.2.3-ivcore10 +MANIFEST=${BUNDLE_DIR}/SHA256SUMS + +[[ -d "$VLLM_ROOT" ]] || { + printf 'vLLM root does not exist: %s\n' "$VLLM_ROOT" >&2 + exit 2 +} +[[ -f "$MANIFEST" ]] || { + printf 'prebuilt CoreX manifest is missing: %s\n' "$MANIFEST" >&2 + exit 2 +} + +mapfile -t artifacts < <(awk '{print $2}' "$MANIFEST") +[[ "${#artifacts[@]}" -eq 12 ]] || { + printf 'expected 12 prebuilt CoreX artifacts, found %s\n' \ + "${#artifacts[@]}" >&2 + exit 2 +} + +for artifact in "${artifacts[@]}"; do + [[ "$artifact" == corex_*.so && "$artifact" != */* ]] || { + printf 'invalid prebuilt artifact name: %s\n' "$artifact" >&2 + exit 2 + } +done + +( + cd "$BUNDLE_DIR" + sha256sum --strict --check SHA256SUMS +) + +for artifact in "${artifacts[@]}"; do + install -m 0755 "$BUNDLE_DIR/$artifact" "$VLLM_ROOT/$artifact" +done + +python3 - "$VLLM_ROOT" "${artifacts[@]}" <<'PY' +import pathlib +import struct +import sys + +root = pathlib.Path(sys.argv[1]) +for name in sys.argv[2:]: + path = root / name + if not path.is_file() or path.stat().st_size == 0: + raise SystemExit(f"installed CoreX extension is empty: {path}") + header = path.read_bytes()[:20] + if len(header) < 20 or header[:4] != b"\x7fELF": + raise SystemExit(f"installed CoreX extension is not ELF: {path}") + if header[4:6] != b"\x02\x01": + raise SystemExit( + f"installed CoreX extension is not 64-bit little-endian ELF: {path}") + machine = struct.unpack_from(" 0 + for cache_t in self.mamba_cache: + cache_t[:, [to_index,from_index]] = \ + cache_t[:, [from_index,to_index]] + + def _copy_mamba_cache(self, from_index: int, to_index: int): + assert len(self.mamba_cache) > 0 + for cache_t in self.mamba_cache: + cache_t[:, to_index].copy_(cache_t[:, from_index], + non_blocking=True) + + def _move_out_if_already_occupied(self, index: int, + all_occupied_indices: List[int]): + if index in all_occupied_indices: + first_free_index = self._first_free_index_in_mamba_cache() + # In case occupied, move the occupied to a new empty block + self._move_cache_index_and_mappings(from_index=index, + to_index=first_free_index) + + def _assign_seq_id_to_mamba_cache_in_specific_dest(self, cur_rid: str, + seq_id: int, + destination_index: int): + """ + Assign (req_id,seq_id) pair to a `destination_index` index, if + already occupied, move the occupying index to a free index. + """ + all_occupied_indices = self._get_all_occupied_indices() + if cur_rid not in self.mamba_cache_indices_mapping: + self._move_out_if_already_occupied( + index=destination_index, + all_occupied_indices=all_occupied_indices) + for cache_t in self.mamba_cache: + cache_t[:, destination_index].zero_() + self.mamba_cache_indices_mapping[cur_rid] = { + seq_id: destination_index + } + elif seq_id not in (seq_ids2indices := + self.mamba_cache_indices_mapping[cur_rid]): + # parallel sampling , where n > 1, assume prefill have + # already happened now we only need to copy the already + # existing cache into the siblings seq_ids caches + self._move_out_if_already_occupied( + index=destination_index, + all_occupied_indices=all_occupied_indices) + index_exists = list(seq_ids2indices.values())[0] + # case of decoding n>1, copy prefill cache to decoding indices + self._copy_mamba_cache(from_index=index_exists, + to_index=destination_index) + self.mamba_cache_indices_mapping[cur_rid][ + seq_id] = destination_index + else: + # already exists + cache_index_already_exists = self.mamba_cache_indices_mapping[ + cur_rid][seq_id] + if cache_index_already_exists != destination_index: + # In case the seq id already exists but not in + # the right destination, swap it with what's occupying it + self._swap_pair_indices_and_mappings( + from_index=cache_index_already_exists, + to_index=destination_index) + + def _prepare_current_run_mamba_cache( + self, request_ids_to_seq_ids: Dict[str, list[int]], + finished_requests_ids: List[str]): + running_indices = [] + request_ids_to_seq_ids_flatten = [ + (req_id, seq_id) + for req_id, seq_ids in request_ids_to_seq_ids.items() + for seq_id in seq_ids + ] + batch_size = len(request_ids_to_seq_ids_flatten) + for dest_index, (request_id, + seq_id) in enumerate(request_ids_to_seq_ids_flatten): + if request_id in finished_requests_ids: + # Do not allocate cache index for requests that run + # and finish right after + continue + self._assign_seq_id_to_mamba_cache_in_specific_dest( + request_id, seq_id, dest_index) + running_indices.append(dest_index) + + self._clean_up_first_bs_blocks(batch_size, running_indices) + conv_state = self.mamba_cache[0][:, :batch_size] + temporal_state = self.mamba_cache[1][:, :batch_size] + + return (conv_state, temporal_state) + + def _get_all_occupied_indices(self): + return [ + cache_idx + for seq_ids2indices in self.mamba_cache_indices_mapping.values() + for cache_idx in seq_ids2indices.values() + ] + + def _clean_up_first_bs_blocks(self, batch_size: int, + indices_for_current_run: List[int]): + # move out all of the occupied but currently not running blocks + # outside of the first n blocks + destination_indices = range(batch_size) + max_possible_batch_size = self.mamba_cache[0].shape[1] + for destination_index in destination_indices: + if destination_index in self._get_all_occupied_indices() and \ + destination_index not in indices_for_current_run: + # move not running indices outside of the batch + all_other_indices = list( + range(batch_size, max_possible_batch_size)) + first_avail_index = self._first_free_index_in_mamba_cache( + all_other_indices) + self._swap_indices(from_index=destination_index, + to_index=first_avail_index) + + def _move_cache_index_and_mappings(self, from_index: int, to_index: int): + self._copy_mamba_cache(from_index=from_index, to_index=to_index) + self._update_mapping_index(from_index=from_index, to_index=to_index) + + def _swap_pair_indices_and_mappings(self, from_index: int, to_index: int): + self._swap_mamba_cache(from_index=from_index, to_index=to_index) + self._swap_mapping_index(from_index=from_index, to_index=to_index) + + def _swap_mapping_index(self, from_index: int, to_index: int): + for seq_ids2index in self.mamba_cache_indices_mapping.values(): + for seq_id, index in seq_ids2index.items(): + if from_index == index: + seq_ids2index.update({seq_id: to_index}) + elif to_index == index: + seq_ids2index.update({seq_id: from_index}) + + def _update_mapping_index(self, from_index: int, to_index: int): + for seq_ids2index in self.mamba_cache_indices_mapping.values(): + for seq_id, index in seq_ids2index.items(): + if from_index == index: + seq_ids2index.update({seq_id: to_index}) + return + + def _release_finished_requests(self, + finished_seq_groups_req_ids: List[str]): + for req_id in finished_seq_groups_req_ids: + if req_id in self.mamba_cache_indices_mapping: + self.mamba_cache_indices_mapping.pop(req_id) + + def _first_free_index_in_mamba_cache( + self, indices_range: Optional[List[int]] = None) -> int: + assert self.mamba_cache is not None + if indices_range is None: + max_possible_batch_size = self.mamba_cache[0].shape[1] + indices_range = list(range(max_possible_batch_size)) + all_occupied_indices = self._get_all_occupied_indices() + for i in indices_range: + if i not in all_occupied_indices: + return i + raise Exception("Couldn't find a free spot in the mamba cache! This" + "should never happen") diff --git a/qwen3_6_scripts/paged_attn.py b/qwen3_6_scripts/paged_attn.py new file mode 100644 index 0000000..e7daa2f --- /dev/null +++ b/qwen3_6_scripts/paged_attn.py @@ -0,0 +1,2259 @@ +from dataclasses import dataclass +from typing import List, Optional, Tuple +import hashlib +import json +import os +from pathlib import Path +import re +import sys +import tempfile +import torch +import traceback +from vllm import _custom_ops as ops +from vllm.bi100_env import env_bool, env_int +from vllm.bi100_profile import bi100_profile_count, bi100_timer + +try: + from vllm import corex_paged_kv_gather as _corex_paged_kv_gather +except ImportError: + _corex_paged_kv_gather = None + +try: + from vllm import corex_fused_paged_prefill as _corex_fused_paged_prefill +except ImportError: + _corex_fused_paged_prefill = None + +# from vllm.attention.ops.prefix_prefill import context_attention_fwd +# NOTE: context_attention_fwd (Triton kernel from prefix_prefill.py) is NOT +# imported here. On Iluvatar BI-V100 that kernel hangs the GPU card +# permanently. Chunked-prefill / prefix-caching attention is handled by +# _forward_prefix_pytorch below (pure PyTorch, no Triton dependency). + +# Should be the same as PARTITION_SIZE in `paged_attention_v2_launcher`. +_PARTITION_SIZE = 512 +_PYTORCH_DECODE_THRESHOLD = env_int( + "BI100_PYTORCH_DECODE_THRESHOLD", 32768, 1, 262144) +_PREFIX_BLOCKS_PER_TILE = env_int( + "BI100_PREFIX_BLOCKS_PER_TILE", 32, 1, 1024) +_FORCE_PAGED_ATTN_V2 = env_bool("BI100_FORCE_PAGED_ATTN_V2", False) +_PAGED_ATTN_DIAGNOSTICS = env_bool( + "BI100_PAGED_ATTN_DIAGNOSTICS", False) +_USE_COREX_PAGED_KV_GATHER = ( + _corex_paged_kv_gather is not None + and env_bool("BI100_ATTN_COREX_PAGED_GATHER", True)) +_ENABLE_COREX_FUSED_PAGED_PREFILL = env_bool( + "BI100_ATTN_COREX_FUSED_PREFILL", False) +_FUSED_PREFILL_DIAGNOSTICS = env_bool( + "BI100_ATTN_COREX_FUSED_PREFILL_DIAGNOSTICS", False) + + +def _env_choice(name: str, default: str, choices: Tuple[str, ...]) -> str: + value = os.environ.get(name, default) + if value not in choices: + raise RuntimeError( + f"{name} must be one of {', '.join(choices)}, got {value!r}") + return value + + +_FUSED_PREFILL_SHADOW = env_bool( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW", False) +_FUSED_PREFILL_SHADOW_MAX_CALLS_PER_CONTEXT = env_int( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_MAX_CALLS_PER_CONTEXT", + 2, 1, 8) +_FUSED_PREFILL_SHADOW_NUMERIC_MODE = _env_choice( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_NUMERIC_MODE", + "legacy", + ("legacy", "calibrated"), +) +_FUSED_PREFILL_SHADOW_FAILURE_ACTION = _env_choice( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_FAILURE_ACTION", + "raise", + ("raise", "record"), +) +_USE_COREX_FUSED_PAGED_PREFILL = ( + _corex_fused_paged_prefill is not None + and _ENABLE_COREX_FUSED_PAGED_PREFILL) +_DECODE_LOG_INTERVAL = 8192 if _PAGED_ATTN_DIAGNOSTICS else 0 +_DECODE_DISPATCH_LOGGED = set() +_PREFIX_DISPATCH_LOGGED = set() +_FUSED_PREFILL_DIAGNOSTICS_LOGGED = set() +_CACHE_WRITE_LOGGED = False +_FUSED_PREFILL_SHADOW_RELATIVE_L2_LIMIT = 1.0e-5 +_FUSED_PREFILL_SHADOW_MAX_ABS_LIMIT = 1.0e-3 +_FUSED_PREFILL_SHADOW_ERROR_MULTIPLIER = 2.0 +_FUSED_PREFILL_SHADOW_RATIO_FLOOR = 1.0e-12 +_FUSED_PREFILL_SHADOW_STATE = { + "pid": None, + "records": [], +} +_ACTIVATION_CAPTURE_ENABLED = env_bool( + "BI100_ATTN_CAPTURE_REPLAY", False) +_ACTIVATION_CAPTURE_ATTESTATION = ( + "synthetic-exact-prompt-v1") +_ACTIVATION_CAPTURE_STATE = { + "pid": None, + "seen_by_bucket": {}, + "records": [], +} + + +def _parse_fused_prefill_shadow_contexts(raw: str) -> Tuple[int, ...]: + """Parse fixed lower-bound buckets used by the diagnostic shadow.""" + values = [] + for field in raw.split(","): + field = field.strip() + if not field: + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_CONTEXTS " + "contains an empty field") + try: + value = int(field) + except ValueError as exc: + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_CONTEXTS must " + "contain integers") from exc + if value < 0 or value > 262144: + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_CONTEXTS value " + "is outside [0, 262144]") + values.append(value) + if not values or values != sorted(set(values)): + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_CONTEXTS must be " + "strictly increasing and unique") + return tuple(values) + + +_FUSED_PREFILL_SHADOW_CONTEXTS = _parse_fused_prefill_shadow_contexts( + os.environ.get( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_CONTEXTS", + "49152,114688")) + + +def _validate_fused_prefill_shadow_configuration( + enabled: bool, + fused_enabled: bool, + report_dir: Optional[str], + run_id: Optional[str], +) -> Optional[Path]: + """Validate that the diagnostic cannot silently write ambiguous data.""" + if not enabled: + return None + if not fused_enabled: + raise RuntimeError( + "fused-prefill shadow requires the fused-prefill path") + if ( + _FUSED_PREFILL_SHADOW_FAILURE_ACTION == "record" + and _FUSED_PREFILL_SHADOW_NUMERIC_MODE != "calibrated" + ): + raise RuntimeError( + "record-only shadow failures require calibrated numeric mode") + if not report_dir: + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_REPORT_DIR is required") + path = Path(report_dir).expanduser() + tmp_root = Path("/tmp").resolve() + try: + path = path.resolve(strict=False) + except OSError as exc: + raise RuntimeError( + "fused-prefill shadow report directory cannot be resolved") \ + from exc + if ( + not path.is_absolute() + or path == tmp_root + or not path.is_relative_to(tmp_root) + ): + raise RuntimeError( + "fused-prefill shadow report directory must be under /tmp") + if ( + not run_id + or len(run_id) > 96 + or re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]*", run_id) is None + ): + raise RuntimeError( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_RUN_ID is invalid") + path.mkdir(mode=0o700, parents=True, exist_ok=True) + if not path.resolve(strict=True).is_relative_to(tmp_root): + raise RuntimeError( + "fused-prefill shadow report directory escaped /tmp") + try: + path.chmod(0o700) + except OSError: + pass + return path + + +_FUSED_PREFILL_SHADOW_RUN_ID = os.environ.get( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_RUN_ID") +_FUSED_PREFILL_SHADOW_REPORT_DIR = ( + _validate_fused_prefill_shadow_configuration( + _FUSED_PREFILL_SHADOW, + _USE_COREX_FUSED_PAGED_PREFILL, + os.environ.get( + "BI100_ATTN_COREX_FUSED_PREFILL_SHADOW_REPORT_DIR"), + _FUSED_PREFILL_SHADOW_RUN_ID, + )) + + +def _parse_strict_int_tuple( + raw: str, + *, + name: str, + minimum: int, + maximum: int, +) -> Tuple[int, ...]: + values = [] + for field in raw.split(","): + field = field.strip() + if not field: + raise RuntimeError(f"{name} contains an empty field") + try: + value = int(field) + except ValueError as exc: + raise RuntimeError(f"{name} must contain integers") from exc + if value < minimum or value > maximum: + raise RuntimeError( + f"{name} value is outside [{minimum}, {maximum}]") + values.append(value) + if not values or values != sorted(set(values)): + raise RuntimeError(f"{name} must be strictly increasing and unique") + return tuple(values) + + +_ACTIVATION_CAPTURE_CONTEXTS = _parse_strict_int_tuple( + os.environ.get( + "BI100_ATTN_CAPTURE_REPLAY_CONTEXTS", + "24576,57344,122880", + ), + name="BI100_ATTN_CAPTURE_REPLAY_CONTEXTS", + minimum=0, + maximum=262144, +) +_ACTIVATION_CAPTURE_CALL_ORDINALS = _parse_strict_int_tuple( + os.environ.get( + "BI100_ATTN_CAPTURE_REPLAY_CALL_ORDINALS", + "0,4,9", + ), + name="BI100_ATTN_CAPTURE_REPLAY_CALL_ORDINALS", + minimum=0, + maximum=63, +) + + +def _validate_activation_capture_configuration( + enabled: bool, + fused_enabled: bool, + report_dir: Optional[str], + run_id: Optional[str], + source_revision: Optional[str], + runtime_identity: Optional[str], + attestation: Optional[str], +) -> Optional[Path]: + if not enabled: + return None + if fused_enabled: + raise RuntimeError( + "activation capture requires the baseline PyTorch fallback") + if attestation != _ACTIVATION_CAPTURE_ATTESTATION: + raise RuntimeError( + "activation capture requires the synthetic prompt attestation") + if ( + not source_revision + or re.fullmatch(r"[0-9a-f]{40,64}", source_revision) is None + ): + raise RuntimeError( + "BI100_ATTN_CAPTURE_REPLAY_SOURCE_REVISION is invalid") + if ( + not runtime_identity + or len(runtime_identity) > 160 + or re.fullmatch( + r"[A-Za-z0-9][A-Za-z0-9_.:/-]*", runtime_identity) is None + ): + raise RuntimeError( + "BI100_ATTN_CAPTURE_REPLAY_RUNTIME_IDENTITY is invalid") + if ( + not run_id + or len(run_id) > 96 + or re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]*", run_id) is None + ): + raise RuntimeError( + "BI100_ATTN_CAPTURE_REPLAY_RUN_ID is invalid") + if not report_dir: + raise RuntimeError( + "BI100_ATTN_CAPTURE_REPLAY_DIR is required") + path = Path(report_dir).expanduser() + tmp_root = Path("/tmp").resolve() + try: + path = path.resolve(strict=False) + except OSError as exc: + raise RuntimeError( + "activation capture directory cannot be resolved") from exc + if ( + not path.is_absolute() + or path == tmp_root + or not path.is_relative_to(tmp_root) + ): + raise RuntimeError( + "activation capture directory must be under /tmp") + path.mkdir(mode=0o700, parents=True, exist_ok=True) + if not path.resolve(strict=True).is_relative_to(tmp_root): + raise RuntimeError( + "activation capture directory escaped /tmp") + try: + path.chmod(0o700) + except OSError: + pass + return path + + +_ACTIVATION_CAPTURE_RUN_ID = os.environ.get( + "BI100_ATTN_CAPTURE_REPLAY_RUN_ID") +_ACTIVATION_CAPTURE_SOURCE_REVISION = os.environ.get( + "BI100_ATTN_CAPTURE_REPLAY_SOURCE_REVISION") +_ACTIVATION_CAPTURE_RUNTIME_IDENTITY = os.environ.get( + "BI100_ATTN_CAPTURE_REPLAY_RUNTIME_IDENTITY") +_ACTIVATION_CAPTURE_DIR = _validate_activation_capture_configuration( + _ACTIVATION_CAPTURE_ENABLED, + _ENABLE_COREX_FUSED_PAGED_PREFILL, + os.environ.get("BI100_ATTN_CAPTURE_REPLAY_DIR"), + _ACTIVATION_CAPTURE_RUN_ID, + _ACTIVATION_CAPTURE_SOURCE_REVISION, + _ACTIVATION_CAPTURE_RUNTIME_IDENTITY, + os.environ.get("BI100_ATTN_CAPTURE_REPLAY_SYNTHETIC_ATTESTATION"), +) + + +def _log_corex_fused_prefill_diagnostic(stage: str, **fields) -> None: + """Emit one privacy-safe guard snapshot per stage and worker.""" + if not _FUSED_PREFILL_DIAGNOSTICS: + return + key = (os.getpid(), stage) + if key in _FUSED_PREFILL_DIAGNOSTICS_LOGGED: + return + details = " ".join(f"{name}={value}" for name, value in fields.items()) + print( + "[BI100 PAGED_ATTN] fused_prefill_guard " + f"pid={os.getpid()} rank={os.environ.get('RANK', '?')} " + f"local_rank={os.environ.get('LOCAL_RANK', '?')} stage={stage} " + f"{details}", + file=sys.stderr, + flush=True, + ) + _FUSED_PREFILL_DIAGNOSTICS_LOGGED.add(key) + + +def _fused_prefill_shadow_rank() -> int: + distributed = getattr(torch, "distributed", None) + if distributed is not None: + try: + if ( + distributed.is_available() + and distributed.is_initialized() + ): + rank = int(distributed.get_rank()) + if rank >= 0: + return rank + except (AttributeError, RuntimeError, TypeError, ValueError): + pass + for name in ("RANK", "LOCAL_RANK"): + raw = os.environ.get(name) + if raw is None: + continue + try: + value = int(raw) + except ValueError: + continue + if value >= 0: + return value + cuda = getattr(torch, "cuda", None) + if cuda is not None: + try: + if cuda.is_available(): + device = int(cuda.current_device()) + if device >= 0: + return device + except (AttributeError, RuntimeError, TypeError, ValueError): + pass + return -1 + + +def _activation_capture_process_state() -> dict: + pid = os.getpid() + if _ACTIVATION_CAPTURE_STATE["pid"] != pid: + _ACTIVATION_CAPTURE_STATE["pid"] = pid + _ACTIVATION_CAPTURE_STATE["seen_by_bucket"] = {} + _ACTIVATION_CAPTURE_STATE["records"] = [] + return _ACTIVATION_CAPTURE_STATE + + +def _activation_capture_bucket(context_tokens: int) -> Optional[int]: + for index, lower_bound in enumerate(_ACTIVATION_CAPTURE_CONTEXTS): + upper_bound = ( + _ACTIVATION_CAPTURE_CONTEXTS[index + 1] + if index + 1 < len(_ACTIVATION_CAPTURE_CONTEXTS) + else 262145 + ) + if lower_bound <= context_tokens < upper_bound: + return lower_bound + return None + + +def _atomic_write_activation_manifest(records: list) -> None: + if _ACTIVATION_CAPTURE_DIR is None: + raise RuntimeError("activation capture directory is unset") + rank = _fused_prefill_shadow_rank() + if rank < 0: + raise RuntimeError("activation capture cannot determine TP rank") + value = { + "schema": "bi100-fused-prefill-activation-bank-v1", + "version": 1, + "run_id": _ACTIVATION_CAPTURE_RUN_ID, + "rank": rank, + "source_revision": _ACTIVATION_CAPTURE_SOURCE_REVISION, + "runtime_identity": _ACTIVATION_CAPTURE_RUNTIME_IDENTITY, + "producer": "baseline-pytorch-fallback", + "synthetic_prompt_attestation": ( + _ACTIVATION_CAPTURE_ATTESTATION), + "selection": { + "context_buckets": list(_ACTIVATION_CAPTURE_CONTEXTS), + "full_attention_call_ordinals": list( + _ACTIVATION_CAPTURE_CALL_ORDINALS), + }, + "record_count": len(records), + "records": records, + "privacy": { + "raw_activation_files_private": True, + "raw_activation_files_may_be_committed": False, + "contains_prompts": False, + "contains_model_outputs": False, + "contains_token_ids": False, + "contains_credentials": False, + }, + } + destination = _ACTIVATION_CAPTURE_DIR / f"rank-{rank}.manifest.json" + descriptor, temporary = tempfile.mkstemp( + prefix=f".{destination.name}.", + suffix=".tmp", + dir=destination.parent, + ) + try: + with os.fdopen(descriptor, "w", encoding="ascii") as stream: + json.dump(value, stream, ensure_ascii=True, indent=2, + sort_keys=True) + stream.write("\n") + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, destination) + finally: + try: + os.unlink(temporary) + except FileNotFoundError: + pass + + +def _reserve_activation_capture( + context_tokens: int, +) -> Optional[Tuple[int, int]]: + if not _ACTIVATION_CAPTURE_ENABLED: + return None + bucket = _activation_capture_bucket(context_tokens) + if bucket is None: + return None + state = _activation_capture_process_state() + ordinal = int(state["seen_by_bucket"].get(bucket, 0)) + state["seen_by_bucket"][bucket] = ordinal + 1 + if ordinal not in _ACTIVATION_CAPTURE_CALL_ORDINALS: + return None + return bucket, ordinal + + +def _tensor_shape_dtype(tensor: torch.Tensor) -> dict: + return { + "shape": list(tensor.shape), + "dtype": str(tensor.dtype), + } + + +def _sha256_file(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def _capture_fused_prefill_activation( + reservation: Tuple[int, int], + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + active_block_table: torch.Tensor, + context_tokens: int, + scale: float, +) -> None: + if _ACTIVATION_CAPTURE_DIR is None: + raise RuntimeError("activation capture directory is unset") + bucket, ordinal = reservation + rank = _fused_prefill_shadow_rank() + if rank < 0: + raise RuntimeError("activation capture cannot determine TP rank") + + active_ids = [ + int(value) for value in active_block_table.detach().cpu().tolist() + ] + identity_to_compact = {} + unique_ids = [] + compact_table = [] + for physical_id in active_ids: + if physical_id < 0 or physical_id >= key_cache.shape[0]: + raise RuntimeError( + "activation capture block table is outside the KV cache") + compact_id = identity_to_compact.get(physical_id) + if compact_id is None: + compact_id = len(unique_ids) + identity_to_compact[physical_id] = compact_id + unique_ids.append(physical_id) + compact_table.append(compact_id) + + if unique_ids: + physical = torch.tensor( + unique_ids, + dtype=torch.long, + device=key_cache.device, + ) + compact_key_cache = ( + key_cache.index_select(0, physical).detach().cpu().contiguous()) + compact_value_cache = ( + value_cache.index_select(0, physical).detach().cpu().contiguous()) + else: + compact_key_cache = key_cache[:0].detach().cpu().contiguous() + compact_value_cache = value_cache[:0].detach().cpu().contiguous() + compact_block_table = torch.tensor( + compact_table, + dtype=torch.int32, + ) + tensors = { + "query": query.detach().cpu().contiguous(), + "key": key.detach().cpu().contiguous(), + "value": value.detach().cpu().contiguous(), + "key_cache": compact_key_cache, + "value_cache": compact_value_cache, + "block_table": compact_block_table, + } + filename = ( + f"rank-{rank}.bucket-{bucket}.ordinal-{ordinal}." + f"ctx-{context_tokens}.q-{query.shape[0]}.pt" + ) + destination = _ACTIVATION_CAPTURE_DIR / filename + descriptor, temporary = tempfile.mkstemp( + prefix=f".{filename}.", suffix=".tmp", + dir=destination.parent) + os.close(descriptor) + try: + torch.save({ + "schema": "bi100-fused-prefill-activation-case-v1", + "version": 1, + "context_tokens": context_tokens, + "scale": float(scale), + "rank": rank, + "bucket": bucket, + "call_ordinal": ordinal, + "tensors": tensors, + }, temporary) + os.chmod(temporary, 0o600) + with open(temporary, "rb") as stream: + os.fsync(stream.fileno()) + os.replace(temporary, destination) + finally: + try: + os.unlink(temporary) + except FileNotFoundError: + pass + + state = _activation_capture_process_state() + state["records"].append({ + "bucket_min_context_tokens": bucket, + "call_ordinal": ordinal, + "context_tokens": context_tokens, + "query_length": int(query.shape[0]), + "file": filename, + "sha256": _sha256_file(destination), + "size_bytes": destination.stat().st_size, + "compact_physical_blocks": len(unique_ids), + "logical_blocks": len(compact_table), + "tensors": { + name: _tensor_shape_dtype(tensor) + for name, tensor in tensors.items() + }, + }) + _atomic_write_activation_manifest(state["records"]) + + +def _fused_prefill_shadow_process_state() -> dict: + pid = os.getpid() + if _FUSED_PREFILL_SHADOW_STATE["pid"] != pid: + _FUSED_PREFILL_SHADOW_STATE["pid"] = pid + _FUSED_PREFILL_SHADOW_STATE["records"] = [] + return _FUSED_PREFILL_SHADOW_STATE + + +def _fused_prefill_shadow_report_path() -> Path: + if _FUSED_PREFILL_SHADOW_REPORT_DIR is None: + raise RuntimeError("fused-prefill shadow report directory is unset") + rank = _fused_prefill_shadow_rank() + rank_label = str(rank) if rank >= 0 else "unknown" + return _FUSED_PREFILL_SHADOW_REPORT_DIR / ( + f"rank-{rank_label}-pid-{os.getpid()}.json") + + +def _atomic_write_fused_prefill_shadow_report(value: dict) -> None: + path = _fused_prefill_shadow_report_path() + descriptor, temporary = tempfile.mkstemp( + prefix=f".{path.name}.", suffix=".tmp", dir=path.parent) + try: + os.fchmod(descriptor, 0o600) + with os.fdopen(descriptor, "w", encoding="utf-8") as stream: + json.dump(value, stream, ensure_ascii=True, indent=2, + sort_keys=True) + stream.write("\n") + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + try: + os.unlink(temporary) + except FileNotFoundError: + pass + + +def _build_fused_prefill_shadow_report(records: list) -> dict: + expected = ( + len(_FUSED_PREFILL_SHADOW_CONTEXTS) + * _FUSED_PREFILL_SHADOW_MAX_CALLS_PER_CONTEXT) + completed = [ + record for record in records + if record["status"] in {"pass", "fail", "invalid"} + ] + failures = [record for record in completed if record["status"] == "fail"] + invalid = [record for record in completed if record["status"] == "invalid"] + pending = [record for record in records if record["status"] == "pending"] + relative_l2_values = [ + record["relative_l2"] for record in completed + if isinstance(record.get("relative_l2"), float) + ] + max_abs_values = [ + record["max_abs"] for record in completed + if isinstance(record.get("max_abs"), float) + ] + if invalid: + status = "invalid" + elif failures: + status = "fail" + elif len(completed) == expected and not pending: + status = "pass" + else: + status = "collecting" + report = { + "schema": "bi100-fused-prefill-real-activation-shadow-v1", + "version": 1, + "run_id": _FUSED_PREFILL_SHADOW_RUN_ID, + "pid": os.getpid(), + "rank": _fused_prefill_shadow_rank(), + "status": status, + "selection": { + "minimum_context_tokens": list( + _FUSED_PREFILL_SHADOW_CONTEXTS), + "max_calls_per_context": ( + _FUSED_PREFILL_SHADOW_MAX_CALLS_PER_CONTEXT), + }, + "thresholds": { + "require_finite_candidate": True, + "require_finite_reference": True, + "maximum_relative_l2": ( + _FUSED_PREFILL_SHADOW_RELATIVE_L2_LIMIT), + "maximum_absolute_error": ( + _FUSED_PREFILL_SHADOW_MAX_ABS_LIMIT), + }, + "observations": { + "expected": expected, + "reserved": len(records), + "completed": len(completed), + "passed": sum(record["status"] == "pass" for record in records), + "failed": len(failures), + "invalid": len(invalid), + "pending": len(pending), + "maximum_relative_l2": ( + max(relative_l2_values) if relative_l2_values else None), + "maximum_absolute_error": ( + max(max_abs_values) if max_abs_values else None), + }, + "records": records, + "privacy": { + "contains_prompts": False, + "contains_model_outputs": False, + "contains_tensor_values": False, + "contains_token_ids": False, + "contains_credentials": False, + }, + } + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + report["schema"] = ( + "bi100-fused-prefill-real-activation-calibrated-shadow-v1") + report["thresholds"] = { + "require_finite_candidate": True, + "require_finite_reference": True, + "maximum_candidate_vs_rounded_relative_l2": ( + _FUSED_PREFILL_SHADOW_RELATIVE_L2_LIMIT), + "maximum_error_multiple_over_fp16_rounding": ( + _FUSED_PREFILL_SHADOW_ERROR_MULTIPLIER), + "ratio_denominator_floor": ( + _FUSED_PREFILL_SHADOW_RATIO_FLOOR), + "fixed_max_abs_role": "diagnostic_only", + "finite_failure_action": ( + _FUSED_PREFILL_SHADOW_FAILURE_ACTION), + } + calibrated_metrics = ( + "candidate_to_fp32_relative_l2", + "candidate_to_fp32_max_abs", + "rounded_to_fp32_relative_l2", + "rounded_to_fp32_max_abs", + "relative_l2_baseline_ratio", + "max_abs_baseline_ratio", + ) + for name in calibrated_metrics: + values = [ + record[name] for record in completed + if isinstance(record.get(name), float) + ] + report["observations"][f"maximum_{name}"] = ( + max(values) if values else None) + return report + + +def _reserve_fused_prefill_shadow( + query: torch.Tensor, + block_context_len: int, + num_q_heads: int, + num_kv_heads: int, + head_dim: int, + block_size: int, +) -> Optional[int]: + if not _FUSED_PREFILL_SHADOW: + return None + state = _fused_prefill_shadow_process_state() + records = state["records"] + selected_bucket = None + for bucket_index, bucket in enumerate(_FUSED_PREFILL_SHADOW_CONTEXTS): + upper_bound = ( + _FUSED_PREFILL_SHADOW_CONTEXTS[bucket_index + 1] + if bucket_index + 1 < len(_FUSED_PREFILL_SHADOW_CONTEXTS) + else None) + used = sum( + record["bucket_min_context_tokens"] == bucket + for record in records) + if ( + block_context_len >= bucket + and (upper_bound is None or block_context_len < upper_bound) + and used < _FUSED_PREFILL_SHADOW_MAX_CALLS_PER_CONTEXT + ): + selected_bucket = bucket + break + if selected_bucket is None: + return None + record = { + "index": len(records), + "status": "pending", + "bucket_min_context_tokens": selected_bucket, + "context_tokens": block_context_len, + "query_shape": list(query.shape), + "query_heads": num_q_heads, + "kv_heads": num_kv_heads, + "head_dim": head_dim, + "block_size": block_size, + "candidate_finite": None, + "reference_finite": None, + "relative_l2": None, + "max_abs": None, + "error_stage": None, + "error_type": None, + } + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + record.update({ + "candidate_to_fp32_relative_l2": None, + "candidate_to_fp32_max_abs": None, + "rounded_to_fp32_relative_l2": None, + "rounded_to_fp32_max_abs": None, + "relative_l2_baseline_ratio": None, + "max_abs_baseline_ratio": None, + }) + records.append(record) + _atomic_write_fused_prefill_shadow_report( + _build_fused_prefill_shadow_report(records)) + return record["index"] + + +def _finish_fused_prefill_shadow( + index: int, + *, + status: str, + candidate_finite: Optional[bool] = None, + reference_finite: Optional[bool] = None, + relative_l2: Optional[float] = None, + max_abs: Optional[float] = None, + candidate_to_fp32_relative_l2: Optional[float] = None, + candidate_to_fp32_max_abs: Optional[float] = None, + rounded_to_fp32_relative_l2: Optional[float] = None, + rounded_to_fp32_max_abs: Optional[float] = None, + relative_l2_baseline_ratio: Optional[float] = None, + max_abs_baseline_ratio: Optional[float] = None, + error_stage: Optional[str] = None, + error_type: Optional[str] = None, +) -> None: + state = _fused_prefill_shadow_process_state() + records = state["records"] + if index < 0 or index >= len(records): + raise RuntimeError("fused-prefill shadow record index is invalid") + if status not in {"pass", "fail", "invalid"}: + raise RuntimeError("fused-prefill shadow status is invalid") + record = records[index] + updates = { + "status": status, + "candidate_finite": candidate_finite, + "reference_finite": reference_finite, + "relative_l2": relative_l2, + "max_abs": max_abs, + "error_stage": error_stage, + "error_type": error_type, + } + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + updates.update({ + "candidate_to_fp32_relative_l2": ( + candidate_to_fp32_relative_l2), + "candidate_to_fp32_max_abs": candidate_to_fp32_max_abs, + "rounded_to_fp32_relative_l2": ( + rounded_to_fp32_relative_l2), + "rounded_to_fp32_max_abs": rounded_to_fp32_max_abs, + "relative_l2_baseline_ratio": relative_l2_baseline_ratio, + "max_abs_baseline_ratio": max_abs_baseline_ratio, + }) + record.update(updates) + _atomic_write_fused_prefill_shadow_report( + _build_fused_prefill_shadow_report(records)) + + +def _calibrated_shadow_metrics_qualified(metrics: dict) -> bool: + return ( + metrics["relative_l2"] + <= _FUSED_PREFILL_SHADOW_RELATIVE_L2_LIMIT + and metrics["candidate_to_fp32_relative_l2"] + <= ( + _FUSED_PREFILL_SHADOW_ERROR_MULTIPLIER + * metrics["rounded_to_fp32_relative_l2"] + + _FUSED_PREFILL_SHADOW_RATIO_FLOOR + ) + and metrics["candidate_to_fp32_max_abs"] + <= ( + _FUSED_PREFILL_SHADOW_ERROR_MULTIPLIER + * metrics["rounded_to_fp32_max_abs"] + + _FUSED_PREFILL_SHADOW_RATIO_FLOOR + ) + ) + + +def _error_metrics( + actual: torch.Tensor, + reference: torch.Tensor, + denominator: float, +) -> Tuple[float, float]: + difference = actual - reference + relative_l2 = float(torch.norm(difference).item()) / denominator + max_abs = float(difference.abs().max().item()) + return relative_l2, max_abs + + +def _compare_fused_prefill_shadow_outputs( + candidate: torch.Tensor, + reference: torch.Tensor, + reference_fp32: Optional[torch.Tensor] = None, +) -> dict: + candidate_float = candidate.float() + reference_float = reference.float() + candidate_finite = bool(torch.isfinite(candidate_float).all().item()) + reference_finite = bool(torch.isfinite(reference_float).all().item()) + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + if reference_fp32 is None: + raise RuntimeError( + "calibrated fused-prefill shadow requires FP32 reference") + reference_fp32 = reference_fp32.float() + reference_finite = ( + reference_finite + and bool(torch.isfinite(reference_fp32).all().item()) + ) + if not candidate_finite or not reference_finite: + result = { + "status": "fail" if not candidate_finite else "invalid", + "candidate_finite": candidate_finite, + "reference_finite": reference_finite, + "relative_l2": None, + "max_abs": None, + } + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + result.update({ + "candidate_to_fp32_relative_l2": None, + "candidate_to_fp32_max_abs": None, + "rounded_to_fp32_relative_l2": None, + "rounded_to_fp32_max_abs": None, + "relative_l2_baseline_ratio": None, + "max_abs_baseline_ratio": None, + }) + return result + denominator = max(float(torch.norm(reference_float).item()), 1.0e-12) + relative_l2, max_abs = _error_metrics( + candidate_float, reference_float, denominator) + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE == "calibrated": + if reference_fp32 is None: + raise RuntimeError( + "calibrated fused-prefill shadow lost its FP32 reference") + fp32_denominator = max( + float(torch.norm(reference_fp32).item()), + _FUSED_PREFILL_SHADOW_RATIO_FLOOR, + ) + candidate_fp32_relative_l2, candidate_fp32_max_abs = ( + _error_metrics( + candidate_float, reference_fp32, fp32_denominator)) + rounded_fp32_relative_l2, rounded_fp32_max_abs = ( + _error_metrics( + reference_float, reference_fp32, fp32_denominator)) + metrics = { + "relative_l2": relative_l2, + "max_abs": max_abs, + "candidate_to_fp32_relative_l2": ( + candidate_fp32_relative_l2), + "candidate_to_fp32_max_abs": candidate_fp32_max_abs, + "rounded_to_fp32_relative_l2": rounded_fp32_relative_l2, + "rounded_to_fp32_max_abs": rounded_fp32_max_abs, + "relative_l2_baseline_ratio": ( + candidate_fp32_relative_l2 + / max( + rounded_fp32_relative_l2, + _FUSED_PREFILL_SHADOW_RATIO_FLOOR, + ) + ), + "max_abs_baseline_ratio": ( + candidate_fp32_max_abs + / max( + rounded_fp32_max_abs, + _FUSED_PREFILL_SHADOW_RATIO_FLOOR, + ) + ), + } + return { + "status": ( + "pass" + if _calibrated_shadow_metrics_qualified(metrics) + else "fail" + ), + "candidate_finite": True, + "reference_finite": True, + **metrics, + } + qualified = ( + relative_l2 <= _FUSED_PREFILL_SHADOW_RELATIVE_L2_LIMIT + and max_abs <= _FUSED_PREFILL_SHADOW_MAX_ABS_LIMIT) + return { + "status": "pass" if qualified else "fail", + "candidate_finite": True, + "reference_finite": True, + "relative_l2": relative_l2, + "max_abs": max_abs, + } + + +def _validate_decode_layout( + num_seqs: int, + seq_lens_count: int, + block_table_rows: int, + block_table_width: int, + actual_max: int, + block_size: int, + physical_key_blocks: int, + physical_value_blocks: int, + num_heads: int, + num_kv_heads: int, +) -> int: + """Validate host-visible decode metadata before a native kernel launch.""" + if num_seqs <= 0: + raise RuntimeError(f"decode requires num_seqs > 0, got {num_seqs}") + if seq_lens_count != num_seqs: + raise RuntimeError( + f"seq_lens has {seq_lens_count} entries for {num_seqs} sequences") + if block_table_rows < num_seqs: + raise RuntimeError( + f"block table has {block_table_rows} rows for {num_seqs} sequences") + if actual_max <= 0: + raise RuntimeError(f"decode sequence length must be > 0, got {actual_max}") + if block_size <= 0: + raise RuntimeError(f"KV block_size must be > 0, got {block_size}") + if physical_key_blocks != physical_value_blocks: + raise RuntimeError( + "key/value cache block counts differ: " + f"{physical_key_blocks} != {physical_value_blocks}") + if num_kv_heads <= 0 or num_heads % num_kv_heads != 0: + raise RuntimeError( + f"invalid GQA layout: num_heads={num_heads}, " + f"num_kv_heads={num_kv_heads}") + + required_blocks = (actual_max + block_size - 1) // block_size + if required_blocks > block_table_width: + raise RuntimeError( + f"decode needs {required_blocks} blocks for seq_len={actual_max}, " + f"but block table width is {block_table_width}") + return required_blocks + + +def _strict_prefix_query_segments( + context_len: int, + query_len: int, + block_size: int, +) -> List[Tuple[int, int, int]]: + """Split a query at the strict prefix-cache checkpoint, if it crosses it.""" + if query_len <= 0: + return [] + total_len = context_len + query_len + strict_prefix_len = ((total_len - 1) // block_size) * block_size + split = strict_prefix_len - context_len + if 0 < split < query_len: + return [(0, split, context_len), + (split, query_len, strict_prefix_len)] + return [(0, query_len, context_len)] + + +def _is_supported_corex_fused_paged_prefill_request( + kv_cache_dtype: str, + max_query_len: int, + total_query_len: int, + alibi_slopes: Optional[torch.Tensor], + sliding_window: Optional[int], + k_scale: float, + v_scale: float, + is_causal_decoder: bool, +) -> bool: + """Check request-wide properties that are outside the native ABI.""" + return bool( + is_causal_decoder + and _PREFIX_BLOCKS_PER_TILE == 32 + and kv_cache_dtype == "auto" + and max_query_len == total_query_len + and alibi_slopes is None + and sliding_window is None + and k_scale == 1.0 + and v_scale == 1.0 + ) + + +def _can_enable_corex_fused_paged_prefill_request( + kv_cache_dtype: str, + max_query_len: int, + total_query_len: int, + alibi_slopes: Optional[torch.Tensor], + sliding_window: Optional[int], + k_scale: float, + v_scale: float, + is_causal_decoder: bool, +) -> bool: + return bool( + _USE_COREX_FUSED_PAGED_PREFILL + and _is_supported_corex_fused_paged_prefill_request( + kv_cache_dtype, + max_query_len, + total_query_len, + alibi_slopes, + sliding_window, + k_scale, + v_scale, + is_causal_decoder, + ) + ) + + +def _is_single_sequence_fused_prefill_metadata( + batch_size: int, + block_table_rows: int, + query_start_count: int, + query_start_first: int, + query_start_last: int, + seq_lens_count: int, + seq_len: int, + context_lens_count: int, + context_len: int, + total_query_len: int, +) -> bool: + """Validate the exact single-sequence metadata used by qualification.""" + return bool( + batch_size == 1 + and block_table_rows == 1 + and query_start_count == 2 + and query_start_first == 0 + and query_start_last == total_query_len + and seq_lens_count == 1 + and context_lens_count == 1 + and context_len >= 0 + and seq_len == context_len + total_query_len + and seq_len <= 262144 + ) + + +def _is_supported_corex_fused_paged_prefill_segment( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + prefix_key: torch.Tensor, + prefix_value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + seq_index: int, + block_context_len: int, + num_q_heads: int, + num_kv_heads: int, + head_dim: int, + gqa_ratio: int, + block_size: int, +) -> bool: + """Accept only the fixed M1-47 production shape.""" + query_len = query.shape[0] + if ( + query_len <= 16 + or query_len > 8192 + or block_context_len < 0 + or block_context_len % 16 != 0 + or block_context_len + query_len > 262144 + ): + return False + if (num_q_heads, num_kv_heads, head_dim, gqa_ratio, block_size) != ( + 4, + 1, + 256, + 4, + 16, + ): + return False + if prefix_key.shape[0] != 0 or prefix_value.shape[0] != 0: + return False + if ( + tuple(query.shape) != (query_len, 4, 256) + or tuple(key.shape) != (query_len, 1, 256) + or tuple(value.shape) != (query_len, 1, 256) + ): + return False + if ( + len(key_cache.shape) != 5 + or tuple(key_cache.shape[1:]) != (1, 32, 16, 8) + or len(value_cache.shape) != 4 + or tuple(value_cache.shape[1:]) != (1, 256, 16) + or key_cache.shape[0] != value_cache.shape[0] + ): + return False + if ( + len(block_tables.shape) != 2 + or block_tables.shape[0] != 1 + or seq_index < 0 + or seq_index >= block_tables.shape[0] + or block_tables.shape[1] < block_context_len // block_size + ): + return False + + half_tensors = (query, key, value, key_cache, value_cache) + if any(tensor.dtype != torch.float16 for tensor in half_tensors): + return False + if block_tables.dtype != torch.int32: + return False + tensors = half_tensors + (block_tables,) + if any(not tensor.is_cuda for tensor in tensors): + return False + if any(tensor.device != query.device for tensor in tensors): + return False + if any(not tensor.is_contiguous() for tensor in tensors): + return False + return True + + +def _can_use_corex_fused_paged_prefill( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + prefix_key: torch.Tensor, + prefix_value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + seq_index: int, + block_context_len: int, + num_q_heads: int, + num_kv_heads: int, + head_dim: int, + gqa_ratio: int, + block_size: int, +) -> bool: + return bool( + _USE_COREX_FUSED_PAGED_PREFILL + and _is_supported_corex_fused_paged_prefill_segment( + query, + key, + value, + prefix_key, + prefix_value, + key_cache, + value_cache, + block_tables, + seq_index, + block_context_len, + num_q_heads, + num_kv_heads, + head_dim, + gqa_ratio, + block_size, + ) + ) + + +def _prefix_context_tile_spans( + block_context_len: int, + prefix_query_len: int, + tile_size: int, +) -> List[Tuple[int, int, int, int]]: + """Map context tiles to block-cache and preceding-query token ranges. + + Each tuple is ``(block_start, block_end, prefix_start, prefix_end)``. + Concatenating both ranges reconstructs one tile in the logical context. + Keeping tiles aligned to absolute token positions makes cold segmented + prefill use the same online-softmax partitions as a warm cached request. + """ + if block_context_len < 0 or prefix_query_len < 0 or tile_size <= 0: + raise ValueError("context lengths must be non-negative and tile_size > 0") + spans = [] + total_context_len = block_context_len + prefix_query_len + for tile_start in range(0, total_context_len, tile_size): + tile_end = min(tile_start + tile_size, total_context_len) + block_start = min(tile_start, block_context_len) + block_end = min(tile_end, block_context_len) + prefix_start = max(0, tile_start - block_context_len) + prefix_end = max(0, tile_end - block_context_len) + spans.append((block_start, block_end, prefix_start, prefix_end)) + return spans + + +@dataclass +class PagedAttentionMetadata: + """Metadata for PagedAttention.""" + # (batch_size,). The length of sequences (entire tokens seen so far) per + # sequence. + seq_lens_tensor: Optional[torch.Tensor] + # Maximum sequence length in the batch. 0 if it is prefill-only batch. + max_decode_seq_len: int + # (batch_size, max_blocks_per_seq). + # Block addresses per sequence. (Seq id -> list of physical block) + # E.g., [0, 1, 2] means tokens are stored in 0th, 1st, and 2nd blocks + # in the kv cache. Each block can contain up to block_size tokens. + # 2nd dimensions are padded up to max_blocks_per_seq if it is cuda-graph + # captured. + block_tables: Optional[torch.Tensor] + + +class PagedAttention: + + @staticmethod + def get_supported_head_sizes() -> List[int]: + return [64, 80, 96, 112, 120, 128, 192, 256] + + @staticmethod + def get_kv_cache_shape( + num_blocks: int, + block_size: int, + num_kv_heads: int, + head_size: int, + ) -> Tuple[int, ...]: + return (2, num_blocks, block_size * num_kv_heads * head_size) + + @staticmethod + def split_kv_cache( + kv_cache: torch.Tensor, + num_kv_heads: int, + head_size: int, + ) -> Tuple[torch.Tensor, torch.Tensor]: + x = 16 // kv_cache.element_size() + num_blocks = kv_cache.shape[1] + + key_cache = kv_cache[0] + key_cache = key_cache.view(num_blocks, num_kv_heads, head_size // x, + -1, x) + value_cache = kv_cache[1] + value_cache = value_cache.view(num_blocks, num_kv_heads, head_size, -1) + return key_cache, value_cache + + @staticmethod + def write_to_paged_cache( + key: torch.Tensor, + value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + slot_mapping: torch.Tensor, + kv_cache_dtype: str, + k_scale: float, + v_scale: float, + ) -> None: + global _CACHE_WRITE_LOGGED + flat_slots = slot_mapping.flatten() + if key.shape[0] != value.shape[0]: + raise RuntimeError( + f"key/value token counts differ: {key.shape[0]} != " + f"{value.shape[0]}") + if flat_slots.numel() != key.shape[0]: + raise RuntimeError( + f"slot_mapping has {flat_slots.numel()} entries for " + f"{key.shape[0]} KV tokens") + if key_cache.shape[0] != value_cache.shape[0]: + raise RuntimeError( + "key/value cache block counts differ before cache write: " + f"{key_cache.shape[0]} != {value_cache.shape[0]}") + + if _PAGED_ATTN_DIAGNOSTICS and flat_slots.numel() > 0: + min_slot = int(flat_slots.min().item()) + max_slot = int(flat_slots.max().item()) + max_valid_slot = key_cache.shape[0] * value_cache.shape[3] - 1 + if min_slot < -1 or max_slot > max_valid_slot: + raise RuntimeError( + f"slot_mapping range [{min_slot}, {max_slot}] outside " + f"[-1, {max_valid_slot}]") + + if _PAGED_ATTN_DIAGNOSTICS and not _CACHE_WRITE_LOGGED: + print( + "[BI100 PAGED_ATTN] cache_write " + f"pid={os.getpid()} rank={os.environ.get('RANK', '?')} " + f"local_rank={os.environ.get('LOCAL_RANK', '?')} " + f"key={tuple(key.shape)} value={tuple(value.shape)} " + f"slots={tuple(flat_slots.shape)} " + f"key_cache={tuple(key_cache.shape)} " + f"value_cache={tuple(value_cache.shape)}", + file=sys.stderr, + flush=True, + ) + _CACHE_WRITE_LOGGED = True + + ops.reshape_and_cache( + key, + value, + key_cache, + value_cache, + flat_slots, + kv_cache_dtype, + k_scale, + v_scale, + ) + if _PAGED_ATTN_DIAGNOSTICS: + try: + torch.cuda.synchronize() + except Exception as exc: + print( + "[BI100 PAGED_ATTN] cache_write_sync_failed " + f"pid={os.getpid()} error={type(exc).__name__}: {exc}", + file=sys.stderr, + flush=True, + ) + raise + + @staticmethod + def _forward_decode_pytorch( + query: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + seq_lens: torch.Tensor, + scale: float, + ) -> torch.Tensor: + """Pure-PyTorch decode attention for long contexts (no hardware kernel). + + paged_attention_v1 hangs on BI-V100 when max_seq_len > ~32K due to + shared memory limits. For decode, q_len=1 per sequence so no Q-tiling + is needed — the attention weight tensor is [H, 1, seq_len] which is + trivially small (~5 MB at 50K). + + Shapes + ------ + query : [num_seqs, num_heads, head_dim] + key_cache : [num_blocks, num_kv_heads, head_dim//x, block_size, x] + value_cache : [num_blocks, num_kv_heads, head_dim, block_size] + block_tables: [num_seqs, max_blocks_per_seq] + seq_lens : [num_seqs] + """ + num_seqs, num_heads, head_dim = query.shape + num_kv_heads = key_cache.shape[1] + block_size = value_cache.shape[3] + gqa_ratio = num_heads // num_kv_heads + orig_dtype = query.dtype + + output = torch.empty_like(query) + + try: + for i in range(num_seqs): + seq_len = int(seq_lens[i].item()) + num_blocks = (seq_len + block_size - 1) // block_size + blk_ids = block_tables[i, :num_blocks] + + use_corex_gather = ( + _USE_COREX_PAGED_KV_GATHER + and query.dtype == torch.float16 + and key_cache.dtype == torch.float16 + and value_cache.dtype == torch.float16 + and block_tables.dtype == torch.int32 + and key_cache.is_contiguous() + and value_cache.is_contiguous() + and blk_ids.is_contiguous()) + if use_corex_gather: + k_t, v_t = _corex_paged_kv_gather.gather( + key_cache, value_cache, blk_ids, seq_len) + else: + # Gather K: [kv_h, head_dim, seq_len] fp32 without GQA + # expansion. The CoreX path above fuses these layout copies + # and FP16-to-FP32 conversions into one kernel. + k_t = (key_cache[blk_ids] + .permute(0, 3, 1, 2, 4) + .contiguous() + .view(-1, num_kv_heads, head_dim))[:seq_len] \ + .permute(1, 2, 0).contiguous().float() + v_t = (value_cache[blk_ids] + .permute(0, 3, 1, 2) + .contiguous() + .view(-1, num_kv_heads, head_dim))[:seq_len] \ + .permute(1, 0, 2).contiguous().float() + + # Reshape Q for lazy GQA: [kv_h, gqa_ratio, 1, d] + q_grouped = (query[i].float() + .view(num_kv_heads, gqa_ratio, head_dim) + .unsqueeze(2)) + + # [kv_h, gqa_ratio, 1, seq_len] + attn_w = torch.matmul( + q_grouped * scale, # [kv_h, gqa, 1, d] + k_t.unsqueeze(1)) # [kv_h, 1, d, seq_len] + attn_w = torch.softmax(attn_w, dim=-1) + + # [kv_h, gqa_ratio, 1, d] → [num_heads, head_dim] + out_i = torch.matmul(attn_w, v_t.unsqueeze(1)) + output[i] = out_i.view(num_heads, head_dim).to(orig_dtype) + + except Exception as e: + print(f"[decode_pytorch ERROR] {type(e).__name__}: {e}", + file=sys.stderr, flush=True) + traceback.print_exc(file=sys.stderr) + raise + + return output + + # paged_attention_v1 on BI-V100 fails for long contexts. + # Route on actual sequence length (seq_lens.max()), not the max_seq_len + # parameter which is inflated to max_model_len in CUDA graph mode. + _PYTORCH_DECODE_THRESHOLD = _PYTORCH_DECODE_THRESHOLD + _FORCE_PAGED_ATTN_V2 = _FORCE_PAGED_ATTN_V2 + + @staticmethod + def _should_use_paged_attention_v1( + max_seq_len: int, + max_num_partitions: int, + num_seqs: int, + num_heads: int, + ) -> bool: + if PagedAttention._FORCE_PAGED_ATTN_V2: + return False + # Keep the stable BI100 default: V1 is used unless long-context decode + # has already routed to the PyTorch fallback above. + return True + + @staticmethod + def _validate_prefix_block_table( + seq_index: int, + num_ctx_blocks: int, + block_table_width: int, + ctx_len: int, + ) -> int: + if num_ctx_blocks <= block_table_width: + return num_ctx_blocks + msg = ( + f"seq {seq_index}: num_ctx_blocks={num_ctx_blocks} " + f"> block_tables.shape[1]={block_table_width}, " + f"ctx_len={ctx_len}. Block table is undersized; " + "refusing to truncate context because attention would be incorrect.") + if env_bool("BI100_ALLOW_PREFIX_GUARD_CAP", False): + print( + "[paged_attn RISK] BI100_ALLOW_PREFIX_GUARD_CAP=1; " + f"{msg} Debug cap is enabled and may corrupt output.", + file=sys.stderr, + flush=True) + return block_table_width + raise RuntimeError(msg) + + @staticmethod + def forward_decode( + query: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + seq_lens: torch.Tensor, + max_seq_len: int, + kv_cache_dtype: str, + head_mapping: torch.Tensor, + scale: float, + alibi_slopes: Optional[torch.Tensor], + k_scale: float, + v_scale: float, + tp_rank: int = 0, + blocksparse_local_blocks: int = 0, + blocksparse_vert_stride: int = 0, + blocksparse_block_size: int = 64, + blocksparse_head_sliding_step: int = 0, + ) -> torch.Tensor: + actual_max = int(seq_lens.max().item()) if seq_lens.numel() > 0 else max_seq_len + block_size = value_cache.shape[3] + num_seqs, num_heads, head_size = query.shape + if key_cache.shape[1] != value_cache.shape[1]: + raise RuntimeError( + "key/value cache KV-head counts differ: " + f"{key_cache.shape[1]} != {value_cache.shape[1]}") + if head_mapping.numel() != num_heads: + raise RuntimeError( + f"head_mapping has {head_mapping.numel()} entries for " + f"{num_heads} query heads") + required_blocks = _validate_decode_layout( + num_seqs=num_seqs, + seq_lens_count=seq_lens.numel(), + block_table_rows=block_tables.shape[0], + block_table_width=block_tables.shape[1], + actual_max=actual_max, + block_size=block_size, + physical_key_blocks=key_cache.shape[0], + physical_value_blocks=value_cache.shape[0], + num_heads=num_heads, + num_kv_heads=key_cache.shape[1], + ) + if actual_max > max_seq_len: + raise RuntimeError( + f"actual decode length {actual_max} exceeds max_seq_len " + f"{max_seq_len}") + + if actual_max > PagedAttention._PYTORCH_DECODE_THRESHOLD: + path = ("pytorch_corex_gather" if _USE_COREX_PAGED_KV_GATHER + else "pytorch") + else: + path = "native_v1" + log_key = (path, None) + if (_DECODE_LOG_INTERVAL > 0 and + actual_max % _DECODE_LOG_INTERVAL == 0): + log_key = (path, actual_max) + if log_key not in _DECODE_DISPATCH_LOGGED: + print( + "[BI100 PAGED_ATTN] decode_dispatch " + f"pid={os.getpid()} rank={os.environ.get('RANK', '?')} " + f"local_rank={os.environ.get('LOCAL_RANK', '?')} " + f"path={path} actual_max={actual_max} " + f"max_seq_len={max_seq_len} query={tuple(query.shape)} " + f"key_cache={tuple(key_cache.shape)} " + f"value_cache={tuple(value_cache.shape)} " + f"block_tables={tuple(block_tables.shape)} " + f"required_blocks={required_blocks} " + f"threshold={PagedAttention._PYTORCH_DECODE_THRESHOLD}", + file=sys.stderr, + flush=True, + ) + _DECODE_DISPATCH_LOGGED.add(log_key) + + if _PAGED_ATTN_DIAGNOSTICS: + for seq_index in range(num_seqs): + seq_len = int(seq_lens[seq_index].item()) + if seq_len <= 0: + raise RuntimeError( + f"seq {seq_index}: decode length must be > 0, got {seq_len}") + seq_blocks = (seq_len + block_size - 1) // block_size + block_ids = block_tables[seq_index, :seq_blocks] + min_block = int(block_ids.min().item()) + max_block = int(block_ids.max().item()) + if min_block < 0 or max_block >= key_cache.shape[0]: + raise RuntimeError( + f"seq {seq_index}: physical block range " + f"[{min_block}, {max_block}] outside " + f"[0, {key_cache.shape[0] - 1}]") + + if actual_max > PagedAttention._PYTORCH_DECODE_THRESHOLD: + with bi100_timer("paged_attn.decode_pytorch"): + return PagedAttention._forward_decode_pytorch( + query, key_cache, value_cache, block_tables, seq_lens, + scale) + + if blocksparse_vert_stride is not None and blocksparse_vert_stride > 1: + # use blocksparse paged attention + block_size = value_cache.size(-1) + assert (blocksparse_block_size > 0 and + blocksparse_block_size % block_size == 0), \ + (f"{blocksparse_block_size=} needs to be a multiple of" + f"{block_size=} used in block_tables.") + + output = torch.empty_like(query) + max_num_partitions = ((max_seq_len + _PARTITION_SIZE - 1) // + _PARTITION_SIZE) + # NOTE(woosuk): We use a simple heuristic to decide whether to use + # PagedAttention V1 or V2. If the number of partitions is 1, we use + # V1 to avoid the overhead of reduction. Also, if the number of + # sequences or heads is large, we use V1 since there is enough work + # to parallelize. + # TODO(woosuk): Tune this heuristic. + # For context len > 8192, use V2 kernel to avoid shared memory shortage. + use_v1 = PagedAttention._should_use_paged_attention_v1( + max_seq_len, max_num_partitions, num_seqs, num_heads) + if use_v1: + # Run PagedAttention V1. + ops.paged_attention_v1( + output, + query, + key_cache, + value_cache, + head_mapping, + scale, + block_tables, + seq_lens, + block_size, + max_seq_len, + alibi_slopes, + ) + else: + # Run PagedAttention V2. + assert _PARTITION_SIZE % block_size == 0 + tmp_output = torch.empty( + size=(num_seqs, num_heads, max_num_partitions, head_size), + dtype=output.dtype, + device=output.device, + ) + exp_sums = torch.empty( + size=(num_seqs, num_heads, max_num_partitions), + dtype=torch.float32, + device=output.device, + ) + max_logits = torch.empty_like(exp_sums) + ops.paged_attention_v2( + output, + exp_sums, + max_logits, + tmp_output, + query, + key_cache, + value_cache, + head_mapping, + scale, + block_tables, + seq_lens, + block_size, + max_seq_len, + alibi_slopes, + kv_cache_dtype, + k_scale, + v_scale, + tp_rank, + blocksparse_local_blocks, + blocksparse_vert_stride, + blocksparse_block_size, + blocksparse_head_sliding_step, + ) + return output + + @staticmethod + def forward_prefix( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + kv_cache_dtype: str, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + query_start_loc: torch.Tensor, + seq_lens_tensor: torch.Tensor, + context_lens: torch.Tensor, + max_query_len: int, + alibi_slopes: Optional[torch.Tensor], + sliding_window: Optional[int], + k_scale: float, + v_scale: float, + is_causal_decoder: bool = False, + ) -> torch.Tensor: + # NOTE: The Triton context_attention_fwd kernel hangs on Iluvatar + # BI-V100 hardware (same class of issue as cudnnFlashAttnForward). + # Use a pure-PyTorch fallback that reads the paged KV cache directly. + supported_request = bool( + (_USE_COREX_FUSED_PAGED_PREFILL + or _ACTIVATION_CAPTURE_ENABLED) + and _is_supported_corex_fused_paged_prefill_request( + kv_cache_dtype, + max_query_len, + query.shape[0], + alibi_slopes, + sliding_window, + k_scale, + v_scale, + is_causal_decoder, + )) + fused_request_eligible = bool( + _USE_COREX_FUSED_PAGED_PREFILL and supported_request) + capture_request_eligible = bool( + _ACTIVATION_CAPTURE_ENABLED and supported_request) + _log_corex_fused_prefill_diagnostic( + "request", + eligible=fused_request_eligible, + use_native=_USE_COREX_FUSED_PAGED_PREFILL, + causal=is_causal_decoder, + kv_cache_dtype=kv_cache_dtype, + max_query_len=max_query_len, + total_query_len=query.shape[0], + tile_blocks=_PREFIX_BLOCKS_PER_TILE, + alibi_none=alibi_slopes is None, + sliding_window=sliding_window, + k_scale=k_scale, + v_scale=v_scale, + ) + return PagedAttention._forward_prefix_pytorch( + query, key, value, + key_cache, value_cache, + block_tables, query_start_loc, + seq_lens_tensor, context_lens, + fused_request_eligible=fused_request_eligible, + capture_request_eligible=capture_request_eligible, + ) + + @staticmethod + def _forward_prefix_pytorch( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + query_start_loc: torch.Tensor, + seq_lens_tensor: torch.Tensor, + context_lens: torch.Tensor, + fused_request_eligible: bool = False, + capture_request_eligible: bool = False, + ) -> torch.Tensor: + """Pure-PyTorch prefix-attention with K-tiling (Flash-Attention online softmax). + + Memory complexity: O(q_len), independent of kv_len. + Query segments end at the same strict block boundary used by prefix + caching. This keeps online-softmax reduction partitions identical when + an otherwise equivalent request reuses that prefix. + + Algorithm: Flash Attention online softmax. + Q is reshaped once to [kv_h, gqa, q_len, d] (24 MB) and held for all + K-tiles. For each tile a running (m, l, o) accumulator is updated — + the [q_len × kv_len] attention matrix is NEVER materialised in full. + + Tile budget (kv_h=1, gqa=6, q_len=4096, tile=256 tokens): + q_seq [1, 6, 4096, 256] fp32 24 MB (held all tiles) + o_acc same shape 24 MB (held all tiles) + s same shape 24 MB (per tile, freed before exp_s) + exp_s same shape 24 MB (per tile, brief overlap with s) + Peak ≈ 96 MB (s and exp_s briefly coexist during update). + + Shapes + ------ + query : [total_q_tokens, num_q_heads, head_dim] + key : [total_q_tokens, num_kv_heads, head_dim] + value : [total_q_tokens, num_kv_heads, head_dim] + key_cache : [num_blocks, num_kv_heads, head_dim//x, block_size, x] + value_cache : [num_blocks, num_kv_heads, head_dim, block_size] + block_tables : [batch_size, max_blocks_per_seq] + query_start_loc: [batch_size + 1] + seq_lens_tensor: [batch_size] total length (context + query) + context_lens : [batch_size] tokens already in KV cache + """ + try: + profile_name = "paged_attn.prefix_pytorch" + # Paged-block tiles for context phase. + # tile_sz = _BLOCKS_PER_TILE × block_size (e.g. 16×16 = 256 tokens). + # Score tensor [kv_h, gqa, q_len, tile_sz] fp32 = 24 MB per tile. + # Same tile size reused for the current-chunk phase. + _BLOCKS_PER_TILE = _PREFIX_BLOCKS_PER_TILE + + batch_size = seq_lens_tensor.shape[0] + num_q_heads = query.shape[1] + num_kv_heads = key_cache.shape[1] + head_dim = query.shape[2] + gqa_ratio = num_q_heads // num_kv_heads + block_size = value_cache.shape[3] + tile_sz = _BLOCKS_PER_TILE * block_size + scale = head_dim ** -0.5 + orig_dtype = query.dtype + output = torch.empty_like(query) + + if fused_request_eligible or capture_request_eligible: + query_start_count = query_start_loc.numel() + seq_lens_count = seq_lens_tensor.numel() + context_lens_count = context_lens.numel() + query_start_first = ( + int(query_start_loc[0].item()) + if query_start_count == 2 else -1) + query_start_last = ( + int(query_start_loc[1].item()) + if query_start_count == 2 else -1) + seq_len = ( + int(seq_lens_tensor[0].item()) + if seq_lens_count == 1 else -1) + context_len = ( + int(context_lens[0].item()) + if context_lens_count == 1 else -1) + metadata_eligible = ( + _is_single_sequence_fused_prefill_metadata( + batch_size=batch_size, + block_table_rows=block_tables.shape[0], + query_start_count=query_start_count, + query_start_first=query_start_first, + query_start_last=query_start_last, + seq_lens_count=seq_lens_count, + seq_len=seq_len, + context_lens_count=context_lens_count, + context_len=context_len, + total_query_len=query.shape[0], + )) + _log_corex_fused_prefill_diagnostic( + "metadata", + eligible=metadata_eligible, + batch_size=batch_size, + block_table_rows=block_tables.shape[0], + query_start_count=query_start_count, + query_start_first=query_start_first, + query_start_last=query_start_last, + seq_lens_count=seq_lens_count, + seq_len=seq_len, + context_lens_count=context_lens_count, + context_len=context_len, + total_query_len=query.shape[0], + ) + fused_request_eligible = bool( + fused_request_eligible and metadata_eligible) + capture_request_eligible = bool( + capture_request_eligible and metadata_eligible) + + for i in range(batch_size): + ctx_len = int(context_lens[i].item()) + q_start = int(query_start_loc[i].item()) + q_end = int(query_start_loc[i + 1].item()) + q_len = q_end - q_start + + for seg_start, seg_end, seg_ctx_len in ( + _strict_prefix_query_segments( + ctx_len, q_len, block_size)): + absolute_start = q_start + seg_start + absolute_end = q_start + seg_end + bi100_profile_count( + "paged_attn.prefix_dispatch", + path="pytorch", + query_len=seg_end - seg_start, + request_query_len=q_len, + context_len=seg_ctx_len, + block_size=block_size, + query_heads=num_q_heads, + kv_heads=num_kv_heads, + head_dim=head_dim, + ) + with bi100_timer(profile_name): + output[absolute_start:absolute_end] = ( + PagedAttention._forward_prefix_segment_pytorch( + query[absolute_start:absolute_end], + key[absolute_start:absolute_end], + value[absolute_start:absolute_end], + key[q_start:absolute_start], + value[q_start:absolute_start], + key_cache, + value_cache, + block_tables, + i, + ctx_len, + num_q_heads, + num_kv_heads, + head_dim, + gqa_ratio, + block_size, + tile_sz, + scale, + orig_dtype, + fused_request_eligible=( + fused_request_eligible), + capture_request_eligible=( + capture_request_eligible), + )) + + except Exception as e: + print(f"[paged_attn ERROR] {type(e).__name__}: {e}", + file=sys.stderr, flush=True) + traceback.print_exc(file=sys.stderr) + raise + return output + + @staticmethod + def _forward_prefix_segment_pytorch( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + prefix_key: torch.Tensor, + prefix_value: torch.Tensor, + key_cache: torch.Tensor, + value_cache: torch.Tensor, + block_tables: torch.Tensor, + seq_index: int, + block_context_len: int, + num_q_heads: int, + num_kv_heads: int, + head_dim: int, + gqa_ratio: int, + block_size: int, + tile_sz: int, + scale: float, + orig_dtype, + fused_request_eligible: bool = False, + return_fp32: bool = False, + capture_request_eligible: bool = False, + ) -> torch.Tensor: + """Run online-softmax attention for one strict-prefix query segment.""" + q_len = query.shape[0] + supported_segment = ( + _is_supported_corex_fused_paged_prefill_segment( + query, + key, + value, + prefix_key, + prefix_value, + key_cache, + value_cache, + block_tables, + seq_index, + block_context_len, + num_q_heads, + num_kv_heads, + head_dim, + gqa_ratio, + block_size, + )) + segment_eligible = bool( + fused_request_eligible + and _USE_COREX_FUSED_PAGED_PREFILL + and supported_segment) + capture_segment_eligible = bool( + capture_request_eligible + and _ACTIVATION_CAPTURE_ENABLED + and supported_segment) + _log_corex_fused_prefill_diagnostic( + "segment", + eligible=segment_eligible, + request_eligible=fused_request_eligible, + query_shape=tuple(query.shape), + key_shape=tuple(key.shape), + value_shape=tuple(value.shape), + prefix_key_shape=tuple(prefix_key.shape), + prefix_value_shape=tuple(prefix_value.shape), + key_cache_shape=tuple(key_cache.shape), + value_cache_shape=tuple(value_cache.shape), + block_table_shape=tuple(block_tables.shape), + context_len=block_context_len, + seq_index=seq_index, + q_dtype=query.dtype, + block_table_dtype=block_tables.dtype, + query_cuda=query.is_cuda, + query_contiguous=query.is_contiguous(), + key_contiguous=key.is_contiguous(), + value_contiguous=value.is_contiguous(), + block_table_contiguous=block_tables.is_contiguous(), + heads=f"{num_q_heads}/{num_kv_heads}/{head_dim}", + gqa_ratio=gqa_ratio, + block_size=block_size, + ) + if segment_eligible or capture_segment_eligible: + required_blocks = block_context_len // block_size + active_block_table = block_tables[ + seq_index, :required_blocks].contiguous() + if capture_segment_eligible: + reservation = _reserve_activation_capture(block_context_len) + if reservation is not None: + _capture_fused_prefill_activation( + reservation, + query, + key, + value, + key_cache, + value_cache, + active_block_table, + block_context_len, + scale, + ) + if segment_eligible: + shadow_index = _reserve_fused_prefill_shadow( + query, + block_context_len, + num_q_heads, + num_kv_heads, + head_dim, + block_size, + ) + try: + fused_result = _corex_fused_paged_prefill.forward( + query, + key, + value, + key_cache, + value_cache, + active_block_table, + block_context_len, + scale, + ) + except Exception as exc: + if shadow_index is not None: + _finish_fused_prefill_shadow( + shadow_index, + status="invalid", + error_stage="candidate-execution", + error_type=type(exc).__name__, + ) + raise + if ( + not isinstance(fused_result, (list, tuple)) + or len(fused_result) != 2 + ): + if shadow_index is not None: + _finish_fused_prefill_shadow( + shadow_index, + status="invalid", + error_stage="candidate-contract", + error_type="InvalidResult", + ) + raise RuntimeError( + "corex fused paged-prefill returned an invalid result") + fused_output = fused_result[0] + if ( + tuple(fused_output.shape) != tuple(query.shape) + or fused_output.dtype != query.dtype + or fused_output.device != query.device + ): + if shadow_index is not None: + _finish_fused_prefill_shadow( + shadow_index, + status="invalid", + error_stage="candidate-contract", + error_type="InvalidOutput", + ) + raise RuntimeError( + "corex fused paged-prefill returned an invalid output") + if shadow_index is not None: + try: + reference_result = ( + PagedAttention._forward_prefix_segment_pytorch( + query, + key, + value, + prefix_key, + prefix_value, + key_cache, + value_cache, + block_tables, + seq_index, + block_context_len, + num_q_heads, + num_kv_heads, + head_dim, + gqa_ratio, + block_size, + tile_sz, + scale, + orig_dtype, + fused_request_eligible=False, + capture_request_eligible=False, + return_fp32=( + _FUSED_PREFILL_SHADOW_NUMERIC_MODE + == "calibrated"), + )) + reference_fp32 = ( + reference_result + if _FUSED_PREFILL_SHADOW_NUMERIC_MODE + == "calibrated" + else None + ) + reference_output = ( + reference_result.to(orig_dtype) + if reference_fp32 is not None + else reference_result + ) + shadow_metrics = _compare_fused_prefill_shadow_outputs( + fused_output, + reference_output, + reference_fp32, + ) + except Exception as exc: + _finish_fused_prefill_shadow( + shadow_index, + status="invalid", + error_stage="reference-execution", + error_type=type(exc).__name__, + ) + raise + _finish_fused_prefill_shadow( + shadow_index, + **shadow_metrics, + ) + if ( + shadow_metrics["status"] != "pass" + and ( + _FUSED_PREFILL_SHADOW_FAILURE_ACTION == "raise" + or shadow_metrics["status"] == "invalid" + or shadow_metrics.get("candidate_finite") is not True + or shadow_metrics.get("reference_finite") is not True + ) + ): + raise RuntimeError( + "corex fused paged-prefill failed the real-activation " + "shadow-reference numerical gate") + log_key = "corex_split4" + if log_key not in _PREFIX_DISPATCH_LOGGED: + print( + "[BI100 PAGED_ATTN] prefix_dispatch " + f"pid={os.getpid()} rank={os.environ.get('RANK', '?')} " + f"local_rank={os.environ.get('LOCAL_RANK', '?')} " + f"path={log_key} context_len={block_context_len} " + f"query_len={q_len} required_blocks={required_blocks}", + file=sys.stderr, + flush=True, + ) + _PREFIX_DISPATCH_LOGGED.add(log_key) + return fused_output + + dev = query.device + q_seq = (query.permute(1, 0, 2) + .float() + .view(num_kv_heads, gqa_ratio, q_len, head_dim) + .mul(scale)) + m = torch.full((num_kv_heads, gqa_ratio, q_len), + float('-inf'), dtype=torch.float32, device=dev) + l = torch.zeros_like(m) + o = torch.zeros((num_kv_heads, gqa_ratio, q_len, head_dim), + dtype=torch.float32, device=dev) + + if block_context_len > 0: + num_ctx_blocks = (block_context_len + block_size - 1) // block_size + num_ctx_blocks = PagedAttention._validate_prefix_block_table( + seq_index, num_ctx_blocks, block_tables.shape[1], + block_context_len) + + for block_start, block_end, prefix_start, prefix_end in ( + _prefix_context_tile_spans( + block_context_len, prefix_key.shape[0], tile_sz)): + k_parts = [] + v_parts = [] + if block_end > block_start: + first_block = block_start // block_size + last_block = (block_end + block_size - 1) // block_size + blk_ids = block_tables[seq_index, first_block:last_block] + k_blocks = (key_cache[blk_ids] + .permute(0, 3, 1, 2, 4) + .contiguous() + .view(-1, num_kv_heads, head_dim)) + v_blocks = (value_cache[blk_ids] + .permute(0, 3, 1, 2) + .contiguous() + .view(-1, num_kv_heads, head_dim)) + offset = block_start - first_block * block_size + length = block_end - block_start + k_parts.append(k_blocks[offset:offset + length]) + v_parts.append(v_blocks[offset:offset + length]) + if prefix_end > prefix_start: + k_parts.append(prefix_key[prefix_start:prefix_end]) + v_parts.append(prefix_value[prefix_start:prefix_end]) + k_context = (k_parts[0] if len(k_parts) == 1 + else torch.cat(k_parts, dim=0)) + v_context = (v_parts[0] if len(v_parts) == 1 + else torch.cat(v_parts, dim=0)) + k_t = (k_context.permute(1, 0, 2) + .unsqueeze(1).transpose(-1, -2).float()) + v_t = v_context.permute(1, 0, 2).unsqueeze(1).float() + PagedAttention._update_online_softmax(q_seq, k_t, v_t, m, l, o) + + for key_start in range(0, q_len, tile_sz): + key_end = min(key_start + tile_sz, q_len) + k_t = (key[key_start:key_end].permute(1, 0, 2) + .unsqueeze(1).transpose(-1, -2).float()) + v_t = (value[key_start:key_end].permute(1, 0, 2) + .unsqueeze(1).float()) + scores = torch.matmul(q_seq, k_t) + del k_t + key_positions = torch.arange(key_start, key_end, device=dev) + query_positions = torch.arange(q_len, device=dev) + mask = key_positions.unsqueeze(0) > query_positions.unsqueeze(1) + scores.masked_fill_(mask.unsqueeze(0).unsqueeze(0), float('-inf')) + del mask, key_positions, query_positions + PagedAttention._update_online_softmax_from_scores( + scores, v_t, m, l, o) + + o.div_(l.unsqueeze(-1)) + output = ( + o.view(num_q_heads, q_len, head_dim) + .permute(1, 0, 2) + ) + return output if return_fp32 else output.to(orig_dtype) + + @staticmethod + def _update_online_softmax( + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + running_max: torch.Tensor, + running_sum: torch.Tensor, + running_output: torch.Tensor, + ) -> None: + scores = torch.matmul(query, key) + PagedAttention._update_online_softmax_from_scores( + scores, value, running_max, running_sum, running_output) + + @staticmethod + def _update_online_softmax_from_scores( + scores: torch.Tensor, + value: torch.Tensor, + running_max: torch.Tensor, + running_sum: torch.Tensor, + running_output: torch.Tensor, + ) -> None: + block_max = scores.amax(dim=-1) + new_max = torch.maximum(running_max, block_max) + exp_scores = scores - new_max.unsqueeze(-1) + del scores + exp_scores.exp_() + correction = torch.exp(running_max - new_max) + running_max.copy_(new_max) + running_sum.mul_(correction).add_(exp_scores.sum(dim=-1)) + running_output.mul_(correction.unsqueeze(-1)).add_( + torch.matmul(exp_scores, value)) + + @staticmethod + def swap_blocks( + src_kv_cache: torch.Tensor, + dst_kv_cache: torch.Tensor, + src_to_dst: torch.Tensor, + ) -> None: + src_key_cache = src_kv_cache[0] + dst_key_cache = dst_kv_cache[0] + ops.swap_blocks(src_key_cache, dst_key_cache, src_to_dst) + + src_value_cache = src_kv_cache[1] + dst_value_cache = dst_kv_cache[1] + ops.swap_blocks(src_value_cache, dst_value_cache, src_to_dst) + + @staticmethod + def copy_blocks( + kv_caches: List[torch.Tensor], + src_to_dists: torch.Tensor, + ) -> None: + key_caches = [kv_cache[0] for kv_cache in kv_caches] + value_caches = [kv_cache[1] for kv_cache in kv_caches] + ops.copy_blocks(key_caches, value_caches, src_to_dists) diff --git a/qwen3_6_scripts/patch_block_major_cache_engine.py b/qwen3_6_scripts/patch_block_major_cache_engine.py new file mode 100644 index 0000000..74b8a7c --- /dev/null +++ b/qwen3_6_scripts/patch_block_major_cache_engine.py @@ -0,0 +1,91 @@ +from patch_utils import package_root, replace_once + + +CACHE_ENGINE = package_root("vllm") / "worker" / "cache_engine.py" + +IMPORT_ANCHOR = """\ +from vllm.logger import init_logger +""" + +IMPORT_REPLACEMENT = """\ +from vllm.block_major_kv_cache import ( + BlockMajorCpuKVCache, + block_major_cpu_kv_enabled, +) +from vllm.logger import init_logger +""" + +ALLOCATION_ANCHOR = """\ + self.gpu_cache = self._allocate_kv_cache( + self.num_gpu_blocks, self.device_config.device_type) + self.cpu_cache = self._allocate_kv_cache(self.num_cpu_blocks, "cpu") +""" + +ALLOCATION_REPLACEMENT = """\ + self.gpu_cache = self._allocate_kv_cache( + self.num_gpu_blocks, self.device_config.device_type) + self._bi100_block_major_cpu_kv = None + if block_major_cpu_kv_enabled(): + self._bi100_block_major_cpu_kv = BlockMajorCpuKVCache( + self.gpu_cache, + self.num_cpu_blocks, + pin_memory=is_pin_memory_available(), + ) + self.cpu_cache = self._bi100_block_major_cpu_kv.layer_views + else: + self.cpu_cache = self._allocate_kv_cache( + self.num_cpu_blocks, "cpu") +""" + +SWAP_ANCHOR = """\ + def swap_in(self, src_to_dst: torch.Tensor) -> None: + for i in range(self.num_attention_layers): + self.attn_backend.swap_blocks(self.cpu_cache[i], self.gpu_cache[i], + src_to_dst) + + def swap_out(self, src_to_dst: torch.Tensor) -> None: + for i in range(self.num_attention_layers): + self.attn_backend.swap_blocks(self.gpu_cache[i], self.cpu_cache[i], + src_to_dst) +""" + +SWAP_REPLACEMENT = """\ + def swap_in(self, src_to_dst: torch.Tensor) -> None: + if self._bi100_block_major_cpu_kv is not None: + self._bi100_block_major_cpu_kv.swap_in(src_to_dst) + return + for i in range(self.num_attention_layers): + self.attn_backend.swap_blocks(self.cpu_cache[i], self.gpu_cache[i], + src_to_dst) + + def swap_out(self, src_to_dst: torch.Tensor) -> None: + if self._bi100_block_major_cpu_kv is not None: + self._bi100_block_major_cpu_kv.swap_out(src_to_dst) + return + for i in range(self.num_attention_layers): + self.attn_backend.swap_blocks(self.gpu_cache[i], self.cpu_cache[i], + src_to_dst) +""" + + +replace_once( + CACHE_ENGINE, + IMPORT_ANCHOR, + IMPORT_REPLACEMENT, + required=True, + already_contains="from vllm.block_major_kv_cache import", +) +replace_once( + CACHE_ENGINE, + ALLOCATION_ANCHOR, + ALLOCATION_REPLACEMENT, + required=True, + already_contains="self._bi100_block_major_cpu_kv = None", +) +replace_once( + CACHE_ENGINE, + SWAP_ANCHOR, + SWAP_REPLACEMENT, + required=True, + already_contains="self._bi100_block_major_cpu_kv.swap_in", +) diff --git a/qwen3_6_scripts/patch_block_major_worker_capacity.py b/qwen3_6_scripts/patch_block_major_worker_capacity.py new file mode 100644 index 0000000..599e194 --- /dev/null +++ b/qwen3_6_scripts/patch_block_major_worker_capacity.py @@ -0,0 +1,46 @@ +from patch_utils import package_root, replace_once + + +WORKER = package_root("vllm") / "worker" / "worker.py" + +IMPORT_ANCHOR = """\ +from vllm.logger import init_logger +""" + +IMPORT_REPLACEMENT = """\ +from vllm.block_major_kv_cache import reserve_block_major_gpu_blocks +from vllm.logger import init_logger +""" + +CAPACITY_ANCHOR = """\ + num_gpu_blocks = max(num_gpu_blocks, 0) + num_cpu_blocks = max(num_cpu_blocks, 0) +""" + +CAPACITY_REPLACEMENT = """\ + num_gpu_blocks = reserve_block_major_gpu_blocks( + num_gpu_blocks, cache_block_size) + num_gpu_blocks = max(num_gpu_blocks, 0) + num_cpu_blocks = max(num_cpu_blocks, 0) +""" + + +replace_once( + WORKER, + IMPORT_ANCHOR, + IMPORT_REPLACEMENT, + required=True, + already_contains=( + "from vllm.block_major_kv_cache import " + "reserve_block_major_gpu_blocks" + ), +) +replace_once( + WORKER, + CAPACITY_ANCHOR, + CAPACITY_REPLACEMENT, + required=True, + already_contains=( + "num_gpu_blocks = reserve_block_major_gpu_blocks(" + ), +) diff --git a/qwen3_6_scripts/patch_block_manager_cache_trace.py b/qwen3_6_scripts/patch_block_manager_cache_trace.py new file mode 100644 index 0000000..cc1937d --- /dev/null +++ b/qwen3_6_scripts/patch_block_manager_cache_trace.py @@ -0,0 +1,210 @@ +"""Install the optional BI100 prefix-cache diagnostic trace.""" +from patch_utils import package_root, replace_once, replace_one_of + +VLLM_ROOT = package_root("vllm") +TARGET = VLLM_ROOT / "core" / "block_manager_v2.py" +OUTPUTS_TARGET = VLLM_ROOT / "outputs.py" + +HELPER = ''' + def _bi100_capture_cache_trace(self, seq_group, seq, block_table) -> None: + if os.getenv("BI100_CACHE_TRACE", "0") != "1": + return + + session = getattr(self, "_bi100_trace_session", None) + if session is None: + session = hashlib.sha256(os.urandom(16)).hexdigest()[:16] + self._bi100_trace_session = session + + self._bi100_trace_ordinal = getattr(self, "_bi100_trace_ordinal", 0) + 1 + request_id_sha256 = hashlib.sha256( + str(seq_group.request_id).encode("utf-8")).hexdigest()[:16] + + prompt_tokens = len(seq.get_token_ids()) + requests = getattr(self, "_bi100_trace_requests", None) + if requests is None: + requests = {} + self._bi100_trace_requests = requests + + requests[seq.seq_id] = { + "version": 4, + "trace_session_sha256": session, + "ordinal": self._bi100_trace_ordinal, + "request_id_sha256": request_id_sha256, + "prompt_tokens": prompt_tokens, + "prompt_allocated_blocks": ( + (prompt_tokens + self.block_size - 1) // self.block_size + ), + "block_size": self.block_size, + "capacity_blocks": self.num_total_gpu_blocks, + } + setattr(seq_group, "_bi100_cache_trace_seq_id", seq.seq_id) + setattr(seq_group, "_bi100_cache_trace_emit", + self._bi100_emit_cache_trace) + + def _bi100_update_cache_trace( + self, seq, raw_kv_hit_blocks, restore_key, capture_actions, + evict_keys, policy) -> None: + if os.getenv("BI100_CACHE_TRACE", "0") != "1": + return + requests = getattr(self, "_bi100_trace_requests", None) + if not requests or seq.seq_id not in requests: + return + record = requests[seq.seq_id] + record["gdn_policy"] = policy + if "initial_raw_kv_contiguous_hit_blocks" not in record: + record["initial_raw_kv_contiguous_hit_blocks"] = max( + 0, int(raw_kv_hit_blocks)) + record["gdn_restore_digest_base64"] = ( + base64.b64encode(restore_key[1]).decode("ascii") + if restore_key is not None else None) + record["raw_kv_contiguous_hit_blocks"] = max( + int(raw_kv_hit_blocks), + int(record.get("raw_kv_contiguous_hit_blocks", 0))) + effective_blocks = int(restore_key[0]) if restore_key is not None else 0 + record["effective_gdn_hit_blocks"] = max( + effective_blocks, int(record.get("effective_gdn_hit_blocks", 0))) + + admissions = record.setdefault("gdn_admissions", []) + for key, reason in capture_actions: + admissions.append({ + "block_count": int(key[0]), + "digest_base64": base64.b64encode(key[1]).decode("ascii"), + "reason": str(reason), + }) + evictions = record.setdefault("gdn_evictions", []) + for key in evict_keys: + evictions.append({ + "block_count": int(key[0]), + "digest_base64": base64.b64encode(key[1]).decode("ascii"), + "reason": "capacity_lru", + }) + + def _bi100_finalize_cache_trace(self, seq, block_table) -> None: + if os.getenv("BI100_CACHE_TRACE", "0") != "1": + return + + requests = getattr(self, "_bi100_trace_requests", None) + if not requests: + return + + record = requests.get(seq.seq_id) + if record is None: + return + + total_tokens = len(seq.get_token_ids()) + block_hashes = block_table.get_content_hashes() + for block_hash in block_hashes: + if not isinstance(block_hash, bytes) or len(block_hash) != 32: + raise RuntimeError( + "BI100 cache trace requires 32-byte content hashes") + full_blocks = len(block_hashes) + record.update({ + "total_tokens": total_tokens, + "allocated_blocks": ( + (total_tokens + self.block_size - 1) // self.block_size + ), + "full_blocks": full_blocks, + "hash_encoding": "sha256_base64", + "block_hashes": base64.b64encode(b"".join(block_hashes)).decode("ascii"), + "_finalized": True, + }) + generated_tokens = max(0, total_tokens - record["prompt_tokens"]) + record["generated_tokens"] = generated_tokens + + def _bi100_emit_cache_trace(self, seq_group) -> None: + if os.getenv("BI100_CACHE_TRACE", "0") != "1": + return + seq_id = getattr(seq_group, "_bi100_cache_trace_seq_id", None) + requests = getattr(self, "_bi100_trace_requests", None) + if seq_id is None or not requests: + return + record = requests.pop(seq_id, None) + if record is None: + return + if record.pop("_finalized", False) is not True: + raise RuntimeError( + "BI100 cache trace emitted before block finalization") + + metrics = getattr(seq_group, "metrics", None) + arrival = getattr(metrics, "arrival_time", None) + first_token = getattr(metrics, "first_token_time", None) + finished = getattr(metrics, "finished_time", None) + queue = getattr(metrics, "time_in_queue", None) + cached = getattr(metrics, "num_cached_tokens", None) + if any(value is None for value in ( + arrival, first_token, finished, queue)): + raise RuntimeError( + "BI100 cache trace requires finalized request metrics") + record["ttft_s"] = max(0.0, float(first_token - arrival)) + record["request_latency_s"] = max( + 0.0, float(finished - arrival)) + record["time_in_queue_s"] = max(0.0, float(queue)) + record["observed_effective_cached_tokens"] = max( + 0, int(cached or 0)) + ttft_s = record["ttft_s"] + if ttft_s > 0: + record["observed_input_tps"] = record["prompt_tokens"] / ttft_s + generated_tokens = record["generated_tokens"] + if generated_tokens > 1: + decode_s = finished - first_token + if decode_s > 0: + record["observed_output_tps"] = ( + (generated_tokens - 1) / decode_s) + print("[BI100_CACHE_TRACE] " + json.dumps(record, separators=(",", ":"), + sort_keys=True), flush=True) +''' + + +def main(): + replace_once(TARGET, "from collections.abc import Mapping\n", + "from collections.abc import Mapping\nimport base64\nimport json\nimport os\n", + required=True, already_contains="import base64\n") + replace_once(TARGET, "class BlockSpaceManagerV2(BlockSpaceManager):\n", + "class BlockSpaceManagerV2(BlockSpaceManager):\n" + HELPER, + required=True, already_contains="def _bi100_capture_cache_trace(") + replace_once(TARGET, + " self.block_tables[seq.seq_id] = block_table\n\n # Track seq", + " self.block_tables[seq.seq_id] = block_table\n self._bi100_capture_cache_trace(\n seq_group, seq, block_table)\n\n # Track seq", + required=True, + already_contains="self.block_tables[seq.seq_id] = block_table\n" + " self._bi100_capture_cache_trace(") + replacements = [] + for table_key in ("seq_id", "seq.seq_id"): + prefix = ( + " self._last_access_blocks_tracker." + "update_seq_blocks_last_access(\n" + f" seq_id, self.block_tables[{table_key}]." + "physical_block_ids)\n") + replacements.append(( + prefix + "\n # Untrack seq", + prefix + " self._bi100_finalize_cache_trace(\n" + f" seq, self.block_tables[{table_key}])\n\n" + " # Untrack seq", + )) + replace_one_of( + TARGET, + replacements, + required=True, + already_contains=" self._bi100_finalize_cache_trace(\n" + " seq, self.block_tables[") + replace_once( + OUTPUTS_TARGET, + " seq_group.set_finished_time(finished_time)\n\n" + " init_args = (seq_group.request_id, prompt, prompt_token_ids,\n", + " seq_group.set_finished_time(finished_time)\n" + " if finished_time is not None:\n" + " cache_trace_emit = getattr(\n" + " seq_group, \"_bi100_cache_trace_emit\", None)\n" + " if callable(cache_trace_emit):\n" + " cache_trace_emit(seq_group)\n" + " delattr(seq_group, \"_bi100_cache_trace_emit\")\n" + " delattr(seq_group, \"_bi100_cache_trace_seq_id\")\n\n" + " init_args = (seq_group.request_id, prompt, prompt_token_ids,\n", + required=True, + already_contains="if finished_time is not None:\n" + " cache_trace_emit = getattr(\n", + ) + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_corex_swap_blocks.py b/qwen3_6_scripts/patch_corex_swap_blocks.py new file mode 100644 index 0000000..0988bff --- /dev/null +++ b/qwen3_6_scripts/patch_corex_swap_blocks.py @@ -0,0 +1,65 @@ +from patch_utils import package_root, replace_once + + +CUSTOM_OPS = package_root("vllm") / "_custom_ops.py" + +CLEAN_BLOCK = """\ +def swap_blocks(src: torch.Tensor, dst: torch.Tensor, + block_mapping: torch.Tensor) -> None: + ixf_F.swap_blocks(src, dst, block_mapping) +""" + +COMPATIBLE_BLOCK = """\ +def swap_blocks(src: torch.Tensor, dst: torch.Tensor, + block_mapping: torch.Tensor) -> None: + # BI100 CoreX 3.2.3 exposes vllm_swap_blocks, while this vLLM build calls + # the newer swap_blocks name. Normalize the worker's CPU int64 [N, 2] + # tensor only for the legacy public API and fail fast on malformed maps. + native_swap_blocks = getattr(ixf_F, "swap_blocks", None) + if native_swap_blocks is not None: + native_swap_blocks(src, dst, block_mapping) + return + + vendor_swap_blocks = getattr(ixf_F, "vllm_swap_blocks", None) + if vendor_swap_blocks is None: + raise RuntimeError( + "ixformer exposes neither swap_blocks nor vllm_swap_blocks") + + if isinstance(block_mapping, torch.Tensor): + if block_mapping.device.type != "cpu": + raise ValueError("swap block mapping must be a CPU tensor") + if block_mapping.dtype != torch.int64: + raise ValueError("swap block mapping must use torch.int64") + if block_mapping.dim() != 2 or block_mapping.shape[1] != 2: + raise ValueError("swap block mapping must have shape [N, 2]") + pairs = block_mapping.tolist() + elif isinstance(block_mapping, dict): + pairs = list(block_mapping.items()) + else: + raise TypeError("swap block mapping must be a tensor or dict") + + normalized_mapping = {} + destinations = set() + for source, destination in pairs: + source = int(source) + destination = int(destination) + if source < 0 or destination < 0: + raise ValueError("swap block indices must be non-negative") + if source in normalized_mapping: + raise ValueError(f"duplicate swap source block: {source}") + if destination in destinations: + raise ValueError( + f"duplicate swap destination block: {destination}") + normalized_mapping[source] = destination + destinations.add(destination) + vendor_swap_blocks(src, dst, normalized_mapping) +""" + + +replace_once( + CUSTOM_OPS, + CLEAN_BLOCK, + COMPATIBLE_BLOCK, + required=True, + already_contains="BI100 CoreX 3.2.3 exposes vllm_swap_blocks", +) diff --git a/qwen3_6_scripts/patch_executor_startup_debug.py b/qwen3_6_scripts/patch_executor_startup_debug.py new file mode 100644 index 0000000..c554e95 --- /dev/null +++ b/qwen3_6_scripts/patch_executor_startup_debug.py @@ -0,0 +1,61 @@ +from patch_utils import package_root, replace_once + +VLLM_ROOT = package_root("vllm") + +MULTIPROC_GPU_EXECUTOR = VLLM_ROOT / "executor" / "multiproc_gpu_executor.py" +MULTIPROC_WORKER_UTILS = VLLM_ROOT / "executor" / "multiproc_worker_utils.py" + + +def ensure_import_os(path): + text = path.read_text() + if "import os\n" in text: + print(f"[skip] import os already present: {path}") + return + for anchor in ("import time\n", "import signal\n", "import sys\n"): + if anchor in text: + replace_once( + path, + anchor, + anchor + "import os\n", + required=True, + already_contains="import os\n", + ) + return + raise RuntimeError(f"no import anchor found for os in {path}") + + +ensure_import_os(MULTIPROC_GPU_EXECUTOR) +ensure_import_os(MULTIPROC_WORKER_UTILS) + + +replace_once( + MULTIPROC_GPU_EXECUTOR, + """logger = init_logger(__name__)\n""", + """logger = init_logger(__name__)\n\n\ndef _bi100_startup_debug(message: str, *args) -> None:\n if os.getenv(\"BI100_EXECUTOR_STARTUP_DEBUG\") == \"1\":\n logger.info(\"[BI100 startup] \" + message, *args)\n""", + required=True, + already_contains="def _bi100_startup_debug(", +) + +replace_once( + MULTIPROC_GPU_EXECUTOR, + """ self.driver_worker = self._create_worker(\n distributed_init_method=distributed_init_method)\n self._run_workers(\"init_device\")\n self._run_workers(\"load_model\",\n max_concurrent_workers=self.parallel_config.\n max_parallel_loading_workers)\n""", + """ _bi100_startup_debug(\"creating driver worker\")\n self.driver_worker = self._create_worker(\n distributed_init_method=distributed_init_method)\n _bi100_startup_debug(\"created driver worker\")\n _bi100_startup_debug(\"starting init_device\")\n self._run_workers(\"init_device\")\n _bi100_startup_debug(\"finished init_device\")\n _bi100_startup_debug(\"starting load_model\")\n self._run_workers(\"load_model\",\n max_concurrent_workers=self.parallel_config.\n max_parallel_loading_workers)\n _bi100_startup_debug(\"finished load_model\")\n""", + required=True, + already_contains='_bi100_startup_debug("starting init_device")', +) + +replace_once( + MULTIPROC_GPU_EXECUTOR, + """ # Start all remote workers first.\n worker_outputs = [\n worker.execute_method(method, *args, **kwargs)\n for worker in self.workers\n ]\n\n driver_worker_method = getattr(self.driver_worker, method)\n driver_worker_output = driver_worker_method(*args, **kwargs)\n\n # Get the results of the workers.\n return [driver_worker_output\n ] + [output.get() for output in worker_outputs]\n""", + """ _bi100_startup_debug(\"enqueue remote method=%s workers=%d\", method,\n len(self.workers))\n # Start all remote workers first.\n worker_outputs = [\n worker.execute_method(method, *args, **kwargs)\n for worker in self.workers\n ]\n _bi100_startup_debug(\"remote enqueued method=%s\", method)\n\n driver_worker_method = getattr(self.driver_worker, method)\n _bi100_startup_debug(\"driver start method=%s\", method)\n driver_worker_output = driver_worker_method(*args, **kwargs)\n _bi100_startup_debug(\"driver done method=%s\", method)\n\n # Get the results of the workers.\n _bi100_startup_debug(\"waiting remote results method=%s\", method)\n remote_outputs = [output.get() for output in worker_outputs]\n _bi100_startup_debug(\"remote done method=%s\", method)\n return [driver_worker_output] + remote_outputs\n""", + required=True, + already_contains='_bi100_startup_debug("enqueue remote method=%s workers=%d"', +) + +replace_once( + MULTIPROC_WORKER_UTILS, + """ task_id, method, args, kwargs = items\n try:\n executor = getattr(worker, method)\n output = executor(*args, **kwargs)\n except SystemExit:\n""", + """ task_id, method, args, kwargs = items\n if os.getenv(\"BI100_EXECUTOR_STARTUP_DEBUG\") == \"1\":\n logger.info(\"[BI100 worker] start method=%s\", method)\n try:\n executor = getattr(worker, method)\n output = executor(*args, **kwargs)\n if os.getenv(\"BI100_EXECUTOR_STARTUP_DEBUG\") == \"1\":\n logger.info(\"[BI100 worker] done method=%s\", method)\n except SystemExit:\n""", + required=True, + already_contains='logger.info("[BI100 worker] start method=%s", method)', +) diff --git a/qwen3_6_scripts/patch_model_runner.py b/qwen3_6_scripts/patch_model_runner.py new file mode 100644 index 0000000..a74f94e --- /dev/null +++ b/qwen3_6_scripts/patch_model_runner.py @@ -0,0 +1,408 @@ +"""Patch vLLM 0.6.3 prefix-cache and MRoPE chunk alignment bugs.""" + +from __future__ import annotations + +import pathlib + +from patch_utils import package_root, replace_once + + +HELPER_ANCHOR = """\ +logger = init_logger(__name__) + +LORA_WARMUP_RANK = 8""" + +HELPER_REPLACEMENT = """\ +logger = init_logger(__name__) + + +def _slice_mrope_positions(positions, start, stop, expected_len): + if positions is None or len(positions) != 3: + raise RuntimeError("MRoPE positions must contain three axes") + sliced = [axis[start:stop] for axis in positions] + lengths = [len(axis) for axis in sliced] + if lengths != [expected_len] * 3: + raise RuntimeError( + "MRoPE/input token length mismatch after chunk alignment: " + f"positions={lengths}, input_tokens={expected_len}, " + f"slice=({start}, {stop})") + return sliced + + +LORA_WARMUP_RANK = 8""" + +PREFIX_PAST_ANCHOR = """\ + if prefix_cache_len <= context_len: + # We already passed the cache hit region, + # so do normal computation. + pass""" + +PREFIX_PAST_REPLACEMENT = """\ + if prefix_cache_len <= context_len: + # We already passed the cache hit region, + # so do normal computation. + # Must clear prefix_cache_hit so _add_seq_group uses the full + # block_tables (prefix + previous-chunk blocks) instead of only + # computed_block_nums (prefix only). Without this, block_tables + # passed to _forward_prefix_pytorch is too narrow for context_len, + # causing an empty blk_ids slice and a zero-dim amax() crash. + inter_data.prefix_cache_hit = False""" + +PARTIAL_HIT_ANCHOR = """\ + inter_data.input_positions[seq_idx] = inter_data.input_positions[ + seq_idx][uncomputed_start:] + context_len = prefix_cache_len + + inter_data.context_lens[seq_idx] = context_len + inter_data.query_lens[ + seq_idx] = inter_data.seq_lens[seq_idx] - context_len""" + +PARTIAL_HIT_REPLACEMENT = """\ + inter_data.input_positions[seq_idx] = inter_data.input_positions[ + seq_idx][uncomputed_start:] + context_len = prefix_cache_len + + inter_data.context_lens[seq_idx] = context_len + inter_data.query_lens[ + seq_idx] = inter_data.seq_lens[seq_idx] - context_len + if inter_data.mrope_input_positions is not None: + positions = inter_data.mrope_input_positions[seq_idx] + if positions is not None: + inter_data.mrope_input_positions[seq_idx] = \\ + _slice_mrope_positions( + positions, uncomputed_start, None, + inter_data.query_lens[seq_idx])""" + +FULL_HIT_ANCHOR = """\ + inter_data.input_positions[seq_idx] = inter_data.input_positions[ + seq_idx][-1:] + inter_data.query_lens[seq_idx] = 1 + inter_data.context_lens[seq_idx] = inter_data.seq_lens[seq_idx] - 1""" + +FULL_HIT_REPLACEMENT = """\ + inter_data.input_positions[seq_idx] = inter_data.input_positions[ + seq_idx][-1:] + inter_data.query_lens[seq_idx] = 1 + inter_data.context_lens[seq_idx] = inter_data.seq_lens[seq_idx] - 1 + if inter_data.mrope_input_positions is not None: + positions = inter_data.mrope_input_positions[seq_idx] + if positions is not None: + inter_data.mrope_input_positions[seq_idx] = \\ + _slice_mrope_positions(positions, -1, None, 1)""" + +MULTIMODAL_MROPE_ANCHOR = """\ + mrope_input_positions, mrope_position_delta = \\ + MRotaryEmbedding.get_input_positions( + token_ids, + image_grid_thw=image_grid_thw, + video_grid_thw=video_grid_thw, + image_token_id=hf_config.image_token_id, + video_token_id=hf_config.video_token_id, + vision_start_token_id=hf_config.vision_start_token_id, + vision_end_token_id=hf_config.vision_end_token_id, + spatial_merge_size=hf_config.vision_config. + spatial_merge_size, + context_len=inter_data.context_lens[seq_idx], + ) + + seq_data.mrope_position_delta = mrope_position_delta + inter_data.mrope_input_positions[ + seq_idx] = mrope_input_positions""" + +MULTIMODAL_MROPE_REPLACEMENT = """\ + # vLLM 0.6.3 returns positions through the end of token_ids, + # while chunked prefill sends only [context_len:seq_len]. + # Compute the full MRoPE map once so the delta remains tied to + # the complete request, then select exactly the physical query. + mrope_input_positions, mrope_position_delta = \\ + MRotaryEmbedding.get_input_positions( + token_ids, + image_grid_thw=image_grid_thw, + video_grid_thw=video_grid_thw, + image_token_id=hf_config.image_token_id, + video_token_id=hf_config.video_token_id, + vision_start_token_id=hf_config.vision_start_token_id, + vision_end_token_id=hf_config.vision_end_token_id, + spatial_merge_size=hf_config.vision_config. + spatial_merge_size, + context_len=0, + ) + mrope_input_positions = _slice_mrope_positions( + mrope_input_positions, + inter_data.context_lens[seq_idx], + inter_data.seq_lens[seq_idx], + len(inter_data.input_tokens[seq_idx])) + + seq_data.mrope_position_delta = mrope_position_delta + inter_data.mrope_input_positions[ + seq_idx] = mrope_input_positions""" + +MODEL_INPUT_FIELDS_ANCHOR = """\ + multi_modal_kwargs: Optional[BatchedTensorInputs] = None + request_ids_to_seq_ids: Optional[Dict[str, List[int]]] = None""" + +MODEL_INPUT_FIELDS_REPLACEMENT = """\ + multi_modal_kwargs: Optional[BatchedTensorInputs] = None + # BI100 scheduler-owned GDN prefix-cache actions. These plain Python + # objects are included in the multiprocess model-input broadcast. + gdn_restore_key: Optional[Tuple[int, bytes]] = None + gdn_capture_points: Optional[List[Tuple[int, Tuple[int, bytes]]]] = None + gdn_evict_keys: Optional[List[Tuple[int, bytes]]] = None + gdn_segment_offsets: Optional[List[int]] = None + request_ids_to_seq_ids: Optional[Dict[str, List[int]]] = None""" + +BASE_BROADCAST_ANCHOR = """\ + \"multi_modal_kwargs\": self.multi_modal_kwargs, + \"prompt_adapter_mapping\": self.prompt_adapter_mapping, + \"prompt_adapter_requests\": self.prompt_adapter_requests, + \"virtual_engine\": self.virtual_engine, + \"request_ids_to_seq_ids\": self.request_ids_to_seq_ids, + \"finished_requests_ids\": self.finished_requests_ids, + } + _add_attn_metadata_broadcastable_dict(tensor_dict, self.attn_metadata) + return tensor_dict + + @classmethod""" + +BASE_BROADCAST_REPLACEMENT = """\ + \"multi_modal_kwargs\": self.multi_modal_kwargs, + \"gdn_restore_key\": self.gdn_restore_key, + \"gdn_capture_points\": self.gdn_capture_points, + \"gdn_evict_keys\": self.gdn_evict_keys, + \"gdn_segment_offsets\": self.gdn_segment_offsets, + \"prompt_adapter_mapping\": self.prompt_adapter_mapping, + \"prompt_adapter_requests\": self.prompt_adapter_requests, + \"virtual_engine\": self.virtual_engine, + \"request_ids_to_seq_ids\": self.request_ids_to_seq_ids, + \"finished_requests_ids\": self.finished_requests_ids, + } + _add_attn_metadata_broadcastable_dict(tensor_dict, self.attn_metadata) + return tensor_dict + + @classmethod""" + +SAMPLING_BROADCAST_ANCHOR = """\ + \"multi_modal_kwargs\": self.multi_modal_kwargs, + \"prompt_adapter_mapping\": self.prompt_adapter_mapping, + \"prompt_adapter_requests\": self.prompt_adapter_requests, + \"virtual_engine\": self.virtual_engine, + \"request_ids_to_seq_ids\": self.request_ids_to_seq_ids, + \"finished_requests_ids\": self.finished_requests_ids, + } + _add_attn_metadata_broadcastable_dict(tensor_dict, self.attn_metadata) + _add_sampling_metadata_broadcastable_dict(tensor_dict, + self.sampling_metadata)""" + +SAMPLING_BROADCAST_REPLACEMENT = """\ + \"multi_modal_kwargs\": self.multi_modal_kwargs, + \"gdn_restore_key\": self.gdn_restore_key, + \"gdn_capture_points\": self.gdn_capture_points, + \"gdn_evict_keys\": self.gdn_evict_keys, + \"gdn_segment_offsets\": self.gdn_segment_offsets, + \"prompt_adapter_mapping\": self.prompt_adapter_mapping, + \"prompt_adapter_requests\": self.prompt_adapter_requests, + \"virtual_engine\": self.virtual_engine, + \"request_ids_to_seq_ids\": self.request_ids_to_seq_ids, + \"finished_requests_ids\": self.finished_requests_ids, + } + _add_attn_metadata_broadcastable_dict(tensor_dict, self.attn_metadata) + _add_sampling_metadata_broadcastable_dict(tensor_dict, + self.sampling_metadata)""" + +BUILDER_INIT_ANCHOR = """\ + self.finished_requests_ids = finished_requests_ids + self.decode_only = True + + # Intermediate data""" + +BUILDER_INIT_REPLACEMENT = """\ + self.finished_requests_ids = finished_requests_ids + self.decode_only = True + self.gdn_restore_key = None + self.gdn_capture_points = None + self.gdn_evict_keys = None + self.gdn_segment_offsets = None + + # Intermediate data""" + +ADD_SEQ_GROUP_ANCHOR = """\ + def add_seq_group(self, seq_group_metadata: SequenceGroupMetadata): + \"\"\"Add a sequence group to the builder.\"\"\" + seq_ids = seq_group_metadata.seq_data.keys()""" + +ADD_SEQ_GROUP_REPLACEMENT = """\ + def add_seq_group(self, seq_group_metadata: SequenceGroupMetadata): + \"\"\"Add a sequence group to the builder.\"\"\" + gdn_actions = ( + seq_group_metadata.gdn_restore_key, + seq_group_metadata.gdn_capture_points, + seq_group_metadata.gdn_evict_keys, + seq_group_metadata.gdn_segment_offsets, + ) + if any(value is not None for value in gdn_actions): + if not seq_group_metadata.is_prompt: + raise RuntimeError(\"GDN prefix-cache actions require prefill\") + if any(value is not None for value in ( + self.gdn_restore_key, self.gdn_capture_points, + self.gdn_evict_keys, self.gdn_segment_offsets)): + raise RuntimeError( + \"only one GDN prefix-cache action group is supported\") + (self.gdn_restore_key, self.gdn_capture_points, + self.gdn_evict_keys, self.gdn_segment_offsets) = gdn_actions + seq_ids = seq_group_metadata.seq_data.keys()""" + +BUILD_RESULT_ANCHOR = """\ + lora_mapping=lora_mapping, + lora_requests=lora_requests, + multi_modal_kwargs=multi_modal_kwargs, + request_ids_to_seq_ids=request_ids_to_seq_ids,""" + +BUILD_RESULT_REPLACEMENT = """\ + lora_mapping=lora_mapping, + lora_requests=lora_requests, + multi_modal_kwargs=multi_modal_kwargs, + gdn_restore_key=self.gdn_restore_key, + gdn_capture_points=self.gdn_capture_points, + gdn_evict_keys=self.gdn_evict_keys, + gdn_segment_offsets=self.gdn_segment_offsets, + request_ids_to_seq_ids=request_ids_to_seq_ids,""" + +EXECUTE_KWARGS_ANCHOR = """\ + seqlen_agnostic_kwargs = { + \"finished_requests_ids\": model_input.finished_requests_ids, + \"request_ids_to_seq_ids\": model_input.request_ids_to_seq_ids, + } if self.has_inner_state else {} + if (self.observability_config is not None""" + +EXECUTE_KWARGS_REPLACEMENT = """\ + seqlen_agnostic_kwargs = { + \"finished_requests_ids\": model_input.finished_requests_ids, + \"request_ids_to_seq_ids\": model_input.request_ids_to_seq_ids, + } if self.has_inner_state else {} + gdn_prefix_kwargs = {} + if model_input.gdn_restore_key is not None: + gdn_prefix_kwargs[\"gdn_restore_key\"] = model_input.gdn_restore_key + if model_input.gdn_capture_points is not None: + gdn_prefix_kwargs[\"gdn_capture_points\"] = ( + model_input.gdn_capture_points) + if model_input.gdn_evict_keys is not None: + gdn_prefix_kwargs[\"gdn_evict_keys\"] = model_input.gdn_evict_keys + if model_input.gdn_segment_offsets is not None: + gdn_prefix_kwargs[\"gdn_segment_offsets\"] = ( + model_input.gdn_segment_offsets) + if (self.observability_config is not None""" + +MODEL_CALL_ANCHOR = """\ + **MultiModalInputs.as_kwargs(multi_modal_kwargs, + device=self.device), + **seqlen_agnostic_kwargs)""" + +MODEL_CALL_REPLACEMENT = """\ + **MultiModalInputs.as_kwargs(multi_modal_kwargs, + device=self.device), + **seqlen_agnostic_kwargs, + **gdn_prefix_kwargs)""" + +PROFILE_KV_LAYERS_ANCHOR = """\ + num_layers = self.model_config.get_num_layers(self.parallel_config)""" + +PROFILE_KV_LAYERS_REPLACEMENT = """\ + num_layers = self.model_config.get_num_attention_layers( + self.parallel_config)""" + + +def patch_model_runner(model_runner: pathlib.Path) -> None: + replace_once( + model_runner, + HELPER_ANCHOR, + HELPER_REPLACEMENT, + required=True, + already_contains="def _slice_mrope_positions(", + ) + replace_once( + model_runner, + PREFIX_PAST_ANCHOR, + PREFIX_PAST_REPLACEMENT, + required=True, + already_contains="Must clear prefix_cache_hit so _add_seq_group", + ) + replace_once( + model_runner, + PARTIAL_HIT_ANCHOR, + PARTIAL_HIT_REPLACEMENT, + required=True, + already_contains="positions, uncomputed_start, None,", + ) + replace_once( + model_runner, + FULL_HIT_ANCHOR, + FULL_HIT_REPLACEMENT, + required=True, + already_contains="_slice_mrope_positions(positions, -1, None, 1)", + ) + replace_once( + model_runner, + MULTIMODAL_MROPE_ANCHOR, + MULTIMODAL_MROPE_REPLACEMENT, + required=True, + already_contains="Compute the full MRoPE map once", + ) + replace_once( + model_runner, + MODEL_INPUT_FIELDS_ANCHOR, + MODEL_INPUT_FIELDS_REPLACEMENT, + already_contains="gdn_restore_key: Optional[Tuple[int, bytes]]", + ) + replace_once( + model_runner, + BASE_BROADCAST_ANCHOR, + BASE_BROADCAST_REPLACEMENT, + already_contains=BASE_BROADCAST_REPLACEMENT, + ) + replace_once( + model_runner, + SAMPLING_BROADCAST_ANCHOR, + SAMPLING_BROADCAST_REPLACEMENT, + already_contains=SAMPLING_BROADCAST_REPLACEMENT, + ) + replace_once( + model_runner, + BUILDER_INIT_ANCHOR, + BUILDER_INIT_REPLACEMENT, + already_contains="self.gdn_restore_key = None", + ) + replace_once( + model_runner, + ADD_SEQ_GROUP_ANCHOR, + ADD_SEQ_GROUP_REPLACEMENT, + already_contains="gdn_actions = (", + ) + replace_once( + model_runner, + BUILD_RESULT_ANCHOR, + BUILD_RESULT_REPLACEMENT, + already_contains="gdn_restore_key=self.gdn_restore_key", + ) + replace_once( + model_runner, + EXECUTE_KWARGS_ANCHOR, + EXECUTE_KWARGS_REPLACEMENT, + already_contains="gdn_prefix_kwargs = {}", + ) + replace_once( + model_runner, + MODEL_CALL_ANCHOR, + MODEL_CALL_REPLACEMENT, + already_contains="**gdn_prefix_kwargs)", + ) + replace_once( + model_runner, + PROFILE_KV_LAYERS_ANCHOR, + PROFILE_KV_LAYERS_REPLACEMENT, + required=True, + already_contains=PROFILE_KV_LAYERS_REPLACEMENT, + ) + + +if __name__ == "__main__": + patch_model_runner(package_root("vllm") / "worker" / "model_runner.py") diff --git a/qwen3_6_scripts/patch_ops.sh b/qwen3_6_scripts/patch_ops.sh new file mode 100755 index 0000000..3f70c7c --- /dev/null +++ b/qwen3_6_scripts/patch_ops.sh @@ -0,0 +1,251 @@ +#!/usr/bin/env bash +# BI-V100 patch script for Qwen3.6-35B-A3B (Qwen3_5 MoE architecture) +# +# Triton situation on BI-V100: +# - Standard Triton 2.3.1 is already present in the image. +# - HAS_TRITON = False (hardcoded in vendor vllm), but Triton is still used +# for TP-mode cache management (custom_cache_manager / libentry). +# - The vendor's triton_utils/__init__.py, custom_cache_manager.py, libentry.py +# are already correct for standard Triton 2.3.1 — do NOT overwrite them. +# - DO NOT install BI-V150 corex Triton 2.1.0 (pkgs/triton): that causes +# GPU hang on BI-V100 because the Triton CUDA PTX kernels are incompatible. + +# Recommended server start command for TP=4 support 256K, needs chunked prefill +# CUDA_VISIBLE_DEVICES="4,5,6,7" VLLM_ENGINE_ITERATION_TIMEOUT_S=3600 python3 -m vllm.entrypoints.openai.api_server \ +# --model /workspace/models/Qwen3.6-35B-A3B --port 1111 --served-model-name llm \ +# --max-model-len 262144 --trust-remote-code -tp 4 --gpu-memory-utilization 0.90 \ +# --max-num-seqs 1 --disable-log-requests --disable-frontend-multiprocessing \ +# --max-num-batched-tokens 8192 --enable-chunked-prefill --enable-prefix-caching \ +# --max-seq-len-to-capture 32768 --enable-auto-tool-choice \ +# --tool-call-parser qwen3_coder --reasoning-parser qwen3 +# +# With prefix caching (GDN align-mode, requires chunked prefill): +# CUDA_VISIBLE_DEVICES="4,5,6,7" VLLM_ENGINE_ITERATION_TIMEOUT_S=3600 python3 -m vllm.entrypoints.openai.api_server \ +# --model /workspace/models/Qwen3.6-35B-A3B --port 1111 --served-model-name llm \ +# --max-model-len 262144 --trust-remote-code -tp 4 --gpu-memory-utilization 0.90 \ +# --max-num-seqs 1 --disable-log-requests --disable-frontend-multiprocessing \ +# --max-num-batched-tokens 8192 --enable-chunked-prefill --enable-prefix-caching \ +# --max-seq-len-to-capture 32768 --enable-auto-tool-choice \ +# --tool-call-parser qwen3_coder --reasoning-parser qwen3 + +set -eo pipefail + +# cd into this script's directory so ./relative paths work +cd "$(dirname "${BASH_SOURCE[0]}")" +echo "[patch_ops] working directory: $(pwd)" + +build_stage() { printf '[BI100 BUILD] %s\n' "$1" >&2; } +require_file() { + local path=$1 + [[ -f "$path" ]] || { + printf 'required patch source is missing: %s\n' "$path" >&2 + exit 2 + } +} +install_patch_file() { + local source=$1 + local target=$2 + + require_file "$source" + mkdir -p "$(dirname "$target")" + install -m 0644 "$source" "$target" +} + +build_stage "patch script entered" + +build_stage "checking offline transformers dependency" +# --- transformers: Qwen3_5 tokenizer / model files -------------------------- +TRANSFORMERS_REQUIRED_VERSION="4.55.3" +if ! python3 - "$TRANSFORMERS_REQUIRED_VERSION" <<'PY' +import importlib.metadata +import sys + +required = sys.argv[1] +try: + installed = importlib.metadata.version("transformers") +except importlib.metadata.PackageNotFoundError: + raise SystemExit(1) +raise SystemExit(0 if installed == required else 1) +PY +then + WHEEL_DIR="./wheels" + if ! ls "${WHEEL_DIR}/transformers-${TRANSFORMERS_REQUIRED_VERSION}"*.whl >/dev/null 2>&1; then + echo "transformers ${TRANSFORMERS_REQUIRED_VERSION} is required, but no offline wheel was found in ${WHEEL_DIR}" >&2 + exit 2 + fi + python3 -m pip install --no-index --no-deps --find-links="${WHEEL_DIR}" \ + "transformers==${TRANSFORMERS_REQUIRED_VERSION}" +fi + +python3 - "$TRANSFORMERS_REQUIRED_VERSION" <<'PY' +import importlib.metadata +import sys + +required = sys.argv[1] +installed = importlib.metadata.version("transformers") +if installed != required: + raise SystemExit( + f"transformers version mismatch: expected {required}, got {installed}") +print(f"[ok] transformers {installed}") +PY + +build_stage "discovering Python package roots" +python3 - <<'PY' > /tmp/qwen36_patch_paths.env +from patch_utils import package_root, shell_env_line + +print(shell_env_line("VLLM_ROOT", package_root("vllm"))) +print(shell_env_line("TRANSFORMERS_ROOT", package_root("transformers"))) +PY +source /tmp/qwen36_patch_paths.env + +echo "VLLM_ROOT=${VLLM_ROOT}" +echo "TRANSFORMERS_ROOT=${TRANSFORMERS_ROOT}" +[[ -d "$VLLM_ROOT" ]] || { + printf 'vLLM root does not exist: %s\n' "$VLLM_ROOT" >&2 + exit 2 +} + +VLLM_OVERRIDE_ROOT="./vendor_overrides/vllm" +[[ -d "$VLLM_OVERRIDE_ROOT" ]] || { + printf 'vLLM override directory missing: %s\n' "$VLLM_OVERRIDE_ROOT" >&2 + exit 2 +} + +build_stage "installing authoritative vLLM core block overrides" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/evictor_v2.py" \ + "${VLLM_ROOT}/core/evictor_v2.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/block/cpu_kv_content_cache.py" \ + "${VLLM_ROOT}/core/block/cpu_kv_content_cache.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/block/cpu_gpu_block_allocator.py" \ + "${VLLM_ROOT}/core/block/cpu_gpu_block_allocator.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/block/prefix_caching_block.py" \ + "${VLLM_ROOT}/core/block/prefix_caching_block.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/block/block_table.py" \ + "${VLLM_ROOT}/core/block/block_table.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/core/block_manager_v2.py" \ + "${VLLM_ROOT}/core/block_manager_v2.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/sampling_params.py" \ + "${VLLM_ROOT}/sampling_params.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/model_executor/sampling_metadata.py" \ + "${VLLM_ROOT}/model_executor/sampling_metadata.py" +install_patch_file \ + "${VLLM_OVERRIDE_ROOT}/model_executor/layers/sampler.py" \ + "${VLLM_ROOT}/model_executor/layers/sampler.py" + +build_stage "installing hash-pinned CoreX 3.2.3 extensions" +bash ./install_prebuilt_corex.sh "${VLLM_ROOT}" + +build_stage "installing BI100 runtime modules" +cp ./bi100_env.py "${VLLM_ROOT}/bi100_env.py" +cp ./bi100_profile.py "${VLLM_ROOT}/bi100_profile.py" +cp ./block_major_kv_cache.py "${VLLM_ROOT}/block_major_kv_cache.py" +cp ./gdn_prefix.py "${VLLM_ROOT}/gdn_prefix.py" + +build_stage "installing CoreX paged-KV swap compatibility" +python3 ./patch_corex_swap_blocks.py +python3 ./patch_block_major_cache_engine.py +python3 ./patch_worker_cache_transfer_order.py + +# --- paged_attn.py: replace forward_prefix with pure-PyTorch fallback ------- +# The Triton context_attention_fwd kernel hangs BI-V100 GPUs permanently +# (standard Triton 2.3.1 PTX is not supported by the corex runtime either). +# Our paged_attn.py bypasses it entirely via _forward_prefix_pytorch, which +# utilizes K-tiling techniques, and also have _forward_decode_pytorch to bypass kernel +# when context length is high +cp ./paged_attn.py "${VLLM_ROOT}/attention/ops/paged_attn.py" + +# --- model_runner.py: fix prefix_cache_hit stays True in chunked-prefill chunk 2+ --- +# Bug: _compute_for_prefix_cache_hit Case 1 (prefix_cache_len <= context_len) +# leaves prefix_cache_hit=True. Then _add_seq_group uses block_table=computed_block_nums +# (only the original prefix blocks), ignoring chunk-1 KV cache blocks. +# _forward_prefix_pytorch then gets an undersized block_tables and crashes with +# "amax(): Expected reduction dim -1 to have non-zero size" on the 2nd tile. +# Fix: set prefix_cache_hit=False for Case 1 so the full block_tables is used. +python3 ./patch_model_runner.py + +build_stage "installing executor startup diagnostics" +python3 ./patch_executor_startup_debug.py +python3 ./patch_worker_startup_profile_guard.py +python3 ./patch_block_major_worker_capacity.py + +build_stage "installing transformers Qwen3.5 model support" +cp -r ./qwen3_5 "${TRANSFORMERS_ROOT}/models/" +cp -r ./qwen3_5_moe "${TRANSFORMERS_ROOT}/models/" +python3 ./patch_transformers_qwen3_5.py + +build_stage "installing vLLM Qwen3.6 model implementation" +# --- vllm model: Qwen3.6-35B-A3B (Qwen3_5 MoE arch) ------------------------- +cp ./mamba_cache.py "${VLLM_ROOT}/model_executor/models/" +cp ./qwen3_5.py "${VLLM_ROOT}/model_executor/models/qwen3_5.py" +python3 ./patch_vllm_qwen3_5.py + +# --- sequence.py: fix completion_tokens inflation under chunked prefill ------ +# Bug: get_output_token_ids_to_return(delta=True) with num_new_tokens=0 +# returns _cached_all_token_ids[-0:] == [0:] (the ENTIRE prompt+output list). +# Each prefill chunk step adds prompt_len to previous_num_tokens, so a 10K +# prompt processed in 3 chunks inflates completion_tokens by ~30K. +# Also adds num_cached_tokens field to RequestMetrics for prefix-cache stats. +cp ./sequence.py "${VLLM_ROOT}/sequence.py" + +# --- scheduler.py: record num_cached_tokens in RequestMetrics ---------------- +# Reports only the longest prefix backed by both live KV blocks and an exact +# GDN restore state. Raw KV-only hits must not inflate cached_tokens. +# serving_chat.py exposes the value in the OpenAI-compatible usage details. +cp ./scheduler.py "${VLLM_ROOT}/core/scheduler.py" + +build_stage "installing diagnostic initial allocation trace" +python3 ./patch_block_manager_cache_trace.py + +build_stage "installing scheduler and attention patches" +# --- xformers: bypass cudnnFlashAttnForward (head_dim=256 > 128 limit) ------ +# Injects _run_sdpa_fallback (pure matmul+softmax) into xformers.py. +# Required because head_dim=256 > 128 and ixformer flash attention either +# crashes (is_causal=True) or produces wrong output (attn_mask path). +# The fallback uses query_start_loc to derive actual query lengths, so it +# works correctly during profiling runs with chunked-prefill-style batches. +# also bypasses auto chunked prefill on +python3 ./patch_xformers_sdpa_seq.py +python3 ./patch_xformers_profile.py + +build_stage "installing API parsers and serving modules" +# --- tool parser: Qwen3 XML tool call format --------------------------------- +# Registers "qwen3_coder" parser for Qwen3.6 XML-style tool calls: +# \nvalue\n +# Use at server start: --tool-call-parser qwen3_coder --enable-auto-tool-choice +cp ./qwen3coder_tool_parser.py "${VLLM_ROOT}/entrypoints/openai/tool_parsers/" +python3 ./patch_vllm_tool_parser.py + +# --- reasoning parser: Qwen3 ... split ------------------------ +# Adds --reasoning-parser qwen3 support. +# Routes thinking tokens to reasoning_content, rest to content in the delta. +# Works together with --tool-call-parser qwen3_coder (think → tool call flow). +cp -r ./reasoning "${VLLM_ROOT}/" +cp ./protocol.py "${VLLM_ROOT}/entrypoints/openai/protocol.py" +cp ./cli_args.py "${VLLM_ROOT}/entrypoints/openai/cli_args.py" +cp ./serving_chat.py "${VLLM_ROOT}/entrypoints/openai/serving_chat.py" +cp ./serving_tokenization.py \ + "${VLLM_ROOT}/entrypoints/openai/serving_tokenization.py" +cp ./api_server.py "${VLLM_ROOT}/entrypoints/openai/api_server.py" +cp ./chat_utils.py "${VLLM_ROOT}/entrypoints/chat_utils.py" +python3 - ./api_server.py \ + "${VLLM_ROOT}/entrypoints/openai/api_server.py" <<'PY' +from pathlib import Path +import sys + +source = Path(sys.argv[1]).read_bytes() +installed = Path(sys.argv[2]).read_bytes() +if source != installed: + raise SystemExit("runtime api_server overlay identity mismatch") +PY + +build_stage "compiling submission Python sources" +find . -path './wheels' -prune -o -name '*.py' -print0 | xargs -0 python3 -m py_compile +build_stage "patch script completed" diff --git a/qwen3_6_scripts/patch_transformers_qwen3_5.py b/qwen3_6_scripts/patch_transformers_qwen3_5.py new file mode 100644 index 0000000..f0619a9 --- /dev/null +++ b/qwen3_6_scripts/patch_transformers_qwen3_5.py @@ -0,0 +1,100 @@ +""" +Patches transformers 4.55.3 to register qwen3_5 and qwen3_5_moe model types. + +Deploy steps on the remote machine: + 1. patch_ops.sh locates transformers with importlib.util.find_spec. + 2. cp -r modified_scripts/qwen3_5* into the detected transformers/models. + 3. python3 modified_scripts/patch_transformers_qwen3_5.py +""" + +import sys + +from patch_utils import package_root, replace_once, replace_one_of + +TRANSFORMERS_ROOT = package_root("transformers") +AUTO_CONFIG = TRANSFORMERS_ROOT / "models" / "auto" / "configuration_auto.py" +MODELS_INIT = TRANSFORMERS_ROOT / "models" / "__init__.py" + + +def main(): + print(f"=== Patching {AUTO_CONFIG} ===") + replace_one_of(AUTO_CONFIG, [ + # CONFIG_MAPPING_NAMES: insert qwen3_5 + qwen3_5_moe right after qwen3 + ( + '("qwen3", "Qwen3Config"),', + '("qwen3", "Qwen3Config"),\n ("qwen3_5", "Qwen3_5Config"),\n ("qwen3_5_moe", "Qwen3_5MoeConfig"),', + ), + ( + '("qwen3", "Qwen3Config")\n', + '("qwen3", "Qwen3Config"),\n ("qwen3_5", "Qwen3_5Config"),\n ("qwen3_5_moe", "Qwen3_5MoeConfig"),\n', + ), + ], required=True, already_contains='("qwen3_5_moe", "Qwen3_5MoeConfig")') + replace_one_of(AUTO_CONFIG, [ + # MODEL_NAMES_MAPPING (model_type -> human readable name) + ( + '("qwen3", "Qwen3"),', + '("qwen3", "Qwen3"),\n ("qwen3_5", "Qwen3_5"),\n ("qwen3_5_moe", "Qwen3_5_MoE"),', + ), + ( + '("qwen3", "Qwen3")\n', + '("qwen3", "Qwen3"),\n ("qwen3_5", "Qwen3_5"),\n ("qwen3_5_moe", "Qwen3_5_MoE"),\n', + ), + ], required=True, already_contains='("qwen3_5_moe", "Qwen3_5_MoE")') + + print(f"\n=== Patching {MODELS_INIT} ===") + replace_once( + MODELS_INIT, + "from .qwen3 import *\n", + "from .qwen3 import *\n from .qwen3_5 import *\n from .qwen3_5_moe import *\n", + required=True, + already_contains="from .qwen3_5_moe import *") + + # Verification + print("\n=== Verification ===") + try: + import importlib.util, types + + def _load_config_mod(module_name, file_path): + spec = importlib.util.spec_from_file_location(module_name, file_path) + mod = importlib.util.module_from_spec(spec) + mod.__package__ = ".".join(module_name.split(".")[:-1]) + pkg = sys.modules.setdefault("transformers", types.ModuleType("transformers")) + pkg.__path__ = [str(TRANSFORMERS_ROOT)] + cu = sys.modules.setdefault( + "transformers.configuration_utils", types.ModuleType("transformers.configuration_utils")) + class _PC: + def __init__(self, **kwargs): + return None + cu.PretrainedConfig = _PC + for sub in ("transformers.models", f"transformers.models.{module_name.split('.')[-2]}"): + m = sys.modules.setdefault(sub, types.ModuleType(sub)) + m.__path__ = [str(TRANSFORMERS_ROOT)] + spec.loader.exec_module(mod) + return mod + + mod27 = _load_config_mod( + "transformers.models.qwen3_5.configuration_qwen3_5", + str(TRANSFORMERS_ROOT / "models" / "qwen3_5" / + "configuration_qwen3_5.py"), + ) + cfg = mod27.Qwen3_5Config() + print(f" Qwen3_5Config() smoke-test OK (model_type={cfg.model_type})") + + mod35 = _load_config_mod( + "transformers.models.qwen3_5_moe.configuration_qwen3_5_moe", + str(TRANSFORMERS_ROOT / "models" / "qwen3_5_moe" / + "configuration_qwen3_5_moe.py"), + ) + moe_cfg = mod35.Qwen3_5MoeConfig() + print(f" Qwen3_5MoeConfig() smoke-test OK (model_type={moe_cfg.model_type})") + t = moe_cfg.text_config + print(f" num_experts={t.num_experts}, top_k={t.num_experts_per_tok}, " + f"shared={t.shared_expert_intermediate_size}, layers={t.num_hidden_layers}") + except Exception as e: + print(f" [optional] smoke-test failed (may be fine at runtime): {e}") + + print("\nDone.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_utils.py b/qwen3_6_scripts/patch_utils.py new file mode 100644 index 0000000..104adc4 --- /dev/null +++ b/qwen3_6_scripts/patch_utils.py @@ -0,0 +1,81 @@ +from __future__ import annotations + +import importlib.util +import pathlib +import shlex +from typing import Iterable, Optional, Sequence, Tuple + + +def package_root(pkg: str) -> pathlib.Path: + spec = importlib.util.find_spec(pkg) + if spec is None: + raise RuntimeError(f"package not found: {pkg}") + if not spec.submodule_search_locations: + raise RuntimeError(f"package has no package root: {pkg}") + return pathlib.Path(next(iter(spec.submodule_search_locations))).resolve() + + +def ensure_file(path: pathlib.Path) -> pathlib.Path: + if not path.is_file(): + raise FileNotFoundError(str(path)) + return path + + +def ensure_dir(path: pathlib.Path) -> pathlib.Path: + if not path.is_dir(): + raise FileNotFoundError(str(path)) + return path + + +def replace_once(path: pathlib.Path, + old: str, + new: str, + *, + required: bool = True, + already_contains: Optional[str] = None) -> bool: + path = ensure_file(path) + text = path.read_text() + marker = already_contains if already_contains is not None else new + if marker in text: + print(f"[skip] already patched: {path}") + return False + if old not in text: + msg = f"anchor not found in {path}: {old[:120]!r}" + if required: + raise RuntimeError(msg) + print(f"[warn] {msg}") + return False + path.write_text(text.replace(old, new, 1)) + print(f"[ok] patched: {path}") + return True + + +def replace_one_of(path: pathlib.Path, + replacements: Sequence[Tuple[str, str]], + *, + required: bool = True, + already_contains: Optional[str] = None) -> bool: + path = ensure_file(path) + text = path.read_text() + if already_contains is not None and already_contains in text: + print(f"[skip] already patched: {path}") + return False + for _, new in replacements: + if new in text: + print(f"[skip] already patched: {path}") + return False + for old, new in replacements: + if old in text: + path.write_text(text.replace(old, new, 1)) + print(f"[ok] patched: {path}") + return True + anchors = ", ".join(repr(old[:80]) for old, _ in replacements) + msg = f"anchor not found in {path}; tried: {anchors}" + if required: + raise RuntimeError(msg) + print(f"[warn] {msg}") + return False + + +def shell_env_line(name: str, value: pathlib.Path) -> str: + return f"{name}={shlex.quote(str(value))}" diff --git a/qwen3_6_scripts/patch_vllm_qwen3_5.py b/qwen3_6_scripts/patch_vllm_qwen3_5.py new file mode 100644 index 0000000..55313fc --- /dev/null +++ b/qwen3_6_scripts/patch_vllm_qwen3_5.py @@ -0,0 +1,73 @@ +""" +Patches the vLLM model registry and deploys the Qwen3_5 model file. + +Deploy steps on the remote machine: + 1. patch_ops.sh locates vLLM with importlib.util.find_spec. + 2. cp modified_scripts/qwen3_5.py into the detected vllm model directory. + 2. python3 modified_scripts/patch_vllm_qwen3_5.py + +The registry patch installs Qwen3.6 aliases so /model/config.json does not +need to be edited by hand. +""" + +import ast + +from patch_utils import package_root, replace_once + +VLLM_ROOT = package_root("vllm") +REGISTRY = VLLM_ROOT / "model_executor" / "models" / "registry.py" +MODEL = VLLM_ROOT / "model_executor" / "models" / "qwen3_5.py" + +EXPECTED_REGISTRY_ENTRIES = ( + '"Qwen3ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM")', + '"Qwen3MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM")', + '"Qwen3_5ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM")', + '"Qwen3_5MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM")', + '"Qwen3_6ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM")', + '"Qwen3_6MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM")', +) + + +def main(): + print(f"=== Patching {REGISTRY} ===") + replace_once( + REGISTRY, + ' "Qwen3ForCausalLM": ("qwen3", "Qwen3ForCausalLM"),\n' + ' "Qwen3MoeForCausalLM": ("qwen3_moe", "Qwen3MoeForCausalLM"),', + ' "Qwen3ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM"),\n' + ' "Qwen3MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM"),\n' + ' "Qwen3_5ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM"),\n' + ' "Qwen3_5MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM"),\n' + ' "Qwen3_6ForCausalLM": ("qwen3_5", "Qwen3_5ForCausalLM"),\n' + ' "Qwen3_6MoeForCausalLM": ("qwen3_5", "Qwen3_5MoeForCausalLM"),', + required=True, + already_contains='"Qwen3_6MoeForCausalLM"') + + print("\n=== Static verification ===") + model_source = MODEL.read_text(encoding="utf-8") + tree = ast.parse(model_source, filename=str(MODEL)) + class_names = { + node.name for node in tree.body if isinstance(node, ast.ClassDef) + } + required_classes = {"Qwen3_5ForCausalLM", "Qwen3_5MoeForCausalLM"} + missing_classes = required_classes - class_names + if missing_classes: + raise RuntimeError( + f"Qwen3.5 model classes missing: {sorted(missing_classes)}") + + registry_source = REGISTRY.read_text(encoding="utf-8") + missing_entries = [ + entry for entry in EXPECTED_REGISTRY_ENTRIES + if entry not in registry_source + ] + if missing_entries: + raise RuntimeError( + f"Qwen3.5 registry entries missing: {missing_entries}") + print(" model syntax and class declarations verified without import") + print(f" registry aliases verified: {len(EXPECTED_REGISTRY_ENTRIES)}") + + print("\nDone. Registry aliases installed; do not edit /model/config.json.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_vllm_tool_parser.py b/qwen3_6_scripts/patch_vllm_tool_parser.py new file mode 100644 index 0000000..18463a5 --- /dev/null +++ b/qwen3_6_scripts/patch_vllm_tool_parser.py @@ -0,0 +1,57 @@ +""" +Patches vLLM 0.6.3 to register Qwen3CoderToolParser under the name "qwen3_coder". + +Deploy steps on the remote machine (already called by patch_ops.sh): + 1. patch_ops.sh locates vLLM with importlib.util.find_spec. + 2. cp qwen3coder_tool_parser.py into the detected vllm tool_parsers. + 2. python3 patch_vllm_tool_parser.py + +Usage after patching: + --tool-call-parser qwen3_coder --enable-auto-tool-choice +""" + +from patch_utils import ensure_dir, package_root, replace_once + +VLLM_ROOT = package_root("vllm") +TOOL_PARSERS_DIR = VLLM_ROOT / "entrypoints" / "openai" / "tool_parsers" +INIT_FILE = TOOL_PARSERS_DIR / "__init__.py" + + +def main(): + ensure_dir(TOOL_PARSERS_DIR) + + print(f"=== Patching {INIT_FILE} ===") + replace_once( + INIT_FILE, + "from .mistral_tool_parser import MistralToolParser", + "from .mistral_tool_parser import MistralToolParser\n" + "from .qwen3coder_tool_parser import Qwen3CoderToolParser", + required=True, + already_contains="from .qwen3coder_tool_parser import Qwen3CoderToolParser") + replace_once( + INIT_FILE, + '"MistralToolParser", "Internlm2ToolParser", "Llama3JsonToolParser"\n]', + '"MistralToolParser", "Internlm2ToolParser", "Llama3JsonToolParser",\n' + ' "Qwen3CoderToolParser"\n]', + required=True, + already_contains='"Qwen3CoderToolParser"') + + print("\n=== Verification ===") + try: + import importlib.util + spec = importlib.util.spec_from_file_location( + "qwen3coder_tool_parser", + str(TOOL_PARSERS_DIR / "qwen3coder_tool_parser.py"), + ) + mod = importlib.util.module_from_spec(spec) + print(f" Module spec loaded: {spec.name}") + print(" (full import requires torch/vllm runtime — skipping exec)") + except Exception as e: + print(f" [optional] spec check failed: {e}") + + print("\nDone. Start vLLM server with:") + print(" --tool-call-parser qwen3_coder --enable-auto-tool-choice") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_worker_cache_transfer_order.py b/qwen3_6_scripts/patch_worker_cache_transfer_order.py new file mode 100644 index 0000000..af7df40 --- /dev/null +++ b/qwen3_6_scripts/patch_worker_cache_transfer_order.py @@ -0,0 +1,37 @@ +from patch_utils import package_root, replace_once + + +WORKER = package_root("vllm") / "worker" / "worker.py" + +CLEAN_BLOCK = """\ + if (worker_input.blocks_to_swap_in is not None + and worker_input.blocks_to_swap_in.numel() > 0): + self.cache_engine[virtual_engine].swap_in( + worker_input.blocks_to_swap_in) + if (worker_input.blocks_to_swap_out is not None + and worker_input.blocks_to_swap_out.numel() > 0): + self.cache_engine[virtual_engine].swap_out( + worker_input.blocks_to_swap_out) +""" + +ORDERED_BLOCK = """\ + # BI100 content-addressed CPU KV tier may preserve a victim and reuse + # that same GPU slot in one step. Complete every D2H before any H2D. + if (worker_input.blocks_to_swap_out is not None + and worker_input.blocks_to_swap_out.numel() > 0): + self.cache_engine[virtual_engine].swap_out( + worker_input.blocks_to_swap_out) + if (worker_input.blocks_to_swap_in is not None + and worker_input.blocks_to_swap_in.numel() > 0): + self.cache_engine[virtual_engine].swap_in( + worker_input.blocks_to_swap_in) +""" + + +replace_once( + WORKER, + CLEAN_BLOCK, + ORDERED_BLOCK, + required=True, + already_contains="Complete every D2H before any H2D", +) diff --git a/qwen3_6_scripts/patch_worker_profile_override.py b/qwen3_6_scripts/patch_worker_profile_override.py new file mode 100644 index 0000000..a62a2ce --- /dev/null +++ b/qwen3_6_scripts/patch_worker_profile_override.py @@ -0,0 +1,81 @@ +from patch_utils import package_root, replace_one_of + +WORKER = package_root("vllm") / "worker" / "worker.py" + +CLEAN_BLOCK = """\ + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. + self.model_runner.profile_run() +""" + +GUARDED_BLOCK = """\ + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. Mark this synthetic pass so BI100_PROFILE can skip + # timing it by default; profiling real requests is the useful signal. + _bi100_prev_startup_profile = os.environ.get("BI100_IN_STARTUP_PROFILE") + os.environ["BI100_IN_STARTUP_PROFILE"] = "1" + try: + self.model_runner.profile_run() + finally: + if _bi100_prev_startup_profile is None: + os.environ.pop("BI100_IN_STARTUP_PROFILE", None) + else: + os.environ["BI100_IN_STARTUP_PROFILE"] = _bi100_prev_startup_profile +""" + +NEW_BLOCK = """\ + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + + # BI100: Qwen3.6 batched dummy profile_run can trip GDN non-finite + # checks before the server starts. If the operator explicitly provides + # --num-gpu-blocks-override, trust that conservative capacity value and + # skip only the synthetic profile pass. Real inference still uses the + # normal GDN fail-fast path. + if self.cache_config.num_gpu_blocks_override is not None: + cache_block_size = self.get_cache_block_size_bytes() + if cache_block_size == 0: + num_cpu_blocks = 0 + else: + num_cpu_blocks = int(self.cache_config.swap_space_bytes // + cache_block_size) + logger.warning( + "[BI100] skipping worker.profile_run because " + "num_gpu_blocks_override=%d was explicitly set", + self.cache_config.num_gpu_blocks_override) + gc.collect() + torch.cuda.empty_cache() + return self.cache_config.num_gpu_blocks_override, max(num_cpu_blocks, 0) + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. Mark this synthetic pass so BI100_PROFILE can skip + # timing it by default; profiling real requests is the useful signal. + _bi100_prev_startup_profile = os.environ.get("BI100_IN_STARTUP_PROFILE") + os.environ["BI100_IN_STARTUP_PROFILE"] = "1" + try: + self.model_runner.profile_run() + finally: + if _bi100_prev_startup_profile is None: + os.environ.pop("BI100_IN_STARTUP_PROFILE", None) + else: + os.environ["BI100_IN_STARTUP_PROFILE"] = _bi100_prev_startup_profile +""" + +replace_one_of( + WORKER, + [ + (GUARDED_BLOCK, NEW_BLOCK), + (CLEAN_BLOCK, NEW_BLOCK), + ], + required=True, + already_contains="[BI100] skipping worker.profile_run", +) diff --git a/qwen3_6_scripts/patch_worker_startup_profile_guard.py b/qwen3_6_scripts/patch_worker_startup_profile_guard.py new file mode 100644 index 0000000..6110aa6 --- /dev/null +++ b/qwen3_6_scripts/patch_worker_startup_profile_guard.py @@ -0,0 +1,34 @@ +from patch_utils import package_root, replace_one_of + + +WORKER = package_root("vllm") / "worker" / "worker.py" + +CLEAN_BLOCK = """\ + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. + self.model_runner.profile_run() +""" + +GUARDED_BLOCK = """\ + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. Mark this synthetic pass so BI100_PROFILE can exclude + # it without changing vLLM's normal capacity calculation. + _bi100_prev_startup_profile = os.environ.get("BI100_IN_STARTUP_PROFILE") + os.environ["BI100_IN_STARTUP_PROFILE"] = "1" + try: + self.model_runner.profile_run() + finally: + if _bi100_prev_startup_profile is None: + os.environ.pop("BI100_IN_STARTUP_PROFILE", None) + else: + os.environ["BI100_IN_STARTUP_PROFILE"] = _bi100_prev_startup_profile +""" + + +replace_one_of( + WORKER, + [(CLEAN_BLOCK, GUARDED_BLOCK)], + required=True, + already_contains=( + "Mark this synthetic pass so BI100_PROFILE can exclude"), +) diff --git a/qwen3_6_scripts/patch_xformers_profile.py b/qwen3_6_scripts/patch_xformers_profile.py new file mode 100644 index 0000000..51d2465 --- /dev/null +++ b/qwen3_6_scripts/patch_xformers_profile.py @@ -0,0 +1,121 @@ +"""Install disabled-by-default M1-48 XFormers timing boundaries.""" + +from __future__ import annotations + +from pathlib import Path + +try: + from patch_utils import package_root, replace_once +except ModuleNotFoundError: + from .patch_utils import package_root, replace_once + + +IMPORT_OLD = "from vllm.logger import init_logger" +IMPORT_NEW = """\ +from vllm.bi100_profile import bi100_timer +from vllm.logger import init_logger""" + +KV_WRITE_OLD = """\ + PagedAttention.write_to_paged_cache(key, value, key_cache, + value_cache, + updated_slot_mapping, + self.kv_cache_dtype, + k_scale, v_scale)""" +KV_WRITE_NEW = """\ + with bi100_timer("xformers.kv_write"): + PagedAttention.write_to_paged_cache( + key, value, key_cache, value_cache, + updated_slot_mapping, self.kv_cache_dtype, + k_scale, v_scale)""" + +DENSE_OLD = """\ + out = self._run_memory_efficient_xformers_forward( + query, key, value, prefill_meta, attn_type=attn_type)""" +DENSE_NEW = """\ + with bi100_timer("xformers.dense_prefill"): + out = self._run_memory_efficient_xformers_forward( + query, key, value, prefill_meta, attn_type=attn_type)""" + +PAGED_OLD = """\ + out = PagedAttention.forward_prefix( + query, + key, + value, + self.kv_cache_dtype, + key_cache, + value_cache, + prefill_meta.block_tables, + prefill_meta.query_start_loc, + prefill_meta.seq_lens_tensor, + prefill_meta.context_lens_tensor, + prefill_meta.max_query_len, + self.alibi_slopes, + self.sliding_window, + k_scale, + v_scale, + is_causal_decoder=(attn_type == AttentionType.DECODER), + )""" +PAGED_NEW = """\ + with bi100_timer("xformers.paged_prefill"): + out = PagedAttention.forward_prefix( + query, + key, + value, + self.kv_cache_dtype, + key_cache, + value_cache, + prefill_meta.block_tables, + prefill_meta.query_start_loc, + prefill_meta.seq_lens_tensor, + prefill_meta.context_lens_tensor, + prefill_meta.max_query_len, + self.alibi_slopes, + self.sliding_window, + k_scale, + v_scale, + is_causal_decoder=(attn_type == AttentionType.DECODER), + )""" + + +def patch_file(path: Path) -> None: + replace_once( + path, + IMPORT_OLD, + IMPORT_NEW, + already_contains="from vllm.bi100_profile import bi100_timer", + ) + replace_once( + path, + KV_WRITE_OLD, + KV_WRITE_NEW, + already_contains='bi100_timer("xformers.kv_write")', + ) + replace_once( + path, + DENSE_OLD, + DENSE_NEW, + already_contains='bi100_timer("xformers.dense_prefill")', + ) + replace_once( + path, + PAGED_OLD, + PAGED_NEW, + already_contains='bi100_timer("xformers.paged_prefill")', + ) + text = path.read_text(encoding="utf-8") + canonical = "\n".join(line.rstrip(" \t") for line in text.split("\n")) + if not canonical.endswith("\n"): + canonical += "\n" + if canonical != text: + path.write_text(canonical, encoding="utf-8") + + +def main() -> None: + path = package_root("vllm") / "attention" / "backends" / "xformers.py" + print("=== patch_xformers_profile (M1-48 diagnostic timers) ===") + print(f"Target: {path}") + patch_file(path) + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_xformers_sdpa_batch.py b/qwen3_6_scripts/patch_xformers_sdpa_batch.py new file mode 100644 index 0000000..72315b5 --- /dev/null +++ b/qwen3_6_scripts/patch_xformers_sdpa_batch.py @@ -0,0 +1,177 @@ +""" +策略:批量(block-diagonal)fallback — 纯 PyTorch 数学实现 +============================================================= +构建块对角 causal mask,对整批序列一次 matmul + softmax, +完全绕开所有硬件 flash attention kernel。 + +背景: + ixformer flshattF: head_dim > 128 报错拒绝 + cudnnFlashAttnForward: 接受 head_dim=256,但数值结果错误(输出全"!") + 两者大概率是同一硬件单元,ixformer 提前拦截了硬件不支持的配置。 + 纯 matmul 路径完全绕开硬件 flash attention,数值正确。 + +优点: + 数值正确。 + 并发请求 prefill attention 在 GPU 上真正并行(一次大 matmul)。 + +缺点: + 峰值显存 = total_tokens² × H × dtype_size + total_tokens 受 --max-num-batched-tokens 控制,max-model-len 控制不住。 + +内存参考(fp16,H_local=6,--max-num-batched-tokens=T): + T=2048 → 峰值 ~50 MB + T=4096 → 峰值 ~200 MB + T=8192 → 峰值 ~800 MB + T=16384 → 峰值 ~3.2 GB + +Deploy: + python3 modified_scripts/patch_xformers_sdpa_batch.py +""" + +from patch_utils import package_root, replace_once + +XFORMERS_PATH = package_root("vllm") / "attention" / "backends" / "xformers.py" + +FALLBACK_METHOD = ''' + def _run_sdpa_fallback( + self, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attn_metadata: "XFormersMetadata", + ) -> torch.Tensor: + """批量纯数学 attention fallback。 + + 构建块对角 causal mask(等价于 ixformer BlockDiagonalCausalMask), + 对整批序列一次 matmul + softmax,GPU 并行处理所有序列。 + + 块对角 mask 结构(seq1 len=3,seq2 len=2): + s1,0 s1,1 s1,2 s2,0 s2,1 + s1,0 [ 0 -inf -inf -inf -inf ] + s1,1 [ 0 0 -inf -inf -inf ] + s1,2 [ 0 0 0 -inf -inf ] + s2,0 [-inf -inf -inf 0 -inf ] + s2,1 [-inf -inf -inf 0 0 ] + + softmax 在 float32 下计算防止 float16 溢出,结果转回原始 dtype。 + + Args: + query : [1, total_prefill_tokens, num_heads, head_dim] + key : [1, total_prefill_tokens, num_kv_heads, head_dim] + value : [1, total_prefill_tokens, num_kv_heads, head_dim] + Returns: + [1, total_prefill_tokens, num_heads, head_dim] + """ + assert attn_metadata.seq_lens is not None + orig_dtype = query.dtype + total_tokens = query.shape[1] + + # ── 构建块对角 causal mask [T, T] ──────────────────────────────── + # 全部初始化为 -inf,再对每条序列的对角块填入下三角 0 + mask = torch.full( + (total_tokens, total_tokens), + float("-inf"), + dtype=torch.float32, + device=query.device, + ) + start = 0 + for seq_len in attn_metadata.seq_lens: + end = start + seq_len + mask[start:end, start:end] = torch.tril( + torch.zeros(seq_len, seq_len, + dtype=torch.float32, device=query.device) + ) + start = end + + # ── [1, H, T, D],.contiguous() ────────────────────────────────── + q_all = query.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + k_all = key.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + v_all = value.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + + # ── GQA:展开 KV heads ──────────────────────────────────────────── + if k_all.shape[1] != q_all.shape[1]: + n = q_all.shape[1] // k_all.shape[1] + k_all = k_all.repeat_interleave(n, dim=1).contiguous() + v_all = v_all.repeat_interleave(n, dim=1).contiguous() + + # ── 纯数学 attention(float32 防溢出)──────────────────────────── + # [1, H, T, T] + attn_w = torch.matmul(q_all.float(), k_all.float().transpose(-2, -1)) + attn_w = attn_w * self.scale + attn_w = attn_w + mask # 加法广播:mask [T,T] → [1, H, T, T] + attn_w = torch.softmax(attn_w, dim=-1) + + out = torch.matmul(attn_w, v_all.float()).to(orig_dtype) + # [1, H, T, D] → [1, T, H, D] + return out.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + +''' + +OLD_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op = self.attn_op + ) + return out.view_as(original_query)\ +""" + +NEW_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + if self.head_size > 128: + out = self._run_sdpa_fallback(query, key, value, attn_metadata) + else: + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op=self.attn_op, + ) + return out.view_as(original_query)\ +""" + +INJECT_ANCHOR = " def _run_memory_efficient_xformers_forward(" + + +def patch_file(path): + replace_once( + path, + INJECT_ANCHOR, + FALLBACK_METHOD + INJECT_ANCHOR, + required=True, + already_contains="def _run_sdpa_fallback(") + replace_once( + path, + OLD_XFORMER_BLOCK, + NEW_XFORMER_BLOCK, + required=True, + already_contains="out = self._run_sdpa_fallback(query, key, value, attn_metadata)") + + +def main(): + print("=== patch_xformers_sdpa_batch (batch, pure-math) ===") + print(f"Target: {XFORMERS_PATH}") + patch_file(XFORMERS_PATH) + print("\nDone.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_xformers_sdpa_batch_kernel.py b/qwen3_6_scripts/patch_xformers_sdpa_batch_kernel.py new file mode 100644 index 0000000..f521208 --- /dev/null +++ b/qwen3_6_scripts/patch_xformers_sdpa_batch_kernel.py @@ -0,0 +1,176 @@ +""" +策略:批量(block-diagonal)— F.scaled_dot_product_attention,可走硬件 kernel +============================================================================= +构建块对角 causal mask,对整批序列一次 F.scaled_dot_product_attention。 +与 patch_xformers_sdpa_batch.py(纯 matmul)的区别: + SDPA 会根据 PyTorch/驱动能力分发到最优 kernel(Flash Attention / + mem-efficient attention / math fallback),而不是固定走 cublas matmul。 + +历史说明: + 该方案最早因输出全"!"而被弃用,后续排查确认"!"由 mamba_cache.py bug + 引起,与 attention 实现无关。当前恢复此方案用于性能对比测试。 + +已知硬件限制(BI-V100): + cudnnFlashAttnForward 不支持 is_causal=True(报错)。 + 本实现使用 is_causal=False + 显式块对角 additive mask 规避此限制。 + 若 SDPA 仍分发到有问题的 kernel,回退到 patch_xformers_sdpa_batch.py。 + +优点(vs 纯 matmul): + SDPA 可分发到 Flash Attention kernel → O(L) 显存、更快的 CUDA kernel。 + +缺点: + 依赖硬件 kernel 行为,若 kernel 有 bug 则数值错误(需与 matmul 版对比验证)。 + +Deploy: + python3 modified_scripts/patch_xformers_sdpa_batch_kernel.py +""" + +from patch_utils import package_root, replace_once + +XFORMERS_PATH = package_root("vllm") / "attention" / "backends" / "xformers.py" + +FALLBACK_METHOD = ''' + def _run_sdpa_fallback( + self, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attn_metadata: "XFormersMetadata", + ) -> torch.Tensor: + """批量 F.scaled_dot_product_attention fallback(可走硬件 kernel)。 + + 构建块对角 causal mask,对整批序列一次 SDPA 调用。 + SDPA 可分发到 Flash Attention / mem-efficient attention kernel。 + is_causal=False + 显式 additive mask,规避 cudnnFlashAttnForward + 不支持 is_causal=True 的限制。 + + 块对角 mask(seq1 len=3,seq2 len=2): + s1,0 s1,1 s1,2 s2,0 s2,1 + s1,0 [ 0 -inf -inf -inf -inf ] + s1,1 [ 0 0 -inf -inf -inf ] + s1,2 [ 0 0 0 -inf -inf ] + s2,0 [-inf -inf -inf 0 -inf ] + s2,1 [-inf -inf -inf 0 0 ] + + Args: + query : [1, total_prefill_tokens, num_heads, head_dim] + key : [1, total_prefill_tokens, num_kv_heads, head_dim] + value : [1, total_prefill_tokens, num_kv_heads, head_dim] + Returns: + [1, total_prefill_tokens, num_heads, head_dim] + """ + import torch.nn.functional as F + + assert attn_metadata.seq_lens is not None + orig_dtype = query.dtype + total_tokens = query.shape[1] + + # ── 块对角 causal mask [T, T] ───────────────────────────────────── + mask = torch.full( + (total_tokens, total_tokens), + float("-inf"), + dtype=orig_dtype, + device=query.device, + ) + start = 0 + for seq_len in attn_metadata.seq_lens: + end = start + seq_len + mask[start:end, start:end] = torch.tril( + torch.zeros(seq_len, seq_len, dtype=orig_dtype, device=query.device) + ) + start = end + + # ── [1, H, T, D] ────────────────────────────────────────────────── + q_all = query.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + k_all = key.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + v_all = value.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + + # ── GQA:展开 KV heads ──────────────────────────────────────────── + if k_all.shape[1] != q_all.shape[1]: + n = q_all.shape[1] // k_all.shape[1] + k_all = k_all.repeat_interleave(n, dim=1).contiguous() + v_all = v_all.repeat_interleave(n, dim=1).contiguous() + + # ── F.scaled_dot_product_attention(可走硬件 kernel)───────────── + # is_causal=False:避免 cudnnFlashAttnForward "not support causal mode" + # attn_mask 传 additive float mask(非 bool),SDPA 选择 math/kernel 路径 + out = F.scaled_dot_product_attention( + q_all, k_all, v_all, + attn_mask=mask, + dropout_p=0.0, + is_causal=False, + scale=self.scale, + ) + # [1, H, T, D] → [1, T, H, D] + return out.squeeze(0).permute(1, 0, 2).contiguous().unsqueeze(0) + +''' + +OLD_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op = self.attn_op + ) + return out.view_as(original_query)\ +""" + +NEW_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + if self.head_size > 128: + out = self._run_sdpa_fallback(query, key, value, attn_metadata) + else: + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op=self.attn_op, + ) + return out.view_as(original_query)\ +""" + +INJECT_ANCHOR = " def _run_memory_efficient_xformers_forward(" + + +def patch_file(path): + replace_once( + path, + INJECT_ANCHOR, + FALLBACK_METHOD + INJECT_ANCHOR, + required=True, + already_contains="def _run_sdpa_fallback(") + replace_once( + path, + OLD_XFORMER_BLOCK, + NEW_XFORMER_BLOCK, + required=True, + already_contains="out = self._run_sdpa_fallback(query, key, value, attn_metadata)") + + +def main(): + print("=== patch_xformers_sdpa_batch_kernel (batch, F.sdpa + kernel dispatch) ===") + print(f"Target: {XFORMERS_PATH}") + patch_file(XFORMERS_PATH) + print("\nDone.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_xformers_sdpa_seq.py b/qwen3_6_scripts/patch_xformers_sdpa_seq.py new file mode 100644 index 0000000..05d9733 --- /dev/null +++ b/qwen3_6_scripts/patch_xformers_sdpa_seq.py @@ -0,0 +1,427 @@ +""" +策略:顺序(per-sequence)fallback — 纯 PyTorch 数学实现 +========================================================== +逐条序列用 matmul + softmax 手写 attention,完全绕开所有硬件 +flash attention kernel(ixformer / cudnnFlashAttnForward)。 + +背景: + Iluvatar cudnnFlashAttnForward 存在两个已知问题: + 1. 不支持 is_causal=True(报错) + 2. 使用 attn_mask 路径时数值结果不正确(静默错误,输出全为"!") + 与华为昇腾 910B4 上 llama.cpp --flash-attn off 修复同类问题的原理相同。 + 纯数学路径(matmul + softmax)在任何 PyTorch 后端上结果都正确。 + +优点: + 数值正确,不依赖任何硬件特定 attention kernel。 + 峰值显存 = max(seq_len)² × H × dtype_size,由 --max-model-len 控制。 + +缺点: + 并发请求的 prefill attention 串行执行。 + O(L²) 显存(无 flash attention 的 O(L) 优化)。 + +内存参考(fp16,H_local=6): + max-model-len=4096 → 峰值 ~200 MB + max-model-len=8192 → 峰值 ~800 MB + max-model-len=16384 → 峰值 ~3.2 GB + +额外 patch(arg_utils.py): + vllm 0.6.3 在 max_model_len > 32K 时会自动开启 chunked prefill(无命令行 + 关闭选项),原意是防止 profiling OOM。但 _run_sdpa_fallback 已通过 Q-tiling + 解决了该问题,chunked prefill 反而会把推理路径从 _run_sdpa_fallback 切换到 + _forward_prefix_pytorch,属于不必要的行为变更,因此一并禁用该自动逻辑。 + +Deploy: + python3 modified_scripts/patch_xformers_sdpa_seq.py +""" + +from patch_utils import package_root, replace_one_of, replace_once + +VLLM_ROOT = package_root("vllm") +XFORMERS_PATH = VLLM_ROOT / "attention" / "backends" / "xformers.py" +ARG_UTILS_PATH = VLLM_ROOT / "engine" / "arg_utils.py" +LOGITS_PROC_PATH = ( + VLLM_ROOT / "model_executor" / "layers" / "logits_processor.py") +OUTLINES_DECODING_PATH = ( + VLLM_ROOT / "model_executor" / "guided_decoding" / + "outlines_decoding.py") + +# _apply_logits_processors crashes when seq_groups is None (intermediate +# chunked-prefill chunks on the driver rank). Add an early-return guard. +_LP_OLD_BLOCK = """\ +def _apply_logits_processors( + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, +) -> torch.Tensor: + found_logits_processors = False\ +""" + +_LP_NEW_BLOCK = """\ +def _apply_logits_processors( + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, +) -> torch.Tensor: + if sampling_metadata.seq_groups is None: # intermediate chunked-prefill chunk + return logits + found_logits_processors = False\ +""" + +# Outlines' UNESCAPED_STRING accepts raw JSON control characters, including +# newlines and tabs. The generated text can therefore satisfy the CFG while +# still failing json.loads(). Use the RFC 8259 string character constraints. +_JSON_STRING_OLD_BLOCK = """\ +| UNESCAPED_STRING +| SIGNED_NUMBER -> number +| "true" -> true +| "false" -> false +| "null" -> null + +array : "[" [value ("," value)*] "]" +object : "{" [pair ("," pair)*] "}" +pair : UNESCAPED_STRING ":" value + +%import common.UNESCAPED_STRING +%import common.SIGNED_NUMBER +%import common.WS + +%ignore WS\ +""" + +_JSON_STRING_V1_BLOCK = r'''| JSON_STRING +| SIGNED_NUMBER -> number +| "true" -> true +| "false" -> false +| "null" -> null + +array : "[" [value ("," value)*] "]" +object : "{" [pair ("," pair)*] "}" +pair : JSON_STRING ":" value + +JSON_STRING: /"(\\["\\\/bfnrt]|\\u[0-9a-fA-F]{4}|[^"\\\x00-\x1f])*"/ +%import common.SIGNED_NUMBER +%import common.WS + +%ignore WS''' + +_JSON_STRING_NEW_BLOCK = r'''| JSON_STRING +| SIGNED_NUMBER -> number +| "true" -> true +| "false" -> false +| "null" -> null + +array : "[" _ws [value (_ws "," _ws value)*] _ws "]" +object : "{" _ws [pair (_ws "," _ws pair)*] _ws "}" +pair : JSON_STRING _ws ":" _ws value +_ws : JSON_WS? + +JSON_STRING: /"(\\["\\\/bfnrt]|\\u[0-9a-fA-F]{4}|[^"\\\x00-\x1f])*"/ +JSON_WS: /[ \t\r\n]{1,4}/ +%import common.SIGNED_NUMBER''' + +# vllm 0.6.3 自动开启 chunked prefill 的原始块 +_ARG_OLD_BLOCK = """\ + if (is_gpu and not use_sliding_window and not use_spec_decode + and not self.enable_lora + and not self.enable_prompt_adapter): + self.enable_chunked_prefill = True + logger.warning( + "Chunked prefill is enabled by default for models with " + "max_model_len > 32K. Currently, chunked prefill might " + "not work with some features or models. If you " + "encounter any issues, please disable chunked prefill " + "by setting --enable-chunked-prefill=False.")\ +""" + +_ARG_NEW_BLOCK = """\ + if (is_gpu and not use_sliding_window and not use_spec_decode + and not self.enable_lora + and not self.enable_prompt_adapter): + pass # skip auto-enable: Q-tiling in _run_sdpa_fallback + # handles long-context memory without chunked prefill\ +""" + +_MM_PREFIX_OLD_BLOCK = """\ + if model_config.is_multimodal_model: + if self.enable_prefix_caching: + logger.warning( + "--enable-prefix-caching is currently not " + "supported for multimodal models and has been disabled.") + self.enable_prefix_caching = False\ +""" + +_MM_PREFIX_NEW_BLOCK = """\ + if model_config.is_multimodal_model: + architectures = getattr(model_config.hf_config, + "architectures", []) or [] + qwen36_native_vision = "Qwen3_5MoeForCausalLM" in architectures + if self.enable_prefix_caching and qwen36_native_vision: + logger.info( + "Keeping prefix caching enabled for the Qwen3.6 native " + "vision path.") + elif self.enable_prefix_caching: + logger.warning( + "--enable-prefix-caching is currently not " + "supported for multimodal models and has been disabled.") + self.enable_prefix_caching = False\ +""" + +FALLBACK_METHOD = ''' + def _run_sdpa_fallback( + self, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attn_metadata: "XFormersMetadata", + ) -> torch.Tensor: + """纯数学 causal attention fallback,带 Q-tiling 内存优化。 + + 调用时机:kv_cache.numel()==0(profiling 阶段)。 + 此路径无 KV 缓存前缀,KV 长度 == query 长度。 + + 内存优化(Q-tiling,与 Flash Attention 同思路): + 将 Q 分成 _Q_CHUNK 大小的子块逐块计算,每块峰值内存 + O(_Q_CHUNK × q_len) 而非 O(q_len²)。 + profiling 阶段序列可能达到 max_model_len(如 20K tokens), + 不加 Q-tiling 会产生 9.6 GB 矩阵直接 OOM。 + + softmax 在 float32 下计算以防止 float16 溢出,结果转回原始 dtype。 + + Args: + query : [1, total_query_tokens, num_heads, head_dim] + key : [1, total_query_tokens, num_kv_heads, head_dim] + value : [1, total_query_tokens, num_kv_heads, head_dim] + Returns: + [1, total_query_tokens, num_heads, head_dim] + """ + _Q_CHUNK = 256 # 与 _forward_prefix_pytorch 的 _ATTN_Q_CHUNK 保持一致 + + assert attn_metadata.seq_lens is not None + orig_dtype = query.dtype + num_seqs = len(attn_metadata.seq_lens) + + # 推导每条序列的实际 query 长度。 + # 正常 prefill 时 q_len == seq_len;如果将来遇到 chunked 场景, + # query_start_loc 记录的是真实 query token 数(非全序列长度)。 + if (attn_metadata.query_start_loc is not None + and len(attn_metadata.query_start_loc) == num_seqs + 1): + q_lens = [ + int(attn_metadata.query_start_loc[i + 1].item()) - + int(attn_metadata.query_start_loc[i].item()) + for i in range(num_seqs) + ] + else: + q_lens = list(attn_metadata.seq_lens) + + q_flat = query.squeeze(0) # [T, H, D] + k_flat = key.squeeze(0) # [T, Hkv, D] + v_flat = value.squeeze(0) + + output = torch.empty_like(q_flat) + seq_start = 0 + for q_len in q_lens: + seq_end = seq_start + q_len + + # 当前序列的完整 K/V(此路径无前缀,KV == Q) + k_s = k_flat[seq_start:seq_end].permute(1, 0, 2).float() # [Hkv, q_len, D] + v_s = v_flat[seq_start:seq_end].permute(1, 0, 2).float() # [Hkv, q_len, D] + + # GQA:展开 KV heads 至与 query heads 一致 + if k_s.shape[0] != self.num_heads: + n = self.num_heads // k_s.shape[0] + k_s = k_s.repeat_interleave(n, dim=0).contiguous() + v_s = v_s.repeat_interleave(n, dim=0).contiguous() + + # k_pos 用于因果掩码 + k_pos = torch.arange(q_len, device=query.device) + + # Q-tiling:分块处理 query,峰值内存 O(_Q_CHUNK × q_len) + for qc_start in range(0, q_len, _Q_CHUNK): + qc_end = min(qc_start + _Q_CHUNK, q_len) + + # [H, qc, D] + q_c = q_flat[seq_start + qc_start:seq_start + qc_end] \ + .permute(1, 0, 2).float() + + # [H, qc, q_len] + attn_w = torch.matmul(q_c, k_s.transpose(-2, -1)) * self.scale + + # 因果掩码:q_c 里位置 j 只能看 k_pos <= j(相对位置) + qc_q_pos = torch.arange(qc_start, qc_end, device=query.device) + mask = k_pos.unsqueeze(0) > qc_q_pos.unsqueeze(1) + attn_w = attn_w.masked_fill(mask.unsqueeze(0), float("-inf")) + + attn_w = torch.softmax(attn_w, dim=-1) + out_c = torch.matmul(attn_w, v_s).to(orig_dtype) # [H, qc, D] + + output[seq_start + qc_start:seq_start + qc_end] = ( + out_c.permute(1, 0, 2)) + + seq_start = seq_end + + return output.unsqueeze(0) # [1, T, H, D] + +''' + +OLD_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op = self.attn_op + ) + return out.view_as(original_query)\ +""" + +NEW_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + if self.head_size > 128: + out = self._run_sdpa_fallback(query, key, value, attn_metadata) + else: + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op=self.attn_op, + ) + return out.view_as(original_query)\ +""" + +INJECT_ANCHOR = " def _run_memory_efficient_xformers_forward(" + +_PREFIX_CALL_OLD_BLOCK = """\ + out = PagedAttention.forward_prefix( + query, + key, + value, + self.kv_cache_dtype, + key_cache, + value_cache, + prefill_meta.block_tables, + prefill_meta.query_start_loc, + prefill_meta.seq_lens_tensor, + prefill_meta.context_lens_tensor, + prefill_meta.max_query_len, + self.alibi_slopes, + self.sliding_window, + k_scale, + v_scale, + )\ +""" + +_PREFIX_CALL_NEW_BLOCK = """\ + out = PagedAttention.forward_prefix( + query, + key, + value, + self.kv_cache_dtype, + key_cache, + value_cache, + prefill_meta.block_tables, + prefill_meta.query_start_loc, + prefill_meta.seq_lens_tensor, + prefill_meta.context_lens_tensor, + prefill_meta.max_query_len, + self.alibi_slopes, + self.sliding_window, + k_scale, + v_scale, + is_causal_decoder=(attn_type == AttentionType.DECODER), + )\ +""" + + +def patch_file(path): + replace_once( + path, + INJECT_ANCHOR, + FALLBACK_METHOD + INJECT_ANCHOR, + required=True, + already_contains="def _run_sdpa_fallback(") + replace_once( + path, + OLD_XFORMER_BLOCK, + NEW_XFORMER_BLOCK, + required=True, + already_contains="out = self._run_sdpa_fallback(query, key, value, attn_metadata)") + replace_once( + path, + _PREFIX_CALL_OLD_BLOCK, + _PREFIX_CALL_NEW_BLOCK, + required=True, + already_contains=( + "is_causal_decoder=(attn_type == AttentionType.DECODER)")) + + +def patch_arg_utils(path): + replace_once( + path, + _ARG_OLD_BLOCK, + _ARG_NEW_BLOCK, + required=True, + already_contains="skip auto-enable: Q-tiling") + replace_once( + path, + _MM_PREFIX_OLD_BLOCK, + _MM_PREFIX_NEW_BLOCK, + required=True, + already_contains="Keeping prefix caching enabled for the Qwen3.6") + + +def patch_logits_processor(path): + replace_once( + path, + _LP_OLD_BLOCK, + _LP_NEW_BLOCK, + required=True, + already_contains="intermediate chunked-prefill chunk") + + +def patch_outlines_json_grammar(path): + replace_one_of( + path, + [ + (_JSON_STRING_V1_BLOCK, _JSON_STRING_NEW_BLOCK), + (_JSON_STRING_OLD_BLOCK, _JSON_STRING_NEW_BLOCK), + ], + required=True, + already_contains="JSON_WS:") + + +def main(): + print("=== patch_xformers_sdpa_seq (sequential, pure-math) ===") + print(f"Target: {XFORMERS_PATH}") + patch_file(XFORMERS_PATH) + + print("\n=== patch_arg_utils (disable chunked-prefill auto-enable) ===") + print(f"Target: {ARG_UTILS_PATH}") + patch_arg_utils(ARG_UTILS_PATH) + + print("\n=== patch_logits_processor (seq_groups=None guard for chunked prefill) ===") + print(f"Target: {LOGITS_PROC_PATH}") + patch_logits_processor(LOGITS_PROC_PATH) + + print("\n=== patch_outlines_json_grammar (reject raw control chars) ===") + print(f"Target: {OUTLINES_DECODING_PATH}") + patch_outlines_json_grammar(OUTLINES_DECODING_PATH) + + print("\nDone.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/patch_xformers_sdpa_seq_kernel.py b/qwen3_6_scripts/patch_xformers_sdpa_seq_kernel.py new file mode 100644 index 0000000..39633d6 --- /dev/null +++ b/qwen3_6_scripts/patch_xformers_sdpa_seq_kernel.py @@ -0,0 +1,166 @@ +""" +策略:顺序(per-sequence)— F.scaled_dot_product_attention,可走硬件 kernel +============================================================================= +逐条序列调用 F.scaled_dot_product_attention,is_causal=False + 显式因果 mask。 +与 patch_xformers_sdpa_seq.py(纯 matmul)的区别: + SDPA 可分发到 Flash Attention / mem-efficient attention kernel, + 而纯 matmul 固定走 cublas。 + +硬件限制(BI-V100): + cudnnFlashAttnForward 不支持 is_causal=True(直接报错)。 + 必须使用 is_causal=False + 显式 additive causal mask。 + 每条序列单独构造上三角 -inf mask,peak 显存 = max(seq_len)² × dtype, + 比 batch 版的 total_tokens² 小得多。 + +与 batch_kernel 的对比: + seq_kernel: 显存小,peak = max_single_seq²;并发 prefill 串行排队 + batch_kernel: 显存大,peak = total_tokens²;并发 prefill 一次并行处理, + 通过 --max-num-batched-tokens 控制 total_tokens 上限 + +Deploy: + python3 modified_scripts/patch_xformers_sdpa_seq_kernel.py +""" + +from patch_utils import package_root, replace_once + +XFORMERS_PATH = package_root("vllm") / "attention" / "backends" / "xformers.py" + +FALLBACK_METHOD = ''' + def _run_sdpa_fallback( + self, + query: torch.Tensor, + key: torch.Tensor, + value: torch.Tensor, + attn_metadata: "XFormersMetadata", + ) -> torch.Tensor: + """顺序 F.scaled_dot_product_attention fallback(可走硬件 kernel)。 + + 逐条序列调用 SDPA,is_causal=False + 显式上三角 additive mask。 + cudnnFlashAttnForward 不支持 is_causal=True,必须用显式 mask。 + 逐序列构造 mask,peak 显存 = max(seq_len)² × dtype(远小于 batch 版)。 + + Args: + query : [1, total_prefill_tokens, num_heads, head_dim] + key : [1, total_prefill_tokens, num_kv_heads, head_dim] + value : [1, total_prefill_tokens, num_kv_heads, head_dim] + Returns: + [1, total_prefill_tokens, num_heads, head_dim] + """ + import torch.nn.functional as F + + assert attn_metadata.seq_lens is not None + orig_dtype = query.dtype + + q_flat = query.squeeze(0) # [T, H, D] + k_flat = key.squeeze(0) # [T, Hkv, D] + v_flat = value.squeeze(0) + + output = torch.empty_like(q_flat) + start = 0 + for seq_len in attn_metadata.seq_lens: + end = start + seq_len + # [1, H, L, D] + q_s = q_flat[start:end].permute(1, 0, 2).contiguous().unsqueeze(0) + k_s = k_flat[start:end].permute(1, 0, 2).contiguous().unsqueeze(0) + v_s = v_flat[start:end].permute(1, 0, 2).contiguous().unsqueeze(0) + + # GQA:展开 KV heads + if k_s.shape[1] != q_s.shape[1]: + n = q_s.shape[1] // k_s.shape[1] + k_s = k_s.repeat_interleave(n, dim=1).contiguous() + v_s = v_s.repeat_interleave(n, dim=1).contiguous() + + # 逐序列因果 mask [L, L],上三角 -inf + causal_mask = torch.tril( + torch.zeros(seq_len, seq_len, dtype=orig_dtype, device=q_s.device) + ) + causal_mask = causal_mask.masked_fill( + torch.triu(torch.ones(seq_len, seq_len, dtype=torch.bool, + device=q_s.device), diagonal=1), + float("-inf"), + ) + + # is_causal=False + 显式 mask,规避 cudnnFlashAttnForward 不支持 is_causal=True + out_s = F.scaled_dot_product_attention( + q_s, k_s, v_s, + attn_mask=causal_mask, + dropout_p=0.0, + is_causal=False, + scale=self.scale, + ) + # [1, H, L, D] → [L, H, D] + output[start:end] = out_s.squeeze(0).permute(1, 0, 2).to(orig_dtype) + start = end + + return output.unsqueeze(0) # [1, T, H, D] + +''' + +OLD_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op = self.attn_op + ) + return out.view_as(original_query)\ +""" + +NEW_XFORMER_BLOCK = """\ + self.attn_op = xops.fmha.flash.FwOp() + if self.alibi_slopes is None: + # Add the batch dimension. + query = query.unsqueeze(0) + key = key.unsqueeze(0) + value = value.unsqueeze(0) + if self.head_size > 128: + out = self._run_sdpa_fallback(query, key, value, attn_metadata) + else: + out = xops.memory_efficient_attention_forward( + query, + key, + value, + attn_bias=attn_bias[0], + p=0.0, + scale=self.scale, + op=self.attn_op, + ) + return out.view_as(original_query)\ +""" + +INJECT_ANCHOR = " def _run_memory_efficient_xformers_forward(" + + +def patch_file(path): + replace_once( + path, + INJECT_ANCHOR, + FALLBACK_METHOD + INJECT_ANCHOR, + required=True, + already_contains="def _run_sdpa_fallback(") + replace_once( + path, + OLD_XFORMER_BLOCK, + NEW_XFORMER_BLOCK, + required=True, + already_contains="out = self._run_sdpa_fallback(query, key, value, attn_metadata)") + + +def main(): + print("=== patch_xformers_sdpa_seq_kernel (seq, F.sdpa + kernel dispatch) ===") + print(f"Target: {XFORMERS_PATH}") + patch_file(XFORMERS_PATH) + print("\nDone.") + + +if __name__ == "__main__": + main() diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/SHA256SUMS b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/SHA256SUMS new file mode 100644 index 0000000..f2915aa --- /dev/null +++ b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/SHA256SUMS @@ -0,0 +1,12 @@ +534019b3c2ad2d2c65492b01a975874ee440026eda2e8666bc3c1dc8a0a0a6f6 corex_attn_head_rms_norm.so +7e2aafd8dc755b0ee16c3b9bb812b95548fc042bbaa840dd9db7d2c51a10474c corex_block_major_kv_transfer.so +ad4ea7707bb2f2bfe04e07a7ad5fd58a647232be70a3056937a0d738c8254bff corex_fused_paged_prefill.so +1856c86e3100415061aa698a48bdeff3fe785994b45b4e72a42cd9158552a7d8 corex_gdn_beta_decay.so +957c7518f5831299fc73f19a4ca2aa3c8231afe9ea7c979127b4f426cd9d6906 corex_gdn_causal_conv.so +ec2d11fa82d9d0816a6da53e62605e962786fa20ecd5f62e50f9d43087fc4d67 corex_gdn_gated_norm.so +27b7ae2ce4fe173336355d72a2678d043df4bd1ed85e9231a99bfb81885a6ce3 corex_gdn_packed_decode.so +015b61046ad73d8f12d754f7a87d4f6cba33070af1c079879e15b71a94571670 corex_gdn_qk_map.so +0eb120e89608bb5b64ca4356a5d3d362121806d081ccc1ccf346dac472a819ec corex_moe_direct_routed.so +d26f2fa39c3921a95793786601e90cf6ebadd06f1d752af541bf82c21acbc1c9 corex_moe_exact_reduce.so +50b0b44c1da779bb2c03419ed549aee9bb922d1f9bab8b7f11a3d91cca0d21c3 corex_moe_weight_gather.so +e944ec0528ed9b6cb74518de3c57e3730543a7bdebc872f993bfdc8424f13e6b corex_paged_kv_gather.so diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_attn_head_rms_norm.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_attn_head_rms_norm.so new file mode 100755 index 0000000..27a560d Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_attn_head_rms_norm.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_block_major_kv_transfer.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_block_major_kv_transfer.so new file mode 100755 index 0000000..ac96e36 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_block_major_kv_transfer.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_fused_paged_prefill.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_fused_paged_prefill.so new file mode 100644 index 0000000..f45e017 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_fused_paged_prefill.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_beta_decay.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_beta_decay.so new file mode 100755 index 0000000..89ad2a2 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_beta_decay.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_causal_conv.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_causal_conv.so new file mode 100755 index 0000000..f9fa884 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_causal_conv.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_gated_norm.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_gated_norm.so new file mode 100755 index 0000000..e858d82 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_gated_norm.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_packed_decode.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_packed_decode.so new file mode 100755 index 0000000..b3d0ea4 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_packed_decode.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_qk_map.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_qk_map.so new file mode 100755 index 0000000..ccd9187 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_gdn_qk_map.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_direct_routed.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_direct_routed.so new file mode 100755 index 0000000..15368ca Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_direct_routed.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_exact_reduce.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_exact_reduce.so new file mode 100755 index 0000000..21f0363 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_exact_reduce.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_weight_gather.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_weight_gather.so new file mode 100755 index 0000000..ce05424 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_moe_weight_gather.so differ diff --git a/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_paged_kv_gather.so b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_paged_kv_gather.so new file mode 100755 index 0000000..9810c97 Binary files /dev/null and b/qwen3_6_scripts/prebuilt/corex-3.2.3-ivcore10/corex_paged_kv_gather.so differ diff --git a/qwen3_6_scripts/protocol.py b/qwen3_6_scripts/protocol.py new file mode 100644 index 0000000..a51a1c5 --- /dev/null +++ b/qwen3_6_scripts/protocol.py @@ -0,0 +1,1215 @@ +# Adapted from +# https://github.com/lm-sys/FastChat/blob/168ccc29d3f7edc50823016105c024fe2282732a/fastchat/protocol/openai_api_protocol.py +import json +import time +from argparse import Namespace +from typing import Any, Dict, List, Literal, Optional, Union + +import torch +from openai.types.chat import ChatCompletionContentPartParam +from pydantic import BaseModel, ConfigDict, Field, model_validator +from typing_extensions import Annotated, Required, TypedDict + +from vllm.entrypoints.chat_utils import ChatCompletionMessageParam +from vllm.pooling_params import PoolingParams +from vllm.sampling_params import (BeamSearchParams, GuidedDecodingParams, + RequestOutputKind, SamplingParams) +from vllm.sequence import Logprob +from vllm.utils import random_uuid + +# torch is mocked during docs generation, +# so we have to provide the values as literals +_MOCK_LONG_INFO = Namespace(min=-9223372036854775808, max=9223372036854775807) +_LONG_INFO: Union["torch.iinfo", Namespace] + +try: + from sphinx.ext.autodoc.mock import _MockModule + + if isinstance(torch, _MockModule): + _LONG_INFO = _MOCK_LONG_INFO + else: + _LONG_INFO = torch.iinfo(torch.long) +except ModuleNotFoundError: + _LONG_INFO = torch.iinfo(torch.long) + +assert _LONG_INFO.min == _MOCK_LONG_INFO.min +assert _LONG_INFO.max == _MOCK_LONG_INFO.max + + +class CustomChatCompletionMessageParam(TypedDict, total=False): + """Enables custom roles in the Chat Completion API.""" + role: Required[str] + """The role of the message's author.""" + + content: Union[str, List[ChatCompletionContentPartParam]] + """The contents of the message.""" + + name: str + """An optional name for the participant. + + Provides the model information to differentiate between participants of the + same role. + """ + + tool_call_id: Optional[str] + + tool_calls: Optional[List[dict]] + + +class OpenAIBaseModel(BaseModel): + # OpenAI API does not allow extra fields + model_config = ConfigDict(extra="forbid") + + +class ErrorResponse(OpenAIBaseModel): + object: str = "error" + message: str + type: str + param: Optional[str] = None + code: int + + +class ModelPermission(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"modelperm-{random_uuid()}") + object: str = "model_permission" + created: int = Field(default_factory=lambda: int(time.time())) + allow_create_engine: bool = False + allow_sampling: bool = True + allow_logprobs: bool = True + allow_search_indices: bool = False + allow_view: bool = True + allow_fine_tuning: bool = False + organization: str = "*" + group: Optional[str] = None + is_blocking: bool = False + + +class ModelCard(OpenAIBaseModel): + id: str + object: str = "model" + created: int = Field(default_factory=lambda: int(time.time())) + owned_by: str = "vllm" + root: Optional[str] = None + parent: Optional[str] = None + max_model_len: Optional[int] = None + permission: List[ModelPermission] = Field(default_factory=list) + + +class ModelList(OpenAIBaseModel): + object: str = "list" + data: List[ModelCard] = Field(default_factory=list) + + +class PromptTokensDetails(OpenAIBaseModel): + cached_tokens: int = 0 + + +class UsageInfo(OpenAIBaseModel): + prompt_tokens: int = 0 + total_tokens: int = 0 + completion_tokens: Optional[int] = 0 + reasoning_tokens: Optional[int] = None + prompt_tokens_details: Optional[PromptTokensDetails] = None + + +class RequestResponseMetadata(BaseModel): + request_id: str + final_usage_info: Optional[UsageInfo] = None + + +class JsonSchemaResponseFormat(OpenAIBaseModel): + name: str + description: Optional[str] = None + # schema is the field in openai but that causes conflicts with pydantic so + # instead use json_schema with an alias + json_schema: Optional[Dict[str, Any]] = Field(default=None, alias='schema') + strict: Optional[bool] = None + + +class ResponseFormat(OpenAIBaseModel): + # type must be "json_schema", "json_object" or "text" + type: Literal["text", "json_object", "json_schema"] + json_schema: Optional[JsonSchemaResponseFormat] = None + + +class StreamOptions(OpenAIBaseModel): + include_usage: Optional[bool] = True + continuous_usage_stats: Optional[bool] = True + + +class FunctionDefinition(OpenAIBaseModel): + name: str + description: Optional[str] = None + parameters: Optional[Dict[str, Any]] = None + # OpenAI clients commonly serialize strict=false explicitly. It is a + # semantic no-op, so accept it but keep it out of the tokenizer template. + # strict=true requires constrained tool decoding that this runtime does not + # provide and must not be silently degraded to ordinary auto tool choice. + strict: Optional[bool] = Field(default=None, exclude=True) + + @model_validator(mode="after") + def reject_unsupported_strict_tools(self): + if self.strict is True: + raise ValueError( + "Function tools with strict=true are not supported by this " + "runtime.") + return self + + +class ChatCompletionToolsParam(OpenAIBaseModel): + type: Literal["function"] = "function" + function: FunctionDefinition + + +class ChatCompletionNamedFunction(OpenAIBaseModel): + name: str + + +class ChatCompletionNamedToolChoiceParam(OpenAIBaseModel): + function: ChatCompletionNamedFunction + type: Literal["function"] = "function" + + +class ChatCompletionRequest(OpenAIBaseModel): + # Ordered by official OpenAI API documentation + # https://platform.openai.com/docs/api-reference/chat/create + messages: List[ChatCompletionMessageParam] + model: str + frequency_penalty: Optional[float] = 0.0 + logit_bias: Optional[Dict[str, float]] = None + logprobs: Optional[bool] = False + top_logprobs: Optional[int] = 0 + max_tokens: Optional[int] = None + n: Optional[int] = 1 + presence_penalty: Optional[float] = 0.0 + response_format: Optional[ResponseFormat] = None + seed: Optional[int] = Field(None, ge=_LONG_INFO.min, le=_LONG_INFO.max) + stop: Optional[Union[str, List[str]]] = Field(default_factory=list) + stream: Optional[bool] = False + stream_options: Optional[StreamOptions] = None + temperature: Optional[float] = 0.7 + top_p: Optional[float] = 1.0 + tools: Optional[List[ChatCompletionToolsParam]] = None + tool_choice: Optional[Union[Literal["none"], Literal["auto"], + ChatCompletionNamedToolChoiceParam]] = "none" + thinking: Optional[Union[bool, str, Dict[str, Any]]] = None + + # NOTE this will be ignored by VLLM -- the model determines the behavior + parallel_tool_calls: Optional[bool] = False + user: Optional[str] = None + + # doc: begin-chat-completion-sampling-params + best_of: Optional[int] = None + use_beam_search: bool = False + top_k: int = -1 + min_p: float = 0.0 + repetition_penalty: float = 1.0 + length_penalty: float = 1.0 + stop_token_ids: Optional[List[int]] = Field(default_factory=list) + include_stop_str_in_output: bool = False + ignore_eos: bool = False + min_tokens: int = 0 + skip_special_tokens: bool = True + spaces_between_special_tokens: bool = True + truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None + prompt_logprobs: Optional[int] = None + bi100_prompt_logprobs_sample_positions: Optional[List[int]] = None + # doc: end-chat-completion-sampling-params + + # doc: begin-chat-completion-extra-params + echo: bool = Field( + default=False, + description=( + "If true, the new message will be prepended with the last message " + "if they belong to the same role."), + ) + add_generation_prompt: bool = Field( + default=True, + description= + ("If true, the generation prompt will be added to the chat template. " + "This is a parameter used by chat template in tokenizer config of the " + "model."), + ) + continue_final_message: bool = Field( + default=False, + description= + ("If this is set, the chat will be formatted so that the final " + "message in the chat is open-ended, without any EOS tokens. The " + "model will continue this message rather than starting a new one. " + "This allows you to \"prefill\" part of the model's response for it. " + "Cannot be used at the same time as `add_generation_prompt`."), + ) + add_special_tokens: bool = Field( + default=False, + description=( + "If true, special tokens (e.g. BOS) will be added to the prompt " + "on top of what is added by the chat template. " + "For most models, the chat template takes care of adding the " + "special tokens so this should be set to false (as is the " + "default)."), + ) + documents: Optional[List[Dict[str, str]]] = Field( + default=None, + description= + ("A list of dicts representing documents that will be accessible to " + "the model if it is performing RAG (retrieval-augmented generation)." + " If the template does not support RAG, this argument will have no " + "effect. We recommend that each document should be a dict containing " + "\"title\" and \"text\" keys."), + ) + chat_template: Optional[str] = Field( + default=None, + description=( + "A Jinja template to use for this conversion. " + "As of transformers v4.44, default chat template is no longer " + "allowed, so you must provide a chat template if the tokenizer " + "does not define one."), + ) + chat_template_kwargs: Optional[Dict[str, Any]] = Field( + default=None, + description=("Additional kwargs to pass to the template renderer. " + "Will be accessible by the chat template."), + ) + guided_json: Optional[Union[str, dict, BaseModel]] = Field( + default=None, + description=("If specified, the output will follow the JSON schema."), + ) + guided_regex: Optional[str] = Field( + default=None, + description=( + "If specified, the output will follow the regex pattern."), + ) + guided_choice: Optional[List[str]] = Field( + default=None, + description=( + "If specified, the output will be exactly one of the choices."), + ) + guided_grammar: Optional[str] = Field( + default=None, + description=( + "If specified, the output will follow the context free grammar."), + ) + guided_decoding_backend: Optional[str] = Field( + default=None, + description=( + "If specified, will override the default guided decoding backend " + "of the server for this specific request. If set, must be either " + "'outlines' / 'lm-format-enforcer'")) + guided_whitespace_pattern: Optional[str] = Field( + default=None, + description=( + "If specified, will override the default whitespace pattern " + "for guided json decoding.")) + priority: int = Field( + default=0, + description=( + "The priority of the request (lower means earlier handling; " + "default: 0). Any priority other than 0 will raise an error " + "if the served model does not use priority scheduling.")) + + # doc: end-chat-completion-extra-params + + def to_beam_search_params(self, + default_max_tokens: int) -> BeamSearchParams: + max_tokens = self.max_tokens + if max_tokens is None: + max_tokens = default_max_tokens + + n = self.n if self.n is not None else 1 + temperature = self.temperature if self.temperature is not None else 0.0 + + return BeamSearchParams( + beam_width=n, + max_tokens=max_tokens, + ignore_eos=self.ignore_eos, + temperature=temperature, + length_penalty=self.length_penalty, + ) + + def to_sampling_params(self, default_max_tokens: int) -> SamplingParams: + max_tokens = self.max_tokens + if max_tokens is None: + max_tokens = default_max_tokens + + prompt_logprobs = self.prompt_logprobs + if prompt_logprobs is None and self.echo: + prompt_logprobs = self.top_logprobs + + guided_json_object = None + guided_json_from_schema = None + if self.response_format is not None: + if self.response_format.type == "json_object": + # The generic CFG backend has a stateful first-request bug in + # this vLLM/Outlines build. A generic object schema has the + # same API semantics and uses the stable regex backend. + guided_json_from_schema = {"type": "object"} + elif (self.response_format.type == "json_schema" + and self.response_format.json_schema is not None + and self.response_format.json_schema.json_schema is not None): + guided_json_from_schema = \ + self.response_format.json_schema.json_schema + + guided_decoding = GuidedDecodingParams.from_optional( + json=(self._get_guided_json_from_tool() + or self.guided_json + or guided_json_from_schema), + regex=self.guided_regex, + choice=self.guided_choice, + grammar=self.guided_grammar, + json_object=guided_json_object, + backend=self.guided_decoding_backend, + whitespace_pattern=self.guided_whitespace_pattern) + + return SamplingParams.from_optional( + n=self.n, + best_of=self.best_of, + presence_penalty=self.presence_penalty, + frequency_penalty=self.frequency_penalty, + repetition_penalty=self.repetition_penalty, + temperature=self.temperature, + top_p=self.top_p, + top_k=self.top_k, + min_p=self.min_p, + seed=self.seed, + stop=self.stop, + stop_token_ids=self.stop_token_ids, + logprobs=self.top_logprobs if self.logprobs else None, + prompt_logprobs=prompt_logprobs, + prompt_logprob_positions=( + self.bi100_prompt_logprobs_sample_positions), + ignore_eos=self.ignore_eos, + max_tokens=max_tokens, + min_tokens=self.min_tokens, + skip_special_tokens=self.skip_special_tokens, + spaces_between_special_tokens=self.spaces_between_special_tokens, + include_stop_str_in_output=self.include_stop_str_in_output, + truncate_prompt_tokens=self.truncate_prompt_tokens, + output_kind=RequestOutputKind.DELTA if self.stream \ + else RequestOutputKind.FINAL_ONLY, + guided_decoding=guided_decoding, + logit_bias=self.logit_bias) + + def _get_guided_json_from_tool( + self) -> Optional[Union[str, dict, BaseModel]]: + # user has chosen to not use any tool + if self.tool_choice == "none" or self.tools is None: + return None + + # user has chosen to use a named tool + if type(self.tool_choice) is ChatCompletionNamedToolChoiceParam: + tool_name = self.tool_choice.function.name + tools = {tool.function.name: tool.function for tool in self.tools} + if tool_name not in tools: + raise ValueError( + f"Tool '{tool_name}' has not been passed in `tools`.") + tool = tools[tool_name] + return tool.parameters + + return None + + @model_validator(mode="before") + @classmethod + def normalize_messages(cls, data): + """Normalize incoming messages before pydantic union validation. + + Real-world clients (e.g. from other providers) send assistant tool_call + messages with content=null, which fails the strict Union type check. + Replace null content with "" so validation passes. + reasoning_content is intentionally kept — chat_utils.py wraps it as + ... for multi-turn reasoning history. + """ + messages = data.get("messages") + if not isinstance(messages, list): + return data + normalized = [] + for msg in messages: + if not isinstance(msg, dict): + normalized.append(msg) + continue + tool_calls = msg.get("tool_calls") + if isinstance(tool_calls, list): + normalized_calls = [] + for call in tool_calls: + if not isinstance(call, dict): + normalized_calls.append(call) + continue + function = call.get("function") + if not isinstance(function, dict): + normalized_calls.append(call) + continue + arguments = function.get("arguments") + if isinstance(arguments, dict): + arguments = json.dumps( + arguments, + ensure_ascii=False, + separators=(",", ":"), + ) + elif isinstance(arguments, str): + try: + decoded_arguments = json.loads(arguments) + except json.JSONDecodeError as exc: + raise ValueError( + "Tool call arguments are not valid JSON." + ) from exc + if not isinstance(decoded_arguments, dict): + raise ValueError( + "Tool call arguments must decode to a JSON " + "object.") + elif arguments is not None: + raise ValueError( + "Tool call arguments must be a JSON object or a " + "JSON-encoded object string.") + if arguments is not None: + function = {**function, "arguments": arguments} + call = {**call, "function": function} + normalized_calls.append(call) + msg = {**msg, "tool_calls": normalized_calls} + if msg.get("content") is None: + if (msg.get("reasoning_content") is None + and not msg.get("tool_calls")): + raise ValueError( + "Each message must have at least one of 'content' or " + "'reasoning_content', or contain 'tool_calls'.") + msg = {**msg, "content": ""} + if (msg.get("role") == "system" + and isinstance(msg.get("content"), list)): + content_parts = msg["content"] + if all( + isinstance(part, dict) + and part.get("type") == "text" + and isinstance(part.get("text"), str) + for part in content_parts): + # Match chat_utils' existing text-part semantics before + # combining multiple system messages for Qwen. + msg = { + **msg, + "content": "\n".join( + part["text"] for part in content_parts), + } + normalized.append(msg) + + # Qwen's tokenizer template accepts at most one system message and + # requires it to be first. OpenAI-compatible clients may send several + # system messages, including after conversation history. Preserve + # their order and semantics by merging text content at the beginning. + system_messages = [ + msg for msg in normalized + if isinstance(msg, dict) and msg.get("role") == "system" + ] + if system_messages: + system_contents = [ + msg.get("content") for msg in system_messages + ] + if all(isinstance(content, str) + for content in system_contents): + merged_system = { + **system_messages[0], + "content": "\n\n".join(system_contents), + } + normalized = [merged_system] + [ + msg for msg in normalized + if not (isinstance(msg, dict) + and msg.get("role") == "system") + ] + data = {**data, "messages": normalized} + return data + + @model_validator(mode="before") + @classmethod + def normalize_thinking(cls, data): + thinking = data.get("thinking") + if thinking is None: + return data + + enable_thinking: Optional[bool] = None + if thinking is False: + enable_thinking = False + elif thinking is True: + enable_thinking = True + elif isinstance(thinking, str): + lowered = thinking.lower() + if lowered == "disabled": + enable_thinking = False + elif lowered == "enabled": + enable_thinking = True + elif isinstance(thinking, dict): + thinking_type = thinking.get("type") + if isinstance(thinking_type, str): + lowered = thinking_type.lower() + if lowered == "disabled": + enable_thinking = False + elif lowered == "enabled": + enable_thinking = True + + if enable_thinking is None: + raise ValueError( + "`thinking` must be false, \"disabled\", true, \"enabled\", " + "or an object with type \"disabled\"/\"enabled\".") + + chat_template_kwargs = dict(data.get("chat_template_kwargs") or {}) + chat_template_kwargs["enable_thinking"] = enable_thinking + data = {**data, "chat_template_kwargs": chat_template_kwargs} + return data + + @model_validator(mode="before") + @classmethod + def validate_stream_options(cls, data): + if data.get("stream_options") and not data.get("stream"): + raise ValueError( + "Stream options can only be defined when `stream=True`.") + + return data + + @model_validator(mode="before") + @classmethod + def check_logprobs(cls, data): + if (prompt_logprobs := data.get("prompt_logprobs")) is not None: + if data.get("stream") and prompt_logprobs > 0: + raise ValueError( + "`prompt_logprobs` are not available when `stream=True`.") + + if prompt_logprobs < 0: + raise ValueError("`prompt_logprobs` must be a positive value.") + + if (top_logprobs := data.get("top_logprobs")) is not None: + if top_logprobs < 0: + raise ValueError("`top_logprobs` must be a positive value.") + + if not data.get("logprobs"): + raise ValueError( + "when using `top_logprobs`, `logprobs` must be set to true." + ) + + return data + + @model_validator(mode="before") + @classmethod + def validate_bi100_prompt_logprob_sample(cls, data): + positions = data.get("bi100_prompt_logprobs_sample_positions") + if positions is None: + return data + if ( + not isinstance(positions, list) + or not positions + or len(positions) > 4096 + or any( + not isinstance(position, int) + or isinstance(position, bool) + or position <= 0 + or position >= 262144 + for position in positions + ) + or positions != sorted(set(positions)) + ): + raise ValueError( + "`bi100_prompt_logprobs_sample_positions` must be a sorted " + "unique list of prompt positions in [1, 262143].") + if data.get("stream"): + raise ValueError( + "BI100 sampled prompt logprobs require `stream=False`.") + if not isinstance(data.get("prompt_logprobs"), int) \ + or data["prompt_logprobs"] <= 0: + raise ValueError( + "BI100 sampled prompt logprobs require positive " + "`prompt_logprobs`.") + return data + + @model_validator(mode="before") + @classmethod + def check_guided_decoding_count(cls, data): + if isinstance(data, ValueError): + raise data + + guide_count = sum([ + "guided_json" in data and data["guided_json"] is not None, + "guided_regex" in data and data["guided_regex"] is not None, + "guided_choice" in data and data["guided_choice"] is not None + ]) + # you can only use one kind of guided decoding + if guide_count > 1: + raise ValueError( + "You can only use one kind of guided decoding " + "('guided_json', 'guided_regex' or 'guided_choice').") + # you can only either use guided decoding or a forced tool, not both + if guide_count > 0 and data.get("tool_choice", + "none") not in ("none", "auto"): + raise ValueError( + "You can only either use guided decoding or tools, not both.") + return data + + @model_validator(mode="before") + @classmethod + def check_tool_usage(cls, data): + + # if "tool_choice" is not specified but tools are provided, + # default to "auto" tool_choice + if "tool_choice" not in data and data.get("tools"): + data["tool_choice"] = "auto" + + # if "tool_choice" is specified -- validation + if "tool_choice" in data: + if data["tool_choice"] == "none": + return data + + # ensure that if "tool choice" is specified, tools are present + if "tools" not in data or data["tools"] is None: + raise ValueError( + "When using `tool_choice`, `tools` must be set.") + + # make sure that tool choice is either a named tool + # OR that it's set to "auto"/"none" + if data["tool_choice"] != "auto" and not isinstance( + data["tool_choice"], dict): + raise ValueError( + "`tool_choice` must be a named tool, \"auto\", or " + "\"none\".") + + # ensure that if "tool_choice" is specified as an object, + # it matches a valid tool + if isinstance(data["tool_choice"], dict): + valid_tool = False + specified_function = data["tool_choice"]["function"] + if not specified_function: + raise ValueError( + "Incorrectly formatted `tool_choice`. Should be like " + "`{\"type\": \"function\"," + " \"function\": {\"name\": \"my_function\"}}`") + specified_function_name = specified_function["name"] + if not specified_function_name: + raise ValueError( + "Incorrectly formatted `tool_choice`. Should be like " + "`{\"type\": \"function\", " + "\"function\": {\"name\": \"my_function\"}}`") + for tool in data["tools"]: + if tool["function"]["name"] == specified_function_name: + valid_tool = True + break + if not valid_tool: + raise ValueError( + "The tool specified in `tool_choice` does not match any" + " of the specified `tools`") + return data + + @model_validator(mode="before") + @classmethod + def check_generation_prompt(cls, data): + if data.get("continue_final_message") and data.get( + "add_generation_prompt"): + raise ValueError("Cannot set both `continue_final_message` and " + "`add_generation_prompt` to True.") + return data + + +class CompletionRequest(OpenAIBaseModel): + # Ordered by official OpenAI API documentation + # https://platform.openai.com/docs/api-reference/completions/create + model: str + prompt: Union[List[int], List[List[int]], str, List[str]] + best_of: Optional[int] = None + echo: Optional[bool] = False + frequency_penalty: Optional[float] = 0.0 + logit_bias: Optional[Dict[str, float]] = None + logprobs: Optional[int] = None + max_tokens: Optional[int] = 16 + n: int = 1 + presence_penalty: Optional[float] = 0.0 + seed: Optional[int] = Field(None, ge=_LONG_INFO.min, le=_LONG_INFO.max) + stop: Optional[Union[str, List[str]]] = Field(default_factory=list) + stream: Optional[bool] = False + stream_options: Optional[StreamOptions] = None + suffix: Optional[str] = None + temperature: Optional[float] = 1.0 + top_p: Optional[float] = 1.0 + user: Optional[str] = None + + # doc: begin-completion-sampling-params + use_beam_search: bool = False + top_k: int = -1 + min_p: float = 0.0 + repetition_penalty: float = 1.0 + length_penalty: float = 1.0 + stop_token_ids: Optional[List[int]] = Field(default_factory=list) + include_stop_str_in_output: bool = False + ignore_eos: bool = False + min_tokens: int = 0 + skip_special_tokens: bool = True + spaces_between_special_tokens: bool = True + truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None + allowed_token_ids: Optional[List[int]] = None + prompt_logprobs: Optional[int] = None + # doc: end-completion-sampling-params + + # doc: begin-completion-extra-params + add_special_tokens: bool = Field( + default=True, + description=( + "If true (the default), special tokens (e.g. BOS) will be added to " + "the prompt."), + ) + response_format: Optional[ResponseFormat] = Field( + default=None, + description= + ("Similar to chat completion, this parameter specifies the format of " + "output. Only {'type': 'json_object'} or {'type': 'text' } is " + "supported."), + ) + guided_json: Optional[Union[str, dict, BaseModel]] = Field( + default=None, + description="If specified, the output will follow the JSON schema.", + ) + guided_regex: Optional[str] = Field( + default=None, + description=( + "If specified, the output will follow the regex pattern."), + ) + guided_choice: Optional[List[str]] = Field( + default=None, + description=( + "If specified, the output will be exactly one of the choices."), + ) + guided_grammar: Optional[str] = Field( + default=None, + description=( + "If specified, the output will follow the context free grammar."), + ) + guided_decoding_backend: Optional[str] = Field( + default=None, + description=( + "If specified, will override the default guided decoding backend " + "of the server for this specific request. If set, must be one of " + "'outlines' / 'lm-format-enforcer'")) + guided_whitespace_pattern: Optional[str] = Field( + default=None, + description=( + "If specified, will override the default whitespace pattern " + "for guided json decoding.")) + priority: int = Field( + default=0, + description=( + "The priority of the request (lower means earlier handling; " + "default: 0). Any priority other than 0 will raise an error " + "if the served model does not use priority scheduling.")) + + # doc: end-completion-extra-params + + def to_beam_search_params(self, + default_max_tokens: int) -> BeamSearchParams: + max_tokens = self.max_tokens + if max_tokens is None: + max_tokens = default_max_tokens + + n = self.n if self.n is not None else 1 + temperature = self.temperature if self.temperature is not None else 0.0 + + return BeamSearchParams( + beam_width=n, + max_tokens=max_tokens, + ignore_eos=self.ignore_eos, + temperature=temperature, + length_penalty=self.length_penalty, + ) + + def to_sampling_params(self, default_max_tokens: int) -> SamplingParams: + max_tokens = self.max_tokens + if max_tokens is None: + max_tokens = default_max_tokens + + prompt_logprobs = self.prompt_logprobs + if prompt_logprobs is None and self.echo: + prompt_logprobs = self.logprobs + + echo_without_generation = self.echo and self.max_tokens == 0 + + guided_json_object = None + guided_json_from_schema = None + if self.response_format is not None: + if self.response_format.type == "json_object": + # Keep CompletionRequest aligned with ChatCompletionRequest. + guided_json_from_schema = {"type": "object"} + elif (self.response_format.type == "json_schema" + and self.response_format.json_schema is not None + and self.response_format.json_schema.json_schema is not None): + guided_json_from_schema = \ + self.response_format.json_schema.json_schema + + guided_decoding = GuidedDecodingParams.from_optional( + json=self.guided_json or guided_json_from_schema, + regex=self.guided_regex, + choice=self.guided_choice, + grammar=self.guided_grammar, + json_object=guided_json_object, + backend=self.guided_decoding_backend, + whitespace_pattern=self.guided_whitespace_pattern) + + return SamplingParams.from_optional( + n=self.n, + best_of=self.best_of, + presence_penalty=self.presence_penalty, + frequency_penalty=self.frequency_penalty, + repetition_penalty=self.repetition_penalty, + temperature=self.temperature, + top_p=self.top_p, + top_k=self.top_k, + min_p=self.min_p, + seed=self.seed, + stop=self.stop, + stop_token_ids=self.stop_token_ids, + logprobs=self.logprobs, + ignore_eos=self.ignore_eos, + max_tokens=max_tokens if not echo_without_generation else 1, + min_tokens=self.min_tokens, + prompt_logprobs=prompt_logprobs, + skip_special_tokens=self.skip_special_tokens, + spaces_between_special_tokens=self.spaces_between_special_tokens, + include_stop_str_in_output=self.include_stop_str_in_output, + truncate_prompt_tokens=self.truncate_prompt_tokens, + output_kind=RequestOutputKind.DELTA if self.stream \ + else RequestOutputKind.FINAL_ONLY, + guided_decoding=guided_decoding, + logit_bias=self.logit_bias, + allowed_token_ids=self.allowed_token_ids) + + @model_validator(mode="before") + @classmethod + def check_guided_decoding_count(cls, data): + guide_count = sum([ + "guided_json" in data and data["guided_json"] is not None, + "guided_regex" in data and data["guided_regex"] is not None, + "guided_choice" in data and data["guided_choice"] is not None + ]) + if guide_count > 1: + raise ValueError( + "You can only use one kind of guided decoding " + "('guided_json', 'guided_regex' or 'guided_choice').") + return data + + @model_validator(mode="before") + @classmethod + def check_logprobs(cls, data): + if (prompt_logprobs := data.get("prompt_logprobs")) is not None: + if data.get("stream") and prompt_logprobs > 0: + raise ValueError( + "`prompt_logprobs` are not available when `stream=True`.") + + if prompt_logprobs < 0: + raise ValueError("`prompt_logprobs` must be a positive value.") + + if (logprobs := data.get("logprobs")) is not None and logprobs < 0: + raise ValueError("`logprobs` must be a positive value.") + + return data + + @model_validator(mode="before") + @classmethod + def validate_stream_options(cls, data): + if data.get("stream_options") and not data.get("stream"): + raise ValueError( + "Stream options can only be defined when `stream=True`.") + + return data + + +class EmbeddingRequest(OpenAIBaseModel): + # Ordered by official OpenAI API documentation + # https://platform.openai.com/docs/api-reference/embeddings + model: str + input: Union[List[int], List[List[int]], str, List[str]] + encoding_format: Literal["float", "base64"] = "float" + dimensions: Optional[int] = None + user: Optional[str] = None + truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None + + # doc: begin-embedding-pooling-params + additional_data: Optional[Any] = None + + # doc: end-embedding-pooling-params + + # doc: begin-embedding-extra-params + priority: int = Field( + default=0, + description=( + "The priority of the request (lower means earlier handling; " + "default: 0). Any priority other than 0 will raise an error " + "if the served model does not use priority scheduling.")) + + # doc: end-embedding-extra-params + + def to_pooling_params(self): + return PoolingParams(additional_data=self.additional_data) + + +class CompletionLogProbs(OpenAIBaseModel): + text_offset: List[int] = Field(default_factory=list) + token_logprobs: List[Optional[float]] = Field(default_factory=list) + tokens: List[str] = Field(default_factory=list) + top_logprobs: List[Optional[Dict[str, + float]]] = Field(default_factory=list) + + +class CompletionResponseChoice(OpenAIBaseModel): + index: int + text: str + logprobs: Optional[CompletionLogProbs] = None + finish_reason: Optional[str] = None + stop_reason: Optional[Union[int, str]] = Field( + default=None, + description=( + "The stop string or token id that caused the completion " + "to stop, None if the completion finished for some other reason " + "including encountering the EOS token"), + ) + prompt_logprobs: Optional[List[Optional[Dict[int, Logprob]]]] = None + + +class CompletionResponse(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"cmpl-{random_uuid()}") + object: str = "text_completion" + created: int = Field(default_factory=lambda: int(time.time())) + model: str + choices: List[CompletionResponseChoice] + usage: UsageInfo + + +class CompletionResponseStreamChoice(OpenAIBaseModel): + index: int + text: str + logprobs: Optional[CompletionLogProbs] = None + finish_reason: Optional[str] = None + stop_reason: Optional[Union[int, str]] = Field( + default=None, + description=( + "The stop string or token id that caused the completion " + "to stop, None if the completion finished for some other reason " + "including encountering the EOS token"), + ) + + +class CompletionStreamResponse(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"cmpl-{random_uuid()}") + object: str = "text_completion" + created: int = Field(default_factory=lambda: int(time.time())) + model: str + choices: List[CompletionResponseStreamChoice] + usage: Optional[UsageInfo] = Field(default=None) + + +class EmbeddingResponseData(OpenAIBaseModel): + index: int + object: str = "embedding" + embedding: Union[List[float], str] + + +class EmbeddingResponse(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"cmpl-{random_uuid()}") + object: str = "list" + created: int = Field(default_factory=lambda: int(time.time())) + model: str + data: List[EmbeddingResponseData] + usage: UsageInfo + + +class FunctionCall(OpenAIBaseModel): + name: str + arguments: str + + +class ToolCall(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"chatcmpl-tool-{random_uuid()}") + type: Literal["function"] = "function" + function: FunctionCall + + +class DeltaFunctionCall(BaseModel): + name: Optional[str] = None + arguments: Optional[str] = None + + +# a tool call delta where everything is optional +class DeltaToolCall(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"chatcmpl-tool-{random_uuid()}") + type: Literal["function"] = "function" + index: int + function: Optional[DeltaFunctionCall] = None + + +class ExtractedToolCallInformation(BaseModel): + # indicate if tools were called + tools_called: bool + + # extracted tool calls + tool_calls: List[ToolCall] + + # content - per OpenAI spec, content AND tool calls can be returned rarely + # But some models will do this intentionally + content: Optional[str] = None + + +class ChatMessage(OpenAIBaseModel): + role: str + reasoning_content: Optional[str] = None + content: Optional[str] = None + tool_calls: List[ToolCall] = Field(default_factory=list) + + +class ChatCompletionLogProb(OpenAIBaseModel): + token: str + logprob: float = -9999.0 + bytes: Optional[List[int]] = None + + +class ChatCompletionLogProbsContent(ChatCompletionLogProb): + top_logprobs: List[ChatCompletionLogProb] = Field(default_factory=list) + + +class ChatCompletionLogProbs(OpenAIBaseModel): + content: Optional[List[ChatCompletionLogProbsContent]] = None + + +class ChatCompletionResponseChoice(OpenAIBaseModel): + index: int + message: ChatMessage + logprobs: Optional[ChatCompletionLogProbs] = None + # per OpenAI spec this is the default + finish_reason: Optional[str] = "stop" + # not part of the OpenAI spec but included in vLLM for legacy reasons + stop_reason: Optional[Union[int, str]] = None + + +class ChatCompletionResponse(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}") + object: Literal["chat.completion"] = "chat.completion" + created: int = Field(default_factory=lambda: int(time.time())) + model: str + choices: List[ChatCompletionResponseChoice] + usage: UsageInfo + prompt_logprobs: Optional[List[Optional[Dict[int, Logprob]]]] = None + + +class DeltaMessage(OpenAIBaseModel): + role: Optional[str] = None + reasoning_content: Optional[str] = None + content: Optional[str] = None + tool_calls: List[DeltaToolCall] = Field(default_factory=list) + + +class ChatCompletionResponseStreamChoice(OpenAIBaseModel): + index: int + delta: DeltaMessage + logprobs: Optional[ChatCompletionLogProbs] = None + finish_reason: Optional[str] = None + stop_reason: Optional[Union[int, str]] = None + + +class ChatCompletionStreamResponse(OpenAIBaseModel): + id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}") + object: Literal["chat.completion.chunk"] = "chat.completion.chunk" + created: int = Field(default_factory=lambda: int(time.time())) + model: str + choices: List[ChatCompletionResponseStreamChoice] + usage: Optional[UsageInfo] = Field(default=None) + + +class BatchRequestInput(OpenAIBaseModel): + """ + The per-line object of the batch input file. + + NOTE: Currently only the `/v1/chat/completions` endpoint is supported. + """ + + # A developer-provided per-request id that will be used to match outputs to + # inputs. Must be unique for each request in a batch. + custom_id: str + + # The HTTP method to be used for the request. Currently only POST is + # supported. + method: str + + # The OpenAI API relative URL to be used for the request. Currently + # /v1/chat/completions is supported. + url: str + + # The parameters of the request. + body: Union[ChatCompletionRequest, EmbeddingRequest] + + +class BatchResponseData(OpenAIBaseModel): + # HTTP status code of the response. + status_code: int = 200 + + # An unique identifier for the API request. + request_id: str + + # The body of the response. + body: Optional[Union[ChatCompletionResponse, EmbeddingResponse]] = None + + +class BatchRequestOutput(OpenAIBaseModel): + """ + The per-line object of the batch output and error files + """ + + id: str + + # A developer-provided per-request id that will be used to match outputs to + # inputs. + custom_id: str + + response: Optional[BatchResponseData] + + # For requests that failed with a non-HTTP error, this will contain more + # information on the cause of the failure. + error: Optional[Any] + + +class TokenizeCompletionRequest(OpenAIBaseModel): + model: str + prompt: str + + add_special_tokens: bool = Field(default=True) + + +class TokenizeChatRequest(OpenAIBaseModel): + model: str + messages: List[ChatCompletionMessageParam] + + add_generation_prompt: bool = Field(default=True) + continue_final_message: bool = Field(default=False) + add_special_tokens: bool = Field(default=False) + chat_template_kwargs: Optional[Dict[str, Any]] = Field(default=None) + + @model_validator(mode="before") + @classmethod + def check_generation_prompt(cls, data): + if data.get("continue_final_message") and data.get( + "add_generation_prompt"): + raise ValueError("Cannot set both `continue_final_message` and " + "`add_generation_prompt` to True.") + return data + + +TokenizeRequest = Union[TokenizeCompletionRequest, TokenizeChatRequest] + + +class TokenizeResponse(OpenAIBaseModel): + count: int + max_model_len: int + tokens: List[int] + + +class DetokenizeRequest(OpenAIBaseModel): + model: str + tokens: List[int] + + +class DetokenizeResponse(OpenAIBaseModel): + prompt: str + + +class LoadLoraAdapterRequest(BaseModel): + lora_name: str + lora_path: str + + +class UnloadLoraAdapterRequest(BaseModel): + lora_name: str + lora_int_id: Optional[int] = Field(default=None) diff --git a/qwen3_6_scripts/qwen3_5.py b/qwen3_6_scripts/qwen3_5.py new file mode 100644 index 0000000..1823934 --- /dev/null +++ b/qwen3_6_scripts/qwen3_5.py @@ -0,0 +1,2615 @@ +# Inference-only Qwen3.6-35B-A3B (Qwen3_5 MoE architecture) for Iluvatar BI-V100. +# Pure-PyTorch DeltaNet (no fla / causal_conv1d dependency). +# Includes the native Qwen3.6 vision tower; MTP remains unsupported. + +from functools import lru_cache, partial +import hashlib +import os +import sys +import time +from typing import (Any, Dict, Iterable, List, Literal, Mapping, Optional, + Tuple, TypedDict, Union) + +def _bi100_model_trace(message: str) -> None: + if os.getenv("BI100_EXECUTOR_STARTUP_DEBUG") == "1": + stamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) + rank = os.getenv("RANK", os.getenv("LOCAL_RANK", "?")) + print(f"[BI100 STARTUP] {stamp} pid={os.getpid()} rank={rank} {message}", + file=sys.stderr, flush=True) + + +_bi100_model_trace("qwen3_5 stdlib imports complete; importing torch and vLLM") + +import torch +import torch.nn.functional as F +from torch import nn +from PIL import Image +from transformers.image_utils import (ChannelDimension, get_image_size, + infer_channel_dimension_format, + to_numpy_array) +from transformers.models.qwen2_vl import ( + image_processing_qwen2_vl as _qwen2_vl_image_processing) +from transformers.models.qwen2_vl.image_processing_qwen2_vl import ( + Qwen2VLImageProcessor, smart_resize) + + +def _compat_make_batched_images(images): + return images if isinstance(images, list) else [images] + + +def _compat_make_batched_videos(videos): + if isinstance(videos, list) and videos and isinstance(videos[0], list): + return videos + return [videos] + + +# The CoreX image pins transformers 4.55.3, while its vLLM Qwen2-VL module +# imports helpers introduced by another transformers build. +if not hasattr(_qwen2_vl_image_processing, "make_batched_images"): + _qwen2_vl_image_processing.make_batched_images = \ + _compat_make_batched_images +if not hasattr(_qwen2_vl_image_processing, "make_batched_videos"): + _qwen2_vl_image_processing.make_batched_videos = \ + _compat_make_batched_videos + +from vllm.attention import Attention, AttentionMetadata +from vllm.config import (CacheConfig, LoRAConfig, MultiModalConfig, + SchedulerConfig) +from vllm.distributed import (get_tensor_model_parallel_rank, + get_tensor_model_parallel_world_size, + tensor_model_parallel_all_reduce) +from vllm.model_executor.layers.activation import SiluAndMul +from vllm.model_executor.layers.layernorm import GemmaRMSNorm +from vllm.model_executor.layers.linear import (ColumnParallelLinear, + MergedColumnParallelLinear, + ReplicatedLinear, + RowParallelLinear) +from vllm.model_executor.layers.fused_moe import FusedMoE +from vllm.model_executor.layers.logits_processor import LogitsProcessor +from vllm.model_executor.layers.quantization import QuantizationConfig +from vllm.model_executor.layers.rotary_embedding import ( + MRotaryEmbedding, _apply_rotary_emb) +from vllm.model_executor.layers.sampler import Sampler, SamplerOutput +from vllm.model_executor.layers.vocab_parallel_embedding import ( + ParallelLMHead, VocabParallelEmbedding) +from vllm.model_executor.model_loader.weight_utils import ( + default_weight_loader, sharded_weight_loader) +from vllm.model_executor.models.mamba_cache import MambaCacheManager +from vllm.model_executor.models.qwen2_vl import (Qwen2VisionAttention, + Qwen2VisionRotaryEmbedding) +from vllm.model_executor.sampling_metadata import SamplingMetadata +from vllm.model_executor.utils import set_weight_attrs +from vllm.inputs import INPUT_REGISTRY, InputContext, LLMInputs +from vllm.multimodal import (MULTIMODAL_REGISTRY, MultiModalDataDict, + MultiModalInputs) +from vllm.multimodal.base import MultiModalData +from vllm.sequence import IntermediateTensors, SequenceData +from vllm.transformers_utils.tokenizer import get_tokenizer +from vllm.worker.model_runner import (_BATCH_SIZES_TO_CAPTURE, + _get_graph_batch_size) +from vllm.logger import init_logger +from vllm.bi100_env import env_bool, env_int +from vllm.bi100_profile import (bi100_profile_event_enabled, + bi100_profile_flush, + bi100_profile_transaction, bi100_timer) + +try: + from vllm import corex_gdn_causal_conv as _corex_gdn_causal_conv +except ImportError: + _corex_gdn_causal_conv = None + +try: + from vllm import corex_gdn_gated_norm as _corex_gdn_gated_norm +except ImportError: + _corex_gdn_gated_norm = None + +try: + from vllm import corex_gdn_beta_decay as _corex_gdn_beta_decay +except ImportError: + _corex_gdn_beta_decay = None + +try: + from vllm import corex_gdn_qk_map as _corex_gdn_qk_map +except ImportError: + _corex_gdn_qk_map = None + +try: + from vllm import corex_gdn_packed_decode as _corex_gdn_packed_decode +except ImportError: + _corex_gdn_packed_decode = None + +try: + from vllm import corex_attn_head_rms_norm as _corex_attn_head_rms_norm +except ImportError: + _corex_attn_head_rms_norm = None + +try: + from vllm import corex_moe_exact_reduce as _corex_moe_exact_reduce +except ImportError: + _corex_moe_exact_reduce = None + +try: + from vllm import corex_moe_weight_gather as _corex_moe_weight_gather +except ImportError: + _corex_moe_weight_gather = None + +try: + from vllm import corex_moe_direct_routed as _corex_moe_direct_routed +except ImportError: + _corex_moe_direct_routed = None + +from vllm.model_executor.models.interfaces import (HasInnerState, SupportsLoRA, + SupportsMultiModal) + +logger = init_logger(__name__) + +_bi100_model_trace("qwen3_5 runtime imports complete") + +_ALLOW_GDN_NAN_ZERO = env_bool("BI100_GDN_ALLOW_NAN_ZERO", False) +_GDN_FINITE_CHECK = (env_bool("BI100_GDN_FINITE_CHECK", False) + or _ALLOW_GDN_NAN_ZERO) +_DNN_CHUNK_SIZE = env_int("BI100_DNN_CHUNK", 4096, 64, 65536) +_USE_COREX_GDN_CAUSAL_CONV = ( + _corex_gdn_causal_conv is not None + and env_bool("BI100_GDN_COREX_CAUSAL_CONV", True)) +_USE_COREX_GDN_GATED_NORM = ( + _corex_gdn_gated_norm is not None + and env_bool("BI100_GDN_COREX_GATED_NORM", True)) +_USE_COREX_GDN_BETA_DECAY = ( + _corex_gdn_beta_decay is not None + and env_bool("BI100_GDN_COREX_BETA_DECAY", True)) +_USE_COREX_GDN_QK_MAP = ( + _corex_gdn_qk_map is not None + and env_bool("BI100_GDN_COREX_QK_MAP", True)) +_USE_COREX_GDN_COMBINED_QK_NORM = ( + _USE_COREX_GDN_QK_MAP + and env_bool("BI100_GDN_COMBINED_QK_NORM", False)) +_USE_COREX_GDN_PACKED_DECODE = ( + _corex_gdn_packed_decode is not None + and env_bool("BI100_GDN_COREX_PACKED_DECODE", False)) +_USE_COREX_ATTN_HEAD_RMS_NORM = ( + _corex_attn_head_rms_norm is not None + and env_bool("BI100_ATTN_COREX_HEAD_RMS_NORM", True)) +_USE_COREX_MOE_EXACT_REDUCE = ( + _corex_moe_exact_reduce is not None + and env_bool("BI100_MOE_COREX_EXACT_REDUCE", True)) +_USE_COREX_MOE_WEIGHT_GATHER = ( + _corex_moe_weight_gather is not None + and env_bool("BI100_MOE_COREX_WEIGHT_GATHER", True)) +_USE_COREX_MOE_DIRECT_ROUTED = ( + _corex_moe_direct_routed is not None + and env_bool("BI100_MOE_COREX_DIRECT_ROUTED", False)) +_USE_FUSED_MOE_ACTIVATION = env_bool("BI100_MOE_FUSED_ACTIVATION", True) + + +# --------------------------------------------------------------------------- +# Qwen3.6 vision tower and vLLM 0.6 multimodal input integration +# --------------------------------------------------------------------------- + +_MAX_IMAGE_TOKENS = 1280 + + +@lru_cache(maxsize=None) +def _cached_get_qwen36_image_processor(model_path: str): + # The fast processor in transformers 4.55 calls torch.compiler APIs that + # are absent from the evaluator's torch 2.1 CoreX build. + return Qwen2VLImageProcessor.from_pretrained(model_path) + + +@lru_cache(maxsize=None) +def _cached_get_qwen36_tokenizer(model_path: str, trust_remote_code: bool): + return get_tokenizer(model_path, trust_remote_code=trust_remote_code) + + +def _image_cache_marker_tokens(image, tokenizer) -> List[int]: + array = to_numpy_array(image) + digest = hashlib.sha256() + digest.update(str(array.shape).encode("ascii")) + digest.update(str(array.dtype).encode("ascii")) + digest.update(array.tobytes()) + marker = f"[image-cache-key:{digest.hexdigest()[:16]}]" + return tokenizer.encode(marker, add_special_tokens=False) + + +def _make_batched_images(images): + if isinstance(images, list): + if images and isinstance(images[0], list): + return [image for batch in images for image in batch] + return images + return [images] + + +class Qwen3_5ImagePixelInputs(TypedDict): + type: Literal["pixel_values"] + data: torch.Tensor + image_grid_thw: torch.Tensor + + +class Qwen3_5ImageEmbeddingInputs(TypedDict): + type: Literal["image_embeds"] + data: torch.Tensor + + +Qwen3_5ImageInputs = Union[Qwen3_5ImagePixelInputs, + Qwen3_5ImageEmbeddingInputs] + + +def _vision_pos_embed_interpolate( + embed_weight: torch.Tensor, + t: int, + h: int, + w: int, + num_grid_per_side: int, + merge_size: int, + dtype: torch.dtype, +) -> torch.Tensor: + if h % merge_size or w % merge_size: + raise ValueError( + f"vision grid {(t, h, w)} is not divisible by merge_size=" + f"{merge_size}") + hidden_dim = embed_weight.shape[1] + device = embed_weight.device + h_idxs = torch.linspace(0, num_grid_per_side - 1, h, + dtype=torch.float32, device=device) + w_idxs = torch.linspace(0, num_grid_per_side - 1, w, + dtype=torch.float32, device=device) + h_floor = h_idxs.long() + w_floor = w_idxs.long() + h_ceil = torch.clamp(h_floor + 1, max=num_grid_per_side - 1) + w_ceil = torch.clamp(w_floor + 1, max=num_grid_per_side - 1) + dh = h_idxs - h_floor + dw = w_idxs - w_floor + dh_grid, dw_grid = torch.meshgrid(dh, dw, indexing="ij") + hf_grid, wf_grid = torch.meshgrid(h_floor, w_floor, indexing="ij") + hc_grid, wc_grid = torch.meshgrid(h_ceil, w_ceil, indexing="ij") + w11 = dh_grid * dw_grid + w10 = dh_grid - w11 + w01 = dw_grid - w11 + w00 = 1 - dh_grid - w01 + h_grid = torch.stack([hf_grid, hf_grid, hc_grid, hc_grid]) + w_grid = torch.stack([wf_grid, wc_grid, wf_grid, wc_grid]) + indices = (h_grid * num_grid_per_side + w_grid).reshape(4, -1) + weights = torch.stack([w00, w01, w10, w11], dim=0) + weights = weights.reshape(4, -1, 1).to(dtype=dtype) + combined = (embed_weight[indices] * weights).sum(dim=0) + combined = combined.reshape( + h // merge_size, merge_size, + w // merge_size, merge_size, hidden_dim) + combined = combined.permute(0, 2, 1, 3, 4).reshape(1, -1, hidden_dim) + return combined.expand(t, -1, -1).reshape(-1, hidden_dim).to(dtype) + + +class Qwen3_5VisionPatchEmbed(nn.Module): + def __init__(self, vision_config) -> None: + super().__init__() + self.patch_size = vision_config.patch_size + self.temporal_patch_size = vision_config.temporal_patch_size + self.hidden_size = vision_config.hidden_size + kernel = (self.temporal_patch_size, self.patch_size, self.patch_size) + self.proj = nn.Conv3d( + vision_config.in_channels, + self.hidden_size, + kernel_size=kernel, + stride=kernel, + bias=True, + ) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + length = x.shape[0] + x = x.view(length, -1, self.temporal_patch_size, + self.patch_size, self.patch_size) + return self.proj(x).view(length, self.hidden_size) + + +class Qwen3_5VisionMLP(nn.Module): + def __init__(self, vision_config, + quant_config: Optional[QuantizationConfig] = None) -> None: + super().__init__() + self.linear_fc1 = ColumnParallelLinear( + vision_config.hidden_size, + vision_config.intermediate_size, + bias=True, + quant_config=quant_config, + ) + self.linear_fc2 = RowParallelLinear( + vision_config.intermediate_size, + vision_config.hidden_size, + bias=True, + quant_config=quant_config, + ) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + x, _ = self.linear_fc1(x) + x = F.gelu(x, approximate="tanh") + x, _ = self.linear_fc2(x) + return x + + +class Qwen3_5VisionBlock(nn.Module): + def __init__(self, vision_config, + quant_config: Optional[QuantizationConfig] = None) -> None: + super().__init__() + dim = vision_config.hidden_size + self.norm1 = nn.LayerNorm(dim, eps=1e-6) + self.norm2 = nn.LayerNorm(dim, eps=1e-6) + self.attn = Qwen2VisionAttention( + embed_dim=dim, + num_heads=vision_config.num_heads, + projection_size=dim, + quant_config=quant_config, + ) + self.mlp = Qwen3_5VisionMLP(vision_config, quant_config) + + def forward(self, x: torch.Tensor, cu_seqlens: torch.Tensor, + rotary_pos_emb: torch.Tensor) -> torch.Tensor: + x = x + self.attn( + self.norm1(x), + cu_seqlens=cu_seqlens, + rotary_pos_emb=rotary_pos_emb, + ) + return x + self.mlp(self.norm2(x)) + + +class Qwen3_5VisionPatchMerger(nn.Module): + def __init__(self, vision_config, + quant_config: Optional[QuantizationConfig] = None) -> None: + super().__init__() + self.hidden_size = (vision_config.hidden_size + * vision_config.spatial_merge_size ** 2) + self.norm = nn.LayerNorm(vision_config.hidden_size, eps=1e-6) + self.linear_fc1 = ColumnParallelLinear( + self.hidden_size, + self.hidden_size, + bias=True, + quant_config=quant_config, + ) + self.linear_fc2 = RowParallelLinear( + self.hidden_size, + vision_config.out_hidden_size, + bias=True, + quant_config=quant_config, + ) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + x = self.norm(x).view(-1, self.hidden_size) + x, _ = self.linear_fc1(x) + x = F.gelu(x) + x, _ = self.linear_fc2(x) + return x + + +class Qwen3_5VisionTransformer(nn.Module): + def __init__(self, vision_config, + quant_config: Optional[QuantizationConfig] = None) -> None: + super().__init__() + self.hidden_size = vision_config.hidden_size + self.num_heads = vision_config.num_heads + self.spatial_merge_size = vision_config.spatial_merge_size + self.num_grid_per_side = int(vision_config.num_position_embeddings ** .5) + self.patch_embed = Qwen3_5VisionPatchEmbed(vision_config) + self.pos_embed = nn.Embedding( + vision_config.num_position_embeddings, self.hidden_size) + head_dim = self.hidden_size // self.num_heads + self.rotary_pos_emb = Qwen2VisionRotaryEmbedding(head_dim // 2) + self.blocks = nn.ModuleList([ + Qwen3_5VisionBlock(vision_config, quant_config) + for _ in range(vision_config.depth) + ]) + self.merger = Qwen3_5VisionPatchMerger(vision_config, quant_config) + + @property + def dtype(self) -> torch.dtype: + return self.patch_embed.proj.weight.dtype + + @property + def device(self) -> torch.device: + return self.patch_embed.proj.weight.device + + def _rot_pos_emb(self, grid_thw: torch.Tensor) -> torch.Tensor: + pos_ids = [] + for t, h, w in grid_thw.tolist(): + h_ids = torch.arange(h).unsqueeze(1).expand(-1, w) + w_ids = torch.arange(w).unsqueeze(0).expand(h, -1) + h_ids = h_ids.reshape( + h // self.spatial_merge_size, self.spatial_merge_size, + w // self.spatial_merge_size, self.spatial_merge_size, + ).permute(0, 2, 1, 3).flatten() + w_ids = w_ids.reshape( + h // self.spatial_merge_size, self.spatial_merge_size, + w // self.spatial_merge_size, self.spatial_merge_size, + ).permute(0, 2, 1, 3).flatten() + pos_ids.append(torch.stack([h_ids, w_ids], dim=-1).repeat(t, 1)) + pos_ids_t = torch.cat(pos_ids, dim=0).to(self.device) + max_grid_size = int(grid_thw[:, 1:].max().item()) + return self.rotary_pos_emb(max_grid_size)[pos_ids_t].flatten(1) + + def _absolute_pos_emb(self, grid_thw: torch.Tensor) -> torch.Tensor: + return torch.cat([ + _vision_pos_embed_interpolate( + self.pos_embed.weight, int(t), int(h), int(w), + self.num_grid_per_side, self.spatial_merge_size, self.dtype) + for t, h, w in grid_thw.tolist() + ], dim=0) + + def forward(self, x: torch.Tensor, grid_thw: torch.Tensor) -> torch.Tensor: + x = x.to(device=self.device, dtype=self.dtype) + grid_thw = grid_thw.to(device=self.device) + x = self.patch_embed(x) + x = x + self._absolute_pos_emb(grid_thw) + rotary_pos_emb = self._rot_pos_emb(grid_thw) + cu_seqlens = torch.repeat_interleave( + grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0], + ).cumsum(dim=0, dtype=torch.int32) + cu_seqlens = F.pad(cu_seqlens, (1, 0), "constant", 0) + x = x.unsqueeze(1) + for block in self.blocks: + x = block(x, cu_seqlens, rotary_pos_emb) + return self.merger(x) + + +class Qwen3_5InterleavedMRotaryEmbedding(MRotaryEmbedding): + """Qwen3.5 frequency-interleaved T/H/W rotary embedding.""" + + def forward(self, positions: torch.Tensor, query: torch.Tensor, + key: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + if positions.ndim not in (1, 2): + raise ValueError(f"invalid MRoPE positions shape {positions.shape}") + num_tokens = positions.shape[-1] + cos_sin = self.cos_sin_cache[positions] + cos_all, sin_all = cos_sin.chunk(2, dim=-1) + if positions.ndim == 2: + if not self.mrope_section: + raise ValueError("mrope_section is required") + cos = cos_all[0].clone() + sin = sin_all[0].clone() + for dim, offset in enumerate((1, 2), start=1): + stop = self.mrope_section[dim] * 3 + cos[..., offset:stop:3] = cos_all[dim, ..., offset:stop:3] + sin[..., offset:stop:3] = sin_all[dim, ..., offset:stop:3] + else: + cos, sin = cos_all, sin_all + + query_shape = query.shape + query = query.view(num_tokens, -1, self.head_size) + query_rot = _apply_rotary_emb( + query[..., :self.rotary_dim], cos, sin, self.is_neox_style) + query = torch.cat((query_rot, query[..., self.rotary_dim:]), dim=-1) + + key_shape = key.shape + key = key.view(num_tokens, -1, self.head_size) + key_rot = _apply_rotary_emb( + key[..., :self.rotary_dim], cos, sin, self.is_neox_style) + key = torch.cat((key_rot, key[..., self.rotary_dim:]), dim=-1) + return query.reshape(query_shape), key.reshape(key_shape) + + +def _qwen36_pixel_limits(image_processor) -> Tuple[int, int]: + min_pixels = 256 * 256 + configured_max = 4096 * 4096 + runtime_max = _MAX_IMAGE_TOKENS * ( + image_processor.patch_size * image_processor.merge_size) ** 2 + return min_pixels, min(configured_max, runtime_max) + + +def _qwen36_image_token_count(image, image_processor) -> int: + if isinstance(image, Image.Image): + image = image.convert("RGB") + image_array = to_numpy_array(image) + height, width = get_image_size( + image_array, channel_dim=ChannelDimension.LAST) + min_pixels, max_pixels = _qwen36_pixel_limits(image_processor) + if getattr(image_processor, "do_resize", True): + height, width = smart_resize( + height=height, + width=width, + factor=image_processor.patch_size * image_processor.merge_size, + min_pixels=min_pixels, + max_pixels=max_pixels, + ) + return (height // image_processor.patch_size + * width // image_processor.patch_size + // image_processor.merge_size ** 2) + + +def qwen36_image_input_mapper( + ctx: InputContext, + data: MultiModalData[object], +) -> MultiModalInputs: + if isinstance(data, dict): + return MultiModalInputs({ + "image_embeds": data.get("image_embeds"), + "image_grid_thw": data.get("image_grid_thw"), + }) + image_processor = _cached_get_qwen36_image_processor( + ctx.model_config.model) + min_pixels, max_pixels = _qwen36_pixel_limits(image_processor) + batch_data = image_processor.preprocess( + images=data, + return_tensors="pt", + size={"shortest_edge": min_pixels, "longest_edge": max_pixels}, + do_convert_rgb=True, + input_data_format=ChannelDimension.LAST, + ).data + return MultiModalInputs(batch_data) + + +def get_max_qwen36_image_tokens(_ctx: InputContext) -> int: + return _MAX_IMAGE_TOKENS + + +def dummy_data_for_qwen36( + ctx: InputContext, + seq_len: int, + mm_counts: Mapping[str, int], +) -> Tuple[SequenceData, Optional[MultiModalDataDict]]: + num_images = mm_counts.get("image", 0) + image_tokens = _MAX_IMAGE_TOKENS * num_images + if seq_len < image_tokens + 2: + raise RuntimeError( + f"Qwen3.6 needs {image_tokens + 2} tokens for {num_images} " + f"max-size image(s), but max_model_len is {seq_len}") + config = ctx.model_config.hf_config + seq_data = SequenceData.from_token_counts( + (config.vision_start_token_id, 1), + (config.image_token_id, image_tokens), + (config.vision_end_token_id, 1), + (0, seq_len - image_tokens - 2), + ) + dummy_image = Image.new("RGB", (1280, 1024), color=0) + return seq_data, { + "image": (dummy_image if num_images == 1 + else [dummy_image] * num_images) + } + + +def input_processor_for_qwen36(ctx: InputContext, + llm_inputs: LLMInputs) -> LLMInputs: + multi_modal_data = llm_inputs.get("multi_modal_data") + if not multi_modal_data or "image" not in multi_modal_data: + return llm_inputs + images = multi_modal_data["image"] + prompt_token_ids = llm_inputs.get("prompt_token_ids") + if prompt_token_ids is None: + raise ValueError("Qwen3.6 image requests require tokenized prompt input") + config = ctx.model_config.hf_config + image_processor = _cached_get_qwen36_image_processor( + ctx.model_config.model) + tokenizer = _cached_get_qwen36_tokenizer( + ctx.model_config.tokenizer, + ctx.model_config.trust_remote_code, + ) + batched_images = _make_batched_images(images) + image_indices = [ + idx for idx, token in enumerate(prompt_token_ids) + if token == config.image_token_id + ] + if len(image_indices) != len(batched_images): + raise ValueError( + f"found {len(image_indices)} image placeholders for " + f"{len(batched_images)} image(s)") + expanded = [] + previous = 0 + for index, image in zip(image_indices, batched_images): + vision_start = index - 1 + if (vision_start < previous + or prompt_token_ids[vision_start] + != config.vision_start_token_id): + raise ValueError("image token is not preceded by vision_start") + expanded.extend(prompt_token_ids[previous:vision_start]) + expanded.extend(_image_cache_marker_tokens(image, tokenizer)) + expanded.extend(prompt_token_ids[vision_start:index]) + expanded.extend([config.image_token_id] + * _qwen36_image_token_count(image, image_processor)) + previous = index + 1 + expanded.extend(prompt_token_ids[previous:]) + return LLMInputs( + prompt_token_ids=expanded, + prompt=llm_inputs["prompt"], + multi_modal_data=multi_modal_data, + ) + + +# --------------------------------------------------------------------------- +# Pure-PyTorch DeltaNet kernels (fallbacks from transformers 5.2.0) +# --------------------------------------------------------------------------- + +def _l2norm(x: torch.Tensor, dim: int = -1, eps: float = 1e-6) -> torch.Tensor: + return x * torch.rsqrt((x * x).sum(dim=dim, keepdim=True) + eps) + + +def _check_gdn_finite(tensor: torch.Tensor, *, layer_idx: int, + stage: str) -> torch.Tensor: + if not _GDN_FINITE_CHECK: + return tensor + if torch.isfinite(tensor).all(): + return tensor + bad = (~torch.isfinite(tensor)).float().mean().item() + msg = ( + f"non-finite values in {stage} GatedDeltaNet layer {layer_idx} " + f"(frac={bad:.4f})" + ) + if not _ALLOW_GDN_NAN_ZERO: + raise RuntimeError(msg) + logger.warning("%s; replacing with zeros because BI100_GDN_ALLOW_NAN_ZERO=1", + msg) + return torch.nan_to_num(tensor, nan=0.0, posinf=0.0, neginf=0.0) + + +def _gdn_segment_ends(seq_len: int, chunk_size: int, + capture_offsets: Iterable[int]) -> List[int]: + ends = list(range(chunk_size, seq_len, chunk_size)) + ends.append(seq_len) + ends.extend(offset for offset in capture_offsets + if 0 < offset < seq_len) + return sorted(set(ends)) + + +def _validate_gdn_prefix_key(key: Any) -> Tuple[int, bytes]: + if (not isinstance(key, tuple) or len(key) != 2 + or not isinstance(key[0], int) or key[0] <= 0 + or not isinstance(key[1], bytes) or len(key[1]) != 32): + raise RuntimeError(f"invalid GDN prefix key: {key!r}") + return key + + +def _torch_causal_conv1d_update( + hidden_states: torch.Tensor, # (batch, channels, seq=1) + conv_state: torch.Tensor, # (batch, channels, state_len) modified in-place + weight: torch.Tensor, # (channels, kernel_size) + bias: Optional[torch.Tensor] = None, + activation: Optional[str] = None, +) -> torch.Tensor: + _, channels, seq_len = hidden_states.shape + state_len = conv_state.shape[-1] + cat = torch.cat([conv_state, hidden_states], dim=-1).to(weight.dtype) + conv_state.copy_(cat[:, :, -state_len:]) + out = F.conv1d(cat, weight.unsqueeze(1), bias, padding=0, groups=channels) + out = out[:, :, -seq_len:] + if activation is not None: + out = F.silu(out) + return out.to(hidden_states.dtype) + + +def _torch_chunk_gated_delta_rule( + query: torch.Tensor, # (batch, seq, num_heads, head_k_dim) + key: torch.Tensor, + value: torch.Tensor, # (batch, seq, num_heads, head_v_dim) + g: torch.Tensor, # (batch, seq, num_heads) + beta: torch.Tensor, # (batch, seq, num_heads) + chunk_size: int = 64, + initial_state: Optional[torch.Tensor] = None, + output_final_state: bool = False, + use_qk_l2norm_in_kernel: bool = False, +) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + if use_qk_l2norm_in_kernel: + query = _l2norm(query) + key = _l2norm(key) + # Transpose to (batch, num_heads, seq, dim) + query, key, value, beta, g = [ + x.transpose(1, 2).contiguous().to(torch.float32) + for x in (query, key, value, beta, g) + ] + batch, num_heads, seq_len, k_dim = key.shape + v_dim = value.shape[-1] + pad = (chunk_size - seq_len % chunk_size) % chunk_size + query = F.pad(query, (0, 0, 0, pad)) + key = F.pad(key, (0, 0, 0, pad)) + value = F.pad(value, (0, 0, 0, pad)) + beta = F.pad(beta, (0, pad)) + g = F.pad(g, (0, pad)) + total_len = seq_len + pad + scale = 1.0 / (query.shape[-1] ** 0.5) + query = query * scale + + v_beta = value * beta.unsqueeze(-1) + k_beta = key * beta.unsqueeze(-1) + query, key, value, k_beta, v_beta = [ + x.reshape(x.shape[0], x.shape[1], -1, chunk_size, x.shape[-1]) + for x in (query, key, value, k_beta, v_beta) + ] + g = g.reshape(g.shape[0], g.shape[1], -1, chunk_size) + mask_upper = torch.triu( + torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), + diagonal=0) + + g = g.cumsum(dim=-1) + decay_mask = ((g.unsqueeze(-1) - g.unsqueeze(-2)).tril().exp().float()).tril() + attn = -((k_beta @ key.transpose(-1, -2)) * decay_mask).masked_fill(mask_upper, 0) + for i in range(1, chunk_size): + row = attn[..., i, :i].clone() + sub = attn[..., :i, :i].clone() + attn[..., i, :i] = row + (row.unsqueeze(-1) * sub).sum(-2) + attn = attn + torch.eye(chunk_size, dtype=attn.dtype, device=attn.device) + value = attn @ v_beta + k_cumdecay = attn @ (k_beta * g.exp().unsqueeze(-1)) + + last_state = ( + torch.zeros(batch, num_heads, k_dim, v_dim, dtype=value.dtype, device=value.device) + if initial_state is None + else initial_state.to(value) + ) + core_out = torch.zeros_like(value) + mask_upper2 = torch.triu( + torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), + diagonal=1) + + for i in range(total_len // chunk_size): + q_i, k_i, v_i = query[:, :, i], key[:, :, i], value[:, :, i] + attn_i = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill_(mask_upper2, 0) + v_prime = k_cumdecay[:, :, i] @ last_state + v_new = v_i - v_prime + attn_inter = (q_i * g[:, :, i, :, None].exp()) @ last_state + core_out[:, :, i] = attn_inter + attn_i @ v_new + last_state = ( + last_state * g[:, :, i, -1, None, None].exp() + + (k_i * (g[:, :, i, -1, None] - g[:, :, i]).exp()[..., None]) + .transpose(-1, -2) @ v_new + ) + + if not output_final_state: + last_state = None + core_out = core_out.reshape(batch, num_heads, -1, v_dim)[:, :, :seq_len] + core_out = core_out.transpose(1, 2).contiguous() + return core_out, last_state + +def _torch_recurrent_gated_delta_rule( + query: torch.Tensor, # (batch, 1, num_heads, head_k_dim) + key: torch.Tensor, + value: torch.Tensor, + g: torch.Tensor, # (batch, 1, num_heads) + beta: torch.Tensor, + initial_state: Optional[torch.Tensor] = None, + output_final_state: bool = False, + use_qk_l2norm_in_kernel: bool = False, +) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + if use_qk_l2norm_in_kernel: + query = _l2norm(query) + key = _l2norm(key) + query, key, value, beta, g = [ + x.transpose(1, 2).contiguous().to(torch.float32) + for x in (query, key, value, beta, g) + ] + batch, num_heads, seq_len, k_dim = key.shape + v_dim = value.shape[-1] + scale = 1.0 / (query.shape[-1] ** 0.5) + query = query * scale + + core_out = torch.zeros(batch, num_heads, seq_len, v_dim, + dtype=value.dtype, device=value.device) + last_state = ( + torch.zeros(batch, num_heads, k_dim, v_dim, + dtype=value.dtype, device=value.device) + if initial_state is None + else initial_state.to(value) + ) + for t in range(seq_len): + q_t = query[:, :, t] + k_t = key[:, :, t] + v_t = value[:, :, t] + g_t = g[:, :, t].exp().unsqueeze(-1).unsqueeze(-1) + beta_t = beta[:, :, t].unsqueeze(-1) + last_state = last_state * g_t + kv_mem = (last_state * k_t.unsqueeze(-1)).sum(dim=-2) + delta = (v_t - kv_mem) * beta_t + last_state = last_state + k_t.unsqueeze(-1) * delta.unsqueeze(-2) + core_out[:, :, t] = (last_state * q_t.unsqueeze(-1)).sum(dim=-2) + + if not output_final_state: + last_state = None + core_out = core_out.transpose(1, 2).contiguous() + return core_out, last_state + + +# --------------------------------------------------------------------------- +# Gated RMSNorm (for DeltaNet output normalisation) +# --------------------------------------------------------------------------- + +class Qwen3_5RMSNormGated(nn.Module): + def __init__(self, hidden_size: int, eps: float = 1e-6): + super().__init__() + self.weight = nn.Parameter(torch.ones(hidden_size)) + self.variance_epsilon = eps + + def forward(self, hidden_states: torch.Tensor, + gate: torch.Tensor) -> torch.Tensor: + input_dtype = hidden_states.dtype + hs = hidden_states.to(torch.float32) + variance = hs.pow(2).mean(-1, keepdim=True) + hs = hs * torch.rsqrt(variance + self.variance_epsilon) + hs = self.weight * hs.to(input_dtype) + return (hs * F.silu(gate.to(torch.float32))).to(input_dtype) + + def forward_decode(self, hidden_states: torch.Tensor, + gate: torch.Tensor) -> torch.Tensor: + if (_USE_COREX_GDN_GATED_NORM + and hidden_states.dtype == torch.float32 + and gate.dtype == torch.float16 + and self.weight.dtype == torch.float16 + and hidden_states.shape[-1] == 128): + hs = hidden_states.float() + inverse = torch.rsqrt( + hs.pow(2).mean(-1, keepdim=True) + self.variance_epsilon) + return _corex_gdn_gated_norm.apply_inverse( + hs, gate, self.weight, inverse) + return self.forward(hidden_states, gate).to(gate.dtype) + + +def _load_gdn_projection_weight(params_dict, name: str, + loaded_weight: torch.Tensor, + text_cfg) -> bool: + projections = { + "in_proj_qkv": None, + "in_proj_z": 3, + "in_proj_b": 4, + "in_proj_a": 5, + } + source = next((projection for projection in projections + if f".linear_attn.{projection}." in name), None) + if source is None: + return False + + target_name = name.replace( + f".linear_attn.{source}.", + ".linear_attn.in_proj_qkvzba.", + ) + if target_name not in params_dict: + raise ValueError(f"missing fused GDN projection parameter: {target_name}") + param = params_dict[target_name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + + if source == "in_proj_qkv": + key_dim = (text_cfg.linear_num_key_heads + * text_cfg.linear_key_head_dim) + value_dim = (text_cfg.linear_num_value_heads + * text_cfg.linear_value_head_dim) + shard_sizes = (key_dim, key_dim, value_dim) + if loaded_weight.shape[0] != sum(shard_sizes): + raise ValueError( + "unexpected fused QKV output size: " + f"{loaded_weight.shape[0]} != {sum(shard_sizes)}") + for shard_id, shard in enumerate( + torch.split(loaded_weight, shard_sizes, dim=0)): + weight_loader(param, shard, shard_id) + else: + weight_loader(param, loaded_weight, projections[source]) + return True + + +def _load_full_attention_qgkv_weight(params_dict, name: str, + loaded_weight: torch.Tensor, + text_cfg) -> bool: + projections = {"q_proj": 0, "k_proj": 1, "v_proj": 2} + source = next((projection for projection in projections + if f".self_attn.{projection}." in name), None) + if source is None: + return False + target_name = name.replace( + f".self_attn.{source}.", ".self_attn.qgkv_proj.") + if target_name not in params_dict: + return False + + tp_size = get_tensor_model_parallel_world_size() + tp_rank = get_tensor_model_parallel_rank() + qg_dim = text_cfg.num_attention_heads * text_cfg.head_dim * 2 + if qg_dim % tp_size != 0: + raise ValueError(f"QG output size {qg_dim} is not divisible by TP {tp_size}") + local_qg_dim = qg_dim // tp_size + kv_dim = text_cfg.num_key_value_heads * text_cfg.head_dim + expected_rows = qg_dim if source == "q_proj" else kv_dim + if loaded_weight.shape[0] != expected_rows: + raise ValueError( + f"unexpected full-attention {source} output size: " + f"{loaded_weight.shape[0]} != {expected_rows}") + + if source == "q_proj": + loaded_weight = loaded_weight.narrow( + 0, tp_rank * local_qg_dim, local_qg_dim) + offset = 0 + elif source == "k_proj": + offset = local_qg_dim + else: + offset = local_qg_dim + kv_dim + param = params_dict[target_name] + default_weight_loader( + param[offset:offset + loaded_weight.shape[0]], loaded_weight) + return True + + +# --------------------------------------------------------------------------- +# Gated DeltaNet (linear_attention layers) +# --------------------------------------------------------------------------- + +class GatedDeltaNet(nn.Module): + def __init__( + self, + text_cfg, + layer_idx: int, + quant_config: Optional[QuantizationConfig] = None, + ) -> None: + super().__init__() + self.layer_idx = layer_idx + self.hidden_size = text_cfg.hidden_size + self.num_v_heads = text_cfg.linear_num_value_heads # checkpoint: 32 + self.num_k_heads = text_cfg.linear_num_key_heads # checkpoint: 16 + self.head_k_dim = text_cfg.linear_key_head_dim # 128 + self.head_v_dim = text_cfg.linear_value_head_dim # 128 + self.key_dim = self.num_k_heads * self.head_k_dim # 2048 + self.value_dim = self.num_v_heads * self.head_v_dim # checkpoint: 4096 + self.conv_dim = self.key_dim * 2 + self.value_dim # checkpoint: 8192 + self.conv_kernel_size = text_cfg.linear_conv_kernel_dim # 4 + self.head_expand_ratio = self.num_v_heads // self.num_k_heads # checkpoint: 2 + + tp_size = get_tensor_model_parallel_world_size() + + # Keep each logical projection independently TP-sharded while executing + # one GEMM. Per-rank output order is [q, k, v, z, beta, decay]. + self.in_proj_qkvzba = MergedColumnParallelLinear( + self.hidden_size, + [self.key_dim, self.key_dim, self.value_dim, self.value_dim, + self.num_v_heads, self.num_v_heads], + bias=False, quant_config=quant_config) + self.out_proj = RowParallelLinear( + self.value_dim, self.hidden_size, + bias=False, quant_config=quant_config) + + # Depthwise conv weight — sharded along channel dim (dim 0) + local_conv_dim = self.conv_dim // tp_size + self.conv1d_weight = nn.Parameter( + torch.empty(local_conv_dim, 1, self.conv_kernel_size)) + set_weight_attrs(self.conv1d_weight, { + "weight_loader": self._conv1d_weight_loader}) + + # Per-head scalar parameters — sharded along dim 0 + local_num_v = self.num_v_heads // tp_size + self.A_log = nn.Parameter(torch.zeros(local_num_v)) + self.dt_bias = nn.Parameter(torch.zeros(local_num_v)) + set_weight_attrs(self.A_log, {"weight_loader": sharded_weight_loader(0)}) + set_weight_attrs(self.dt_bias, {"weight_loader": sharded_weight_loader(0)}) + + # Gated RMSNorm on head_v_dim — replicated (head_v_dim=128 is small) + self.norm = Qwen3_5RMSNormGated(self.head_v_dim, + eps=text_cfg.rms_norm_eps) + self.captured_conv_states: Dict[int, torch.Tensor] = {} + self.captured_temporal_states: Dict[int, torch.Tensor] = {} + + def _conv1d_weight_loader(self, param: torch.Tensor, + loaded_weight: torch.Tensor) -> None: + # loaded_weight is ordered as [q, k, v] along its channel dimension. + # Must gather channels in the same non-contiguous pattern that + # MergedColumnParallelLinear uses for in_proj_qkv, so that each rank's + # conv1d_weight[i] applies to the correct in_proj_qkv output channel. + tp_rank = get_tensor_model_parallel_rank() + tp_size = get_tensor_model_parallel_world_size() + key_local = self.key_dim // tp_size # 512 with TP=4 + val_local = self.value_dim // tp_size # 1024 with TP=4 + q_s = loaded_weight[tp_rank * key_local : (tp_rank + 1) * key_local] + k_s = loaded_weight[self.key_dim + tp_rank * key_local : + self.key_dim + (tp_rank + 1) * key_local] + v_s = loaded_weight[2 * self.key_dim + tp_rank * val_local : + 2 * self.key_dim + (tp_rank + 1) * val_local] + param.data.copy_(torch.cat([q_s, k_s, v_s], dim=0)) + + def forward( + self, + hidden_states: torch.Tensor, # (total_tokens, hidden_size) + attn_metadata: AttentionMetadata, + conv_state: torch.Tensor, # (batch, local_conv_dim, kernel-1) in-place + temporal_state: torch.Tensor, # (batch, local_v_heads, k_dim, v_dim) in-place + capture_offsets: Optional[Iterable[int]] = None, + segment_offsets: Optional[Iterable[int]] = None, + ) -> torch.Tensor: + tp_size = get_tensor_model_parallel_world_size() + local_key_dim = self.key_dim // tp_size + local_val_dim = self.value_dim // tp_size + local_num_v = self.num_v_heads // tp_size + local_num_k = self.num_k_heads // tp_size + local_conv_dim = self.conv_dim // tp_size + self.captured_conv_states = {} + self.captured_temporal_states = {} + + is_prefill = attn_metadata.num_prefill_tokens > 0 + + projected, _ = self.in_proj_qkvzba(hidden_states) + mixed_qkv_all, z_all, b_all, a_all = torch.split( + projected, + [local_conv_dim, local_val_dim, local_num_v, local_num_v], + dim=-1, + ) + + if is_prefill: + seq_starts = attn_metadata.query_start_loc.tolist() + outputs = [] + state_len = self.conv_kernel_size - 1 + weight_2d = self.conv1d_weight.squeeze(1) # (local_conv_dim, kernel) + + for si in range(len(seq_starts) - 1): + s, e = int(seq_starts[si]), int(seq_starts[si + 1]) + seq_len = e - s + + # Shape: (1, local_conv_dim, seq_len) + mixed_qkv = (mixed_qkv_all[s:e] + .transpose(0, 1).unsqueeze(0) + .to(weight_2d.dtype)) + + # Load prev conv state BEFORE overwriting (needed for causal conv padding). + # For first prefill of a request: mamba_cache is zeros → correct. + # For chunked prefill chunk 2+: carries last state_len tokens from prev chunk. + prev_conv = conv_state[si:si + 1].clone().to(weight_2d.dtype) # [1, local_conv_dim, state_len] + + # Save conv state (last state_len positions) + if seq_len >= state_len: + conv_state[si].copy_(mixed_qkv[0, :, -state_len:]) + else: + conv_state[si, :, state_len - seq_len:].copy_( + mixed_qkv[0]) + conv_state[si, :, :state_len - seq_len] = 0 + + # Causal conv: left-pad with previous conv state (not zeros). + padded = torch.cat([prev_conv, mixed_qkv], dim=2) + seq_capture_offsets = (set(capture_offsets or ()) + if si == 0 else set()) + seq_segment_offsets = (set(segment_offsets or ()) + if si == 0 else set()) + for capture_offset in seq_capture_offsets: + if 0 < capture_offset < seq_len: + self.captured_conv_states[capture_offset] = padded[ + 0, :, capture_offset: + capture_offset + state_len].clone() + mixed_qkv_conv = F.conv1d( + padded, self.conv1d_weight, + bias=None, padding=0, groups=local_conv_dim) + mixed_qkv_conv = F.silu(mixed_qkv_conv) + # (1, seq_len, local_conv_dim) + mixed_qkv_conv = mixed_qkv_conv.squeeze(0).transpose(0, 1).unsqueeze(0) + + q, k, v = torch.split( + mixed_qkv_conv, + [local_key_dim, local_key_dim, local_val_dim], dim=-1) + q = q.reshape(1, seq_len, local_num_k, self.head_k_dim) + k = k.reshape(1, seq_len, local_num_k, self.head_k_dim) + v = v.reshape(1, seq_len, local_num_v, self.head_v_dim) + + beta = b_all[s:e].sigmoid().unsqueeze(0) # (1, seq_len, local_num_v) + g = (-self.A_log.float().exp() + * F.softplus(a_all[s:e].float() + self.dt_bias) + ).unsqueeze(0) # (1, seq_len, local_num_v) + + # Expand k/q to match num_v_heads + q = q.repeat_interleave(self.head_expand_ratio, dim=2) + k = k.repeat_interleave(self.head_expand_ratio, dim=2) + + # Sub-sequence chunking: call _torch_chunk_gated_delta_rule + # on _DNN_CHUNK tokens at a time to cap peak memory. + # Full 18K: tensors [1,6,282,64,64]=220 MB each → ~990 MB/call. + # With _DNN_CHUNK=4096: [1,6,64,64,64]=6 MB each → ~137 MB/call. + # State is chained via initial_state / output_final_state. + cur_state = temporal_state[si:si + 1].clone() + core_out_parts = [] + segment_ends = _gdn_segment_ends( + seq_len, _DNN_CHUNK_SIZE, + seq_capture_offsets | seq_segment_offsets) + sc_start = 0 + with bi100_timer(f"L{self.layer_idx}.gdn.prefill"): + for sc_end in segment_ends: + c_out, cur_state = _torch_chunk_gated_delta_rule( + q[:, sc_start:sc_end], + k[:, sc_start:sc_end], + v[:, sc_start:sc_end], + g[:, sc_start:sc_end], + beta[:, sc_start:sc_end], + initial_state=cur_state, + output_final_state=True, + use_qk_l2norm_in_kernel=True, + ) + core_out_parts.append(c_out) + if sc_end in seq_capture_offsets: + self.captured_temporal_states[sc_end] = ( + cur_state[0].clone()) + sc_start = sc_end + if cur_state is not None: + temporal_state[si].copy_(cur_state[0]) + # [1, seq_len, num_v_heads, head_v_dim] + core_out = torch.cat(core_out_parts, dim=1) + + # Gate + norm + output proj + z = z_all[s:e].reshape(seq_len, local_num_v, self.head_v_dim) + core_out = core_out.reshape(seq_len, local_num_v, self.head_v_dim) + normed = self.norm( + core_out.reshape(-1, self.head_v_dim), + z.reshape(-1, self.head_v_dim)) + normed = _check_gdn_finite( + normed, layer_idx=self.layer_idx, + stage="prefill-norm").reshape(seq_len, -1) + normed = normed.to(z_all.dtype) + out, _ = self.out_proj(normed) + outputs.append(out) + + result = torch.cat(outputs, dim=0) + return _check_gdn_finite( + result, layer_idx=self.layer_idx, stage="prefill-output") + + else: + # Decode: one token per sequence + num_seqs = hidden_states.shape[0] + weight_2d = self.conv1d_weight.squeeze(1) + + # (num_seqs, local_conv_dim, 1) + mixed_qkv = (mixed_qkv_all + .to(weight_2d.dtype) + .unsqueeze(-1)) + + if _USE_COREX_GDN_CAUSAL_CONV: + mixed_qkv_conv = _corex_gdn_causal_conv.causal_conv_update( + conv_state, mixed_qkv, weight_2d) + else: + mixed_qkv_conv = _torch_causal_conv1d_update( + mixed_qkv, conv_state, weight_2d, + bias=None, activation='silu') + # (num_seqs, local_conv_dim, 1) → (num_seqs, 1, local_conv_dim) + mixed_qkv_conv = mixed_qkv_conv.squeeze(-1).unsqueeze(1) + + packed_mixed_qkv = mixed_qkv_conv.squeeze(1) + use_corex_packed_decode = ( + _USE_COREX_GDN_PACKED_DECODE + and num_seqs == 1 + and local_num_k == 4 + and local_num_v == 8 + and self.head_k_dim == 128 + and self.head_v_dim == 128 + and packed_mixed_qkv.dtype == torch.float16 + and packed_mixed_qkv.shape == (1, 2048) + and packed_mixed_qkv.is_contiguous() + and b_all.dtype == torch.float16 + and b_all.shape == (1, 8) + and b_all.is_contiguous() + and a_all.dtype == torch.float16 + and a_all.shape == (1, 8) + and a_all.is_contiguous() + and self.A_log.dtype == torch.float16 + and self.A_log.shape == (8,) + and self.A_log.is_contiguous() + and self.dt_bias.dtype == torch.float16 + and self.dt_bias.shape == (8,) + and self.dt_bias.is_contiguous() + and temporal_state.dtype == torch.float32 + and temporal_state.shape == (1, 8, 128, 128) + and temporal_state.is_contiguous()) + if use_corex_packed_decode: + with bi100_timer(f"L{self.layer_idx}.gdn.decode"): + core_out = _corex_gdn_packed_decode.packed_decode( + temporal_state, packed_mixed_qkv, b_all, a_all, + self.A_log, self.dt_bias) + else: + q, k, v = torch.split( + mixed_qkv_conv, + [local_key_dim, local_key_dim, local_val_dim], dim=-1) + q = q.reshape(num_seqs, 1, local_num_k, self.head_k_dim) + k = k.reshape(num_seqs, 1, local_num_k, self.head_k_dim) + v = v.reshape(num_seqs, 1, local_num_v, self.head_v_dim) + + use_corex_beta_decay = ( + _USE_COREX_GDN_BETA_DECAY + and b_all.dtype == torch.float16 + and a_all.dtype == torch.float16 + and self.A_log.dtype == torch.float16 + and self.dt_bias.dtype == torch.float16 + and b_all.is_contiguous() + and a_all.is_contiguous()) + if use_corex_beta_decay: + beta_decay = _corex_gdn_beta_decay.beta_decay( + b_all, a_all, self.A_log, self.dt_bias) + bt = beta_decay[0] + g_t = beta_decay[1] + else: + beta = b_all.sigmoid() + g = (-self.A_log.float().exp() + * F.softplus(a_all.float() + self.dt_bias)) + bt = beta.float() + g_t = g.float().exp_() + + # Inlined decode recurrent step (seq_len=1). + # Uses bmm/baddbmm_ to avoid large intermediate tensors. + _scale = self.head_k_dim ** -0.5 + q_raw = q.squeeze(1) + k_raw = k.squeeze(1) + use_corex_qk_map = ( + _USE_COREX_GDN_QK_MAP + and q_raw.dtype == torch.float16 + and k_raw.dtype == torch.float16 + and self.head_k_dim == 128 + and q_raw.is_contiguous() + and k_raw.is_contiguous()) + if use_corex_qk_map: + use_combined_qk_norm = ( + _USE_COREX_GDN_COMBINED_QK_NORM + and num_seqs == 1 + and local_num_k == 4 + and local_num_v == 8 + and packed_mixed_qkv.dtype == torch.float16 + and packed_mixed_qkv.shape == (1, 2048) + and packed_mixed_qkv.is_contiguous()) + if use_combined_qk_norm: + raw_qk = packed_mixed_qkv.narrow( + 1, 0, 2 * local_key_dim).view( + num_seqs, 2 * local_num_k, + self.head_k_dim) + normalized_qk = _l2norm(raw_qk) + normalized_q, normalized_k = torch.split( + normalized_qk, local_num_k, dim=1) + else: + normalized_q = _l2norm(q_raw) + normalized_k = _l2norm(k_raw) + qk_mapped = _corex_gdn_qk_map.qk_map( + normalized_q, normalized_k, local_num_v) + q_t = qk_mapped[0] + k_t = qk_mapped[1] + else: + q_expanded = q_raw.repeat_interleave( + self.head_expand_ratio, dim=1) + k_expanded = k_raw.repeat_interleave( + self.head_expand_ratio, dim=1) + q_t = _l2norm(q_expanded).float() * _scale + k_t = _l2norm(k_expanded).float() + v_t = v.squeeze(1).float() + + with bi100_timer(f"L{self.layer_idx}.gdn.decode"): + # State shape is (B, H_v, k_dim, v_dim). + temporal_state.mul_(g_t[:, :, None, None]) + ts_flat = temporal_state.view( + -1, self.head_k_dim, self.head_v_dim) + BH = ts_flat.shape[0] + kv_mem = torch.bmm( + k_t.view(BH, 1, self.head_k_dim), ts_flat + ).view(num_seqs, local_num_v, self.head_v_dim) + delta = (v_t - kv_mem) * bt[:, :, None] + ts_flat.baddbmm_( + k_t.view(BH, self.head_k_dim, 1), + delta.view(BH, 1, self.head_v_dim), + ) + core_out = torch.bmm( + q_t.view(BH, 1, self.head_k_dim), ts_flat + ).view(num_seqs, local_num_v, self.head_v_dim) + # core_out: (B, H_v, v_dim) = (num_seqs, local_num_v, head_v_dim) already + + z = z_all.reshape(num_seqs, local_num_v, self.head_v_dim) + normed = self.norm.forward_decode( + core_out.reshape(-1, self.head_v_dim), + z.reshape(-1, self.head_v_dim)) + normed = _check_gdn_finite( + normed, layer_idx=self.layer_idx, + stage="decode-norm").reshape(num_seqs, -1) + out, _ = self.out_proj(normed) + return _check_gdn_finite( + out, layer_idx=self.layer_idx, stage="decode-output") + + +# --------------------------------------------------------------------------- +# Full Attention (with gated q — unique to Qwen3.5) +# --------------------------------------------------------------------------- + +class Qwen3_5AttentionHeadRMSNorm(GemmaRMSNorm): + def forward_cuda( + self, + x: torch.Tensor, + residual: Optional[torch.Tensor] = None, + ): + if (_USE_COREX_ATTN_HEAD_RMS_NORM + and residual is None + and x.dtype == torch.float16 + and self.weight.dtype == torch.float16 + and x.dim() == 3 + and x.shape[0] == 1 + and x.shape[-1] == 256 + and x.is_contiguous() + and self.weight.is_contiguous()): + original_shape = x.shape + converted, squares = _corex_attn_head_rms_norm.prepare( + x.view(-1, 256)) + inverse = torch.rsqrt( + squares.mean(dim=-1, keepdim=True) + + self.variance_epsilon) + return _corex_attn_head_rms_norm.apply_inverse( + converted, self.weight, inverse).view(original_shape) + return super().forward_cuda(x, residual) + + +class Qwen3_5FullAttention(nn.Module): + def __init__( + self, + text_cfg, + layer_idx: int, + cache_config: Optional[CacheConfig] = None, + quant_config: Optional[QuantizationConfig] = None, + prefix: str = "", + ) -> None: + super().__init__() + self.layer_idx = layer_idx + self.hidden_size = text_cfg.hidden_size # 5120 + self.num_heads = text_cfg.num_attention_heads # 24 + self.num_kv_heads = text_cfg.num_key_value_heads # 4 + self.head_dim = text_cfg.head_dim # 256 + self.rms_norm_eps = text_cfg.rms_norm_eps + + tp_size = get_tensor_model_parallel_world_size() + self.local_num_heads = self.num_heads // tp_size + self.scaling = self.head_dim ** -0.5 + self.use_packed_local_qgkv = tp_size > self.num_kv_heads + + # When num_kv_heads < tp_size we cannot shard KV further (would give + # fractional heads per rank). Use ReplicatedLinear so every rank holds + # all KV heads; local_num_kv_heads equals the full count. + # When num_kv_heads >= tp_size standard ColumnParallel sharding applies. + if tp_size > self.num_kv_heads: + # GQA-aware TP sharding: ixformer kernel only supports num_kv_heads=1 + # per rank. With num_kv_heads=2 < tp_size=4 we cannot shard KV + # evenly, but we CAN assign each rank the ONE KV head that serves + # its Q heads: + # q_per_kv = num_heads // num_kv_heads (e.g. 16//2 = 8) + # Rank r uses KV head r * local_num_heads // q_per_kv + # e.g. ranks 0,1 → KV head 0; ranks 2,3 → KV head 1. + # We replicate all KV heads to every rank and select in forward(). + self.proj_kv_heads = self.num_kv_heads # heads available from projection + self.local_num_kv_heads = 1 # heads after rank-local selection + self.q_per_kv_global = self.num_heads // self.num_kv_heads + local_qg_dim = self.local_num_heads * self.head_dim * 2 + replicated_kv_dim = self.num_kv_heads * self.head_dim + self.qgkv_proj = ReplicatedLinear( + self.hidden_size, local_qg_dim + 2 * replicated_kv_dim, + bias=False, quant_config=quant_config, + prefix=f"{prefix}.qgkv_proj") + else: + # Standard sharding: each rank gets num_kv_heads // tp_size heads. + self.local_num_kv_heads = self.num_kv_heads // tp_size + self.proj_kv_heads = self.local_num_kv_heads # already sharded + self.q_per_kv_global = None + self.k_proj = ColumnParallelLinear( + self.hidden_size, self.num_kv_heads * self.head_dim, + bias=False, quant_config=quant_config, + prefix=f"{prefix}.k_proj") + self.v_proj = ColumnParallelLinear( + self.hidden_size, self.num_kv_heads * self.head_dim, + bias=False, quant_config=quant_config, + prefix=f"{prefix}.v_proj") + + self.local_q_dim = self.local_num_heads * self.head_dim + self.local_kv_dim = self.local_num_kv_heads * self.head_dim + + if not self.use_packed_local_qgkv: + # q_proj includes gate: output = num_heads * head_dim * 2 + self.q_proj = ColumnParallelLinear( + self.hidden_size, self.num_heads * self.head_dim * 2, + bias=False, quant_config=quant_config, + prefix=f"{prefix}.q_proj") + self.o_proj = RowParallelLinear( + self.num_heads * self.head_dim, self.hidden_size, + bias=False, quant_config=quant_config, + prefix=f"{prefix}.o_proj") + + self.q_norm = Qwen3_5AttentionHeadRMSNorm( + self.head_dim, eps=self.rms_norm_eps) + self.k_norm = Qwen3_5AttentionHeadRMSNorm( + self.head_dim, eps=self.rms_norm_eps) + + # Partial RoPE: rotary_dim = head_dim * partial_rotary_factor = 256 * 0.25 = 64 + rope_params = getattr(text_cfg, "rope_parameters", {}) or {} + rope_theta = rope_params.get("rope_theta", 10_000_000) + partial_factor = rope_params.get("partial_rotary_factor", 0.25) + rotary_dim = int(self.head_dim * partial_factor) + + self.rotary_emb = Qwen3_5InterleavedMRotaryEmbedding( + head_size=self.head_dim, + rotary_dim=rotary_dim, + max_position_embeddings=text_cfg.max_position_embeddings, + base=rope_theta, + is_neox_style=True, + dtype=torch.get_default_dtype(), + mrope_section=rope_params.get("mrope_section", [11, 11, 10]), + ) + + self.attn = Attention( + self.local_num_heads, + self.head_dim, + self.scaling, + num_kv_heads=self.local_num_kv_heads, + cache_config=cache_config, + quant_config=quant_config, + prefix=f"{prefix}.attn", + ) + + def forward( + self, + positions: torch.Tensor, + hidden_states: torch.Tensor, + kv_cache: torch.Tensor, + attn_metadata: AttentionMetadata, + ) -> torch.Tensor: + total_tokens = hidden_states.shape[0] + + with bi100_timer("full_attn.project_qgkv"): + if self.use_packed_local_qgkv: + projected, _ = self.qgkv_proj(hidden_states) + qg, k, v = torch.split( + projected, + [self.local_num_heads * self.head_dim * 2, + self.proj_kv_heads * self.head_dim, + self.proj_kv_heads * self.head_dim], + dim=-1) + else: + qg, _ = self.q_proj(hidden_states) + k, _ = self.k_proj(hidden_states) + v, _ = self.v_proj(hidden_states) + + with bi100_timer("full_attn.norm_rope"): + # q projection output includes gate (dim doubled). + qg = qg.view(total_tokens, self.local_num_heads, + self.head_dim * 2) + q = qg[:, :, :self.head_dim].reshape(total_tokens, -1) + gate = qg[:, :, self.head_dim:].reshape(total_tokens, -1) + + q = self.q_norm.forward_cuda( + q.view(total_tokens, self.local_num_heads, self.head_dim) + .contiguous()).view(total_tokens, -1) + + # Select the one rank-local KV head before k_norm and RoPE. + if self.q_per_kv_global is not None: + tp_rank = get_tensor_model_parallel_rank() + kv_idx = ((tp_rank * self.local_num_heads) + // self.q_per_kv_global) + k = (k.view(total_tokens, self.proj_kv_heads, self.head_dim) + [:, kv_idx, :].contiguous()) + v = (v.view(total_tokens, self.proj_kv_heads, self.head_dim) + [:, kv_idx, :].contiguous()) + + k = self.k_norm.forward_cuda( + k.view(total_tokens, self.local_num_kv_heads, self.head_dim) + .contiguous()).view(total_tokens, -1) + q, k = self.rotary_emb(positions, q, k) + + with bi100_timer("full_attn.attention"): + with bi100_timer(f"L{self.layer_idx}.full_attn"): + attn_out = self.attn(q, k, v, kv_cache, attn_metadata) + + with bi100_timer("full_attn.gate"): + attn_out = (attn_out + * torch.sigmoid(gate.float()).to(attn_out.dtype)) + with bi100_timer("full_attn.output_proj"): + output, _ = self.o_proj(attn_out) + return output + + +# --------------------------------------------------------------------------- +# MLP (SwiGLU, same as Qwen2/Qwen3) +# --------------------------------------------------------------------------- + +class Qwen3_5MLP(nn.Module): + def __init__( + self, + hidden_size: int, + intermediate_size: int, + hidden_act: str, + quant_config: Optional[QuantizationConfig] = None, + ) -> None: + super().__init__() + self.gate_up_proj = MergedColumnParallelLinear( + hidden_size, [intermediate_size] * 2, + bias=False, quant_config=quant_config) + self.down_proj = RowParallelLinear( + intermediate_size, hidden_size, + bias=False, quant_config=quant_config) + if hidden_act != "silu": + raise ValueError(f"Unsupported activation: {hidden_act}") + self.act_fn = SiluAndMul() + + def forward(self, x: torch.Tensor) -> torch.Tensor: + gate_up, _ = self.gate_up_proj(x) + x = self.act_fn(gate_up) + x, _ = self.down_proj(x) + return x + + +# --------------------------------------------------------------------------- +# MoE sparse block (Qwen3.5-MoE / Qwen3.6-35B-A3B) +# --------------------------------------------------------------------------- + +class Qwen3_5MoeSparseBlock(nn.Module): + """Replaces Qwen3_5MLP for qwen3_5_moe_text layers. + + FusedMoE is used ONLY for weight storage and loading (create_weights / + weight_loader are pure PyTorch). Its forward kernel is bypassed because + ixformer on BI-V100 lacks vllm_moe_topk_softmax / vllm_invoke_fused_moe_kernel. + Routing and expert computation use a pure-PyTorch loop instead. + + Shared expert uses RowParallelLinear(reduce_results=False) so both paths + produce partial (pre-all-reduce) outputs that are combined before a single + all-reduce. + """ + + def __init__( + self, + text_cfg, + quant_config: Optional[QuantizationConfig] = None, + ) -> None: + super().__init__() + hidden_size = text_cfg.hidden_size + self.num_experts = text_cfg.num_experts + self.top_k = text_cfg.num_experts_per_tok + + # Router and scalar shared-expert gate read the same hidden state. Keep + # their checkpoint shards in one replicated weight so forward needs a + # single GEMM for 256 + 1 outputs. + self.router_shared_gate = ReplicatedLinear( + hidden_size, text_cfg.num_experts + 1, + bias=False, quant_config=quant_config) + self.router_shared_gate.weight.weight_loader = \ + self._router_shared_gate_weight_loader + + # FusedMoE: only used for weight storage + weight_loader. + # Forward is bypassed — see _pure_pytorch_experts(). + self.experts = FusedMoE( + num_experts=text_cfg.num_experts, + top_k=text_cfg.num_experts_per_tok, + hidden_size=hidden_size, + intermediate_size=text_cfg.moe_intermediate_size, + reduce_results=False, # we do the all-reduce ourselves below + renormalize=True, + quant_config=quant_config, + ) + + # Shared expert: defer all-reduce to combine with routed output first + shared_size = text_cfg.shared_expert_intermediate_size + self.shared_expert_gate_up = MergedColumnParallelLinear( + hidden_size, [shared_size] * 2, bias=False, + quant_config=quant_config) + self.shared_expert_down = RowParallelLinear( + shared_size, hidden_size, bias=False, reduce_results=False, + quant_config=quant_config) + self.act_fn = SiluAndMul() + + def _router_shared_gate_weight_loader( + self, + param: torch.Tensor, + loaded_weight: torch.Tensor, + shard_id: int, + ) -> None: + if shard_id == 0: + offset = 0 + rows = self.num_experts + elif shard_id == 1: + offset = self.num_experts + rows = 1 + else: + raise ValueError(f"unexpected router/shared gate shard: {shard_id}") + + expected = (rows, param.shape[1]) + if tuple(loaded_weight.shape) != expected: + raise ValueError( + "unexpected router/shared gate weight shape: " + f"expected {expected}, got {tuple(loaded_weight.shape)}") + param.data.narrow(0, offset, rows).copy_(loaded_weight) + + def _pure_pytorch_experts( + self, + hidden_states: torch.Tensor, + router_logits: torch.Tensor, + ) -> torch.Tensor: + """Pure-PyTorch MoE (ixformer has no MoE kernels on BI-V100). + + w13_weight: (num_experts, 2*inter_per_partition, hidden) [TP-sharded] + w2_weight: (num_experts, hidden, inter_per_partition) [TP-sharded] + Output is partial (pre-all-reduce), same contract as FusedMoE + with reduce_results=False. + """ + # Softmax is monotonic, so selecting logits first is equivalent to + # full-expert softmax -> top-k -> renormalise while normalising only K + # values. This saves one 256-wide softmax in the decode hot path. + topk_logits, topk_ids = torch.topk( + router_logits.float(), self.top_k, dim=-1) # (T, top_k) + topk_weights = torch.softmax(topk_logits, dim=-1) + topk_weights = topk_weights.to(hidden_states.dtype) + + w13 = self.experts.w13_weight # (E, 2*I, H) + w2 = self.experts.w2_weight # (E, H, I) + + T = hidden_states.shape[0] + if T == 1: + # Fast path: single token (decode). + # Batched GEMM: replace top_k separate F.linear calls with 2 fused ops. + # gate_up: 1 large GEMM (1,H) × (K*2*I,H)^T → (1, K*2*I) + # down: 1 bmm (K,H,I) @ (K,I,1) → (K,H) + # Total: 3 kernel launches vs previous 16 (top_k*2). + eids = topk_ids[0] # (K,) + ws = topk_weights[0].to(hidden_states.dtype) # (K,) + use_corex_direct = ( + _USE_COREX_MOE_DIRECT_ROUTED + and hidden_states.dtype == torch.float16 + and w13.dtype == torch.float16 + and w2.dtype == torch.float16 + and ws.dtype == torch.float16 + and hidden_states.is_cuda and w13.is_cuda and w2.is_cuda + and eids.is_cuda and ws.is_cuda + and hidden_states.is_contiguous() + and w13.is_contiguous() and w2.is_contiguous() + and eids.is_contiguous() and ws.is_contiguous() + and hidden_states.shape == (1, 2048) + and w13.shape == (256, 256, 2048) + and w2.shape == (256, 2048, 128) + and eids.shape == (8,) and ws.shape == (8,)) + if use_corex_direct: + gate_up = _corex_moe_direct_routed.w13( + hidden_states, w13, eids) + act = self.act_fn(gate_up) + return _corex_moe_direct_routed.w2_reduce( + act, w2, eids, ws) + + use_corex_gather = ( + _USE_COREX_MOE_WEIGHT_GATHER + and hidden_states.dtype == torch.float16 + and w13.dtype == torch.float16 + and w2.dtype == torch.float16 + and w13.is_cuda and w2.is_cuda and eids.is_cuda + and w13.is_contiguous() and w2.is_contiguous() + and eids.is_contiguous() + and w13.dim() == 3 and w2.dim() == 3 + and eids.dim() == 1 and eids.numel() == 8 + and w13.shape[0] == w2.shape[0] + and w13.shape[2] == w2.shape[1] + and w13.shape[1] == 2 * w2.shape[2] + and w13.shape[1] * w13.shape[2] % 8 == 0 + and w2.shape[1] * w2.shape[2] % 8 == 0) + if use_corex_gather: + w13_sel, w2_sel = _corex_moe_weight_gather.gather( + w13, w2, eids) + else: + w13_sel = w13[eids] # (K, 2*I, H) + w2_sel = w2[eids] # (K, H, I) + + H = hidden_states.shape[-1] + + gate_up = F.linear( + hidden_states, + w13_sel.reshape(-1, H), # (K*2*I, H) — contiguous after indexing + ) # (1, K*2*I) + gate_up = gate_up.view(self.top_k, -1) # (K, 2*I) + if _USE_FUSED_MOE_ACTIVATION: + act = self.act_fn(gate_up) # (K, I) + else: + gate, up = gate_up.chunk(2, dim=-1) + act = F.silu(gate) * up + + # bmm: (K,H,I) @ (K,I,1) → (K,H,1) → (K,H) + expert_out = torch.bmm(w2_sel, act.unsqueeze(-1)).squeeze(-1) # (K, H) + + if (_USE_COREX_MOE_EXACT_REDUCE + and expert_out.dtype == torch.float16 + and ws.dtype == torch.float16 + and expert_out.shape[0] == 8): + out = _corex_moe_exact_reduce.serial_float(expert_out, ws) + else: + out = (expert_out * ws.unsqueeze(-1)).sum( + 0, keepdim=True).to(hidden_states.dtype) # (1, H) + else: + # General path (prefill / multi-seq): group assignments once. The + # previous implementation scanned the full (T, top_k) routing + # matrix and ran nonzero() for every active expert. + out = torch.zeros_like(hidden_states) + flat_eids = topk_ids.reshape(-1) + order = torch.argsort(flat_eids, stable=True) + sorted_tok_ids = torch.arange( + T, device=topk_ids.device).repeat_interleave(self.top_k)[order] + sorted_weights = topk_weights.reshape(-1)[order] + expert_counts = torch.bincount( + flat_eids, minlength=w13.shape[0]).tolist() + + start = 0 + for eid, count in enumerate(expert_counts): + end = start + count + if count == 0: + start = end + continue + tok_ids = sorted_tok_ids[start:end] + tokens = hidden_states[tok_ids] # (n, H) + gate_up = F.linear(tokens, w13[eid]) # (n, 2*I) + gate, up = gate_up.chunk(2, dim=-1) + act = F.silu(gate) * up # (n, I) + expert_out = F.linear(act, w2[eid]) # (n, H) + weights = sorted_weights[start:end].unsqueeze(-1) + out.index_add_(0, tok_ids, (expert_out * weights).to(out.dtype)) + start = end + + return out # partial, all-reduce done in forward() + + def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: + with bi100_timer("moe.router"): + router_and_shared_gate, _ = self.router_shared_gate(hidden_states) + router_logits = router_and_shared_gate[..., :self.num_experts] + gate_score = router_and_shared_gate[..., self.num_experts:] + with bi100_timer("moe.routed"): + routed_out = self._pure_pytorch_experts(hidden_states, router_logits) + + with bi100_timer("moe.shared"): + gate_up, _ = self.shared_expert_gate_up(hidden_states) + shared_out = self.act_fn(gate_up) + shared_out, _ = self.shared_expert_down(shared_out) + shared_out = shared_out * torch.sigmoid(gate_score) + + with bi100_timer("moe.combine"): + out = routed_out + shared_out + if self.experts.tp_size > 1: + with bi100_timer("moe.all_reduce"): + out = tensor_model_parallel_all_reduce(out) + return out + + +# --------------------------------------------------------------------------- +# Decoder layer (dispatches to GatedDeltaNet or Qwen3_5FullAttention) +# --------------------------------------------------------------------------- + + +class Qwen3_5DecoderLayer(nn.Module): + def __init__( + self, + text_cfg, + layer_idx: int, + layer_type: str, + cache_config: Optional[CacheConfig] = None, + quant_config: Optional[QuantizationConfig] = None, + ) -> None: + super().__init__() + self.layer_idx = layer_idx + self.layer_type = layer_type + self._diagnostic_trace_pending = ( + os.getenv("BI100_DIAGNOSTIC_LAYER_TRACE") == "1") + self.input_layernorm = GemmaRMSNorm(text_cfg.hidden_size, + eps=text_cfg.rms_norm_eps) + self.post_attention_layernorm = GemmaRMSNorm(text_cfg.hidden_size, + eps=text_cfg.rms_norm_eps) + + if layer_type == "linear_attention": + self.linear_attn = GatedDeltaNet(text_cfg, layer_idx, + quant_config=quant_config) + else: + self.self_attn = Qwen3_5FullAttention( + text_cfg, layer_idx, + cache_config=cache_config, + quant_config=quant_config, + prefix=f"layers.{layer_idx}.self_attn", + ) + + if getattr(text_cfg, 'model_type', '') == 'qwen3_5_moe_text': + self.mlp = Qwen3_5MoeSparseBlock(text_cfg, quant_config=quant_config) + else: + self.mlp = Qwen3_5MLP( + hidden_size=text_cfg.hidden_size, + intermediate_size=text_cfg.intermediate_size, + hidden_act=text_cfg.hidden_act, + quant_config=quant_config, + ) + + def forward( + self, + positions: torch.Tensor, + hidden_states: torch.Tensor, + kv_cache: Optional[torch.Tensor], + attn_metadata: AttentionMetadata, + residual: Optional[torch.Tensor], + # Only for linear_attention layers: + conv_state: Optional[torch.Tensor] = None, + temporal_state: Optional[torch.Tensor] = None, + gdn_capture_offsets: Optional[Iterable[int]] = None, + gdn_segment_offsets: Optional[Iterable[int]] = None, + ) -> Tuple[torch.Tensor, torch.Tensor]: + with bi100_timer("layer.input_norm"): + if residual is None: + residual = hidden_states + hidden_states = self.input_layernorm(hidden_states) + else: + hidden_states, residual = self.input_layernorm( + hidden_states, residual) + + if self.layer_type == "linear_attention": + with bi100_timer("layer.gdn"): + hidden_states = self.linear_attn( + hidden_states, attn_metadata, conv_state, temporal_state, + capture_offsets=gdn_capture_offsets, + segment_offsets=gdn_segment_offsets) + else: + with bi100_timer("layer.full_attn"): + hidden_states = self.self_attn( + positions, hidden_states, kv_cache, attn_metadata) + + with bi100_timer("layer.post_attn_norm"): + hidden_states, residual = self.post_attention_layernorm( + hidden_states, residual) + + with bi100_timer("layer.moe"): + hidden_states = self.mlp(hidden_states) + + if self._diagnostic_trace_pending: + self._diagnostic_trace_pending = False + rank = os.getenv("RANK", os.getenv("LOCAL_RANK", "?")) + print( + "[BI100 DIAGNOSTIC] " + f"rank={rank} layer={self.layer_idx} " + f"attention={self.layer_type} " + f"mlp={type(self.mlp).__name__} stage=completed", + file=sys.stderr, + flush=True, + ) + + return hidden_states, residual + + +# --------------------------------------------------------------------------- +# Full transformer model +# --------------------------------------------------------------------------- + +def _validate_qwen_kv_cache_count(configured_count, kv_caches): + if len(kv_caches) != configured_count: + raise RuntimeError( + "Qwen3.5 allocated KV cache count mismatch: " + f"configured {configured_count}, received {len(kv_caches)}") + + +class Qwen3_5Model(nn.Module): + def __init__( + self, + text_cfg, + cache_config: Optional[CacheConfig] = None, + quant_config: Optional[QuantizationConfig] = None, + kv_cache_count: Optional[int] = None, + ) -> None: + super().__init__() + self.text_cfg = text_cfg + full_attention_count = sum( + layer_type == "full_attention" + for layer_type in text_cfg.layer_types) + if kv_cache_count is None: + kv_cache_count = full_attention_count + if (not isinstance(kv_cache_count, int) or isinstance(kv_cache_count, bool) + or kv_cache_count < full_attention_count): + raise RuntimeError( + "Qwen3.5 configured KV cache count must cover every " + f"full-attention layer: configured {kv_cache_count}, " + f"required {full_attention_count}") + self.kv_cache_count = kv_cache_count + self.embed_tokens = VocabParallelEmbedding( + text_cfg.vocab_size, text_cfg.hidden_size) + self.layers = nn.ModuleList([ + Qwen3_5DecoderLayer( + text_cfg, i, text_cfg.layer_types[i], + cache_config=cache_config, quant_config=quant_config) + for i in range(text_cfg.num_hidden_layers) + ]) + self.norm = GemmaRMSNorm(text_cfg.hidden_size, eps=text_cfg.rms_norm_eps) + + def forward( + self, + input_ids: torch.Tensor, + positions: torch.Tensor, + kv_caches: List[torch.Tensor], + attn_metadata: AttentionMetadata, + conv_states: torch.Tensor, # (num_linear_layers, batch, ...) + temporal_states: torch.Tensor, # (num_linear_layers, batch, ...) + inputs_embeds: Optional[torch.Tensor] = None, + gdn_capture_offsets: Optional[Iterable[int]] = None, + gdn_segment_offsets: Optional[Iterable[int]] = None, + ) -> torch.Tensor: + _validate_qwen_kv_cache_count(self.kv_cache_count, kv_caches) + with bi100_timer("model.embed"): + hidden_states = (self.embed_tokens(input_ids) + if inputs_embeds is None else inputs_embeds) + residual = None + + attn_idx = 0 + linear_idx = 0 + capture_offsets = tuple(gdn_capture_offsets or ()) + captured_conv_states: Dict[int, List[torch.Tensor]] = { + offset: [] for offset in capture_offsets + } + captured_temporal_states: Dict[int, List[torch.Tensor]] = { + offset: [] for offset in capture_offsets + } + for layer in self.layers: + if layer.layer_type == "linear_attention": + hidden_states, residual = layer( + positions, hidden_states, + kv_cache=None, + attn_metadata=attn_metadata, + residual=residual, + conv_state=conv_states[linear_idx], + temporal_state=temporal_states[linear_idx], + gdn_capture_offsets=capture_offsets, + gdn_segment_offsets=gdn_segment_offsets, + ) + for offset in capture_offsets: + captured_conv_states[offset].append( + layer.linear_attn.captured_conv_states[offset]) + captured_temporal_states[offset].append( + layer.linear_attn.captured_temporal_states[offset]) + linear_idx += 1 + else: + kv_cache = kv_caches[attn_idx] + hidden_states, residual = layer( + positions, hidden_states, + kv_cache=kv_cache, + attn_metadata=attn_metadata, + residual=residual, + ) + attn_idx += 1 + + with bi100_timer("model.final_norm"): + hidden_states, _ = self.norm(hidden_states, residual) + self.captured_conv_states = { + offset: torch.stack(states) + for offset, states in captured_conv_states.items() + } + self.captured_temporal_states = { + offset: torch.stack(states) + for offset, states in captured_temporal_states.items() + } + return hidden_states + + +# --------------------------------------------------------------------------- +# Top-level CausalLM wrapper with MambaCacheManager +# --------------------------------------------------------------------------- + +class Qwen3_5ForCausalLM(nn.Module, HasInnerState, SupportsLoRA, + SupportsMultiModal): + + has_inner_state = True + supports_lora = True + + packed_modules_mapping = { + "gate_up_proj": ["gate_proj", "up_proj"], + } + + supported_lora_modules = [ + "gate_up_proj", + "down_proj", + "o_proj", + ] + embedding_modules = {} + embedding_padding_modules = [] + + def __init__( + self, + config, # Qwen3_5Config (top-level) + cache_config: Optional[CacheConfig] = None, + quant_config: Optional[QuantizationConfig] = None, + lora_config: Optional[LoRAConfig] = None, + scheduler_config: Optional[SchedulerConfig] = None, + multimodal_config: Optional[MultiModalConfig] = None, + prefix: str = "", + ) -> None: + _bi100_model_trace("Qwen3_5ForCausalLM initialization begin") + super().__init__() + self.config = config + self.scheduler_config = scheduler_config + self.multimodal_config = multimodal_config + + # The text config holds all architecture parameters + text_cfg = config.text_config + self.text_cfg = text_cfg + rope_parameters = getattr(text_cfg, "rope_parameters", {}) or {} + mrope_sections = rope_parameters.get("mrope_section", [11, 11, 10]) + if getattr(config, "rope_scaling", None) is None: + config.rope_scaling = { + "type": "mrope", + "mrope_section": mrope_sections, + } + + # Pre-compute counts + self.num_linear_layers = sum( + 1 for lt in text_cfg.layer_types if lt == "linear_attention") + self.num_attn_layers = sum( + 1 for lt in text_cfg.layer_types if lt == "full_attention") + layers_block_type = getattr( + config, "layers_block_type", + ["attention"] * text_cfg.num_hidden_layers) + self.num_kv_cache_layers = sum( + layer_type == "attention" for layer_type in layers_block_type) + if self.num_kv_cache_layers < self.num_attn_layers: + raise RuntimeError( + "Qwen3.5 KV accounting provides fewer caches than " + f"full-attention layers: {self.num_kv_cache_layers} < " + f"{self.num_attn_layers}") + accounting_mode = getattr( + config, "bi100_hybrid_kv_accounting_mode", "legacy40") + accounting_env = os.getenv("BI100_HYBRID_KV_ACCOUNTING", "") + tp_rank = get_tensor_model_parallel_rank() + full_attention_ordinals = ",".join( + str(index) for index, layer_type in enumerate(text_cfg.layer_types) + if layer_type == "full_attention") + logger.info( + "[BI100] Qwen hybrid KV accounting; tp_rank=%d " + "env_mode=%s config_mode=%s " + "configured_kv_layers=%d full_attention_layers=%d " + "full_attention_ordinals=%s", + tp_rank, + accounting_env, + accounting_mode, + self.num_kv_cache_layers, + self.num_attn_layers, + full_attention_ordinals, + ) + + # DeltaNet state dimensions (per layer, per sequence, TP-sharded) + tp_size = get_tensor_model_parallel_world_size() + self.conv_dim = (text_cfg.linear_num_key_heads * text_cfg.linear_key_head_dim * 2 + + text_cfg.linear_num_value_heads * text_cfg.linear_value_head_dim) + self.num_v_heads = text_cfg.linear_num_value_heads + self.head_k_dim = text_cfg.linear_key_head_dim + self.head_v_dim = text_cfg.linear_value_head_dim + self.conv_kernel_size = text_cfg.linear_conv_kernel_dim + + self.model = Qwen3_5Model( + text_cfg, + cache_config=cache_config, + quant_config=quant_config, + kv_cache_count=self.num_kv_cache_layers, + ) + + self.visual = Qwen3_5VisionTransformer( + config.vision_config, + quant_config=None, + ) + + self.lm_head = ParallelLMHead( + text_cfg.vocab_size, text_cfg.hidden_size, + quant_config=quant_config, + ) + + self.logits_processor = LogitsProcessor(text_cfg.vocab_size) + self.sampler = Sampler() + + # Lazy initialised in first forward call + self.mamba_cache: Optional[MambaCacheManager] = None + + # Scheduler-owned recurrent prefix states. Keys are stable chained + # content hashes, never recyclable physical KV block ids. + self._gdn_prefix_cache: Dict[ + Tuple[int, bytes], Tuple[torch.Tensor, torch.Tensor]] = {} + self._block_size: int = (cache_config.block_size + if cache_config is not None else 16) + self._startup_forward_traced = False + _bi100_model_trace("Qwen3_5ForCausalLM initialization complete") + + def _get_mamba_cache_shape(self): + tp_size = get_tensor_model_parallel_world_size() + # Each sequence's state is stored in float32 + conv_state_shape = (self.conv_dim // tp_size, self.conv_kernel_size - 1) + temporal_state_shape = ( + self.num_v_heads // tp_size, self.head_k_dim, self.head_v_dim) + return conv_state_shape, temporal_state_shape + + @staticmethod + def _validate_and_reshape_mm_tensor( + mm_input: Union[torch.Tensor, List[torch.Tensor]], + name: str, + ) -> torch.Tensor: + if isinstance(mm_input, list): + return torch.cat(mm_input) + if not isinstance(mm_input, torch.Tensor): + raise ValueError(f"incorrect type for {name}: {type(mm_input)}") + if mm_input.ndim == 2: + return mm_input + if mm_input.ndim == 3: + return torch.cat(list(mm_input)) + raise ValueError( + f"{name} must be a 2D tensor or batched 3D tensor, got " + f"shape={tuple(mm_input.shape)}") + + def _parse_and_validate_image_input( + self, + **kwargs: object, + ) -> Optional[Qwen3_5ImageInputs]: + pixel_values = kwargs.get("pixel_values") + image_embeds = kwargs.get("image_embeds") + image_grid_thw = kwargs.get("image_grid_thw") + if pixel_values is None and image_embeds is None: + return None + if pixel_values is not None: + if image_grid_thw is None: + raise ValueError("image_grid_thw is required with pixel_values") + return Qwen3_5ImagePixelInputs( + type="pixel_values", + data=self._validate_and_reshape_mm_tensor( + pixel_values, "image pixel values"), + image_grid_thw=self._validate_and_reshape_mm_tensor( + image_grid_thw, "image grid_thw"), + ) + return Qwen3_5ImageEmbeddingInputs( + type="image_embeds", + data=self._validate_and_reshape_mm_tensor( + image_embeds, "image embeddings"), + ) + + def _process_image_input( + self, + image_input: Qwen3_5ImageInputs, + ) -> torch.Tensor: + if image_input["type"] == "image_embeds": + return image_input["data"].to(dtype=self.visual.dtype, + device=self.visual.device) + return self.visual( + image_input["data"], + grid_thw=image_input["image_grid_thw"], + ) + + @bi100_profile_transaction + def forward( + self, + input_ids: torch.Tensor, + positions: torch.Tensor, + kv_caches: List[torch.Tensor], + attn_metadata: AttentionMetadata, + intermediate_tensors: Optional[IntermediateTensors] = None, + **kwargs, + ) -> torch.Tensor: + if not self._startup_forward_traced: + self._startup_forward_traced = True + _bi100_model_trace("first model forward entered") + if self.mamba_cache is None: + if self.scheduler_config is not None: + max_batch_size = _get_graph_batch_size( + self.scheduler_config.max_num_seqs) + else: + max_batch_size = max(_BATCH_SIZES_TO_CAPTURE) + 2 + self.mamba_cache = MambaCacheManager( + torch.float32, + self.num_linear_layers, + max_batch_size, + *self._get_mamba_cache_shape(), + ) + + gdn_restore_key = kwargs.pop("gdn_restore_key", None) + gdn_capture_points = kwargs.pop("gdn_capture_points", None) or [] + gdn_evict_keys = kwargs.pop("gdn_evict_keys", None) or [] + gdn_segment_offsets = kwargs.pop("gdn_segment_offsets", None) or [] + + mamba_tensors = self.mamba_cache.current_run_tensors( + input_ids, attn_metadata, **kwargs) + # conv_states: (num_linear_layers, batch, local_conv_dim, kernel-1) + # temporal_states: (num_linear_layers, batch, local_num_v, k_dim, v_dim) + conv_states, temporal_states = mamba_tensors + + _is_single_seq_prefill = ( + attn_metadata is not None + and attn_metadata.num_prefill_tokens > 0 + and conv_states.shape[1] == 1 # batch == 1 + and getattr(attn_metadata, 'context_lens_tensor', None) is not None + ) + has_gdn_actions = (gdn_restore_key is not None + or bool(gdn_capture_points) + or bool(gdn_evict_keys) + or bool(gdn_segment_offsets)) + if has_gdn_actions and not _is_single_seq_prefill: + raise RuntimeError( + "GDN prefix-cache actions require a single-sequence prefill") + + for evict_key in gdn_evict_keys: + self._gdn_prefix_cache.pop(_validate_gdn_prefix_key(evict_key), + None) + + if gdn_restore_key is not None: + restore_key = _validate_gdn_prefix_key(gdn_restore_key) + saved_state = self._gdn_prefix_cache.get(restore_key) + if saved_state is None: + raise RuntimeError( + "scheduler requested a missing GDN prefix state: " + f"blocks={restore_key[0]} digest={restore_key[1].hex()}") + saved_conv, saved_temporal = saved_state + with bi100_timer("gdn_prefix.restore"): + conv_states[:, 0].copy_( + saved_conv.to(device=conv_states.device, + dtype=conv_states.dtype), + non_blocking=True) + temporal_states[:, 0].copy_( + saved_temporal.to(device=temporal_states.device, + dtype=temporal_states.dtype), + non_blocking=True) + + query_len = (int(attn_metadata.num_prefill_tokens) + if _is_single_seq_prefill else 0) + capture_keys: Dict[int, Tuple[int, bytes]] = {} + for capture_point in gdn_capture_points: + if not isinstance(capture_point, tuple) or len(capture_point) != 2: + raise RuntimeError( + f"invalid GDN capture point: {capture_point!r}") + offset, capture_key = capture_point + if (not isinstance(offset, int) or offset <= 0 + or offset > query_len or offset in capture_keys): + raise RuntimeError( + f"invalid GDN capture offset: {offset!r} " + f"for query_len={query_len}") + capture_keys[offset] = _validate_gdn_prefix_key(capture_key) + if len(capture_keys) > 2: + raise RuntimeError("at most two GDN capture points are supported") + interior_capture_offsets = tuple( + offset for offset in capture_keys if offset < query_len) + segment_offsets = set() + for offset in gdn_segment_offsets: + if (not isinstance(offset, int) or offset <= 0 + or offset >= query_len): + raise RuntimeError( + f"invalid GDN segment offset: {offset!r} " + f"for query_len={query_len}") + segment_offsets.add(offset) + if len(segment_offsets) > 128: + raise RuntimeError("at most 128 GDN segment offsets are supported") + interior_segment_offsets = tuple(sorted(segment_offsets)) + + inputs_embeds = None + image_input = self._parse_and_validate_image_input(**kwargs) + if image_input is not None: + image_mask = input_ids == self.config.image_token_id + num_placeholders = int(image_mask.sum().item()) + if num_placeholders: + inputs_embeds = self.model.embed_tokens(input_ids) + image_embeds = self._process_image_input(image_input) + if num_placeholders > image_embeds.shape[0]: + raise ValueError( + f"image token count ({num_placeholders}) exceeds " + f"vision embeddings ({image_embeds.shape[0]})") + # Prefix caching can consume the leading image tokens while + # vLLM 0.6 still supplies the full pixel tensor. The query's + # remaining placeholders always form a suffix of the flattened + # visual token stream. + image_embeds = image_embeds[-num_placeholders:] + inputs_embeds[image_mask, :] = image_embeds.to( + inputs_embeds.dtype) + + with bi100_timer("model.forward"): + hidden_states = self.model( + input_ids, positions, kv_caches, attn_metadata, + conv_states, temporal_states, + inputs_embeds=inputs_embeds, + gdn_capture_offsets=interior_capture_offsets, + gdn_segment_offsets=interior_segment_offsets) + + for offset, capture_key in capture_keys.items(): + if offset == query_len: + captured_conv = conv_states[:, 0] + captured_temporal = temporal_states[:, 0] + else: + captured_conv = self.model.captured_conv_states[offset] + captured_temporal = self.model.captured_temporal_states[offset] + with bi100_timer("gdn_prefix.save"): + self._gdn_prefix_cache[capture_key] = ( + captured_conv.detach().cpu().clone(), + captured_temporal.detach().cpu().clone(), + ) + + if bi100_profile_event_enabled(): + profile_prefill_tokens = int( + getattr(attn_metadata, "num_prefill_tokens", 0) or 0) + profile_decode_tokens = int( + getattr(attn_metadata, "num_decode_tokens", 0) or 0) + profile_context_len = 0 + if profile_prefill_tokens > 0: + profile_seq_lens = getattr(attn_metadata, "seq_lens", None) + if (not isinstance(profile_seq_lens, list) + or len(profile_seq_lens) != 1 + or not isinstance(profile_seq_lens[0], int)): + raise RuntimeError( + "BI100 profile requires one host-visible prefill " + "sequence length") + profile_context_len = ( + profile_seq_lens[0] - profile_prefill_tokens) + if profile_context_len < 0: + raise RuntimeError( + "BI100 profile observed a negative prefill context") + bi100_profile_flush( + tp_rank=get_tensor_model_parallel_rank(), + phase=("prefill" if profile_prefill_tokens > 0 else "decode"), + prefill_tokens=profile_prefill_tokens, + decode_tokens=profile_decode_tokens, + context_len=profile_context_len, + gdn_restore=bool(gdn_restore_key is not None), + gdn_capture_points=len(gdn_capture_points), + gdn_evict_keys=len(gdn_evict_keys), + ) + return hidden_states + + def compute_logits( + self, + hidden_states: torch.Tensor, + sampling_metadata: SamplingMetadata, + ) -> Optional[torch.Tensor]: + # All TP ranks must call logits_processor to participate in the NCCL + # gather inside lm_head. Non-driver ranks return None after the gather. + # With chunked prefill, intermediate chunks have seq_groups=None on all + # ranks; _apply_logits_processors is guarded against this in + # logits_processor.py (patched by patch_xformers_sdpa_seq.py). + logits = self.logits_processor(self.lm_head, hidden_states, + sampling_metadata) + return logits + + def sample( + self, + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, + ) -> Optional[SamplerOutput]: + return self.sampler(logits, sampling_metadata) + + def copy_inputs_before_cuda_graphs(self, input_buffers, **kwargs): + return self.mamba_cache.copy_inputs_before_cuda_graphs( + input_buffers, **kwargs) + + def get_seqlen_agnostic_capture_inputs(self, batch_size: int): + return self.mamba_cache.get_seqlen_agnostic_capture_inputs(batch_size) + + def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): + _bi100_model_trace("dense load_weights begin") + loaded_count = 0 + stacked_params_mapping = [ + # (param_name, weight_name, shard_id) + ("gate_up_proj", "gate_proj", 0), + ("gate_up_proj", "up_proj", 1), + ] + params_dict = dict(self.named_parameters()) + + for name, loaded_weight in weights: + loaded_count += 1 + # Skip vision and MTP branches + if (name.startswith("model.visual") + or name.startswith("mtp.") + or name.startswith("model.mtp")): + continue + + # Prefix remapping: checkpoint may wrap under language_model + if name.startswith("model.language_model."): + name = "model." + name[len("model.language_model."):] + + # Skip positional embedding caches + if "rotary_emb.inv_freq" in name: + continue + + if _load_full_attention_qgkv_weight( + params_dict, name, loaded_weight, self.text_cfg): + continue + + if _load_gdn_projection_weight( + params_dict, name, loaded_weight, self.text_cfg): + continue + + # Remap conv1d.weight → conv1d_weight + # The conv has depth (1) dim in the checkpoint that we handle separately + if ".linear_attn.conv1d.weight" in name: + name = name.replace(".linear_attn.conv1d.weight", + ".linear_attn.conv1d_weight") + + # Stacked param loading (gate_up_proj) + for param_name, weight_name, shard_id in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_name, param_name) + if name.endswith(".bias") and name not in params_dict: + break + if name not in params_dict: + break + param = params_dict[name] + weight_loader = param.weight_loader + weight_loader(param, loaded_weight, shard_id) + break + else: + if name.endswith(".bias") and name not in params_dict: + continue + if name not in params_dict: + continue + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", + default_weight_loader) + weight_loader(param, loaded_weight) + _bi100_model_trace(f"dense load_weights complete items={loaded_count}") + + +# --------------------------------------------------------------------------- +# Qwen3.6-35B-A3B (Qwen3_5-MoE architecture) +# --------------------------------------------------------------------------- + +@MULTIMODAL_REGISTRY.register_image_input_mapper(qwen36_image_input_mapper) +@MULTIMODAL_REGISTRY.register_max_image_tokens(get_max_qwen36_image_tokens) +@INPUT_REGISTRY.register_dummy_data(dummy_data_for_qwen36) +@INPUT_REGISTRY.register_input_processor(input_processor_for_qwen36) +class Qwen3_5MoeForCausalLM(Qwen3_5ForCausalLM): + """Qwen3.6-35B-A3B: same hybrid-attention backbone as 27B, dense MLP + replaced by Qwen3_5MoeSparseBlock (256 routed experts + shared expert). + Only load_weights differs from the dense variant. + """ + + def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): + _bi100_model_trace("MoE load_weights begin") + loaded_count = 0 + vision_loaded_count = 0 + # Checkpoint key format for this model (transformers Qwen3_5MoeExperts): + # mlp.experts.gate_up_proj shape (num_experts, 2*intermediate, hidden) + # mlp.experts.down_proj shape (num_experts, hidden, intermediate) + # mlp.gate.weight shape (num_experts, hidden) [router] + # mlp.shared_expert_gate.weight shape (1, hidden) + # mlp.shared_expert.{gate,up,down}_proj.weight [shared MLP] + # Our FusedMoE stores: + # mlp.experts.w13_weight shape (num_experts, 2*intermediate//tp, hidden) + # mlp.experts.w2_weight shape (num_experts, hidden, intermediate//tp) + # Our router/shared gate stores both tensors in one (num_experts+1, H) + # replicated weight. Our shared expert stores: + # mlp.shared_expert_gate_up.weight (merged gate+up) + # mlp.shared_expert_down.weight + + stacked_params_mapping = [ + # (param_name, weight_name, shard_id) + # shared expert + ("shared_expert_gate_up", "shared_expert.gate_proj", 0), + ("shared_expert_gate_up", "shared_expert.up_proj", 1), + # linear_attention dense proj (same as 27B) + ("gate_up_proj", "gate_proj", 0), + ("gate_up_proj", "up_proj", 1), + ] + + params_dict = dict(self.named_parameters()) + + for name, loaded_weight in weights: + loaded_count += 1 + if name.startswith("model.visual."): + name = "visual." + name[len("model.visual."):] + if "attn.qkv.weight" in name: + num_heads = self.config.vision_config.num_heads + hidden_size = self.config.vision_config.hidden_size + head_size = hidden_size // num_heads + loaded_weight = loaded_weight.view( + 3, num_heads, head_size, hidden_size) + loaded_weight = loaded_weight.transpose(0, 1).reshape( + -1, hidden_size) + elif "attn.qkv.bias" in name: + num_heads = self.config.vision_config.num_heads + hidden_size = self.config.vision_config.hidden_size + head_size = hidden_size // num_heads + loaded_weight = loaded_weight.view( + 3, num_heads, head_size) + loaded_weight = loaded_weight.transpose(0, 1).reshape(-1) + if name not in params_dict: + raise ValueError(f"unexpected Qwen3.6 vision weight: {name}") + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", + default_weight_loader) + weight_loader(param, loaded_weight) + vision_loaded_count += 1 + continue + + # MTP is not used by the fixed evaluator command. + if (name.startswith("mtp.") + or name.startswith("model.mtp")): + continue + + # Prefix remapping for VL checkpoint (Qwen3_5MoeForConditionalGeneration): + # model.language_model.model.{layers,embed_tokens,norm} -> model.{...} + # model.language_model.lm_head -> lm_head + # Prefix remapping: checkpoint may wrap under language_model + if name.startswith("model.language_model."): + name = "model." + name[len("model.language_model."):] + + if "rotary_emb.inv_freq" in name: + continue + + if _load_full_attention_qgkv_weight( + params_dict, name, loaded_weight, self.text_cfg): + continue + + if _load_gdn_projection_weight( + params_dict, name, loaded_weight, self.text_cfg): + continue + + if name.endswith(".mlp.gate.weight"): + fused_name = name[:-len("gate.weight")] \ + + "router_shared_gate.weight" + if fused_name not in params_dict: + raise ValueError( + f"missing fused router/shared gate: {fused_name}") + params_dict[fused_name].weight_loader( + params_dict[fused_name], loaded_weight, 0) + continue + + if name.endswith(".mlp.shared_expert_gate.weight"): + fused_name = name[:-len("shared_expert_gate.weight")] \ + + "router_shared_gate.weight" + if fused_name not in params_dict: + raise ValueError( + f"missing fused router/shared gate: {fused_name}") + params_dict[fused_name].weight_loader( + params_dict[fused_name], loaded_weight, 1) + continue + + if ".linear_attn.conv1d.weight" in name: + name = name.replace(".linear_attn.conv1d.weight", + ".linear_attn.conv1d_weight") + + # --- Fused routed-expert weights (all experts in one tensor) --- + + if "mlp.experts.gate_up_proj" in name: + # loaded_weight: (num_experts, 2*intermediate, hidden) + w13_name = name.replace("mlp.experts.gate_up_proj", + "mlp.experts.w13_weight") + if w13_name not in params_dict: + continue + param = params_dict[w13_name] + n_exp = loaded_weight.shape[0] + inter = loaded_weight.shape[1] // 2 + gate_w = loaded_weight[:, :inter, :].contiguous() + up_w = loaded_weight[:, inter:, :].contiguous() + for eid in range(n_exp): + param.weight_loader(param, gate_w[eid], "w1_weight", "w1", eid) + param.weight_loader(param, up_w[eid], "w3_weight", "w3", eid) + continue + + if "mlp.experts.down_proj" in name: + # loaded_weight: (num_experts, hidden, intermediate) + w2_name = name.replace("mlp.experts.down_proj", + "mlp.experts.w2_weight") + if w2_name not in params_dict: + continue + param = params_dict[w2_name] + n_exp = loaded_weight.shape[0] + for eid in range(n_exp): + param.weight_loader(param, loaded_weight[eid], "w2_weight", "w2", eid) + continue + + # --- Shared expert down_proj rename --- + if "mlp.shared_expert.down_proj" in name: + name = name.replace("mlp.shared_expert.down_proj", + "mlp.shared_expert_down") + if name not in params_dict: + continue + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + continue + + # --- Individual expert weights (FT checkpoint: experts.{i}.{proj}.weight) --- + # Standard transformers fine-tuning saves each expert separately instead of + # the pre-merged (num_experts, ...) tensors in the original checkpoint. + if ".mlp.experts." in name: + parts = name.split(".mlp.experts.", 1) + expert_rest = parts[1] # e.g. "0.gate_proj.weight" + dot_pos = expert_rest.find(".") + if dot_pos > 0 and expert_rest[:dot_pos].isdigit(): + eid = int(expert_rest[:dot_pos]) + proj_raw = expert_rest[dot_pos + 1:] + proj = proj_raw[:-7] if proj_raw.endswith(".weight") else proj_raw + prefix = parts[0] # e.g. "model.layers.0" + if proj == "gate_proj": + w13_name = f"{prefix}.mlp.experts.w13_weight" + if w13_name in params_dict: + param = params_dict[w13_name] + param.weight_loader(param, loaded_weight, "w1_weight", "w1", eid) + elif proj == "up_proj": + w13_name = f"{prefix}.mlp.experts.w13_weight" + if w13_name in params_dict: + param = params_dict[w13_name] + param.weight_loader(param, loaded_weight, "w3_weight", "w3", eid) + elif proj == "down_proj": + w2_name = f"{prefix}.mlp.experts.w2_weight" + if w2_name in params_dict: + param = params_dict[w2_name] + param.weight_loader(param, loaded_weight, "w2_weight", "w2", eid) + continue + + # --- Stacked / standard weights --- + for param_name, weight_name, shard_id in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_name, param_name) + if name not in params_dict: + break + param = params_dict[name] + param.weight_loader(param, loaded_weight, shard_id) + break + else: + if name not in params_dict: + continue + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + _bi100_model_trace( + f"MoE load_weights complete items={loaded_count} " + f"vision_items={vision_loaded_count}") diff --git a/qwen3_6_scripts/qwen3_5/__init__.py b/qwen3_6_scripts/qwen3_5/__init__.py new file mode 100644 index 0000000..6988a59 --- /dev/null +++ b/qwen3_6_scripts/qwen3_5/__init__.py @@ -0,0 +1,3 @@ +from .configuration_qwen3_5 import Qwen3_5Config, Qwen3_5TextConfig, Qwen3_5VisionConfig + +__all__ = ["Qwen3_5Config", "Qwen3_5TextConfig", "Qwen3_5VisionConfig"] diff --git a/qwen3_6_scripts/qwen3_5/configuration_qwen3_5.py b/qwen3_6_scripts/qwen3_5/configuration_qwen3_5.py new file mode 100644 index 0000000..a12d246 --- /dev/null +++ b/qwen3_6_scripts/qwen3_5/configuration_qwen3_5.py @@ -0,0 +1,242 @@ +# Adapted from transformers 5.2.0 for compatibility with transformers 4.55.3 + torch 2.1.0 +# Stubs layer_type_validation and RopeParameters which do not exist in 4.55.3 + +import os +from typing import Optional, List + +from ...configuration_utils import PretrainedConfig as PreTrainedConfig + +# --- Local stubs for APIs not present in transformers 4.55.3 --- +# Always use these definitions; do NOT import from the older transformers +# as same-named functions there have incompatible signatures. + +def layer_type_validation(layer_types, num_hidden_layers=None, attention=True): + allowed = {"full_attention", "linear_attention"} + if not all(lt in allowed for lt in layer_types): + raise ValueError(f"layer_types entries must be in {allowed}, got {layer_types}") + if num_hidden_layers is not None and num_hidden_layers != len(layer_types): + raise ValueError( + f"num_hidden_layers ({num_hidden_layers}) != len(layer_types) ({len(layer_types)})" + ) + + +HYBRID_KV_ACCOUNTING_ENV = "BI100_HYBRID_KV_ACCOUNTING" +HYBRID_KV_ACCOUNTING_CONFIG = "bi100_hybrid_kv_accounting_mode" +LEGACY_KV_ACCOUNTING = "legacy40" +FULL_ATTENTION_KV_ACCOUNTING = "full_attention" + + +def _hybrid_kv_accounting_mode(environ=None, serialized_mode=None): + source = os.environ if environ is None else environ + environment_mode = source.get(HYBRID_KV_ACCOUNTING_ENV) + if (environment_mode is not None and serialized_mode is not None + and environment_mode != serialized_mode): + raise RuntimeError( + f"{HYBRID_KV_ACCOUNTING_ENV}={environment_mode!r} conflicts " + f"with serialized {HYBRID_KV_ACCOUNTING_CONFIG}=" + f"{serialized_mode!r}") + mode = environment_mode or serialized_mode or LEGACY_KV_ACCOUNTING + if mode not in (LEGACY_KV_ACCOUNTING, FULL_ATTENTION_KV_ACCOUNTING): + raise RuntimeError( + f"{HYBRID_KV_ACCOUNTING_ENV} must be " + f"'{LEGACY_KV_ACCOUNTING}' or " + f"'{FULL_ATTENTION_KV_ACCOUNTING}', got {mode!r}") + return mode + + +def _vllm_layers_block_type( + layer_types, + environ=None, + serialized_mode=None, +): + """Expose hybrid-layer ownership in the form vLLM 0.6.3 consumes.""" + mode = _hybrid_kv_accounting_mode(environ, serialized_mode) + if mode == LEGACY_KV_ACCOUNTING: + return ["attention"] * len(layer_types) + return [ + "attention" if layer_type == "full_attention" else layer_type + for layer_type in layer_types + ] + +try: + from typing import TypedDict +except ImportError: + RopeParameters = dict +else: + class RopeParameters(TypedDict, total=False): + rope_theta: float + rope_type: str + partial_rotary_factor: float + factor: float + +# --- End stubs --- + + +class Qwen3_5TextConfig(PreTrainedConfig): + r""" + Configuration for the text backbone of Qwen3.5 / Qwen3.6-35B-A3B models. + model_type is "qwen3_5_text" (used internally by the nested config). + """ + + model_type = "qwen3_5_text" + keys_to_ignore_at_inference = ["past_key_values"] + + def __init__( + self, + vocab_size=248320, + hidden_size=4096, + intermediate_size=12288, + num_hidden_layers=32, + num_attention_heads=16, + num_key_value_heads=4, + hidden_act="silu", + max_position_embeddings=32768, + initializer_range=0.02, + rms_norm_eps=1e-6, + use_cache=True, + tie_word_embeddings=False, + rope_parameters=None, + attention_bias=False, + attention_dropout=0.0, + head_dim=256, + linear_conv_kernel_dim=4, + linear_key_head_dim=128, + linear_value_head_dim=128, + linear_num_key_heads=16, + linear_num_value_heads=32, + layer_types=None, + pad_token_id=None, + bos_token_id=None, + eos_token_id=None, + **kwargs, + ): + self.pad_token_id = pad_token_id + self.bos_token_id = bos_token_id + self.eos_token_id = eos_token_id + self.tie_word_embeddings = tie_word_embeddings + self.vocab_size = vocab_size + self.max_position_embeddings = max_position_embeddings + self.hidden_size = hidden_size + self.intermediate_size = intermediate_size + self.num_hidden_layers = num_hidden_layers + self.num_attention_heads = num_attention_heads + self.num_key_value_heads = num_key_value_heads + self.hidden_act = hidden_act + self.initializer_range = initializer_range + self.rms_norm_eps = rms_norm_eps + self.use_cache = use_cache + self.attention_bias = attention_bias + self.attention_dropout = attention_dropout + self.head_dim = head_dim + self.rope_parameters = rope_parameters + kwargs.setdefault("partial_rotary_factor", 0.25) + + self.layer_types = layer_types + if self.layer_types is None: + interval_pattern = kwargs.get("full_attention_interval", 4) + self.layer_types = [ + "linear_attention" if bool((i + 1) % interval_pattern) else "full_attention" + for i in range(self.num_hidden_layers) + ] + layer_type_validation(self.layer_types, self.num_hidden_layers) + + self.linear_conv_kernel_dim = linear_conv_kernel_dim + self.linear_key_head_dim = linear_key_head_dim + self.linear_value_head_dim = linear_value_head_dim + self.linear_num_key_heads = linear_num_key_heads + self.linear_num_value_heads = linear_num_value_heads + super().__init__(**kwargs) + + +class Qwen3_5VisionConfig(PreTrainedConfig): + model_type = "qwen3_5_vision" + + def __init__( + self, + depth=27, + hidden_size=1152, + hidden_act="gelu_pytorch_tanh", + intermediate_size=4304, + num_heads=16, + in_channels=3, + patch_size=16, + spatial_merge_size=2, + temporal_patch_size=2, + out_hidden_size=3584, + num_position_embeddings=2304, + initializer_range=0.02, + **kwargs, + ): + super().__init__(**kwargs) + self.depth = depth + self.hidden_size = hidden_size + self.hidden_act = hidden_act + self.intermediate_size = intermediate_size + self.num_heads = num_heads + self.in_channels = in_channels + self.patch_size = patch_size + self.spatial_merge_size = spatial_merge_size + self.temporal_patch_size = temporal_patch_size + self.out_hidden_size = out_hidden_size + self.num_position_embeddings = num_position_embeddings + self.initializer_range = initializer_range + + +class Qwen3_5Config(PreTrainedConfig): + r""" + Top-level configuration for Qwen3.5 / Qwen3.6-35B-A3B. + model_type = "qwen3_5" matches the model card / config.json. + Wraps Qwen3_5TextConfig (and optionally Qwen3_5VisionConfig for multimodal use). + For vLLM text-only inference only text_config is consumed. + """ + + model_type = "qwen3_5" + keys_to_ignore_at_inference = ["past_key_values"] + + def __init__( + self, + text_config=None, + vision_config=None, + image_token_id=248056, + video_token_id=248057, + vision_start_token_id=248053, + vision_end_token_id=248054, + tie_word_embeddings=False, + **kwargs, + ): + serialized_mode = kwargs.pop(HYBRID_KV_ACCOUNTING_CONFIG, None) + serialized_layers = kwargs.pop("layers_block_type", None) + if isinstance(text_config, dict): + self.text_config = Qwen3_5TextConfig(**text_config) + elif text_config is None: + self.text_config = Qwen3_5TextConfig() + else: + self.text_config = text_config + + if isinstance(vision_config, dict): + self.vision_config = Qwen3_5VisionConfig(**vision_config) + elif vision_config is None: + self.vision_config = Qwen3_5VisionConfig() + else: + self.vision_config = vision_config + + self.image_token_id = image_token_id + self.video_token_id = video_token_id + self.vision_start_token_id = vision_start_token_id + self.vision_end_token_id = vision_end_token_id + self.tie_word_embeddings = tie_word_embeddings + super().__init__(**kwargs) + mode = _hybrid_kv_accounting_mode( + serialized_mode=serialized_mode) + layers_block_type = _vllm_layers_block_type( + self.text_config.layer_types, serialized_mode=mode) + if (serialized_layers is not None + and list(serialized_layers) != layers_block_type): + raise RuntimeError( + "serialized layers_block_type conflicts with " + f"{HYBRID_KV_ACCOUNTING_CONFIG}={mode!r}") + setattr(self, HYBRID_KV_ACCOUNTING_CONFIG, mode) + self.layers_block_type = layers_block_type + + +__all__ = ["Qwen3_5Config", "Qwen3_5TextConfig", "Qwen3_5VisionConfig"] diff --git a/qwen3_6_scripts/qwen3_5_moe/__init__.py b/qwen3_6_scripts/qwen3_5_moe/__init__.py new file mode 100644 index 0000000..1c27df9 --- /dev/null +++ b/qwen3_6_scripts/qwen3_5_moe/__init__.py @@ -0,0 +1,3 @@ +from .configuration_qwen3_5_moe import Qwen3_5MoeConfig, Qwen3_5MoeTextConfig + +__all__ = ["Qwen3_5MoeConfig", "Qwen3_5MoeTextConfig"] diff --git a/qwen3_6_scripts/qwen3_5_moe/configuration_qwen3_5_moe.py b/qwen3_6_scripts/qwen3_5_moe/configuration_qwen3_5_moe.py new file mode 100644 index 0000000..34667d8 --- /dev/null +++ b/qwen3_6_scripts/qwen3_5_moe/configuration_qwen3_5_moe.py @@ -0,0 +1,252 @@ +# Adapted from transformers 5.2.0 for compatibility with transformers 4.55.3 + torch 2.1.0 +# Source: transformers/models/qwen3_5_moe/configuration_qwen3_5_moe.py +# Stubs layer_type_validation and RopeParameters which do not exist in 4.55.3 +# Removes ignore_keys_at_rope_validation / base_model_tp_plan / base_model_pp_plan +# which are 5.x-only and irrelevant for vLLM inference. + +import os +from typing import Optional + +from ...configuration_utils import PretrainedConfig as PreTrainedConfig + +# --- Local stubs for APIs not present in transformers 4.55.3 --- +def layer_type_validation(layer_types, num_hidden_layers=None, attention=True): + allowed = {"full_attention", "linear_attention"} + if not all(lt in allowed for lt in layer_types): + raise ValueError(f"layer_types entries must be in {allowed}, got {layer_types}") + if num_hidden_layers is not None and num_hidden_layers != len(layer_types): + raise ValueError( + f"num_hidden_layers ({num_hidden_layers}) != len(layer_types) ({len(layer_types)})" + ) + + +HYBRID_KV_ACCOUNTING_ENV = "BI100_HYBRID_KV_ACCOUNTING" +HYBRID_KV_ACCOUNTING_CONFIG = "bi100_hybrid_kv_accounting_mode" +LEGACY_KV_ACCOUNTING = "legacy40" +FULL_ATTENTION_KV_ACCOUNTING = "full_attention" + + +def _hybrid_kv_accounting_mode(environ=None, serialized_mode=None): + source = os.environ if environ is None else environ + environment_mode = source.get(HYBRID_KV_ACCOUNTING_ENV) + if (environment_mode is not None and serialized_mode is not None + and environment_mode != serialized_mode): + raise RuntimeError( + f"{HYBRID_KV_ACCOUNTING_ENV}={environment_mode!r} conflicts " + f"with serialized {HYBRID_KV_ACCOUNTING_CONFIG}=" + f"{serialized_mode!r}") + mode = environment_mode or serialized_mode or LEGACY_KV_ACCOUNTING + if mode not in (LEGACY_KV_ACCOUNTING, FULL_ATTENTION_KV_ACCOUNTING): + raise RuntimeError( + f"{HYBRID_KV_ACCOUNTING_ENV} must be " + f"'{LEGACY_KV_ACCOUNTING}' or " + f"'{FULL_ATTENTION_KV_ACCOUNTING}', got {mode!r}") + return mode + + +def _vllm_layers_block_type( + layer_types, + environ=None, + serialized_mode=None, +): + """Expose hybrid-layer ownership in the form vLLM 0.6.3 consumes.""" + mode = _hybrid_kv_accounting_mode(environ, serialized_mode) + if mode == LEGACY_KV_ACCOUNTING: + return ["attention"] * len(layer_types) + return [ + "attention" if layer_type == "full_attention" else layer_type + for layer_type in layer_types + ] + +try: + from typing import TypedDict +except ImportError: + RopeParameters = dict +else: + class RopeParameters(TypedDict, total=False): + rope_theta: float + rope_type: str + partial_rotary_factor: float + factor: float + +# --- End stubs --- + + +class Qwen3_5MoeTextConfig(PreTrainedConfig): + r""" + Configuration for the text backbone of Qwen3.5-MoE / Qwen3.6-35B-A3B models. + model_type is "qwen3_5_moe_text" (used internally by the nested config). + """ + + model_type = "qwen3_5_moe_text" + keys_to_ignore_at_inference = ["past_key_values"] + + def __init__( + self, + vocab_size=248320, + hidden_size=2048, + num_hidden_layers=40, + num_attention_heads=16, + num_key_value_heads=2, + hidden_act="silu", + max_position_embeddings=32768, + initializer_range=0.02, + rms_norm_eps=1e-6, + use_cache=True, + tie_word_embeddings=False, + rope_parameters=None, + attention_bias=False, + attention_dropout=0.0, + head_dim=256, + linear_conv_kernel_dim=4, + linear_key_head_dim=128, + linear_value_head_dim=128, + linear_num_key_heads=16, + linear_num_value_heads=32, + moe_intermediate_size=512, + shared_expert_intermediate_size=512, + num_experts_per_tok=8, + num_experts=256, + output_router_logits=False, + router_aux_loss_coef=0.001, + layer_types=None, + pad_token_id=None, + bos_token_id=None, + eos_token_id=None, + **kwargs, + ): + self.pad_token_id = pad_token_id + self.bos_token_id = bos_token_id + self.eos_token_id = eos_token_id + self.tie_word_embeddings = tie_word_embeddings + self.vocab_size = vocab_size + self.max_position_embeddings = max_position_embeddings + self.hidden_size = hidden_size + self.num_hidden_layers = num_hidden_layers + self.num_attention_heads = num_attention_heads + self.num_key_value_heads = num_key_value_heads + self.hidden_act = hidden_act + self.initializer_range = initializer_range + self.rms_norm_eps = rms_norm_eps + self.use_cache = use_cache + self.attention_bias = attention_bias + self.attention_dropout = attention_dropout + self.head_dim = head_dim + self.rope_parameters = rope_parameters + kwargs.setdefault("partial_rotary_factor", 0.25) + + self.layer_types = layer_types + if self.layer_types is None: + interval_pattern = kwargs.get("full_attention_interval", 4) + self.layer_types = [ + "linear_attention" if bool((i + 1) % interval_pattern) else "full_attention" + for i in range(self.num_hidden_layers) + ] + layer_type_validation(self.layer_types, self.num_hidden_layers) + + self.linear_conv_kernel_dim = linear_conv_kernel_dim + self.linear_key_head_dim = linear_key_head_dim + self.linear_value_head_dim = linear_value_head_dim + self.linear_num_key_heads = linear_num_key_heads + self.linear_num_value_heads = linear_num_value_heads + self.moe_intermediate_size = moe_intermediate_size + self.shared_expert_intermediate_size = shared_expert_intermediate_size + self.num_experts_per_tok = num_experts_per_tok + self.num_experts = num_experts + self.output_router_logits = output_router_logits + self.router_aux_loss_coef = router_aux_loss_coef + super().__init__(**kwargs) + + +class Qwen3_5MoeVisionConfig(PreTrainedConfig): + model_type = "qwen3_5_moe" + + def __init__( + self, + depth=27, + hidden_size=1152, + hidden_act="gelu_pytorch_tanh", + intermediate_size=4304, + num_heads=16, + in_channels=3, + patch_size=16, + spatial_merge_size=2, + temporal_patch_size=2, + out_hidden_size=3584, + num_position_embeddings=2304, + initializer_range=0.02, + **kwargs, + ): + super().__init__(**kwargs) + self.depth = depth + self.hidden_size = hidden_size + self.hidden_act = hidden_act + self.intermediate_size = intermediate_size + self.num_heads = num_heads + self.in_channels = in_channels + self.patch_size = patch_size + self.spatial_merge_size = spatial_merge_size + self.temporal_patch_size = temporal_patch_size + self.out_hidden_size = out_hidden_size + self.num_position_embeddings = num_position_embeddings + self.initializer_range = initializer_range + + +class Qwen3_5MoeConfig(PreTrainedConfig): + r""" + Top-level configuration for Qwen3.5-MoE / Qwen3.6-35B-A3B. + model_type = "qwen3_5_moe" matches the model card / config.json. + Wraps Qwen3_5MoeTextConfig (and optionally Qwen3_5MoeVisionConfig). + For vLLM text-only inference only text_config is consumed. + """ + + model_type = "qwen3_5_moe" + keys_to_ignore_at_inference = ["past_key_values"] + + def __init__( + self, + text_config=None, + vision_config=None, + image_token_id=248056, + video_token_id=248057, + vision_start_token_id=248053, + vision_end_token_id=248054, + tie_word_embeddings=False, + **kwargs, + ): + serialized_mode = kwargs.pop(HYBRID_KV_ACCOUNTING_CONFIG, None) + serialized_layers = kwargs.pop("layers_block_type", None) + if isinstance(text_config, dict): + self.text_config = Qwen3_5MoeTextConfig(**text_config) + elif text_config is None: + self.text_config = Qwen3_5MoeTextConfig() + else: + self.text_config = text_config + + if isinstance(vision_config, dict): + self.vision_config = Qwen3_5MoeVisionConfig(**vision_config) + elif vision_config is None: + self.vision_config = Qwen3_5MoeVisionConfig() + else: + self.vision_config = vision_config + + self.image_token_id = image_token_id + self.video_token_id = video_token_id + self.vision_start_token_id = vision_start_token_id + self.vision_end_token_id = vision_end_token_id + self.tie_word_embeddings = tie_word_embeddings + super().__init__(**kwargs) + mode = _hybrid_kv_accounting_mode( + serialized_mode=serialized_mode) + layers_block_type = _vllm_layers_block_type( + self.text_config.layer_types, serialized_mode=mode) + if (serialized_layers is not None + and list(serialized_layers) != layers_block_type): + raise RuntimeError( + "serialized layers_block_type conflicts with " + f"{HYBRID_KV_ACCOUNTING_CONFIG}={mode!r}") + setattr(self, HYBRID_KV_ACCOUNTING_CONFIG, mode) + self.layers_block_type = layers_block_type + + +__all__ = ["Qwen3_5MoeConfig", "Qwen3_5MoeTextConfig"] diff --git a/qwen3_6_scripts/qwen3coder_tool_parser.py b/qwen3_6_scripts/qwen3coder_tool_parser.py new file mode 100644 index 0000000..04cbc6f --- /dev/null +++ b/qwen3_6_scripts/qwen3coder_tool_parser.py @@ -0,0 +1,519 @@ +import ast +import json +import uuid +from typing import Any, Dict, List, Optional, Sequence, Union + +import regex as re + +from vllm.entrypoints.openai.protocol import (ChatCompletionRequest, + ChatCompletionToolsParam, + DeltaFunctionCall, DeltaMessage, + DeltaToolCall, + ExtractedToolCallInformation, + FunctionCall, ToolCall) +from vllm.entrypoints.openai.tool_parsers.abstract_tool_parser import ( + ToolParser, ToolParserManager) +from vllm.logger import init_logger +from vllm.transformers_utils.tokenizer import AnyTokenizer + +logger = init_logger(__name__) + + +@ToolParserManager.register_module("qwen3_coder") +class Qwen3CoderToolParser(ToolParser): + """ + Tool parser for Qwen3 models using XML-style tool call format: + + value + + + Port of vllm-original qwen3coder_tool_parser.py to vllm 0.6.3 API. + """ + + def __init__(self, tokenizer: AnyTokenizer): + super().__init__(tokenizer) + + self.current_tool_name_sent: bool = False + self.prev_tool_call_arr: List[Dict] = [] + # Base class uses int; we override with string IDs + self.current_tool_id: Optional[str] = None # type: ignore[assignment] + self.streamed_args_for_tool: List[str] = [] + + self.tool_call_start_token: str = "" + self.tool_call_end_token: str = "" + self.tool_call_prefix: str = "(.*?)", re.DOTALL) + self.tool_call_regex = re.compile( + r"(.*?)|(.*?)$", re.DOTALL) + self.tool_call_function_regex = re.compile( + r"||(?=)|$)", + re.DOTALL) + + if not self.model_tokenizer: + raise ValueError( + "The model tokenizer must be passed to the ToolParser " + "constructor during construction.") + + self.tool_call_start_token_id = self.vocab.get( + self.tool_call_start_token) + self.tool_call_end_token_id = self.vocab.get(self.tool_call_end_token) + + if (self.tool_call_start_token_id is None + or self.tool_call_end_token_id is None): + raise RuntimeError( + "Qwen3 XML Tool parser could not locate tool call start/end " + "tokens in the tokenizer!") + + logger.debug("vLLM Successfully imported tool parser %s !", + self.__class__.__name__) + + + def _generate_tool_call_id(self) -> str: + return f"call_{uuid.uuid4().hex[:24]}" + + def _reset_streaming_state(self) -> None: + self.current_tool_index = 0 + self.is_tool_call_started = False + self.header_sent = False + self.current_tool_id = None + self.current_function_name: Optional[str] = None + self.current_param_name: Optional[str] = None + self.current_param_value: str = "" + self.param_count = 0 + self.in_param = False + self.in_function = False + self.accumulated_text: str = "" + self.json_started = False + self.json_closed = False + self.accumulated_params: Dict[str, Any] = {} + self.streaming_request: Optional[ChatCompletionRequest] = None + + def _get_arguments_config( + self, func_name: str, + tools: Optional[List[ChatCompletionToolsParam]]) -> Dict: + if tools is None: + return {} + for config in tools: + if not hasattr(config, "type") or not ( + hasattr(config, "function") + and hasattr(config.function, "name")): + continue + if config.type == "function" and config.function.name == func_name: + if not hasattr(config.function, "parameters"): + return {} + params = config.function.parameters + if isinstance(params, dict) and "properties" in params: + return params["properties"] + elif isinstance(params, dict): + return params + else: + return {} + logger.debug("Tool '%s' is not defined in the tools list.", func_name) + return {} + + def _convert_param_value(self, param_value: str, param_name: str, + param_config: Dict, func_name: str) -> Any: + if param_value.lower() == "null": + return None + + if param_name not in param_config: + if param_config != {}: + logger.debug( + "Parsed parameter '%s' is not defined in tool '%s', " + "returning string value.", param_name, func_name) + return param_value + + if (isinstance(param_config[param_name], dict) + and "type" in param_config[param_name]): + param_type = str( + param_config[param_name]["type"]).strip().lower() + else: + param_type = "string" + + if param_type in ["string", "str", "text", "varchar", "char", "enum"]: + return param_value + elif (param_type.startswith("int") or param_type.startswith("uint") + or param_type.startswith("long") + or param_type.startswith("short") + or param_type.startswith("unsigned")): + try: + return int(param_value) + except (ValueError, TypeError): + return param_value + elif param_type.startswith("num") or param_type.startswith("float"): + try: + v = float(param_value) + return int(v) if v - int(v) == 0 else v + except (ValueError, TypeError): + return param_value + elif param_type in ["boolean", "bool", "binary"]: + lower = param_value.lower() + if lower not in ["true", "false"]: + logger.debug( + "Parameter '%s' value '%s' is not boolean in tool '%s'.", + param_name, param_value, func_name) + return lower == "true" + else: + if (param_type in ["object", "array", "arr"] + or param_type.startswith("dict") + or param_type.startswith("list")): + try: + return json.loads(param_value) + except (json.JSONDecodeError, TypeError, ValueError): + logger.debug( + "Could not JSON-decode parameter '%s' for tool '%s'; " + "falling back to literal evaluation.", + param_name, + func_name, + exc_info=True) + try: + return ast.literal_eval(param_value) + except (ValueError, SyntaxError, TypeError): + logger.debug( + "Could not literal-eval parameter '%s' for tool '%s'; " + "returning string value.", + param_name, + func_name, + exc_info=True) + return param_value + + def _parse_xml_function_call( + self, function_call_str: str, + tools: Optional[List[ChatCompletionToolsParam]]) -> ToolCall: + end_index = function_call_str.index(">") + function_name = function_call_str[:end_index] + param_config = self._get_arguments_config(function_name, tools) + parameters = function_call_str[end_index + 1:] + param_dict: Dict[str, Any] = {} + for match_text in self.tool_call_parameter_regex.findall(parameters): + idx = match_text.index(">") + param_name = match_text[:idx] + param_value = str(match_text[idx + 1:]) + if param_value.startswith("\n"): + param_value = param_value[1:] + if param_value.endswith("\n"): + param_value = param_value[:-1] + param_dict[param_name] = self._convert_param_value( + param_value, param_name, param_config, function_name) + return ToolCall( + type="function", + function=FunctionCall( + name=function_name, + arguments=json.dumps(param_dict, ensure_ascii=False))) + + def _get_function_calls(self, model_output: str) -> List[str]: + matched_ranges = self.tool_call_regex.findall(model_output) + raw_tool_calls = [ + match[0] if match[0] else match[1] for match in matched_ranges + ] + if not raw_tool_calls: + raw_tool_calls = [model_output] + raw_function_calls: List[tuple] = [] + for tool_call in raw_tool_calls: + raw_function_calls.extend( + self.tool_call_function_regex.findall(tool_call)) + return [match[0] if match[0] else match[1] + for match in raw_function_calls] + + def extract_tool_calls( + self, model_output: str, + request: ChatCompletionRequest) -> ExtractedToolCallInformation: + if self.tool_call_prefix not in model_output: + return ExtractedToolCallInformation(tools_called=False, + tool_calls=[], + content=model_output) + try: + function_calls = self._get_function_calls(model_output) + if not function_calls: + return ExtractedToolCallInformation(tools_called=False, + tool_calls=[], + content=model_output) + + tool_calls = [ + self._parse_xml_function_call(fc, request.tools) + for fc in function_calls + ] + + self.prev_tool_call_arr.clear() + for tc in tool_calls: + self.prev_tool_call_arr.append({ + "name": tc.function.name, + "arguments": tc.function.arguments, + }) + + content_index = model_output.find(self.tool_call_start_token) + idx = model_output.find(self.tool_call_prefix) + content_index = content_index if content_index >= 0 else idx + content = model_output[:content_index] + + return ExtractedToolCallInformation( + tools_called=bool(tool_calls), + tool_calls=tool_calls, + content=content if content else None, + ) + except Exception: + logger.exception("Error extracting tool call from response.") + return ExtractedToolCallInformation(tools_called=False, + tool_calls=[], + content=model_output) + + def extract_tool_calls_streaming( + self, + previous_text: str, + current_text: str, + delta_text: str, + previous_token_ids: Sequence[int], + current_token_ids: Sequence[int], + delta_token_ids: Sequence[int], + request: ChatCompletionRequest, + ) -> Union[DeltaMessage, None]: + if not previous_text: + self._reset_streaming_state() + self.streaming_request = request + + if not delta_text: + if delta_token_ids and self.tool_call_end_token_id not in delta_token_ids: + complete_calls = len( + self.tool_call_complete_regex.findall(current_text)) + if complete_calls > 0 and self.prev_tool_call_arr: + open_calls = ( + current_text.count(self.tool_call_start_token) - + current_text.count(self.tool_call_end_token)) + if open_calls == 0: + return DeltaMessage(content="") + elif not self.is_tool_call_started and current_text: + return DeltaMessage(content="") + return None + + self.accumulated_text = current_text + + if self.json_closed and not self.in_function: + tool_ends = current_text.count(self.tool_call_end_token) + if tool_ends > self.current_tool_index: + self.current_tool_index += 1 + self.header_sent = False + self.param_count = 0 + self.json_started = False + self.json_closed = False + self.accumulated_params = {} + tool_starts = current_text.count(self.tool_call_start_token) + if self.current_tool_index >= tool_starts: + self.is_tool_call_started = False + return None + + if not self.is_tool_call_started: + if (self.tool_call_start_token_id in delta_token_ids + or self.tool_call_start_token in delta_text): + self.is_tool_call_started = True + if self.tool_call_start_token in delta_text: + content_before = delta_text[:delta_text.index( + self.tool_call_start_token)] + if content_before: + return DeltaMessage(content=content_before) + return None + else: + if (current_text.rstrip().endswith(self.tool_call_end_token) + and delta_text.strip() == ""): + return None + return DeltaMessage(content=delta_text) + + tool_starts_count = current_text.count(self.tool_call_start_token) + if self.current_tool_index >= tool_starts_count: + return None + + # Locate the current tool call's text slice + tool_start_positions: List[int] = [] + search = 0 + while True: + search = current_text.find(self.tool_call_start_token, search) + if search == -1: + break + tool_start_positions.append(search) + search += len(self.tool_call_start_token) + + if self.current_tool_index >= len(tool_start_positions): + return None + + tool_start_idx = tool_start_positions[self.current_tool_index] + tool_end_idx = current_text.find(self.tool_call_end_token, + tool_start_idx) + if tool_end_idx == -1: + tool_text = current_text[tool_start_idx:] + else: + tool_text = current_text[tool_start_idx:tool_end_idx + + len(self.tool_call_end_token)] + + if not self.header_sent: + if self.tool_call_prefix in tool_text: + func_start = (tool_text.find(self.tool_call_prefix) + + len(self.tool_call_prefix)) + func_end = tool_text.find(">", func_start) + if func_end != -1: + self.current_function_name = tool_text[func_start:func_end] + self.current_tool_id = self._generate_tool_call_id() + self.header_sent = True + self.in_function = True + self.prev_tool_call_arr.append({ + "name": self.current_function_name, + "arguments": "{}", + }) + self.streamed_args_for_tool.append("") + return DeltaMessage(tool_calls=[ + DeltaToolCall( + index=self.current_tool_index, + id=self.current_tool_id, + function=DeltaFunctionCall( + name=self.current_function_name, + arguments=""), + type="function", + ) + ]) + return None + + if self.in_function: + if not self.json_started: + self.json_started = True + self.streamed_args_for_tool[self.current_tool_index] += "{" + return DeltaMessage(tool_calls=[ + DeltaToolCall( + index=self.current_tool_index, + function=DeltaFunctionCall(arguments="{"), + ) + ]) + + # Collect all complete parameters in one pass (speculative-decode safe) + param_starts: List[int] = [] + search = 0 + while True: + search = tool_text.find(self.parameter_prefix, search) + if search == -1: + break + param_starts.append(search) + search += len(self.parameter_prefix) + + json_fragments: List[str] = [] + while not self.in_param and self.param_count < len(param_starts): + param_idx = param_starts[self.param_count] + param_start = param_idx + len(self.parameter_prefix) + remaining = tool_text[param_start:] + + if ">" not in remaining: + break + + name_end = remaining.find(">") + current_param_name = remaining[:name_end] + value_start = param_start + name_end + 1 + value_text = tool_text[value_start:] + if value_text.startswith("\n"): + value_text = value_text[1:] + + param_end_idx = value_text.find(self.parameter_end_token) + if param_end_idx == -1: + next_param = value_text.find(self.parameter_prefix) + func_end = value_text.find(self.function_end_token) + if next_param != -1 and (func_end == -1 + or next_param < func_end): + param_end_idx = next_param + elif func_end != -1: + param_end_idx = func_end + else: + tool_end_in_value = value_text.find( + self.tool_call_end_token) + if tool_end_in_value != -1: + param_end_idx = tool_end_in_value + else: + break + + if param_end_idx == -1: + break + + param_value = value_text[:param_end_idx] + if param_value.endswith("\n"): + param_value = param_value[:-1] + + self.accumulated_params[current_param_name] = param_value + param_config = self._get_arguments_config( + self.current_function_name or "", + self.streaming_request.tools + if self.streaming_request else None) + converted = self._convert_param_value( + param_value, current_param_name, param_config, + self.current_function_name or "") + serialized = json.dumps(converted, ensure_ascii=False) + + sep = "" if self.param_count == 0 else ", " + key = json.dumps(current_param_name, ensure_ascii=False) + json_fragments.append(f"{sep}{key}: {serialized}") + self.param_count += 1 + + if json_fragments: + combined = "".join(json_fragments) + if self.current_tool_index < len(self.streamed_args_for_tool): + self.streamed_args_for_tool[ + self.current_tool_index] += combined + else: + logger.warning( + "streamed_args_for_tool out of sync: index=%d len=%d", + self.current_tool_index, + len(self.streamed_args_for_tool)) + return DeltaMessage(tool_calls=[ + DeltaToolCall( + index=self.current_tool_index, + function=DeltaFunctionCall(arguments=combined), + ) + ]) + + # Emit closing brace when is seen (after params are done) + if not self.json_closed and self.function_end_token in tool_text: + self.json_closed = True + func_start = (tool_text.find(self.tool_call_prefix) + + len(self.tool_call_prefix)) + func_content_end = tool_text.find(self.function_end_token, + func_start) + if func_content_end != -1: + try: + parsed_tool = self._parse_xml_function_call( + tool_text[func_start:func_content_end], + self.streaming_request.tools + if self.streaming_request else None) + if self.current_tool_index < len( + self.prev_tool_call_arr): + self.prev_tool_call_arr[ + self.current_tool_index]["arguments"] = ( + parsed_tool.function.arguments) + except Exception: + logger.debug("Failed to parse tool call during " + "streaming: %s", + tool_text, + exc_info=True) + + if self.current_tool_index < len(self.streamed_args_for_tool): + self.streamed_args_for_tool[ + self.current_tool_index] += "}" + else: + logger.warning( + "streamed_args_for_tool out of sync: index=%d len=%d", + self.current_tool_index, + len(self.streamed_args_for_tool)) + + result = DeltaMessage(tool_calls=[ + DeltaToolCall( + index=self.current_tool_index, + function=DeltaFunctionCall(arguments="}"), + ) + ]) + self.in_function = False + self.accumulated_params = {} + return result + + return None diff --git a/qwen3_6_scripts/reasoning/__init__.py b/qwen3_6_scripts/reasoning/__init__.py new file mode 100644 index 0000000..b32c3f5 --- /dev/null +++ b/qwen3_6_scripts/reasoning/__init__.py @@ -0,0 +1,16 @@ +""" +Reasoning parser module for vLLM 0.6.3 (BI-V100 / Qwen3.6-35B-A3B adaptation). + +Usage: --reasoning-parser qwen3 +""" + +from vllm.reasoning.abs_reasoning_parsers import ReasoningParser, ReasoningParserManager + +__all__ = ["ReasoningParser", "ReasoningParserManager"] + +# Lazy-register Qwen3 parser; imported on first get_reasoning_parser("qwen3"). +ReasoningParserManager.register_lazy( + "qwen3", + "vllm.reasoning.qwen3_reasoning_parser", + "Qwen3ReasoningParser", +) diff --git a/qwen3_6_scripts/reasoning/abs_reasoning_parsers.py b/qwen3_6_scripts/reasoning/abs_reasoning_parsers.py new file mode 100644 index 0000000..8d91f7f --- /dev/null +++ b/qwen3_6_scripts/reasoning/abs_reasoning_parsers.py @@ -0,0 +1,243 @@ +""" +Abstract reasoning parser base classes for vLLM 0.6.3. +Adapted from vllm-original/vllm/reasoning/abs_reasoning_parsers.py: + - Removed vllm.entrypoints.mcp, vllm.utils.collection_utils, import_utils + - DeltaMessage from vllm 0.6.3 protocol path + - TokenizerLike -> AnyTokenizer + - ReasoningParserManager: simplified eager + lazy registration +""" + +import importlib +from abc import abstractmethod +from collections.abc import Iterable, Sequence +from functools import cached_property +from typing import Any, Optional, TYPE_CHECKING + +if TYPE_CHECKING: + from vllm.entrypoints.openai.protocol import DeltaMessage + from vllm.transformers_utils.tokenizer import AnyTokenizer +else: + DeltaMessage = Any + AnyTokenizer = Any + + +class ReasoningParser: + """Abstract base for all reasoning parsers.""" + + def __init__(self, tokenizer: "AnyTokenizer", *args, **kwargs): + self.model_tokenizer = tokenizer + + @cached_property + def vocab(self) -> dict: + return self.model_tokenizer.get_vocab() + + @abstractmethod + def is_reasoning_end(self, input_ids: Sequence[int]) -> bool: + """Return True once the reasoning block has closed in input_ids.""" + + def is_reasoning_end_streaming( + self, input_ids: Sequence[int], delta_ids: Iterable[int] + ) -> bool: + return self.is_reasoning_end(input_ids) + + @abstractmethod + def extract_content_ids(self, input_ids: list) -> list: + """Return token ids that belong to the content (post-reasoning) part.""" + + def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int: + return 0 + + @abstractmethod + def extract_reasoning( + self, model_output: str, request: Any + ) -> "tuple[Optional[str], Optional[str]]": + """ + Split a complete model output into (reasoning_text, content_text). + Either part may be None. + """ + + @abstractmethod + def extract_reasoning_streaming( + self, + previous_text: str, + current_text: str, + delta_text: str, + previous_token_ids: Sequence[int], + current_token_ids: Sequence[int], + delta_token_ids: Sequence[int], + ) -> Optional["DeltaMessage"]: + """ + Extract reasoning from a streaming delta. + Returns a DeltaMessage with reasoning_content and/or content set, + or None if this delta should be suppressed (control token). + """ + + +class BaseThinkingReasoningParser(ReasoningParser): + """ + Base for parsers that use ... delimiters. + Subclasses define start_token / end_token properties. + """ + + @property + @abstractmethod + def start_token(self) -> str: + raise NotImplementedError + + @property + @abstractmethod + def end_token(self) -> str: + raise NotImplementedError + + def __init__(self, tokenizer: "AnyTokenizer", *args, **kwargs): + super().__init__(tokenizer, *args, **kwargs) + + if not self.model_tokenizer: + raise ValueError("Tokenizer must be passed to ReasoningParser.") + if not self.start_token or not self.end_token: + raise ValueError("start_token and end_token must be defined.") + + self.start_token_id: Optional[int] = self.vocab.get(self.start_token) + self.end_token_id: Optional[int] = self.vocab.get(self.end_token) + if self.start_token_id is None or self.end_token_id is None: + raise RuntimeError( + f"{self.__class__.__name__}: could not find think tokens " + f"'{self.start_token}'/'{self.end_token}' in tokenizer vocab." + ) + + def is_reasoning_end(self, input_ids: Sequence[int]) -> bool: + for token_id in reversed(input_ids): + if token_id == self.start_token_id: + return False + if token_id == self.end_token_id: + return True + return False + + def is_reasoning_end_streaming( + self, input_ids: Sequence[int], delta_ids: Iterable[int] + ) -> bool: + return self.end_token_id in delta_ids + + def extract_content_ids(self, input_ids: list) -> list: + if self.end_token_id not in input_ids[:-1]: + return [] + return input_ids[input_ids.index(self.end_token_id) + 1:] + + def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int: + count = 0 + depth = 0 + for tid in token_ids: + if tid == self.start_token_id: + depth += 1 + elif tid == self.end_token_id: + if depth > 0: + depth -= 1 + elif depth > 0: + count += 1 + return count + + def extract_reasoning( + self, model_output: str, request: Any + ) -> "tuple[Optional[str], Optional[str]]": + # Strip if the model generated it (old-style template). + parts = model_output.partition(self.start_token) + model_output = parts[2] if parts[1] else parts[0] + + if self.end_token not in model_output: + return model_output, None + reasoning, _, content = model_output.partition(self.end_token) + return reasoning, content or None + + def extract_reasoning_streaming( + self, + previous_text: str, + current_text: str, + delta_text: str, + previous_token_ids: Sequence[int], + current_token_ids: Sequence[int], + delta_token_ids: Sequence[int], + ) -> Optional["DeltaMessage"]: + from vllm.entrypoints.openai.protocol import DeltaMessage as _DeltaMessage + + # Suppress lone control tokens. + if len(delta_token_ids) == 1 and delta_token_ids[0] in ( + self.start_token_id, self.end_token_id + ): + return None + + start_in_prev = self.start_token_id in previous_token_ids + start_in_delta = self.start_token_id in delta_token_ids + end_in_prev = self.end_token_id in previous_token_ids + end_in_delta = self.end_token_id in delta_token_ids + + if start_in_prev: + if end_in_delta: + end_idx = delta_text.find(self.end_token) + reasoning = delta_text[:end_idx] if end_idx >= 0 else "" + content = delta_text[end_idx + len(self.end_token):] if end_idx >= 0 else None + return _DeltaMessage( + reasoning_content=reasoning or None, + content=content or None, + ) + elif end_in_prev: + return _DeltaMessage(content=delta_text) + else: + return _DeltaMessage(reasoning_content=delta_text) + + elif start_in_delta: + if end_in_delta: + start_idx = delta_text.find(self.start_token) + end_idx = delta_text.find(self.end_token) + reasoning = delta_text[start_idx + len(self.start_token):end_idx] + content = delta_text[end_idx + len(self.end_token):] + return _DeltaMessage( + reasoning_content=reasoning or None, + content=content or None, + ) + else: + return _DeltaMessage(reasoning_content=delta_text) + + else: + return _DeltaMessage(content=delta_text) + + +class ReasoningParserManager: + """ + Registry for ReasoningParser implementations. + Supports eager and lazy registration. + """ + + _parsers: dict = {} # name -> class (eager) + _lazy: dict = {} # name -> (module_path, class_name) + + @classmethod + def register_module(cls, name: str, parser_cls: type) -> None: + """Eagerly register a ReasoningParser class.""" + if not issubclass(parser_cls, ReasoningParser): + raise TypeError(f"{parser_cls} is not a ReasoningParser subclass.") + cls._parsers[name] = parser_cls + + @classmethod + def register_lazy(cls, name: str, module_path: str, class_name: str) -> None: + """Register a parser for deferred import.""" + cls._lazy[name] = (module_path, class_name) + + @classmethod + def get_reasoning_parser(cls, name: str) -> type: + if name in cls._parsers: + return cls._parsers[name] + if name in cls._lazy: + module_path, class_name = cls._lazy[name] + mod = importlib.import_module(module_path) + parser_cls = getattr(mod, class_name) + cls._parsers[name] = parser_cls + return parser_cls + registered = sorted(set(cls._parsers) | set(cls._lazy)) + raise KeyError( + f"Reasoning parser '{name}' not found. " + f"Available: {registered}" + ) + + @classmethod + def list_registered(cls) -> list: + return sorted(set(cls._parsers) | set(cls._lazy)) diff --git a/qwen3_6_scripts/reasoning/qwen3_reasoning_parser.py b/qwen3_6_scripts/reasoning/qwen3_reasoning_parser.py new file mode 100644 index 0000000..c6ad6ca --- /dev/null +++ b/qwen3_6_scripts/reasoning/qwen3_reasoning_parser.py @@ -0,0 +1,112 @@ +""" +Reasoning parser for Qwen3 / Qwen3.5 / Qwen3.6 model family. +Adapted from vllm-original/vllm/reasoning/qwen3_reasoning_parser.py. + +The model uses ... to wrap chain-of-thought output. +For Qwen3.5+ the chat template injects into the prompt, so only + appears in the generated tokens; older templates generate +themselves. Both styles are handled. +""" + +from typing import Optional, Sequence, Any + +from vllm.reasoning.abs_reasoning_parsers import ( + BaseThinkingReasoningParser, + ReasoningParserManager, +) + + +class Qwen3ReasoningParser(BaseThinkingReasoningParser): + + def __init__(self, tokenizer: Any, *args, **kwargs): + super().__init__(tokenizer, *args, **kwargs) + chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {} + self.thinking_enabled = chat_kwargs.get("enable_thinking", True) + + @property + def start_token(self) -> str: + return "" + + @property + def end_token(self) -> str: + return "" + + def extract_reasoning( + self, model_output: str, request: Any + ) -> "tuple[Optional[str], Optional[str]]": + # Strip if the model generated it (old template / edge case). + parts = model_output.partition(self.start_token) + model_output = parts[2] if parts[1] else parts[0] + + if not self.thinking_enabled: + if self.end_token in model_output: + _, _, content = model_output.partition(self.end_token) + return None, content or "" + return None, model_output + + if self.end_token not in model_output: + # Thinking enabled but output truncated before . + return model_output, None + + reasoning, _, content = model_output.partition(self.end_token) + return reasoning, content or None + + def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int: + token_ids = list(token_ids) + if self.start_token_id in token_ids: + # Old-style template: model generates itself. + # Use depth-counting from the base class. + return super().count_reasoning_tokens(token_ids) + elif self.end_token_id in token_ids: + # New-style template (Qwen3.5+): is injected into the + # prompt, so output starts already inside the thinking block. + # Every token before is a reasoning token. + return token_ids.index(self.end_token_id) + else: + # No in output: either truncated (all reasoning) + # or thinking disabled (none). + return len(token_ids) if self.thinking_enabled else 0 + + def extract_reasoning_streaming( + self, + previous_text: str, + current_text: str, + delta_text: str, + previous_token_ids: Sequence[int], + current_token_ids: Sequence[int], + delta_token_ids: Sequence[int], + ): + from vllm.entrypoints.openai.protocol import DeltaMessage + + if not self.thinking_enabled: + return DeltaMessage(content=delta_text) if delta_text else None + + # Strip from delta if the model generates it itself. + if self.start_token_id in delta_token_ids: + start_idx = delta_text.find(self.start_token) + if start_idx >= 0: + delta_text = delta_text[start_idx + len(self.start_token):] + + if self.end_token_id in delta_token_ids: + end_idx = delta_text.find(self.end_token) + if end_idx >= 0: + reasoning = delta_text[:end_idx] + content = delta_text[end_idx + len(self.end_token):] + if not reasoning and not content: + return None + return DeltaMessage( + reasoning_content=reasoning or None, + content=content or None, + ) + return None + + if not delta_text: + return None + elif self.end_token_id in previous_token_ids: + return DeltaMessage(content=delta_text) + else: + return DeltaMessage(reasoning_content=delta_text) + + +# Register immediately when this module is imported. +ReasoningParserManager.register_module("qwen3", Qwen3ReasoningParser) diff --git a/qwen3_6_scripts/scheduler.py b/qwen3_6_scripts/scheduler.py new file mode 100644 index 0000000..ac5155a --- /dev/null +++ b/qwen3_6_scripts/scheduler.py @@ -0,0 +1,1995 @@ +import enum +import os +import random +import time +from collections import deque +from dataclasses import dataclass, field +from typing import (Callable, Deque, Dict, Iterable, List, Optional, Set, + Tuple, Union) + +from vllm.config import CacheConfig, LoRAConfig, SchedulerConfig +from vllm.core.interfaces import AllocStatus, BlockSpaceManager +from vllm.logger import init_logger +from vllm.lora.request import LoRARequest +from vllm.prompt_adapter.request import PromptAdapterRequest +from vllm.sequence import (Sequence, SequenceData, SequenceGroup, + SequenceGroupMetadata, SequenceGroupMetadataDelta, + SequenceStatus) +from vllm.utils import Device, PyObjectCache + +try: + from vllm.gdn_prefix import (GdnPrefixKey, GdnPrefixStatePolicy, + cap_prefill_end_at_capture_boundary, + canonical_direct_segment_offsets, + capture_points_for_step, + final_capture_key, + gdn_cache_policy_from_env, + gdn_restore_alignment, + gdn_restore_mode_from_env, + keys_from_block_hashes, + restore_key_is_eligible, + strict_prefix_block_count) +except ImportError: # Local source-tree tests. + from qwen3_6_scripts.gdn_prefix import ( + GdnPrefixKey, GdnPrefixStatePolicy, + cap_prefill_end_at_capture_boundary, + canonical_direct_segment_offsets, capture_points_for_step, + final_capture_key, gdn_cache_policy_from_env, + gdn_restore_alignment, gdn_restore_mode_from_env, + keys_from_block_hashes, restore_key_is_eligible, + strict_prefix_block_count) + +logger = init_logger(__name__) + +# Test-only. If configured, decode is preempted with +# ARTIFICIAL_PREEMPTION_PROB% probability. +ENABLE_ARTIFICIAL_PREEMPT = bool( + os.getenv("VLLM_TEST_ENABLE_ARTIFICIAL_PREEMPT", False)) # noqa +ARTIFICIAL_PREEMPTION_PROB = 0.5 +ARTIFICIAL_PREEMPTION_MAX_CNT = 500 + + +def _plan_gdn_prefix_fast_forward( + restore_key: Optional[GdnPrefixKey], num_computed_tokens: int, + prompt_len: int, nominal_chunk_size: int, + remaining_token_budget: int, block_size: int, + logical_chunk_alignment: Optional[int] = None) -> Tuple[int, int]: + """Return logical progress and physical query tokens for a direct hit. + + The scheduler normally uses one value for both quantities. A GDN prefix + state makes it safe to advance over a much larger logical prefix while + sending only the suffix after that checkpoint to the model runner. + """ + fallback = (nominal_chunk_size, nominal_chunk_size) + if (restore_key is None or num_computed_tokens != 0 or prompt_len <= 0 + or nominal_chunk_size <= 0 or remaining_token_budget <= 0 + or block_size <= 0): + return fallback + + checkpoint_tokens = restore_key[0] * block_size + logical_limit = checkpoint_tokens + remaining_token_budget + if logical_chunk_alignment is not None: + if (logical_chunk_alignment <= 0 + or logical_chunk_alignment % block_size != 0): + raise ValueError("logical_chunk_alignment must be a positive " + "multiple of block_size") + next_boundary = ( + checkpoint_tokens // logical_chunk_alignment + 1 + ) * logical_chunk_alignment + logical_limit = min(logical_limit, next_boundary) + + logical_chunk_size = min(prompt_len, logical_limit) + physical_query_tokens = logical_chunk_size - checkpoint_tokens + if (physical_query_tokens <= 0 + or physical_query_tokens > remaining_token_budget): + return fallback + if (logical_chunk_size <= nominal_chunk_size + and (logical_chunk_alignment is None + or physical_query_tokens >= nominal_chunk_size)): + return fallback + return logical_chunk_size, physical_query_tokens + + +class PreemptionMode(enum.Enum): + """Preemption modes. + + 1. Swapping: Swap out the blocks of the preempted sequences to CPU memory + and swap them back in when the sequences are resumed. + 2. Recomputation: Discard the blocks of the preempted sequences and + recompute them when the sequences are resumed, treating the sequences as + new prompts. + """ + SWAP = enum.auto() + RECOMPUTE = enum.auto() + + +@dataclass +class SchedulingBudget: + """The available slots for scheduling. + + TODO(sang): Right now, the budget is request_id-aware meaning it can ignore + budget update from the same request_id. It is because in normal scheduling + path, we update RUNNING num_seqs ahead of time, meaning it could be + updated more than once when scheduling RUNNING requests. Since this won't + happen if we only have chunked prefill scheduling, we can remove this + feature from the API when chunked prefill is enabled by default. + """ + token_budget: int + max_num_seqs: int + _request_ids_num_batched_tokens: Set[str] = field(default_factory=set) + _request_ids_num_curr_seqs: Set[str] = field(default_factory=set) + _num_batched_tokens: int = 0 + _num_scheduled_tokens: int = 0 + _request_num_scheduled_tokens: Dict[str, int] = field( + default_factory=dict) + _num_curr_seqs: int = 0 + + def can_schedule(self, *, num_new_tokens: int, num_new_seqs: int): + assert num_new_tokens != 0 + assert num_new_seqs != 0 + return (self.num_batched_tokens + num_new_tokens <= self.token_budget + and self.num_curr_seqs + num_new_seqs <= self.max_num_seqs) + + def remaining_token_budget(self): + return self.token_budget - self.num_batched_tokens + + def add_num_batched_tokens( + self, req_id: str, num_batched_tokens: int, + num_scheduled_tokens: Optional[int] = None): + if req_id in self._request_ids_num_batched_tokens: + return + + if num_scheduled_tokens is None: + num_scheduled_tokens = num_batched_tokens + self._request_ids_num_batched_tokens.add(req_id) + self._num_batched_tokens += num_batched_tokens + self._num_scheduled_tokens += num_scheduled_tokens + self._request_num_scheduled_tokens[req_id] = num_scheduled_tokens + + def subtract_num_batched_tokens(self, req_id: str, + num_batched_tokens: int): + if req_id in self._request_ids_num_batched_tokens: + self._request_ids_num_batched_tokens.remove(req_id) + self._num_batched_tokens -= num_batched_tokens + self._num_scheduled_tokens -= ( + self._request_num_scheduled_tokens.pop(req_id)) + + def add_num_seqs(self, req_id: str, num_curr_seqs: int): + if req_id in self._request_ids_num_curr_seqs: + return + + self._request_ids_num_curr_seqs.add(req_id) + self._num_curr_seqs += num_curr_seqs + + def subtract_num_seqs(self, req_id: str, num_curr_seqs: int): + if req_id in self._request_ids_num_curr_seqs: + self._request_ids_num_curr_seqs.remove(req_id) + self._num_curr_seqs -= num_curr_seqs + + @property + def num_batched_tokens(self): + return self._num_batched_tokens + + @property + def num_scheduled_tokens(self): + return self._num_scheduled_tokens + + @property + def num_curr_seqs(self): + return self._num_curr_seqs + + +@dataclass +class ScheduledSequenceGroup: + # A sequence group that's scheduled. + seq_group: SequenceGroup + # The total chunk size (number of tokens) to process for next iteration. + # 1 for decoding. Same as prompt tokens for prefill, but if prefill is + # chunked, it can be smaller than that. + token_chunk_size: int + + +@dataclass +class SchedulerOutputs: + """The scheduling decision made from a scheduler.""" + # Scheduled sequence groups. + scheduled_seq_groups: Iterable[ScheduledSequenceGroup] + # Number of prefill groups scheduled. + num_prefill_groups: int + # Total number of batched tokens. + num_batched_tokens: int + # Blocks to swap in. List of CPU -> GPU block number. + blocks_to_swap_in: List[Tuple[int, int]] + # Blocks to swap out. List of GPU -> CPU block number. + blocks_to_swap_out: List[Tuple[int, int]] + # Blocks to copy. Source to dest block. + blocks_to_copy: List[Tuple[int, int]] + # Sequence groups that are going to be ignored. + ignored_seq_groups: List[SequenceGroup] + # The number of slots for lookahead decoding. + num_lookahead_slots: int + # The number of requests in the running queue + running_queue_size: int + preempted: int + + def __post_init__(self): + # Request-level preemption cannot swap both ways in one step. The + # content-addressed CPU tier appends its ordered maps after creation. + assert not (self.blocks_to_swap_in and self.blocks_to_swap_out) + + self.num_loras: int = len(self.lora_requests) + if self.num_loras > 0: + self._sort_by_lora_ids() + + self.num_prompt_adapters: int = len(self.prompt_adapter_requests) + + def is_empty(self) -> bool: + # NOTE: We do not consider the ignored sequence groups. + return (not self.scheduled_seq_groups and not self.blocks_to_swap_in + and not self.blocks_to_swap_out and not self.blocks_to_copy) + + def _sort_by_lora_ids(self): + self.scheduled_seq_groups = sorted( + self.scheduled_seq_groups, + key=lambda g: (g.seq_group.lora_int_id, g.seq_group.request_id)) + + @property + def lora_requests(self) -> Set[LoRARequest]: + return { + g.seq_group.lora_request + for g in self.scheduled_seq_groups + if g.seq_group.lora_request is not None + } + + @property + def prompt_adapter_requests(self) -> Set[PromptAdapterRequest]: + return { + g.seq_group.prompt_adapter_request + for g in self.scheduled_seq_groups + if g.seq_group.prompt_adapter_request is not None + } + + +@dataclass +class SchedulerRunningOutputs: + """The requests that are scheduled from a running queue. + + Could contain prefill (prefill that's chunked) or decodes. If there's not + enough memory, it can be preempted (for recompute) or swapped out. + """ + # Selected sequences that are running and in a decoding phase. + decode_seq_groups: List[ScheduledSequenceGroup] + # Selected sequences that are running and in a prefill phase. + # I.e., it means the prefill has been chunked. + prefill_seq_groups: List[ScheduledSequenceGroup] + # The preempted sequences. + preempted: List[SequenceGroup] + # Sequences that are swapped out. + swapped_out: List[SequenceGroup] + # The blocks to swap out. + blocks_to_swap_out: List[Tuple[int, int]] + # The blocks to copy. + blocks_to_copy: List[Tuple[int, int]] + # The number of slots for lookahead decoding. + num_lookahead_slots: int + + # Optimization for fast-access to seq_group lists + decode_seq_groups_list: List[SequenceGroup] + prefill_seq_groups_list: List[SequenceGroup] + + @classmethod + def create_empty(cls) -> "SchedulerRunningOutputs": + return SchedulerRunningOutputs( + decode_seq_groups=[], + prefill_seq_groups=[], + preempted=[], + swapped_out=[], + blocks_to_swap_out=[], + blocks_to_copy=[], + num_lookahead_slots=0, + decode_seq_groups_list=[], + prefill_seq_groups_list=[], + ) + + +@dataclass +class SchedulerSwappedInOutputs: + """The requests that are scheduled from a swap queue. + + Could contain prefill (prefill that's chunked) or decodes. + """ + # Selected sequences that are going to be swapped in and is in a + # decoding phase. + decode_seq_groups: List[ScheduledSequenceGroup] + # Selected sequences that are going to be swapped in and in a prefill + # phase. I.e., it means the prefill has been chunked. + prefill_seq_groups: List[ScheduledSequenceGroup] + # The blocks to swap in. + blocks_to_swap_in: List[Tuple[int, int]] + # The blocks to copy. + blocks_to_copy: List[Tuple[int, int]] + # The number of slots for lookahead decoding. + num_lookahead_slots: int + # Infeasible sequence groups. + infeasible_seq_groups: List[SequenceGroup] + + @classmethod + def create_empty(cls) -> "SchedulerSwappedInOutputs": + return SchedulerSwappedInOutputs( + decode_seq_groups=[], + prefill_seq_groups=[], + blocks_to_swap_in=[], + blocks_to_copy=[], + num_lookahead_slots=0, + infeasible_seq_groups=[], + ) + + +@dataclass +class SchedulerPrefillOutputs: + """The requests that are scheduled from a waiting queue. + + Could contain a fresh prefill requests or preempted requests that need + to be recomputed from scratch. + """ + # Selected sequences for prefill. + seq_groups: List[ScheduledSequenceGroup] + # Ignored sequence groups. + ignored_seq_groups: List[SequenceGroup] + num_lookahead_slots: int + + @classmethod + def create_empty(cls) -> "SchedulerPrefillOutputs": + return SchedulerPrefillOutputs( + seq_groups=[], + ignored_seq_groups=[], + num_lookahead_slots=0, + ) + + +def seq_group_metadata_builder(): + return SequenceGroupMetadata(request_id="", + is_prompt=False, + seq_data={}, + sampling_params=None, + block_tables={}) + + +def scheduler_running_outputs_builder(): + return SchedulerRunningOutputs(decode_seq_groups=[], + prefill_seq_groups=[], + preempted=[], + swapped_out=[], + blocks_to_swap_out=[], + blocks_to_copy=[], + num_lookahead_slots=0, + prefill_seq_groups_list=[], + decode_seq_groups_list=[]) + + +def scheduled_seq_group_builder(): + return ScheduledSequenceGroup(SequenceGroup("", [], -1), + token_chunk_size=0) + # return ScheduledSequenceGroup(seq_group=None, token_chunk_size=0) + + +class Scheduler: + + def __init__( + self, + scheduler_config: SchedulerConfig, + cache_config: CacheConfig, + lora_config: Optional[LoRAConfig], + pipeline_parallel_size: int = 1, + output_proc_callback: Optional[Callable] = None, + ) -> None: + self.scheduler_config = scheduler_config + self.cache_config = cache_config + # Note for LoRA scheduling: the current policy is extremely + # simple and NOT fair. It can lead to starvation of some + # LoRAs. This should be improved in the future. + self.lora_config = lora_config + + version = "v1" + if self.scheduler_config.use_v2_block_manager: + version = "v2" + if (self.scheduler_config.embedding_mode + or self.cache_config.is_attention_free): + version = "placeholder" + + BlockSpaceManagerImpl = BlockSpaceManager.get_block_space_manager_class( + version) + + num_gpu_blocks = cache_config.num_gpu_blocks + if num_gpu_blocks: + num_gpu_blocks //= pipeline_parallel_size + + num_cpu_blocks = cache_config.num_cpu_blocks + if num_cpu_blocks: + num_cpu_blocks //= pipeline_parallel_size + + # Create the block space manager. + self.block_manager = BlockSpaceManagerImpl( + block_size=self.cache_config.block_size, + num_gpu_blocks=num_gpu_blocks, + num_cpu_blocks=num_cpu_blocks, + sliding_window=self.cache_config.sliding_window, + enable_caching=self.cache_config.enable_prefix_caching) + + # Sequence groups in the WAITING state. + # Contain new prefill or preempted requests. + self.waiting: Deque[SequenceGroup] = deque() + # Sequence groups in the RUNNING state. + # Contain decode requests. + self.running: Deque[SequenceGroup] = deque() + # Sequence groups in the SWAPPED state. + # Contain decode requests that are swapped out. + self.swapped: Deque[SequenceGroup] = deque() + # Sequence groups finished requests ids since last step iteration. + # It lets the model know that any state associated with these requests + # can and must be released after the current step. + # This is used to evict the finished requests from the Mamba cache. + self._finished_requests_ids: List[str] = list() + self._gdn_prefix_policy = GdnPrefixStatePolicy( + gdn_cache_policy_from_env()) + self._gdn_restore_mode = gdn_restore_mode_from_env() + try: + self._gdn_replay_alignment = gdn_restore_alignment( + self._gdn_restore_mode, self.cache_config.block_size, + scheduler_config.max_num_batched_tokens) + except ValueError as exc: + raise RuntimeError(str(exc)) from exc + self._gdn_request_restore_keys: Dict[ + str, Optional[GdnPrefixKey]] = {} + self._gdn_request_capture_targets: Dict[ + str, Tuple[GdnPrefixKey, ...]] = {} + # Time at previous scheduling step + self.prev_time = 0.0 + # Did we schedule a prompt at previous step? + self.prev_prompt = False + # Latency of the last prompt step + self.last_prompt_latency = 0.0 + # preemption mode, RECOMPUTE or SWAP + self.user_specified_preemption_mode = scheduler_config.preemption_mode + + # The following field is test-only. It is used to inject artificial + # preemption. + self.enable_artificial_preemption = ENABLE_ARTIFICIAL_PREEMPT + self.artificial_preempt_cnt = (ARTIFICIAL_PREEMPTION_MAX_CNT + if self.enable_artificial_preemption + else 0) + self.num_cumulative_preemption: int = 0 + + # Used to cache python objects + self._seq_group_metadata_cache: List[PyObjectCache] = [] + self._scheduler_running_outputs_cache: List[PyObjectCache] = [] + self._scheduled_seq_group_cache: List[PyObjectCache] = [] + + # For async output processing, we need to swap cache buffers between + # iterations. I.e. since the output processing is lagged one step, + # we cannot reuse the cached objects immediately when the schedule() + # is called again, but only when schedule() is called the second time. + self.output_proc_callback = output_proc_callback + self.use_async_output_proc = self.output_proc_callback is not None + self.num_cache_iters = 2 if self.use_async_output_proc else 1 + + self.cache_id = 0 + for i in range(self.num_cache_iters): + self._seq_group_metadata_cache.append( + PyObjectCache(seq_group_metadata_builder)) + self._scheduler_running_outputs_cache.append( + PyObjectCache(scheduler_running_outputs_builder)) + self._scheduled_seq_group_cache.append( + PyObjectCache(scheduled_seq_group_builder)) + + # For async postprocessor, the extra decode run cannot be done + # when the request reaches max_model_len. In this case, the request + # will be stopped during schedule() call and added to this stop list + # for processing and deallocation by the free_finished_seq_groups() + self._async_stopped: List[SequenceGroup] = [] + + @property + def next_cache_id(self): + return (self.cache_id + 1) % self.num_cache_iters + + @property + def lora_enabled(self) -> bool: + return bool(self.lora_config) + + @property + def num_decoding_tokens_per_seq(self) -> int: + """The number of new tokens.""" + return 1 + + def add_seq_group(self, seq_group: SequenceGroup) -> None: + # Add sequence groups to the waiting queue. + self.waiting.append(seq_group) + + def _add_seq_group_to_running(self, seq_group: SequenceGroup) -> None: + # Add sequence groups to the running queue. + # Only for testing purposes. + self.running.append(seq_group) + + def _add_seq_group_to_swapped(self, seq_group: SequenceGroup) -> None: + # Add sequence groups to the swapped queue. + # Only for testing purposes. + self.swapped.append(seq_group) + + def _cap_gdn_capture_boundary( + self, seq_group: SequenceGroup, token_chunk_size: int, + physical_query_tokens: int) -> Tuple[int, int]: + """Align admission64 capture state with a physical model forward.""" + targets = self._gdn_request_capture_targets.get( + seq_group.request_id, ()) + if (self._gdn_prefix_policy.policy != "admission64" + or not targets): + return token_chunk_size, physical_query_tokens + if token_chunk_size <= 0 or physical_query_tokens <= 0: + raise RuntimeError("GDN prefill token counts must be positive") + + seqs = seq_group.get_seqs() + if len(seqs) != 1 or not seq_group.is_prefill(): + raise RuntimeError( + "GDN capture boundary requires one prefill sequence") + num_computed_tokens = seqs[0].data.get_num_computed_tokens() + logical_end_tokens = num_computed_tokens + token_chunk_size + logical_start_tokens = logical_end_tokens - physical_query_tokens + if logical_start_tokens < num_computed_tokens: + raise RuntimeError( + "GDN physical query starts before scheduler progress") + + capped_end_tokens = cap_prefill_end_at_capture_boundary( + logical_start_tokens, logical_end_tokens, targets, + self.cache_config.block_size) + if capped_end_tokens == logical_end_tokens: + return token_chunk_size, physical_query_tokens + + capped_chunk_size = capped_end_tokens - num_computed_tokens + capped_query_tokens = capped_end_tokens - logical_start_tokens + if capped_chunk_size <= 0 or capped_query_tokens <= 0: + raise RuntimeError("GDN capture boundary produced an empty step") + logger.info( + "[BI100 GDN CAPTURE BOUNDARY] request=%s logical_start=%d " + "logical_end=%d capped_end=%d physical_query_tokens=%d", + seq_group.request_id, logical_start_tokens, logical_end_tokens, + capped_end_tokens, capped_query_tokens) + return capped_chunk_size, capped_query_tokens + + def abort_seq_group(self, request_id: Union[str, Iterable[str]]) -> None: + """Aborts a sequence group with the given ID. + + Check if the sequence group with the given ID + is present in any of the state queue. + If present, remove the sequence group from the state queue. + Also, if any of the sequences in the sequence group is not finished, + free the sequence with status `FINISHED_ABORTED`. + Otherwise, do nothing. + + Args: + request_id: The ID(s) of the sequence group to abort. + """ + if isinstance(request_id, str): + request_id = (request_id, ) + request_ids = set(request_id) + for state_queue in [self.waiting, self.running, self.swapped]: + aborted_groups: List[SequenceGroup] = [] + for seq_group in state_queue: + if not request_ids: + # Using 'break' here may add two extra iterations, + # but is acceptable to reduce complexity. + break + if seq_group.request_id in request_ids: + # Appending aborted group into pending list. + aborted_groups.append(seq_group) + request_ids.remove(seq_group.request_id) + for aborted_group in aborted_groups: + # Remove the sequence group from the state queue. + state_queue.remove(aborted_group) + # Remove the aborted request from the Mamba cache. + self._finished_requests_ids.append(aborted_group.request_id) + for seq in aborted_group.get_seqs(): + if seq.is_finished(): + continue + seq.status = SequenceStatus.FINISHED_ABORTED + self.free_seq(seq) + + self._free_seq_group_cross_attn_blocks(aborted_group) + + def _free_seq_group_cross_attn_blocks( + self, + seq_group: SequenceGroup, + ) -> None: + """ + Free a sequence group from a cross-attention block table. + Also release any request-local multimodal cache namespace. + """ + try: + if seq_group.is_encoder_decoder(): + self.block_manager.free_cross(seq_group) + finally: + release_namespace = getattr( + self.block_manager, "release_request_cache_namespace", None) + if release_namespace is not None: + release_namespace(seq_group.request_id) + + def has_unfinished_seqs(self) -> bool: + return len(self.waiting) != 0 or len(self.running) != 0 or len( + self.swapped) != 0 + + def get_prefix_cache_hit_rate(self, device: Device) -> float: + return self.block_manager.get_prefix_cache_hit_rate(device) + + def get_num_unfinished_seq_groups(self) -> int: + return len(self.waiting) + len(self.running) + len(self.swapped) + + def get_and_reset_finished_requests_ids(self) -> List[str]: + """Flushes the list of request ids of previously finished seq_groups.""" + finished_requests_ids = self._finished_requests_ids + self._finished_requests_ids = list() + return finished_requests_ids + + def _schedule_running( + self, + budget: SchedulingBudget, + curr_loras: Optional[Set[int]], + enable_chunking: bool = False, + ) -> SchedulerRunningOutputs: + """Schedule sequence groups that are running. + + Running queue should include decode and chunked prefill requests. + + Args: + budget: The scheduling budget. The argument is in-place updated + when any decodes are preempted. + curr_loras: Currently batched lora request ids. The argument is + in-place updated when any decodes are preempted. + enable_chunking: If True, seq group can be chunked and only a + chunked number of tokens are scheduled if + `budget.num_batched_tokens` has not enough capacity to schedule + all tokens. + + Returns: + SchedulerRunningOutputs. + """ + ret: SchedulerRunningOutputs = \ + self._scheduler_running_outputs_cache[self.cache_id].get_object() + ret.blocks_to_swap_out.clear() + ret.blocks_to_copy.clear() + ret.decode_seq_groups.clear() + ret.prefill_seq_groups.clear() + ret.preempted.clear() + ret.swapped_out.clear() + + ret.num_lookahead_slots = self._get_num_lookahead_slots( + is_prefill=False, enable_chunking=enable_chunking) + + ret.decode_seq_groups_list.clear() + ret.prefill_seq_groups_list.clear() + + # Blocks that need to be swapped or copied before model execution. + blocks_to_swap_out: List[Tuple[int, int]] = ret.blocks_to_swap_out + blocks_to_copy: List[Tuple[int, int]] = ret.blocks_to_copy + + decode_seq_groups: List[ScheduledSequenceGroup] = ret.decode_seq_groups + prefill_seq_groups: List[ + ScheduledSequenceGroup] = ret.prefill_seq_groups + preempted: List[SequenceGroup] = ret.preempted + swapped_out: List[SequenceGroup] = ret.swapped_out + + running_queue = self.running + assert len(self._async_stopped) == 0 + while running_queue: + seq_group = running_queue[0] + num_running_tokens = self._get_num_new_tokens( + seq_group, SequenceStatus.RUNNING, enable_chunking, budget) + + if num_running_tokens == 0: + # No budget => Stop + break + if enable_chunking and seq_group.is_prefill(): + num_running_tokens, _ = self._cap_gdn_capture_boundary( + seq_group, num_running_tokens, num_running_tokens) + + running_queue.popleft() + + # With async postprocessor, an extra decode run is done + # to process the final tokens. The check below avoids this extra + # decode run when the model max len is reached, in order to avoid + # a memory overflow. + if self.use_async_output_proc and seq_group.seqs[0].get_len( + ) > self.scheduler_config.max_model_len: + self._async_stopped.append(seq_group) + continue + + # NOTE(woosuk): Preemption happens only when there is no available + # slot to keep all the sequence groups in the RUNNING state. + while not self._can_append_slots(seq_group, enable_chunking): + budget.subtract_num_batched_tokens(seq_group.request_id, + num_running_tokens) + num_running_seqs = seq_group.get_max_num_running_seqs() + budget.subtract_num_seqs(seq_group.request_id, + num_running_seqs) + + if (curr_loras is not None and seq_group.lora_int_id > 0 + and seq_group.lora_int_id in curr_loras): + curr_loras.remove(seq_group.lora_int_id) + + # Determine victim sequence + cont_loop = True + if running_queue: + # Preempt the lowest-priority sequence group. + victim_seq_group = running_queue.pop() + else: + # No other sequence group can be preempted. + # Preempt the current sequence group. + # Note: This is also where we stop this loop + # (since there is nothing else to preempt) + victim_seq_group = seq_group + cont_loop = False + + # With async postprocessor, before preempting a sequence + # we need to ensure it has no pending async postprocessor + do_preempt = True + if self.use_async_output_proc: + assert self.output_proc_callback is not None + self.output_proc_callback( + request_id=victim_seq_group.request_id) + + # It may be that the async pending "victim_seq_group" + # becomes finished, in which case we simply free it. + if victim_seq_group.is_finished(): + self._free_finished_seq_group(victim_seq_group) + do_preempt = False + + # Do preemption + if do_preempt: + preempted_mode = self._preempt(victim_seq_group, + blocks_to_swap_out) + if preempted_mode == PreemptionMode.RECOMPUTE: + preempted.append(victim_seq_group) + else: + swapped_out.append(victim_seq_group) + + if not cont_loop: + break + else: + self._append_slots(seq_group, blocks_to_copy, enable_chunking) + is_prefill = seq_group.is_prefill() + + scheduled_seq_group: ScheduledSequenceGroup = \ + self._scheduled_seq_group_cache[self.cache_id].get_object() + scheduled_seq_group.seq_group = seq_group + if is_prefill: + scheduled_seq_group.token_chunk_size = num_running_tokens + prefill_seq_groups.append(scheduled_seq_group) + ret.prefill_seq_groups_list.append(seq_group) + else: + scheduled_seq_group.token_chunk_size = 1 + decode_seq_groups.append(scheduled_seq_group) + ret.decode_seq_groups_list.append(seq_group) + + budget.add_num_batched_tokens(seq_group.request_id, + num_running_tokens) + # OPTIMIZATION: Note that get_max_num_running_seqs is + # expensive. For the default scheduling chase where + # enable_chunking is False, num_seqs are updated before running + # this method, so we don't have to update it again here. + if enable_chunking: + num_running_seqs = seq_group.get_max_num_running_seqs() + budget.add_num_seqs(seq_group.request_id, num_running_seqs) + if curr_loras is not None and seq_group.lora_int_id > 0: + curr_loras.add(seq_group.lora_int_id) + + self._scheduler_running_outputs_cache[self.next_cache_id].reset() + self._scheduled_seq_group_cache[self.next_cache_id].reset() + + return ret + + def _schedule_swapped( + self, + budget: SchedulingBudget, + curr_loras: Optional[Set[int]], + enable_chunking: bool = False, + ) -> SchedulerSwappedInOutputs: + """Schedule sequence groups that are swapped out. + + It schedules swapped requests as long as it fits `budget` and + curr_loras <= max_lora from the scheduling config. The input arguments + `budget` and `curr_loras` are updated based on scheduled seq_groups. + + Args: + budget: The scheduling budget. The argument is in-place updated + when any requests are swapped in. + curr_loras: Currently batched lora request ids. The argument is + in-place updated when any requests are swapped in. + enable_chunking: If True, seq group can be chunked and only a + chunked number of tokens are scheduled if + `budget.num_batched_tokens` has not enough capacity to schedule + all tokens. + + Returns: + SchedulerSwappedInOutputs. + """ + # Blocks that need to be swapped or copied before model execution. + blocks_to_swap_in: List[Tuple[int, int]] = [] + blocks_to_copy: List[Tuple[int, int]] = [] + decode_seq_groups: List[ScheduledSequenceGroup] = [] + prefill_seq_groups: List[ScheduledSequenceGroup] = [] + infeasible_seq_groups: List[SequenceGroup] = [] + + swapped_queue = self.swapped + + leftover_swapped: Deque[SequenceGroup] = deque() + while swapped_queue: + seq_group = swapped_queue[0] + + # If the sequence group cannot be swapped in, stop. + is_prefill = seq_group.is_prefill() + alloc_status = self.block_manager.can_swap_in( + seq_group, + self._get_num_lookahead_slots(is_prefill, enable_chunking)) + if alloc_status == AllocStatus.LATER: + break + elif alloc_status == AllocStatus.NEVER: + logger.warning( + "Failing the request %s because there's not enough kv " + "cache blocks to run the entire sequence.", + seq_group.request_id) + for seq in seq_group.get_seqs(): + seq.status = SequenceStatus.FINISHED_IGNORED + infeasible_seq_groups.append(seq_group) + swapped_queue.popleft() + continue + + lora_int_id = 0 + if self.lora_enabled: + lora_int_id = seq_group.lora_int_id + assert curr_loras is not None + assert self.lora_config is not None + if (lora_int_id > 0 and (lora_int_id not in curr_loras) + and len(curr_loras) >= self.lora_config.max_loras): + # We don't have a space for another LoRA, so + # we ignore this request for now. + leftover_swapped.appendleft(seq_group) + swapped_queue.popleft() + continue + + # The total number of sequences in the RUNNING state should not + # exceed the maximum number of sequences. + num_new_seqs = seq_group.get_max_num_running_seqs() + num_new_tokens = self._get_num_new_tokens(seq_group, + SequenceStatus.SWAPPED, + enable_chunking, budget) + + if (num_new_tokens == 0 + or not budget.can_schedule(num_new_tokens=num_new_tokens, + num_new_seqs=num_new_seqs)): + break + + if lora_int_id > 0 and curr_loras is not None: + curr_loras.add(lora_int_id) + swapped_queue.popleft() + self._swap_in(seq_group, blocks_to_swap_in) + self._append_slots(seq_group, blocks_to_copy, enable_chunking) + is_prefill = seq_group.is_prefill() + if is_prefill: + prefill_seq_groups.append( + ScheduledSequenceGroup(seq_group, + token_chunk_size=num_new_tokens)) + else: + decode_seq_groups.append( + ScheduledSequenceGroup(seq_group, token_chunk_size=1)) + budget.add_num_batched_tokens(seq_group.request_id, num_new_tokens) + budget.add_num_seqs(seq_group.request_id, num_new_seqs) + + swapped_queue.extendleft(leftover_swapped) + + return SchedulerSwappedInOutputs( + decode_seq_groups=decode_seq_groups, + prefill_seq_groups=prefill_seq_groups, + blocks_to_swap_in=blocks_to_swap_in, + blocks_to_copy=blocks_to_copy, + num_lookahead_slots=self._get_num_lookahead_slots( + is_prefill=False, enable_chunking=enable_chunking), + infeasible_seq_groups=infeasible_seq_groups, + ) + + def _get_prompt_limit(self, seq_group: SequenceGroup) -> int: + if self.scheduler_config.chunked_prefill_enabled and \ + not self.scheduler_config.is_multi_step: + prompt_limit = self.scheduler_config.max_model_len + else: + prompt_limit = min(self.scheduler_config.max_model_len, + self.scheduler_config.max_num_batched_tokens) + + # Model is fine tuned with long context. Return the fine tuned max_len. + if (seq_group.lora_request + and seq_group.lora_request.long_lora_max_len): + assert prompt_limit <= seq_group.lora_request.long_lora_max_len + return seq_group.lora_request.long_lora_max_len + else: + return prompt_limit + + def _get_priority(self, + seq_group: SequenceGroup) -> Tuple[Optional[int], float]: + """ Get the priority of the sequence group. + Highest preference to user-defined priority, followed by arrival time. + Args: + seq_group: The sequence group input. + Returns: + The priority of the sequence group. + """ + return seq_group.priority, seq_group.arrival_time + + def _schedule_priority_preemption( + self, + budget: SchedulingBudget, + ) -> int: + """Sorts waiting and running queue. Also, force preempt requests + from the running queue if their priority is lower. + Priority-based preemption is used with the priority policy. + Args: + budget: The scheduling budget. The argument is in-place updated + when any requests are scheduled. + Returns: + A count of priority-based preemptions. + """ + + waiting_queue = self.waiting + + running_queue = deque(sorted(self.running, key=self._get_priority)) + + blocks_to_swap_out: List[Tuple[int, int]] = [] + force_preemption_count = 0 + + if waiting_queue: + seq_group = waiting_queue.popleft() + num_new_seqs = seq_group.get_max_num_running_seqs() + num_new_tokens = self._get_num_new_tokens(seq_group, + SequenceStatus.WAITING, + False, budget) + + #Only preempt if priority inversion exists + while running_queue and self._get_priority( + running_queue[-1]) > self._get_priority(seq_group): + #Only preempt if waiting sequence cannot be allocated + can_allocate = self.block_manager.can_allocate(seq_group) + if (num_new_tokens and can_allocate == AllocStatus.OK + and budget.can_schedule(num_new_tokens=num_new_tokens, + num_new_seqs=num_new_seqs)): + break + + #Adjust budget to remove the victim sequence group + vseq_group = running_queue.pop() + num_running_tokens = self._get_num_new_tokens( + vseq_group, SequenceStatus.RUNNING, False, budget) + budget.subtract_num_batched_tokens(vseq_group.request_id, + num_running_tokens) + num_running_seqs = vseq_group.get_max_num_running_seqs() + budget.subtract_num_seqs(vseq_group.request_id, + num_running_seqs) + + #Preempt out the victim sequence group + self._preempt(vseq_group, blocks_to_swap_out, + PreemptionMode.RECOMPUTE) + waiting_queue.appendleft(vseq_group) + force_preemption_count += 1 + #Put the sequence back into the waiting queue + waiting_queue.appendleft(seq_group) + + waiting_queue = deque(sorted(waiting_queue, key=self._get_priority)) + + self.waiting = waiting_queue + self.running = running_queue + return force_preemption_count + + def _schedule_prefills( + self, + budget: SchedulingBudget, + curr_loras: Optional[Set[int]], + enable_chunking: bool = False, + ) -> SchedulerPrefillOutputs: + """Schedule sequence groups that are in prefill stage. + + Note that the current scheduler treats PREEMPTED_FOR_RECOMPUTE + as a new prefill (that starts from beginning -> most recently generated + tokens). + + It schedules waiting requests as long as it fits `budget` and + curr_loras <= max_lora from the scheduling config. The input arguments + `budget` and `curr_loras` are updated based on scheduled seq_groups. + + Args: + budget: The scheduling budget. The argument is in-place updated + when any requests are scheduled. + curr_loras: Currently batched lora request ids. The argument is + in-place updated when any requests are scheduled. + enable_chunking: If True, seq group can be chunked and only a + chunked number of tokens are scheduled if + `budget.num_batched_tokens` has not enough capacity to schedule + all tokens. + + Returns: + SchedulerPrefillOutputs. + """ + ignored_seq_groups: List[SequenceGroup] = [] + seq_groups: List[ScheduledSequenceGroup] = [] + + waiting_queue = self.waiting + + leftover_waiting_sequences: Deque[SequenceGroup] = deque() + while self._passed_delay(time.time()) and waiting_queue: + seq_group = waiting_queue[0] + + waiting_seqs = seq_group.get_seqs(status=SequenceStatus.WAITING) + assert len(waiting_seqs) == 1, ( + "Waiting sequence group should have only one prompt " + "sequence.") + num_new_tokens = self._get_num_new_tokens(seq_group, + SequenceStatus.WAITING, + enable_chunking, budget) + if not enable_chunking: + num_prompt_tokens = waiting_seqs[0].get_len() + assert num_new_tokens == num_prompt_tokens + + prompt_limit = self._get_prompt_limit(seq_group) + if num_new_tokens > prompt_limit: + logger.warning( + "Input prompt (%d tokens) is too long" + " and exceeds limit of %d", num_new_tokens, prompt_limit) + for seq in waiting_seqs: + seq.status = SequenceStatus.FINISHED_IGNORED + ignored_seq_groups.append(seq_group) + waiting_queue.popleft() + continue + + num_lookahead_slots: int = 0 + if self.scheduler_config.is_multi_step and enable_chunking: + num_lookahead_slots = self._get_num_lookahead_slots( + True, enable_chunking) + + # If the sequence group cannot be allocated, stop. + can_allocate = self.block_manager.can_allocate( + seq_group, num_lookahead_slots=num_lookahead_slots) + if can_allocate == AllocStatus.LATER: + break + elif can_allocate == AllocStatus.NEVER: + logger.warning( + "Input prompt (%d tokens) + lookahead slots (%d) is " + "too long and exceeds the capacity of block_manager", + num_new_tokens, num_lookahead_slots) + for seq in waiting_seqs: + seq.status = SequenceStatus.FINISHED_IGNORED + ignored_seq_groups.append(seq_group) + waiting_queue.popleft() + continue + + lora_int_id = 0 + if self.lora_enabled: + lora_int_id = seq_group.lora_int_id + assert curr_loras is not None + assert self.lora_config is not None + if (self.lora_enabled and lora_int_id > 0 + and lora_int_id not in curr_loras + and len(curr_loras) >= self.lora_config.max_loras): + # We don't have a space for another LoRA, so + # we ignore this request for now. + leftover_waiting_sequences.appendleft(seq_group) + waiting_queue.popleft() + continue + + num_new_seqs = seq_group.get_max_num_running_seqs() + if (num_new_tokens == 0 + or not budget.can_schedule(num_new_tokens=num_new_tokens, + num_new_seqs=num_new_seqs)): + break + + # Can schedule this request. + if curr_loras is not None and lora_int_id > 0: + curr_loras.add(lora_int_id) + waiting_queue.popleft() + self._allocate_and_set_running(seq_group) + + budget_token_count = num_new_tokens + if (enable_chunking + and self.cache_config.enable_prefix_caching + and len(waiting_seqs) == 1): + prompt_seq = waiting_seqs[0] + computed_block_nums = list( + self.block_manager.get_common_computed_block_ids( + waiting_seqs)) + block_hashes = self.block_manager.get_content_hashes(prompt_seq) + max_live_blocks = min( + len(computed_block_nums), len(block_hashes), + strict_prefix_block_count( + prompt_seq.data.get_len(), + self.cache_config.block_size)) + live_keys = keys_from_block_hashes( + block_hashes[:max_live_blocks]) + direct_final_key = final_capture_key( + block_hashes, prompt_seq.data.get_len(), + self.cache_config.block_size, "direct", + self.cache_config.block_size) + live_keys = [ + key for key in live_keys + if restore_key_is_eligible( + key, prompt_seq.data.get_len(), + self.cache_config.block_size, + self._gdn_restore_mode, + self._gdn_replay_alignment, + direct_final_key=( + direct_final_key + if self._gdn_restore_mode == "hybrid64" else None)) + ] + restore_key = self._gdn_prefix_policy.select_restore( + live_keys, len(live_keys)) + self._gdn_request_restore_keys[ + seq_group.request_id] = restore_key + + capture_targets = [] + branch_key = self._gdn_prefix_policy.repeated_branch_candidate( + live_keys, len(live_keys)) + if branch_key is not None: + capture_targets.append(branch_key) + final_key = final_capture_key( + block_hashes, prompt_seq.data.get_len(), + self.cache_config.block_size, self._gdn_restore_mode, + self._gdn_replay_alignment) + if (final_key is not None + and final_key not in capture_targets + and self._gdn_prefix_policy.should_capture_final( + final_key)): + capture_targets.append(final_key) + self._gdn_request_capture_targets[seq_group.request_id] = tuple( + capture_targets) + + num_new_tokens, budget_token_count = ( + _plan_gdn_prefix_fast_forward( + restore_key, + prompt_seq.data.get_num_computed_tokens(), + prompt_seq.data.get_len(), + num_new_tokens, + budget.remaining_token_budget(), + self.cache_config.block_size, + logical_chunk_alignment=( + self.scheduler_config.max_num_batched_tokens + if self._gdn_restore_mode == "hybrid64" else None))) + if budget_token_count != num_new_tokens: + logger.info( + "[BI100 GDN FAST-FORWARD] request=%s " + "checkpoint_tokens=%d logical_tokens=%d " + "physical_query_tokens=%d", + seq_group.request_id, + num_new_tokens - budget_token_count, + num_new_tokens, + budget_token_count) + num_new_tokens, budget_token_count = ( + self._cap_gdn_capture_boundary( + seq_group, num_new_tokens, budget_token_count)) + + if enable_chunking and self.scheduler_config.is_multi_step: + blocks_to_copy: List[Tuple[int, int]] = [] + # init_multi_step_from_lookahead_slots happens in append_slots + self._append_slots(seq_group, blocks_to_copy, enable_chunking) + # This assert will trip when a copy-on-write happens. This is + # not a concern as the very first sequence-group block + # allocation happens above. Still, we have the assert to + # catch any edge-cases. + assert not blocks_to_copy + else: + seq_group.init_multi_step_from_lookahead_slots( + num_lookahead_slots, + num_scheduler_steps=self.scheduler_config. + num_scheduler_steps, + is_multi_step=self.scheduler_config.is_multi_step, + enable_chunking=enable_chunking) + + seq_groups.append( + ScheduledSequenceGroup(seq_group=seq_group, + token_chunk_size=num_new_tokens)) + budget.add_num_batched_tokens( + seq_group.request_id, + budget_token_count, + num_scheduled_tokens=num_new_tokens) + budget.add_num_seqs(seq_group.request_id, num_new_seqs) + + # Queue requests that couldn't be scheduled. + waiting_queue.extendleft(leftover_waiting_sequences) + if len(seq_groups) > 0: + self.prev_prompt = True + + return SchedulerPrefillOutputs( + seq_groups=seq_groups, + ignored_seq_groups=ignored_seq_groups, + num_lookahead_slots=self._get_num_lookahead_slots( + is_prefill=True, enable_chunking=enable_chunking)) + + def _schedule_default(self) -> SchedulerOutputs: + """Schedule queued requests. + + The current policy is designed to optimize the throughput. First, + it batches as many prefill requests as possible. And it schedules + decodes. If there's a pressure on GPU memory, decode requests can + be swapped or preempted. + """ + # Include running requests to the budget. + budget = SchedulingBudget( + token_budget=self.scheduler_config.max_num_batched_tokens, + max_num_seqs=self.scheduler_config.max_num_seqs, + ) + # Make sure we include num running seqs before scheduling prefill, + # so that we don't schedule beyond max_num_seqs for prefill. + for seq_group in self.running: + budget.add_num_seqs(seq_group.request_id, + seq_group.get_max_num_running_seqs()) + curr_loras = set( + seq_group.lora_int_id for seq_group in self.running + if seq_group.lora_int_id > 0) if self.lora_enabled else None + + prefills = SchedulerPrefillOutputs.create_empty() + running_scheduled = SchedulerRunningOutputs.create_empty() + swapped_in = SchedulerSwappedInOutputs.create_empty() + + # If any requests are swapped, prioritized swapped requests. + if not self.swapped: + prefills = self._schedule_prefills(budget, + curr_loras, + enable_chunking=False) + + if len(prefills.seq_groups + ) == 0 and self.scheduler_config.policy == "priority": + self._schedule_priority_preemption(budget) + + # Don't schedule decodes if prefills are scheduled. + # NOTE: If `_schedule_prefills` doesn't enable chunking, self.running + # only contains decode requests, not chunked prefills. + if len(prefills.seq_groups) == 0: + running_scheduled = self._schedule_running(budget, + curr_loras, + enable_chunking=False) + + # If any sequence group is preempted, do not swap in any sequence + # group. because it means there's no slot for new running requests. + if len(running_scheduled.preempted) + len( + running_scheduled.swapped_out) == 0: + swapped_in = self._schedule_swapped(budget, curr_loras) + + assert (budget.num_batched_tokens <= + self.scheduler_config.max_num_batched_tokens) + assert budget.num_curr_seqs <= self.scheduler_config.max_num_seqs + + # Update waiting requests. + self.waiting.extendleft(running_scheduled.preempted) + # Update new running requests. + if len(prefills.seq_groups) > 0: + self.running.extend([s.seq_group for s in prefills.seq_groups]) + + self.running.extend(running_scheduled.decode_seq_groups_list) + + if len(swapped_in.decode_seq_groups) > 0: + self.running.extend( + [s.seq_group for s in swapped_in.decode_seq_groups]) + + # Update swapped requests. + self.swapped.extend(running_scheduled.swapped_out) + preempted = (len(running_scheduled.preempted) + + len(running_scheduled.swapped_out)) + + # There should be no prefill from running queue because this policy + # doesn't allow chunked prefills. + assert len(running_scheduled.prefill_seq_groups) == 0 + assert len(swapped_in.prefill_seq_groups) == 0 + + # Merge lists + num_prefill_groups = len(prefills.seq_groups) + if num_prefill_groups > 0: + scheduled_seq_groups = prefills.seq_groups + scheduled_seq_groups.extend(running_scheduled.decode_seq_groups) + else: + scheduled_seq_groups = running_scheduled.decode_seq_groups + scheduled_seq_groups.extend(swapped_in.decode_seq_groups) + + blocks_to_copy = running_scheduled.blocks_to_copy + blocks_to_copy.extend(swapped_in.blocks_to_copy) + + ignored_seq_groups = prefills.ignored_seq_groups + ignored_seq_groups.extend(swapped_in.infeasible_seq_groups) + + return SchedulerOutputs( + scheduled_seq_groups=scheduled_seq_groups, + num_prefill_groups=num_prefill_groups, + num_batched_tokens=budget.num_scheduled_tokens, + blocks_to_swap_in=swapped_in.blocks_to_swap_in, + blocks_to_swap_out=running_scheduled.blocks_to_swap_out, + blocks_to_copy=blocks_to_copy, + ignored_seq_groups=ignored_seq_groups, + num_lookahead_slots=running_scheduled.num_lookahead_slots, + running_queue_size=len(self.running), + preempted=preempted, + ) + + def _schedule_chunked_prefill(self) -> SchedulerOutputs: + """Schedule queued requests. + + Chunked prefill allows to chunk prefill requests, batch them together + with decode requests. This policy 1. schedule as many decoding requests + as possible. 2. schedule chunked prefill requests that are not + finished. 3. schedule swapped request. 4. schedule new prefill + requests. + + The policy can sustain the high GPU utilization because it can put + prefill and decodes requests to the same batch, while it improves + inter token latency because decodes requests don't need to be blocked + by prefill requests. + """ + budget = SchedulingBudget( + token_budget=self.scheduler_config.max_num_batched_tokens, + max_num_seqs=self.scheduler_config.max_num_seqs, + ) + curr_loras: Set[int] = set() + + prefills = SchedulerPrefillOutputs.create_empty() + swapped_in = SchedulerSwappedInOutputs.create_empty() + + # Decoding should be always scheduled first by fcfs. + running_scheduled = self._schedule_running(budget, + curr_loras, + enable_chunking=True) + + # Schedule swapped out requests. + # If preemption happens, it means we don't have space for swap-in. + if len(running_scheduled.preempted) + len( + running_scheduled.swapped_out) == 0: + swapped_in = self._schedule_swapped(budget, curr_loras) + + # Schedule new prefills. + prefills = self._schedule_prefills(budget, + curr_loras, + enable_chunking=True) + + assert (budget.num_batched_tokens <= + self.scheduler_config.max_num_batched_tokens) + assert budget.num_curr_seqs <= self.scheduler_config.max_num_seqs + + # Update waiting requests. + self.waiting.extendleft(running_scheduled.preempted) + + # Update new running requests. + # By default, vLLM scheduler prioritizes prefills. + # Once chunked prefill is enabled, + # the policy is changed to prioritize decode requests. + self.running.extend( + [s.seq_group for s in swapped_in.decode_seq_groups]) + self.running.extend( + [s.seq_group for s in swapped_in.prefill_seq_groups]) + self.running.extend( + [s.seq_group for s in running_scheduled.decode_seq_groups]) + self.running.extend( + [s.seq_group for s in running_scheduled.prefill_seq_groups]) + self.running.extend([s.seq_group for s in prefills.seq_groups]) + + # Update swapped requests. + self.swapped.extend(running_scheduled.swapped_out) + return SchedulerOutputs( + scheduled_seq_groups=(prefills.seq_groups + + running_scheduled.prefill_seq_groups + + swapped_in.prefill_seq_groups + + running_scheduled.decode_seq_groups + + swapped_in.decode_seq_groups), + num_prefill_groups=(len(prefills.seq_groups) + + len(swapped_in.prefill_seq_groups) + + len(running_scheduled.prefill_seq_groups)), + num_batched_tokens=budget.num_scheduled_tokens, + blocks_to_swap_in=swapped_in.blocks_to_swap_in, + blocks_to_swap_out=running_scheduled.blocks_to_swap_out, + blocks_to_copy=running_scheduled.blocks_to_copy + + swapped_in.blocks_to_copy, + ignored_seq_groups=prefills.ignored_seq_groups + + swapped_in.infeasible_seq_groups, + num_lookahead_slots=running_scheduled.num_lookahead_slots, + running_queue_size=len(self.running), + preempted=(len(running_scheduled.preempted) + + len(running_scheduled.swapped_out)), + ) + + def _schedule(self) -> SchedulerOutputs: + """Schedule queued requests.""" + if self.scheduler_config.chunked_prefill_enabled: + return self._schedule_chunked_prefill() + else: + return self._schedule_default() + + def _can_append_slots(self, seq_group: SequenceGroup, + enable_chunking: bool) -> bool: + """Determine whether or not we have enough space in the KV cache to + continue generation of the sequence group. + """ + # It is True only for testing case to trigger artificial preemption. + if (self.enable_artificial_preemption + and random.uniform(0, 1) < ARTIFICIAL_PREEMPTION_PROB + and self.artificial_preempt_cnt > 0): + self.artificial_preempt_cnt -= 1 + return False + + is_prefill = seq_group.is_prefill() + num_lookahead_slots = self._get_num_lookahead_slots( + is_prefill, enable_chunking) + + if is_prefill and num_lookahead_slots > 0: + # Appending prefill slots only happens multi-step and + # chunked-prefill are enabled together. + assert self.scheduler_config.is_multi_step and enable_chunking + + return self.block_manager.can_append_slots( + seq_group=seq_group, num_lookahead_slots=num_lookahead_slots) + + def _allow_async_output_proc(self, seq_group: SequenceGroup) -> bool: + # async_output_proc is allowed only when we have a single sequence + # in the sequence group + no_single_seq = seq_group.sampling_params is None or ( + seq_group.sampling_params.n == 1) + return no_single_seq + + def schedule( + self + ) -> Tuple[List[SequenceGroupMetadata], SchedulerOutputs, bool]: + # Schedule sequence groups. + # This function call changes the internal states of the scheduler + # such as self.running, self.swapped, and self.waiting. + scheduler_start_time = time.perf_counter() + + begin_prefix_cache_step = getattr( + self.block_manager, "begin_prefix_cache_step", None) + if callable(begin_prefix_cache_step): + begin_prefix_cache_step() + scheduler_outputs: SchedulerOutputs = self._schedule() + drain_prefix_swaps = getattr( + self.block_manager, "get_and_reset_prefix_swaps", None) + if callable(drain_prefix_swaps): + prefix_swap_in, prefix_swap_out = drain_prefix_swaps() + if prefix_swap_in or prefix_swap_out: + if (scheduler_outputs.blocks_to_swap_in + or scheduler_outputs.blocks_to_swap_out): + raise RuntimeError( + "content-addressed CPU KV transfers cannot share a " + "scheduler step with request-level preemption swap") + # Both directions are valid for this tier: a GPU victim is + # preserved before that same physical slot is reused by H2D. + scheduler_outputs.blocks_to_swap_in.extend(prefix_swap_in) + scheduler_outputs.blocks_to_swap_out.extend(prefix_swap_out) + now = time.time() + + if not self.cache_config.enable_prefix_caching: + common_computed_block_nums = [] + + allow_async_output_proc: bool = self.use_async_output_proc + + # Create input data structures. + seq_group_metadata_list: List[SequenceGroupMetadata] = [] + for i, scheduled_seq_group in enumerate( + scheduler_outputs.scheduled_seq_groups): + seq_group = scheduled_seq_group.seq_group + token_chunk_size = scheduled_seq_group.token_chunk_size + seq_group.maybe_set_first_scheduled_time(now) + + seq_group_metadata = self._seq_group_metadata_cache[ + self.cache_id].get_object() + seq_group_metadata.seq_data.clear() + seq_group_metadata.block_tables.clear() + + # seq_id -> SequenceData + seq_data: Dict[int, SequenceData] = {} + # seq_id -> physical block numbers + block_tables: Dict[int, List[int]] = {} + + if seq_group.is_encoder_decoder(): + # Encoder associated with SequenceGroup + encoder_seq = seq_group.get_encoder_seq() + assert encoder_seq is not None + encoder_seq_data = encoder_seq.data + # Block table for cross-attention + # Also managed at SequenceGroup level + cross_block_table = self.block_manager.get_cross_block_table( + seq_group) + else: + encoder_seq_data = None + cross_block_table = None + + for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING): + seq_id = seq.seq_id + seq_data[seq_id] = seq.data + block_tables[seq_id] = self.block_manager.get_block_table(seq) + self.block_manager.access_all_blocks_in_seq(seq, now) + + common_computed_block_nums = [] + if self.cache_config.enable_prefix_caching: + raw_computed_block_nums = list( + self.block_manager.get_common_computed_block_ids( + seq_group.get_seqs(status=SequenceStatus.RUNNING))) + if not seq_group.is_prefill(): + common_computed_block_nums = raw_computed_block_nums + + do_sample = True + is_prompt = seq_group.is_prefill() + # We should send the metadata to workers when the first prefill + # is sent. Subsequent requests could be chunked prefill or decode. + is_first_prefill = False + gdn_restore_key = None + gdn_capture_points = None + gdn_evict_keys = None + gdn_segment_offsets = None + if is_prompt: + gdn_capture_points = [] + gdn_evict_keys = [] + gdn_segment_offsets = [] + seqs = seq_group.get_seqs() + # Prefill has only 1 sequence. + assert len(seqs) == 1 + num_computed_tokens = seqs[0].data.get_num_computed_tokens() + is_first_prefill = num_computed_tokens == 0 + logical_end_tokens = min( + seqs[0].data.get_len(), + num_computed_tokens + token_chunk_size) + if self.cache_config.enable_prefix_caching: + restore_key = self._gdn_request_restore_keys.get( + seq_group.request_id) + if is_first_prefill and restore_key is not None: + gdn_restore_key = restore_key + common_computed_block_nums = raw_computed_block_nums[ + :restore_key[0]] + if len(common_computed_block_nums) != restore_key[0]: + raise RuntimeError( + "GDN restore key exceeds the live KV prefix") + else: + # Once this request has started, the request-local Mamba + # state is authoritative. Never let a longer raw KV hit + # skip ahead without a matching recurrent state. + max_context_blocks = (num_computed_tokens + // self.cache_config.block_size) + common_computed_block_nums = raw_computed_block_nums[ + :max_context_blocks] + + restore_tokens = ( + restore_key[0] * self.cache_config.block_size + if is_first_prefill and restore_key is not None else 0) + if seq_group.metrics is not None and restore_tokens: + seq_group.metrics.num_cached_tokens = max( + seq_group.metrics.num_cached_tokens or 0, + restore_tokens) + + capture_targets = list( + self._gdn_request_capture_targets.get( + seq_group.request_id, ())) + if self._gdn_prefix_policy.policy == "fine32": + step_key = final_capture_key( + self.block_manager.get_content_hashes(seqs[0]), + logical_end_tokens, self.cache_config.block_size, + self._gdn_restore_mode, + self._gdn_replay_alignment) + capture_targets = ([step_key] + if step_key is not None else []) + if self._gdn_prefix_policy.policy != "off": + physical_context_tokens = ( + restore_tokens if is_first_prefill + else num_computed_tokens) + if (self._gdn_restore_mode == "hybrid64" + and self._gdn_prefix_policy.policy + == "admission64"): + gdn_segment_offsets = list( + canonical_direct_segment_offsets( + self.block_manager.get_content_hashes( + seqs[0]), + physical_context_tokens, + logical_end_tokens, + self.cache_config.block_size, + self.scheduler_config.max_num_batched_tokens)) + gdn_capture_points = list(capture_points_for_step( + capture_targets, physical_context_tokens, + logical_end_tokens, self.cache_config.block_size)) + gdn_evict_keys = list( + self._gdn_prefix_policy.admit( + key for _, key in gdn_capture_points)) + trace_update = getattr( + self.block_manager, "_bi100_update_cache_trace", None) + if callable(trace_update): + capture_actions = [] + for _, key in gdn_capture_points: + if self._gdn_prefix_policy.policy == "fine32": + reason = "fine32_chunk" + elif (capture_targets + and key == capture_targets[-1]): + reason = "final_prefill" + else: + reason = "repeated_branch" + capture_actions.append((key, reason)) + trace_update( + seqs[0], len(raw_computed_block_nums), + gdn_restore_key, capture_actions, + gdn_evict_keys, self._gdn_prefix_policy.policy) + # In the next iteration, all prompt tokens are not computed. + # It means the prefill is chunked, and we don't need sampling. + # NOTE: We use get_len instead of get_prompt_len because when + # a sequence is preempted, prefill includes previous generated + # output tokens. + if (token_chunk_size + num_computed_tokens < + seqs[0].data.get_len()): + do_sample = False + + if logical_end_tokens >= seqs[0].data.get_len(): + self._gdn_request_restore_keys.pop(seq_group.request_id, + None) + self._gdn_request_capture_targets.pop(seq_group.request_id, + None) + + # It assumes the scheduled_seq_groups is ordered by + # prefill < decoding. + if is_first_prefill or not self.scheduler_config.send_delta_data: + seq_group_metadata = SequenceGroupMetadata( + request_id=seq_group.request_id, + is_prompt=is_prompt, + seq_data=seq_data, + sampling_params=seq_group.sampling_params, + block_tables=block_tables, + do_sample=do_sample, + pooling_params=seq_group.pooling_params, + token_chunk_size=token_chunk_size, + lora_request=seq_group.lora_request, + computed_block_nums=common_computed_block_nums, + encoder_seq_data=encoder_seq_data, + cross_block_table=cross_block_table, + state=seq_group.state, + # `multi_modal_data` will only be present for the 1st comm + # between engine and worker. + # the subsequent comms can still use delta, but + # `multi_modal_data` will be None. + multi_modal_data=seq_group.multi_modal_data + if scheduler_outputs.num_prefill_groups > 0 else None, + mm_processor_kwargs=seq_group.mm_processor_kwargs, + prompt_adapter_request=seq_group.prompt_adapter_request, + gdn_restore_key=gdn_restore_key, + gdn_capture_points=gdn_capture_points, + gdn_evict_keys=gdn_evict_keys, + gdn_segment_offsets=gdn_segment_offsets, + ) + else: + # When SPMD mode is enabled, we only send delta data except for + # the first request to reduce serialization cost. + seq_data_delta = {} + for id, data in seq_data.items(): + seq_data_delta[id] = data.get_delta_and_reset() + seq_group_metadata = SequenceGroupMetadataDelta( + seq_data_delta, + seq_group.request_id, + block_tables, + is_prompt, + do_sample=do_sample, + token_chunk_size=token_chunk_size, + computed_block_nums=common_computed_block_nums, + gdn_restore_key=gdn_restore_key, + gdn_capture_points=gdn_capture_points, + gdn_evict_keys=gdn_evict_keys, + gdn_segment_offsets=gdn_segment_offsets, + ) + seq_group_metadata_list.append(seq_group_metadata) + + if allow_async_output_proc: + allow_async_output_proc = self._allow_async_output_proc( + seq_group) + + # Now that the batch has been created, we can assume all blocks in the + # batch will have been computed before the next scheduling invocation. + # This is because the engine assumes that a failure in model execution + # will crash the vLLM instance / will not retry. + for scheduled_seq_group in scheduler_outputs.scheduled_seq_groups: + self.block_manager.mark_blocks_as_computed( + scheduled_seq_group.seq_group, + scheduled_seq_group.token_chunk_size) + + self._seq_group_metadata_cache[self.next_cache_id].reset() + + scheduler_time = time.perf_counter() - scheduler_start_time + # Add this to scheduler time to all the sequences that are currently + # running. This will help estimate if the scheduler is a significant + # component in the e2e latency. + for seq_group in self.running: + if seq_group is not None and seq_group.metrics is not None: + if seq_group.metrics.scheduler_time is not None: + seq_group.metrics.scheduler_time += scheduler_time + else: + seq_group.metrics.scheduler_time = scheduler_time + + # Move to next cache (if exists) + self.cache_id = self.next_cache_id + + # Return results + return (seq_group_metadata_list, scheduler_outputs, + allow_async_output_proc) + + def fork_seq(self, parent_seq: Sequence, child_seq: Sequence) -> None: + self.block_manager.fork(parent_seq, child_seq) + + def free_seq(self, seq: Sequence) -> None: + """Free a sequence from a block table.""" + self.block_manager.free(seq) + + def _free_finished_seqs(self, seq_group: SequenceGroup) -> None: + """Free finished seqs in a sequence group.""" + for seq in seq_group.get_seqs(): + if seq.is_finished(): + self.free_seq(seq) + + def _free_finished_seq_group(self, seq_group: SequenceGroup) -> None: + if seq_group.is_finished(): + # Free cross-attention block table, if it exists + self._free_seq_group_cross_attn_blocks(seq_group) + + self._gdn_request_restore_keys.pop(seq_group.request_id, None) + self._gdn_request_capture_targets.pop(seq_group.request_id, None) + + # Add the finished requests to the finished requests list. + # This list will be used to update the Mamba cache in the + # next step. + self._finished_requests_ids.append(seq_group.request_id) + + # Free finished seqs + self._free_finished_seqs(seq_group) + + def free_finished_seq_groups(self) -> None: + remaining: Deque[SequenceGroup] = deque() + for seq_group in self.running: + self._free_finished_seq_group(seq_group) + if not seq_group.is_finished(): + remaining.append(seq_group) + + self.running = remaining + + # Handle async stopped sequence groups + # (ones that reached max model len) + if self._async_stopped: + for seq_group in self._async_stopped: + self._free_seq_group_cross_attn_blocks(seq_group) + self._finished_requests_ids.append(seq_group.request_id) + + # Free finished seqs + self._free_finished_seqs(seq_group) + + self._async_stopped.clear() + + def _allocate_and_set_running(self, seq_group: SequenceGroup) -> None: + self.block_manager.allocate(seq_group) + for seq in seq_group.get_seqs(status=SequenceStatus.WAITING): + seq.status = SequenceStatus.RUNNING + + def _append_slots(self, + seq_group: SequenceGroup, + blocks_to_copy: List[Tuple[int, int]], + enable_chunking: bool = False) -> None: + """Appends new slots to the sequences in the given sequence group. + + Args: + seq_group (SequenceGroup): The sequence group containing the + sequences to append slots to. + blocks_to_copy (List[Tuple[int, int]]): A list of tuple of two + ints, the first int is the source block index, and the second + int is the destination block index. This list is updated with + the new source and destination block indices for the appended + slots. + enable_chunking (bool): True if chunked prefill is enabled. + """ + is_prefill: bool = seq_group.is_prefill() + num_lookahead_slots: int = self._get_num_lookahead_slots( + is_prefill, enable_chunking) + + seq_group.init_multi_step_from_lookahead_slots( + num_lookahead_slots, + num_scheduler_steps=self.scheduler_config.num_scheduler_steps, + is_multi_step=self.scheduler_config.is_multi_step, + enable_chunking=enable_chunking) + + seq_status: Optional[SequenceStatus] = SequenceStatus.RUNNING + if self.scheduler_config.is_multi_step and enable_chunking: + # In multi-step chunked-prefill any sequence type can have + # slots appended. + seq_status = None + + for seq in seq_group.get_seqs(status=seq_status): + cows = self.block_manager.append_slots(seq, num_lookahead_slots) + if len(cows) > 0: + blocks_to_copy.extend(cows) + + def _preempt( + self, + seq_group: SequenceGroup, + blocks_to_swap_out: List[Tuple[int, int]], + preemption_mode: Optional[PreemptionMode] = None, + ) -> PreemptionMode: + # If preemption mode is not specified, we determine the mode as follows: + # We use recomputation by default since it incurs lower overhead than + # swapping. However, when the sequence group has multiple sequences + # (e.g., beam search), recomputation is not currently supported. In + # such a case, we use swapping instead. + # FIXME(woosuk): This makes our scheduling policy a bit bizarre. + # As swapped sequences are prioritized over waiting sequences, + # sequence groups with multiple sequences are implicitly prioritized + # over sequence groups with a single sequence. + # TODO(woosuk): Support recomputation for sequence groups with multiple + # sequences. This may require a more sophisticated CUDA kernel. + if self.user_specified_preemption_mode is None: + if seq_group.get_max_num_running_seqs() == 1: + preemption_mode = PreemptionMode.RECOMPUTE + else: + preemption_mode = PreemptionMode.SWAP + + elif self.user_specified_preemption_mode == "swap": + preemption_mode = PreemptionMode.SWAP + else: + preemption_mode = PreemptionMode.RECOMPUTE + + if self.num_cumulative_preemption % 50 == 0: + logger.warning( + "Sequence group %s is preempted by %s mode because there is " + "not enough KV cache space. This can affect the end-to-end " + "performance. Increase gpu_memory_utilization or " + "tensor_parallel_size to provide more KV cache memory. " + "total_num_cumulative_preemption=%d", seq_group.request_id, + preemption_mode, self.num_cumulative_preemption + 1) + self.num_cumulative_preemption += 1 + + if preemption_mode == PreemptionMode.RECOMPUTE: + self._preempt_by_recompute(seq_group) + elif preemption_mode == PreemptionMode.SWAP: + self._preempt_by_swap(seq_group, blocks_to_swap_out) + else: + raise AssertionError("Invalid preemption mode.") + return preemption_mode + + def _preempt_by_recompute( + self, + seq_group: SequenceGroup, + ) -> None: + seqs = seq_group.get_seqs(status=SequenceStatus.RUNNING) + assert len(seqs) == 1 + for seq in seqs: + seq.status = SequenceStatus.WAITING + self.free_seq(seq) + seq.reset_state_for_recompute() + + def _preempt_by_swap( + self, + seq_group: SequenceGroup, + blocks_to_swap_out: List[Tuple[int, int]], + ) -> None: + self._swap_out(seq_group, blocks_to_swap_out) + + def _swap_in( + self, + seq_group: SequenceGroup, + blocks_to_swap_in: List[Tuple[int, int]], + ) -> None: + mapping = self.block_manager.swap_in(seq_group) + blocks_to_swap_in.extend(mapping) + for seq in seq_group.get_seqs(status=SequenceStatus.SWAPPED): + seq.status = SequenceStatus.RUNNING + + def _swap_out( + self, + seq_group: SequenceGroup, + blocks_to_swap_out: List[Tuple[int, int]], + ) -> None: + if not self.block_manager.can_swap_out(seq_group): + # FIXME(woosuk): Abort the sequence group instead of aborting the + # entire engine. + raise RuntimeError( + "Aborted due to the lack of CPU swap space. Please increase " + "the swap space to avoid this error.") + mapping = self.block_manager.swap_out(seq_group) + blocks_to_swap_out.extend(mapping) + for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING): + seq.status = SequenceStatus.SWAPPED + + def _passed_delay(self, now: float) -> bool: + if self.prev_prompt: + self.last_prompt_latency = now - self.prev_time + self.prev_time, self.prev_prompt = now, False + # Delay scheduling prompts to let waiting queue fill up + if self.scheduler_config.delay_factor > 0 and self.waiting: + earliest_arrival_time = min( + [e.metrics.arrival_time for e in self.waiting]) + passed_delay = ( + (now - earliest_arrival_time) > + (self.scheduler_config.delay_factor * self.last_prompt_latency) + or not self.running) + else: + passed_delay = True + return passed_delay + + def _get_num_lookahead_slots(self, is_prefill: bool, + enable_chunking: bool) -> int: + """The number of slots to allocate per sequence per step, beyond known + token ids. Speculative decoding uses these slots to store KV activations + of tokens which may or may not be accepted. + + Speculative decoding does not yet support prefill, so we do not perform + lookahead allocation for prefill. + + When chunking is enabled with multi-step, we allocate lookahead slots + for the prefills for when the prefills turn into decodes in the first + step. + """ + if is_prefill: + if self.scheduler_config.is_multi_step and enable_chunking: + # num_lookahead_slots was introduced in the context of decodes, + # in Speculative Decoding. + # When the num_scheduler_steps is 8, say, then the + # num_lookahead_slots is 7. Meaning, we are doing a 1-step of + # decode anyways and we wish to do 7 more. + # + # "lookaheads" for prefills, is introduced in support for + # Chunked-Prefill in Multi-Step. + return self.scheduler_config.num_lookahead_slots + 1 + else: + return 0 + + return self.scheduler_config.num_lookahead_slots + + def _get_num_new_tokens(self, seq_group: SequenceGroup, + status: SequenceStatus, enable_chunking: bool, + budget: SchedulingBudget) -> int: + """Get the next new tokens to compute for a given sequence group + that's in a given `status`. + + The API could chunk the number of tokens to compute based on `budget` + if `enable_chunking` is True. If a sequence group has multiple + sequences (e.g., running beam search), it means it is in decoding + phase, so chunking doesn't happen. + + Returns 0 if the new token cannot be computed due to token budget. + """ + num_new_tokens = 0 + seqs = seq_group.get_seqs(status=status) + for seq in seqs: + num_new_tokens += seq.get_num_new_tokens() + assert num_new_tokens > 0 + # Chunk if a running request cannot fit in the given budget. + # If number of seq > 1, it means it is doing beam search + # in a decode phase. Do not chunk. + if enable_chunking and len(seqs) == 1: + remaining_token_budget = budget.remaining_token_budget() + if self.scheduler_config.is_multi_step: + # The current multi-step + chunked prefill capability does + # not actually support chunking prompts. + # + # Therefore, `num_new_tokens` is computed in the same fashion + # for both multi-step+chunked-prefill & + # multi-step+chunked-prefill+APC + # + # Prompts with more tokens than the current remaining budget + # are postponed to future scheduler steps + if num_new_tokens > self._get_prompt_limit(seq_group): + # If the seq_group is in prompt-stage, pass the + # num_new_tokens as-is so the caller can ignore + # the sequence. + pass + else: + num_new_tokens = 0 \ + if num_new_tokens > remaining_token_budget \ + else num_new_tokens + elif self.cache_config.enable_prefix_caching: + # When prefix caching is enabled, we always allocate + # the number of new tokens that is dividable by the block + # size to avoid partial block matching. + block_size = self.cache_config.block_size + remainder = budget.token_budget % block_size + if remainder != 0: + raise ValueError("When enabling chunked prefill and " + "prefix caching, max_num_batched_tokens " + "(chunk size) must be dividable by " + "block size, but got chunk_size " + f"({budget.token_budget}) % block_size " + f"({block_size}) = {remainder}") + if remaining_token_budget < num_new_tokens: + num_new_tokens = (remaining_token_budget // + block_size) * block_size + else: + num_new_tokens = min(num_new_tokens, remaining_token_budget) + return num_new_tokens diff --git a/qwen3_6_scripts/sequence.py b/qwen3_6_scripts/sequence.py new file mode 100644 index 0000000..b0bd8ff --- /dev/null +++ b/qwen3_6_scripts/sequence.py @@ -0,0 +1,1406 @@ +"""Sequence and its related classes.""" +import copy +import enum +from abc import ABC, abstractmethod +from array import array +from collections import defaultdict +from dataclasses import dataclass +from functools import cached_property, reduce +from typing import TYPE_CHECKING, Any, Callable, Dict, List, Mapping, Optional +from typing import Sequence as GenericSequence +from typing import Set, Tuple, Union, cast + +import msgspec +import torch + +from vllm.inputs import EncoderDecoderLLMInputs, LLMInputs +from vllm.inputs.parse import is_valid_encoder_decoder_llm_inputs +from vllm.lora.request import LoRARequest +from vllm.pooling_params import PoolingParams +from vllm.prompt_adapter.request import PromptAdapterRequest +from vllm.sampling_params import SamplingParams +from vllm.spec_decode.metrics import SpecDecodeWorkerMetrics + +if TYPE_CHECKING: + from vllm.multimodal.base import MultiModalDataDict + +VLLM_TOKEN_ID_ARRAY_TYPE = "l" + +VLLM_INVALID_TOKEN_ID = -1 + + +# We use dataclass for now because it is used for +# openai server output, and msgspec is not serializable. +# TODO(sang): Fix it. +@dataclass +class Logprob: + """Infos for supporting OpenAI compatible logprobs and token ranks. + + Attributes: + logprob: The logprob of chosen token + rank: The vocab rank of chosen token (>=1) + decoded_token: The decoded chosen token index + """ + logprob: float + rank: Optional[int] = None + decoded_token: Optional[str] = None + + +# {token_id -> logprob} per each sequence group. None if the corresponding +# sequence group doesn't require prompt logprob. +PromptLogprobs = List[Optional[Dict[int, Logprob]]] +# {token_id -> logprob} for each sequence group. +SampleLogprobs = List[Dict[int, Logprob]] + + +class SequenceStatus(enum.IntEnum): + """Status of a sequence.""" + WAITING = 0 + RUNNING = 1 + SWAPPED = 2 + # Note: anything after SWAPPED (2) will be considered + # as a finished status. + FINISHED_STOPPED = 3 + FINISHED_LENGTH_CAPPED = 4 + FINISHED_ABORTED = 5 + FINISHED_IGNORED = 6 + + @staticmethod + def is_finished(status: "SequenceStatus") -> bool: + return status > SequenceStatus.SWAPPED + + @staticmethod + def get_finished_reason(status: "SequenceStatus") -> Union[str, None]: + if status == SequenceStatus.FINISHED_STOPPED: + finish_reason = "stop" + elif status == SequenceStatus.FINISHED_LENGTH_CAPPED: + finish_reason = "length" + elif status == SequenceStatus.FINISHED_ABORTED: + finish_reason = "abort" + elif status == SequenceStatus.FINISHED_IGNORED: + # The ignored sequences are the sequences whose prompt lengths + # are longer than the model's length cap. Therefore, the stop + # reason should also be "length" as in OpenAI API. + finish_reason = "length" + else: + finish_reason = None + return finish_reason + + +class SequenceStage(enum.Enum): + PREFILL = enum.auto() + DECODE = enum.auto() + + +@dataclass +class RequestMetrics: + """Metrics associated with a request. + + Attributes: + arrival_time: The time when the request arrived. + first_scheduled_time: The time when the request was first scheduled. + first_token_time: The time when the first token was generated. + time_in_queue: The time the request spent in the queue. + finished_time: The time when the request was finished. + scheduler_time: The time spent in the scheduler when this request was + being considered by the scheduler. + model_forward_time: The time spent in the model forward pass when this + request was in the batch. + model_execute_time: The time spent in the model execute function. This + will include model forward, block/sync across + workers, cpu-gpu sync time and sampling time. + """ + arrival_time: float + last_token_time: float + first_scheduled_time: Optional[float] + first_token_time: Optional[float] + time_in_queue: Optional[float] + finished_time: Optional[float] = None + scheduler_time: Optional[float] = None + model_forward_time: Optional[float] = None + model_execute_time: Optional[float] = None + num_cached_tokens: Optional[int] = None + + +class SequenceDataDelta( + msgspec.Struct, + array_like=True, # type: ignore[call-arg] + omit_defaults=True): # type: ignore[call-arg] + """Delta SequenceData to send to workers per step.""" + # A new token to be appended to existing SequenceData. + new_output_token_ids: List[int] + # Overwriting existing `cumulative_logprob` + new_cumulative_logprob: float + # Overwriting existing `num_computed_tokens`. + new_num_computed_tokens: int + # Overwriting existing `stage`. + new_stage: SequenceStage + + +class SequenceData(msgspec.Struct, + omit_defaults=True): # type: ignore[call-arg] + """Data associated with a sequence. + + Args: + prompt_token_ids: The token IDs of the prompt. + output_token_ids: The token IDs of the output. Set to an empty list if + None. + + Attributes: + prompt_token_ids: The token IDs of the prompt. + output_token_ids: The token IDs of the output. + cumulative_logprob: The cumulative log probability of the output. + """ + # NOTE: we cannot use Union[List, array] because msgspec cannot support + # union of 2 list types. + _prompt_token_ids: array + _output_token_ids: array = msgspec.field( + default_factory=lambda: array(VLLM_TOKEN_ID_ARRAY_TYPE, [])) + + ### The below fields should not be passed as an argument ### + _cumulative_logprob: float = 0.0 + _prompt_token_ids_tuple: Tuple[int, + ...] = msgspec.field(default_factory=tuple) + # The number of tokens that are computed (that run against the model). + _num_computed_tokens: int = 0 + _stage: SequenceStage = SequenceStage.PREFILL + _cached_all_token_ids: List[int] = msgspec.field(default_factory=list) + + # It is used to get delta input. It is reset when `get_delta_and_reset` + # is called. + _new_appended_tokens: List[int] = msgspec.field(default_factory=list) + + # It is used to compute mrope_position_ids. + _mrope_position_delta: Optional[int] = None + + @staticmethod + def from_token_counts(*token_counts: Tuple[int, int]) -> "SequenceData": + if len(token_counts) == 0: + return SequenceData.from_seqs([]) + + arrs = [ + array(VLLM_TOKEN_ID_ARRAY_TYPE, [token_id]) * count + for token_id, count in token_counts + ] + + return SequenceData(reduce(array.__add__, arrs)) + + @staticmethod + def from_seqs( + prompt_token_ids: GenericSequence[int], + output_token_ids: Optional[GenericSequence[int]] = None, + ) -> "SequenceData": + prompt_token_ids_arr = array(VLLM_TOKEN_ID_ARRAY_TYPE, + prompt_token_ids) + + if output_token_ids is None: + return SequenceData(prompt_token_ids_arr) + + output_token_ids_arr = array(VLLM_TOKEN_ID_ARRAY_TYPE, + output_token_ids) + + return SequenceData(prompt_token_ids_arr, + _output_token_ids=output_token_ids_arr) + + def __post_init__(self) -> None: + assert self._prompt_token_ids.typecode == "l" + assert self._output_token_ids.typecode == "l" + self._prompt_token_ids_tuple: Tuple[int, ...] = tuple( + self._prompt_token_ids) + self._update_cached_all_tokens() + + def _update_cached_all_tokens(self): + assert isinstance(self._prompt_token_ids, array) + assert isinstance(self._output_token_ids, array) + self._cached_all_token_ids: List[int] = list(self._prompt_token_ids + + self._output_token_ids) + + @property + def cumulative_logprob(self) -> float: + return self._cumulative_logprob + + @property + def prompt_token_ids(self) -> Tuple[int, ...]: + return self._prompt_token_ids_tuple + + @prompt_token_ids.setter + def prompt_token_ids(self, new_prompt_token_ids) -> None: + raise NotImplementedError + + @property + def prompt_token_ids_array(self) -> array: + """Return the prompt token ids in array type. + + Note that the array is in "I" type, and it is not compatible + with torch.long (2 bytes vs 4 bytes). So beware of the usage. + """ + return self._prompt_token_ids + + @property + def output_token_ids(self) -> Tuple[int, ...]: + return tuple(self._output_token_ids) + + @output_token_ids.setter + def output_token_ids(self, new_output_token_ids: List[int]) -> None: + self._output_token_ids = array(VLLM_TOKEN_ID_ARRAY_TYPE, + new_output_token_ids) + self._update_cached_all_tokens() + + @property + def output_token_ids_array(self) -> array: + """Return the prompt token ids in array type. + + Note that the array is in "I" type, and it is not compatible + with torch.long (2 bytes vs 4 bytes). So beware of the usage. + """ + assert isinstance(self._output_token_ids, array) + return self._output_token_ids + + @property + def mrope_position_delta(self) -> Optional[int]: + return self._mrope_position_delta + + @mrope_position_delta.setter + def mrope_position_delta(self, new_mrope_position_delta): + self._mrope_position_delta = new_mrope_position_delta + + def append_token_id(self, token_id: int, logprob: float) -> None: + self._output_token_ids.append(token_id) + self._new_appended_tokens.append(token_id) + self._cached_all_token_ids.append(token_id) + self._cumulative_logprob += logprob + + def get_len(self) -> int: + return len(self._output_token_ids) + len(self._prompt_token_ids) + + def get_prompt_len(self) -> int: + return len(self._prompt_token_ids) + + def get_output_len(self) -> int: + return len(self._output_token_ids) + + def get_token_ids(self) -> List[int]: + return self._cached_all_token_ids + + def get_prefix_token_ids( + self, num_tokens: int + ) -> Tuple[Tuple[int, ...], Optional[Tuple[int, ...]]]: + """Get prefix tokens, and make the return value hashable""" + prompt_length = self.get_prompt_len() + if num_tokens > prompt_length: + return (self._prompt_token_ids_tuple, + tuple(self._output_token_ids[:num_tokens - prompt_length])) + else: + return (self._prompt_token_ids_tuple[:num_tokens], None) + + def get_num_computed_tokens(self) -> int: + """Return the number of prefill tokens that are already computed.""" + return self._num_computed_tokens + + def update_num_computed_tokens(self, num_new_computed_tokens: int): + """Update number of tokens computed so far.""" + self._num_computed_tokens += num_new_computed_tokens + assert self._num_computed_tokens <= self.get_len(), ( + self._num_computed_tokens, self.get_len()) + # If all tokens are computed, it means it is in decoding phase. + if self.get_num_uncomputed_tokens() == 0: + self._stage = SequenceStage.DECODE + + def reset_state_for_recompute(self) -> None: + """Reset the number of computed tokens from this sequence. It is + supposed to be called when a sequence needs to be started from + the beginning again (e.g., sequence is preempted). + """ + self._num_computed_tokens = 0 + self._stage = SequenceStage.PREFILL + self._new_appended_tokens = [] + + def get_num_uncomputed_tokens(self) -> int: + """Return the number of prefill tokens that are not computed.""" + # we use `get_len()` which includes prompt_len + output_len instead + # of prompt_len here. This is because during recompute we need to + # prefill for both prompt and output. + return self.get_len() - self.get_num_computed_tokens() + + def get_last_token_id(self) -> int: + if not self._output_token_ids: + return self._prompt_token_ids[-1] + return self._output_token_ids[-1] + + def get_prompt_token_ids(self) -> Tuple[int, ...]: + return self.prompt_token_ids + + def get_output_token_ids(self) -> Tuple[int, ...]: + return self.output_token_ids + + def get_delta_and_reset(self) -> SequenceDataDelta: + delta = SequenceDataDelta(self._new_appended_tokens, + self._cumulative_logprob, + self.get_num_computed_tokens(), self.stage) + # Reset delta state. + self._new_appended_tokens = [] + return delta + + def apply_delta(self, delta: SequenceDataDelta): + self._num_computed_tokens = delta.new_num_computed_tokens + self._cumulative_logprob = delta.new_cumulative_logprob + self._stage = delta.new_stage + self._output_token_ids.extend(delta.new_output_token_ids) + self._cached_all_token_ids.extend(delta.new_output_token_ids) + + @property + def stage(self) -> SequenceStage: + return self._stage + + def __repr__(self) -> str: + return (f"SequenceData(" + f"prompt_token_ids={self._prompt_token_ids}, " + f"output_token_ids={self.output_token_ids}, " + f"cumulative_logprob={self.cumulative_logprob}, " + f"get_num_computed_tokens={self.get_num_computed_tokens()}") + + +class Sequence: + """Stores the data, status, and block information of a sequence. + + The sequence is constructed from the LLMInputs instance passed + in through the `inputs` constructor argument. + + For encoder/decoder models, LLMInputs encapsulates both a + decoder and encoder prompt, creating an ambiguity about which + prompt to construct the sequence from. The `from_decoder_prompt` + constructor argument signals whether to construct the Sequence + from the LLMInputs decoder prompt, or encoder prompt. + + Args: + seq_id: The ID of the sequence. + inputs: The inputs of the sequence. + block_size: The block size of the sequence. Should be the same as the + block size used by the block manager and cache engine. + eos_token_id: The end-of-sequence (EOS) token id recognized by this LLM. + lora_request: LoRA request. + prompt_adapter_request: Prompt Adapter request. + from_decoder_prompt: Construct Sequence from LLMInputs decoder prompt + (True) or encoder prompt (False.) Must be True + for decoder-only model. + + """ + + def __init__( + self, + seq_id: int, + inputs: "LLMInputs", + block_size: int, + eos_token_id: Optional[int] = None, + lora_request: Optional[LoRARequest] = None, + prompt_adapter_request: Optional[PromptAdapterRequest] = None, + from_decoder_prompt: bool = True, + ) -> None: + self.seq_id = seq_id + self.inputs = inputs + self.block_size = block_size + self.eos_token_id = eos_token_id + self.lora_request = lora_request + self.prompt_adapter_request = prompt_adapter_request + self.from_decoder_prompt = from_decoder_prompt + + # For decoder-only models, a Sequence is constructed + # from an LLMInputs instance (the `inputs` arg.) + # + # For encoder/decoder models the same `inputs` + # instance could be utilized to construct either an + # encoder sequence or a decoder sequence, because + # `LLMInputs` has both decoder- and encoder-oriented + # member variables (i.e. it encapsulates both an encoder + # and a decoder prompt.) The decision of which type of sequence + # to generate is determined by the `from_decoder_prompt` argument. + # + # When constructing a encoder sequence + # (`from_decoder_prompt` False) it matters that + # the `LLMInputs` instance stored in `inputs` is valid + # in the sense that its encoder-related member variables are + # populated; below, an exception is raised if this is + # not the case. + # + # When constructing a decoder sequence (`from_decoder_prompt` True) + # it does not matter whether `inputs` has its encoder-related + # member variables populated. + if not (from_decoder_prompt + or is_valid_encoder_decoder_llm_inputs(inputs)): + raise ValueError("Cannot extract encoder input prompt from " + f"invalid input {inputs}; did you forget the " + "encoder input prompt fields?") + + self.data = SequenceData.from_seqs(self.prompt_token_ids) + self.output_logprobs: SampleLogprobs = [] + self.output_text = "" + + self.status = SequenceStatus.WAITING + self.stop_reason: Union[int, str, None] = None + + # These are used to keep track of delta outputs + self._last_output_token_ids_offset: int = 0 + self._last_output_text_offset: int = 0 + + # Used for incremental detokenization + self.prefix_offset = 0 + self.read_offset = 0 + # Input + output tokens + self.tokens: Optional[List[str]] = None + + @property + def n_blocks(self) -> int: + return (self.get_len() + self.block_size - 1) // self.block_size + + @cached_property + def prompt(self) -> Optional[str]: + # Select decoder or encoder input prompt str, as appropriate + prompt_key: str = ("prompt" + if self.from_decoder_prompt else "encoder_prompt") + + return cast(Optional[str], self.inputs.get(prompt_key)) + + @cached_property + def prompt_token_ids(self) -> List[int]: + # Select decoder or encoder input prompt token ids, as appropriate + prompt_token_ids_key: str = ("prompt_token_ids" + if self.from_decoder_prompt else + "encoder_prompt_token_ids") + + # Cache computed prompt token ids + return cast(List[int], self.inputs.get(prompt_token_ids_key)) + + @property + def multi_modal_data(self) -> "MultiModalDataDict": + if self.inputs.get("multi_modal_data") and self.inputs.get( + "encoder_multi_modal_data"): + raise ValueError( + "Multi-modal data in both encoder and decoder is not supported." + ) + inputs = self.inputs + return self.inputs.get("multi_modal_data") or (cast( + EncoderDecoderLLMInputs, + inputs).get("encoder_multi_modal_data")) or {} + + @property + def mm_processor_kwargs(self) -> Dict[str, Any]: + return self.inputs.get("mm_processor_kwargs") or {} + + @property + def lora_int_id(self) -> int: + return self.lora_request.lora_int_id if self.lora_request else 0 + + @property + def prompt_adapter_id(self) -> int: + return self.prompt_adapter_request.prompt_adapter_id \ + if self.prompt_adapter_request else 0 + + def get_output_text_to_return(self, buffer_length: int, + delta: bool) -> str: + """If delta is True, only new text since the last call to + this method is returned""" + + # We return the full output text if the sequence is finished. + truncate = buffer_length and not self.is_finished() + if not delta: + return self.output_text[:-buffer_length] if truncate else ( + self.output_text) + length = len(self.output_text) + if truncate: + length -= buffer_length + last_offset = self._last_output_text_offset + if last_offset < length: + self._last_output_text_offset = length + return self.output_text[last_offset:length] + return "" + + def get_output_token_ids_to_return( + self, delta: bool) -> Union[GenericSequence[int], int]: + """If delta is True, only new tokens since the last call to + this method are returned""" + if not delta: + return self.get_output_token_ids() + + output_len = self.get_output_len() + + # Get the number of new tokens + num_new_tokens = output_len - self._last_output_token_ids_offset + self._last_output_token_ids_offset = output_len + + # Return new tokens + if num_new_tokens == 0: + # During chunked prefill steps with no output yet, num_new_tokens=0. + # Python's [-0:] == [0:] returns the ENTIRE list — guard against this. + return [] + + if num_new_tokens == 1: + # Optimization for single decode token case + # (which is what we have most of the time) + return self.data._cached_all_token_ids[-1] + + return self.data._cached_all_token_ids[-num_new_tokens:] + + def hash_of_block(self, logical_idx: int) -> int: + # TODO This can produce incorrect hash when block size > prompt size + + # Compute the number of tokens in the sequence + # TODO: The current hashing function is O(L^2). We should optimize + # this in the future. + num_tokens = self.num_hashed_tokens_of_block(logical_idx) + hashed_tokens = self.data.get_prefix_token_ids(num_tokens) + return hash((hashed_tokens, self.lora_int_id)) + + def num_hashed_tokens_of_block(self, logical_idx: int): + return logical_idx * self.block_size + self.block_size + + def reset_state_for_recompute(self): + """Reset the sequence states for recomputation.""" + self.data.reset_state_for_recompute() + + def append_token_id(self, token_id: int, logprobs: Dict[int, + Logprob]) -> None: + assert token_id in logprobs + self.output_logprobs.append(logprobs) + self.data.append_token_id(token_id, logprobs[token_id].logprob) + + def get_len(self) -> int: + return self.data.get_len() + + def get_prompt_len(self) -> int: + return self.data.get_prompt_len() + + def get_output_len(self) -> int: + return self.data.get_output_len() + + def get_token_ids(self) -> List[int]: + return self.data.get_token_ids() + + def get_prompt_token_ids(self) -> Tuple[int, ...]: + return self.data.get_prompt_token_ids() + + def get_last_token_id(self) -> int: + return self.data.get_last_token_id() + + def get_output_token_ids(self) -> Tuple[int, ...]: + return self.data.get_output_token_ids() + + def get_cumulative_logprob(self) -> float: + return self.data.cumulative_logprob + + def is_finished(self) -> bool: + return SequenceStatus.is_finished(self.status) + + def fork(self, new_seq_id: int) -> "Sequence": + new_seq = copy.deepcopy(self) + new_seq.seq_id = new_seq_id + return new_seq + + def get_num_new_tokens(self) -> int: + """Get the number of new tokens to be computed. + + Returns: + The new number of tokens to be computed. I.e., 1 for decode, or + the remaining prompt size for prefill. + """ + if self.data.stage == SequenceStage.DECODE: + return 1 + return self.data.get_num_uncomputed_tokens() + + def is_prefill(self) -> bool: + return self.data.stage == SequenceStage.PREFILL + + def __repr__(self) -> str: + return (f"Sequence(seq_id={self.seq_id}, " + f"status={self.status.name}, " + f"num_blocks={self.n_blocks}, ") + + +class SequenceGroupState(msgspec.Struct, + omit_defaults=True): # type: ignore[call-arg] + """Mutable state tied to a specific sequence group""" + + # for multi-step decoding + num_steps: int = 1 + current_step: int = 0 + + @property + def remaining_steps(self) -> int: + return self.num_steps - self.current_step + + +class SequenceGroup: + """A group of sequences that are generated from the same prompt. + + Args: + request_id: The ID of the request. + seqs: The list of sequences. + sampling_params: The sampling parameters used to generate the outputs. + arrival_time: The arrival time of the request. + lora_request: LoRA request. + embeddings: The embeddings vectors of the prompt of the sequence group + for an embedding model. + pooling_params: The pooling parameters used to generate the pooling + for an embedding model. + encoder_seq: Optional, the single encoder sequence. Should be None + unless you are working with an encoder/decoder model. + trace_headers: OpenTelemetry trace headers. + prompt_adapter_request: Prompt Adapter request. + priority: User-defined priority of the request. + """ + + def __init__( + self, + request_id: str, + seqs: List[Sequence], + arrival_time: float, + sampling_params: Optional[SamplingParams] = None, + lora_request: Optional[LoRARequest] = None, + embeddings: Optional[List[float]] = None, + pooling_params: Optional[PoolingParams] = None, + encoder_seq: Optional[Sequence] = None, + trace_headers: Optional[Mapping[str, str]] = None, + prompt_adapter_request: Optional[PromptAdapterRequest] = None, + priority: int = 0, + ) -> None: + self.request_id = request_id + self.seqs = seqs + self.arrival_time = arrival_time + self.is_single_seq = len(seqs) == 1 + self.seqs_dict = {seq.seq_id: seq for seq in seqs} + + self.sampling_params = sampling_params + self.metrics = RequestMetrics(arrival_time=arrival_time, + last_token_time=arrival_time, + first_scheduled_time=None, + first_token_time=None, + time_in_queue=None) + self.lora_request = lora_request + self.prompt_logprobs: Optional[PromptLogprobs] = None + self.state = SequenceGroupState() + self.embeddings = embeddings + self.pooling_params = pooling_params + self.prompt_adapter_request = prompt_adapter_request + self.encoder_seq = encoder_seq + self.trace_headers = trace_headers + self.priority = priority + + self.cached_request_output = None + + @property + def prompt(self) -> Optional[str]: + # All sequences in the group should have the same prompt. + # We use the prompt of an arbitrary sequence. + return self.seqs[0].prompt + + @property + def prompt_token_ids(self) -> List[int]: + # All sequences in the group should have the same prompt. + # We use the prompt of an arbitrary sequence. + return self.seqs[0].prompt_token_ids + + @property + def encoder_prompt(self) -> Optional[str]: + # There are either 0 or 1 encoder sequences + # If one is present, its prompt is distinct + # from the decoder's. + return (self.encoder_seq.prompt + if self.encoder_seq is not None else None) + + @property + def encoder_prompt_token_ids(self) -> Optional[List[int]]: + # There are either 0 or 1 encoder sequences + # If one is present, its prompt token ids are + # distinct from the decoder's. + return (self.encoder_seq.prompt_token_ids + if self.encoder_seq is not None else None) + + @property + def multi_modal_data(self) -> "MultiModalDataDict": + # All sequences in the group should have the same multi-modal data. + # We use the multi-modal data of an arbitrary sequence. + return self.seqs[0].multi_modal_data + + @property + def mm_processor_kwargs(self) -> Dict[str, Any]: + # As with multi-modal data, all sequences in the group should have the + # same processor kwargs (i.e., mm_processor_kwargs are optionally + # provided per request; note that are independent of whether the model + # decoder-only or an encoder-decoder). + return self.seqs[0].mm_processor_kwargs + + @property + def lora_int_id(self) -> int: + return self.lora_request.lora_int_id if self.lora_request else 0 + + @property + def prompt_adapter_id(self) -> int: + return self.prompt_adapter_request.prompt_adapter_id \ + if self.prompt_adapter_request else 0 + + @property + def prompt_adapter_num_virtual_tokens(self) -> int: + return self.prompt_adapter_request.prompt_adapter_num_virtual_tokens\ + if self.prompt_adapter_request else 0 + + def init_multi_step(self, num_steps: int) -> None: + self.state.num_steps = num_steps + self.state.current_step = 0 + + def init_multi_step_from_lookahead_slots(self, num_lookahead_slots: int, + num_scheduler_steps: int, + is_multi_step: bool, + enable_chunking: bool) -> None: + + if not is_multi_step: + self.init_multi_step(num_steps=num_scheduler_steps) + return + + # Multi-Step case + is_prefill = self.is_prefill() + + # The asserts below reflect the expectations of the current system. + if is_prefill and enable_chunking: + assert num_lookahead_slots == num_scheduler_steps + self.init_multi_step(num_steps=num_lookahead_slots) + else: + is_decode: bool = not is_prefill + # If it is a prefill, num_lookahead_slots must be 0 + assert num_lookahead_slots == 0 or is_decode + # If it is a decode, num_lookahead_slots + 1 must match + # the scheduler steps. + assert num_lookahead_slots + 1 == num_scheduler_steps or is_prefill + self.init_multi_step(num_steps=num_lookahead_slots + 1) + + def get_last_latency(self, now: float) -> Optional[float]: + """Sets the last token time for Request level timings.""" + # If still in prefill phase, raise Error. + if self.is_prefill(): + raise ValueError( + "seq_group.get_last_latency() should not be called " + "if the seq_group is in prefill phase.") + + # Otherwise return token latency. + latency = now - self.metrics.last_token_time + self.metrics.last_token_time = now + return latency + + def maybe_set_first_token_time(self, time: float) -> None: + """Sets the first token time for Request level timings.""" + # Note: in a case where a sequence_group is swapped and + # recomputed, the time between iterations is counted + # in TPOT, rather than recalculating TTFT (since from the ) + # POV of the user, there is simply a long generation delay. + if (self.metrics.first_token_time is None + and self.seqs[0].get_output_len() == 1): + self.metrics.first_token_time = time + + def maybe_set_first_scheduled_time(self, time: float) -> None: + """Sets the first scheduled time and time in queue for Request + level timings.""" + if self.metrics.first_scheduled_time is None: + self.metrics.first_scheduled_time = time + self.metrics.time_in_queue = time - self.metrics.arrival_time + + def set_finished_time(self, time: Optional[float]) -> None: + """Sets the finished time for Request level timings.""" + self.metrics.finished_time = time + + def get_max_num_running_seqs(self) -> int: + """The maximum number of sequences running in parallel in the remaining + lifetime of the request.""" + if self.sampling_params: + n = self.sampling_params.n + assert isinstance(n, int) + if n > self.num_seqs(): + # At prompt stage, the sequence group is not yet filled up + # and only have one sequence running. However, in the + # generation stage, we will have `n` sequences + # running. + return n + # At sampling stages, return the number of actual sequences + # that are not finished yet. + return self.num_unfinished_seqs() + + def get_seqs( + self, + status: Optional[SequenceStatus] = None, + ) -> List[Sequence]: + if status is None: + return self.seqs + + if self.is_single_seq: + return self.seqs if self.seqs[0].status == status else [] + + return [seq for seq in self.seqs if seq.status == status] + + def is_encoder_decoder(self) -> bool: + return self.encoder_seq is not None + + def get_encoder_seq(self) -> Optional[Sequence]: + return self.encoder_seq + + def get_unfinished_seqs(self) -> List[Sequence]: + if self.is_single_seq: + return self.seqs if not self.seqs[0].is_finished() else [] + + return [seq for seq in self.seqs if not seq.is_finished()] + + def get_finished_seqs(self) -> List[Sequence]: + if self.is_single_seq: + return self.seqs if self.seqs[0].is_finished() else [] + + return [seq for seq in self.seqs if seq.is_finished()] + + def update_num_computed_tokens(self, num_new_computed_tokens: int): + """Update number of tokens computed so far.""" + for seq in self.seqs: + if not seq.is_finished(): + seq.data.update_num_computed_tokens(num_new_computed_tokens) + + def get_num_uncomputed_tokens(self) -> int: + num_uncomputed_tokens = 0 + for seq in self.seqs: + if not seq.is_finished(): + num_uncomputed_tokens += seq.data.get_num_uncomputed_tokens() + return num_uncomputed_tokens + + def num_seqs(self, status: Optional[SequenceStatus] = None) -> int: + # Optimization. We don't need to call get_seqs if we don't need to + # filter by states. + if status is None: + return len(self.seqs) + + if self.is_single_seq: + return 1 if self.seqs[0].status == status else 0 + + return len(self.get_seqs(status)) + + def num_unfinished_seqs(self) -> int: + if self.is_single_seq: + return 1 if not self.seqs[0].is_finished() else 0 + + return len(self.get_unfinished_seqs()) + + def num_finished_seqs(self) -> int: + if self.is_single_seq: + return 1 if self.seqs[0].is_finished() else 0 + + return len(self.get_finished_seqs()) + + def find(self, seq_id: int) -> Sequence: + if seq_id not in self.seqs_dict: + raise ValueError(f"Sequence {seq_id} not found.") + return self.seqs_dict[seq_id] + + def add(self, seq: Sequence) -> None: + if seq.seq_id in self.seqs_dict: + raise ValueError(f"Sequence {seq.seq_id} already exists.") + self.seqs_dict[seq.seq_id] = seq + self.seqs.append(seq) + self.is_single_seq = len(self.seqs) == 1 + + def remove(self, seq_id: int) -> None: + seq = self.seqs_dict.pop(seq_id, None) + if seq is None: + raise ValueError(f"Sequence {seq_id} not found.") + self.seqs.remove(seq) + self.is_single_seq = len(self.seqs) == 1 + + def is_finished(self) -> bool: + if self.is_single_seq: + return self.seqs[0].is_finished() + + return all(seq.is_finished() for seq in self.seqs) + + def is_prefill(self) -> bool: + # Every sequence should be in the same stage. + return self.seqs[0].is_prefill() + + def __repr__(self) -> str: + return (f"SequenceGroup(request_id={self.request_id}, " + f"sampling_params={self.sampling_params}, " + f"num_seqs={len(self.seqs)})") + + +class SequenceGroupMetadataDelta( + msgspec.Struct, + tag=True, # type: ignore[call-arg] + array_like=True, # type: ignore[call-arg] + omit_defaults=True): # type: ignore[call-arg] + """Delta of SequenceGroupMetadata. + + After sending the first SequenceGroupMetadata, vLLM scheduler + only sends delta to reduce the data payload size. + """ + seq_data_delta: Dict[int, SequenceDataDelta] + request_id: str + block_tables: Dict[int, List[int]] + is_prompt: bool + do_sample: bool = True + token_chunk_size: Optional[int] = None + computed_block_nums: Optional[List[int]] = None + state: Optional[SequenceGroupState] = msgspec.field( + default_factory=lambda: SequenceGroupState()) + # BI100 hybrid prefix-cache actions. Fields are appended for msgspec wire + # compatibility with the pre-existing array-like structure. + gdn_restore_key: Optional[Tuple[int, bytes]] = None + gdn_capture_points: Optional[List[Tuple[int, Tuple[int, bytes]]]] = None + gdn_evict_keys: Optional[List[Tuple[int, bytes]]] = None + gdn_segment_offsets: Optional[List[int]] = None + + +class SequenceGroupMetadata( + msgspec.Struct, + tag=True, # type: ignore[call-arg] + array_like=True, # type: ignore[call-arg] + omit_defaults=True): # type: ignore[call-arg] + """Metadata for a sequence group. Used to create `AttentionMetadata`. + + Args: + request_id: The ID of the request. + is_prompt: Whether the request is at prompt stage. + seq_data: The sequence data. (Seq id -> sequence data) + sampling_params: The sampling parameters used to generate the outputs. + block_tables: The block tables. (Seq id -> list of physical block + numbers) + do_sample: True if sampling is required. Sampling is not required when + e.g., prefill is chunked, and the current iteration only computes + query tokens for prefill, we don't need sampling. + token_chunk_size: The number of tokens to be processed (per sequence). + None if chunking is not required. + lora_request: LoRA request. + computed_block_nums: The block numbers that are already computed, + used in prefix caching. + state: Internal state tied to this sequence group. + multi_modal_data: Multi modal data. + mm_processor_kwargs: Multimodal input processor / mapper overrides. + encoder_seq_data: Optional sequence data for encoder prompt + (SequenceGroup.encoder_seq). Should be None + unless you are working with an encoder/decoder + model. + cross_block_table: Optional cross-attention block table associated + with the encoder prompt + (SequenceGroup.encoder_seq). Should be None + unless you are working with an encoder/decoder + model. + prompt_adapter_request: Prompt Adapter request. + """ + + request_id: str + is_prompt: bool + seq_data: Dict[int, SequenceData] + sampling_params: Optional[SamplingParams] + block_tables: Dict[int, List[int]] + do_sample: bool = True + pooling_params: Optional[PoolingParams] = None + lora_request: Optional[LoRARequest] = None + computed_block_nums: Optional[List[int]] = None + state: Optional[SequenceGroupState] = msgspec.field( + default_factory=lambda: SequenceGroupState()) + # "MultiModalDataDict" types. We have to use Any due to msgspec + # doesn't allow to have union of 2 different dicts. + multi_modal_data: Optional[Any] = None + mm_processor_kwargs: Optional[Dict[str, Any]] = None + encoder_seq_data: Optional[SequenceData] = None + cross_block_table: Optional[List[int]] = None + prompt_adapter_request: Optional[PromptAdapterRequest] = None + token_chunk_size: Optional[int] = None + + ### Stateful fields that are lazily defined. ### + # The number of speculative tokens adopted in this request. + # None means specuative decoding is not used. + # Zero means speculative decoding is disabled for some reasons. + # TODO: We should maintain this states out of the sequence group. + num_speculative_tokens: Optional[int] = None + # BI100 hybrid prefix-cache actions. These are internal scheduler-to-worker + # metadata and never surface through the OpenAI API. + gdn_restore_key: Optional[Tuple[int, bytes]] = None + gdn_capture_points: Optional[List[Tuple[int, Tuple[int, bytes]]]] = None + gdn_evict_keys: Optional[List[Tuple[int, bytes]]] = None + gdn_segment_offsets: Optional[List[int]] = None + + def __post_init__(self): + if self.seq_data is not None and self.token_chunk_size is None: + if self.is_prompt: + self.token_chunk_size = next(iter( + self.seq_data.values())).get_len() + else: + self.token_chunk_size = 1 + + @property + def lora_int_id(self) -> int: + return self.lora_request.lora_int_id if self.lora_request else 0 + + @property + def prompt_adapter_id(self) -> int: + return self.prompt_adapter_request.prompt_adapter_id \ + if self.prompt_adapter_request else 0 + + @property + def prompt_adapter_num_virtual_tokens(self) -> int: + return self.prompt_adapter_request.prompt_adapter_num_virtual_tokens \ + if self.prompt_adapter_request else 0 + + # Multi-Step Chunked-Prefill property + @property + def is_single_step_prompt(self) -> bool: + # do_sample is true, only when the token_chunk_size matches the + # num_uncomputed_tokens of the sequence. This indicates that + # the prompt will finish processing in a single `execute_model` + # step. + return self.is_prompt and self.do_sample + + def get_first_seq_id(self) -> int: + # This is an efficient way of fetching the seq_id when + # we know this SequenceGroup has only one sequence. + return next(iter(self.seq_data)) + + def apply_delta(self, + sequence_group_metadata_delta: SequenceGroupMetadataDelta): + for id, delta in sequence_group_metadata_delta.seq_data_delta.items(): + self.seq_data[id].apply_delta(delta) + assert self.request_id == sequence_group_metadata_delta.request_id + self.block_tables = sequence_group_metadata_delta.block_tables + self.token_chunk_size = sequence_group_metadata_delta.token_chunk_size + self.do_sample = sequence_group_metadata_delta.do_sample + self.is_prompt = sequence_group_metadata_delta.is_prompt + self.computed_block_nums = ( + sequence_group_metadata_delta.computed_block_nums) + self.gdn_restore_key = sequence_group_metadata_delta.gdn_restore_key + self.gdn_capture_points = ( + sequence_group_metadata_delta.gdn_capture_points) + self.gdn_evict_keys = sequence_group_metadata_delta.gdn_evict_keys + self.gdn_segment_offsets = ( + sequence_group_metadata_delta.gdn_segment_offsets) + + def finish_step(self) -> None: + assert self.state is not None + assert self.state.current_step < self.state.num_steps, \ + f"current step {self.state.current_step}, num_steps {self.state.num_steps}" # noqa + self.state.current_step += 1 + + +class SequenceOutput( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + array_like=True): # type: ignore[call-arg] + """The model output associated with a sequence. + + Args: + parent_seq_id: The ID of the parent sequence (for forking in beam + search). + output_token: The output token ID. + logprobs: The logprobs of the output token. + (Token id -> logP(x_i+1 | x_0, ..., x_i)) + """ + parent_seq_id: int + output_token: int + logprobs: Dict[int, Logprob] + + def __repr__(self) -> str: + return (f"SequenceOutput(parent_seq_id={self.parent_seq_id}, " + f"output_token={self.output_token}, " + f"logprobs={self.logprobs})") + + def __eq__(self, other: object) -> bool: + if not isinstance(other, SequenceOutput): + raise NotImplementedError() + equal = (self.parent_seq_id == other.parent_seq_id + and self.output_token == other.output_token) + log_probs_equal = other.logprobs == self.logprobs + return equal and log_probs_equal + + +class SequenceGroupOutput(ABC): + """The base class for model outputs associated with a sequence group.""" + + @abstractmethod + def __repr__(self) -> str: + pass + + @abstractmethod + def __eq__(self, other: object) -> bool: + pass + + +class CompletionSequenceGroupOutput( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + array_like=True): # type: ignore[call-arg] + __metaclass__ = SequenceGroupOutput + """The model output associated with a completion sequence group.""" + samples: List[SequenceOutput] + # Prompt logprob for each prompt query token. + prompt_logprobs: Optional[PromptLogprobs] + + def __repr__(self) -> str: + return (f"CompletionSequenceGroupOutput(samples={self.samples}, " + f"prompt_logprobs={self.prompt_logprobs})") + + def __eq__(self, other: object) -> bool: + if not isinstance(other, CompletionSequenceGroupOutput): + raise NotImplementedError() + return (self.samples == other.samples + and self.prompt_logprobs == other.prompt_logprobs) + + +class EmbeddingSequenceGroupOutput( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + array_like=True, # type: ignore[call-arg] +): + """The model output associated with an embedding sequence group.""" + __metaclass__ = SequenceGroupOutput + embeddings: List[int] + + def __repr__(self) -> str: + return (f"EmbeddingSequenceGroupOutput(" + f"embeddings_shape={len(self.embeddings)})") + + def __eq__(self, other: object) -> bool: + if not isinstance(other, EmbeddingSequenceGroupOutput): + raise NotImplementedError() + return self.embeddings == other.embeddings + + +# cannot use msgspec.Struct here because Dynamo does not support it +@dataclass +class IntermediateTensors: + """For all pipeline stages except the last, we need to return the hidden + states and residuals to be sent to the next stage. This data structure + contains the hidden states and residuals for a request. + """ + + tensors: Dict[str, torch.Tensor] + + def __getitem__(self, key: Union[str, slice]): + if isinstance(key, str): + return self.tensors[key] + elif isinstance(key, slice): + return self.__class__({k: v[key] for k, v in self.tensors.items()}) + + def __setitem__(self, key: str, value): + self.tensors[key] = value + + def __len__(self): + return len(self.tensors) + + def __eq__(self, other: object): + return isinstance(other, self.__class__) and self + + def __repr__(self) -> str: + return f"IntermediateTensors(tensors={self.tensors})" + + +class PoolerOutput( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + array_like=True): # type: ignore[call-arg] + """The output from a pooling operation in the embedding model.""" + outputs: List[EmbeddingSequenceGroupOutput] + + spec_decode_worker_metrics: Optional[SpecDecodeWorkerMetrics] = None + + def __getitem__(self, idx: int): + return self.outputs[idx] + + def __setitem__(self, idx: int, value): + self.outputs[idx] = value + + def __len__(self): + return len(self.outputs) + + def __eq__(self, other: object): + return isinstance(other, + self.__class__) and self.outputs == other.outputs + + +def get_all_seq_ids( + seq_group_metadata_list: List[SequenceGroupMetadata]) -> List[int]: + """Given a list of SequenceGroupMetadata, create a list of all + sequence ids. + """ + return [seq_id for sg in seq_group_metadata_list for seq_id in sg.seq_data] + + +def get_all_seq_ids_and_request_ids( + seq_group_metadata_list: List[SequenceGroupMetadata] +) -> Tuple[List[int], Dict[str, Set[int]]]: + """Given a list of SequenceGroupMetadata, create a list of all + sequence ids. + """ + seq_ids: List[int] = [] + request_id_seq_ids_mapping: Dict[str, Set[int]] = defaultdict(set) + for sg in seq_group_metadata_list: + for seq_id in sg.seq_data: + seq_ids.append(seq_id) + request_id_seq_ids_mapping[sg.request_id].add(seq_id) + return seq_ids, request_id_seq_ids_mapping + + +class HiddenStates(msgspec.Struct, array_like=True, + omit_defaults=True): # type: ignore[call-arg] + """Hidden states corresponding to in-progress sequences. + Used in speculative decoding to pass hidden states from + the target model to the proposer model. + + seq_ids are the sequence ids of each entry of the batch + dimension of the hidden_states tensor""" + # Scorer hidden states. For prefill step, it is used for hidden states of + # all tokens, whereas for decode step, it use used for last accepted tokens. + hidden_states: torch.Tensor + # The sequence group metadata list. Only needed for decode step. + seq_group_metadata_list: Optional[List[SequenceGroupMetadata]] = None + # Scorer hidden states of the 2nd last token proposed by the proposer ( + # irrespective of whether it was accepted or not). Only used for cases when + # last proposed token is accepted (i.e., in case of bonus tokens). For the + # case of no bonus tokens, these are ignored. + second_last_token_hidden_states: Optional[torch.Tensor] = None + + _seq_ids: List[int] = msgspec.field(default_factory=list) + + def __post_init__(self): + if self.seq_group_metadata_list is not None: + assert len(self.seq_group_metadata_list) == len(self.hidden_states) + self._seq_ids = get_all_seq_ids(self.seq_group_metadata_list) + + @property + def seq_ids(self) -> List[int]: + return self._seq_ids + + def update(self, + hidden_states: torch.Tensor, + seq_group_metadata_list: List[SequenceGroupMetadata], + second_last_token_hidden_states: Optional[torch.Tensor] = None): + """Update hidden states from target model invocation. Only used for + decode steps""" + assert len(seq_group_metadata_list) == len(hidden_states) + self._seq_ids.extend(get_all_seq_ids(seq_group_metadata_list)) + self.hidden_states = torch.cat([self.hidden_states, hidden_states]) + + if self.second_last_token_hidden_states is not None: + # Adding dummy hidden_states to this to maintain same shape + self.second_last_token_hidden_states = torch.cat([ + self.second_last_token_hidden_states, + torch.zeros_like(hidden_states) + if second_last_token_hidden_states is None else + second_last_token_hidden_states + ]) + + def prune(self, + seq_group_metadata_list: List[SequenceGroupMetadata]) -> None: + """Prune to provided list of sequence ids. Only used for decode steps. + """ + # Currently this prunes all seq_ids not present in + # seq_group_metadata_list which might cause problems where a sequence + # may be "paused" then "resumed" later. This should only prune sequences + # which are confirmed to be aborted. + seq_ids = get_all_seq_ids(seq_group_metadata_list) + if seq_ids != self._seq_ids: + # Batch contents changed - prune removed sequences. + index = [self._seq_ids.index(seq_id) for seq_id in seq_ids] + self.hidden_states = self.hidden_states[index] + if self.second_last_token_hidden_states is not None: + self.second_last_token_hidden_states = self\ + .second_last_token_hidden_states[index] + self._seq_ids = seq_ids + + def expand_with_bonus_tokens( + self, seq_with_bonus_token_in_last_step: set) -> None: + """Expand hidden states for sequences with bonus tokens. This is in + alignment with `MultiStepWorker._expand_execute_model_request`.""" + if self.second_last_token_hidden_states is None \ + or not seq_with_bonus_token_in_last_step: + return + + index = [] + for seq_id in self._seq_ids: + i = self._seq_ids.index(seq_id) + if seq_id in seq_with_bonus_token_in_last_step: + index.append(i + len(self._seq_ids)) + index.append(i) + + self.hidden_states = torch.cat( + [self.hidden_states, self.second_last_token_hidden_states])[index] + + +class ExecuteModelRequest( + msgspec.Struct, + array_like=True, # type: ignore[call-arg] + omit_defaults=True): # type: ignore[call-arg] + """The model execution request, containing CPU metadata only. The LLM + engine should create an instance of this class for each request batch.""" + # The sequence group metadata list. + seq_group_metadata_list: List[Union[SequenceGroupMetadata, + SequenceGroupMetadataDelta]] + # Blocks to swap in. List of CPU -> GPU block number. + blocks_to_swap_in: List[Tuple[int, + int]] = msgspec.field(default_factory=list) + # Blocks to swap out. List of GPU -> CPU block number. + blocks_to_swap_out: List[Tuple[int, + int]] = msgspec.field(default_factory=list) + # Blocks to copy. Source to dest block. + blocks_to_copy: List[Tuple[int, int]] = msgspec.field(default_factory=list) + # Virtual engine ID for pipeline parallel. + virtual_engine: int = 0 + # The number of slots for lookahead decoding. + num_lookahead_slots: int = 0 + # The number of requests in the running queue. + running_queue_size: int = 0 + # Optional hidden states from prior step. + previous_hidden_states: Optional[HiddenStates] = None + # The number of forward steps to run. + num_steps: int = 1 + # Finished request ids since last step. + finished_requests_ids: List[str] = msgspec.field(default_factory=list) + # The last sampled token ids for multi step decoding. + last_sampled_token_ids: Optional[torch.Tensor] = None + # Async callback + async_callback: Optional[Callable] = None + + @property + def is_first_multi_step(self) -> bool: + # TODO(will) make this be able to handle batches with variable number of + # steps + assert len(self.seq_group_metadata_list) > 0 + first_seq_group = self.seq_group_metadata_list[0] + assert first_seq_group.state is not None + return first_seq_group.state.current_step == 0 + + @property + def is_last_step(self) -> bool: + # TODO(will) make this be able to handle batches with variable number of + # steps + assert len(self.seq_group_metadata_list) > 0 + first_seq_group = self.seq_group_metadata_list[0] + assert first_seq_group.state is not None + return first_seq_group.state.remaining_steps == 1 + + @property + def current_step(self) -> int: + # TODO(will) make this be able to handle batches with variable number of + # steps + assert len(self.seq_group_metadata_list) > 0 + state = self.seq_group_metadata_list[0].state + assert state is not None + return state.current_step + + def clone( + self, seq_group_metadata_list: List[Union[SequenceGroupMetadata, + SequenceGroupMetadataDelta]] + ) -> "ExecuteModelRequest": + """Clone the request with a new sequence group metadata list.""" + return ExecuteModelRequest( + seq_group_metadata_list=seq_group_metadata_list, + blocks_to_swap_in=self.blocks_to_swap_in.copy(), + blocks_to_swap_out=self.blocks_to_swap_out.copy(), + blocks_to_copy=self.blocks_to_copy.copy(), + virtual_engine=self.virtual_engine, + num_lookahead_slots=self.num_lookahead_slots, + running_queue_size=self.running_queue_size, + previous_hidden_states=self.previous_hidden_states, + num_steps=self.num_steps, + finished_requests_ids=self.finished_requests_ids, + last_sampled_token_ids=self.last_sampled_token_ids.clone() + if self.last_sampled_token_ids is not None else None, + async_callback=self.async_callback) diff --git a/qwen3_6_scripts/serving_chat.py b/qwen3_6_scripts/serving_chat.py new file mode 100644 index 0000000..4ed841b --- /dev/null +++ b/qwen3_6_scripts/serving_chat.py @@ -0,0 +1,1358 @@ +import asyncio +import json +import time +from typing import (AsyncGenerator, AsyncIterator, Callable, Dict, Final, List, + Optional) +from typing import Sequence as GenericSequence +from typing import Union + +from fastapi import Request + +from vllm.config import ModelConfig +from vllm.engine.async_llm_engine import AsyncLLMEngine +from vllm.engine.multiprocessing.client import MQLLMEngineClient +from vllm.engine.protocol import EngineClient +from vllm.entrypoints.chat_utils import (ConversationMessage, + apply_hf_chat_template, + apply_mistral_chat_template, + load_chat_template, + parse_chat_messages_futures) +from vllm.entrypoints.logger import RequestLogger +from vllm.entrypoints.openai.protocol import ( + ChatCompletionLogProb, ChatCompletionLogProbs, + ChatCompletionLogProbsContent, ChatCompletionNamedToolChoiceParam, + ChatCompletionRequest, ChatCompletionResponse, + ChatCompletionResponseChoice, ChatCompletionResponseStreamChoice, + ChatCompletionStreamResponse, ChatMessage, DeltaFunctionCall, DeltaMessage, + DeltaToolCall, ErrorResponse, FunctionCall, RequestResponseMetadata, + PromptTokensDetails, ToolCall, UsageInfo) +from vllm.entrypoints.openai.serving_engine import (BaseModelPath, + LoRAModulePath, + OpenAIServing, + PromptAdapterPath, + TextTokensPrompt) +from vllm.entrypoints.openai.tool_parsers import ToolParser, ToolParserManager +from vllm.inputs import TokensPrompt +from vllm.logger import init_logger +from vllm.outputs import CompletionOutput, RequestOutput +from vllm.sampling_params import BeamSearchParams, SamplingParams +from vllm.sequence import Logprob +from vllm.tracing import (contains_trace_headers, extract_trace_headers, + log_tracing_disabled_warning) +from vllm.transformers_utils.tokenizer import AnyTokenizer, MistralTokenizer +from vllm.utils import iterate_with_cancellation, random_uuid + +logger = init_logger(__name__) + + +def _serialize_tool_arguments(arguments) -> str: + if arguments is None: + return "{}" + if isinstance(arguments, str): + return arguments + if isinstance(arguments, (dict, list)): + return json.dumps(arguments, ensure_ascii=False) + return json.dumps(arguments, ensure_ascii=False) + + +def _tool_arguments_are_json_object(arguments: str) -> bool: + try: + value = json.loads(arguments) + except (json.JSONDecodeError, TypeError, ValueError): + return False + return isinstance(value, dict) + + +def _reclassify_named_guided_json( + reasoning_text: Optional[str], + output_text: str, +) -> tuple[Optional[str], str]: + """Recover guided JSON misclassified as unterminated reasoning.""" + if (not output_text and reasoning_text is not None + and _tool_arguments_are_json_object(reasoning_text)): + return None, reasoning_text + return reasoning_text, output_text + + +def _select_named_tool_arguments( + output_text: str, + expected_name: str, + parsed_tool_calls: Optional[List[ToolCall]], +) -> str: + """Use parser output only to repair a malformed named-tool payload.""" + if _tool_arguments_are_json_object(output_text): + return output_text + if not parsed_tool_calls or len(parsed_tool_calls) != 1: + return output_text + call = parsed_tool_calls[0] + function = getattr(call, "function", None) + if function is None or getattr(function, "name", None) != expected_name: + return output_text + arguments = _serialize_tool_arguments( + getattr(function, "arguments", None)) + if not _tool_arguments_are_json_object(arguments): + return output_text + return arguments + + +def _named_tool_delta_payload(name: str, arguments: str, index: int, + call_id: str, first_delta: bool + ) -> Dict[str, object]: + function: Dict[str, object] = {"arguments": arguments} + payload: Dict[str, object] = {"index": index, "function": function} + if first_delta: + function["name"] = name + payload["id"] = call_id + payload["type"] = "function" + return payload + + +def _consume_named_tool_header_slot(header_sent: List[bool], + index: int) -> bool: + first_delta = not header_sent[index] + header_sent[index] = True + return first_delta + + +def _sequential_greedy_fanout_count( + request: ChatCompletionRequest, + max_num_seqs: int, +) -> int: + """Return the supported deterministic fan-out width, or zero.""" + n = request.n if request.n is not None else 1 + if ( + max_num_seqs == 1 + and n == 2 + and request.temperature == 0 + and not request.stream + and not request.use_beam_search + and request.best_of is None + and request.prompt_logprobs is None + ): + return n + return 0 + + +def _merge_sequential_chat_responses( + responses: List[ChatCompletionResponse], + request_id: str, + created_time: int, +) -> ChatCompletionResponse: + if len(responses) != 2: + raise ValueError("deterministic fan-out requires exactly two responses") + + first = responses[0] + if any(response.model != first.model for response in responses): + raise ValueError("fan-out response models differ") + if any(len(response.choices) != 1 for response in responses): + raise ValueError("fan-out child response must contain one choice") + if any( + response.usage.prompt_tokens != first.usage.prompt_tokens + for response in responses + ): + raise ValueError("fan-out prompt token counts differ") + if any( + response.usage.completion_tokens is None for response in responses + ): + raise ValueError("fan-out completion token count is missing") + + choices = [ + response.choices[0].model_copy( + deep=True, + update={"index": index}, + ) + for index, response in enumerate(responses) + ] + completion_tokens = sum( + response.usage.completion_tokens or 0 for response in responses + ) + reasoning_counts = [ + response.usage.reasoning_tokens for response in responses + ] + reasoning_tokens = ( + None + if all(value is None for value in reasoning_counts) + else sum(value or 0 for value in reasoning_counts) + ) + prompt_details = first.usage.prompt_tokens_details + usage = UsageInfo( + prompt_tokens=first.usage.prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=first.usage.prompt_tokens + completion_tokens, + reasoning_tokens=reasoning_tokens, + prompt_tokens_details=( + prompt_details.model_copy(deep=True) + if prompt_details is not None + else None + ), + ) + return ChatCompletionResponse( + id=request_id, + created=created_time, + model=first.model, + choices=choices, + usage=usage, + prompt_logprobs=first.prompt_logprobs, + ) + + +class OpenAIServingChat(OpenAIServing): + + def __init__(self, + engine_client: EngineClient, + model_config: ModelConfig, + base_model_paths: List[BaseModelPath], + response_role: str, + *, + lora_modules: Optional[List[LoRAModulePath]], + prompt_adapters: Optional[List[PromptAdapterPath]], + request_logger: Optional[RequestLogger], + chat_template: Optional[str], + return_tokens_as_token_ids: bool = False, + enable_auto_tools: bool = False, + tool_parser: Optional[str] = None, + reasoning_parser: Optional[str] = None): + super().__init__(engine_client=engine_client, + model_config=model_config, + base_model_paths=base_model_paths, + lora_modules=lora_modules, + prompt_adapters=prompt_adapters, + request_logger=request_logger, + return_tokens_as_token_ids=return_tokens_as_token_ids) + + self.response_role = response_role + self.use_tool_use_model_template = False + self.chat_template = load_chat_template(chat_template) + + # set up tool use + self.enable_auto_tools: bool = enable_auto_tools + if self.enable_auto_tools: + logger.info( + "\"auto\" tool choice has been enabled please note that while" + " the parallel_tool_calls client option is preset for " + "compatibility reasons, it will be ignored.") + + self.tool_parser: Optional[Callable[[AnyTokenizer], ToolParser]] = None + if self.enable_auto_tools: + try: + self.tool_parser = ToolParserManager.get_tool_parser( + tool_parser) + except Exception as e: + raise TypeError("Error: --enable-auto-tool-choice requires " + f"tool_parser:'{tool_parser}' which has not " + "been registered") from e + + # set up reasoning parser + self.reasoning_parser_cls = None + if reasoning_parser: + try: + from vllm.reasoning import ReasoningParserManager + self.reasoning_parser_cls = \ + ReasoningParserManager.get_reasoning_parser(reasoning_parser) + logger.info("Reasoning parser '%s' enabled.", reasoning_parser) + except Exception as e: + raise TypeError( + f"Error: --reasoning-parser '{reasoning_parser}' could not " + "be loaded. Make sure vllm/reasoning/ is installed." + ) from e + + async def create_chat_completion( + self, + request: ChatCompletionRequest, + raw_request: Optional[Request] = None, + ) -> Union[AsyncGenerator[str, None], ChatCompletionResponse, + ErrorResponse]: + """Completion API similar to OpenAI's API. + + See https://platform.openai.com/docs/api-reference/chat/create + for the API specification. This API mimics the OpenAI + ChatCompletion API. + + """ + if not request.messages: + return self.create_error_response( + "messages must contain at least one message") + + error_check_ret = await self._check_model(request) + if error_check_ret is not None: + logger.error("Error with model %s", error_check_ret) + return error_check_ret + + # If the engine is dead, raise the engine's DEAD_ERROR. + # This is required for the streaming case, where we return a + # success status before we actually start generating text :). + if self.engine_client.errored: + raise self.engine_client.dead_error + + # The fixed competition command uses max_num_seqs=1. Native vLLM + # cannot schedule n=2 in that configuration and also rejects greedy + # n>1. Two greedy choices are identical by definition, so execute two + # isolated n=1 requests and merge only this exact deterministic shape. + if request.n is not None and request.n > 1: + scheduler_config = await self.engine_client.get_scheduler_config() + max_num_seqs = scheduler_config.max_num_seqs + if request.n > max_num_seqs: + fanout_count = _sequential_greedy_fanout_count( + request, max_num_seqs) + if fanout_count: + return await self._create_sequential_greedy_fanout( + request, raw_request, fanout_count) + return self.create_error_response( + f"n={request.n} exceeds max_num_seqs={max_num_seqs}. " + f"Use n<={max_num_seqs} or omit n.") + + try: + ( + lora_request, + prompt_adapter_request, + ) = self._maybe_get_adapters(request) + + model_config = self.model_config + tokenizer = await self.engine_client.get_tokenizer(lora_request) + + conversation, mm_data_future = parse_chat_messages_futures( + request.messages, model_config, tokenizer) + + tool_dicts = None if request.tools is None else [ + tool.model_dump() for tool in request.tools + ] + + prompt: Union[str, List[int]] + is_mistral_tokenizer = isinstance(tokenizer, MistralTokenizer) + if is_mistral_tokenizer: + prompt = apply_mistral_chat_template( + tokenizer, + messages=request.messages, + chat_template=request.chat_template or self.chat_template, + add_generation_prompt=request.add_generation_prompt, + continue_final_message=request.continue_final_message, + tools=tool_dicts, + documents=request.documents, + **(request.chat_template_kwargs or {}), + ) + else: + prompt = apply_hf_chat_template( + tokenizer, + conversation=conversation, + chat_template=request.chat_template or self.chat_template, + add_generation_prompt=request.add_generation_prompt, + continue_final_message=request.continue_final_message, + tools=tool_dicts, + documents=request.documents, + **(request.chat_template_kwargs or {}), + ) + except Exception as e: + logger.exception("Error in applying chat template from request") + return self.create_error_response(str(e)) + + try: + mm_data = await mm_data_future + except Exception as e: + logger.exception("Error in loading multi-modal data") + return self.create_error_response(str(e)) + + # validation for OpenAI tools + # tool_choice = "required" is not supported + if request.tool_choice == "required": + return self.create_error_response( + "tool_choice = \"required\" is not supported!") + + if not is_mistral_tokenizer and request.tool_choice == "auto" and not ( + self.enable_auto_tools and self.tool_parser is not None): + # for hf tokenizers, "auto" tools requires + # --enable-auto-tool-choice and --tool-call-parser + return self.create_error_response( + "\"auto\" tool choice requires " + "--enable-auto-tool-choice and --tool-call-parser to be set") + + request_id = f"chat-{random_uuid()}" + + request_metadata = RequestResponseMetadata(request_id=request_id) + if raw_request: + raw_request.state.request_metadata = request_metadata + + try: + if self.enable_auto_tools and self.tool_parser: + request = self.tool_parser(tokenizer).adjust_request( + request=request) + + if isinstance(prompt, str): + prompt_inputs = self._tokenize_prompt_input( + request, + tokenizer, + prompt, + truncate_prompt_tokens=request.truncate_prompt_tokens, + add_special_tokens=request.add_special_tokens, + ) + else: + assert isinstance(prompt, list) and isinstance( + prompt[0], int + ), "Prompt has to be either a string or a list of token ids" + prompt_inputs = TextTokensPrompt( + prompt=tokenizer.decode(prompt), prompt_token_ids=prompt) + + assert prompt_inputs is not None + + sampling_params: Union[SamplingParams, BeamSearchParams] + default_max_tokens = self.max_model_len - len( + prompt_inputs["prompt_token_ids"]) + if request.use_beam_search: + sampling_params = request.to_beam_search_params( + default_max_tokens) + else: + sampling_params = request.to_sampling_params( + default_max_tokens) + + self._log_inputs(request_id, + prompt_inputs, + params=sampling_params, + lora_request=lora_request, + prompt_adapter_request=prompt_adapter_request) + + engine_inputs = TokensPrompt( + prompt_token_ids=prompt_inputs["prompt_token_ids"]) + if mm_data is not None: + engine_inputs["multi_modal_data"] = mm_data + + is_tracing_enabled = (await + self.engine_client.is_tracing_enabled()) + trace_headers = None + if is_tracing_enabled and raw_request: + trace_headers = extract_trace_headers(raw_request.headers) + if (not is_tracing_enabled and raw_request + and contains_trace_headers(raw_request.headers)): + log_tracing_disabled_warning() + + if isinstance(sampling_params, BeamSearchParams): + assert isinstance(self.engine_client, + (AsyncLLMEngine, + MQLLMEngineClient)), \ + "Beam search is only supported with" \ + "AsyncLLMEngine and MQLLMEngineClient." + result_generator = self.engine_client.beam_search( + engine_inputs['prompt_token_ids'], + request_id, + sampling_params, + ) + else: + result_generator = self.engine_client.generate( + engine_inputs, + sampling_params, + request_id, + lora_request=lora_request, + trace_headers=trace_headers, + prompt_adapter_request=prompt_adapter_request, + priority=request.priority, + ) + except ValueError as e: + # TODO: Use a vllm-specific Validation Error + return self.create_error_response(str(e)) + + if raw_request: + result_generator = iterate_with_cancellation( + result_generator, raw_request.is_disconnected) + + # Streaming response + if request.stream: + return self.chat_completion_stream_generator( + request, result_generator, request_id, conversation, tokenizer, + request_metadata, raw_request=raw_request) + + try: + return await self.chat_completion_full_generator( + request, result_generator, request_id, conversation, tokenizer, + request_metadata, raw_request=raw_request) + except ValueError as e: + # TODO: Use a vllm-specific Validation Error + return self.create_error_response(str(e)) + + async def _create_sequential_greedy_fanout( + self, + request: ChatCompletionRequest, + raw_request: Optional[Request], + fanout_count: int, + ) -> Union[ChatCompletionResponse, ErrorResponse]: + request_id = f"chat-{random_uuid()}" + created_time = int(time.time()) + responses: List[ChatCompletionResponse] = [] + + for _ in range(fanout_count): + child_request = request.model_copy( + deep=True, + update={"n": 1}, + ) + child_response = await self.create_chat_completion( + child_request, raw_request) + if isinstance(child_response, ErrorResponse): + return child_response + if not isinstance(child_response, ChatCompletionResponse): + logger.error( + "Sequential greedy fan-out unexpectedly returned a stream") + return self.create_error_response( + "Failed to aggregate deterministic n=2 completion") + responses.append(child_response) + + try: + response = _merge_sequential_chat_responses( + responses, + request_id, + created_time, + ) + except ValueError as error: + logger.error( + "Sequential greedy fan-out aggregation failed: %s", + type(error).__name__, + ) + return self.create_error_response( + "Failed to aggregate deterministic n=2 completion") + + if raw_request is not None: + metadata = RequestResponseMetadata( + request_id=request_id, + final_usage_info=response.usage, + ) + raw_request.state.request_metadata = metadata + logger.info( + "[BI100 N_FANOUT] choices=%d mode=sequential_greedy", + fanout_count, + ) + return response + + def get_chat_request_role(self, request: ChatCompletionRequest) -> str: + if request.add_generation_prompt: + return self.response_role + return request.messages[-1]["role"] + + async def chat_completion_stream_generator( + self, + request: ChatCompletionRequest, + result_generator: AsyncIterator[RequestOutput], + request_id: str, + conversation: List[ConversationMessage], + tokenizer: AnyTokenizer, + request_metadata: RequestResponseMetadata, + raw_request: Optional[Request] = None, + ) -> AsyncGenerator[str, None]: + model_name = self.base_model_paths[0].name + created_time = int(time.time()) + chunk_object_type: Final = "chat.completion.chunk" + first_iteration = True + + # Send response for each token for each request.n (index) + num_choices = 1 if request.n is None else request.n + previous_num_tokens = [0] * num_choices + finish_reason_sent = [False] * num_choices + num_prompt_tokens = 0 + num_cached_tokens: Optional[int] = None + + if isinstance(request.tool_choice, ChatCompletionNamedToolChoiceParam): + tool_choice_function_name = request.tool_choice.function.name + else: + tool_choice_function_name = None + named_tool_call_ids = ( + [f"chatcmpl-tool-{random_uuid()}" for _ in range(num_choices)] + if tool_choice_function_name else []) + named_tool_header_sent = [False] * num_choices + + # Determine whether tools are in use with "auto" tool choice + tool_choice_auto = ( + not tool_choice_function_name + and self._should_stream_with_auto_tool_parsing(request)) + + use_reasoning = self.reasoning_parser_cls is not None + + all_previous_token_ids: Optional[List[List[int]]] + # previous_texts / all_previous_token_ids are needed for both tool + # parsing and reasoning parsing (both require full-history context). + if tool_choice_auto or use_reasoning: + previous_texts = [""] * num_choices + all_previous_token_ids = [[]] * num_choices + else: + previous_texts, all_previous_token_ids = None, None + + # Prepare the tool parser if it's needed + try: + if tool_choice_auto and self.tool_parser: + tool_parsers: List[Optional[ToolParser]] = [ + self.tool_parser(tokenizer) + ] * num_choices + else: + tool_parsers = [None] * num_choices + except RuntimeError as e: + logger.error("Error in tool parser creation: %s", e) + data = self.create_streaming_error_response(str(e)) + yield f"data: {data}\n\n" + yield "data: [DONE]\n\n" + return + + # Prepare reasoning parsers (one instance per choice for state isolation) + reasoning_parsers: List[Optional[object]] = [None] * num_choices + reasoning_end_arr: List[bool] = [False] * num_choices + reasoning_token_counts: List[int] = [0] * num_choices + if use_reasoning: + try: + reasoning_parsers = [ + self.reasoning_parser_cls( + tokenizer, + chat_template_kwargs=request.chat_template_kwargs) + for _ in range(num_choices) + ] + # If thinking is disabled per-request, mark reasoning as + # already ended so the tool-auto branch is reachable. + for idx, rp in enumerate(reasoning_parsers): + if hasattr(rp, 'thinking_enabled') and not rp.thinking_enabled: + reasoning_end_arr[idx] = True + except RuntimeError as e: + logger.error("Error in reasoning parser creation: %s", e) + data = self.create_streaming_error_response(str(e)) + yield f"data: {data}\n\n" + yield "data: [DONE]\n\n" + return + + # Background task: poll is_disconnected() every 300 ms and abort the + # engine request as soon as the client goes away. This catches the + # case where the HTTP layer (Starlette/uvicorn) does not actively read + # the receive channel during streaming, so is_disconnected() in + # iterate_with_cancellation never fires during fast decode. + _disconnect_watcher: Optional[asyncio.Task] = None + if raw_request is not None: + async def _watch_disconnect() -> None: + try: + while True: + if await raw_request.is_disconnected(): + logger.info( + "Client disconnected (decode watcher), " + "aborting request %s", request_id) + await self.engine_client.abort(request_id) + return + await asyncio.sleep(0.3) + except asyncio.CancelledError: + pass + _disconnect_watcher = asyncio.ensure_future(_watch_disconnect()) + + try: + async for res in result_generator: + if res.prompt_token_ids is not None: + num_prompt_tokens = len(res.prompt_token_ids) + if res.encoder_prompt_token_ids is not None: + num_prompt_tokens += len(res.encoder_prompt_token_ids) + if (num_cached_tokens is None + and res.metrics is not None + and res.metrics.num_cached_tokens is not None): + num_cached_tokens = res.metrics.num_cached_tokens + + # We need to do it here, because if there are exceptions in + # the result_generator, it needs to be sent as the FIRST + # response (by the try...catch). + if first_iteration: + # Send first response for each request.n (index) with + # the role + role = self.get_chat_request_role(request) + + # NOTE num_choices defaults to 1 so this usually executes + # once per request + for i in range(num_choices): + tool_parser = tool_parsers[i] + choice_data = ChatCompletionResponseStreamChoice( + index=i, + delta=DeltaMessage( + role=role, + content="", + ), + logprobs=None, + finish_reason=None) + chunk = ChatCompletionStreamResponse( + id=request_id, + object=chunk_object_type, + created=created_time, + choices=[choice_data], + model=model_name) + + # if usage should be included + if (request.stream_options + and request.stream_options.include_usage): + # if continuous usage stats are requested, add it + if request.stream_options.continuous_usage_stats: + usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=0, + total_tokens=num_prompt_tokens) + chunk.usage = usage + # otherwise don't + else: + chunk.usage = None + + data = chunk.model_dump_json(exclude_unset=True) + yield f"data: {data}\n\n" + + # Send response to echo the input portion of the + # last message + if request.echo or request.continue_final_message: + last_msg_content: str = "" + if conversation and "content" in conversation[ + -1] and conversation[-1].get("role") == role: + last_msg_content = conversation[-1]["content"] or "" + + if last_msg_content: + for i in range(num_choices): + choice_data = ( + ChatCompletionResponseStreamChoice( + index=i, + delta=DeltaMessage( + content=last_msg_content), + logprobs=None, + finish_reason=None)) + chunk = ChatCompletionStreamResponse( + id=request_id, + object=chunk_object_type, + created=created_time, + choices=[choice_data], + model=model_name) + if (request.stream_options and + request.stream_options.include_usage): + if (request.stream_options. + continuous_usage_stats): + usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=0, + total_tokens=num_prompt_tokens) + chunk.usage = usage + else: + chunk.usage = None + + data = chunk.model_dump_json( + exclude_unset=True) + yield f"data: {data}\n\n" + first_iteration = False + + for output in res.outputs: + i = output.index + tool_parser = tool_parsers[i] + + if finish_reason_sent[i]: + continue + + if request.logprobs and request.top_logprobs is not None: + assert output.logprobs is not None, ( + "Did not output logprobs") + logprobs = self._create_chat_logprobs( + token_ids=output.token_ids, + top_logprobs=output.logprobs, + tokenizer=tokenizer, + num_output_top_logprobs=request.top_logprobs, + ) + else: + logprobs = None + + delta_text = output.text + delta_message: Optional[DeltaMessage] + + # Maintain text/token history when either reasoning or + # auto-tool parsing is active. + assert previous_texts is not None or not ( + tool_choice_auto or use_reasoning) + if previous_texts is not None: + assert all_previous_token_ids is not None + previous_text = previous_texts[i] + previous_token_ids = all_previous_token_ids[i] + current_text = previous_text + delta_text + current_token_ids = previous_token_ids + list( + output.token_ids) + previous_texts[i] = current_text + all_previous_token_ids[i] = current_token_ids + else: + previous_text = "" + previous_token_ids = [] + current_text = delta_text + current_token_ids = list(output.token_ids) + + # handle streaming deltas for tools with named tool_choice + if tool_choice_function_name: + first_named_delta = _consume_named_tool_header_slot( + named_tool_header_sent, i) + delta_message = DeltaMessage(tool_calls=[ + DeltaToolCall(**_named_tool_delta_payload( + tool_choice_function_name, + delta_text, + i, + named_tool_call_ids[i], + first_named_delta, + )) + ]) + + # handle reasoning: route through reasoning parser while + # has not yet been seen. + elif use_reasoning and not reasoning_end_arr[i]: + r_parser = reasoning_parsers[i] + delta_message = r_parser.extract_reasoning_streaming( + previous_text=previous_text, + current_text=current_text, + delta_text=delta_text, + previous_token_ids=previous_token_ids, + current_token_ids=current_token_ids, + delta_token_ids=output.token_ids, + ) + # Mark reasoning as ended when end token appears. + if r_parser.end_token_id in current_token_ids: + reasoning_end_arr[i] = True + + # handle streaming deltas for tools with "auto" tool choice + # (only reached after reasoning block, if any, has ended) + elif tool_choice_auto: + assert tool_parser is not None + delta_message = ( + tool_parser.extract_tool_calls_streaming( + previous_text=previous_text, + current_text=current_text, + delta_text=delta_text, + previous_token_ids=previous_token_ids, + current_token_ids=current_token_ids, + delta_token_ids=output.token_ids, + request=request)) + + # handle streaming just a content delta + else: + delta_message = DeltaMessage(content=delta_text) + + # set the previous values for the next iteration + previous_num_tokens[i] += len(output.token_ids) + + # if the message delta is None (e.g. because it was a + # "control token" for tool calls or the parser otherwise + # wasn't ready to send a token, then + # get the next token without streaming a chunk. + # However, if this is the finish token we must NOT skip — + # the finish block updates reasoning_token_counts, sets + # finish_reason_sent, and flushes the final usage chunk. + if delta_message is None: + if output.finish_reason is None: + continue + delta_message = DeltaMessage() + + if output.finish_reason is None: + # Send token-by-token response for each request.n + + choice_data = ChatCompletionResponseStreamChoice( + index=i, + delta=delta_message, + logprobs=logprobs, + finish_reason=None) + chunk = ChatCompletionStreamResponse( + id=request_id, + object=chunk_object_type, + created=created_time, + choices=[choice_data], + model=model_name) + + # handle usage stats if requested & if continuous + if (request.stream_options + and request.stream_options.include_usage): + if request.stream_options.continuous_usage_stats: + completion_tokens = len(output.token_ids) + usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=num_prompt_tokens + + completion_tokens, + ) + chunk.usage = usage + else: + chunk.usage = None + + data = chunk.model_dump_json(exclude_unset=True) + yield f"data: {data}\n\n" + + # if the model is finished generating + else: + # check to make sure we haven't "forgotten" to stream + # any tokens that were generated but previously + # matched by partial json parsing + # only happens if we are NOT using guided decoding + auto_tools_called = False + if tool_parser: + auto_tools_called = len( + tool_parser.prev_tool_call_arr) > 0 + index = len(tool_parser.prev_tool_call_arr + ) - 1 if auto_tools_called else 0 + else: + index = 0 + + if self._should_check_for_unstreamed_tool_arg_tokens( + delta_message, output) and tool_parser: + # get the expected call based on partial JSON + # parsing which "autocompletes" the JSON + expected_call = _serialize_tool_arguments( + tool_parser.prev_tool_call_arr[index].get( + "arguments", {})) + + # get what we've streamed so far for arguments + # for the current tool + actual_call = tool_parser.streamed_args_for_tool[ + index] + + # check to see if there's anything left to stream + remaining_call = expected_call.replace( + actual_call, "", 1) + + # set that as a delta message + delta_message = DeltaMessage(tool_calls=[ + DeltaToolCall(index=index, + function=DeltaFunctionCall( + arguments=remaining_call). + model_dump(exclude_none=True)) + ]) + + # Count reasoning tokens for this choice at finish time. + if use_reasoning and all_previous_token_ids is not None: + r_parser = reasoning_parsers[i] + reasoning_token_counts[i] = \ + r_parser.count_reasoning_tokens( + all_previous_token_ids[i]) + + # Send the finish response for each request.n only once + choice_data = ChatCompletionResponseStreamChoice( + index=i, + delta=delta_message, + logprobs=logprobs, + finish_reason=("tool_calls" if ( + auto_tools_called or tool_choice_function_name) + else output.finish_reason), + stop_reason=output.stop_reason) + chunk = ChatCompletionStreamResponse( + id=request_id, + object=chunk_object_type, + created=created_time, + choices=[choice_data], + model=model_name) + if (request.stream_options + and request.stream_options.include_usage): + if request.stream_options.continuous_usage_stats: + completion_tokens = len(output.token_ids) + usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=num_prompt_tokens + + completion_tokens, + ) + chunk.usage = usage + else: + chunk.usage = None + data = chunk.model_dump_json(exclude_unset=True) + yield f"data: {data}\n\n" + finish_reason_sent[i] = True + + # once the final token is handled, if stream_options.include_usage + # is sent, send the usage + if (request.stream_options + and request.stream_options.include_usage): + completion_tokens = sum(previous_num_tokens) + total_reasoning = sum(reasoning_token_counts) if use_reasoning else None + final_usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=completion_tokens, + total_tokens=num_prompt_tokens + completion_tokens, + reasoning_tokens=total_reasoning, + prompt_tokens_details=( + PromptTokensDetails(cached_tokens=num_cached_tokens) + if num_cached_tokens is not None else None), + ) + + final_usage_chunk = ChatCompletionStreamResponse( + id=request_id, + object=chunk_object_type, + created=created_time, + choices=[], + model=model_name, + usage=final_usage) + final_usage_data = (final_usage_chunk.model_dump_json( + exclude_unset=True, exclude_none=True)) + yield f"data: {final_usage_data}\n\n" + + # report to FastAPI middleware aggregate usage across all choices + num_completion_tokens = sum(previous_num_tokens) + total_reasoning = sum(reasoning_token_counts) if use_reasoning else None + request_metadata.final_usage_info = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=num_completion_tokens, + total_tokens=num_prompt_tokens + num_completion_tokens, + reasoning_tokens=total_reasoning) + + except asyncio.CancelledError: + # Client disconnected via CancelledError path; abort engine request. + await self.engine_client.abort(request_id) + return + except ValueError as e: + # TODO: Use a vllm-specific Validation Error + logger.error("error in chat completion stream generator: %s", e) + data = self.create_streaming_error_response(str(e)) + yield f"data: {data}\n\n" + finally: + # Stop the disconnect watcher (it may already be done if it fired). + if _disconnect_watcher is not None and not _disconnect_watcher.done(): + _disconnect_watcher.cancel() + try: + await _disconnect_watcher + except asyncio.CancelledError: + pass + # Covers GeneratorExit when Starlette calls aclose() on disconnect + # during decode (tokens arrive fast so CancelledError path is not + # always triggered). abort() is a no-op for already-finished requests. + await self.engine_client.abort(request_id) + # Send the final done message after all response.n are finished + yield "data: [DONE]\n\n" + + async def chat_completion_full_generator( + self, + request: ChatCompletionRequest, + result_generator: AsyncIterator[RequestOutput], + request_id: str, + conversation: List[ConversationMessage], + tokenizer: AnyTokenizer, + request_metadata: RequestResponseMetadata, + raw_request: Optional[Request] = None, + ) -> Union[ErrorResponse, ChatCompletionResponse]: + + model_name = self.base_model_paths[0].name + created_time = int(time.time()) + final_res: Optional[RequestOutput] = None + + # Background watcher: same logic as the streaming path — polls + # is_disconnected() every 300 ms so that a client disconnect during + # non-streaming decode is caught even when uvicorn isn't actively + # reading the receive channel. + _disconnect_watcher: Optional[asyncio.Task] = None + if raw_request is not None: + async def _watch_disconnect() -> None: + try: + while True: + if await raw_request.is_disconnected(): + logger.info( + "Client disconnected (non-stream watcher), " + "aborting request %s", request_id) + await self.engine_client.abort(request_id) + return + await asyncio.sleep(0.3) + except asyncio.CancelledError: + pass + _disconnect_watcher = asyncio.ensure_future(_watch_disconnect()) + + try: + async for res in result_generator: + final_res = res + except asyncio.CancelledError: + await self.engine_client.abort(request_id) + return self.create_error_response("Client disconnected") + finally: + if _disconnect_watcher is not None and not _disconnect_watcher.done(): + _disconnect_watcher.cancel() + try: + await _disconnect_watcher + except asyncio.CancelledError: + pass + await self.engine_client.abort(request_id) + + assert final_res is not None + + choices: List[ChatCompletionResponseChoice] = [] + + role = self.get_chat_request_role(request) + for output in final_res.outputs: + token_ids = output.token_ids + out_logprobs = output.logprobs + + if request.logprobs and request.top_logprobs is not None: + assert out_logprobs is not None, "Did not output logprobs" + logprobs = self._create_chat_logprobs( + token_ids=token_ids, + top_logprobs=out_logprobs, + num_output_top_logprobs=request.top_logprobs, + tokenizer=tokenizer, + ) + else: + logprobs = None + + # In the OpenAI API the finish_reason is "tools_called" + # if the tool choice is auto and the model produced a tool + # call. The same is not true for named function calls + auto_tools_called = False + + # Extract reasoning content if parser is configured. + # output_text is what remains after stripping .... + reasoning_text: Optional[str] = None + output_text: str = output.text + if self.reasoning_parser_cls: + r_parser = self.reasoning_parser_cls( + tokenizer, + chat_template_kwargs=request.chat_template_kwargs) + reasoning_text, extracted = r_parser.extract_reasoning( + output.text, request) + output_text = extracted or "" + if isinstance(request.tool_choice, + ChatCompletionNamedToolChoiceParam): + reasoning_text, output_text = \ + _reclassify_named_guided_json( + reasoning_text, output_text) + + named_tool_called = False + + # if auto tools are not enabled, and a named tool choice using + # outlines is not being used + if (not self.enable_auto_tools + or not self.tool_parser) and not isinstance( + request.tool_choice, + ChatCompletionNamedToolChoiceParam): + message = ChatMessage(role=role, + reasoning_content=reasoning_text, + content=output_text) + + # if the request uses tools and specified a tool choice + elif request.tool_choice and type( + request.tool_choice) is ChatCompletionNamedToolChoiceParam: + + named_tool_called = True + parsed_named_tool_calls: Optional[List[ToolCall]] = None + if (not _tool_arguments_are_json_object(output_text) + and self.tool_parser is not None): + try: + named_tool_info = self.tool_parser( + tokenizer).extract_tool_calls( + output_text, request=request) + if named_tool_info.tools_called: + parsed_named_tool_calls = named_tool_info.tool_calls + except RuntimeError as e: + logger.warning( + "Named tool parser unavailable; preserving raw " + "arguments: %s", type(e).__name__) + named_arguments = _select_named_tool_arguments( + output_text, + request.tool_choice.function.name, + parsed_named_tool_calls, + ) + message = ChatMessage( + role=role, + reasoning_content=reasoning_text, + content="", + tool_calls=[ + ToolCall(function=FunctionCall( + name=request.tool_choice.function.name, + arguments=named_arguments)) + ]) + + # if the request doesn't use tool choice + # OR specifies to not use a tool + elif not request.tool_choice or request.tool_choice == "none": + + message = ChatMessage(role=role, + reasoning_content=reasoning_text, + content=output_text) + + # handle when there are tools and tool choice is auto + elif request.tools and ( + request.tool_choice == "auto" + or request.tool_choice is None) and self.enable_auto_tools \ + and self.tool_parser: + + try: + tool_parser = self.tool_parser(tokenizer) + except RuntimeError as e: + logger.error("Error in tool parser creation: %s", e) + return self.create_error_response(str(e)) + + # Parse tool calls from the post-reasoning content. + tool_call_info = tool_parser.extract_tool_calls( + output_text, request=request) + auto_tools_called = tool_call_info.tools_called + if tool_call_info.tools_called: + message = ChatMessage( + role=role, + reasoning_content=reasoning_text, + content=tool_call_info.content, + tool_calls=tool_call_info.tool_calls) + else: + message = ChatMessage(role=role, + reasoning_content=reasoning_text, + content=output_text) + + # undetermined case that is still important to handle + else: + logger.error( + "Error in chat_completion_full_generator - cannot determine" + " if tools should be extracted. Returning a standard chat " + "completion.") + message = ChatMessage(role=role, + reasoning_content=reasoning_text, + content=output_text) + + choice_data = ChatCompletionResponseChoice( + index=output.index, + message=message, + logprobs=logprobs, + finish_reason="tool_calls" if ( + auto_tools_called or named_tool_called) else + output.finish_reason if output.finish_reason else "stop", + stop_reason=output.stop_reason) + choices.append(choice_data) + + if request.echo or request.continue_final_message: + last_msg_content = "" + if conversation and "content" in conversation[-1] and conversation[ + -1].get("role") == role: + last_msg_content = conversation[-1]["content"] or "" + + for choice in choices: + full_message = last_msg_content + (choice.message.content + or "") + choice.message.content = full_message + + assert final_res.prompt_token_ids is not None + num_prompt_tokens = len(final_res.prompt_token_ids) + if final_res.encoder_prompt_token_ids is not None: + num_prompt_tokens += len(final_res.encoder_prompt_token_ids) + num_generated_tokens = sum( + len(output.token_ids) for output in final_res.outputs) + total_reasoning_tokens: Optional[int] = None + if self.reasoning_parser_cls: + rp = self.reasoning_parser_cls( + tokenizer, + chat_template_kwargs=request.chat_template_kwargs) + total_reasoning_tokens = sum( + rp.count_reasoning_tokens(list(output.token_ids)) + for output in final_res.outputs) + num_cached_tokens = (final_res.metrics.num_cached_tokens + if final_res.metrics is not None else None) + usage = UsageInfo( + prompt_tokens=num_prompt_tokens, + completion_tokens=num_generated_tokens, + total_tokens=num_prompt_tokens + num_generated_tokens, + reasoning_tokens=total_reasoning_tokens, + prompt_tokens_details=( + PromptTokensDetails(cached_tokens=num_cached_tokens) + if num_cached_tokens is not None else None), + ) + + request_metadata.final_usage_info = usage + + prompt_logprobs = final_res.prompt_logprobs + sample_positions = request.bi100_prompt_logprobs_sample_positions + if sample_positions is not None: + if num_cached_tokens not in (None, 0): + return self.create_error_response( + "BI100 sampled prompt logprobs require a cold request.") + if prompt_logprobs is None or ( + sample_positions + and sample_positions[-1] >= len(prompt_logprobs)): + return self.create_error_response( + "BI100 prompt-logprob sample positions exceed the prompt.") + selected = set(sample_positions) + prompt_logprobs = [ + row if position in selected else None + for position, row in enumerate(prompt_logprobs) + ] + + response = ChatCompletionResponse( + id=request_id, + created=created_time, + model=model_name, + choices=choices, + usage=usage, + prompt_logprobs=prompt_logprobs, + ) + + return response + + def _get_top_logprobs( + self, logprobs: Dict[int, Logprob], top_logprobs: Optional[int], + tokenizer: AnyTokenizer) -> List[ChatCompletionLogProb]: + return [ + ChatCompletionLogProb(token=(token := self._get_decoded_token( + p[1], + p[0], + tokenizer, + return_as_token_id=self.return_tokens_as_token_ids)), + logprob=max(p[1].logprob, -9999.0), + bytes=list( + token.encode("utf-8", errors="replace"))) + for i, p in enumerate(logprobs.items()) + if top_logprobs and i < top_logprobs + ] + + def _create_chat_logprobs( + self, + token_ids: GenericSequence[int], + top_logprobs: GenericSequence[Optional[Dict[int, Logprob]]], + tokenizer: AnyTokenizer, + num_output_top_logprobs: Optional[int] = None, + ) -> ChatCompletionLogProbs: + """Create OpenAI-style logprobs.""" + logprobs_content: List[ChatCompletionLogProbsContent] = [] + + for i, token_id in enumerate(token_ids): + step_top_logprobs = top_logprobs[i] + if step_top_logprobs is None: + token = tokenizer.decode(token_id) + if self.return_tokens_as_token_ids: + token = f"token_id:{token_id}" + + logprobs_content.append( + ChatCompletionLogProbsContent( + token=token, + bytes=list(token.encode("utf-8", errors="replace")), + )) + else: + step_token = step_top_logprobs[token_id] + step_decoded = step_token.decoded_token + + logprobs_content.append( + ChatCompletionLogProbsContent( + token=self._get_decoded_token( + step_token, + token_id, + tokenizer, + self.return_tokens_as_token_ids, + ), + logprob=max(step_token.logprob, -9999.0), + bytes=None if step_decoded is None else list( + step_decoded.encode("utf-8", errors="replace")), + top_logprobs=self._get_top_logprobs( + step_top_logprobs, + num_output_top_logprobs, + tokenizer, + ), + )) + + return ChatCompletionLogProbs(content=logprobs_content) + + def _should_stream_with_auto_tool_parsing(self, + request: ChatCompletionRequest): + """ + Utility function to check if streamed tokens should go through the tool + call parser that was configured. + + We only want to do this IF user-provided tools are set, a tool parser + is configured, "auto" tool choice is enabled, and the request's tool + choice field indicates that "auto" tool choice should be used. + """ + return (request.tools and self.tool_parser and self.enable_auto_tools + and request.tool_choice in ['auto', None]) + + def _should_check_for_unstreamed_tool_arg_tokens( + self, + delta_message: Optional[DeltaMessage], + output: CompletionOutput, + ) -> bool: + """ + Check to see if we should check for unstreamed tool arguments tokens. + This is only applicable when auto tool parsing is enabled, the delta + is a tool call with arguments. + """ + + # yapf: disable + return bool( + # if there is a delta message that includes tool calls which + # include a function that has arguments + output.finish_reason is not None + and self.enable_auto_tools and self.tool_parser and delta_message + and delta_message.tool_calls and delta_message.tool_calls[0] + and delta_message.tool_calls[0].function + and delta_message.tool_calls[0].function.arguments is not None + ) diff --git a/qwen3_6_scripts/serving_tokenization.py b/qwen3_6_scripts/serving_tokenization.py new file mode 100644 index 0000000..b078293 --- /dev/null +++ b/qwen3_6_scripts/serving_tokenization.py @@ -0,0 +1,159 @@ +from typing import List, Optional, Union + +from vllm.config import ModelConfig +from vllm.engine.protocol import EngineClient +from vllm.entrypoints.chat_utils import (apply_hf_chat_template, + apply_mistral_chat_template, + load_chat_template, + parse_chat_messages_futures) +from vllm.entrypoints.logger import RequestLogger +# yapf conflicts with isort for this block +# yapf: disable +from vllm.entrypoints.openai.protocol import (DetokenizeRequest, + DetokenizeResponse, + ErrorResponse, + TokenizeChatRequest, + TokenizeRequest, + TokenizeResponse) +# yapf: enable +from vllm.entrypoints.openai.serving_engine import (BaseModelPath, + LoRAModulePath, + OpenAIServing) +from vllm.logger import init_logger +from vllm.transformers_utils.tokenizer import MistralTokenizer +from vllm.utils import random_uuid + +logger = init_logger(__name__) + + +class OpenAIServingTokenization(OpenAIServing): + + def __init__( + self, + engine_client: EngineClient, + model_config: ModelConfig, + base_model_paths: List[BaseModelPath], + *, + lora_modules: Optional[List[LoRAModulePath]], + request_logger: Optional[RequestLogger], + chat_template: Optional[str], + ): + super().__init__(engine_client=engine_client, + model_config=model_config, + base_model_paths=base_model_paths, + lora_modules=lora_modules, + prompt_adapters=None, + request_logger=request_logger) + + # If this is None we use the tokenizer's default chat template + # the list of commonly-used chat template names for HF named templates + hf_chat_templates: List[str] = ['default', 'tool_use'] + self.chat_template = chat_template \ + if chat_template in hf_chat_templates \ + else load_chat_template(chat_template) + + async def create_tokenize( + self, + request: TokenizeRequest, + ) -> Union[TokenizeResponse, ErrorResponse]: + error_check_ret = await self._check_model(request) + if error_check_ret is not None: + return error_check_ret + + request_id = f"tokn-{random_uuid()}" + + ( + lora_request, + prompt_adapter_request, + ) = self._maybe_get_adapters(request) + + tokenizer = await self.engine_client.get_tokenizer(lora_request) + + prompt: Union[str, List[int]] + if isinstance(request, TokenizeChatRequest): + model_config = self.model_config + + conversation, mm_data_future = parse_chat_messages_futures( + request.messages, model_config, tokenizer) + + mm_data = await mm_data_future + if mm_data: + logger.warning( + "Multi-modal inputs are ignored during tokenization") + + if isinstance(tokenizer, MistralTokenizer): + prompt = apply_mistral_chat_template( + tokenizer, + messages=request.messages, + chat_template=self.chat_template, + add_generation_prompt=request.add_generation_prompt, + continue_final_message=request.continue_final_message, + **(request.chat_template_kwargs or {}), + ) + else: + prompt = apply_hf_chat_template( + tokenizer, + conversation=conversation, + chat_template=self.chat_template, + add_generation_prompt=request.add_generation_prompt, + continue_final_message=request.continue_final_message, + **(request.chat_template_kwargs or {}), + ) + else: + prompt = request.prompt + + self._log_inputs(request_id, + prompt, + params=None, + lora_request=lora_request, + prompt_adapter_request=prompt_adapter_request) + + # Silently ignore prompt adapter since it does not affect tokenization + + prompt_input = self._tokenize_prompt_input( + request, + tokenizer, + prompt, + add_special_tokens=request.add_special_tokens, + ) + input_ids = prompt_input["prompt_token_ids"] + + return TokenizeResponse(tokens=input_ids, + count=len(input_ids), + max_model_len=self.max_model_len) + + async def create_detokenize( + self, + request: DetokenizeRequest, + ) -> Union[DetokenizeResponse, ErrorResponse]: + error_check_ret = await self._check_model(request) + if error_check_ret is not None: + return error_check_ret + + request_id = f"tokn-{random_uuid()}" + + ( + lora_request, + prompt_adapter_request, + ) = self._maybe_get_adapters(request) + + tokenizer = await self.engine_client.get_tokenizer(lora_request) + + self._log_inputs(request_id, + request.tokens, + params=None, + lora_request=lora_request, + prompt_adapter_request=prompt_adapter_request) + + if prompt_adapter_request is not None: + raise NotImplementedError("Prompt adapter is not supported " + "for tokenization") + + prompt_input = self._tokenize_prompt_input( + request, + tokenizer, + request.tokens, + ) + input_text = prompt_input["prompt"] + + return DetokenizeResponse(prompt=input_text) diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/block/block_table.py b/qwen3_6_scripts/vendor_overrides/vllm/core/block/block_table.py new file mode 100644 index 0000000..40bfd23 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/block/block_table.py @@ -0,0 +1,456 @@ +import math +from typing import List, Optional + +from vllm.core.block.common import BlockList +from vllm.core.block.interfaces import Block, DeviceAwareBlockAllocator +from vllm.utils import Device, cdiv, chunk_list + + +class BlockTable: + """A class to manage blocks for a specific sequence. + + The BlockTable maps a sequence of tokens to a list of blocks, where each + block represents a contiguous memory allocation for a portion of the + sequence. The blocks are managed by a DeviceAwareBlockAllocator, which is + responsible for allocating and freeing memory for the blocks. + + Args: + block_size (int): The maximum number of tokens that can be stored in a + single block. + block_allocator (DeviceAwareBlockAllocator): The block allocator used to + manage memory for the blocks. + _blocks (Optional[List[Block]], optional): An optional list of existing + blocks to initialize the BlockTable with. If not provided, an empty + BlockTable is created. + max_block_sliding_window (Optional[int], optional): The number of + blocks to keep around for each sequance. If None, all blocks + are kept (eg., when sliding window is not used). + It should at least fit the sliding window size of the model. + + Attributes: + _block_size (int): The maximum number of tokens that can be stored in a + single block. + _allocator (DeviceAwareBlockAllocator): The block allocator used to + manage memory for the blocks. + _blocks (Optional[List[Block]]): The list of blocks managed by this + BlockTable. + _num_full_slots (int): The number of tokens currently stored in the + blocks. + """ + + def __init__( + self, + block_size: int, + block_allocator: DeviceAwareBlockAllocator, + _blocks: Optional[List[Block]] = None, + max_block_sliding_window: Optional[int] = None, + cache_namespace: Optional[bytes] = None, + ): + self._block_size = block_size + self._allocator = block_allocator + self._cache_namespace = cache_namespace + if _blocks is None: + _blocks = [] + self._blocks: BlockList = BlockList(_blocks) + + self._max_block_sliding_window = max_block_sliding_window + self._num_full_slots = self._get_num_token_ids() + + @staticmethod + def get_num_required_blocks(token_ids: List[int], + block_size: int, + num_lookahead_slots: int = 0) -> int: + """Calculates the minimum number of blocks required to store a given + sequence of token IDs along with any look-ahead slots that may be + required (like in multi-step + chunked-prefill). + + This assumes worst-case scenario, where every block requires a new + allocation (e.g. ignoring prefix caching). + + Args: + token_ids (List[int]): The sequence of token IDs to be stored. + block_size (int): The maximum number of tokens that can be stored in + a single block. + num_lookahead_slots (int): look-ahead slots that the sequence may + require. + + Returns: + int: The minimum number of blocks required to store the given + sequence of token IDs along with any required look-ahead slots. + """ + return cdiv(len(token_ids) + num_lookahead_slots, block_size) + + def allocate(self, + token_ids: List[int], + device: Device = Device.GPU) -> None: + """Allocates memory blocks for storing the given sequence of token IDs. + + This method allocates the required number of blocks to store the given + sequence of token IDs. + + Args: + token_ids (List[int]): The sequence of token IDs to be stored. + device (Device, optional): The device on which the blocks should be + allocated. Defaults to Device.GPU. + """ + assert not self._is_allocated + assert token_ids + blocks = self._allocate_blocks_for_token_ids(prev_block=None, + token_ids=token_ids, + device=device) + self.update(blocks) + self._num_full_slots = len(token_ids) + + def update(self, blocks: List[Block]) -> None: + """Resets the table to the newly provided blocks + (with their corresponding block ids) + """ + self._blocks.update(blocks) + + def get_content_hashes(self) -> List[bytes]: + """Returns block-level content hashes for full blocks in order.""" + content_hashes: List[bytes] = [] + for block in self._blocks: + block_hash = block.content_hash + if block_hash is not None: + content_hashes.append(block_hash) + return content_hashes + + def append_token_ids(self, + token_ids: List[int], + num_lookahead_slots: int = 0, + num_computed_slots: Optional[int] = None) -> None: + """Appends a sequence of token IDs to the existing blocks in the + BlockTable. + + This method appends the given sequence of token IDs to the existing + blocks in the BlockTable. If there is not enough space in the existing + blocks, new blocks are allocated using the `ensure_num_empty_slots` + method to accommodate the additional tokens. + + The token IDs are divided into chunks of size `block_size` (except for + the first chunk, which may be smaller), and each chunk is appended to a + separate block. + + Args: + token_ids (List[int]): The sequence of token IDs to be appended. + num_computed_slots (Optional[int]): The number of KV cache slots + that are already filled (computed). + When sliding window is enabled, this is used to compute how many + blocks to drop at the front of the sequence. + Without sliding window, None can be passed. + Without chunked prefill, it should be the same as + _num_full_slots. + """ + assert self._is_allocated, "no blocks have been allocated" + assert len(self._blocks) > 0 + + # Drop blocks that are no longer needed due to sliding window + if self._max_block_sliding_window is not None: + null_block = self._allocator.allocate_or_get_null_block() + assert num_computed_slots is not None + end_block_idx = (num_computed_slots // + self._block_size) - self._max_block_sliding_window + for idx in range(0, end_block_idx): + b = self._blocks[idx] + if b is not null_block: + self._allocator.free(b) + self._blocks[idx] = null_block + + # Ensure there are enough empty slots for the new tokens plus + # lookahead slots + self.ensure_num_empty_slots(num_empty_slots=len(token_ids) + + num_lookahead_slots) + + # Update the blocks with the new tokens + first_block_idx = self._num_full_slots // self._block_size + token_blocks = self._chunk_token_blocks_for_append(token_ids) + + for i, token_block in enumerate(token_blocks): + self._blocks.append_token_ids(first_block_idx + i, token_block) + + self._num_full_slots += len(token_ids) + + def ensure_num_empty_slots(self, num_empty_slots: int) -> None: + """Ensures that the BlockTable has at least the specified number of + empty slots available. + + This method checks if the BlockTable has enough empty slots (i.e., + available space) to accommodate the requested number of tokens. If not, + it allocates additional blocks on the GPU to ensure that the required + number of empty slots is available. + + Args: + num_empty_slots (int): The minimum number of empty slots required. + """ + # Currently the block table only supports + # appending tokens to GPU blocks. + device = Device.GPU + assert self._is_allocated + + if self._num_empty_slots >= num_empty_slots: + return + + slots_to_allocate = num_empty_slots - self._num_empty_slots + blocks_to_allocate = cdiv(slots_to_allocate, self._block_size) + + for _ in range(blocks_to_allocate): + assert len(self._blocks) > 0 + self._blocks.append( + self._allocator.allocate_mutable_block( + prev_block=self._blocks[-1], device=device)) + + def fork(self) -> "BlockTable": + """Creates a new BlockTable instance with a copy of the blocks from the + current instance. + + This method creates a new BlockTable instance with the same block size, + block allocator, and a copy of the blocks from the current instance. The + new BlockTable has its own independent set of blocks, but shares the + same underlying memory allocation with the original BlockTable. + + Returns: + BlockTable: A new BlockTable instance with a copy of the blocks from + the current instance. + """ + assert self._is_allocated + assert len(self._blocks) > 0 + forked_blocks = self._allocator.fork(self._blocks[-1]) + return BlockTable( + block_size=self._block_size, + block_allocator=self._allocator, + _blocks=forked_blocks, + max_block_sliding_window=self._max_block_sliding_window, + cache_namespace=self._cache_namespace, + ) + + def free(self) -> None: + """Frees the memory occupied by the blocks in the BlockTable. + + This method iterates over all the blocks in the `_blocks` list and calls + the `free` method of the `_allocator` object to release the memory + occupied by each block. After freeing all the blocks, the `_blocks` list + is set to `None`. + """ + for block in self.blocks: + self._allocator.free(block) + self._blocks.reset() + + @property + def physical_block_ids(self) -> List[int]: + """Returns a list of physical block indices for the blocks in the + BlockTable. + + This property returns a list of integers, where each integer represents + the physical block index of a corresponding block in the `_blocks` list. + The physical block index is a unique identifier for the memory location + occupied by the block. + + Returns: + List[int]: A list of physical block indices for the blocks in the + BlockTable. + """ + return self._blocks.ids() + + def get_unseen_token_ids(self, sequence_token_ids: List[int]) -> List[int]: + """Get the number of "unseen" tokens in the sequence. + + Unseen tokens are tokens in the sequence corresponding to this block + table, but are not yet appended to this block table. + + Args: + sequence_token_ids (List[int]): The list of token ids in the + sequence. + + Returns: + List[int]: The postfix of sequence_token_ids that has not yet been + appended to the block table. + """ + + # Since the block table is append-only, the unseen token ids are the + # ones after the appended ones. + return sequence_token_ids[self.num_full_slots:] + + def _allocate_blocks_for_token_ids(self, prev_block: Optional[Block], + token_ids: List[int], + device: Device) -> List[Block]: + blocks: List[Block] = [] + + block_token_ids = [] + tail_token_ids = [] + for cur_token_ids in chunk_list(token_ids, self._block_size): + if len(cur_token_ids) == self._block_size: + block_token_ids.append(cur_token_ids) + else: + tail_token_ids.append(cur_token_ids) + + if block_token_ids: + blocks.extend(self._allocate_immutable_blocks( + prev_block=prev_block, + block_token_ids=block_token_ids, + device=device)) + prev_block = blocks[-1] + + if tail_token_ids: + assert len(tail_token_ids) == 1 + cur_token_ids = tail_token_ids[0] + + block = self._allocate_mutable_block(prev_block=prev_block, + device=device) + block.append_token_ids(cur_token_ids) + + blocks.append(block) + + return blocks + + def _allocate_mutable_block(self, prev_block: Optional[Block], + device: Device) -> Block: + if self._cache_namespace is None: + return self._allocator.allocate_mutable_block( + prev_block=prev_block, device=device) + + with_cache_namespace = getattr( + self._allocator, "allocate_mutable_block_with_cache_namespace", + None) + if callable(with_cache_namespace): + return with_cache_namespace( + prev_block=prev_block, + cache_namespace=self._cache_namespace, + device=device) + + backend_allocators = getattr(self._allocator, "_allocators", None) + if isinstance(backend_allocators, dict): + device_allocator = backend_allocators.get(device) + if device_allocator is not None: + with_cache_namespace = getattr( + device_allocator, + "allocate_mutable_block_with_cache_namespace", None) + if callable(with_cache_namespace): + return with_cache_namespace( + prev_block=prev_block, + cache_namespace=self._cache_namespace) + + return self._allocator.allocate_mutable_block( + prev_block=prev_block, device=device) + + def _allocate_immutable_blocks(self, + prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Device) -> List[Block]: + if self._cache_namespace is None: + return self._allocator.allocate_immutable_blocks( + prev_block, + block_token_ids=block_token_ids, + device=device) + + with_cache_namespace = getattr( + self._allocator, "allocate_immutable_blocks_with_cache_namespace", None) + if callable(with_cache_namespace): + return with_cache_namespace( + prev_block=prev_block, + block_token_ids=block_token_ids, + cache_namespace=self._cache_namespace, + device=device) + + backend_allocator = getattr(self._allocator, "_allocators", None) + if isinstance(backend_allocator, dict): + device_allocator = backend_allocator.get(device) + if device_allocator is not None: + with_cache_namespace = getattr( + device_allocator, + "allocate_immutable_blocks_with_cache_namespace", + None) + if callable(with_cache_namespace): + return with_cache_namespace( + prev_block=prev_block, + block_token_ids=block_token_ids, + cache_namespace=self._cache_namespace) + + # Fallback: keep behavior identical when no namespace-aware allocator + # is available. + return self._allocator.allocate_immutable_blocks( + prev_block, + block_token_ids=block_token_ids, + device=device) + + def _get_all_token_ids(self) -> List[int]: + # NOTE: This function is O(seq_len); use sparingly. + token_ids: List[int] = [] + + if not self._is_allocated: + return token_ids + + for block in self.blocks: + token_ids.extend(block.token_ids) + + return token_ids + + def _get_num_token_ids(self) -> int: + res = 0 + for block in self.blocks: + res += len(block.token_ids) + + return res + + @property + def _is_allocated(self) -> bool: + return len(self._blocks) > 0 + + @property + def blocks(self) -> List[Block]: + return self._blocks.list() + + @property + def _num_empty_slots(self) -> int: + assert self._is_allocated + return len(self._blocks) * self._block_size - self._num_full_slots + + @property + def num_full_slots(self) -> int: + """Returns the total number of tokens currently stored in the + BlockTable. + + Returns: + int: The total number of tokens currently stored in the BlockTable. + """ + return self._num_full_slots + + def get_num_blocks_touched_by_append_slots( + self, token_ids: List[int], num_lookahead_slots: int) -> int: + """Determine how many blocks will be "touched" by appending the token + ids. + + This is required for the scheduler to determine whether a sequence can + continue generation, or if it must be preempted. + """ + # Math below is equivalent to: + # all_token_ids = token_ids + [-1] * num_lookahead_slots + # token_blocks = self._chunk_token_blocks_for_append(all_token_ids) + # return len(token_blocks) + + num_token_ids = len(token_ids) + num_lookahead_slots + first_chunk_size = self._block_size - (self._num_full_slots % + self._block_size) + num_token_blocks = (1 + math.ceil( + (num_token_ids - first_chunk_size) / self._block_size)) + return num_token_blocks + + def _chunk_token_blocks_for_append( + self, token_ids: List[int]) -> List[List[int]]: + """Split the token ids into block-sized chunks so they can be easily + appended to blocks. The first such "token block" may have less token ids + than the block size, since the last allocated block may be partially + full. + + If no token ids are provided, then no chunks are returned. + """ + + if not token_ids: + return [] + + first_chunk_size = self._block_size - (self._num_full_slots % + self._block_size) + token_blocks = [token_ids[:first_chunk_size]] + token_blocks.extend( + chunk_list(token_ids[first_chunk_size:], self._block_size)) + return token_blocks diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_gpu_block_allocator.py b/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_gpu_block_allocator.py new file mode 100644 index 0000000..9628289 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_gpu_block_allocator.py @@ -0,0 +1,475 @@ +from typing import Dict, FrozenSet, List, Optional, Tuple + +from vllm.core.block.cpu_kv_content_cache import (CpuKvContentCache, + cpu_kv_offload_enabled) +from vllm.core.block.interfaces import (Block, BlockAllocator, BlockId, + DeviceAwareBlockAllocator) +from vllm.core.block.naive_block import NaiveBlock, NaiveBlockAllocator +from vllm.core.block.prefix_caching_block import PrefixCachingBlockAllocator +from vllm.utils import Device + + +class CpuGpuBlockAllocator(DeviceAwareBlockAllocator): + """A block allocator that can allocate blocks on both CPU and GPU memory. + + This class implements the `DeviceAwareBlockAllocator` interface and provides + functionality for allocating and managing blocks of memory on both CPU and + GPU devices. + + The `CpuGpuBlockAllocator` maintains separate memory pools for CPU and GPU + blocks, and allows for allocation, deallocation, forking, and swapping of + blocks across these memory pools. + """ + + @staticmethod + def create( + allocator_type: str, + num_gpu_blocks: int, + num_cpu_blocks: int, + block_size: int, + ) -> DeviceAwareBlockAllocator: + """Creates a CpuGpuBlockAllocator instance with the specified + configuration. + + This static method creates and returns a CpuGpuBlockAllocator instance + based on the provided parameters. It initializes the CPU and GPU block + allocators with the specified number of blocks, block size, and + allocator type. + + Args: + allocator_type (str): The type of block allocator to use for CPU + and GPU blocks. Currently supported values are "naive" and + "prefix_caching". + num_gpu_blocks (int): The number of blocks to allocate for GPU + memory. + num_cpu_blocks (int): The number of blocks to allocate for CPU + memory. + block_size (int): The size of each block in number of tokens. + + Returns: + DeviceAwareBlockAllocator: A CpuGpuBlockAllocator instance with the + specified configuration. + + Notes: + - The block IDs are assigned contiguously, with GPU block IDs coming + before CPU block IDs. + """ + content_offload = cpu_kv_offload_enabled() + if content_offload and allocator_type != "prefix_caching": + raise RuntimeError( + "BI100_CPU_KV_OFFLOAD=1 requires prefix caching") + if content_offload and num_cpu_blocks <= 0: + raise RuntimeError( + "BI100_CPU_KV_OFFLOAD=1 requires at least one CPU KV block") + + block_ids = list(range(num_gpu_blocks + num_cpu_blocks)) + gpu_block_ids = block_ids[:num_gpu_blocks] + cpu_block_ids = block_ids[num_gpu_blocks:] + + if allocator_type == "naive": + gpu_allocator: BlockAllocator = NaiveBlockAllocator( + create_block=NaiveBlock, # type: ignore + num_blocks=num_gpu_blocks, + block_size=block_size, + block_ids=gpu_block_ids, + ) + + cpu_allocator: BlockAllocator = NaiveBlockAllocator( + create_block=NaiveBlock, # type: ignore + num_blocks=num_cpu_blocks, + block_size=block_size, + block_ids=cpu_block_ids, + ) + elif allocator_type == "prefix_caching": + gpu_allocator = PrefixCachingBlockAllocator( + num_blocks=num_gpu_blocks, + block_size=block_size, + block_ids=gpu_block_ids, + ) + + cpu_allocator = PrefixCachingBlockAllocator( + num_blocks=num_cpu_blocks, + block_size=block_size, + block_ids=cpu_block_ids, + ) + else: + raise ValueError(f"Unknown allocator type {allocator_type=}") + + return CpuGpuBlockAllocator( + cpu_block_allocator=cpu_allocator, + gpu_block_allocator=gpu_allocator, + cpu_content_cache=(CpuKvContentCache(num_cpu_blocks) + if content_offload else None), + ) + + def __init__(self, cpu_block_allocator: BlockAllocator, + gpu_block_allocator: BlockAllocator, + cpu_content_cache: Optional[CpuKvContentCache] = None): + assert not ( + cpu_block_allocator.all_block_ids + & gpu_block_allocator.all_block_ids + ), "cpu and gpu block allocators can't have intersection of block ids" + + self._allocators = { + Device.CPU: cpu_block_allocator, + Device.GPU: gpu_block_allocator, + } + + self._swap_mapping: Dict[int, int] = {} + self._null_block: Optional[Block] = None + self._cpu_content_cache = cpu_content_cache + + self._block_ids_to_allocator: Dict[int, BlockAllocator] = {} + for _, allocator in self._allocators.items(): + for block_id in allocator.all_block_ids: + self._block_ids_to_allocator[block_id] = allocator + + if self._cpu_content_cache is not None: + if not isinstance(gpu_block_allocator, + PrefixCachingBlockAllocator): + raise RuntimeError( + "CPU KV content tier requires PrefixCachingBlockAllocator") + if (self._cpu_content_cache.capacity != + cpu_block_allocator.get_num_total_blocks()): + raise RuntimeError( + "CPU KV content capacity must cover the complete CPU cache") + gpu_block_allocator.set_external_cache_callbacks( + claim=self._claim_cpu_content, + load=self._stage_cpu_to_gpu, + cancel=self._cancel_cpu_claim, + store=self._stage_gpu_to_cpu, + ) + + @property + def content_offload_enabled(self) -> bool: + return self._cpu_content_cache is not None + + def _claim_cpu_content(self, content_hash: bytes) -> Optional[int]: + assert self._cpu_content_cache is not None + return self._cpu_content_cache.claim_load(content_hash) + + def _cancel_cpu_claim(self, content_hash: bytes, cpu_slot: int) -> None: + assert self._cpu_content_cache is not None + self._cpu_content_cache.cancel_load(content_hash, cpu_slot) + + def _stage_cpu_to_gpu(self, content_hash: bytes, cpu_slot: int, + gpu_block_id: BlockId) -> None: + assert self._cpu_content_cache is not None + gpu_slot = self.get_physical_block_id(Device.GPU, gpu_block_id) + self._cpu_content_cache.stage_load( + content_hash, cpu_slot, gpu_slot) + + def _stage_gpu_to_cpu(self, content_hash: bytes, + gpu_block_id: BlockId) -> bool: + assert self._cpu_content_cache is not None + gpu_slot = self.get_physical_block_id(Device.GPU, gpu_block_id) + return self._cpu_content_cache.stage_store(content_hash, gpu_slot) + + def allocate_or_get_null_block(self) -> Block: + if self._null_block is None: + self._null_block = NullBlock( + self.allocate_mutable_block(None, Device.GPU)) + return self._null_block + + def allocate_mutable_block(self, prev_block: Optional[Block], + device: Device) -> Block: + """Allocates a new mutable block on the specified device. + + Args: + prev_block (Optional[Block]): The previous block to in the sequence. + Used for prefix hashing. + device (Device): The device on which to allocate the new block. + + Returns: + Block: The newly allocated mutable block. + """ + return self._allocators[device].allocate_mutable_block(prev_block) + + def allocate_immutable_blocks(self, prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Device) -> List[Block]: + """Allocates a new group of immutable blocks with the provided block + token IDs on the specified device. + + Args: + prev_block (Optional[Block]): The previous block in the sequence. + Used for prefix hashing. + block_token_ids (List[int]): The list of block token IDs to be + stored in the new blocks. + device (Device): The device on which to allocate the new block. + + Returns: + List[Block]: The newly allocated list of immutable blocks + containing the provided block token IDs. + """ + return self._allocators[device].allocate_immutable_blocks( + prev_block, block_token_ids) + + def allocate_immutable_block(self, prev_block: Optional[Block], + token_ids: List[int], + device: Device) -> Block: + """Allocates a new immutable block with the provided token IDs on the + specified device. + + Args: + prev_block (Optional[Block]): The previous block in the sequence. + Used for prefix hashing. + token_ids (List[int]): The list of token IDs to be stored in the new + block. + device (Device): The device on which to allocate the new block. + + Returns: + Block: The newly allocated immutable block containing the provided + token IDs. + """ + return self._allocators[device].allocate_immutable_block( + prev_block, token_ids) + + def free(self, block: Block) -> None: + """Frees the memory occupied by the given block. + + Args: + block (Block): The block to be freed. + """ + # Null block should never be freed + if isinstance(block, NullBlock): + return + block_id = block.block_id + assert block_id is not None + allocator = self._block_ids_to_allocator[block_id] + allocator.free(block) + + def fork(self, last_block: Block) -> List[Block]: + """Creates a new sequence of blocks that shares the same underlying + memory as the original sequence. + + Args: + last_block (Block): The last block in the original sequence. + + Returns: + List[Block]: A new list of blocks that shares the same memory as the + original sequence. + """ + # do not attempt to fork the null block + assert not isinstance(last_block, NullBlock) + block_id = last_block.block_id + assert block_id is not None + allocator = self._block_ids_to_allocator[block_id] + return allocator.fork(last_block) + + def get_num_free_blocks(self, device: Device) -> int: + """Returns the number of free blocks available on the specified device. + + Args: + device (Device): The device for which to query the number of free + blocks. AssertionError is raised if None is passed. + + Returns: + int: The number of free blocks available on the specified device. + """ + return self._allocators[device].get_num_free_blocks() + + def get_num_total_blocks(self, device: Device) -> int: + return self._allocators[device].get_num_total_blocks() + + def get_physical_block_id(self, device: Device, absolute_id: int) -> int: + """Returns the zero-offset block id on certain device given the + absolute block id. + + Args: + device (Device): The device for which to query relative block id. + absolute_id (int): The absolute block id for the block in + whole allocator. + + Returns: + int: The zero-offset block id on certain device. + """ + return self._allocators[device].get_physical_block_id(absolute_id) + + def swap(self, blocks: List[Block], src_device: Device, + dst_device: Device) -> Dict[int, int]: + """Execute the swap for the given blocks from source_device + on to dest_device, save the current swap mapping and append + them to the accumulated `self._swap_mapping` for each + scheduling move. + + Args: + blocks: List of blocks to be swapped. + src_device (Device): Device to swap the 'blocks' from. + dst_device (Device): Device to swap the 'blocks' to. + + Returns: + Dict[int, int]: Swap mapping from source_device + on to dest_device. + """ + if self.content_offload_enabled: + raise RuntimeError( + "request-level preemption swap cannot share CPU slots with " + "BI100_CPU_KV_OFFLOAD") + + src_block_ids = [block.block_id for block in blocks] + self._allocators[src_device].swap_out(blocks) + self._allocators[dst_device].swap_in(blocks) + dst_block_ids = [block.block_id for block in blocks] + + current_swap_mapping: Dict[int, int] = {} + for src_block_id, dst_block_id in zip(src_block_ids, dst_block_ids): + if src_block_id is not None and dst_block_id is not None: + self._swap_mapping[src_block_id] = dst_block_id + current_swap_mapping[src_block_id] = dst_block_id + return current_swap_mapping + + def get_num_full_blocks_touched(self, blocks: List[Block], + device: Device) -> int: + """Returns the number of full blocks that will be touched by + swapping in/out the given blocks on to the 'device'. + + Args: + blocks: List of blocks to be swapped. + device (Device): Device to swap the 'blocks' on. + + Returns: + int: the number of full blocks that will be touched by + swapping in/out the given blocks on to the 'device'. + Non full blocks are ignored when deciding the number + of blocks to touch. + """ + return self._allocators[device].get_num_full_blocks_touched(blocks) + + def clear_copy_on_writes(self) -> List[Tuple[int, int]]: + """Clears the copy-on-write (CoW) state and returns the mapping of + source to destination block IDs. + + Returns: + List[Tuple[int, int]]: A list mapping source block IDs to + destination block IDs. + """ + # CoW only supported on GPU + device = Device.GPU + return self._allocators[device].clear_copy_on_writes() + + def mark_blocks_as_accessed(self, block_ids: List[int], + now: float) -> None: + """Mark blocks as accessed, only use for prefix caching.""" + # Prefix caching only supported on GPU. + device = Device.GPU + return self._allocators[device].mark_blocks_as_accessed(block_ids, now) + + def mark_blocks_as_computed(self, block_ids: List[int]) -> None: + """Mark blocks as accessed, only use for prefix caching.""" + # Prefix caching only supported on GPU. + device = Device.GPU + return self._allocators[device].mark_blocks_as_computed(block_ids) + + def get_computed_block_ids(self, prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool) -> List[int]: + # Prefix caching only supported on GPU. + device = Device.GPU + return self._allocators[device].get_computed_block_ids( + prev_computed_block_ids, block_ids, skip_last_block_id) + + def get_common_computed_block_ids( + self, computed_seq_block_ids: List[List[int]]) -> List[int]: + # Prefix caching only supported on GPU. + device = Device.GPU + return self._allocators[device].get_common_computed_block_ids( + computed_seq_block_ids) + + @property + def all_block_ids(self) -> FrozenSet[int]: + return frozenset(self._block_ids_to_allocator.keys()) + + def get_prefix_cache_hit_rate(self, device: Device) -> float: + """Prefix cache hit rate. -1 means not supported or disabled.""" + assert device in self._allocators + return self._allocators[device].get_prefix_cache_hit_rate() + + def get_and_reset_swaps(self) -> List[Tuple[int, int]]: + """Returns and clears the mapping of source to destination block IDs. + Will be called after every swapping operations for now, and after every + schedule when BlockManagerV2 become default. Currently not useful. + + Returns: + List[Tuple[int, int]]: A mapping of source to destination block IDs. + """ + mapping = self._swap_mapping.copy() + self._swap_mapping.clear() + return list(mapping.items()) + + def get_and_reset_prefix_swaps( + self) -> Tuple[List[Tuple[int, int]], List[Tuple[int, int]]]: + """Return scheduler-owned (CPU->GPU, GPU->CPU) content maps.""" + if self._cpu_content_cache is None: + return [], [] + return self._cpu_content_cache.drain_step() + + def begin_prefix_cache_step(self) -> None: + if self._cpu_content_cache is not None: + self._cpu_content_cache.begin_step() + + +class NullBlock(Block): + """ + Null blocks are used as a placeholders for KV cache blocks that have + been dropped due to sliding window. + This implementation just wraps an ordinary block and prevents it from + being modified. It also allows for testing if a block is NullBlock + via isinstance(). + """ + + def __init__(self, proxy: Block): + super().__init__() + self._proxy = proxy + + def append_token_ids(self, token_ids: List[BlockId]): + raise ValueError("null block should not be modified") + + @property + def block_id(self): + return self._proxy.block_id + + @block_id.setter + def block_id(self, value: Optional[BlockId]): + raise ValueError("null block should not be modified") + + @property + def token_ids(self) -> List[BlockId]: + return self._proxy.token_ids + + @property + def num_tokens_total(self) -> int: + raise NotImplementedError( + "num_tokens_total is not used for null block") + + @property + def num_empty_slots(self) -> BlockId: + return self._proxy.num_empty_slots + + @property + def is_full(self): + return self._proxy.is_full + + @property + def prev_block(self): + return self._proxy.prev_block + + @property + def computed(self): + return self._proxy.computed + + @computed.setter + def computed(self, value): + self._proxy.computed = value + + @property + def last_accessed(self) -> float: + return self._proxy.last_accessed + + @last_accessed.setter + def last_accessed(self, last_accessed_ts: float): + self._proxy.last_accessed = last_accessed_ts + + @property + def content_hash(self): + return self._proxy.content_hash diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_kv_content_cache.py b/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_kv_content_cache.py new file mode 100644 index 0000000..9d77ebd --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/block/cpu_kv_content_cache.py @@ -0,0 +1,255 @@ +"""Scheduler-owned content index for an inclusive CPU KV cache tier.""" + +from __future__ import annotations + +import heapq +import os +from collections import OrderedDict +from typing import Dict, List, Mapping, Optional, Set, Tuple + + +ContentHash = bytes +SwapMapping = List[Tuple[int, int]] + + +def cpu_kv_offload_enabled( + environ: Optional[Mapping[str, str]] = None, +) -> bool: + """Read the experimental selector without accepting ambiguous values.""" + source = os.environ if environ is None else environ + value = source.get("BI100_CPU_KV_OFFLOAD", "0") + if value == "0": + return False + if value == "1": + return True + raise RuntimeError( + "BI100_CPU_KV_OFFLOAD must be exactly '0' or '1', " + f"got {value!r}") + + +class CpuKvContentCache: + """Track immutable KV blocks held in the worker's pinned CPU cache. + + The scheduler owns this metadata and sends identical physical block maps + to every tensor-parallel worker. CPU copies are inclusive: loading a block + back to GPU does not remove its CPU entry. Slots touched by either transfer + direction are pinned for the whole scheduling step so a D2H destination + can never overwrite an H2D source before workers execute the maps. + """ + + def __init__(self, capacity: int) -> None: + if capacity <= 0: + raise ValueError("CPU KV content cache capacity must be positive") + self.capacity = capacity + self._hash_to_slot: Dict[ContentHash, int] = {} + self._slot_to_hash: Dict[int, ContentHash] = {} + self._ready_slots: Set[int] = set() + self._lru: OrderedDict[int, None] = OrderedDict() + self._free_slots = list(range(capacity)) + heapq.heapify(self._free_slots) + + self._step_slots_in_use: Set[int] = set() + self._step_load_slots: Set[int] = set() + self._step_h2d: Dict[int, int] = {} + self._step_d2h: Dict[int, int] = {} + self._deferred_d2h: Dict[int, ContentHash] = {} + self._deferred_hashes: Set[ContentHash] = set() + self._pending_ready_slots: Set[int] = set() + + self.hits = 0 + self.misses = 0 + self.stores = 0 + self.deduplicated_stores = 0 + self.evictions = 0 + self.skipped_stores = 0 + + @staticmethod + def _validate_hash(content_hash: ContentHash) -> None: + if not isinstance(content_hash, bytes) or len(content_hash) != 32: + raise ValueError("CPU KV cache key must be a 32-byte content hash") + + @staticmethod + def _validate_block_id(name: str, block_id: int) -> None: + if not isinstance(block_id, int) or isinstance(block_id, bool): + raise TypeError(f"{name} must be an integer") + if block_id < 0: + raise ValueError(f"{name} must be non-negative") + + def _touch(self, slot: int) -> None: + self._lru.pop(slot, None) + self._lru[slot] = None + + def _select_store_slot(self) -> Optional[int]: + if self._free_slots: + return heapq.heappop(self._free_slots) + for slot in self._lru: + if slot not in self._step_slots_in_use: + return slot + return None + + def _commit_store(self, content_hash: ContentHash, + gpu_block: int, slot: int) -> None: + old_hash = self._slot_to_hash.get(slot) + if old_hash is not None: + if slot in self._step_slots_in_use: + raise RuntimeError("selected an in-use CPU KV slot for eviction") + del self._hash_to_slot[old_hash] + self._ready_slots.discard(slot) + self.evictions += 1 + + if slot in self._step_h2d: + raise RuntimeError( + "a CPU KV slot cannot be an H2D source and D2H destination " + "in one scheduler step") + if slot in self._step_d2h.values(): + raise RuntimeError(f"duplicate D2H destination CPU slot {slot}") + + self._hash_to_slot[content_hash] = slot + self._slot_to_hash[slot] = content_hash + self._ready_slots.discard(slot) + self._step_slots_in_use.add(slot) + self._step_d2h[gpu_block] = slot + self._touch(slot) + self.stores += 1 + + def begin_step(self) -> None: + """Publish D2H stores returned by the preceding synchronous step.""" + if (self._step_slots_in_use or self._step_h2d or self._step_d2h + or self._deferred_d2h or self._deferred_hashes): + raise RuntimeError("cannot begin a CPU KV step before draining it") + self._ready_slots.update(self._pending_ready_slots) + self._pending_ready_slots.clear() + + def _require_step_started(self) -> None: + if self._pending_ready_slots: + raise RuntimeError( + "CPU KV step must begin before content lookup or eviction") + + def claim_load(self, content_hash: ContentHash) -> Optional[int]: + """Pin and return a ready CPU source for this scheduling step.""" + self._validate_hash(content_hash) + self._require_step_started() + slot = self._hash_to_slot.get(content_hash) + if slot is None or slot not in self._ready_slots: + self.misses += 1 + return None + if slot in self._step_slots_in_use: + raise RuntimeError( + f"CPU KV slot {slot} was claimed twice in one scheduler step") + self._step_slots_in_use.add(slot) + self._step_load_slots.add(slot) + self._touch(slot) + self.hits += 1 + return slot + + def cancel_load(self, content_hash: ContentHash, cpu_slot: int) -> None: + """Release a claim when GPU allocation fails before H2D is staged.""" + self._validate_hash(content_hash) + self._validate_block_id("cpu_slot", cpu_slot) + if self._hash_to_slot.get(content_hash) != cpu_slot: + raise RuntimeError("CPU KV load cancellation key/slot mismatch") + if cpu_slot in self._step_h2d: + raise RuntimeError("cannot cancel a CPU KV load after H2D staging") + if cpu_slot not in self._step_slots_in_use: + raise RuntimeError("cannot cancel an unclaimed CPU KV load") + self._step_slots_in_use.remove(cpu_slot) + self._step_load_slots.remove(cpu_slot) + + def stage_load(self, content_hash: ContentHash, cpu_slot: int, + gpu_block: int) -> None: + """Stage one CPU-to-GPU promotion after the GPU slot is reserved.""" + self._validate_hash(content_hash) + self._validate_block_id("cpu_slot", cpu_slot) + self._validate_block_id("gpu_block", gpu_block) + if self._hash_to_slot.get(content_hash) != cpu_slot: + raise RuntimeError("CPU KV load key/slot mismatch") + if cpu_slot not in self._ready_slots: + raise RuntimeError("CPU KV load source is not ready") + if cpu_slot not in self._step_slots_in_use: + raise RuntimeError("CPU KV load source was not claimed") + if cpu_slot in self._step_h2d: + raise RuntimeError(f"duplicate H2D source CPU slot {cpu_slot}") + if gpu_block in self._step_h2d.values(): + raise RuntimeError(f"duplicate H2D destination GPU block {gpu_block}") + if cpu_slot in self._step_d2h.values(): + raise RuntimeError( + "a CPU KV slot cannot be an H2D source and D2H destination " + "in one scheduler step") + self._step_h2d[cpu_slot] = gpu_block + + def stage_store(self, content_hash: ContentHash, + gpu_block: int) -> bool: + """Stage a lazy GPU-to-CPU copy for an evicted immutable block.""" + self._validate_hash(content_hash) + self._validate_block_id("gpu_block", gpu_block) + self._require_step_started() + if (content_hash in self._hash_to_slot + or content_hash in self._deferred_hashes): + slot = self._hash_to_slot.get(content_hash) + if slot is not None: + self._touch(slot) + self.deduplicated_stores += 1 + return False + if gpu_block in self._step_d2h or gpu_block in self._deferred_d2h: + raise RuntimeError(f"duplicate D2H source GPU block {gpu_block}") + + if self._free_slots: + self._commit_store( + content_hash, gpu_block, heapq.heappop(self._free_slots)) + return True + + # Do not replace resident content until every lookup in this scheduler + # step is known. A later H2D claim can refer to any current LRU entry. + self._deferred_d2h[gpu_block] = content_hash + self._deferred_hashes.add(content_hash) + return True + + def _resolve_deferred_stores(self) -> None: + if self._step_load_slots: + self.skipped_stores += len(self._deferred_d2h) + else: + for gpu_block, content_hash in self._deferred_d2h.items(): + slot = self._select_store_slot() + if slot is None: + self.skipped_stores += 1 + continue + self._commit_store(content_hash, gpu_block, slot) + self._deferred_d2h.clear() + self._deferred_hashes.clear() + + def drain_step(self) -> Tuple[SwapMapping, SwapMapping]: + """Finalize this synchronous step and return (H2D, D2H) maps.""" + self._resolve_deferred_stores() + transfer_slots = ( + set(self._step_h2d) | set(self._step_d2h.values())) + if transfer_slots != self._step_slots_in_use: + raise RuntimeError( + "CPU KV scheduler step contains an uncommitted slot claim") + if set(self._step_h2d) & set(self._step_d2h.values()): + raise RuntimeError( + "CPU KV scheduler step reuses a CPU slot across directions") + if set(self._step_h2d) != self._step_load_slots: + raise RuntimeError( + "CPU KV scheduler step contains an unstaged load claim") + + swap_in = sorted(self._step_h2d.items()) + swap_out = sorted(self._step_d2h.items()) + self._pending_ready_slots.update(self._step_d2h.values()) + self._step_h2d.clear() + self._step_d2h.clear() + self._step_slots_in_use.clear() + self._step_load_slots.clear() + return swap_in, swap_out + + def resident_slot(self, content_hash: ContentHash) -> Optional[int]: + self._validate_hash(content_hash) + return self._hash_to_slot.get(content_hash) + + def is_ready(self, content_hash: ContentHash) -> bool: + self._validate_hash(content_hash) + slot = self._hash_to_slot.get(content_hash) + return slot is not None and slot in self._ready_slots + + @property + def resident_count(self) -> int: + return len(self._hash_to_slot) diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/block/prefix_caching_block.py b/qwen3_6_scripts/vendor_overrides/vllm/core/block/prefix_caching_block.py new file mode 100644 index 0000000..772fa71 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/block/prefix_caching_block.py @@ -0,0 +1,1183 @@ +"""Token blocks.""" +import hashlib +import struct +from os.path import commonprefix +from typing import (Callable, Dict, FrozenSet, Iterable, List, Optional, Set, + Tuple) + +from vllm.core.block.common import (CacheMetricData, CopyOnWriteTracker, + get_all_blocks_recursively) +from vllm.core.block.interfaces import Block, BlockAllocator, BlockId, Device +from vllm.core.block.naive_block import (BlockPool, NaiveBlock, + NaiveBlockAllocator) +from vllm.core.evictor_v2 import (EvictionPolicy, Evictor, + eviction_policy_from_env, make_evictor) + +PrefixHash = bytes + +# By default, we init our block access time as _DEFAULT_LAST_ACCESSED_TIME +# so that if we find one block is still hold _DEFAULT_LAST_ACCESSED_TIME, +# then we know this block hasn't been accessed yet. +_DEFAULT_LAST_ACCESSED_TIME = -1 + + +class BlockTracker: + """Used to track the status of a block inside the prefix caching allocator + """ + __slots__ = ("active", "last_accessed", "computed") + + def reset(self): + self.last_accessed: float = _DEFAULT_LAST_ACCESSED_TIME + self.computed: bool = False + + def __init__(self): + self.active: bool = False + self.reset() + + def enable(self): + assert not self.active + self.active = True + self.reset() + + def disable(self): + assert self.active + self.active = False + self.reset() + + +class PrefixCachingBlockAllocator(BlockAllocator): + """A block allocator that implements prefix caching. + + The PrefixCachingBlockAllocator maintains a cache of blocks based on their + content hash. It reuses blocks with the same content hash to avoid redundant + memory allocation. The allocator also supports copy-on-write operations. + + Args: + num_blocks (int): The total number of blocks to manage. + block_size (int): The size of each block in tokens. + block_ids(Optional[Iterable[int]], optional): An optional iterable of + block IDs. If not provided, block IDs will be assigned sequentially + from 0 to num_blocks - 1. + """ + + def __init__( + self, + num_blocks: int, + block_size: int, + block_ids: Optional[Iterable[int]] = None, + eviction_policy: Optional[EvictionPolicy] = None, + ): + if block_ids is None: + block_ids = range(num_blocks) + + self._block_size = block_size + + # A mapping of prefix hash to block index. All blocks which have a + # prefix hash will be in this dict, even if they have refcount 0. + self._cached_blocks: Dict[PrefixHash, BlockId] = {} + self._cache_namespace: Optional[bytes] = None + + # A list of immutable block IDs that have been touched by scheduler + # and should be marked as computed after an entire batch of sequences + # are scheduled. + self._touched_blocks: Set[BlockId] = set() + + # Used to track status of each physical block id + self._block_tracker: Dict[BlockId, BlockTracker] = {} + for block_id in block_ids: + self._block_tracker[block_id] = BlockTracker() + + # Pre-allocate "num_blocks * extra_factor" block objects. + # The "* extra_factor" is a buffer to allow more block objects + # than physical blocks + extra_factor = 4 + self._block_pool = BlockPool(self._block_size, self._create_block, + self, num_blocks * extra_factor) + + # An allocator for blocks that do not have prefix hashes. + self._hashless_allocator = NaiveBlockAllocator( + create_block=self._create_block, # type: ignore + num_blocks=num_blocks, + block_size=block_size, + block_ids=block_ids, + block_pool=self._block_pool, # Share block pool here + ) + + if eviction_policy is None: + eviction_policy = eviction_policy_from_env() + self.eviction_policy = eviction_policy + + # Evitor used to maintain how we want to handle those computed blocks + # if we find memory pressure is high. + self.evictor: Evictor = make_evictor(eviction_policy) + + # We share the refcounter between allocators. This allows us to promote + # blocks originally allocated in the hashless allocator to immutable + # blocks. + self._refcounter = self._hashless_allocator.refcounter + + self._cow_tracker = CopyOnWriteTracker( + refcounter=self._refcounter.as_readonly()) + + self.metric_data = CacheMetricData() + + self._external_cache_claim: Optional[ + Callable[[PrefixHash], Optional[int]]] = None + self._external_cache_load: Optional[ + Callable[[PrefixHash, int, BlockId], None]] = None + self._external_cache_cancel: Optional[ + Callable[[PrefixHash, int], None]] = None + self._external_cache_store: Optional[ + Callable[[PrefixHash, BlockId], bool]] = None + + def set_external_cache_callbacks( + self, + claim: Callable[[PrefixHash], Optional[int]], + load: Callable[[PrefixHash, int, BlockId], None], + cancel: Callable[[PrefixHash, int], None], + store: Callable[[PrefixHash, BlockId], bool], + ) -> None: + """Attach one scheduler-owned lower cache tier. + + Prefix caching is single-threaded in the scheduler. Keeping these + callbacks here lets the allocator reserve the CPU source before a GPU + victim is selected, which is required when that victim's physical slot + is immediately reused as the H2D destination. + """ + if self._external_cache_claim is not None: + raise RuntimeError("external prefix cache is already configured") + if not all(callable(callback) + for callback in (claim, load, cancel, store)): + raise TypeError("external prefix cache callbacks must be callable") + self._external_cache_claim = claim + self._external_cache_load = load + self._external_cache_cancel = cancel + self._external_cache_store = store + + # Implements Block.Factory. + def _create_block( + self, + prev_block: Optional[Block], + token_ids: List[int], + block_size: int, + allocator: BlockAllocator, + block_id: Optional[int] = None, + computed: bool = False, + ) -> Block: + # Bind block to self. + allocator = self + cache_namespace = self._cache_namespace + + return PrefixCachingBlock( + prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + block_id=block_id, + allocator=allocator, + computed=computed, + cache_namespace=cache_namespace, + ) + + def _init_block( + self, + prev_block: Optional[Block], + token_ids: List[int], + block_size: int, + *, + physical_block_id: Optional[int] = None, + cache_namespace: Optional[bytes] = None, + ) -> Block: + prev_namespace = self._cache_namespace + self._cache_namespace = cache_namespace + try: + block = self._block_pool.init_block( + prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + physical_block_id=physical_block_id) + # BlockPool reinitializes a pre-created block object directly, so + # the allocator factory above is bypassed on normal pool reuse. + # Restore the namespace before content_hash can be observed. + resolved_namespace = cache_namespace or b"" + if prev_block is not None and not resolved_namespace: + resolved_namespace = prev_block.cache_namespace + if block._cached_content_hash is not None: # type: ignore[attr-defined] + raise RuntimeError( + "pooled prefix block retained a content hash during init") + block._cache_namespace = resolved_namespace # type: ignore[attr-defined] + return block + finally: + self._cache_namespace = prev_namespace + + def allocate_immutable_block_with_cache_namespace( + self, + prev_block: Optional[Block], + token_ids: List[int], + cache_namespace: bytes, + device: Optional[Device] = None, + ) -> Block: + """Allocates an immutable block with a namespace prefix.""" + assert device is None + assert_prefix_caching_block_or_none(prev_block) + + block = self._init_block(prev_block=prev_block, + token_ids=token_ids, + block_size=self._block_size, + physical_block_id=None, + cache_namespace=cache_namespace) + assert block.content_hash is not None + + cached_block_id = self._cached_blocks.get(block.content_hash, None) + if cached_block_id is not None: + self.metric_data.query(hit=True) + block.block_id = cached_block_id + self._incr_refcount_cached_block(block) + return block + + if self._maybe_restore_external_cached_block(block): + self.metric_data.query(hit=True) + return block + self.metric_data.query(hit=False) + self._block_pool.free_block(block) + + block = self.allocate_mutable_block_with_cache_namespace( + prev_block=prev_block, + cache_namespace=cache_namespace, + device=device) + block.append_token_ids(token_ids) + return block + + def _maybe_restore_external_cached_block(self, block: Block) -> bool: + """Promote an immutable lower-tier hit into a computed GPU block.""" + if self._external_cache_claim is None: + return False + assert self._external_cache_load is not None + assert self._external_cache_cancel is not None + assert block.content_hash is not None + assert block.block_id is None + + cpu_slot = self._external_cache_claim(block.content_hash) + if cpu_slot is None: + return False + + try: + block_id = self._allocate_block_id() + except Exception: + self._external_cache_cancel(block.content_hash, cpu_slot) + raise + + block.block_id = block_id + try: + self._external_cache_load(block.content_hash, cpu_slot, block_id) + except Exception: + self._decr_refcount_hashless_block(block) + self._external_cache_cancel(block.content_hash, cpu_slot) + self._block_pool.free_block(block) + raise + + if block.content_hash in self._cached_blocks: + raise RuntimeError( + "external prefix promotion raced with a GPU cache insert") + self._cached_blocks[block.content_hash] = block_id + block.computed = True + self._block_tracker[block_id].computed = True + return True + + def allocate_immutable_blocks_with_cache_namespace( + self, + prev_block: Optional[Block], + block_token_ids: List[List[int]], + cache_namespace: bytes, + device: Optional[Device] = None) -> List[Block]: + if not block_token_ids: + return [] + + blocks = [] + prev_block = self.allocate_immutable_block_with_cache_namespace( + prev_block=prev_block, + token_ids=block_token_ids[0], + cache_namespace=cache_namespace, + device=device) + blocks.append(prev_block) + + for token_ids in block_token_ids[1:]: + prev_block = self.allocate_immutable_block_with_cache_namespace( + prev_block=prev_block, + token_ids=token_ids, + cache_namespace=cache_namespace, + device=device) + blocks.append(prev_block) + + return blocks + + def allocate_mutable_block_with_cache_namespace( + self, + prev_block: Optional[Block], + cache_namespace: bytes, + device: Optional[Device] = None, + ) -> Block: + """Allocates a mutable block with an optional namespace context.""" + assert device is None + assert_prefix_caching_block_or_none(prev_block) + + block_id = self._allocate_block_id() + block = self._init_block(prev_block=prev_block, + token_ids=[], + block_size=self._block_size, + physical_block_id=block_id, + cache_namespace=cache_namespace) + assert not block.computed + assert block.content_hash is None + return block + + def allocate_immutable_block(self, + prev_block: Optional[Block], + token_ids: List[int], + device: Optional[Device] = None) -> Block: + """Allocates an immutable block with the given token IDs, reusing cached + blocks if possible. + + Args: + prev_block (Optional[Block]): The previous block in the sequence. + token_ids (List[int]): The token IDs to be stored in the block. + + Returns: + Block: The allocated immutable block. + """ + return self.allocate_immutable_block_with_cache_namespace( + prev_block=prev_block, + token_ids=token_ids, + cache_namespace=b"", + device=device) + + def allocate_immutable_blocks( + self, + prev_block: Optional[Block], + block_token_ids: List[List[int]], + device: Optional[Device] = None) -> List[Block]: + blocks = [] + for token_ids in block_token_ids: + prev_block = self.allocate_immutable_block(prev_block=prev_block, + token_ids=token_ids, + device=device) + blocks.append(prev_block) + return blocks + + def allocate_mutable_block(self, + prev_block: Optional[Block], + device: Optional[Device] = None) -> Block: + """Allocates a mutable block. If there are no free blocks, this will + evict unused cached blocks. + + Args: + prev_block (Block): The previous block in the sequence. + None is not allowed unlike it is super class. + + Returns: + Block: The allocated mutable block. + """ + assert device is None + assert_prefix_caching_block_or_none(prev_block) + + block_id = self._allocate_block_id() + block = self._init_block(prev_block=prev_block, + token_ids=[], + block_size=self._block_size, + physical_block_id=block_id) + assert not block.computed + assert block.content_hash is None + return block + + def _incr_refcount_cached_block(self, block: Block) -> None: + # Set this block to be "computed" since it is pointing to a + # cached block id (which was already computed) + block.computed = True + + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.incr(block_id) + if refcount == 1: + # In case a cached block was evicted, restore its tracking + if block_id in self.evictor: + self.evictor.remove(block_id) + + self._track_block_id(block_id, computed=True) + + def _decr_refcount_cached_block(self, block: Block) -> None: + # Ensure this is immutable/cached block + assert block.content_hash is not None + + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.decr(block_id) + if refcount > 0: + block.block_id = None + return + else: + assert refcount == 0 + + # No longer used + assert block.content_hash in self._cached_blocks + + # Add the cached block to the evictor + # (This keeps the cached block around so it can be reused) + self.evictor.add(block_id, block.content_hash, block.num_tokens_total, + self._block_tracker[block_id].last_accessed) + + # Stop tracking the block + self._untrack_block_id(block_id) + + block.block_id = None + + def _decr_refcount_hashless_block(self, block: Block) -> None: + block_id = block.block_id + assert block_id is not None + + # We may have a fork case where block is shared, + # in which case, we cannot remove it from tracking + refcount = self._refcounter.get(block_id) + if refcount == 1: + self._untrack_block_id(block_id) + + # Decrement refcount of the block_id, but do not free the block object + # itself (will be handled by the caller) + self._hashless_allocator.free(block, keep_block_object=True) + + def _allocate_block_id(self) -> BlockId: + """First tries to allocate a block id from the hashless allocator, + and if there are no blocks, then tries to evict an unused cached block. + """ + hashless_block_id = self._maybe_allocate_hashless_block_id() + if hashless_block_id is not None: + return hashless_block_id + + evicted_block_id = self._maybe_allocate_evicted_block_id() + if evicted_block_id is not None: + return evicted_block_id + + # No block available in hashless allocator, nor in unused cache blocks. + raise BlockAllocator.NoFreeBlocksError() + + def _maybe_allocate_hashless_block_id(self) -> Optional[BlockId]: + try: + # Allocate mutable block and extract its block_id + block = self._hashless_allocator.allocate_mutable_block( + prev_block=None) + block_id = block.block_id + self._block_pool.free_block(block) + + self._track_block_id(block_id, computed=False) + return block_id + except BlockAllocator.NoFreeBlocksError: + return None + + def _maybe_allocate_evicted_block_id(self) -> Optional[BlockId]: + if self.evictor.num_blocks == 0: + return None + + # Here we get an evicted block, which is only added + # into evictor if its ref counter is 0 + # and since its content would be changed, we need + # to remove it from _cached_blocks's tracking list + block_id, content_hash_to_evict = self.evictor.evict() + + # Sanity checks + assert content_hash_to_evict in self._cached_blocks + _block_id = self._cached_blocks[content_hash_to_evict] + assert self._refcounter.get(_block_id) == 0 + assert _block_id == block_id + + if self._external_cache_store is not None: + self._external_cache_store(content_hash_to_evict, block_id) + + self._cached_blocks.pop(content_hash_to_evict) + + self._refcounter.incr(block_id) + self._track_block_id(block_id, computed=False) + + return block_id + + def _free_block_id(self, block: Block) -> None: + """Decrements the refcount of the block. The block may be in two + possible states: (1) immutable/cached or (2) mutable/hashless. + In the first case, the refcount is decremented directly and the block + may be possibly added to the evictor. In other case, hashless + allocator free(..) with keep_block_object=True is called to only free + the block id (since the block object may be reused by the caller) + """ + block_id = block.block_id + assert block_id is not None, "Freeing unallocated block is undefined" + + if block.content_hash is not None: + # Immutable: This type of block is always cached, and we want to + # keep it in the evictor for future reuse + self._decr_refcount_cached_block(block) + else: + # Mutable: This type of block is not cached, so we release it + # directly to the hashless allocator + self._decr_refcount_hashless_block(block) + + assert block.block_id is None + + def free(self, block: Block, keep_block_object: bool = False) -> None: + """Release the block (look at free_block_id(..) docs) + """ + # Release the physical block index + self._free_block_id(block) + + # Release the block object to the pool + if not keep_block_object: + self._block_pool.free_block(block) + + def fork(self, last_block: Block) -> List[Block]: + """Creates a new sequence of blocks that shares the same underlying + memory as the original sequence. + + Args: + last_block (Block): The last block in the original sequence. + + Returns: + List[Block]: The new sequence of blocks that shares the same memory + as the original sequence. + """ + source_blocks = get_all_blocks_recursively(last_block) + + forked_blocks: List[Block] = [] + prev_block = None + for block in source_blocks: + block_id = block.block_id + assert block_id is not None + + refcount = self._refcounter.incr(block_id) + assert refcount != 1, "can't fork free'd block_id = {}".format( + block_id) + + forked_block = self._init_block( + prev_block=prev_block, + token_ids=block.token_ids, + block_size=self._block_size, + physical_block_id=block_id, + cache_namespace=block.cache_namespace) + + forked_blocks.append(forked_block) + prev_block = forked_blocks[-1] + + return forked_blocks + + def get_num_free_blocks(self, device: Optional[Device] = None) -> int: + assert device is None + # The number of free blocks is the number of hashless free blocks + # plus the number of blocks evictor could free from its list. + return self._hashless_allocator.get_num_free_blocks( + ) + self.evictor.num_blocks + + def get_num_total_blocks(self) -> int: + return self._hashless_allocator.get_num_total_blocks() + + def get_physical_block_id(self, absolute_id: int) -> int: + """Returns the zero-offset block id on certain block allocator + given the absolute block id. + + Args: + absolute_id (int): The absolute block id for the block + in whole allocator. + + Returns: + int: The rzero-offset block id on certain device. + """ + return sorted(self.all_block_ids).index(absolute_id) + + @property + def all_block_ids(self) -> FrozenSet[int]: + return self._hashless_allocator.all_block_ids + + def get_prefix_cache_hit_rate(self) -> float: + return self.metric_data.get_hit_rate() + + def is_block_cached(self, block: Block) -> bool: + assert block.content_hash is not None + return block.content_hash in self._cached_blocks + + def promote_to_immutable_block(self, block: Block) -> BlockId: + """Once a mutable block is full, it can be promoted to an immutable + block. This means that its content can be referenced by future blocks + having the same prefix. + + Note that if we already have a cached block with the same content, we + will replace the newly-promoted block's mapping with the existing cached + block id. + + Args: + block: The mutable block to be promoted. + + Returns: + BlockId: Either the original block index, or the block index of + the previously cached block matching the same content. + """ + # Ensure block can be promoted + assert block.content_hash is not None + assert block.block_id is not None + assert self._refcounter.get(block.block_id) > 0 + + if block.content_hash not in self._cached_blocks: + # No cached content hash => Set this block as cached. + # Note that this block cannot be marked as computed yet + # because other sequences in the same batch cannot reuse + # this block. + self._cached_blocks[block.content_hash] = block.block_id + # Mark this block as touched so that it can be marked as + # computed after the entire batch of sequences are scheduled. + self._touched_blocks.add(block.block_id) + return block.block_id + + # Reuse the cached content hash + self._decr_refcount_hashless_block(block) + block.block_id = self._cached_blocks[block.content_hash] + + # Increment refcount of the cached block and (possibly) restore + # it from the evictor. + # Note that in this case, the block is marked as computed + self._incr_refcount_cached_block(block) + + return block.block_id + + def cow_block_if_not_appendable(self, block: Block) -> BlockId: + """Performs a copy-on-write operation on the given block if it is not + appendable. + + Args: + block (Block): The block to check for copy-on-write. + + Returns: + BlockId: The block index of the new block if a copy-on-write + operation was performed, or the original block index if + no copy-on-write was necessary. + """ + src_block_id = block.block_id + assert src_block_id is not None + + if self._cow_tracker.is_appendable(block): + return src_block_id + + self._free_block_id(block) + trg_block_id = self._allocate_block_id() + + self._cow_tracker.record_cow(src_block_id, trg_block_id) + + return trg_block_id + + def clear_copy_on_writes(self) -> List[Tuple[BlockId, BlockId]]: + """Returns the copy-on-write source->destination mapping and clears it. + + Returns: + List[Tuple[BlockId, BlockId]]: A list mapping source + block indices to destination block indices. + """ + return self._cow_tracker.clear_cows() + + def mark_blocks_as_accessed(self, block_ids: List[int], + now: float) -> None: + """Mark blocks as accessed, used in prefix caching. + + If the block is added into evictor, we need to update corresponding + info in evictor's metadata. + """ + + for block_id in block_ids: + if self._block_tracker[block_id].active: + self._block_tracker[block_id].last_accessed = now + elif block_id in self.evictor: + self.evictor.update(block_id, now) + else: + raise ValueError( + "Mark block as accessed which is not belonged to GPU") + + def mark_blocks_as_computed(self, block_ids: List[int]) -> None: + # Mark all touched blocks as computed. + for block_id in self._touched_blocks: + self._block_tracker[block_id].computed = True + self._touched_blocks.clear() + + def _track_block_id(self, block_id: Optional[BlockId], + computed: bool) -> None: + assert block_id is not None + self._block_tracker[block_id].enable() + self._block_tracker[block_id].computed = computed + + def _untrack_block_id(self, block_id: Optional[BlockId]) -> None: + assert block_id is not None + self._block_tracker[block_id].disable() + + def block_is_computed(self, block_id: int) -> bool: + if self._block_tracker[block_id].active: + return self._block_tracker[block_id].computed + else: + return block_id in self.evictor + + def get_computed_block_ids(self, + prev_computed_block_ids: List[int], + block_ids: List[int], + skip_last_block_id: bool = True) -> List[int]: + prev_prefix_size = len(prev_computed_block_ids) + cur_size = len(block_ids) + if skip_last_block_id: + cur_size -= 1 + + # Sanity checks + assert cur_size >= 0 + assert prev_prefix_size <= cur_size + + ret = prev_computed_block_ids + for i in range(prev_prefix_size, cur_size): + block_id = block_ids[i] + if not self.block_is_computed(block_id): + break + ret.append(block_id) + return ret + + def get_common_computed_block_ids( + self, computed_seq_block_ids: List[List[int]]) -> List[int]: + """Return the block ids that are common for a given sequence group. + + Only those blocks that are immutable and already be marked + compyted would be taken consideration. + """ + + # NOTE We exclude the last block to avoid the case where the entire + # prompt is cached. This would cause erroneous behavior in model + # runner. + + # It returns a list of int although type annotation says list of string. + if len(computed_seq_block_ids) == 1: + return computed_seq_block_ids[0] + + return commonprefix([ + ids for ids in computed_seq_block_ids # type: ignore + if ids + ]) + + def get_num_full_blocks_touched(self, blocks: List[Block]) -> int: + """Returns the number of full blocks that will be touched by + swapping in/out. + + Args: + blocks: List of blocks to be swapped. + Returns: + int: the number of full blocks that will be touched by + swapping in/out the given blocks. Non full blocks are ignored + when deciding the number of blocks to touch. + """ + num_touched_blocks: int = 0 + for block in blocks: + # If the block has a match in the cache and the cached + # block is not referenced, then we still count it as a + # touched block + if block.is_full and (not self.is_block_cached(block) or \ + (block.content_hash is not None and \ + self._cached_blocks[block.content_hash] in \ + self.evictor)): + num_touched_blocks += 1 + return num_touched_blocks + + def swap_out(self, blocks: List[Block]) -> None: + """Execute the swap out actions. Basically just free the + given blocks. + + Args: + blocks: List of blocks to be swapped out. + """ + for block in blocks: + self._free_block_id(block) + + def swap_in(self, blocks: List[Block]) -> None: + """Execute the swap in actions. Change the block id from + old allocator to current allocator for each block to finish + the block table update. + + Args: + blocks: List of blocks to be swapped in. + """ + for block in blocks: + # Here we allocate either immutable or mutable block and then + # extract its block_id. Note that the block object is released + # and the block_id is assigned to "block" to allow reusing the + # existing "block" object + if block.is_full: + tmp_block = ( + self.allocate_immutable_block_with_cache_namespace( + prev_block=block.prev_block, + token_ids=block.token_ids, + cache_namespace=block.cache_namespace)) + else: + tmp_block = ( + self.allocate_mutable_block_with_cache_namespace( + prev_block=block.prev_block, + cache_namespace=block.cache_namespace)) + tmp_block.append_token_ids(block.token_ids) + + block_id = tmp_block.block_id + self._block_pool.free_block(tmp_block) + + block.block_id = block_id # Assign block_id + + +class PrefixCachingBlock(Block): + """A block implementation that supports prefix caching. + + The PrefixCachingBlock class represents a block of token IDs with prefix + caching capabilities. It wraps a NaiveBlock internally and provides + additional functionality for content hashing and promoting immutable blocks + with the prefix caching allocator. + + Args: + prev_block (Optional[PrefixCachingBlock]): The previous block in the + sequence. + token_ids (List[int]): The initial token IDs to be stored in the block. + block_size (int): The maximum number of token IDs that can be stored in + the block. + allocator (BlockAllocator): The prefix + caching block allocator associated with this block. + block_id (Optional[int], optional): The physical block index + of this block. Defaults to None. + """ + + def __init__( + self, + prev_block: Optional[Block], + token_ids: List[int], + block_size: int, + allocator: BlockAllocator, + block_id: Optional[int] = None, + computed: bool = False, + cache_namespace: Optional[bytes] = None, + ): + assert isinstance(allocator, PrefixCachingBlockAllocator), ( + "Currently this class is only tested with " + "PrefixCachingBlockAllocator. Got instead allocator = {}".format( + allocator)) + assert_prefix_caching_block_or_none(prev_block) + + self._prev_block = prev_block + self._cached_content_hash: Optional[bytes] = None + self._cache_namespace = cache_namespace or b"" + if self._prev_block is not None and not self._cache_namespace: + self._cache_namespace = self._prev_block.cache_namespace + self._cached_num_tokens_total: int = 0 + self._allocator = allocator + self._last_accessed: float = _DEFAULT_LAST_ACCESSED_TIME + self._computed = computed + + # On the first time, we create the block object, and next we only + # reinitialize it + if hasattr(self, "_block"): + self._block.__init__( # type: ignore[has-type] + prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + block_id=block_id, + allocator=self._allocator) + else: + self._block = NaiveBlock(prev_block=prev_block, + token_ids=token_ids, + block_size=block_size, + block_id=block_id, + allocator=self._allocator) + + self._update_num_tokens_total() + + def _update_num_tokens_total(self): + """Incrementally computes the number of tokens that there is + till the current block (included) + """ + res = 0 + + # Add all previous blocks + if self._prev_block is not None: + res += self._prev_block.num_tokens_total + + # Add current block + res += len(self.token_ids) + + self._cached_num_tokens_total = res + + @property + def computed(self) -> bool: + return self._computed + + @computed.setter + def computed(self, value) -> None: + self._computed = value + + @property + def last_accessed(self) -> float: + return self._last_accessed + + @last_accessed.setter + def last_accessed(self, last_accessed_ts: float): + self._last_accessed = last_accessed_ts + + def append_token_ids(self, token_ids: List[int]) -> None: + """Appends the given token IDs to the block and registers the block as + immutable if the block becomes full. + + Args: + token_ids (List[int]): The token IDs to be appended to the block. + """ + # Ensure this is mutable block (not promoted) + assert self.content_hash is None + assert not self.computed + + if len(token_ids) == 0: + return + + # Ensure there are input tokens + assert token_ids, "Got token_ids = {}".format(token_ids) + + # Naive block handles CoW. + self._block.append_token_ids(token_ids) + self._update_num_tokens_total() + + # If the content hash is present, then the block can be made immutable. + # Register ourselves with the allocator, potentially replacing the + # physical block index. + if self.content_hash is not None: + self.block_id = self._allocator.promote_to_immutable_block(self) + + @property + def block_id(self) -> Optional[int]: + return self._block.block_id + + @block_id.setter + def block_id(self, value) -> None: + self._block.block_id = value + + @property + def is_full(self) -> bool: + return self._block.is_full + + @property + def num_empty_slots(self) -> int: + return self._block.num_empty_slots + + @property + def num_tokens_total(self) -> int: + return self._cached_num_tokens_total + + @property + def block_size(self) -> int: + return self._block.block_size + + @property + def token_ids(self) -> List[int]: + return self._block.token_ids + + @property + def prev_block(self) -> Optional[Block]: + return self._prev_block + + @property + def cache_namespace(self) -> bytes: + return self._cache_namespace + + @property + def content_hash(self) -> Optional[bytes]: + """Return the content-based hash of the current block, or None if it is + not yet defined. + + For the content-based hash to be defined, the current block must be + full. + """ + # If the hash is already computed, return it. + if self._cached_content_hash is not None: + return self._cached_content_hash + + # We cannot compute a hash for the current block because it is not full. + if not self.is_full: + return None + + is_first_block = self._prev_block is None + prev_block_hash = ( + None if is_first_block else + self._prev_block.content_hash # type: ignore + ) + + # Previous block exists but does not yet have a hash. + # Return no hash in this case. + if prev_block_hash is None and not is_first_block: + return None + + self._cached_content_hash = PrefixCachingBlock.hash_block_tokens( + is_first_block, + prev_block_hash, + cur_block_token_ids=self.token_ids, + cache_namespace=self._cache_namespace) + return self._cached_content_hash + + @staticmethod + def hash_block_tokens( + is_first_block: bool, + prev_block_hash: Optional[PrefixHash], + cur_block_token_ids: List[int], + cache_namespace: Optional[bytes] = None, + ) -> bytes: + """Computes a hash value corresponding to the contents of a block and + the contents of the preceding block(s). The hash value is used for + prefix caching. + + NOTE: Content-based hashing does not yet support LoRA. + + Parameters: + - is_first_block (bool): A flag indicating if the block is the first in + the sequence. + - prev_block_hash (Optional[int]): The hash of the previous block. None + if this is the first block. + - cur_block_token_ids (List[int]): A list of token ids in the current + block. The current block is assumed to be full. + + Returns: + - bytes: The computed hash value for the block. + """ + assert (prev_block_hash is None) == is_first_block + digest = hashlib.sha256() + digest.update(b"vllm-prefix-cache-v2") + if is_first_block: + digest.update(cache_namespace or b"") + else: + assert prev_block_hash is not None + digest.update(prev_block_hash) + + if cur_block_token_ids: + digest.update(struct.pack( + f"!{len(cur_block_token_ids)}q", + *(int(token_id) for token_id in cur_block_token_ids))) + return digest.digest() + + +class ComputedBlocksTracker: + """Handles caching of per-sequence computed block ids. + When a sequence appears for the first time, it traverses all of the + blocks and detects the prefix of blocks that is computed. On the + subsequent times, it only traverses the new blocks that were added + and updates the already recorded prefix of blocks with the newly + computed blocks. + + To avoid redundant traversals, the algorithm also detects when there + is a "gap" in the computed prefix. For example, if we have blocks = + [1,2,3,4,5], and we have detected [1,2,3] as the computed prefix, then + we won't try to add more computed blocks to [1,2,3] in this sequence + iteration, and will add more computed blocks only after the sequence is + freed and reused again. + + Note that currently, for a given sequence, we also skip the last + block id for caching purposes, to avoid caching of a full sequence + """ + + def __init__(self, allocator): + self._allocator = allocator + self._cached_computed_seq_blocks: Dict[int, Tuple[List[int], + bool]] = {} + + def add_seq(self, seq_id: int) -> None: + """Start tracking seq_id + """ + assert seq_id not in self._cached_computed_seq_blocks + self._cached_computed_seq_blocks[seq_id] = ([], False) + + def remove_seq(self, seq_id: int) -> None: + """Stop tracking seq_id + """ + assert seq_id in self._cached_computed_seq_blocks + del self._cached_computed_seq_blocks[seq_id] + + def get_cached_computed_blocks_and_update( + self, seq_id: int, block_ids: List[int]) -> List[int]: + """ Look at the class documentation for details + """ + # Ensure seq_id is already tracked + assert seq_id in self._cached_computed_seq_blocks + + # Get cached data (may be empty on the first time) + prev_computed_block_ids, has_gap = self._cached_computed_seq_blocks[ + seq_id] + + if has_gap: + # When gap is detected, we do not add more computed blocks at this + # sequence iteration + return prev_computed_block_ids + + # We do not consider the last block id for caching purposes. + num_cur_blocks = len(block_ids) - 1 + assert num_cur_blocks >= 0 + + if len(prev_computed_block_ids) >= num_cur_blocks: + # Cache HIT + assert len(prev_computed_block_ids) == num_cur_blocks + return prev_computed_block_ids + + # If here, then we may possibly add more computed blocks. As a result, + # traverse the additional blocks after prev_computed_block_ids to + # detect more computed blocks and add them. + + # Incremental init for seq_id => Look only at the new blocks + computed_block_ids = self._allocator.get_computed_block_ids( # noqa: E501 + prev_computed_block_ids, + block_ids, + skip_last_block_id= + True, # We skip last block id to avoid caching of full seq + ) + + # Detect if there is a "gap" + has_gap = len(computed_block_ids) < num_cur_blocks + + # Record + self._cached_computed_seq_blocks[seq_id] = (computed_block_ids, + has_gap) + + return computed_block_ids + + +class LastAccessBlocksTracker: + """Manages the last access time of the tracked sequences, in order to allow + an efficient update of allocator's block last access times + """ + + def __init__(self, allocator): + self._allocator = allocator + self._seq_last_access: Dict[int, Optional[float]] = {} + + def add_seq(self, seq_id: int) -> None: + """Start tracking seq_id + """ + assert seq_id not in self._seq_last_access + self._seq_last_access[seq_id] = None + + def remove_seq(self, seq_id: int) -> None: + """Stop tracking seq_id + """ + assert seq_id in self._seq_last_access + del self._seq_last_access[seq_id] + + def update_last_access(self, seq_id: int, time: float) -> None: + assert seq_id in self._seq_last_access + self._seq_last_access[seq_id] = time + + def update_seq_blocks_last_access(self, seq_id: int, + block_ids: List[int]) -> None: + assert seq_id in self._seq_last_access + + ts = self._seq_last_access[seq_id] + + if ts is None: + # No last access was recorded, no need to update. + return + + self._allocator.mark_blocks_as_accessed(block_ids, ts) + + +def assert_prefix_caching_block_or_none(block: Optional[Block]): + if block is None: + return + assert isinstance(block, + PrefixCachingBlock), "Got block = {}".format(block) diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/block_manager_v2.py b/qwen3_6_scripts/vendor_overrides/vllm/core/block_manager_v2.py new file mode 100644 index 0000000..321a60d --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/block_manager_v2.py @@ -0,0 +1,769 @@ +"""A block manager that manages token blocks.""" +import hashlib +import os +import struct +from collections.abc import Mapping +from typing import Any, Dict, List, Optional, Sequence as GenericSequence, Tuple + +try: + from PIL import Image +except Exception: # pragma: no cover - optional dependency in some envs + Image = None # type: ignore +try: + import torch +except Exception: # pragma: no cover - optional dependency in some envs + torch = None # type: ignore + +from vllm.core.block.block_table import BlockTable +from vllm.core.block.cpu_gpu_block_allocator import CpuGpuBlockAllocator +from vllm.core.block.interfaces import Block +from vllm.core.block.prefix_caching_block import (ComputedBlocksTracker, + LastAccessBlocksTracker) +from vllm.core.block.utils import check_no_caching_or_swa_for_blockmgr_encdec +from vllm.core.interfaces import AllocStatus, BlockSpaceManager +from vllm.logger import init_logger +from vllm.sequence import Sequence, SequenceGroup, SequenceStatus +from vllm.utils import Device + +SeqId = int +EncoderSeqId = str + +logger = init_logger(__name__) + + +class BlockSpaceManagerV2(BlockSpaceManager): + """BlockSpaceManager which manages the allocation of KV cache. + + It owns responsibility for allocation, swapping, allocating memory for + autoregressively-generated tokens, and other advanced features such as + prefix caching, forking/copy-on-write, and sliding-window memory allocation. + + This class implements the design described in + https://github.com/vllm-project/vllm/pull/3492. + + Lookahead slots + The block manager has the notion of a "lookahead slot". These are slots + in the KV cache that are allocated for a sequence. Unlike the other + allocated slots, the content of these slots is undefined -- the worker + may use the memory allocations in any way. + + In practice, a worker could use these lookahead slots to run multiple + forward passes for a single scheduler invocation. Each successive + forward pass would write KV activations to the corresponding lookahead + slot. This allows low inter-token latency use-cases, where the overhead + of continuous batching scheduling is amortized over >1 generated tokens. + + Speculative decoding uses lookahead slots to store KV activations of + proposal tokens. + + See https://github.com/vllm-project/vllm/pull/3250 for more information + on lookahead scheduling. + + Args: + block_size (int): The size of each memory block. + num_gpu_blocks (int): The number of memory blocks allocated on GPU. + num_cpu_blocks (int): The number of memory blocks allocated on CPU. + watermark (float, optional): The threshold used for memory swapping. + Defaults to 0.01. + sliding_window (Optional[int], optional): The size of the sliding + window. Defaults to None. + enable_caching (bool, optional): Flag indicating whether caching is + enabled. Defaults to False. + """ + + def __init__( + self, + block_size: int, + num_gpu_blocks: int, + num_cpu_blocks: int, + watermark: float = 0.01, + sliding_window: Optional[int] = None, + enable_caching: bool = False, + ) -> None: + self.block_size = block_size + self.num_total_gpu_blocks = num_gpu_blocks + self.num_total_cpu_blocks = num_cpu_blocks + + self.sliding_window = sliding_window + # max_block_sliding_window is the max number of blocks that need to be + # allocated + self.max_block_sliding_window = None + if sliding_window is not None: + # +1 here because // rounds down + num_blocks = sliding_window // block_size + 1 + # +1 here because the last block may not be full, + # and so the sequence stretches one more block at the beginning + # For example, if sliding_window is 3 and block_size is 4, + # we may need 2 blocks when the second block only holds 1 token. + self.max_block_sliding_window = num_blocks + 1 + + self.watermark = watermark + assert watermark >= 0.0 + + self.enable_caching = enable_caching + + self.watermark_blocks = int(watermark * num_gpu_blocks) + + self.block_allocator = CpuGpuBlockAllocator.create( + allocator_type="prefix_caching" if enable_caching else "naive", + num_gpu_blocks=num_gpu_blocks, + num_cpu_blocks=num_cpu_blocks, + block_size=block_size, + ) + + self.block_tables: Dict[SeqId, BlockTable] = {} + self.cross_block_tables: Dict[EncoderSeqId, BlockTable] = {} + self._warned_mm_namespace_requests = set[str]() + self._request_local_namespace: Dict[str, bytes] = {} + self._runtime_cache_namespace = self._build_runtime_cache_namespace() + + self._computed_blocks_tracker = ComputedBlocksTracker( + self.block_allocator) + self._last_access_blocks_tracker = LastAccessBlocksTracker( + self.block_allocator) + + def can_allocate(self, + seq_group: SequenceGroup, + num_lookahead_slots: int = 0) -> AllocStatus: + # FIXME(woosuk): Here we assume that all sequences in the group share + # the same prompt. This may not be true for preempted sequences. + + check_no_caching_or_swa_for_blockmgr_encdec(self, seq_group) + + seq = seq_group.get_seqs(status=SequenceStatus.WAITING)[0] + num_required_blocks = BlockTable.get_num_required_blocks( + seq.get_token_ids(), + block_size=self.block_size, + num_lookahead_slots=num_lookahead_slots, + ) + + if seq_group.is_encoder_decoder(): + encoder_seq = seq_group.get_encoder_seq() + assert encoder_seq is not None + num_required_blocks += BlockTable.get_num_required_blocks( + encoder_seq.get_token_ids(), + block_size=self.block_size, + ) + + if self.max_block_sliding_window is not None: + num_required_blocks = min(num_required_blocks, + self.max_block_sliding_window) + + num_free_gpu_blocks = self.block_allocator.get_num_free_blocks( + device=Device.GPU) + + # Use watermark to avoid frequent cache eviction. + if (self.num_total_gpu_blocks - num_required_blocks < + self.watermark_blocks): + return AllocStatus.NEVER + if num_free_gpu_blocks - num_required_blocks >= self.watermark_blocks: + return AllocStatus.OK + else: + return AllocStatus.LATER + + def _allocate_sequence( + self, + seq: Sequence, + cache_namespace: Optional[bytes] = None, + ) -> BlockTable: + block_table = BlockTable( + block_size=self.block_size, + block_allocator=self.block_allocator, + max_block_sliding_window=self.max_block_sliding_window, + cache_namespace=cache_namespace, + ) + if seq.get_token_ids(): + # Add blocks to the block table only if the sequence is non empty. + block_table.allocate(seq.get_token_ids()) + + return block_table + + def allocate(self, seq_group: SequenceGroup) -> None: + + # Allocate self-attention block tables for decoder sequences + waiting_seqs = seq_group.get_seqs(status=SequenceStatus.WAITING) + assert not (set(seq.seq_id for seq in waiting_seqs) + & self.block_tables.keys()), "block table already exists" + + # NOTE: Here we assume that all sequences in the group have the same + # prompt. + seq = waiting_seqs[0] + request_id = seq_group.request_id + cache_namespace = self._get_cache_namespace( + seq, + request_id=request_id, + seq_group=seq_group, + ) + block_table: BlockTable = self._allocate_sequence( + seq, + cache_namespace=cache_namespace, + ) + self.block_tables[seq.seq_id] = block_table + + # Track seq + self._computed_blocks_tracker.add_seq(seq.seq_id) + self._last_access_blocks_tracker.add_seq(seq.seq_id) + + # Assign the block table for each sequence. + for seq in waiting_seqs[1:]: + self.block_tables[seq.seq_id] = block_table.fork() + + # Track seq + self._computed_blocks_tracker.add_seq(seq.seq_id) + self._last_access_blocks_tracker.add_seq(seq.seq_id) + + # Allocate cross-attention block table for encoder sequence + # + # NOTE: Here we assume that all sequences in the group have the same + # encoder prompt. + request_id = seq_group.request_id + + assert (request_id + not in self.cross_block_tables), \ + "block table already exists" + + check_no_caching_or_swa_for_blockmgr_encdec(self, seq_group) + + if seq_group.is_encoder_decoder(): + encoder_seq = seq_group.get_encoder_seq() + assert encoder_seq is not None + encoder_cache_namespace = self._get_cache_namespace( + encoder_seq, + request_id=request_id, + seq_group=seq_group) + block_table = self._allocate_sequence( + encoder_seq, cache_namespace=encoder_cache_namespace) + self.cross_block_tables[request_id] = block_table + + @staticmethod + def _has_multi_modal_payload(multi_modal_data: Any) -> bool: + if multi_modal_data is None: + return False + if isinstance(multi_modal_data, Mapping): + try: + return len(multi_modal_data) > 0 + except (TypeError, ValueError, RuntimeError, OSError, + OverflowError, AttributeError, LookupError, struct.error): + # Treat an unusual mapping as payload and let normalization + # either identify it or select request-local isolation. + return True + return True + + def _get_cache_namespace(self, seq: Sequence, request_id: str, + seq_group: SequenceGroup) -> bytes: + digest = hashlib.sha256() + digest.update(b"bi100-request-prefix-namespace-v1|") + digest.update(self._runtime_cache_namespace) + digest.update(self._adapter_cache_namespace(seq_group)) + + multi_modal_data = seq.multi_modal_data + if self._has_multi_modal_payload(multi_modal_data): + try: + mm_namespace = self._hash_multi_modal_namespace( + multi_modal_data) + except (TypeError, ValueError, RuntimeError, OSError, + OverflowError, AttributeError, LookupError, struct.error): + if request_id not in self._warned_mm_namespace_requests: + logger.warning( + "Request %s has multimodal input that cannot be " + "normalized for cache namespace hashing. Falling " + "back to " + "request-local namespace isolation.", + request_id, + ) + self._warned_mm_namespace_requests.add(request_id) + mm_namespace = self._request_local_fallback_cache_namespace( + request_id=request_id) + digest.update(b"mm|") + digest.update(mm_namespace) + else: + digest.update(b"text|") + return digest.digest() + + def can_append_slots(self, seq_group: SequenceGroup, + num_lookahead_slots: int) -> bool: + """Determine if there is enough space in the GPU KV cache to continue + generation of the specified sequence group. + + We use a worst-case heuristic: assume each touched block will require a + new allocation (either via CoW or new block). We can append slots if the + number of touched blocks is less than the number of free blocks. + + "Lookahead slots" are slots that are allocated in addition to the slots + for known tokens. The contents of the lookahead slots are not defined. + This is used by speculative decoding when speculating future tokens. + """ + + num_touched_blocks = 0 + for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING): + block_table = self.block_tables[seq.seq_id] + + num_touched_blocks += ( + block_table.get_num_blocks_touched_by_append_slots( + token_ids=block_table.get_unseen_token_ids( + seq.get_token_ids()), + num_lookahead_slots=num_lookahead_slots, + )) + + num_free_gpu_blocks = self.block_allocator.get_num_free_blocks( + Device.GPU) + return num_touched_blocks <= num_free_gpu_blocks + + def append_slots( + self, + seq: Sequence, + num_lookahead_slots: int, + ) -> List[Tuple[int, int]]: + + block_table = self.block_tables[seq.seq_id] + + block_table.append_token_ids( + token_ids=block_table.get_unseen_token_ids(seq.get_token_ids()), + num_lookahead_slots=num_lookahead_slots, + num_computed_slots=seq.data.get_num_computed_tokens(), + ) + # Return any new copy-on-writes. + new_cows = self.block_allocator.clear_copy_on_writes() + return new_cows + + def free(self, seq: Sequence) -> None: + seq_id = seq.seq_id + + if seq_id not in self.block_tables: + # Already freed or haven't been scheduled yet. + return + + # Update seq block ids with the latest access time + self._last_access_blocks_tracker.update_seq_blocks_last_access( + seq_id, self.block_tables[seq.seq_id].physical_block_ids) + + # Untrack seq + self._last_access_blocks_tracker.remove_seq(seq_id) + self._computed_blocks_tracker.remove_seq(seq_id) + + # Free table/blocks + self.block_tables[seq_id].free() + del self.block_tables[seq_id] + + def free_cross(self, seq_group: SequenceGroup) -> None: + request_id = seq_group.request_id + if request_id not in self.cross_block_tables: + # Already freed or hasn't been scheduled yet. + return + self.cross_block_tables[request_id].free() + del self.cross_block_tables[request_id] + + def get_block_table(self, seq: Sequence) -> List[int]: + block_ids = self.block_tables[seq.seq_id].physical_block_ids + return block_ids # type: ignore + + def get_cross_block_table(self, seq_group: SequenceGroup) -> List[int]: + request_id = seq_group.request_id + assert request_id in self.cross_block_tables + block_ids = self.cross_block_tables[request_id].physical_block_ids + assert all(b is not None for b in block_ids) + return block_ids # type: ignore + + def access_all_blocks_in_seq(self, seq: Sequence, now: float): + if self.enable_caching: + # Record the latest access time for the sequence. The actual update + # of the block ids is deferred to the sequence free(..) call, since + # only during freeing of block ids, the blocks are actually added to + # the evictor (which is when the most updated time is required) + # (This avoids expensive calls to mark_blocks_as_accessed(..)) + self._last_access_blocks_tracker.update_last_access( + seq.seq_id, now) + + def mark_blocks_as_computed(self, seq_group: SequenceGroup, + token_chunk_size: int): + # If prefix caching is enabled, mark immutable blocks as computed + # right after they have been scheduled (for prefill). This assumes + # the scheduler is synchronous so blocks are actually computed when + # scheduling the next batch. + self.block_allocator.mark_blocks_as_computed([]) + + def get_common_computed_block_ids( + self, seqs: List[Sequence]) -> GenericSequence[int]: + """Determine which blocks for which we skip prefill. + + With prefix caching we can skip prefill for previously-generated blocks. + Currently, the attention implementation only supports skipping cached + blocks if they are a contiguous prefix of cached blocks. + + This method determines which blocks can be safely skipped for all + sequences in the sequence group. + """ + computed_seq_block_ids = [] + for seq in seqs: + computed_seq_block_ids.append( + self._computed_blocks_tracker. + get_cached_computed_blocks_and_update( + seq.seq_id, + self.block_tables[seq.seq_id].physical_block_ids)) + + # NOTE(sang): This assumes seq_block_ids doesn't contain any None. + return self.block_allocator.get_common_computed_block_ids( + computed_seq_block_ids) # type: ignore + + def get_content_hashes(self, seq: Sequence) -> List[bytes]: + return self.block_tables[seq.seq_id].get_content_hashes() + + def get_and_reset_prefix_swaps( + self) -> Tuple[List[Tuple[int, int]], List[Tuple[int, int]]]: + """Return scheduler-owned (CPU->GPU, GPU->CPU) content transfers.""" + return self.block_allocator.get_and_reset_prefix_swaps() + + def begin_prefix_cache_step(self) -> None: + self.block_allocator.begin_prefix_cache_step() + + def _build_runtime_cache_namespace(self) -> bytes: + """Bind first-block hashes to the fixed model runtime identity.""" + model = os.getenv("BI100_PREFIX_MODEL_FINGERPRINT", + "Qwen3.6-35B-A3B") + dtype = os.getenv("BI100_PREFIX_DTYPE", "float16") + tp_raw = os.getenv("BI100_PREFIX_TP_SIZE", "4") + try: + tp_size = int(tp_raw) + except ValueError as exc: + raise RuntimeError( + "BI100_PREFIX_TP_SIZE must be a positive integer") from exc + if tp_size <= 0: + raise RuntimeError( + "BI100_PREFIX_TP_SIZE must be a positive integer") + + digest = hashlib.sha256() + digest.update(b"bi100-runtime-prefix-identity-v1|") + for label, value in ( + (b"model", model), + (b"dtype", dtype), + (b"tp", str(tp_size)), + (b"block_size", str(self.block_size))): + encoded = value.encode("utf-8") + digest.update(label) + digest.update(struct.pack("!Q", len(encoded))) + digest.update(encoded) + return digest.digest() + + @staticmethod + def _adapter_cache_namespace(seq_group: SequenceGroup) -> bytes: + digest = hashlib.sha256() + digest.update(b"bi100-adapter-prefix-identity-v1|") + lora = getattr(seq_group, "lora_request", None) + prompt_adapter = getattr(seq_group, "prompt_adapter_request", None) + identities = ( + ("lora", lora, ("lora_name", "lora_int_id", "lora_path", + "base_model_name")), + ("prompt", prompt_adapter, + ("prompt_adapter_name", "prompt_adapter_id", + "prompt_adapter_local_path", + "prompt_adapter_num_virtual_tokens")), + ) + for kind, adapter, fields in identities: + digest.update(kind.encode("ascii")) + if adapter is None: + digest.update(b"none|") + continue + for field in fields: + value = str(getattr(adapter, field, "")) + encoded = value.encode("utf-8") + digest.update(field.encode("ascii")) + digest.update(struct.pack("!Q", len(encoded))) + digest.update(encoded) + return digest.digest() + + def _request_local_fallback_cache_namespace(self, + request_id: str) -> bytes: + namespace = self._request_local_namespace.get(request_id) + if namespace is None: + digest = hashlib.sha256() + digest.update(b"multimodal-unsupported-request-local-v1|") + digest.update(self._runtime_cache_namespace) + digest.update(os.urandom(32)) + digest.update(request_id.encode("utf-8")) + namespace = digest.digest() + self._request_local_namespace[request_id] = namespace + return namespace + + def release_request_cache_namespace(self, request_id: str) -> None: + """Release request-local isolation state after request completion.""" + self._request_local_namespace.pop(request_id, None) + self._warned_mm_namespace_requests.discard(request_id) + + def _hash_multi_modal_namespace(self, mm_data: Any) -> bytes: + digest = hashlib.sha256() + self._hash_multi_modal_obj(digest, mm_data) + return digest.digest() + + @staticmethod + def _sort_map_keys(mm_map: Mapping[Any, Any]) -> List[Any]: + return sorted(mm_map.keys(), key=lambda key: repr(key)) + + @classmethod + def _hash_multi_modal_obj(cls, digest: Any, value: Any) -> None: + if value is None: + digest.update(b"none|") + return + if isinstance(value, Mapping): + digest.update(b"map|") + digest.update(struct.pack("!Q", len(value))) + for key in cls._sort_map_keys(value): + digest.update(b"k|") + cls._hash_multi_modal_obj(digest, key) + digest.update(b"v|") + cls._hash_multi_modal_obj(digest, value[key]) + return + if isinstance(value, list): + digest.update(b"list|") + digest.update(struct.pack("!Q", len(value))) + for item in value: + cls._hash_multi_modal_obj(digest, item) + return + if isinstance(value, tuple): + digest.update(b"tuple|") + digest.update(struct.pack("!Q", len(value))) + for item in value: + cls._hash_multi_modal_obj(digest, item) + return + if isinstance(value, str): + encoded = value.encode() + digest.update(b"str|") + digest.update(struct.pack("!Q", len(encoded))) + digest.update(encoded) + return + if isinstance(value, bytes): + digest.update(b"bytes|") + digest.update(struct.pack("!Q", len(value))) + digest.update(value) + return + if isinstance(value, bytearray): + cls._hash_multi_modal_obj(digest, bytes(value)) + return + if isinstance(value, bool): + digest.update(b"bool|") + digest.update(b"1" if value else b"0") + return + if isinstance(value, int): + digest.update(b"int|") + digest.update(str(value).encode()) + return + if isinstance(value, float): + digest.update(b"float|") + digest.update(struct.pack("!d", value)) + return + if torch is not None and isinstance(value, torch.Tensor): + digest.update(b"tensor|") + tensor = value.detach().cpu().contiguous() + digest.update(struct.pack("!Q", len(tensor.shape))) + for dim in tensor.shape: + digest.update(struct.pack("!Q", int(dim))) + digest.update(str(tensor.dtype).encode()) + # Byte views work for bfloat16 and other dtypes that NumPy cannot + # materialize directly. + tensor_bytes = tensor.view(torch.uint8).numpy().tobytes() + digest.update(struct.pack("!Q", len(tensor_bytes))) + digest.update(tensor_bytes) + return + if Image is not None and isinstance(value, Image.Image): + digest.update(b"image|") + digest.update(value.mode.encode()) + digest.update(struct.pack("!II", value.width, value.height)) + image_bytes = value.tobytes() + digest.update(struct.pack("!Q", len(image_bytes))) + digest.update(image_bytes) + palette = value.getpalette() + digest.update(b"palette-mode|") + cls._hash_multi_modal_obj( + digest, getattr(getattr(value, "palette", None), "mode", None)) + digest.update(b"palette|") + cls._hash_multi_modal_obj(digest, palette) + digest.update(b"transparency|") + cls._hash_multi_modal_obj( + digest, value.info.get("transparency")) + return + + raise TypeError(f"Unsupported multimodal namespace value type {type(value)}") + + + def fork(self, parent_seq: Sequence, child_seq: Sequence) -> None: + if parent_seq.seq_id not in self.block_tables: + # Parent sequence has either been freed or never existed. + return + src_block_table = self.block_tables[parent_seq.seq_id] + self.block_tables[child_seq.seq_id] = src_block_table.fork() + + # Track child seq + self._computed_blocks_tracker.add_seq(child_seq.seq_id) + self._last_access_blocks_tracker.add_seq(child_seq.seq_id) + + def can_swap_in(self, seq_group: SequenceGroup, + num_lookahead_slots: int) -> AllocStatus: + """Returns the AllocStatus for the given sequence_group + with num_lookahead_slots. + + Args: + sequence_group (SequenceGroup): The sequence group to swap in. + num_lookahead_slots (int): Number of lookahead slots used in + speculative decoding, default to 0. + + Returns: + AllocStatus: The AllocStatus for the given sequence group. + """ + if self.block_allocator.content_offload_enabled: + return AllocStatus.NEVER + return self._can_swap(seq_group, Device.GPU, SequenceStatus.SWAPPED, + num_lookahead_slots) + + def swap_in(self, seq_group: SequenceGroup) -> List[Tuple[int, int]]: + """Returns the block id mapping (from CPU to GPU) generated by + swapping in the given seq_group with num_lookahead_slots. + + Args: + seq_group (SequenceGroup): The sequence group to swap in. + + Returns: + List[Tuple[int, int]]: The mapping of swapping block from CPU + to GPU. + """ + physical_block_id_mapping = [] + for seq in seq_group.get_seqs(status=SequenceStatus.SWAPPED): + blocks = self.block_tables[seq.seq_id].blocks + if len(blocks) == 0: + continue + + seq_swap_mapping = self.block_allocator.swap(blocks=blocks, + src_device=Device.CPU, + dst_device=Device.GPU) + + # Refresh the block ids of the table (post-swap) + self.block_tables[seq.seq_id].update(blocks) + + seq_physical_block_id_mapping = { + self.block_allocator.get_physical_block_id( + Device.CPU, cpu_block_id): + self.block_allocator.get_physical_block_id( + Device.GPU, gpu_block_id) + for cpu_block_id, gpu_block_id in seq_swap_mapping.items() + } + + physical_block_id_mapping.extend( + list(seq_physical_block_id_mapping.items())) + + return physical_block_id_mapping + + def can_swap_out(self, seq_group: SequenceGroup) -> bool: + """Returns whether we can swap out the given sequence_group + with num_lookahead_slots. + + Args: + seq_group (SequenceGroup): The sequence group to swap in. + num_lookahead_slots (int): Number of lookahead slots used in + speculative decoding, default to 0. + + Returns: + bool: Whether it's possible to swap out current sequence group. + """ + if self.block_allocator.content_offload_enabled: + return False + alloc_status = self._can_swap(seq_group, Device.CPU, + SequenceStatus.RUNNING) + return alloc_status == AllocStatus.OK + + def swap_out(self, seq_group: SequenceGroup) -> List[Tuple[int, int]]: + """Returns the block id mapping (from GPU to CPU) generated by + swapping out the given sequence_group with num_lookahead_slots. + + Args: + sequence_group (SequenceGroup): The sequence group to swap in. + + Returns: + List[Tuple[int, int]]: The mapping of swapping block from + GPU to CPU. + """ + physical_block_id_mapping = [] + for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING): + blocks = self.block_tables[seq.seq_id].blocks + if len(blocks) == 0: + continue + + seq_swap_mapping = self.block_allocator.swap(blocks=blocks, + src_device=Device.GPU, + dst_device=Device.CPU) + + # Refresh the block ids of the table (post-swap) + self.block_tables[seq.seq_id].update(blocks) + + seq_physical_block_id_mapping = { + self.block_allocator.get_physical_block_id( + Device.GPU, gpu_block_id): + self.block_allocator.get_physical_block_id( + Device.CPU, cpu_block_id) + for gpu_block_id, cpu_block_id in seq_swap_mapping.items() + } + + physical_block_id_mapping.extend( + list(seq_physical_block_id_mapping.items())) + + return physical_block_id_mapping + + def get_num_free_gpu_blocks(self) -> int: + return self.block_allocator.get_num_free_blocks(Device.GPU) + + def get_num_free_cpu_blocks(self) -> int: + return self.block_allocator.get_num_free_blocks(Device.CPU) + + def get_prefix_cache_hit_rate(self, device: Device) -> float: + return self.block_allocator.get_prefix_cache_hit_rate(device) + + def _can_swap(self, + seq_group: SequenceGroup, + device: Device, + status: SequenceStatus, + num_lookahead_slots: int = 0) -> AllocStatus: + """Returns the AllocStatus for swapping in/out the given sequence_group + on to the 'device'. + + Args: + sequence_group (SequenceGroup): The sequence group to swap in. + device (Device): device to swap the 'seq_group' on. + status (SequenceStatus): The status of sequence which is needed + for action. RUNNING for swap out and SWAPPED for swap in + num_lookahead_slots (int): Number of lookahead slots used in + speculative decoding, default to 0. + + Returns: + AllocStatus: The AllocStatus for swapping in/out the given + sequence_group on to the 'device'. + """ + # First determine the number of blocks that will be touched by this + # swap. Then verify if there are available blocks in the device + # to perform the swap. + num_blocks_touched = 0 + blocks: List[Block] = [] + for seq in seq_group.get_seqs(status=status): + block_table = self.block_tables[seq.seq_id] + if block_table.blocks is not None: + # Compute the number blocks to touch for the tokens to be + # appended. This does NOT include the full blocks that need + # to be touched for the swap. + num_blocks_touched += \ + block_table.get_num_blocks_touched_by_append_slots( + block_table.get_unseen_token_ids(seq.get_token_ids()), + num_lookahead_slots=num_lookahead_slots) + blocks.extend(block_table.blocks) + # Compute the number of full blocks to touch and add it to the + # existing count of blocks to touch. + num_blocks_touched += self.block_allocator.get_num_full_blocks_touched( + blocks, device=device) + + watermark_blocks = 0 + if device == Device.GPU: + watermark_blocks = self.watermark_blocks + + if self.block_allocator.get_num_total_blocks( + device) < num_blocks_touched: + return AllocStatus.NEVER + elif self.block_allocator.get_num_free_blocks( + device) - num_blocks_touched >= watermark_blocks: + return AllocStatus.OK + else: + return AllocStatus.LATER diff --git a/qwen3_6_scripts/vendor_overrides/vllm/core/evictor_v2.py b/qwen3_6_scripts/vendor_overrides/vllm/core/evictor_v2.py new file mode 100644 index 0000000..393c842 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/core/evictor_v2.py @@ -0,0 +1,272 @@ +import enum +import heapq +import os +from abc import ABC, abstractmethod +from collections.abc import Mapping +from typing import Dict, List, OrderedDict, Tuple + + +ContentHash = bytes + + +class EvictionPolicy(enum.Enum): + """Enum for eviction policy used by make_evictor to instantiate the correct + Evictor subclass. + """ + LRU = enum.auto() + FREQUENCY_AWARE = enum.auto() + + +class Evictor(ABC): + """The Evictor subclasses should be used by the BlockAllocator class to + handle eviction of freed PhysicalTokenBlocks. + """ + + @abstractmethod + def __init__(self): + pass + + @abstractmethod + def __contains__(self, block_id: int) -> bool: + pass + + @abstractmethod + def evict(self) -> Tuple[int, ContentHash]: + """Runs the eviction algorithm and returns the evicted block's + content hash along with physical block id along with physical block id + """ + pass + + @abstractmethod + def add(self, block_id: int, content_hash: ContentHash, + num_hashed_tokens: int, + last_accessed: float): + """Adds block to the evictor, making it a candidate for eviction""" + pass + + @abstractmethod + def update(self, block_id: int, last_accessed: float): + """Update corresponding block's access time in metadata""" + pass + + @abstractmethod + def remove(self, block_id: int): + """Remove a given block id from the cache.""" + pass + + @property + @abstractmethod + def num_blocks(self) -> int: + pass + + +class BlockMetaData(): + """Data structure for storing key data describe cached block, so that + evitor could use to make its decision which one to choose for eviction + + Here we use physical block id as the dict key, as there maybe several + blocks with the same content hash, but their physical id is unique. + """ + + def __init__(self, content_hash: ContentHash, num_hashed_tokens: int, + last_accessed: float): + self.content_hash = content_hash + self.num_hashed_tokens = num_hashed_tokens + self.last_accessed = last_accessed + + +class LRUEvictor(Evictor): + """Evicts in a least-recently-used order using the last_accessed timestamp + that's recorded in the PhysicalTokenBlock. If there are multiple blocks with + the same last_accessed time, then the one with the largest num_hashed_tokens + will be evicted. If two blocks each have the lowest last_accessed time and + highest num_hashed_tokens value, then one will be chose arbitrarily + """ + + def __init__(self): + self.free_table: OrderedDict[int, BlockMetaData] = OrderedDict() + + def __contains__(self, block_id: int) -> bool: + return block_id in self.free_table + + def evict(self) -> Tuple[int, ContentHash]: + if len(self.free_table) == 0: + raise ValueError("No usable cache memory left") + + evicted_block, evicted_block_id = None, None + # The blocks with the lowest timestamps should be placed consecutively + # at the start of OrderedDict. Loop through all these blocks to + # find the one with maximum number of hashed tokens. + for _id, block in self.free_table.items(): + if evicted_block is None: + evicted_block, evicted_block_id = block, _id + continue + if evicted_block.last_accessed < block.last_accessed: + break + if evicted_block.num_hashed_tokens < block.num_hashed_tokens: + evicted_block, evicted_block_id = block, _id + + assert evicted_block is not None + assert evicted_block_id is not None + self.free_table.pop(evicted_block_id) + + return evicted_block_id, evicted_block.content_hash + + def add(self, block_id: int, content_hash: ContentHash, + num_hashed_tokens: int, + last_accessed: float): + self.free_table[block_id] = BlockMetaData(content_hash, + num_hashed_tokens, + last_accessed) + + def update(self, block_id: int, last_accessed: float): + self.free_table[block_id].last_accessed = last_accessed + + def remove(self, block_id: int): + if block_id not in self.free_table: + raise ValueError( + "Attempting to remove block that's not in the evictor") + self.free_table.pop(block_id) + + @property + def num_blocks(self) -> int: + return len(self.free_table) + + +class FrequencyAwareEvictor(Evictor): + """Evict the least frequently reused logical prefix content first. + + Content frequency survives physical block reuse. Heap entries carry a + generation and are lazily invalidated so eviction remains O(log N) without + allowing stale entries to grow without bound. + """ + + _COMPACTION_FACTOR = 2 + _COMPACTION_SLACK = 1 + + def __init__(self): + self.free_table: Dict[int, BlockMetaData] = {} + self.frequency_by_hash: Dict[ContentHash, int] = {} + self._heap: List[Tuple[int, float, int, int, int]] = [] + self._generations: Dict[int, int] = {} + self._next_generation = 0 + + @staticmethod + def _validate_content_hash(content_hash: ContentHash) -> None: + if not isinstance(content_hash, bytes) or len(content_hash) != 32: + raise ValueError( + "frequency-aware eviction requires a 32-byte content hash") + + def __contains__(self, block_id: int) -> bool: + return block_id in self.free_table + + def _heap_key(self, block_id: int, block: BlockMetaData, + generation: int) -> Tuple[int, float, int, int, int]: + return ( + self.frequency_by_hash[block.content_hash], + block.last_accessed, + -block.num_hashed_tokens, + block_id, + generation, + ) + + def _push(self, block_id: int) -> None: + self._next_generation += 1 + generation = self._next_generation + self._generations[block_id] = generation + heapq.heappush( + self._heap, + self._heap_key( + block_id, self.free_table[block_id], generation), + ) + + def _compact_if_needed(self) -> None: + limit = ( + self._COMPACTION_FACTOR * len(self.free_table) + + self._COMPACTION_SLACK + ) + if len(self._heap) <= limit: + return + self._heap = [ + self._heap_key(block_id, block, self._generations[block_id]) + for block_id, block in self.free_table.items() + ] + heapq.heapify(self._heap) + + def evict(self) -> Tuple[int, ContentHash]: + if not self.free_table: + raise ValueError("No usable cache memory left") + + while self._heap: + entry = heapq.heappop(self._heap) + frequency, _, _, block_id, generation = entry + block = self.free_table.get(block_id) + if ( + block is None + or self._generations.get(block_id) != generation + ): + continue + if self.frequency_by_hash[block.content_hash] != frequency: + heapq.heappush( + self._heap, + self._heap_key(block_id, block, generation), + ) + continue + + block = self.free_table.pop(block_id) + self._generations.pop(block_id) + self._compact_if_needed() + return block_id, block.content_hash + + raise RuntimeError("Evictor heap has no usable entry") + + def add(self, block_id: int, content_hash: ContentHash, + num_hashed_tokens: int, last_accessed: float): + self._validate_content_hash(content_hash) + self.frequency_by_hash[content_hash] = ( + self.frequency_by_hash.get(content_hash, 0) + 1) + self.free_table[block_id] = BlockMetaData( + content_hash, num_hashed_tokens, last_accessed) + self._push(block_id) + self._compact_if_needed() + + def update(self, block_id: int, last_accessed: float): + self.free_table[block_id].last_accessed = last_accessed + self._push(block_id) + self._compact_if_needed() + + def remove(self, block_id: int): + if block_id not in self.free_table: + raise ValueError( + "Attempting to remove block that's not in the evictor") + self.free_table.pop(block_id) + self._generations.pop(block_id) + self._compact_if_needed() + + @property + def num_blocks(self) -> int: + return len(self.free_table) + + +def eviction_policy_from_env( + environ: Mapping[str, str] | None = None, +) -> EvictionPolicy: + source = os.environ if environ is None else environ + value = source.get("BI100_KV_EVICTION_POLICY", "lru").strip().lower() + policies = { + "lru": EvictionPolicy.LRU, + "frequency": EvictionPolicy.FREQUENCY_AWARE, + } + if value not in policies: + raise ValueError( + "BI100_KV_EVICTION_POLICY must be one of: frequency, lru") + return policies[value] + + +def make_evictor(eviction_policy: EvictionPolicy) -> Evictor: + if eviction_policy == EvictionPolicy.LRU: + return LRUEvictor() + elif eviction_policy == EvictionPolicy.FREQUENCY_AWARE: + return FrequencyAwareEvictor() + else: + raise ValueError(f"Unknown cache eviction policy: {eviction_policy}") diff --git a/qwen3_6_scripts/vendor_overrides/vllm/model_executor/layers/sampler.py b/qwen3_6_scripts/vendor_overrides/vllm/model_executor/layers/sampler.py new file mode 100644 index 0000000..ef6db44 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/model_executor/layers/sampler.py @@ -0,0 +1,1340 @@ +"""A layer that samples the next tokens from the model's outputs.""" +import itertools +import warnings +from dataclasses import dataclass +from importlib.util import find_spec +from math import inf +from typing import Dict, List, Optional, Tuple, Union + +import msgspec +import torch +import torch.nn as nn + +import vllm.envs as envs +from vllm.model_executor.sampling_metadata import (SamplingMetadata, + SamplingTensors, + SequenceGroupToSample) +from vllm.sampling_params import SamplingType +from vllm.sequence import (VLLM_INVALID_TOKEN_ID, + CompletionSequenceGroupOutput, Logprob, + PromptLogprobs, SampleLogprobs, SequenceOutput) +from vllm.spec_decode.metrics import SpecDecodeWorkerMetrics + +if envs.VLLM_USE_FLASHINFER_SAMPLER and find_spec("flashinfer"): + import flashinfer.sampling + # yapf: disable + from flashinfer.sampling import ( + top_k_top_p_sampling_from_probs as flashinfer_top_k_top_p_sampling) + + # yapf: enable +else: + flashinfer_top_k_top_p_sampling = None + +# (num_token_ids, num_parent_ids) per sequence group. +SampleResultType = List[Tuple[List[int], List[int]]] + +# Types of temporary data structures used for +# computing sample_result +SampleMetadataType = Dict[SamplingType, Tuple[List[int], + List[SequenceGroupToSample]]] +MultinomialSamplesType = Dict[SamplingType, torch.Tensor] +SampleResultsDictType = Dict[int, Tuple[List[int], List[int]]] + + +# Encapsulates temporary data structures for computing +# sample_result. +# +# * For multi-step scheduling: must be returned +# by `Sampler.forward()` and used later to compute the pythonized +# sample_result +# +# * For single-step scheduling: consumed immediately +# inside `Sampler.forward()` to compute pythonized sample_result. +@dataclass +class SampleResultArgsType: + sample_metadata: SampleMetadataType + multinomial_samples: MultinomialSamplesType + sample_results_dict: SampleResultsDictType + sampling_metadata: SamplingMetadata + greedy_samples: Optional[torch.Tensor] + beam_search_logprobs: Optional[torch.Tensor] + + +# Union of non-deferred (single-step scheduling) +# vs deferred (multi-step scheduling) +# sample result types +MaybeDeferredSampleResultType = Union[SampleResultType, SampleResultArgsType] + +# Abbreviation of the _sample() return type +SampleReturnType = Tuple[MaybeDeferredSampleResultType, Optional[torch.Tensor]] + + +class SamplerOutput( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + array_like=True): # type: ignore[call-arg] + """For each sequence group, we generate a list of SequenceOutput object, + each of which contains one possible candidate for the next token. + + This data structure implements methods, so it can be used like a list, but + also has optional fields for device tensors. + """ + + outputs: List[CompletionSequenceGroupOutput] + + # On-device tensor containing probabilities of each token. + sampled_token_probs: Optional[torch.Tensor] = None + + # On-device tensor containing the logprobs of each token. + logprobs: Optional["torch.Tensor"] = None + + # Holds either (1) the pythonized sampler result (single-step scheduling) + # or (2) what will be arguments for later deferred pythonization of the + # sampler result (muliti-step scheduling) + deferred_sample_results_args: Optional[SampleResultArgsType] = None + + # On-device tensor containing the sampled token ids. + sampled_token_ids: Optional[torch.Tensor] = None + # CPU tensor containing the sampled token ids. Used during multi-step to + # return the sampled token ids from last rank to AsyncLLMEngine to be + # 'broadcasted' to all other PP ranks for next step. + sampled_token_ids_cpu: Optional[torch.Tensor] = None + + # Spec decode metrics populated by workers. + spec_decode_worker_metrics: Optional[SpecDecodeWorkerMetrics] = None + + # Optional last hidden states from the model. + hidden_states: Optional[torch.Tensor] = None + + # Optional prefill hidden states from the model + # (used for models like EAGLE). + prefill_hidden_states: Optional[torch.Tensor] = None + + # Time taken in the forward pass for this across all workers + model_forward_time: Optional[float] = None + + # Time taken in the model execute function. This will include model forward, + # block/sync across workers, cpu-gpu sync time and sampling time. + model_execute_time: Optional[float] = None + + def __getitem__(self, idx: int): + return self.outputs[idx] + + def __setitem__(self, idx: int, value): + self.outputs[idx] = value + + def __len__(self): + return len(self.outputs) + + def __eq__(self, other: object): + return isinstance(other, + self.__class__) and self.outputs == other.outputs + + def __repr__(self) -> str: + """Show the shape of a tensor instead of its values to reduce noise. + """ + sampled_token_probs_repr = ("None" if self.sampled_token_probs is None + else self.sampled_token_probs.shape) + sampled_token_ids_repr = ("None" if self.sampled_token_ids is None else + self.sampled_token_ids.shape) + return ( + f"SamplerOutput(outputs={self.outputs}, " + f"sampled_token_probs={sampled_token_probs_repr}, " + f"sampled_token_ids={sampled_token_ids_repr}, " + f"spec_decode_worker_metrics={self.spec_decode_worker_metrics})") + + +class Sampler(nn.Module): + """Samples the next tokens from the model's outputs. + + This layer does the following: + 1. Discard the hidden states that are not used for sampling (i.e., all + tokens except the final one in each prompt). + 2. Compute the logits for the next tokens. + 3. Apply presence, frequency and repetition penalties. + 4. Apply temperature scaling. + 5. Apply top-p and top-k truncation. + 6. Sample the next tokens. + Here, each sequence group within the batch can have different sampling + parameters (e.g., sampling method, temperature, top-p, top-k, etc.). + + The structure of the logits tensor is coupled with the seq_groups in + sampling_metadata. Typically, each sequence in each seq_group has one row in + logits for the next token to be sampled; however, for a seq_group with a + prompt request with the prompt_logprobs sampling parameter, there are rows + in logits for each token in the input prompt. + """ + + def __init__(self): + super().__init__() + + # Whether or not the SamplerOutput should have on-device tensors + # containing the sampled token ids and probabilities. This is used by + # speculative decoding. + self.include_gpu_probs_tensor = False + self.should_modify_greedy_probs_inplace = False + + def _init_sampling_tensors( + self, + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, + ): + """The goal here is to reuse sampling tensors between similar decode + runs. This is possible because sampling logic does not change between + decodes of the same sequences. + """ + _, vocab_size = logits.shape + + # First free any existing stored sampling tensors. + # This is necessary because some sampling tensors may + # have pinned memory. + self._sampling_tensors = None + + # Initialize new sampling tensors + (sampling_tensors, do_penalties, do_top_p_top_k, + do_min_p) = SamplingTensors.from_sampling_metadata( + sampling_metadata, vocab_size, logits.device, logits.dtype) + + self._sampling_tensors = sampling_tensors + self._do_penalties = do_penalties + self._do_top_p_top_k = do_top_p_top_k + self._do_min_p = do_min_p + + def forward( + self, + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, + ) -> Optional[SamplerOutput]: + """ + Single-step scheduling: + * Perform GPU-side sampling computation & compute + GPU-side logprobs tensor + * Pythonize sampling result & logprobs tensor + + Multi-step scheduling: + * Perform GPU-side sampling computation & compute + GPU-side logprobs tensor + * Defer Pythonization of sampling result & logprobs + tensor + * Encapsulate arguments required for deferred Pythonization + in the :class:`SamplerOutput` structure + + Args: + logits: (num_tokens, vocab_size). + sampling_metadata: Metadata for sampling. + """ + assert logits is not None + _, vocab_size = logits.shape + + # Prepare sampling tensors with pinned memory to avoid blocking. + if not sampling_metadata.reuse_sampling_tensors: + self._init_sampling_tensors(logits, sampling_metadata) + elif self._do_penalties: + # In this case, the sampling tensors logic depends on + # "output_tokens" of a sequence. As a result, we cannot + # reuse sampling tensors, since "output_tokens" changes + # between decode runs. + self._init_sampling_tensors(logits, sampling_metadata) + + assert self._sampling_tensors is not None + sampling_tensors = self._sampling_tensors + do_penalties = self._do_penalties + do_top_p_top_k = self._do_top_p_top_k + do_min_p = self._do_min_p + + logits = _apply_min_tokens_penalty(logits, sampling_metadata) + + # Apply presence and frequency penalties. + if do_penalties: + logits = _apply_penalties(logits, sampling_tensors.prompt_tokens, + sampling_tensors.output_tokens, + sampling_tensors.presence_penalties, + sampling_tensors.frequency_penalties, + sampling_tensors.repetition_penalties) + + # Use float32 to apply temperature scaling. + # Use in-place division to avoid creating a new tensor. + logits = logits.to(torch.float) + logits.div_(sampling_tensors.temperatures.unsqueeze(dim=1)) + + if do_top_p_top_k and flashinfer_top_k_top_p_sampling is None: + logits = _apply_top_k_top_p(logits, sampling_tensors.top_ps, + sampling_tensors.top_ks) + + if do_min_p: + logits = _apply_min_p(logits, sampling_tensors.min_ps) + + # We use float32 for probabilities and log probabilities. + # Compute the probabilities. + probs = torch.softmax(logits, dim=-1, dtype=torch.float) + # Compute the log probabilities. + logprobs = torch.log_softmax(logits, dim=-1, dtype=torch.float) + + # Sample the next tokens. + maybe_deferred_sample_results, maybe_sampled_tokens_tensor = _sample( + probs, + logprobs, + sampling_metadata, + sampling_tensors, + include_gpu_probs_tensor=self.include_gpu_probs_tensor, + modify_greedy_probs=self._should_modify_greedy_probs_inplace, + ) + + if self.include_gpu_probs_tensor: + # Since we will defer sampler result Pythonization, + # preserve GPU-side tensors in support of later + # deferred pythonization of logprobs + assert maybe_sampled_tokens_tensor is not None + on_device_tensors = (probs, logprobs, maybe_sampled_tokens_tensor) + else: + # Since Pythonization has already happened, don't preserve + # GPU-side tensors. + on_device_tensors = None + + # Get the logprobs query results. + prompt_logprobs = None + sample_logprobs = None + if not sampling_metadata.skip_sampler_cpu_output: + # Pythonize logprobs now (GPU -> CPU); do not defer. + assert not isinstance(maybe_deferred_sample_results, + SampleResultArgsType) + prompt_logprobs, sample_logprobs = get_logprobs( + logprobs, sampling_metadata, maybe_deferred_sample_results) + + return _build_sampler_output( + maybe_deferred_sample_results, + sampling_metadata, + prompt_logprobs, + sample_logprobs, + on_device_tensors=on_device_tensors, + skip_sampler_cpu_output=sampling_metadata.skip_sampler_cpu_output) + + @property + def _should_modify_greedy_probs_inplace(self) -> bool: + """Whether or not the sampler should modify the probability distribution + of greedily-sampled tokens such that multinomial sampling would sample + the greedily-sampled token. + + In other words, if True then we set the probability of the greedily- + sampled token to 1. + + This is used by speculative decoding, which requires that the sampling + method be encoded into the probability distribution. + """ + return self.should_modify_greedy_probs_inplace + + +def _get_bin_counts_and_mask( + tokens: torch.Tensor, + vocab_size: int, + num_seqs: int, +) -> Tuple[torch.Tensor, torch.Tensor]: + # Compute the bin counts for the tokens. + # vocab_size + 1 for padding. + bin_counts = torch.zeros((num_seqs, vocab_size + 1), + dtype=torch.long, + device=tokens.device) + bin_counts.scatter_add_(1, tokens, torch.ones_like(tokens)) + bin_counts = bin_counts[:, :vocab_size] + mask = bin_counts > 0 + + return bin_counts, mask + + +def _apply_min_tokens_penalty( + logits: torch.Tensor, + sampling_metadata: SamplingMetadata, +) -> torch.Tensor: + """Apply min_tokens penalty which sets stop tokens to -inf if min_tokens + have not been generated yet + """ + # list of indices in logits that will be set to -inf + logits_to_penalize: List[Tuple[int, int]] = [] + logits_applied = 0 + for seq_group in sampling_metadata.seq_groups: + seq_ids = seq_group.seq_ids + sampling_params = seq_group.sampling_params + + sample_indices = seq_group.sample_indices + logits_applied += len(sample_indices) + len( + seq_group.prompt_logprob_indices) + if not seq_group.do_sample: + continue + + start_idx = sample_indices[0] + min_tokens = sampling_params.min_tokens + token_ids_to_penalize = sampling_params.all_stop_token_ids + if min_tokens > 0 and token_ids_to_penalize: + seqs_to_penalize: List[int] = [] + for j, seq_id in enumerate(seq_ids): + seq_data = seq_group.seq_data[seq_id] + if len(seq_data.output_token_ids_array) < min_tokens: + seqs_to_penalize.append(j) + + if seqs_to_penalize: + # convert to the index into logits + seqs_to_penalize = [start_idx + j for j in seqs_to_penalize] + # itertools.product pairs each seq index with every token id + logits_to_penalize.extend( + itertools.product(seqs_to_penalize, token_ids_to_penalize)) + + if logits_to_penalize: + # use zip and * to group indices along each dimension + # eg. [ (1,2), (1,3), (5,6) ] -> ( (1,1,5), (2,3,6) ) + logits[tuple(zip(*logits_to_penalize))] = -float("inf") + + # verifies that no rows in logits were missed unexpectedly + assert logits_applied == logits.shape[0] + return logits + + +def _apply_penalties(logits: torch.Tensor, prompt_tokens_tensor: torch.Tensor, + output_tokens_tensor: torch.Tensor, + presence_penalties: torch.Tensor, + frequency_penalties: torch.Tensor, + repetition_penalties: torch.Tensor) -> torch.Tensor: + num_seqs, vocab_size = logits.shape + _, prompt_mask = _get_bin_counts_and_mask(prompt_tokens_tensor, vocab_size, + num_seqs) + output_bin_counts, output_mask = _get_bin_counts_and_mask( + output_tokens_tensor, vocab_size, num_seqs) + + repetition_penalties = repetition_penalties[:, None].repeat(1, vocab_size) + repetition_penalties[~(prompt_mask | output_mask)] = 1.0 + logits = torch.where(logits > 0, logits / repetition_penalties, + logits * repetition_penalties) + + # We follow the definition in OpenAI API. + # Refer to https://platform.openai.com/docs/api-reference/parameter-details + logits -= frequency_penalties.unsqueeze_(dim=1) * output_bin_counts + logits -= presence_penalties.unsqueeze_(dim=1) * output_mask + return logits + + +def _apply_top_k_top_p( + logits: torch.Tensor, + p: torch.Tensor, + k: torch.Tensor, +) -> torch.Tensor: + logits_sort, logits_idx = logits.sort(dim=-1, descending=False) + + # Apply top-k. + top_k_mask = logits_sort.size(1) - k.to(torch.long) + # Get all the top_k values. + top_k_mask = logits_sort.gather(1, top_k_mask.unsqueeze(dim=1)) + top_k_mask = logits_sort < top_k_mask + logits_sort.masked_fill_(top_k_mask, -float("inf")) + + # Apply top-p. + probs_sort = logits_sort.softmax(dim=-1) + probs_sum = probs_sort.cumsum(dim=-1) + top_p_mask = probs_sum <= 1 - p.unsqueeze(dim=1) + # at least one + top_p_mask[:, -1] = False + logits_sort.masked_fill_(top_p_mask, -float("inf")) + + # Re-sort the probabilities. + logits = torch.empty_like(logits_sort).scatter_(dim=-1, + index=logits_idx, + src=logits_sort) + return logits + + +def _apply_min_p( + logits: torch.Tensor, + min_p: torch.Tensor, +) -> torch.Tensor: + """ + Adapted from + https://github.com/oobabooga/text-generation-webui/blob/3146124ec01f02c8fb1650a6517cf1b60b537aaf/modules/sampler_hijack.py#L16C17-L16C17 + """ + probs = torch.softmax(logits, dim=-1) + top_probs, _ = probs.max(dim=-1, keepdim=True) + scaled_min_p = min_p.unsqueeze_(dim=1) * top_probs + tokens_to_remove = probs < scaled_min_p + logits = logits.masked_fill_(tokens_to_remove, -float("inf")) + + return logits + + +def _greedy_sample( + selected_seq_groups: List[SequenceGroupToSample], + samples: torch.Tensor, +) -> SampleResultType: + """Run greedy sampling on a given samples. + + Args: + selected_seq_groups: A list of sequence groups batched. + samples: (num_selected_samples,) A tensor of samples. The length of + samples could be smaller than selected_seq_groups if + seq_group.do_sample is False. + Returns: + Tuple of (next_token_ids, parent_ids). The length of returned list is + same as the length of selected_seq_groups. If the corresponding + seq_group has do_sample=False, tuple contains ([], []) + """ + samples_lst = samples.tolist() + sample_idx = 0 + results: SampleResultType = [] + for seq_group in selected_seq_groups: + if not seq_group.do_sample: + results.append(([], [])) + continue + + seq_ids = seq_group.seq_ids + num_parent_seqs = len(seq_ids) + assert num_parent_seqs == 1, ( + "Greedy sampling should have only one seq.") + parent_ids = list(range(num_parent_seqs)) + next_token_ids = [samples_lst[sample_idx]] + results.append((next_token_ids, parent_ids)) + sample_idx += num_parent_seqs + return results + + +def _random_sample( + selected_seq_groups: List[SequenceGroupToSample], + random_samples: torch.Tensor, +) -> SampleResultType: + """Run random sampling on a given samples. + + Args: + selected_seq_groups: A list of sequence groups batched. + random_samples: (num_selected_samples,) A tensor of samples. The + length of samples could be smaller than selected_seq_groups if + seq_group.do_sample is False. + Returns: + Tuple of (next_token_ids, parent_ids). The length of returned list is + same as the length of selected_seq_groups. If the corresponding + seq_group has do_sample=False, tuple contains ([], []) + """ + # Find the maximum n value of the prompt phase requests. + random_samples = random_samples.cpu() + sample_idx = 0 + results: SampleResultType = [] + for seq_group in selected_seq_groups: + if not seq_group.do_sample: + results.append(([], [])) + continue + + seq_ids = seq_group.seq_ids + sampling_params = seq_group.sampling_params + is_prompt = seq_group.is_prompt + num_parent_seqs = len(seq_ids) + if is_prompt: + # Prompt phase. + parent_ids = [0] * sampling_params.n + next_token_ids = random_samples[ + sample_idx, :sampling_params.n].tolist() + else: + # Generation phase. + parent_ids = list(range(num_parent_seqs)) + next_token_ids = random_samples[sample_idx:sample_idx + + num_parent_seqs, 0].tolist() + results.append((next_token_ids, parent_ids)) + sample_idx += num_parent_seqs + return results + + +def _beam_search_sample( + selected_seq_groups: List[SequenceGroupToSample], + logprobs: torch.Tensor, +) -> SampleResultType: + """Run beam sampling on a given samples. + + Args: + selected_seq_groups: A list of sequence groups batched. + logprobs: (num_selected_samples, vocab_size,) A tensor of logprob + on selected sample indices. + Returns: + Tuple of (next_token_ids, parent_ids). The length of returned list is + same as the length of selected_seq_groups. If the corresponding + seq_group has do_sample=False, tuple contains ([], []) + """ + # We sample 2 * beam_width candidates to make sure that with high + # probability we can get `beam_width` candidates in addition to + # the finished sequences for the next iteration. See + # https://github.com/tensorflow/tensor2tensor/blob/bafdc1b67730430d38d6ab802cbd51f9d053ba2e/tensor2tensor/utils/beam_search.py#L557-L563 + # for details. See also HF reference: + # https://github.com/huggingface/transformers/blob/a4dd53d88e4852f023332d284ff07a01afcd5681/src/transformers/generation/utils.py#L3063-L3065 + # + # NOTE: Beam search is not vectorized, so its speed can be slower than + # other sampling methods. + sample_idx = 0 + results: SampleResultType = [] + for seq_group in selected_seq_groups: + if not seq_group.do_sample: + results.append(([], [])) + continue + + is_prompt = seq_group.is_prompt + seq_ids, sampling_params = seq_group.seq_ids, seq_group.sampling_params + num_parent_seqs = len(seq_ids) + beam_width = sampling_params.n + seq_group_logprobs = logprobs[sample_idx:sample_idx + num_parent_seqs] + if is_prompt: + # Prompt phase. + assert num_parent_seqs == 1, ( + "Prompt input should have only one seq.") + parent_ids = [0] * (2 * beam_width) + _, next_token_ids = torch.topk(seq_group_logprobs[0], + 2 * beam_width) + next_token_ids = next_token_ids.tolist() + else: + # Generation phase. + cumulative_logprobs: List[float] = [ + seq_group.seq_data[seq_id].cumulative_logprob + for seq_id in seq_ids + ] + cumulative_logprobs_tensor = torch.tensor( + cumulative_logprobs, + dtype=torch.float, + device=seq_group_logprobs.device) + seq_group_logprobs = (seq_group_logprobs + + cumulative_logprobs_tensor.unsqueeze(dim=1)) + _, topk_ids = torch.topk(seq_group_logprobs.flatten(), + 2 * beam_width) + topk_ids = topk_ids.tolist() + vocab_size = seq_group_logprobs.size(-1) + parent_ids = [i // vocab_size for i in topk_ids] + next_token_ids = [i % vocab_size for i in topk_ids] + results.append((next_token_ids, parent_ids)) + sample_idx += num_parent_seqs + assert sample_idx == logprobs.size(0) + return results + + +# torch.multinomial forces a GPU<->CPU sync. +# Therefore, we use an optimized implementation instead. +# Note that we always sample with replacement. +# probs will be modified in place, but this is fine, as we pass +# in a copy already. +def _multinomial( + probs: torch.Tensor, + num_samples: int, + seq_groups: Optional[List[SequenceGroupToSample]] = None, +) -> torch.Tensor: + if num_samples > 1: + probs = probs.repeat_interleave(num_samples, dim=0) + q = torch.empty_like(probs) + if seq_groups is None: + q.exponential_() + else: + sample_idx = 0 + for seq_group in seq_groups: + seq_ids = seq_group.seq_ids + stride = len(seq_ids) * num_samples + assert seq_group.generator is not None + q[sample_idx:sample_idx + + stride].exponential_(generator=seq_group.generator) + sample_idx += stride + return probs.div_(q).argmax(dim=1).view(-1, num_samples) + + +def _top_k_top_p_multinomial_with_flashinfer( + probs: torch.Tensor, top_ks: torch.Tensor, top_ps: torch.Tensor, + num_samples: int, seq_groups: Optional[List[SequenceGroupToSample]]): + max_top_k_round = 32 + if num_samples > 1: + probs = probs.repeat_interleave(num_samples, dim=0) + top_ks = top_ks.repeat_interleave(num_samples) + top_ps = top_ps.repeat_interleave(num_samples) + batch_size = probs.shape[0] + uniform_samples = torch.empty((max_top_k_round, batch_size), + device=probs.device) + if seq_groups is None: + uniform_samples.uniform_() + else: + sample_idx = 0 + for seq_group in seq_groups: + seq_ids = seq_group.seq_ids + stride = len(seq_ids) * num_samples + assert seq_group.generator is not None + uniform_samples[:, sample_idx:sample_idx + + stride].uniform_(generator=seq_group.generator) + sample_idx += stride + batch_next_token_ids, success = flashinfer_top_k_top_p_sampling( + probs, + uniform_samples, + top_ks, + top_ps, + ) + if not success.all(): + warnings.warn("FlashInfer rejection sampling failed, fallback.", + stacklevel=1) + probs = flashinfer.sampling.top_k_renorm_prob(probs, top_ks) + probs = flashinfer.sampling.top_p_renorm_prob(probs, top_ps) + batch_next_token_ids = flashinfer.sampling.sampling_from_probs( + probs, uniform_samples[0]) + return batch_next_token_ids.view(-1, num_samples) + + +def get_pythonized_sample_results( + sample_result_args: SampleResultArgsType) -> SampleResultType: + '''This function consumes GPU-side sampler results and computes + Pythonized CPU-side sampler results (GPU -> CPU sync.) + + Single-step scheduling: this function is invoked at sampling-time + for immediate Pythonization. + + Multi-step scheduling: Pythonization is deferred until after multiple + GPU-side steps have been completed. + + Args: + sample_result_args: GPU-side inputs to the Pythonization process + + Returns: + Pythonized sampler results + ''' + + ( + sample_metadata, + sampling_metadata, + greedy_samples, + multinomial_samples, + beam_search_logprobs, + sample_results_dict, + ) = ( + sample_result_args.sample_metadata, + sample_result_args.sampling_metadata, + sample_result_args.greedy_samples, + sample_result_args.multinomial_samples, + sample_result_args.beam_search_logprobs, + sample_result_args.sample_results_dict, + ) + + for sampling_type in SamplingType: + if sampling_type not in sample_metadata: + continue + (seq_group_id, seq_groups) = sample_metadata[sampling_type] + if sampling_type == SamplingType.GREEDY: + sample_results = _greedy_sample(seq_groups, greedy_samples) + elif sampling_type in (SamplingType.RANDOM, SamplingType.RANDOM_SEED): + sample_results = _random_sample(seq_groups, + multinomial_samples[sampling_type]) + elif sampling_type == SamplingType.BEAM: + sample_results = _beam_search_sample(seq_groups, + beam_search_logprobs) + sample_results_dict.update(zip(seq_group_id, sample_results)) + + return [ + sample_results_dict.get(i, ([], [])) + for i in range(len(sampling_metadata.seq_groups)) + ] + + +def _sample_with_torch( + probs: torch.Tensor, + logprobs: torch.Tensor, + sampling_metadata: SamplingMetadata, + sampling_tensors: SamplingTensors, + include_gpu_probs_tensor: bool, + modify_greedy_probs: bool, +) -> SampleReturnType: + '''Torch-oriented _sample() implementation. + + Single-step scheduling: + * Perform GPU-side sampling computation + * Immediately Pythonize sampling result + + Multi-step scheduling: + * Perform GPU-side sampling computation + * Defer Pythonization & preserve GPU-side + tensors required for Pythonization + ''' + + categorized_seq_group_ids: Dict[SamplingType, + List[int]] = {t: [] + for t in SamplingType} + categorized_sample_indices = sampling_metadata.categorized_sample_indices + for i, seq_group in enumerate(sampling_metadata.seq_groups): + sampling_params = seq_group.sampling_params + sampling_type = sampling_params.sampling_type + categorized_seq_group_ids[sampling_type].append(i) + + sample_results_dict: SampleResultsDictType = {} + sample_metadata: SampleMetadataType = {} + multinomial_samples: MultinomialSamplesType = {} + greedy_samples: Optional[torch.Tensor] = None + beam_search_logprobs: Optional[torch.Tensor] = None + + # Create output tensor for sampled token ids. + if include_gpu_probs_tensor: + sampled_token_ids_tensor = torch.full((logprobs.shape[0], 1), + VLLM_INVALID_TOKEN_ID, + dtype=torch.long, + device=logprobs.device) + else: + sampled_token_ids_tensor = None + + # Counterintiutively, having two loops here is actually faster. + # The first loop can run without waiting on GPU<->CPU sync. + for sampling_type in SamplingType: + sample_indices = categorized_sample_indices[sampling_type] + num_tokens = len(sample_indices) + if num_tokens == 0: + continue + + seq_group_id = categorized_seq_group_ids[sampling_type] + seq_groups = [sampling_metadata.seq_groups[i] for i in seq_group_id] + sample_metadata[sampling_type] = (seq_group_id, seq_groups) + long_sample_indices = sample_indices.long() + if sampling_type == SamplingType.GREEDY: + greedy_samples = torch.argmax(logprobs[long_sample_indices], + dim=-1) + + if sampled_token_ids_tensor is not None: + # Store sampled tokens in output tensor. + sampled_token_ids_tensor[ + long_sample_indices] = greedy_samples.unsqueeze(-1) + + if modify_greedy_probs: + # If required, modify the probabilities such that sampling from + # the modified distribution would always sample the argmax + # token id. + _modify_greedy_probs_inplace(logprobs, probs, + long_sample_indices, + greedy_samples) + + elif sampling_type in (SamplingType.RANDOM, SamplingType.RANDOM_SEED): + max_n_in_batch = 1 + for seq_group in seq_groups: + if seq_group.is_prompt: + sampling_params = seq_group.sampling_params + max_n_in_batch = max(max_n_in_batch, sampling_params.n) + seq_groups_arg = (None if sampling_type == SamplingType.RANDOM else + seq_groups) + + if flashinfer_top_k_top_p_sampling is not None: + multinomial_samples[ + sampling_type] = _top_k_top_p_multinomial_with_flashinfer( + probs[long_sample_indices], + sampling_tensors.top_ks[long_sample_indices], + sampling_tensors.top_ps[long_sample_indices], + max_n_in_batch, + seq_groups_arg, + ) + else: + multinomial_samples[sampling_type] = _multinomial( + probs[long_sample_indices], + max_n_in_batch, + seq_groups=seq_groups_arg) + + if sampled_token_ids_tensor is not None: + # Store sampled tokens in output tensor. + sampled_token_ids_tensor[long_sample_indices] = \ + multinomial_samples[sampling_type].to(torch.long) + + elif sampling_type == SamplingType.BEAM: + beam_search_logprobs = logprobs[sample_indices] + else: + raise ValueError(f"Unsupported sampling type: {sampling_type}") + + # Encapsulate arguments for computing Pythonized sampler + # results, whether deferred or otherwise. + maybe_deferred_args = SampleResultArgsType( + sampling_metadata=sampling_metadata, + sample_metadata=sample_metadata, + multinomial_samples=multinomial_samples, + greedy_samples=greedy_samples, + beam_search_logprobs=beam_search_logprobs, + sample_results_dict=sample_results_dict) + + if not sampling_metadata.skip_sampler_cpu_output: + # GPU<->CPU sync happens here. + # This also converts the sampler output to a Python object. + # Return Pythonized sampler result & sampled token ids + return get_pythonized_sample_results( + maybe_deferred_args), sampled_token_ids_tensor + else: + # Defer sampler result Pythonization; return deferred + # Pythonization args & sampled token ids + return ( + maybe_deferred_args, + sampled_token_ids_tensor, + ) + + +def _sample( + probs: torch.Tensor, + logprobs: torch.Tensor, + sampling_metadata: SamplingMetadata, + sampling_tensors: SamplingTensors, + include_gpu_probs_tensor: bool, + modify_greedy_probs: bool, +) -> SampleReturnType: + """ + Args: + probs: (num_query_tokens_in_batch, num_vocab) + logprobs: (num_query_tokens_in_batch, num_vocab) + sampling_metadata: The metadata for a batch for sampling. + sampling_tensors: Tensors that include sampling related metadata. + + Returns: + (next_token_ids, parent_seq_ids) for each seq group in a batch. + If sampling is skipped, it returns ([], []) + sampled_token_ids_tensor: A tensor of sampled token ids. + """ + return _sample_with_torch( + probs, + logprobs, + sampling_metadata, + sampling_tensors, + include_gpu_probs_tensor=include_gpu_probs_tensor, + modify_greedy_probs=modify_greedy_probs, + ) + + +def _get_ranks(x: torch.Tensor, indices: torch.Tensor) -> torch.Tensor: + """ + This function calculates the ranks of the chosen tokens in a logprob tensor. + + Args: + x (torch.Tensor): 2D logprob tensor of shape (N, M) + where N is the no. of tokens and M is the vocab dim. + indices (torch.Tensor): List of chosen token indices. + + Returns: + torch.Tensor: 1D tensor of shape (N,) where N is the no. of tokens. + Each element in the returned tensor represents the rank + of the chosen token in the input logprob tensor. + """ + vals = x[torch.arange(0, len(x), device=x.device, dtype=indices.dtype), + indices] + result = (x > vals[:, None]) + del vals + return result.sum(1).add_(1) + + +def get_logprobs( + logprobs: torch.Tensor, + sampling_metadata: SamplingMetadata, + sample_results: SampleResultType, +) -> Tuple[List[Optional[PromptLogprobs]], List[SampleLogprobs]]: + """Return sample logprobs and prompt logprobs. + + The logic consists of 3 parts. + - Select indices to compute logprob from, ranks of token ids, and + the top k token ids from logprobs. + - Compute prompt logprobs if required. + - Compute sample logprobs if required. + + Args: + logprobs: (num_query_tokens_across_batch, num_vocab). Each query token's + logprob per vocab. Sequence groups' query tokens are batched in a + single flattened tensor. For example, assuming there are N + seq groups, it is sorted by prefill tokens for seq_group_1 (if + prompt logprob is enabled), decode tokens for seq_group_1 (if + sampling is required), prefill tokens for seq_group_2, ... + sampling_metadata: The sampling metadata. + sample_results: (num_seq_groups) The tuple of (next_token_ids, + parent_ids) for each sequence group. When beam search is enabled, + sample_results can contain different number of seq_ids from + sampling_metadata.seq_groups. It is because beam search creates + 2 * BEAM_WIDTH number of samples (whereas there are only up to + BEAM_WIDTH number of seq_ids). + + Returns: + A tuple of prompt and sample logprobs per sequence group in a batch. + """ + # The index of query token to calculate logprobs. It includes both + # prompt and sample logprob indices. + query_indices: List[int] = [] + # The next token ids to get the logprob value from. + next_token_ids: List[int] = [] + # The largest requested number of logprobs. We find logprobs as many as the + # largest num logprobs in this API. If every logprobs is None, it will be + # set to -1. + largest_num_logprobs = -1 + + # Select indices to compute logprob from, ranks of token ids, and the top + # k token ids from logprobs. + for (seq_group, sample_result) in zip(sampling_metadata.seq_groups, + sample_results): + sampling_params = seq_group.sampling_params + + # Update indices and tokens for prompt logprobs. + if (seq_group.is_prompt + and sampling_params.prompt_logprobs is not None): + largest_num_logprobs = max(largest_num_logprobs, + sampling_params.prompt_logprobs) + next_prompt_tokens = _get_next_prompt_tokens(seq_group) + query_indices.extend(seq_group.prompt_logprob_indices) + next_token_ids.extend(next_prompt_tokens) + + # Update indices and next tokenes for sample logprob. + if seq_group.do_sample: + token_ids, parent_seq_ids = sample_result + # NOTE: We cannot directly use sample_indices because + # sample_indices only contain parent seq_ids of a previous step. + # The current step may have different number of seq_ids, and + # we can obtain it from `sample_result[1]`. + query_idx = seq_group.sample_indices[0] + query_indices.extend( + [query_idx + parent_id for parent_id in parent_seq_ids]) + next_token_ids.extend(token_ids) + + if sampling_params.logprobs is not None: + largest_num_logprobs = max(largest_num_logprobs, + sampling_params.logprobs) + + assert len(next_token_ids) == len(query_indices) + + selected_logprobs, ranks = None, None + top_logprobs, top_token_ids = None, None + + # If largest_num_logprobs == -1, i.e. no logprobs are requested, we can + # skip the whole logprob calculation. + if query_indices and largest_num_logprobs >= 0: + query_indices_gpu = torch.tensor(query_indices, device=logprobs.device) + next_token_ids_gpu = torch.tensor(next_token_ids, + device=logprobs.device) + + # (num_selected_query_tokens, num_logprobs). Note that query_indices can + # contain duplicates if beam search is enabled. + selected_logprobs = logprobs[[ + query_indices_gpu, + next_token_ids_gpu, + ]] + ranks = _get_ranks( + logprobs[query_indices_gpu], + next_token_ids_gpu, + ) + assert selected_logprobs.shape[0] == ranks.shape[0] + + # We need to compute top k only if there exists logprobs > 0. + if largest_num_logprobs > 0: + # Logprobs of topk tokens for a batch of sequence groups. + # (num_query_tokens_across_batch). + top_logprobs, top_token_ids = torch.topk(logprobs, + largest_num_logprobs, + dim=-1) + top_logprobs = top_logprobs.to('cpu') + top_token_ids = top_token_ids.to('cpu') + + selected_logprobs = selected_logprobs.to('cpu') + ranks = ranks.to('cpu') + + # Find prompt/sample logprobs. + prompt_logprobs_per_seq_group: List[Optional[PromptLogprobs]] = [] + sample_logprobs_per_seq_group: List[SampleLogprobs] = [] + top_logprob_idx = 0 + selected_logprobs_idx = 0 + + for seq_group, sample_result in zip(sampling_metadata.seq_groups, + sample_results): + (prompt_logprobs, top_logprob_idx, + selected_logprobs_idx) = _get_prompt_logprob_if_needed( + seq_group, selected_logprobs, ranks, top_token_ids, top_logprobs, + selected_logprobs_idx, top_logprob_idx) + prompt_logprobs_per_seq_group.append(prompt_logprobs) + + (sampled_logprobs, top_logprob_idx, + selected_logprobs_idx) = _get_sampled_logprob_if_needed( + seq_group, sample_result, selected_logprobs, ranks, top_token_ids, + top_logprobs, selected_logprobs_idx, top_logprob_idx) + sample_logprobs_per_seq_group.append(sampled_logprobs) + + return prompt_logprobs_per_seq_group, sample_logprobs_per_seq_group + + +def _get_prompt_logprob_if_needed( + seq_group: SequenceGroupToSample, + selected_logprobs: torch.Tensor, + ranks: torch.Tensor, + top_token_ids: torch.Tensor, + top_logprobs: torch.Tensor, + selected_logprobs_idx: int, + top_logprob_idx: int, +): + """Compute the prompt logprob from a sequence group if needed.""" + sampling_params = seq_group.sampling_params + is_prompt = seq_group.is_prompt + + # Find prompt logprobs + prompt_logprobs: Optional[PromptLogprobs] = None + if is_prompt and sampling_params.prompt_logprobs is not None: + query_len = seq_group.query_len + assert query_len is not None + requested_prompt_logprob_len = ( + query_len - len(seq_group.seq_ids) + if seq_group.do_sample else query_len) + seq_data = seq_group.seq_data[seq_group.seq_ids[0]] + available_next_tokens = max( + 0, + len(seq_data.prompt_token_ids) + - seq_data.get_num_computed_tokens() + - 1, + ) + full_prompt_logprob_len = min( + requested_prompt_logprob_len, + available_next_tokens, + ) + prompt_logprobs = [None] * full_prompt_logprob_len + num_logprobs = sampling_params.prompt_logprobs + next_prompt_tokens = _get_next_prompt_tokens(seq_group) + assert (len(next_prompt_tokens) + == len(seq_group.prompt_logprob_indices) + == len(seq_group.prompt_logprob_output_indices)) + # Pre-select indexes and create a list. It is faster than calling .item + # repetitively. + if next_prompt_tokens: + assert selected_logprobs is not None + assert ranks is not None + selected_logprob_items = selected_logprobs[ + selected_logprobs_idx:selected_logprobs_idx + + len(next_prompt_tokens)].tolist() + rank_items = ranks[ + selected_logprobs_idx:selected_logprobs_idx + + len(next_prompt_tokens)].tolist() + else: + selected_logprob_items = [] + rank_items = [] + for idx, (token_id, output_index) in enumerate(zip( + next_prompt_tokens, + seq_group.prompt_logprob_output_indices)): + # Calculate the prompt logprob of the real prompt tokens. + # {token_id: (logprob, rank_from_vocab)} + prompt_logprobs_dict: Dict[int, Tuple[float, int]] = { + token_id: (selected_logprob_items[idx], rank_items[idx]) + } + + # Add top K prompt logprobs along with its rank. + if num_logprobs > 0: + assert top_token_ids is not None + assert top_logprobs is not None + top_ids = top_token_ids[ + top_logprob_idx, :num_logprobs].tolist() + top_probs = top_logprobs[ + top_logprob_idx, :num_logprobs].tolist() + # Top K is already sorted by rank, so we can use 1 ~ + # num_logprobs + 1 for rank. + top_ranks = range(1, num_logprobs + 1) + prompt_logprobs_dict.update({ + top_id: (top_prob, rank) + for top_id, top_prob, rank in zip(top_ids, top_probs, + top_ranks) + }) + prompt_logprobs[output_index] = { + token_id: Logprob(*logprob_and_rank) + for token_id, logprob_and_rank in prompt_logprobs_dict.items() + } + # + 1 to go to the next prompt token. + top_logprob_idx += 1 + + # + len(next_prompt_tokens) to go to the next prompt. + selected_logprobs_idx += len(next_prompt_tokens) + return prompt_logprobs, top_logprob_idx, selected_logprobs_idx + + +def _get_sampled_logprob_if_needed( + seq_group: SequenceGroupToSample, + sample_result: Tuple[List[int], List[int]], + selected_logprobs: torch.Tensor, + ranks: torch.Tensor, + top_token_ids: torch.Tensor, + top_logprobs: torch.Tensor, + selected_logprobs_idx: int, + top_logprob_idx: int, +): + """Compute the sample logprob if needed.""" + seq_ids = seq_group.seq_ids + num_logprobs = seq_group.sampling_params.logprobs + sampled_logprobs: SampleLogprobs = [] + next_token_ids, parent_seq_ids = sample_result + + if seq_group.do_sample: + assert len(next_token_ids) > 0 + if num_logprobs is None: + for next_token_id in next_token_ids: + # Use a dummy logprob + sampled_logprobs.append({next_token_id: Logprob(inf)}) + else: + # Pre-select items from tensor. tolist() is faster than repetitive + # `.item()` calls. + selected_logprob_items = selected_logprobs[ + selected_logprobs_idx:selected_logprobs_idx + + len(next_token_ids)].tolist() + rank_items = ranks[selected_logprobs_idx:selected_logprobs_idx + + len(next_token_ids)].tolist() + for idx, (next_token_id, parent_id) in enumerate( + zip(next_token_ids, parent_seq_ids)): + # Get the logprob of a sampled token. + sampled_logprobs_dict = { + next_token_id: + (selected_logprob_items[idx], rank_items[idx]) + } + if num_logprobs is not None and num_logprobs > 0: + # Get top K logprobs. + top_ids = top_token_ids[top_logprob_idx + + parent_id, :num_logprobs].tolist() + top_probs = top_logprobs[ + top_logprob_idx + parent_id, :num_logprobs].tolist() + # Top K is already sorted by rank, so we can use 1 ~ + # num_logprobs + 1 for rank. + top_ranks = range(1, num_logprobs + 1) + sampled_logprobs_dict.update({ + top_id: (top_prob, rank) + for top_id, top_prob, rank in zip( + top_ids, top_probs, top_ranks) + }) + + sampled_logprobs.append({ + token_id: Logprob(*logprob_and_rank) + for token_id, logprob_and_rank in + sampled_logprobs_dict.items() + }) + + # NOTE: This part of code is not intuitive. `selected_logprobs` include + # logprobs for the current step, which has len(next_token_ids) tokens + # per sequence group. `logprobs` includes logprobs from the previous + # steps, which has len(seq_ids) tokens per sequence group. + + # Iterate to the next sequence group in a batch. + selected_logprobs_idx += len(next_token_ids) + # Iterate to the next sequence group in a batch. + top_logprob_idx += len(seq_ids) + return sampled_logprobs, top_logprob_idx, selected_logprobs_idx + + +def _modify_greedy_probs_inplace(logprobs: torch.Tensor, probs: torch.Tensor, + sample_indices: torch.Tensor, + greedy_samples: torch.Tensor) -> None: + """Modify the probability distributions of the greedily-sampled tokens such + that each sampled token has a "probability" of 1.0. This is required by + speculative decoding, which depends on the sampling method being encoded + within the probability distribution for correctness. + + # Why do we only need to do this for greedy sampling? + + vLLM's sampler performs the following steps for greedy or multinomial + (random) sampling: + 1. Get logits from model. + 2. Modify logits according to per-sequence sampling parameters. + - Multiply by temperature, top-k and top-p masking, penalize tokens + according to their frequency, etc. + 3. Sample a token. + - Random sampling simply samples from the modified probability + distribution. + - Greedy sampling performs `argmax` to obtain the token with the + highest likelihood. + + Ignoring greedy sampling for a moment, we find that the computed probability + distribution has the following property: we can sample from it independently + and find that the token sampled by the Sampler has a frequency corresponding + to how often we see it in our sampling. In other words, for tokens sampled + with vLLM's random SamplingType, the computed probability distribution + encodes the sampling methodology completely. + + Greedy sampling does not normally have this property. vLLM modifies logits + according to sampling params, then performs `argmax`, then returns the + sampled token and the computed probability distribution. If we sample from + the distribution, we'll find the likelihood of the greedily-sampled token + is not always 1.0. + + Since lossless speculative decoding requires that the sampling methodology + be encoded within the probability distribution, we are motivated to modify + the probability distribution such that the sampled token has probability 1 + when speculative decoding is used. + + NOTE: Alternatively, we could use an extremely low temperature to achieve + greedy sampling using multinomial computation and unite the codepaths. This + has implications on the overall design of the sampler, e.g. how to record + accurate logprobs for the user, so this improvement is deferred to later. + """ + # NOTE: logprobs are not modified so they can be returned to the user. + probs[sample_indices, :] = 0 + probs[sample_indices, greedy_samples] = 1.0 + + +def _build_sampler_output( + maybe_deferred_sample_results: MaybeDeferredSampleResultType, + sampling_metadata: SamplingMetadata, + prompt_logprobs: Optional[List[Optional[PromptLogprobs]]], + sample_logprobs: Optional[List[SampleLogprobs]], + on_device_tensors: Optional[Tuple[torch.Tensor, torch.Tensor, + torch.Tensor]], + skip_sampler_cpu_output: bool = False, +) -> SamplerOutput: + """Construct Python objects with the output of sampling. + + Args: + on_device_tensors: Tuple containing on-device tensors with the + probabilities used in sampling and the sampled token ids. This + allows post-processing without copies to CPU/serialization, e.g. in + speculative decoding rejection sampling. + """ + sampler_output: List[CompletionSequenceGroupOutput] = [] + + if skip_sampler_cpu_output: + assert isinstance(maybe_deferred_sample_results, SampleResultArgsType) + deferred_sample_results_args = maybe_deferred_sample_results + else: + assert prompt_logprobs is not None + assert sample_logprobs is not None + assert not isinstance(maybe_deferred_sample_results, + SampleResultArgsType) + deferred_sample_results_args = None + + for (seq_group, sample_result, group_prompt_logprobs, + group_sample_logprobs) in zip(sampling_metadata.seq_groups, + maybe_deferred_sample_results, + prompt_logprobs, sample_logprobs): + seq_ids = seq_group.seq_ids + next_token_ids, parent_ids = sample_result + seq_outputs: List[SequenceOutput] = [] + for parent_id, next_token_id, logprobs in zip( + parent_ids, next_token_ids, group_sample_logprobs): + seq_outputs.append( + SequenceOutput(seq_ids[parent_id], next_token_id, + logprobs)) + sampler_output.append( + CompletionSequenceGroupOutput(seq_outputs, + group_prompt_logprobs)) + + # If not specified, store None values in SamplerOutput. + if on_device_tensors is not None: + (sampled_token_probs, logprobs_tensor, + sampled_token_ids) = on_device_tensors + else: + sampled_token_probs, logprobs_tensor, sampled_token_ids = (None, None, + None) + + return SamplerOutput( + outputs=sampler_output, + sampled_token_probs=sampled_token_probs, + sampled_token_ids=sampled_token_ids, + logprobs=logprobs_tensor, + deferred_sample_results_args=deferred_sample_results_args) + + +def _get_next_prompt_tokens(seq_group: SequenceGroupToSample) -> List[int]: + """Get a list of next prompt tokens to compute logprob from a + given sequence group. + + It is used to compute prompt logprob. Imagine you have logprob for each + query token. Query token needs to know the next prompt token id to compute + prompt logprob. This is a helper to obtain next prompt token ids. + + This API has to be used only when the caller knows seq_group is in prefill + stage. + + Returns: + A list of next prompt tokens to compute logprob. + """ + assert seq_group.is_prompt, ( + "Caller should ensure the sequence group is in a prefill stage.") + seq_ids = seq_group.seq_ids + query_len = seq_group.query_len + assert query_len is not None + # prompt has only 1 seq id. + assert len(seq_ids) == 1 + seq_data = seq_group.seq_data[seq_ids[0]] + computed_len = seq_data.get_num_computed_tokens() + prompt_tokens = seq_data.prompt_token_ids + next_prompt_tokens = [] + for output_index in seq_group.prompt_logprob_output_indices: + token_index = computed_len + output_index + 1 + assert token_index < len(prompt_tokens) + next_prompt_tokens.append(prompt_tokens[token_index]) + return next_prompt_tokens diff --git a/qwen3_6_scripts/vendor_overrides/vllm/model_executor/sampling_metadata.py b/qwen3_6_scripts/vendor_overrides/vllm/model_executor/sampling_metadata.py new file mode 100644 index 0000000..5678564 --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/model_executor/sampling_metadata.py @@ -0,0 +1,644 @@ +from array import array +from dataclasses import dataclass +from typing import Dict, List, Optional, Tuple + +import torch + +from vllm.sampling_params import SamplingParams, SamplingType +from vllm.sequence import (VLLM_TOKEN_ID_ARRAY_TYPE, SequenceData, + SequenceGroupMetadata) +from vllm.utils import (PyObjectCache, async_tensor_h2d, + is_pin_memory_available, make_tensor_with_pad) + +_SAMPLING_EPS = 1e-5 + + +@dataclass +class SequenceGroupToSample: + # |---------- N-1 iteration --------| + # |---------------- N iteration ---------------------| + # |- tokenA -|......................|-- newTokens ---| + # |---------- context_len ----------| + # |-------------------- seq_len ----------------------| + # |-- query_len ---| + + # Sequence ids for the sequence group in a previous step. + seq_ids: List[int] + sampling_params: SamplingParams + # seq_id -> sequence data. + seq_data: Dict[int, SequenceData] + # The length of the sequence (all tokens seen in the past + new token to + # compute attention) of the sequence group. None if it is in a decode + # stage. + seq_len: Optional[int] + # The length of new query tokens to compute in the current step. None if it + # is in a decode stage. The length of query_len <= seq_len if chunked + # prefill is enabled. + query_len: Optional[int] + # A random number generator for sampling. + generator: Optional[torch.Generator] + # True if the sequence group is in prefill stage. False if it is in a + # decode stage. + is_prompt: bool + # Query token indices from logits. to compute prompt logprob. Empty if + # prompt logprob is not required. + prompt_logprob_indices: List[int] + # Output offsets within this prefill chunk. Sparse diagnostic requests use + # this to retain the standard full-length prompt-logprob response shape. + prompt_logprob_output_indices: List[int] + # Sample token indices from logits. Empty if sampling is not required. + sample_indices: List[int] + + @property + def do_sample(self): + return len(self.sample_indices) > 0 + + def __post_init__(self): + if len(self.prompt_logprob_indices) > 0: + assert self.sampling_params.prompt_logprobs is not None + assert (len(self.prompt_logprob_indices) + == len(self.prompt_logprob_output_indices)) + assert self.prompt_logprob_output_indices == sorted( + set(self.prompt_logprob_output_indices)) + if self.is_prompt: + assert self.seq_len is not None + assert self.query_len is not None + assert all( + 0 <= index < self.query_len + for index in self.prompt_logprob_output_indices) + + +def gen_seq_group_to_sample_builder(num_seqs: int): + return lambda: SequenceGroupToSample( + seq_ids=[0] * num_seqs, + sampling_params=None, + seq_data=None, # type: ignore + seq_len=0, + query_len=0, + generator=None, + is_prompt=True, + prompt_logprob_indices=[], + prompt_logprob_output_indices=[], + sample_indices=[], + ) + + +class SamplingMetadataCache: + """Used to cache SamplingMetadata objects between scheduler iterations""" + + def __init__(self): + self._seq_group_to_sample_cache: Dict[int, PyObjectCache] = {} + + def get_cached_seq_group_to_sample(self, num_seqs): + if num_seqs not in self._seq_group_to_sample_cache: + self._seq_group_to_sample_cache[num_seqs] = PyObjectCache( + gen_seq_group_to_sample_builder(num_seqs)) + + obj = self._seq_group_to_sample_cache[num_seqs].get_object() + return obj + + def reset(self): + for cache in self._seq_group_to_sample_cache.values(): + cache.reset() + + +class SamplingMetadata: + """Metadata for input sequences. Used in sampler. + + The usage is as follow; + ``` + hidden_states = execute_model(...) + logits = hidden_states[sampling_metadata.selected_token_indices] + sample(logits) + + def sample(logits): + # Use categorized_sample_indices for sampling.... + ``` + + Args: + seq_groups: List of batched sequence groups. + selected_token_indices: (num_query_tokens_to_logprob). Indices to find + logits from the initial model output hidden states. + categorized_sample_indices: SamplingType -> token indices to sample. + Each token indices is 2D tensor of (num_indices, num_indices) where + the first item means the sample index within the returned logit + (before pruning padding), and the second item means the sample + index after pruning using selected_token_indices. + For example, if the returned logit is [1, 2, 3], and we select + [1, 2] for sampling, the pruned logit will be [2, 3]. In this case, + The first tuple is [1, 2] (sampled index within original logit), + and the second tuple is [0, 1] (sampled index within pruned logit). + num_prompts: Number of prompt sequence groups in seq_groups. + skip_sampler_cpu_output: Indicates if we want to skip the GPU=>CPU + serialization of token outputs. + reuse_sampling_tensors: Indicates if we want to reuse sampling + tensors that are part of the sampler forward pass. Currently, + it is mainly used for multi-step decode. + + """ + + def __init__( + self, + seq_groups: List[SequenceGroupToSample], + selected_token_indices: torch.Tensor, + categorized_sample_indices: Dict[SamplingType, torch.Tensor], + num_prompts: int, + skip_sampler_cpu_output: bool = False, + reuse_sampling_tensors: bool = False, + ) -> None: + self.seq_groups = seq_groups + self.selected_token_indices = selected_token_indices + self.categorized_sample_indices = categorized_sample_indices + self.num_prompts = num_prompts + self.skip_sampler_cpu_output = skip_sampler_cpu_output + self.reuse_sampling_tensors = reuse_sampling_tensors + + @staticmethod + def prepare( + seq_group_metadata_list: List[SequenceGroupMetadata], + seq_lens: List[int], + query_lens: List[int], + device: str, + pin_memory: bool, + generators: Optional[Dict[str, torch.Generator]] = None, + cache: Optional[SamplingMetadataCache] = None, + ) -> "SamplingMetadata": + ( + seq_groups, + selected_token_indices, + categorized_sample_indices, + num_prompts, + ) = _prepare_seq_groups(seq_group_metadata_list, seq_lens, query_lens, + device, generators, cache) + selected_token_indices = async_tensor_h2d( + selected_token_indices, + dtype=torch.long, + target_device=device, + pin_memory=pin_memory, + ) + categorized_sample_indices = { + t: async_tensor_h2d( + seq_ids, + dtype=torch.int, + target_device=device, + pin_memory=pin_memory, + ) + for t, seq_ids in categorized_sample_indices.items() + } + + sampling_metadata = SamplingMetadata( + seq_groups=seq_groups, + selected_token_indices=selected_token_indices, + categorized_sample_indices=categorized_sample_indices, + num_prompts=num_prompts, + ) + return sampling_metadata + + def __repr__(self) -> str: + return ( + "SamplingMetadata(" + f"seq_groups={self.seq_groups}, " + f"selected_token_indices={self.selected_token_indices}, " + f"categorized_sample_indices={self.categorized_sample_indices}), ") + + +def _get_prompt_logprob_output_indices( + sampling_params: SamplingParams, + seq_data: SequenceData, + prompt_logprob_len: int, +) -> List[int]: + if sampling_params.prompt_logprobs is None or prompt_logprob_len <= 0: + return [] + positions = sampling_params.prompt_logprob_positions + computed_len = seq_data.get_num_computed_tokens() + available_next_tokens = max( + 0, + len(seq_data.prompt_token_ids) - computed_len - 1, + ) + materialized_len = min(prompt_logprob_len, available_next_tokens) + if positions is None: + return list(range(materialized_len)) + + output_indices = [ + position - computed_len - 1 + for position in positions + if computed_len < position + <= computed_len + materialized_len + ] + assert output_indices == sorted(set(output_indices)) + assert all(0 <= index < materialized_len for index in output_indices) + return output_indices + + +def _prepare_seq_groups( + seq_group_metadata_list: List[SequenceGroupMetadata], + seq_lens: List[int], + query_lens: List[int], + device: str, + generators: Optional[Dict[str, torch.Generator]] = None, + cache: Optional[SamplingMetadataCache] = None, +) -> Tuple[List[SequenceGroupToSample], List[int], Dict[SamplingType, + List[int]], int, ]: + """Prepare sequence groups and indices for sampling. + + Args: + seq_group_metadata_list: A list of sequence group to batch. + seq_lens: A list of sequence lens per sequence group. + Index of prompt len should match with seq_group_metadata_list. + query_lens: A list of query lengths. Prompt lens include the length + of entire prompt tokens, and it could be shorter. + device: A device to use for random number generators, + `SequenceGroupToSample.generator`. + generators: A store of per-request random number generators used + for seeded requests. + + Returns: + seq_groups: A list of sequence group to sample. + selected_token_indices: See the definition from `SamplingMetadata`. + categorized_sample_indices: See the definition from `SamplingMetadata`. + num_prompts: Total number of prompts from `seq_group_metadata_list`. + """ + # Batched sequence groups for the current model forward stsep. + seq_groups: List[SequenceGroupToSample] = [] + # A list of token indices to sample/compute logprob. It is used to + # prune the outcome logits from the model for the performance. + selected_token_indices: List[int] = [] + # Used for selected_token_indices. + model_output_idx = 0 + + # Sampling type -> ( + # indices to sample/prompt logprob within pruned output logits, + # indices to sample within pruned logits) + categorized_sample_indices: Dict[SamplingType, List[int]] = { + t: [] + for t in SamplingType + } + # Index of logits to compute logprob. Logits include both prompt logprob + # and sample logprob indices. + logit_idx = 0 + # Total number of prompts from given sequence groups. + num_prompts = 0 + + for i, seq_group_metadata in enumerate(seq_group_metadata_list): + seq_ids = seq_group_metadata.seq_data.keys() + + if cache is not None: + sample_obj = cache.get_cached_seq_group_to_sample(len(seq_ids)) + + for j, seq_id in enumerate(seq_ids): + sample_obj.seq_ids[j] = seq_id + + sample_obj.prompt_logprob_indices.clear() + sample_obj.prompt_logprob_output_indices.clear() + sample_obj.sample_indices.clear() + + sampling_params = seq_group_metadata.sampling_params + is_prompt = seq_group_metadata.is_prompt + generator: Optional[torch.Generator] = None + # If the current seq group is in decode stage, it is None. + seq_len: Optional[int] = None + query_len: Optional[int] = None + prompt_logprob_indices: List[int] = (sample_obj.prompt_logprob_indices + if cache is not None else []) + prompt_logprob_output_indices: List[int] = ( + sample_obj.prompt_logprob_output_indices + if cache is not None else []) + sample_indices: List[int] = (sample_obj.sample_indices + if cache is not None else []) + do_sample = seq_group_metadata.do_sample + + if seq_group_metadata.is_prompt: + if sampling_params.seed is not None: + generator = torch.Generator(device=device).manual_seed( + sampling_params.seed) + if generators is not None: + generators[seq_group_metadata.request_id] = generator + + num_prompts += 1 + num_prefill_sample = len(seq_ids) + assert num_prefill_sample == 1 + assert query_lens is not None and seq_lens is not None + query_len, seq_len = query_lens[i], seq_lens[i] + # If we need sampling, exclude num_prefill_sample tokens from + # prompt logprob. + prompt_logprob_len = (query_len - num_prefill_sample + if do_sample else query_len) + sample_len = num_prefill_sample if do_sample else 0 + else: + # Decode + prompt_logprob_len = 0 + query_len = query_lens[i] if query_lens is not None else 1 + sample_len = len(seq_ids) * query_len if do_sample else 0 + + if sampling_params.seed is not None and generators is not None: + generator = generators.get(seq_group_metadata.request_id) + + seq_data = next(iter(seq_group_metadata.seq_data.values())) + prompt_logprob_output_indices.extend( + _get_prompt_logprob_output_indices( + sampling_params, + seq_data, + prompt_logprob_len, + )) + + # Update indices to select from the model output. + """ + This blocks computes selected_token_indices which is used in the + following way. + + hidden_states = model(...) + logits = hidden_states[selected_token_indices] + """ + + if sampling_params.prompt_logprobs is not None: + selected_token_indices.extend( + model_output_idx + output_index + for output_index in prompt_logprob_output_indices) + model_output_idx += prompt_logprob_len + if do_sample: + selected_token_indices.extend( + range(model_output_idx, model_output_idx + sample_len)) + model_output_idx += sample_len + + # We now find indices for logprob computation and sampling. + """ + This block computes categorized_sample_indices which is used in the + following way. + + hidden_states = model(...) + logits = hidden_states[selected_token_indices] + def sample(logits): + # Use categorized_sample_indices for sampling. + # prompt_logprob_indices to find prompt logprob indices. + # sample_indices to find sample indices. + """ + + if sampling_params.prompt_logprobs is not None: + prompt_logprob_indices.extend( + range(logit_idx, + logit_idx + len(prompt_logprob_output_indices))) + logit_idx += len(prompt_logprob_output_indices) + if do_sample: + sample_indices.extend(range(logit_idx, logit_idx + sample_len)) + categorized_sample_indices[sampling_params.sampling_type].extend( + list(range(logit_idx, logit_idx + sample_len))) + logit_idx += sample_len + + if cache is not None: + sample_obj.sampling_params = sampling_params + sample_obj.seq_data = seq_group_metadata.seq_data + sample_obj.seq_len = seq_len + sample_obj.query_len = query_len + sample_obj.generator = generator + sample_obj.is_prompt = is_prompt + else: + sample_obj = SequenceGroupToSample( + seq_ids=list(seq_ids), + sampling_params=sampling_params, + seq_data=seq_group_metadata.seq_data, + seq_len=seq_len, + query_len=query_len, + generator=generator, + is_prompt=is_prompt, + prompt_logprob_indices=list(prompt_logprob_indices), + prompt_logprob_output_indices=list( + prompt_logprob_output_indices), + sample_indices=list(sample_indices), + ) + + assert (len(sample_obj.prompt_logprob_indices) + == len(sample_obj.prompt_logprob_output_indices)) + seq_groups.append(sample_obj) + + if cache is not None: + cache.reset() + + return (seq_groups, selected_token_indices, categorized_sample_indices, + num_prompts) + + +@dataclass +class SamplingTensors: + """Tensors for sampling.""" + + temperatures: torch.Tensor + top_ps: torch.Tensor + top_ks: torch.Tensor + min_ps: torch.Tensor + presence_penalties: torch.Tensor + frequency_penalties: torch.Tensor + repetition_penalties: torch.Tensor + prompt_tokens: torch.Tensor + output_tokens: torch.Tensor + + @classmethod + def from_sampling_metadata( + cls, + sampling_metadata: "SamplingMetadata", + vocab_size: int, + device: torch.device, + dtype: torch.dtype, + ) -> Tuple["SamplingTensors", bool, bool, bool]: + prompt_tokens: List[array] = [] + output_tokens: List[array] = [] + top_ks: List[int] = [] + temperatures: List[float] = [] + top_ps: List[float] = [] + min_ps: List[float] = [] + presence_penalties: List[float] = [] + frequency_penalties: List[float] = [] + repetition_penalties: List[float] = [] + do_penalties = False + do_top_p_top_k = False + do_min_p = False + + assert sampling_metadata.seq_groups is not None + for seq_group in sampling_metadata.seq_groups: + seq_ids = seq_group.seq_ids + sampling_params = seq_group.sampling_params + temperature = sampling_params.temperature + p = sampling_params.presence_penalty + f = sampling_params.frequency_penalty + r = sampling_params.repetition_penalty + top_p = sampling_params.top_p + min_p = sampling_params.min_p + + # k should not be greater than the vocab size. + top_k = min(sampling_params.top_k, vocab_size) + top_k = vocab_size if top_k == -1 else top_k + if temperature < _SAMPLING_EPS: + # NOTE: Zero temperature means deterministic sampling + # (i.e., greedy sampling or beam search). + # Set the temperature to 1 to avoid division by zero. + temperature = 1.0 + if not do_top_p_top_k and (top_p < 1.0 - _SAMPLING_EPS + or top_k != vocab_size): + do_top_p_top_k = True + if not do_min_p and min_p > _SAMPLING_EPS: + do_min_p = True + if not do_penalties and (abs(p) >= _SAMPLING_EPS + or abs(f) >= _SAMPLING_EPS + or abs(r - 1.0) >= _SAMPLING_EPS): + do_penalties = True + + is_prompt = seq_group.is_prompt + if is_prompt and sampling_params.prompt_logprobs is not None: + # For tokens in the prompt that we only need to get + # their logprobs + query_len = seq_group.query_len + assert query_len is not None + prefill_len = len(seq_group.prompt_logprob_indices) + temperatures += [temperature] * prefill_len + top_ps += [top_p] * prefill_len + top_ks += [top_k] * prefill_len + min_ps += [min_p] * prefill_len + presence_penalties += [0] * prefill_len + frequency_penalties += [0] * prefill_len + repetition_penalties += [1] * prefill_len + + if seq_group.do_sample: + sample_lens = len(seq_group.sample_indices) + assert sample_lens >= len(seq_ids) + temperatures += [temperature] * sample_lens + top_ps += [top_p] * sample_lens + top_ks += [top_k] * sample_lens + min_ps += [min_p] * sample_lens + presence_penalties += [p] * sample_lens + frequency_penalties += [f] * sample_lens + repetition_penalties += [r] * sample_lens + + if do_penalties: + for seq_group in sampling_metadata.seq_groups: + seq_ids = seq_group.seq_ids + if (seq_group.is_prompt + and sampling_params.prompt_logprobs is not None): + prefill_len = len(seq_group.prompt_logprob_indices) + prompt_tokens.extend( + array(VLLM_TOKEN_ID_ARRAY_TYPE) + for _ in range(prefill_len)) + output_tokens.extend( + array(VLLM_TOKEN_ID_ARRAY_TYPE) + for _ in range(prefill_len)) + if seq_group.do_sample: + for seq_id in seq_ids: + seq_data = seq_group.seq_data[seq_id] + prompt_tokens.append(seq_data.prompt_token_ids_array) + output_tokens.append(seq_data.output_token_ids_array) + + sampling_tensors = SamplingTensors.from_lists( + temperatures, + top_ps, + top_ks, + min_ps, + presence_penalties, + frequency_penalties, + repetition_penalties, + prompt_tokens, + output_tokens, + vocab_size, + device, + dtype, + ) + return (sampling_tensors, do_penalties, do_top_p_top_k, do_min_p) + + @classmethod + def from_lists( + cls, + temperatures: List[float], + top_ps: List[float], + top_ks: List[int], + min_ps: List[float], + presence_penalties: List[float], + frequency_penalties: List[float], + repetition_penalties: List[float], + prompt_tokens: List[array], + output_tokens: List[array], + vocab_size: int, + device: torch.device, + dtype: torch.dtype, + ) -> "SamplingTensors": + # Note that the performance will be very bad without + # pinned memory. + pin_memory = is_pin_memory_available() + + do_penalties = prompt_tokens or output_tokens + + if do_penalties: + prompt_t = make_tensor_with_pad( + prompt_tokens, + vocab_size, + device="cpu", + dtype=torch.int64, + pin_memory=pin_memory, + ) + output_t = make_tensor_with_pad( + output_tokens, + vocab_size, + device="cpu", + dtype=torch.int64, + pin_memory=pin_memory, + ) + else: + empty_tensor = torch.empty(0, device=device, dtype=torch.long) + prompt_t = empty_tensor + output_t = empty_tensor + + temperatures_t = torch.tensor( + temperatures, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + top_ps_t = torch.tensor( + top_ps, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + min_ps_t = torch.tensor( + min_ps, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + presence_penalties_t = torch.tensor( + presence_penalties, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + frequency_penalties_t = torch.tensor( + frequency_penalties, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + repetition_penalties_t = torch.tensor( + repetition_penalties, + device="cpu", + dtype=dtype, + pin_memory=pin_memory, + ) + top_ks_t = torch.tensor( + top_ks, + device="cpu", + dtype=torch.int, + pin_memory=pin_memory, + ) + # Because the memory is pinned, we can do non-blocking + # transfer to device. + + return cls( + temperatures=temperatures_t.to(device=device, non_blocking=True), + top_ps=top_ps_t.to(device=device, non_blocking=True), + top_ks=top_ks_t.to(device=device, non_blocking=True), + min_ps=min_ps_t.to(device=device, non_blocking=True), + presence_penalties=presence_penalties_t.to(device=device, + non_blocking=True), + frequency_penalties=frequency_penalties_t.to(device=device, + non_blocking=True), + repetition_penalties=repetition_penalties_t.to(device=device, + non_blocking=True), + prompt_tokens=prompt_t.to(device=device, non_blocking=True), + output_tokens=output_t.to(device=device, non_blocking=True), + ) diff --git a/qwen3_6_scripts/vendor_overrides/vllm/sampling_params.py b/qwen3_6_scripts/vendor_overrides/vllm/sampling_params.py new file mode 100644 index 0000000..884d07e --- /dev/null +++ b/qwen3_6_scripts/vendor_overrides/vllm/sampling_params.py @@ -0,0 +1,520 @@ +"""Sampling parameters for text generation.""" +import copy +from dataclasses import dataclass +from enum import Enum, IntEnum +from functools import cached_property +from typing import Any, Callable, Dict, List, Optional, Set, Union + +import msgspec +import torch +from pydantic import BaseModel +from typing_extensions import Annotated + +from vllm.logger import init_logger + +logger = init_logger(__name__) + +_SAMPLING_EPS = 1e-5 +_MAX_TEMP = 1e-2 + + +class SamplingType(IntEnum): + GREEDY = 0 + RANDOM = 1 + RANDOM_SEED = 2 + + +LogitsProcessor = Union[Callable[[List[int], torch.Tensor], torch.Tensor], + Callable[[List[int], List[int], torch.Tensor], + torch.Tensor]] +"""LogitsProcessor is a function that takes a list +of previously generated tokens, the logits tensor +for the next token and, optionally, prompt tokens as a +first argument, and returns a modified tensor of logits +to sample from.""" + + +# maybe make msgspec? +@dataclass +class GuidedDecodingParams: + """One of these fields will be used to build a logit processor.""" + json: Optional[Union[str, Dict]] = None + regex: Optional[str] = None + choice: Optional[List[str]] = None + grammar: Optional[str] = None + json_object: Optional[bool] = None + """These are other options that can be set""" + backend: Optional[str] = None + whitespace_pattern: Optional[str] = None + + @staticmethod + def from_optional( + json: Optional[Union[Dict, BaseModel, str]], + regex: Optional[str] = None, + choice: Optional[List[str]] = None, + grammar: Optional[str] = None, + json_object: Optional[bool] = None, + backend: Optional[str] = None, + whitespace_pattern: Optional[str] = None, + ) -> "GuidedDecodingParams": + # Extract json schemas from pydantic models + if isinstance(json, (BaseModel, type(BaseModel))): + json = json.model_json_schema() + return GuidedDecodingParams( + json=json, + regex=regex, + choice=choice, + grammar=grammar, + json_object=json_object, + backend=backend, + whitespace_pattern=whitespace_pattern, + ) + + def __post_init__(self): + """Validate that some fields are mutually exclusive.""" + guide_count = sum([ + self.json is not None, self.regex is not None, self.choice + is not None, self.grammar is not None, self.json_object is not None + ]) + if guide_count > 1: + raise ValueError( + "You can only use one kind of guided decoding but multiple are " + f"specified: {self.__dict__}") + + +class RequestOutputKind(Enum): + # Return entire output so far in every RequestOutput + CUMULATIVE = 0 + # Return only deltas in each RequestOutput + DELTA = 1 + # Do not return intermediate RequestOuputs + FINAL_ONLY = 2 + + +class SamplingParams( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + # required for @cached_property. + dict=True): # type: ignore[call-arg] + """Sampling parameters for text generation. + + Overall, we follow the sampling parameters from the OpenAI text completion + API (https://platform.openai.com/docs/api-reference/completions/create). + In addition, we support beam search, which is not supported by OpenAI. + + Args: + n: Number of output sequences to return for the given prompt. + best_of: Number of output sequences that are generated from the prompt. + From these `best_of` sequences, the top `n` sequences are returned. + `best_of` must be greater than or equal to `n`. By default, + `best_of` is set to `n`. + presence_penalty: Float that penalizes new tokens based on whether they + appear in the generated text so far. Values > 0 encourage the model + to use new tokens, while values < 0 encourage the model to repeat + tokens. + frequency_penalty: Float that penalizes new tokens based on their + frequency in the generated text so far. Values > 0 encourage the + model to use new tokens, while values < 0 encourage the model to + repeat tokens. + repetition_penalty: Float that penalizes new tokens based on whether + they appear in the prompt and the generated text so far. Values > 1 + encourage the model to use new tokens, while values < 1 encourage + the model to repeat tokens. + temperature: Float that controls the randomness of the sampling. Lower + values make the model more deterministic, while higher values make + the model more random. Zero means greedy sampling. + top_p: Float that controls the cumulative probability of the top tokens + to consider. Must be in (0, 1]. Set to 1 to consider all tokens. + top_k: Integer that controls the number of top tokens to consider. Set + to -1 to consider all tokens. + min_p: Float that represents the minimum probability for a token to be + considered, relative to the probability of the most likely token. + Must be in [0, 1]. Set to 0 to disable this. + seed: Random seed to use for the generation. + stop: List of strings that stop the generation when they are generated. + The returned output will not contain the stop strings. + stop_token_ids: List of tokens that stop the generation when they are + generated. The returned output will contain the stop tokens unless + the stop tokens are special tokens. + include_stop_str_in_output: Whether to include the stop strings in + output text. Defaults to False. + ignore_eos: Whether to ignore the EOS token and continue generating + tokens after the EOS token is generated. + max_tokens: Maximum number of tokens to generate per output sequence. + min_tokens: Minimum number of tokens to generate per output sequence + before EOS or stop_token_ids can be generated + logprobs: Number of log probabilities to return per output token. + When set to None, no probability is returned. If set to a non-None + value, the result includes the log probabilities of the specified + number of most likely tokens, as well as the chosen tokens. + Note that the implementation follows the OpenAI API: The API will + always return the log probability of the sampled token, so there + may be up to `logprobs+1` elements in the response. + prompt_logprobs: Number of log probabilities to return per prompt token. + detokenize: Whether to detokenize the output. Defaults to True. + skip_special_tokens: Whether to skip special tokens in the output. + spaces_between_special_tokens: Whether to add spaces between special + tokens in the output. Defaults to True. + logits_processors: List of functions that modify logits based on + previously generated tokens, and optionally prompt tokens as + a first argument. + truncate_prompt_tokens: If set to an integer k, will use only the last k + tokens from the prompt (i.e., left truncation). Defaults to None + (i.e., no truncation). + guided_decoding: If provided, the engine will construct a guided + decoding logits processor from these parameters. Defaults to None. + logit_bias: If provided, the engine will construct a logits processor + that applies these logit biases. Defaults to None. + allowed_token_ids: If provided, the engine will construct a logits + processor which only retains scores for the given token ids. + Defaults to None. + prompt_logprob_positions: Optional prompt-token positions whose logits + should be materialized. None preserves the standard all-position + prompt-logprob behavior. + """ + + n: int = 1 + best_of: Optional[int] = None + _real_n: Optional[int] = None + presence_penalty: float = 0.0 + frequency_penalty: float = 0.0 + repetition_penalty: float = 1.0 + temperature: float = 1.0 + top_p: float = 1.0 + top_k: int = -1 + min_p: float = 0.0 + seed: Optional[int] = None + stop: Optional[Union[str, List[str]]] = None + stop_token_ids: Optional[List[int]] = None + ignore_eos: bool = False + max_tokens: Optional[int] = 16 + min_tokens: int = 0 + logprobs: Optional[int] = None + prompt_logprobs: Optional[int] = None + # NOTE: This parameter is only exposed at the engine level for now. + # It is not exposed in the OpenAI API server, as the OpenAI API does + # not support returning only a list of token IDs. + detokenize: bool = True + skip_special_tokens: bool = True + spaces_between_special_tokens: bool = True + # Optional[List[LogitsProcessor]] type. We use Any here because + # Optional[List[LogitsProcessor]] type is not supported by msgspec. + logits_processors: Optional[Any] = None + include_stop_str_in_output: bool = False + truncate_prompt_tokens: Optional[Annotated[int, msgspec.Meta(ge=1)]] = None + output_kind: RequestOutputKind = RequestOutputKind.CUMULATIVE + + # The below fields are not supposed to be used as an input. + # They are set in post_init. + output_text_buffer_length: int = 0 + _all_stop_token_ids: Set[int] = msgspec.field(default_factory=set) + + # Fields used to construct logits processors + guided_decoding: Optional[GuidedDecodingParams] = None + logit_bias: Optional[Dict[int, float]] = None + allowed_token_ids: Optional[List[int]] = None + prompt_logprob_positions: Optional[List[int]] = None + + @staticmethod + def from_optional( + n: Optional[int] = 1, + best_of: Optional[int] = None, + presence_penalty: Optional[float] = 0.0, + frequency_penalty: Optional[float] = 0.0, + repetition_penalty: Optional[float] = 1.0, + temperature: Optional[float] = 1.0, + top_p: Optional[float] = 1.0, + top_k: int = -1, + min_p: float = 0.0, + seed: Optional[int] = None, + stop: Optional[Union[str, List[str]]] = None, + stop_token_ids: Optional[List[int]] = None, + include_stop_str_in_output: bool = False, + ignore_eos: bool = False, + max_tokens: Optional[int] = 16, + min_tokens: int = 0, + logprobs: Optional[int] = None, + prompt_logprobs: Optional[int] = None, + detokenize: bool = True, + skip_special_tokens: bool = True, + spaces_between_special_tokens: bool = True, + logits_processors: Optional[List[LogitsProcessor]] = None, + truncate_prompt_tokens: Optional[Annotated[int, + msgspec.Meta(ge=1)]] = None, + output_kind: RequestOutputKind = RequestOutputKind.CUMULATIVE, + guided_decoding: Optional[GuidedDecodingParams] = None, + logit_bias: Optional[Union[Dict[int, float], Dict[str, float]]] = None, + allowed_token_ids: Optional[List[int]] = None, + prompt_logprob_positions: Optional[List[int]] = None, + ) -> "SamplingParams": + if logit_bias is not None: + logit_bias = { + int(token): bias + for token, bias in logit_bias.items() + } + + return SamplingParams( + n=1 if n is None else n, + best_of=best_of, + presence_penalty=0.0 + if presence_penalty is None else presence_penalty, + frequency_penalty=0.0 + if frequency_penalty is None else frequency_penalty, + repetition_penalty=1.0 + if repetition_penalty is None else repetition_penalty, + temperature=1.0 if temperature is None else temperature, + top_p=1.0 if top_p is None else top_p, + top_k=top_k, + min_p=min_p, + seed=seed, + stop=stop, + stop_token_ids=stop_token_ids, + include_stop_str_in_output=include_stop_str_in_output, + ignore_eos=ignore_eos, + max_tokens=max_tokens, + min_tokens=min_tokens, + logprobs=logprobs, + prompt_logprobs=prompt_logprobs, + detokenize=detokenize, + skip_special_tokens=skip_special_tokens, + spaces_between_special_tokens=spaces_between_special_tokens, + logits_processors=logits_processors, + truncate_prompt_tokens=truncate_prompt_tokens, + output_kind=output_kind, + guided_decoding=guided_decoding, + logit_bias=logit_bias, + allowed_token_ids=allowed_token_ids, + prompt_logprob_positions=prompt_logprob_positions, + ) + + def __post_init__(self) -> None: + # how we deal with `best_of``: + # if `best_of`` is not set, we default to `n`; + # if `best_of`` is set, we set `n`` to `best_of`, + # and set `_real_n`` to the original `n`. + # when we return the result, we will check + # if we need to return `n` or `_real_n` results + if self.best_of: + if self.best_of < self.n: + raise ValueError( + f"best_of must be greater than or equal to n, " + f"got n={self.n} and best_of={self.best_of}.") + self._real_n = self.n + self.n = self.best_of + if 0 < self.temperature < _MAX_TEMP: + logger.warning( + "temperature %s is less than %s, which may cause numerical " + "errors nan or inf in tensors. We have maxed it out to %s.", + self.temperature, _MAX_TEMP, _MAX_TEMP) + self.temperature = max(self.temperature, _MAX_TEMP) + if self.seed == -1: + self.seed = None + else: + self.seed = self.seed + if self.stop is None: + self.stop = [] + elif isinstance(self.stop, str): + self.stop = [self.stop] + else: + self.stop = list(self.stop) + if self.stop_token_ids is None: + self.stop_token_ids = [] + else: + self.stop_token_ids = list(self.stop_token_ids) + self.logprobs = 1 if self.logprobs is True else self.logprobs + self.prompt_logprobs = (1 if self.prompt_logprobs is True else + self.prompt_logprobs) + if self.prompt_logprob_positions is not None: + self.prompt_logprob_positions = list( + self.prompt_logprob_positions) + + # Number of characters to hold back for stop string evaluation + # until sequence is finished. + if self.stop and not self.include_stop_str_in_output: + self.output_text_buffer_length = max(len(s) for s in self.stop) - 1 + + self._verify_args() + + if self.temperature < _SAMPLING_EPS: + # Zero temperature means greedy sampling. + self.top_p = 1.0 + self.top_k = -1 + self.min_p = 0.0 + self._verify_greedy_sampling() + # eos_token_id is added to this by the engine + self._all_stop_token_ids = set(self.stop_token_ids) + + def _verify_args(self) -> None: + if not isinstance(self.n, int): + raise ValueError(f"n must be an int, but is of " + f"type {type(self.n)}") + if self.n < 1: + raise ValueError(f"n must be at least 1, got {self.n}.") + if not -2.0 <= self.presence_penalty <= 2.0: + raise ValueError("presence_penalty must be in [-2, 2], got " + f"{self.presence_penalty}.") + if not -2.0 <= self.frequency_penalty <= 2.0: + raise ValueError("frequency_penalty must be in [-2, 2], got " + f"{self.frequency_penalty}.") + if not 0.0 < self.repetition_penalty <= 2.0: + raise ValueError("repetition_penalty must be in (0, 2], got " + f"{self.repetition_penalty}.") + if self.temperature < 0.0: + raise ValueError( + f"temperature must be non-negative, got {self.temperature}.") + if not 0.0 < self.top_p <= 1.0: + raise ValueError(f"top_p must be in (0, 1], got {self.top_p}.") + if self.top_k < -1 or self.top_k == 0: + raise ValueError(f"top_k must be -1 (disable), or at least 1, " + f"got {self.top_k}.") + if not isinstance(self.top_k, int): + raise TypeError( + f"top_k must be an integer, got {type(self.top_k).__name__}") + if not 0.0 <= self.min_p <= 1.0: + raise ValueError("min_p must be in [0, 1], got " + f"{self.min_p}.") + if self.max_tokens is not None and self.max_tokens < 1: + raise ValueError( + f"max_tokens must be at least 1, got {self.max_tokens}.") + if self.min_tokens < 0: + raise ValueError(f"min_tokens must be greater than or equal to 0, " + f"got {self.min_tokens}.") + if self.max_tokens is not None and self.min_tokens > self.max_tokens: + raise ValueError( + f"min_tokens must be less than or equal to " + f"max_tokens={self.max_tokens}, got {self.min_tokens}.") + if self.logprobs is not None and self.logprobs < 0: + raise ValueError( + f"logprobs must be non-negative, got {self.logprobs}.") + if self.prompt_logprobs is not None and self.prompt_logprobs < 0: + raise ValueError(f"prompt_logprobs must be non-negative, got " + f"{self.prompt_logprobs}.") + if self.prompt_logprob_positions is not None: + if self.prompt_logprobs is None: + raise ValueError( + "prompt_logprob_positions requires prompt_logprobs.") + if ( + not self.prompt_logprob_positions + or any( + not isinstance(position, int) + or isinstance(position, bool) + or position <= 0 + for position in self.prompt_logprob_positions + ) + or self.prompt_logprob_positions + != sorted(set(self.prompt_logprob_positions)) + ): + raise ValueError( + "prompt_logprob_positions must be a sorted unique list " + "of positive integers.") + if (self.truncate_prompt_tokens is not None + and self.truncate_prompt_tokens < 1): + raise ValueError(f"truncate_prompt_tokens must be >= 1, " + f"got {self.truncate_prompt_tokens}") + assert isinstance(self.stop, list) + if any(not stop_str for stop_str in self.stop): + raise ValueError("stop cannot contain an empty string.") + if self.stop and not self.detokenize: + raise ValueError( + "stop strings are only supported when detokenize is True. " + "Set detokenize=True to use stop.") + if self.best_of != self._real_n and self.output_kind == ( + RequestOutputKind.DELTA): + raise ValueError("best_of must equal n to use output_kind=DELTA") + + def _verify_greedy_sampling(self) -> None: + if self.n > 1: + raise ValueError("n must be 1 when using greedy sampling, " + f"got {self.n}.") + + def update_from_generation_config( + self, + generation_config: Dict[str, Any], + model_eos_token_id: Optional[int] = None) -> None: + """Update if there are non-default values from generation_config""" + + if model_eos_token_id is not None: + # Add the eos token id into the sampling_params to support + # min_tokens processing. + self._all_stop_token_ids.add(model_eos_token_id) + + # Update eos_token_id for generation + if (eos_ids := generation_config.get("eos_token_id")) is not None: + # it can be either int or list of int + eos_ids = {eos_ids} if isinstance(eos_ids, int) else set(eos_ids) + if model_eos_token_id is not None: + # We don't need to include the primary eos_token_id in + # stop_token_ids since it's handled separately for stopping + # purposes. + eos_ids.discard(model_eos_token_id) + if eos_ids: + self._all_stop_token_ids.update(eos_ids) + if not self.ignore_eos: + eos_ids.update(self.stop_token_ids) + self.stop_token_ids = list(eos_ids) + + @cached_property + def sampling_type(self) -> SamplingType: + if self.temperature < _SAMPLING_EPS: + return SamplingType.GREEDY + if self.seed is not None: + return SamplingType.RANDOM_SEED + return SamplingType.RANDOM + + @property + def all_stop_token_ids(self) -> Set[int]: + return self._all_stop_token_ids + + def clone(self) -> "SamplingParams": + """Deep copy excluding LogitsProcessor objects. + + LogitsProcessor objects are excluded because they may contain an + arbitrary, nontrivial amount of data. + See https://github.com/vllm-project/vllm/issues/3087 + """ + + logit_processor_refs = None if self.logits_processors is None else { + id(lp): lp + for lp in self.logits_processors + } + return copy.deepcopy(self, memo=logit_processor_refs) + + def __repr__(self) -> str: + return ( + f"SamplingParams(n={self.n}, " + f"presence_penalty={self.presence_penalty}, " + f"frequency_penalty={self.frequency_penalty}, " + f"repetition_penalty={self.repetition_penalty}, " + f"temperature={self.temperature}, " + f"top_p={self.top_p}, " + f"top_k={self.top_k}, " + f"min_p={self.min_p}, " + f"seed={self.seed}, " + f"stop={self.stop}, " + f"stop_token_ids={self.stop_token_ids}, " + f"include_stop_str_in_output={self.include_stop_str_in_output}, " + f"ignore_eos={self.ignore_eos}, " + f"max_tokens={self.max_tokens}, " + f"min_tokens={self.min_tokens}, " + f"logprobs={self.logprobs}, " + f"prompt_logprobs={self.prompt_logprobs}, " + "prompt_logprob_positions=" + f"{self.prompt_logprob_positions}, " + f"skip_special_tokens={self.skip_special_tokens}, " + "spaces_between_special_tokens=" + f"{self.spaces_between_special_tokens}, " + f"truncate_prompt_tokens={self.truncate_prompt_tokens}), " + f"guided_decoding={self.guided_decoding}") + + +class BeamSearchParams( + msgspec.Struct, + omit_defaults=True, # type: ignore[call-arg] + # required for @cached_property. + dict=True): # type: ignore[call-arg] + """Beam search parameters for text generation.""" + beam_width: int + max_tokens: int + ignore_eos: bool = False + temperature: float = 0.0 + length_penalty: float = 1.0 diff --git a/qwen3_6_scripts/wheels/transformers-4.55.3-py3-none-any.whl b/qwen3_6_scripts/wheels/transformers-4.55.3-py3-none-any.whl new file mode 100644 index 0000000..f7df878 Binary files /dev/null and b/qwen3_6_scripts/wheels/transformers-4.55.3-py3-none-any.whl differ