-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathbase.py
More file actions
87 lines (70 loc) · 2.35 KB
/
base.py
File metadata and controls
87 lines (70 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
import torch
from minisgl.core import Batch
@dataclass
class BaseAttnMetadata(ABC):
@abstractmethod
def get_last_indices(self, bs: int) -> torch.Tensor: ...
class BaseAttnBackend(ABC):
@abstractmethod
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
layer_id: int,
batch: Batch,
*,
window_size: tuple[int, int] = (-1, -1),
softmax_scale: float | None = None,
) -> torch.Tensor: ...
@abstractmethod
def prepare_metadata(self, batch: Batch) -> None: ...
@abstractmethod
def init_capture_graph(self, max_seq_len: int, bs_list: List[int]) -> None: ...
@abstractmethod
def prepare_for_capture(self, batch: Batch) -> None: ...
@abstractmethod
def prepare_for_replay(self, batch: Batch) -> None: ...
class HybridBackend(BaseAttnBackend):
def __init__(
self,
prefill_backend: BaseAttnBackend,
decode_backend: BaseAttnBackend,
) -> None:
self.prefill_backend = prefill_backend
self.decode_backend = decode_backend
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
layer_id: int,
batch: Batch,
*,
window_size: tuple[int, int] = (-1, -1),
softmax_scale: float | None = None,
) -> torch.Tensor:
backend = self.prefill_backend if batch.is_prefill else self.decode_backend
return backend.forward(
q,
k,
v,
layer_id,
batch,
window_size=window_size,
softmax_scale=softmax_scale,
)
def prepare_metadata(self, batch: Batch) -> None:
backend = self.prefill_backend if batch.is_prefill else self.decode_backend
return backend.prepare_metadata(batch)
def init_capture_graph(self, max_seq_len: int, bs_list: List[int]) -> None:
self.decode_backend.init_capture_graph(max_seq_len, bs_list)
def prepare_for_capture(self, batch: Batch) -> None:
self.decode_backend.prepare_for_capture(batch)
def prepare_for_replay(self, batch: Batch) -> None:
self.decode_backend.prepare_for_replay(batch)