input_metadata.py 3.42 KB
Newer Older
1
from typing import Dict, List, Optional, Tuple
Woosuk Kwon's avatar
Woosuk Kwon committed
2
3

import torch
Woosuk Kwon's avatar
Woosuk Kwon committed
4
from xformers.ops import AttentionBias
Woosuk Kwon's avatar
Woosuk Kwon committed
5

Woosuk Kwon's avatar
Woosuk Kwon committed
6
7
from vllm.sampling_params import SamplingParams
from vllm.sequence import SequenceData
8

Woosuk Kwon's avatar
Woosuk Kwon committed
9
10

class InputMetadata:
11
12
13
14
15
16
17
18
19
20
21
    """Metadata for input sequences. Used for PagedAttention.

    Args:
        seq_groups: List of (seq_ids, sampling_params).
        seq_data: Seq_id -> SequenceData.
        prompt_lens: Lengths of prompts.
        slot_mapping: The address to write the new KV to of each token.
        context_lens: the length of attention context for each generation token.
        max_context_len: The maximum context length.
        block_tables: The block tables. (Seq id -> list of physical block)
    """
Woosuk Kwon's avatar
Woosuk Kwon committed
22
23
24

    def __init__(
        self,
25
26
        seq_groups: List[Tuple[List[int], SamplingParams]],
        seq_data: Dict[int, SequenceData],
Woosuk Kwon's avatar
Woosuk Kwon committed
27
28
29
30
31
        prompt_lens: List[int],
        slot_mapping: torch.Tensor,
        context_lens: torch.Tensor,
        max_context_len: int,
        block_tables: torch.Tensor,
32
        sliding_window: Optional[int] = None,
Woosuk Kwon's avatar
Woosuk Kwon committed
33
    ) -> None:
34
        self.seq_groups = seq_groups
35
        self.seq_data = seq_data
Woosuk Kwon's avatar
Woosuk Kwon committed
36
        self.prompt_lens = prompt_lens
Woosuk Kwon's avatar
Woosuk Kwon committed
37
        self.slot_mapping = slot_mapping
Woosuk Kwon's avatar
Woosuk Kwon committed
38
39
40
41
        self.context_lens = context_lens
        self.max_context_len = max_context_len
        self.block_tables = block_tables

42
        self.max_prompt_len = max(prompt_lens) if prompt_lens else 0
43
44
45
46
        self.to_cache = None
        if sliding_window is not None:
            # We need to keep the positions of sliding windows within
            # the key / value tables, this is helpful to know which
47
            # elements we need to cache.
48
49
50
51
52
53
54
            to_cache, start_idx = [], 0
            for prompt_len in self.prompt_lens:
                to_cache.extend(
                    range(
                        start_idx + max(0, prompt_len - sliding_window),
                        start_idx + prompt_len,
                    ))
55
                start_idx += self.max_prompt_len
56
57
58
59
60
            to_cache.extend(range(start_idx, slot_mapping.shape[0]))
            self.to_cache = torch.tensor(to_cache,
                                         dtype=torch.int32,
                                         device=self.slot_mapping.device)

Woosuk Kwon's avatar
Woosuk Kwon committed
61
        self.num_prompts = len(prompt_lens)
62
        self.num_prompt_tokens = self.num_prompts * self.max_prompt_len
Woosuk Kwon's avatar
Woosuk Kwon committed
63
        self.num_generation_tokens = context_lens.shape[0]
Woosuk Kwon's avatar
Woosuk Kwon committed
64
65
66
67
        if block_tables.numel() > 0:
            self.max_num_blocks_per_seq = block_tables.shape[1]
        else:
            self.max_num_blocks_per_seq = 0
68
69
        assert block_tables.shape[0] == self.num_generation_tokens
        assert context_lens.shape[0] == self.num_generation_tokens
Woosuk Kwon's avatar
Woosuk Kwon committed
70

Woosuk Kwon's avatar
Woosuk Kwon committed
71
        # Set during the execution of the first attention op.
72
        self.attn_bias: Optional[AttentionBias] = None
Woosuk Kwon's avatar
Woosuk Kwon committed
73

Woosuk Kwon's avatar
Woosuk Kwon committed
74
    def __repr__(self) -> str:
75
        # Print only useful metadata.
Woosuk Kwon's avatar
Woosuk Kwon committed
76
        return (f'InputMetadata('
77
78
                f'num_prompt_tokens={self.num_prompt_tokens}, '
                f'num_prompts={self.num_prompts}, '
Zhuohan Li's avatar
Zhuohan Li committed
79
                f'prompt_lens={self.prompt_lens}, '
80
                f'num_generation_tokens={self.num_generation_tokens}, '
Zhuohan Li's avatar
Zhuohan Li committed
81
                f'context_lens={self.context_lens}, '
82
83
84
85
                f'max_context_len={self.max_context_len}), '
                f'max_num_blocks_per_seq={self.max_num_blocks_per_seq}, '
                f'block_tables={self.block_tables}), '
                f'slot_mapping={self.slot_mapping}')