Skip to content

Support for KV caching and batched inference #1934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 64 additions & 37 deletions litgpt/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import torch.nn as nn
from typing_extensions import Self

from litgpt.attention import DefaultKeysAndValues, MultiHeadSelfAttention
from litgpt.config import Config as BaseConfig
from litgpt.kvcache.base import KVCache
from litgpt.model import GPT as BaseModel
from litgpt.model import Block as BaseBlock
from litgpt.model import CausalSelfAttention as BaseCausalSelfAttention
Expand All @@ -42,8 +44,9 @@ def __init__(self, config: Config) -> None:
ln_f=config.norm_class(config.n_embd, eps=config.norm_eps),
)
)
self.mask_cache: Optional[torch.Tensor] = None
self.mha = MultiHeadSelfAttention(config)
self.max_seq_length = self.config.block_size
self._default_kv_cache = False

@classmethod
def from_name(cls, name: str, **kwargs: Any) -> Self:
Expand All @@ -57,56 +60,80 @@ def _init_weights(self, module: nn.Module) -> None:


class Block(BaseBlock):
def __init__(self, config: Config, block_idx: int) -> None:
super().__init__(config, block_idx)
self.attn = CausalSelfAttention(config, block_idx)
def __init__(
self,
config: Config,
block_idx: int,
kv_cache: Optional[KVCache] = None,
) -> None:
super().__init__(config, block_idx, kv_cache)
self.attn = CausalSelfAttention(config, block_idx, kv_cache=kv_cache)


class CausalSelfAttention(BaseCausalSelfAttention):
"""A modification of `litgpt.model.CausalSelfAttention` that adds the attention
over the adaption prompt."""

def __init__(self, config: Config, block_idx: int) -> None:
super().__init__(config, block_idx)
if block_idx >= config.adapter_start_layer:
def __init__(
self,
config: Config,
block_idx: int,
kv_cache: Optional[KVCache] = None,
) -> None:
super().__init__(
config=config,
block_idx=block_idx,
kv_cache=kv_cache,
)
self._extend_forward = block_idx >= config.adapter_start_layer
if self._extend_forward:
# adapter embedding layer
self.adapter_wte = nn.Embedding(config.adapter_prompt_length, config.n_embd)
# gate for adaption
self.gating_factor = torch.nn.Parameter(torch.zeros(1, 1, config.n_head, 1))
# kv cache for inference
self.adapter_kv_cache: Optional[Tuple[torch.Tensor, torch.Tensor]] = None

def scaled_dot_product_attention(
self, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, mask: Optional[torch.Tensor] = None
def _transform_output(
self,
y: torch.Tensor,
query: torch.Tensor,
mha: MultiHeadSelfAttention,
) -> torch.Tensor:
y = super().scaled_dot_product_attention(q, k, v, mask)
if self.block_idx < self.config.adapter_start_layer:
return y

aT = self.config.adapter_prompt_length
if self.adapter_kv_cache is not None:
# since this uses the wte weights as the prefix and the kv cache is only used during inference, ak and av
# are the same every call
ak, av = self.adapter_kv_cache
else:
prefix = self.adapter_wte.weight.reshape(1, aT, self.config.n_embd)
aqkv = self.qkv(prefix)
q_per_kv = self.config.n_head // self.config.n_query_groups
aqkv = aqkv.view(1, aT, self.config.n_query_groups, q_per_kv + 2, self.config.head_size)
aqkv = aqkv.permute(0, 2, 3, 1, 4)
_, ak, av = aqkv.split((q_per_kv, 1, 1), dim=2)
if self.config.n_query_groups != 1:
# for MHA this is a no-op
ak = ak.repeat_interleave(q_per_kv, dim=2)
av = av.repeat_interleave(q_per_kv, dim=2)
ak = ak.view(1, -1, aT, self.config.head_size) # (1, nh_ak, aT, hs)
av = av.view(1, -1, aT, self.config.head_size) # (1, nh_av, aT, hs)
self.adapter_kv_cache = (ak, av)

T = q.size(2)
amask = torch.ones(T, aT, dtype=torch.bool, device=q.device)
ay = super().scaled_dot_product_attention(q, ak, av, amask)
return y + self.gating_factor * ay
if self._extend_forward:
B, T, _ = y.shape
y = y.view(B, T, self.config.n_head, self.config.head_size)
aT = self.config.adapter_prompt_length
if self.adapter_kv_cache is not None:
# since this uses the wte weights as the prefix and the kv cache is only used during inference, ak and av
# are the same every call
ak, av = self.adapter_kv_cache
else:
prefix = self.adapter_wte.weight.reshape(1, aT, self.config.n_embd)
aqkv = self.qkv(prefix)
q_per_kv = self.config.n_head // self.config.n_query_groups
aqkv = aqkv.view(1, aT, self.config.n_query_groups, q_per_kv + 2, self.config.head_size)
aqkv = aqkv.permute(0, 2, 3, 1, 4)
_, ak, av = aqkv.split((q_per_kv, 1, 1), dim=2)
if self.config.n_query_groups != 1:
# for MHA this is a no-op
ak = ak.repeat_interleave(q_per_kv, dim=2)
av = av.repeat_interleave(q_per_kv, dim=2)
ak = ak.view(1, -1, aT, self.config.head_size) # (1, nh_ak, aT, hs)
av = av.view(1, -1, aT, self.config.head_size) # (1, nh_av, aT, hs)
self.adapter_kv_cache = (ak, av)

amask = torch.ones(T, aT, dtype=torch.bool, device=query.device)
a_k_and_v = DefaultKeysAndValues(keys=ak, values=av)
ay, _ = mha.scaled_dot_product_attention(
query=query,
k_and_v=a_k_and_v,
mask=amask,
is_causal=False,
)
y = (y + self.gating_factor * ay).view(B, T, -1)

return y

def reset_parameters(self) -> None:
if hasattr(self, "gating_factor"):
Expand Down
25 changes: 19 additions & 6 deletions litgpt/adapter_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from litgpt.adapter import GPT as BaseModel
from litgpt.adapter import CausalSelfAttention as BaseCausalSelfAttention
from litgpt.adapter import Config as BaseConfig
from litgpt.attention import MultiHeadSelfAttention
from litgpt.kvcache.base import KVCache
from litgpt.model import Block as BaseBlock
from litgpt.scripts.convert_hf_checkpoint import qkv_reassemble
from litgpt.utils import map_old_state_dict_weights
Expand Down Expand Up @@ -77,8 +79,9 @@ def __init__(self, config: Config) -> None:
ln_f=config.norm_class(config.n_embd, eps=config.norm_eps),
)
)
self.mask_cache: Optional[torch.Tensor] = None
self.mha = MultiHeadSelfAttention(config)
self.max_seq_length = self.config.block_size
self._default_kv_cache = False

