|
|
@@ -77,12 +77,12 @@ class Llama3Model(nn.Module):
|
|
|
self.cfg = cfg
|
|
|
self.current_pos = 0 # Track current position in KV cache
|
|
|
|
|
|
- def forward(self, in_idx, use_cache=False, cache=None):
|
|
|
+ def forward(self, in_idx, cache=None):
|
|
|
tok_embeds = self.tok_emb(in_idx)
|
|
|
x = tok_embeds
|
|
|
|
|
|
num_tokens = x.shape[1]
|
|
|
- if use_cache:
|
|
|
+ if cache is not None:
|
|
|
pos_start = self.current_pos
|
|
|
pos_end = pos_start + num_tokens
|
|
|
self.current_pos = pos_end
|
|
|
@@ -101,10 +101,9 @@ class Llama3Model(nn.Module):
|
|
|
for i, block in enumerate(self.trf_blocks):
|
|
|
blk_cache = cache.get(i) if cache else None
|
|
|
x, new_blk_cache = block(x, mask, self.cos, self.sin,
|
|
|
- use_cache=use_cache,
|
|
|
start_pos=pos_start,
|
|
|
cache=blk_cache)
|
|
|
- if cache:
|
|
|
+ if cache is not None:
|
|
|
cache.update(i, new_blk_cache)
|
|
|
next_cache.append(new_blk_cache)
|
|
|
|
|
|
@@ -130,11 +129,11 @@ class TransformerBlock(nn.Module):
|
|
|
self.norm1 = nn.RMSNorm(cfg["emb_dim"], eps=1e-5, dtype=cfg["dtype"])
|
|
|
self.norm2 = nn.RMSNorm(cfg["emb_dim"], eps=1e-5, dtype=cfg["dtype"])
|
|
|
|
|
|
- def forward(self, x, mask, cos, sin, use_cache=False, start_pos=0, cache=None):
|
|
|
+ def forward(self, x, mask, cos, sin, start_pos=0, cache=None):
|
|
|
# Shortcut connection for attention block
|
|
|
shortcut = x
|
|
|
x = self.norm1(x)
|
|
|
- x, next_cache = self.att(x, mask, cos, sin, use_cache=use_cache, start_pos=start_pos, cache=cache) # Shape [batch_size, num_tokens, emb_size]
|
|
|
+ x, next_cache = self.att(x, mask, cos, sin, start_pos=start_pos, cache=cache) # Shape [batch_size, num_tokens, emb_size]
|
|
|
x = x + shortcut # Add the original input back
|
|
|
|
|
|
# Shortcut connection for feed-forward block
|
|
|
@@ -180,7 +179,7 @@ class GroupedQueryAttention(nn.Module):
|
|
|
self.W_query = nn.Linear(d_in, d_out, bias=False, dtype=dtype)
|
|
|
self.out_proj = nn.Linear(d_out, d_out, bias=False, dtype=dtype)
|
|
|
|
|
|
- def forward(self, x, mask, cos, sin, use_cache=False, start_pos=0, cache=None):
|
|
|
+ def forward(self, x, mask, cos, sin, start_pos=0, cache=None):
|
|
|
b, num_tokens, _ = x.shape
|
|
|
|
|
|
# Apply projections
|
|
|
@@ -197,18 +196,15 @@ class GroupedQueryAttention(nn.Module):
|
|
|
queries = apply_rope(queries, cos, sin, offset=start_pos)
|
|
|
keys_new = apply_rope(keys_new, cos, sin, offset=start_pos)
|
|
|
|
|
|
- if use_cache:
|
|
|
- if cache is None:
|
|
|
- keys = keys_new
|
|
|
- values = values_new
|
|
|
- else:
|
|
|
- prev_k, prev_v = cache
|
|
|
- keys = torch.cat([prev_k, keys_new], dim=2)
|
|
|
- values = torch.cat([prev_v, values_new], dim=2)
|
|
|
+ if cache is not None:
|
|
|
+ prev_k, prev_v = cache
|
|
|
+ keys = torch.cat([prev_k, keys_new], dim=2)
|
|
|
+ values = torch.cat([prev_v, values_new], dim=2)
|
|
|
next_cache = (keys, values)
|
|
|
else:
|
|
|
+ start_pos = 0 # reset RoPE
|
|
|
keys, values = keys_new, values_new
|
|
|
- next_cache = None
|
|
|
+ next_cache = (keys, values)
|
|
|
|
|
|
# Expand keys and values to match the number of heads
|
|
|
# Shape: (b, num_heads, num_tokens, head_dim)
|
|
|
@@ -226,7 +222,7 @@ class GroupedQueryAttention(nn.Module):
|
|
|
attn_scores = queries @ keys.transpose(2, 3) # Dot product for each head
|
|
|
|
|
|
# Use the mask to fill attention scores
|
|
|
- attn_scores = attn_scores.masked_fill(mask[:num_tokens, :num_tokens], -torch.inf)
|
|
|
+ attn_scores = attn_scores.masked_fill(mask, -torch.inf)
|
|
|
|
|
|
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
|
|
|
assert keys.shape[-1] == self.head_dim
|
|
|
@@ -286,7 +282,7 @@ def compute_rope_params(head_dim, theta_base=10_000, context_length=4096, freq_c
|
|
|
return cos, sin
|
|
|
|
|
|
|
|
|
-def apply_rope(x, cos, sin, offset=9):
|
|
|
+def apply_rope(x, cos, sin, offset=0):
|
|
|
# x: (batch_size, num_heads, seq_len, head_dim)
|
|
|
batch_size, num_heads, seq_len, head_dim = x.shape
|
|
|
assert head_dim % 2 == 0, "Head dimension must be even"
|