File size: 22,512 Bytes
37351b6 | 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | from __future__ import annotations
import gc
import json
import math
import os
import statistics
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple
import torch
# ============================================================
# Small data containers
# ============================================================
@dataclass
class PreparedExample:
index: int
input_text: str
outputs: List[str]
answer_prefix: str
original_prompt_len: int
used_prompt_len: int
prompt_token_ids: List[int]
raw_record: Dict[str, Any]
@dataclass
class PreparedBatch:
batch_id: int
prompt_len: int
examples: List[PreparedExample]
@property
def batch_size(self) -> int:
return len(self.examples)
@property
def example_indices(self) -> List[int]:
return [ex.index for ex in self.examples]
def to_tensor(self, device: torch.device) -> torch.Tensor:
ids = [ex.prompt_token_ids for ex in self.examples]
return torch.tensor(ids, dtype=torch.long, device=device)
# ============================================================
# Files / serialization
# ============================================================
def ensure_dir(path: str | os.PathLike[str]) -> str:
Path(path).mkdir(parents=True, exist_ok=True)
return str(path)
def write_json(path: str | os.PathLike[str], payload: Any) -> None:
with open(path, "w", encoding="utf-8") as f:
json.dump(payload, f, indent=2, ensure_ascii=False)
def write_jsonl(path: str | os.PathLike[str], rows: Iterable[Dict[str, Any]]) -> None:
with open(path, "w", encoding="utf-8") as f:
for row in rows:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
# ============================================================
# Dtypes / formatting
# ============================================================
def dtype_from_str(s: str) -> torch.dtype:
s = s.lower()
if s in ("bf16", "bfloat16"):
return torch.bfloat16
if s in ("fp16", "float16", "half"):
return torch.float16
raise ValueError(f"Unsupported dtype string: {s}")
def dtype_to_name(dtype: torch.dtype) -> str:
if dtype == torch.bfloat16:
return "bfloat16"
if dtype == torch.float16:
return "float16"
return str(dtype)
def format_optional_int(x: Optional[int]) -> str:
if x is None:
return "unset(default)"
return str(int(x))
def load_model_and_tokenizer(
model_name: str,
dtype: torch.dtype,
device: torch.device,
*,
attn_implementation: str = "sdpa",
) -> Tuple[Any, Any]:
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
if tokenizer.pad_token_id is None and tokenizer.eos_token_id is not None:
tokenizer.pad_token = tokenizer.eos_token
def _load_with_kwargs(load_kwargs: Dict[str, Any]) -> Any:
if attn_implementation:
try:
return AutoModelForCausalLM.from_pretrained(
model_name,
attn_implementation=attn_implementation,
**load_kwargs,
)
except TypeError as exc:
msg = str(exc)
if "attn_implementation" not in msg and "unexpected keyword argument" not in msg:
raise
return AutoModelForCausalLM.from_pretrained(model_name, **load_kwargs)
try:
model = _load_with_kwargs({"dtype": dtype, "low_cpu_mem_usage": True})
except TypeError as exc:
msg = str(exc)
if "dtype" not in msg and "unexpected keyword argument" not in msg:
raise
model = _load_with_kwargs({"torch_dtype": dtype, "low_cpu_mem_usage": True})
model.to(device)
model.eval()
gen_cfg = getattr(model, "generation_config", None)
if gen_cfg is not None:
if getattr(gen_cfg, "pad_token_id", None) is None and getattr(tokenizer, "pad_token_id", None) is not None:
gen_cfg.pad_token_id = int(tokenizer.pad_token_id)
if getattr(gen_cfg, "eos_token_id", None) is None and getattr(tokenizer, "eos_token_id", None) is not None:
gen_cfg.eos_token_id = int(tokenizer.eos_token_id)
if getattr(model.config, "pad_token_id", None) is None and getattr(tokenizer, "pad_token_id", None) is not None:
model.config.pad_token_id = int(tokenizer.pad_token_id)
if getattr(model.config, "eos_token_id", None) is None and getattr(tokenizer, "eos_token_id", None) is not None:
model.config.eos_token_id = int(tokenizer.eos_token_id)
return model, tokenizer
def maybe_cuda_sync(device: torch.device) -> None:
if device.type == "cuda":
torch.cuda.synchronize(device)
# ============================================================
# RoPE helpers used by the tutorial runtime.
# ============================================================
def rotate_half(x: torch.Tensor) -> torch.Tensor:
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return torch.cat((-x2, x1), dim=-1)
def apply_rotary_pos_emb(
q: torch.Tensor,
k: torch.Tensor,
cos: torch.Tensor,
sin: torch.Tensor,
unsqueeze_dim: int = 1,
) -> Tuple[torch.Tensor, torch.Tensor]:
cos = cos.unsqueeze(unsqueeze_dim)
sin = sin.unsqueeze(unsqueeze_dim)
q_embed = (q * cos) + (rotate_half(q) * sin)
k_embed = (k * cos) + (rotate_half(k) * sin)
return q_embed, k_embed
def get_rotary(model: Any, attn: Any) -> Any:
rotary = getattr(attn, "rotary_emb", None)
if rotary is None:
rotary = getattr(getattr(model, "model", None), "rotary_emb", None)
if rotary is None:
rotary = getattr(model, "rotary_emb", None)
if rotary is None:
raise AttributeError(
"Could not find rotary embedding module. Tried attn.rotary_emb, model.model.rotary_emb, model.rotary_emb."
)
return rotary
def rotary_cos_sin(rotary: Any, v: torch.Tensor, position_ids: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
try:
return rotary(v, position_ids)
except TypeError:
return rotary(v, position_ids=position_ids)
# ============================================================
# Model / cache helpers
# ============================================================
def get_lm_head(model: Any) -> Any:
if hasattr(model, "lm_head"):
return model.lm_head
return model.get_output_embeddings()
def get_input_embeddings(model: Any) -> Any:
if hasattr(model, "model") and hasattr(model.model, "embed_tokens"):
return model.model.embed_tokens
return model.get_input_embeddings()
def unpack_past_kv(past_key_values: Any) -> List[Tuple[torch.Tensor, torch.Tensor]]:
if hasattr(past_key_values, "to_legacy_cache"):
past_key_values = past_key_values.to_legacy_cache()
if isinstance(past_key_values, (tuple, list)):
out: List[Tuple[torch.Tensor, torch.Tensor]] = []
for layer_past in past_key_values:
if not (isinstance(layer_past, (tuple, list)) and len(layer_past) >= 2):
raise TypeError(f"Unexpected layer past type: {type(layer_past)}")
out.append((layer_past[0], layer_past[1]))
return out
raise TypeError(f"Unsupported past_key_values type: {type(past_key_values)}")
def infer_attention_dims(model: Any) -> Dict[str, int]:
first_layer = model.model.layers[0]
attn = first_layer.self_attn
n_heads = (
getattr(attn, "num_heads", None)
or getattr(attn, "num_attention_heads", None)
or getattr(model.config, "num_attention_heads", None)
)
n_kv = getattr(attn, "num_key_value_heads", None) or getattr(model.config, "num_key_value_heads", None)
if n_kv is None:
n_kv = n_heads
head_dim = getattr(attn, "head_dim", None)
if head_dim is None:
hidden_size = getattr(model.config, "hidden_size", None)
if hidden_size is None or n_heads is None:
raise RuntimeError("Could not infer hidden_size/head_dim from model config.")
head_dim = hidden_size // n_heads
return {
"num_heads": int(n_heads),
"num_kv_heads": int(n_kv),
"head_dim": int(head_dim),
"num_layers": int(len(model.model.layers)),
"hidden_size": int(getattr(model.config, "hidden_size", head_dim * n_heads)),
}
@torch.inference_mode()
def prefill_in_chunks(
model: Any,
input_ids: torch.Tensor,
*,
prefill_chunk_size: int,
) -> Tuple[torch.Tensor, List[Tuple[torch.Tensor, torch.Tensor]]]:
"""
Chunked prefill to avoid materializing full-sequence logits for very long prompts.
Returns:
- last-token logits: [B, V]
- legacy-style past_kv list with per-layer tensors [B, KVH, L, D]
"""
if prefill_chunk_size <= 0:
raise ValueError(f"prefill_chunk_size must be > 0, got {prefill_chunk_size}")
prompt_len = int(input_ids.shape[1])
lm_head = get_lm_head(model)
base_model = getattr(model, "model", None)
past_key_values = None
last_logits: Optional[torch.Tensor] = None
for start in range(0, prompt_len, prefill_chunk_size):
end = min(prompt_len, start + prefill_chunk_size)
chunk = input_ids[:, start:end]
if base_model is not None:
try:
out = base_model(
input_ids=chunk,
use_cache=True,
past_key_values=past_key_values,
return_dict=True,
)
past_key_values = out.past_key_values
if end == prompt_len:
last_hidden = out.last_hidden_state[:, -1:, :].contiguous()
last_logits = lm_head(last_hidden)[:, 0, :].contiguous()
del out
continue
except Exception:
# Fall back to the full CausalLM forward path. This keeps the runtime usable
# across transformer releases whose base-model cache APIs differ slightly.
pass
out = model(
input_ids=chunk,
use_cache=True,
past_key_values=past_key_values,
return_dict=True,
)
past_key_values = out.past_key_values
if end == prompt_len:
last_logits = out.logits[:, -1, :].contiguous()
del out
if last_logits is None:
raise RuntimeError("Chunked prefill failed to produce final logits.")
past_list = unpack_past_kv(past_key_values)
del past_key_values
return last_logits, past_list
def alloc_nhd_caches_from_prefill(
past_list: List[Optional[Tuple[torch.Tensor, torch.Tensor]]],
*,
prompt_len: int,
total_len: int,
dtype: torch.dtype,
device: torch.device,
consume_past: bool = True,
) -> List[Dict[str, torch.Tensor]]:
"""
Create batched contiguous caches per layer:
k_nhd: [B, total_len, KVH, D]
v_nhd: [B, total_len, KVH, D]
k_hnd: [B, KVH, total_len, D] (view)
v_hnd: [B, KVH, total_len, D] (view)
The source HF prefill cache is expected to be [B, KVH, L, D].
"""
caches: List[Dict[str, torch.Tensor]] = []
for layer_idx, kv in enumerate(past_list):
if kv is None:
raise RuntimeError(f"Layer {layer_idx} past KV was already consumed.")
k_pref, v_pref = kv
if k_pref.dim() != 4 or v_pref.dim() != 4:
raise RuntimeError(
f"Expected prefill K/V tensors with rank 4, got {tuple(k_pref.shape)} and {tuple(v_pref.shape)}"
)
bsz, kvh, L, d = k_pref.shape
if L != prompt_len:
raise RuntimeError(f"Prefill prompt length mismatch: expected {prompt_len}, got {L}")
if tuple(v_pref.shape) != tuple(k_pref.shape):
raise RuntimeError(f"K/V shape mismatch at layer {layer_idx}: {tuple(k_pref.shape)} vs {tuple(v_pref.shape)}")
k_nhd = torch.empty((bsz, total_len, kvh, d), device=device, dtype=dtype)
v_nhd = torch.empty((bsz, total_len, kvh, d), device=device, dtype=dtype)
k_nhd[:, :prompt_len].copy_(k_pref.to(dtype).transpose(1, 2).contiguous())
v_nhd[:, :prompt_len].copy_(v_pref.to(dtype).transpose(1, 2).contiguous())
caches.append(
{
"k_nhd": k_nhd,
"v_nhd": v_nhd,
"k_hnd": k_nhd.permute(0, 2, 1, 3),
"v_hnd": v_nhd.permute(0, 2, 1, 3),
}
)
if consume_past:
past_list[layer_idx] = None
del k_pref, v_pref
if consume_past:
gc.collect()
return caches
# ============================================================
# Stop tokens / normalization / exact match
# ============================================================
def get_stop_token_ids(tokenizer: Any, extra_stop_strings: Sequence[str]) -> List[int]:
stop_ids: List[int] = []
if getattr(tokenizer, "eos_token_id", None) is not None:
stop_ids.append(int(tokenizer.eos_token_id))
unk_id = getattr(tokenizer, "unk_token_id", None)
for s in extra_stop_strings:
try:
token_id = tokenizer.convert_tokens_to_ids(s)
except Exception:
token_id = None
if token_id is None or not isinstance(token_id, int) or token_id < 0:
continue
if unk_id is not None and int(token_id) == int(unk_id):
continue
stop_ids.append(int(token_id))
return sorted(set(stop_ids))
def normalize_prediction_text(text: str, answer_prefix: str = "") -> str:
out = text.strip()
prefix = (answer_prefix or "").strip()
if prefix:
out_cmp = out.lower()
prefix_cmp = prefix.lower()
if out_cmp.startswith(prefix_cmp):
out = out[len(prefix) :].strip()
return out
def exact_match_any(prediction_text: str, acceptable_outputs: Sequence[str], answer_prefix: str = "") -> bool:
pred = normalize_prediction_text(prediction_text, answer_prefix=answer_prefix)
gold = {str(x).strip() for x in acceptable_outputs}
return pred in gold
# ============================================================
# Metrics helpers
# ============================================================
def _quantile(values: Sequence[float], q: float) -> float:
if not values:
return float("nan")
if len(values) == 1:
return float(values[0])
ordered = sorted(float(v) for v in values)
pos = (len(ordered) - 1) * q
lo = int(math.floor(pos))
hi = int(math.ceil(pos))
if lo == hi:
return ordered[lo]
frac = pos - lo
return ordered[lo] * (1.0 - frac) + ordered[hi] * frac
def summarize_numeric(values: Sequence[float]) -> Dict[str, float]:
vals = [float(v) for v in values]
if not vals:
return {
"count": 0,
"mean": float("nan"),
"std": float("nan"),
"median": float("nan"),
"p90": float("nan"),
"min": float("nan"),
"max": float("nan"),
"ci95_lo": float("nan"),
"ci95_hi": float("nan"),
}
mean = statistics.fmean(vals)
std = statistics.stdev(vals) if len(vals) > 1 else 0.0
half_width = 1.96 * std / math.sqrt(len(vals)) if len(vals) > 1 else 0.0
return {
"count": len(vals),
"mean": mean,
"std": std,
"median": _quantile(vals, 0.5),
"p90": _quantile(vals, 0.9),
"min": min(vals),
"max": max(vals),
"ci95_lo": mean - half_width,
"ci95_hi": mean + half_width,
}
# ============================================================
# Batched one-token manual decode and lockstep generation
# ============================================================
@torch.inference_mode()
def forward_one_token_manual_batched(
model: Any,
*,
token_id_t: torch.Tensor, # [B,1] long
position_ids: torch.Tensor, # [B,1] long
caches: List[Dict[str, torch.Tensor]],
pos: int,
attention_backend: Any,
) -> torch.Tensor:
if not (hasattr(model, "model") and hasattr(model.model, "layers")):
raise RuntimeError("Expected a HF Llama-style model with model.model.layers")
embed = get_input_embeddings(model)
lm_head = get_lm_head(model)
hidden_states = embed(token_id_t) # [B,1,hidden]
batch_size = int(token_id_t.shape[0])
for layer_idx, layer in enumerate(model.model.layers):
attn = layer.self_attn
n_heads = (
getattr(attn, "num_heads", None)
or getattr(attn, "num_attention_heads", None)
or getattr(model.config, "num_attention_heads", None)
)
n_kv = getattr(attn, "num_key_value_heads", None) or getattr(model.config, "num_key_value_heads", None)
if n_kv is None:
n_kv = n_heads
head_dim = getattr(attn, "head_dim", None)
if head_dim is None:
head_dim = getattr(model.config, "hidden_size", None) // n_heads
residual = hidden_states
x = layer.input_layernorm(hidden_states)
q = attn.q_proj(x)
k = attn.k_proj(x)
v = attn.v_proj(x)
_, q_len, _ = q.shape
q = q.view(batch_size, q_len, n_heads, head_dim).transpose(1, 2).contiguous() # [B,H,1,D]
k = k.view(batch_size, q_len, n_kv, head_dim).transpose(1, 2).contiguous() # [B,KVH,1,D]
v = v.view(batch_size, q_len, n_kv, head_dim).transpose(1, 2).contiguous() # [B,KVH,1,D]
rotary = get_rotary(model, attn)
cos, sin = rotary_cos_sin(rotary, v, position_ids)
q, k = apply_rotary_pos_emb(q, k, cos, sin, unsqueeze_dim=1)
kc = caches[layer_idx]["k_nhd"]
vc = caches[layer_idx]["v_nhd"]
kc[:, pos].copy_(k[:, :, 0, :])
vc[:, pos].copy_(v[:, :, 0, :])
q_bhd = q[:, :, 0, :].contiguous()
attn_out_bhd = attention_backend.decode(q_bhd, kc, vc, valid_len=pos + 1)
attn_out = attn_out_bhd.unsqueeze(2) # [B,H,1,D]
attn_out = attn_out.transpose(1, 2).reshape(batch_size, q_len, n_heads * head_dim).contiguous()
attn_out = attn.o_proj(attn_out)
hidden_states = residual + attn_out
residual = hidden_states
x = layer.post_attention_layernorm(hidden_states)
x = layer.mlp(x)
hidden_states = residual + x
hidden_states = model.model.norm(hidden_states)
logits = lm_head(hidden_states)
return logits # [B,1,V]
@torch.inference_mode()
def generate_lockstep_batch(
model: Any,
tokenizer: Any,
*,
prompt_ids: torch.Tensor, # [B, L]
prefill_logits_last: torch.Tensor, # [B, V]
caches: List[Dict[str, torch.Tensor]],
attention_backend: Any,
stop_token_ids: Sequence[int],
max_new_tokens: int,
lockstep_stop_mode: str,
skip_special_tokens: bool,
answer_prefixes: Sequence[str],
acceptable_outputs: Sequence[Sequence[str]],
) -> Dict[str, Any]:
device = prompt_ids.device
batch_size, prompt_len = prompt_ids.shape
stop_set = {int(x) for x in stop_token_ids}
stop_fill_id = int(next(iter(stop_set))) if stop_set else int(getattr(tokenizer, "eos_token_id", 0) or 0)
generated_all: List[List[int]] = [[] for _ in range(batch_size)]
first_stop_step: List[Optional[int]] = [None for _ in range(batch_size)]
finished = torch.zeros(batch_size, dtype=torch.bool, device=device)
cur_logits = prefill_logits_last.contiguous()
maybe_cuda_sync(device)
t0 = time.perf_counter()
pos = int(prompt_len)
steps_sampled = 0
for step in range(int(max_new_tokens)):
next_ids = torch.argmax(cur_logits, dim=-1)
if finished.any():
next_ids = torch.where(finished, torch.full_like(next_ids, stop_fill_id), next_ids)
next_ids_list = [int(x) for x in next_ids.tolist()]
for b, token_id in enumerate(next_ids_list):
generated_all[b].append(token_id)
if first_stop_step[b] is None and token_id in stop_set:
first_stop_step[b] = step
finished[b] = True
steps_sampled = step + 1
should_break = False
if lockstep_stop_mode == "all_finished" and bool(finished.all().item()):
should_break = True
if step == int(max_new_tokens) - 1:
should_break = True
if should_break:
break
token_id_t = next_ids.view(batch_size, 1)
position_ids = torch.full((batch_size, 1), pos, dtype=torch.long, device=device)
cur_logits = forward_one_token_manual_batched(
model,
token_id_t=token_id_t,
position_ids=position_ids,
caches=caches,
pos=pos,
attention_backend=attention_backend,
)[:, 0, :].contiguous()
pos += 1
maybe_cuda_sync(device)
t1 = time.perf_counter()
records: List[Dict[str, Any]] = []
visible_generated_tokens = 0
for b in range(batch_size):
stop_step = first_stop_step[b]
full_ids = list(generated_all[b])
if stop_step is None:
visible_ids = list(full_ids)
else:
visible_ids = list(full_ids[:stop_step])
visible_text = tokenizer.decode(visible_ids, skip_special_tokens=skip_special_tokens)
normalized_text = normalize_prediction_text(visible_text, answer_prefix=answer_prefixes[b])
is_exact_match = exact_match_any(visible_text, acceptable_outputs[b], answer_prefix=answer_prefixes[b])
visible_generated_tokens += len(visible_ids)
records.append(
{
"generated_token_ids_all": full_ids,
"generated_token_ids_visible": visible_ids,
"generated_text": visible_text,
"generated_text_normalized": normalized_text,
"stop_step": stop_step,
"exact_match": bool(is_exact_match),
}
)
timed_generated_tokens = int(batch_size * steps_sampled)
decode_time_s = float(t1 - t0)
return {
"decode_time_s": decode_time_s,
"steps_sampled": int(steps_sampled),
"timed_generated_tokens": timed_generated_tokens,
"visible_generated_tokens": int(visible_generated_tokens),
"examples": records,
}
|