@classmethod
def from_name(cls, name: str, **kwargs: Any) -> Self:
Expand All @@ -98,18 +101,28 @@ def _load_from_state_dict(self, state_dict: Dict, prefix: str, *args: Any, **kwa


class Block(BaseBlock):
def __init__(self, config: Config, block_idx: int) -> None:
super().__init__(config, block_idx)
self.attn = CausalSelfAttention(config, block_idx)
def __init__(
self,
config: Config,
block_idx: int,
kv_cache: Optional[KVCache] = None,
) -> None:
super().__init__(config, block_idx, kv_cache)
self.attn = CausalSelfAttention(config, block_idx, kv_cache=kv_cache)
self.mlp = config.mlp_class(config)


class CausalSelfAttention(BaseCausalSelfAttention):
"""A modification of `litgpt.adapter.CausalSelfAttention` that uses the Adapter V2 Linear class"""

# Copy&paste from :class:`model.CausalSelfAttention`
def __init__(self, config: Config, block_idx: int) -> None:
super().__init__(config, block_idx)
def __init__(
self,
config: Config,
block_idx: int,
kv_cache: Optional[KVCache] = None,
) -> None:
super().__init__(config, block_idx, kv_cache)
# key, query, value projections for all heads, but in a batch
shape = (config.n_head + 2 * config.n_query_groups) * config.head_size
self.qkv = AdapterV2Linear(in_features=config.n_embd, out_features=shape, bias=config.bias or config.attn_bias)
Expand Down
21 changes: 16 additions & 5 deletions litgpt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ def distribute(
kv_cache_size = model.max_seq_length
else:
kv_cache_size = fixed_kv_cache_size
model.set_kv_cache(batch_size=1, max_seq_length=kv_cache_size, device=fabric.device)
model.set_kv_cache(
batch_size=1,
max_seq_length=kv_cache_size,
device=fabric.device,
)
self.kv_cache_initialized = True
self.fixed_kv_cache_size = fixed_kv_cache_size

Expand Down Expand Up @@ -504,15 +508,22 @@ def generate(
device = self.fabric.device
else:
device = self.preprocessor.device
self.model.set_kv_cache(batch_size=1, max_seq_length=max_returned_tokens, device=device)
self.model.set_kv_cache(
batch_size=1,
max_seq_length=max_returned_tokens,
device=device,
)
self.kv_cache_initialized = True

# Dynamically grow the kv cache size if necessary
if not self.fixed_kv_cache_size and self.prev_generated_seq_length < max_returned_tokens:
tmp_device = self.model.mask_cache.device
tmp_device = self.model.mha.mask_cache.device
self.model.clear_kv_cache()
self.model.set_kv_cache(batch_size=1, max_seq_length=max_returned_tokens, device=tmp_device)

self.model.set_kv_cache(
batch_size=1,
max_seq_length=max_returned_tokens,
device=tmp_device,
)
else:
for block in self.model.transformer.h:
block.attn.kv_cache.reset_parameters()
Expand Down
Loading
Loading