File size: 8,052 Bytes
2cdacf1 | 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | """Hand-written Qwen3 forward pass with an explicit KV cache.
It reads the weights straight out of a loaded Hugging Face Qwen3 model (optionally
wrapped by PEFT), but every computation below is ours: RMSNorm, QK-Norm, RoPE at
explicit positions, grouped-query attention, SwiGLU MLP, and the cache reads/writes.
The same code runs Qwen3-0.6B / 1.7B / 4B, since only config sizes differ.
"""
import time
import torch
import torch.nn.functional as F
from .attention import causal_mask, reference_attention, tiled_attention
def _rms_norm(x, norm):
"""RMSNorm as Qwen3 does it: normalise in float32, scale by the weight in the input dtype."""
dtype = x.dtype
x = x.float()
x = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + norm.variance_epsilon)
return norm.weight * x.to(dtype)
def _linear(mod, x, lora: bool):
"""nn.Linear, or a PEFT LoRA layer: W x (+ B A x * scaling when lora is on)."""
base = getattr(mod, "base_layer", mod)
y = F.linear(x, base.weight, base.bias)
if lora and hasattr(mod, "lora_A"):
for name in mod.active_adapters:
if name in mod.lora_A:
a, b = mod.lora_A[name], mod.lora_B[name]
y = y + (b(a(x.to(a.weight.dtype))) * mod.scaling[name]).to(y.dtype)
return y
def _rotate_half(x):
x1, x2 = x.chunk(2, dim=-1)
return torch.cat([-x2, x1], dim=-1)
def _sync(device):
if device.type == "cuda":
torch.cuda.synchronize(device)
class KVCache:
"""Preallocated K/V buffers for all layers plus a length pointer.
Layout: [layers, batch, kv_heads, max_len, head_dim]. Positions 0..length-1 are valid.
Rolling back (speculative decoding) only lowers `length`; stale entries are overwritten later.
"""
def __init__(self, n_layers, n_kv_heads, head_dim, max_len, dtype, device, batch=1):
shape = (n_layers, batch, n_kv_heads, max_len, head_dim)
self.k = torch.zeros(shape, dtype=dtype, device=device)
self.v = torch.zeros(shape, dtype=dtype, device=device)
self.length = 0
self.max_len = max_len
def crop(self, length: int):
assert 0 <= length <= self.length
self.length = length
class Qwen3Decoder:
def __init__(self, hf_model, attention: str = "reference"):
m = hf_model.get_base_model() if hasattr(hf_model, "get_base_model") else hf_model
cfg = m.config
assert not getattr(cfg, "use_sliding_window", False), "sliding-window attention not implemented"
rope = getattr(cfg, "rope_parameters", None) or getattr(cfg, "rope_scaling", None) or {}
assert rope.get("rope_type", "default") == "default", "only plain RoPE is implemented"
theta = rope.get("rope_theta", getattr(cfg, "rope_theta", None))
self.model, self.lm_head, self.config = m.model, m.lm_head, cfg
self.n_heads = cfg.num_attention_heads
self.n_kv = cfg.num_key_value_heads
self.head_dim = getattr(cfg, "head_dim", None) or cfg.hidden_size // cfg.num_attention_heads
self.dtype = m.model.embed_tokens.weight.dtype
self.device = m.model.embed_tokens.weight.device
self.inv_freq = 1.0 / (theta ** (torch.arange(0, self.head_dim, 2, dtype=torch.float32) / self.head_dim))
self.attention = attention # "reference" | "tiled" | "sdpa"
def new_cache(self, max_len: int, batch: int = 1) -> KVCache:
return KVCache(len(self.model.layers), self.n_kv, self.head_dim, max_len, self.dtype, self.device, batch)
def _rope(self, positions):
freqs = positions[:, None].float() * self.inv_freq.to(positions.device)[None, :]
emb = torch.cat([freqs, freqs], dim=-1)
return emb.cos().to(self.dtype), emb.sin().to(self.dtype)
def _attend(self, q, k, v):
if self.attention == "tiled":
return tiled_attention(q, k, v)
if self.attention == "sdpa": # PyTorch's fused kernel, with our bottom-right causal mask
return F.scaled_dot_product_attention(q, k, v, attn_mask=causal_mask(q.shape[-2], k.shape[-2], q.device))
return reference_attention(q, k, v)
@torch.no_grad()
def forward(self, input_ids, cache: KVCache | None = None, lora: bool = True, all_logits: bool = False):
"""Run new tokens through the model.
With a cache, the new tokens get positions cache.length.. and their K/V are appended;
attention then covers cached + new keys. Without a cache, positions start at 0.
Returns float32 logits for the last position, or for every new position if all_logits.
"""
B, T = input_ids.shape
start = cache.length if cache is not None else 0
assert cache is None or start + T <= cache.max_len, "KV cache full"
cos, sin = self._rope(torch.arange(start, start + T, device=input_ids.device))
rep = self.n_heads // self.n_kv # grouped-query attention: one KV head serves `rep` query heads
h = self.model.embed_tokens(input_ids)
for i, layer in enumerate(self.model.layers):
a = layer.self_attn
x = _rms_norm(h, layer.input_layernorm)
q = _linear(a.q_proj, x, lora).view(B, T, self.n_heads, self.head_dim)
k = _linear(a.k_proj, x, lora).view(B, T, self.n_kv, self.head_dim)
v = _linear(a.v_proj, x, lora).view(B, T, self.n_kv, self.head_dim).transpose(1, 2)
q = _rms_norm(q, a.q_norm).transpose(1, 2) # QK-Norm: per-head RMSNorm, before RoPE
k = _rms_norm(k, a.k_norm).transpose(1, 2)
q = q * cos + _rotate_half(q) * sin
k = k * cos + _rotate_half(k) * sin
if cache is not None:
cache.k[i, :, :, start:start + T] = k
cache.v[i, :, :, start:start + T] = v
k, v = cache.k[i, :, :, :start + T], cache.v[i, :, :, :start + T]
o = self._attend(q, k.repeat_interleave(rep, dim=1), v.repeat_interleave(rep, dim=1))
h = h + _linear(a.o_proj, o.transpose(1, 2).reshape(B, T, -1), lora)
x = _rms_norm(h, layer.post_attention_layernorm)
mlp = layer.mlp
h = h + _linear(mlp.down_proj, F.silu(_linear(mlp.gate_proj, x, lora)) * _linear(mlp.up_proj, x, lora), lora)
if cache is not None:
cache.length = start + T
h = _rms_norm(h if all_logits else h[:, -1:], self.model.norm)
return _linear(self.lm_head, h, lora).float()
@torch.no_grad()
def greedy_generate(dec: Qwen3Decoder, input_ids, max_new_tokens: int, eos_ids=(), use_cache=True, lora=True):
"""Greedy decoding, batch size 1.
use_cache=True : prefill the prompt once, then feed one token per step against the KV cache.
use_cache=False: reference path, re-running the whole growing sequence every step.
Returns (new token ids, timing/memory stats).
"""
assert input_ids.shape[0] == 1
device = input_ids.device
if device.type == "cuda":
torch.cuda.reset_peak_memory_stats(device)
_sync(device)
t0 = time.perf_counter()
cache = dec.new_cache(input_ids.shape[1] + max_new_tokens) if use_cache else None
nxt = dec.forward(input_ids, cache, lora)[:, -1].argmax(-1, keepdim=True)
_sync(device)
t1 = time.perf_counter()
out, seq = [nxt.item()], input_ids
while len(out) < max_new_tokens and out[-1] not in eos_ids:
if use_cache:
logits = dec.forward(nxt, cache, lora)
else:
seq = torch.cat([seq, nxt], dim=1)
logits = dec.forward(seq, None, lora)
nxt = logits[:, -1].argmax(-1, keepdim=True)
out.append(nxt.item())
_sync(device)
t2 = time.perf_counter()
return out, {
"prompt_tokens": input_ids.shape[1],
"new_tokens": len(out),
"prefill_s": t1 - t0,
"decode_s": t2 - t1,
"decode_tok_per_s": (len(out) - 1) / (t2 - t1) if len(out) > 1 else 0.0,
"total_s": t2 - t0,
"peak_mem_mb": torch.cuda.max_memory_allocated(device) / 2**20 if device.type == "cuda" else None,
}
|