vllm-hash

vllm/vllm/v1/core/kv_cache_utils.py · ef9339e · L640–740 · 固定 commit 上游

这是固定版本源码的只读摘录;可在线阅读,也可通过上游链接查看原文件。

640def generate_block_hash_extra_keys(
641    request: Request, start_token_idx: int, end_token_idx: int, start_mm_idx: int
642) -> tuple[tuple[Any, ...] | None, int]:
643    """Generate extra keys for the block hash. The extra keys can come from
644    the multi-modal inputs, request specific metadata (e.g., LoRA names), and
645    hashed data from prompt embeddings.
646
647    Args:
648        request: The request object.
649        start_token_idx: The start token index of the block.
650        end_token_idx: The end token index of the block.
651        start_mm_idx: The start multi-modal index of the block.
652
653    Returns:
654        A tuple of extra keys and the next multi-modal index.
655
656    """
657    mm_extra_keys: list[Any]
658    mm_extra_keys, new_start_mm_idx = _gen_mm_extra_hash_keys(
659        request, start_token_idx, end_token_idx, start_mm_idx
660    )
661    lora_extra_keys = _gen_lora_extra_hash_keys(request)
662    cache_salt_keys: list[tuple[str, str]] = (
663        [("cache_salt", request.cache_salt)]
664        if (start_token_idx == 0 and request.cache_salt)
665        else []
666    )
667    prompt_embeds_keys = _gen_prompt_embeds_extra_hash_keys(
668        request, start_token_idx, end_token_idx
669    )
670
671    extra_keys: list[Any] = (
672        lora_extra_keys + mm_extra_keys + cache_salt_keys + prompt_embeds_keys
673    )
674
675    if not extra_keys:
676        return None, new_start_mm_idx
677
678    return tuple(extra_keys), new_start_mm_idx
679
680
681def to_event_extra_keys(
682    extra_keys: Iterable[tuple[Any, ...] | None] | None,
683) -> list[tuple[Any, ...] | None] | None:
684    """Convert block-hash extra keys to the untagged per-block list published
685    in KV events.
686
687    External KV event consumers parse the pre-tagging shapes: bare LoRA names
688    and cache salts, ``(mm_identifier, offset)`` pairs and bare prompt-embeds
689    digests. Events keep that format until consumers handle the tagged keys.
690
691    Args:
692        extra_keys: One entry per block, each as returned by
693            `generate_block_hash_extra_keys`, or None.
694
695    Returns:
696        One untagged entry per block, or None if there are no entries.
697
698    """
699    if not extra_keys:
700        return None
701    event_keys = [
702        None
703        if keys is None
704        else tuple(key[1:] if key[0] == "mm" else key[1] for key in keys)
705        for keys in extra_keys
706    ]
707    return event_keys or None
708
709
710def hash_block_tokens(
711    hash_function: Callable[[Any], bytes],
712    parent_block_hash: BlockHash | None,
713    curr_block_token_ids: Sequence[int],
714    extra_keys: tuple[Any, ...] | None = None,
715) -> BlockHash:
716    """Computes a hash value corresponding to the contents of a block and
717    the contents of the preceding block(s). The hash value is used for
718    prefix caching. We use LRU cache for this function to avoid recomputing
719    hash values for the same block contents.
720
721    Args:
722        hash_function: The hash function used to compute block hash.
723        parent_block_hash: The hash of the parent block. None
724            if this is the first block.
725        curr_block_token_ids: A list of token ids in the current
726            block. The current block is assumed to be full.
727        extra_keys: Extra keys for the block.
728
729    Returns:
730        The hash value of the block and the token ids in the block.
731        The entire tuple is used as the hash key of the block.
732
733    """
734    if not parent_block_hash:
735        parent_block_hash = NONE_HASH
736
737    curr_block_token_ids_tuple = tuple(curr_block_token_ids)
738    return BlockHash(
739        hash_function((parent_block_hash, curr_block_token_ids_tuple, extra_keys))
740    )