input_metadata.py 1.88 KB
Newer Older
1
from typing import Optional
Woosuk Kwon's avatar
Woosuk Kwon committed
2
3

import torch
4

Woosuk Kwon's avatar
Woosuk Kwon committed
5
6

class InputMetadata:
7
    """Metadata for input sequences. Used in PagedAttention.
8
9
10
11
12

    Args:
        prompt_lens: Lengths of prompts.
        slot_mapping: The address to write the new KV to of each token.
        max_context_len: The maximum context length.
13
        context_lens: the length of attention context for each sequence.
14
        block_tables: The block tables. (Seq id -> list of physical block)
15
        kv_cache_dtype: Data type to store kv cache.
16
    """
Woosuk Kwon's avatar
Woosuk Kwon committed
17
18
19

    def __init__(
        self,
20
        is_prompt: bool,
Woosuk Kwon's avatar
Woosuk Kwon committed
21
        slot_mapping: torch.Tensor,
22
23
24
        prompt_lens: Optional[torch.Tensor],
        max_seq_len: Optional[int],
        start_loc: Optional[torch.Tensor],
25
26
27
        max_context_len: Optional[int],
        context_lens: Optional[torch.Tensor],
        block_tables: Optional[torch.Tensor],
28
        use_cuda_graph: bool,
29
        kv_cache_dtype: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
30
    ) -> None:
31
        self.is_prompt = is_prompt
32
33
34
        self.prompt_lens = prompt_lens
        self.max_seq_len = max_seq_len
        self.start_loc = start_loc
35
        self.max_context_len = max_context_len
Woosuk Kwon's avatar
Woosuk Kwon committed
36
        self.slot_mapping = slot_mapping
Woosuk Kwon's avatar
Woosuk Kwon committed
37
38
        self.context_lens = context_lens
        self.block_tables = block_tables
39
        self.use_cuda_graph = use_cuda_graph
40
        self.kv_cache_dtype = kv_cache_dtype
Woosuk Kwon's avatar
Woosuk Kwon committed
41

Woosuk Kwon's avatar
Woosuk Kwon committed
42
        # Set during the execution of the first attention op.
43
44
        # FIXME(woosuk): This is a hack.
        self.attn_bias = None
Woosuk Kwon's avatar
Woosuk Kwon committed
45

Woosuk Kwon's avatar
Woosuk Kwon committed
46
    def __repr__(self) -> str:
47
        return ("InputMetadata("
48
                f"is_prompt={self.is_prompt}, "
49
50
51
                f"max_context_len={self.max_context_len}, "
                f"slot_mapping={self.slot_mapping}, "
                f"context_lens={self.context_lens}, "
52
                f"block_tables={self.block_tables}, "
53
54
                f"use_cuda_graph={self.use_cuda_graph}, "
                f"kv_cache_dtype={self.kv_cache_dtype})")