File size: 27,917 Bytes
8a88d0a |
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 |
#!/usr/bin/env python3
"""
n_flex.py β Flexible Attention Mechanisms
Constraint: Must support AR (causal), SAT (block), and NAR (bidirectional)
Testing:
1. Linear Attention - O(n) instead of O(nΒ²)
2. Cosine Attention - Different similarity metric
3. Differential Attention - Noise cancellation (Microsoft 2024)
4. Local + Global - Sparse hybrid
5. Multi-Query Attention (MQA) - Inference efficient
6. Grouped Query Attention (GQA) - Between MHA and MQA
7. Retention - RetNet style (recurrent + parallel)
8. Gated Linear Attention - Recent efficient attention
9. ReLU Attention - Simpler activation
10. Sigmoid Attention - Bounded attention
"""
from __future__ import annotations
import argparse, math, time
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Literal
DEV = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.backends.cuda.matmul.allow_tf32 = True
VOCAB = 128256
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Masking utilities for AR/SAT/NAR
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_mask(n: int, mode: str = "ar", block_size: int = 2):
"""
AR (autoregressive): causal, see only past
SAT (semi-autoregressive): see within block + all past blocks
NAR (non-autoregressive): bidirectional, see everything
"""
if mode == "nar":
return None # No mask
elif mode == "ar":
return torch.triu(torch.full((n, n), float("-inf"), device=DEV), 1)
elif mode == "sat":
# Block-wise: can see within same block and all previous blocks
idx = torch.arange(n, device=DEV)
block_idx = idx // block_size
# Allow if same block OR target block is earlier
mask = torch.where(
(block_idx.unsqueeze(0) <= block_idx.unsqueeze(1)),
torch.tensor(0.0, device=DEV),
torch.tensor(float("-inf"), device=DEV)
)
return mask
else:
raise ValueError(f"Unknown mode: {mode}")
def alibi_bias(n_heads: int, n_tokens: int):
def slopes(n):
start = 2 ** (-2 ** -(math.log2(n) - 3))
return [start * (start ** i) for i in range(n)]
if n_heads > 0 and math.log2(n_heads).is_integer():
s = slopes(n_heads)
else:
closest = 2 ** math.floor(math.log2(max(1, n_heads)))
s = slopes(closest)[:n_heads]
s = torch.tensor(s, device=DEV).view(1, n_heads, 1, 1)
i = torch.arange(n_tokens, device=DEV).view(1, 1, n_tokens, 1)
j = torch.arange(n_tokens, device=DEV).view(1, 1, 1, n_tokens)
return -s * (j - i).clamp_min(0).float()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. STANDARD (baseline)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class StandardAttention(nn.Module):
"""Standard multi-head attention - O(nΒ²)"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk)
att = att + alibi_bias(self.h, N)
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
z = (att.softmax(-1) @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. LINEAR ATTENTION - O(n) via kernel trick
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class LinearAttention(nn.Module):
"""
Linear attention: O(n) instead of O(nΒ²)
Uses feature map Ο(x) so that Ο(q)Ο(k)^T β softmax(qk^T)
Key insight: (QK^T)V = Q(K^TV) - compute K^TV first for O(n)
Works with AR/SAT/NAR via cumsum tricks for causal
"""
def __init__(self, d: int, h: int, feature_map: str = "elu"):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
self.feature_map = feature_map
self.eps = 1e-6
def _phi(self, x):
"""Feature map for linear attention"""
if self.feature_map == "elu":
return F.elu(x) + 1
elif self.feature_map == "relu":
return F.relu(x)
elif self.feature_map == "softmax":
return F.softmax(x, dim=-1)
else: # identity
return x
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2] # (B, H, N, dk)
# Apply feature map
q = self._phi(q)
k = self._phi(k)
if mask is None:
# NAR: Full bidirectional - O(n) via associativity
# (Q @ K^T) @ V = Q @ (K^T @ V)
kv = torch.einsum('bhnd,bhnv->bhdv', k, v) # (B, H, dk, dv)
out = torch.einsum('bhnd,bhdv->bhnv', q, kv) # (B, H, N, dv)
# Normalize
k_sum = k.sum(dim=2, keepdim=True) # (B, H, 1, dk)
normalizer = torch.einsum('bhnd,bhkd->bhnk', q, k_sum).clamp(min=self.eps)
out = out / normalizer
else:
# AR/SAT: Causal via cumulative sum
# This is still O(n) but needs sequential computation
kv_cumsum = torch.cumsum(torch.einsum('bhnd,bhnv->bhndv', k, v), dim=2)
k_cumsum = torch.cumsum(k, dim=2)
out = torch.einsum('bhnd,bhndv->bhnv', q, kv_cumsum)
normalizer = torch.einsum('bhnd,bhnd->bhn', q, k_cumsum).unsqueeze(-1).clamp(min=self.eps)
out = out / normalizer
return self.proj(out.transpose(1, 2).reshape(B, N, -1))
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. COSINE ATTENTION - Different similarity metric
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class CosineAttention(nn.Module):
"""
Use cosine similarity instead of dot product.
More stable, bounded [-1, 1] before scaling.
"""
def __init__(self, d: int, h: int, temp: float = 10.0):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
self.temp = nn.Parameter(torch.tensor(temp))
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
# Normalize for cosine similarity
q = F.normalize(q, dim=-1)
k = F.normalize(k, dim=-1)
att = self.temp * (q @ k.transpose(-1, -2)) # Cosine sim scaled by temp
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
z = (att.softmax(-1) @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. DIFFERENTIAL ATTENTION - Noise cancellation
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class DifferentialAttention(nn.Module):
"""
From Microsoft's "Differential Transformer" (2024)
Compute two attention patterns and subtract:
Attn = softmax(Q1 K1^T) - Ξ» * softmax(Q2 K2^T)
Cancels noise, improves signal.
"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
# Two sets of Q, K projections
self.q1 = nn.Linear(d, d, bias=False)
self.k1 = nn.Linear(d, d, bias=False)
self.q2 = nn.Linear(d, d, bias=False)
self.k2 = nn.Linear(d, d, bias=False)
self.v = nn.Linear(d, d, bias=False)
# Learnable lambda for subtraction weight
self.lambda_param = nn.Parameter(torch.tensor(0.5))
self.proj = nn.Linear(d, d, bias=False)
def forward(self, x, mask=None):
B, N, _ = x.shape
q1 = self.q1(x).view(B, N, self.h, self.dk).transpose(1, 2)
k1 = self.k1(x).view(B, N, self.h, self.dk).transpose(1, 2)
q2 = self.q2(x).view(B, N, self.h, self.dk).transpose(1, 2)
k2 = self.k2(x).view(B, N, self.h, self.dk).transpose(1, 2)
v = self.v(x).view(B, N, self.h, self.dk).transpose(1, 2)
scale = math.sqrt(self.dk)
# First attention
att1 = (q1 @ k1.transpose(-1, -2)) / scale
if mask is not None:
att1 = att1 + mask.unsqueeze(0).unsqueeze(0)
att1 = att1.softmax(-1)
# Second attention
att2 = (q2 @ k2.transpose(-1, -2)) / scale
if mask is not None:
att2 = att2 + mask.unsqueeze(0).unsqueeze(0)
att2 = att2.softmax(-1)
# Differential: subtract weighted second from first
lam = torch.sigmoid(self.lambda_param)
att = att1 - lam * att2
# ReLU to ensure non-negative (optional, can remove)
att = F.relu(att)
att = att / (att.sum(dim=-1, keepdim=True) + 1e-6)
z = (att @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. MULTI-QUERY ATTENTION (MQA) - Inference efficient
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class MultiQueryAttention(nn.Module):
"""
MQA: Multiple query heads, single K/V head.
Massive inference speedup (smaller KV cache).
Same training cost as standard.
"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
# H query heads, but only 1 K and 1 V head
self.q = nn.Linear(d, d, bias=False) # H heads
self.k = nn.Linear(d, self.dk, bias=False) # 1 head
self.v = nn.Linear(d, self.dk, bias=False) # 1 head
self.proj = nn.Linear(d, d, bias=False)
def forward(self, x, mask=None):
B, N, _ = x.shape
q = self.q(x).view(B, N, self.h, self.dk).transpose(1, 2) # (B, H, N, dk)
k = self.k(x).view(B, N, 1, self.dk).transpose(1, 2) # (B, 1, N, dk)
v = self.v(x).view(B, N, 1, self.dk).transpose(1, 2) # (B, 1, N, dk)
# K, V broadcast across heads
att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk)
att = att + alibi_bias(self.h, N)
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
z = (att.softmax(-1) @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 6. GROUPED QUERY ATTENTION (GQA) - Between MHA and MQA
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class GroupedQueryAttention(nn.Module):
"""
GQA: Groups of query heads share K/V heads.
Llama 2 uses this. Balance between quality and inference speed.
"""
def __init__(self, d: int, h: int, num_kv_heads: int = 2):
super().__init__()
self.h = h
self.num_kv_heads = num_kv_heads
self.dk = d // h
self.heads_per_group = h // num_kv_heads
self.q = nn.Linear(d, d, bias=False)
self.k = nn.Linear(d, num_kv_heads * self.dk, bias=False)
self.v = nn.Linear(d, num_kv_heads * self.dk, bias=False)
self.proj = nn.Linear(d, d, bias=False)
def forward(self, x, mask=None):
B, N, _ = x.shape
q = self.q(x).view(B, N, self.h, self.dk).transpose(1, 2)
k = self.k(x).view(B, N, self.num_kv_heads, self.dk).transpose(1, 2)
v = self.v(x).view(B, N, self.num_kv_heads, self.dk).transpose(1, 2)
# Repeat K, V for each group
k = k.repeat_interleave(self.heads_per_group, dim=1)
v = v.repeat_interleave(self.heads_per_group, dim=1)
att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk)
att = att + alibi_bias(self.h, N)
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
z = (att.softmax(-1) @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 7. RETENTION - RetNet style
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class RetentionAttention(nn.Module):
"""
From RetNet: Retentive Network
Parallel mode (training): Like linear attention
Recurrent mode (inference): O(1) per step
Key: exponential decay instead of softmax
"""
def __init__(self, d: int, h: int, gamma: float = 0.9):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
# Per-head decay rates
self.gamma = nn.Parameter(torch.ones(h) * gamma)
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
# Build decay matrix D[i,j] = gamma^(i-j) for i >= j
gamma = torch.sigmoid(self.gamma).view(1, self.h, 1, 1)
positions = torch.arange(N, device=x.device).float()
decay = gamma ** (positions.unsqueeze(0) - positions.unsqueeze(1)).clamp(min=0)
# Apply causal mask via decay (future positions get 0)
causal = torch.tril(torch.ones(N, N, device=x.device))
decay = decay * causal.unsqueeze(0).unsqueeze(0)
# If SAT/NAR mask provided, incorporate it
if mask is not None:
mask_binary = (mask == 0).float().unsqueeze(0).unsqueeze(0)
decay = decay * mask_binary
# Retention = (Q @ K^T) * D @ V
att = (q @ k.transpose(-1, -2)) * decay
# Normalize per row
att = att / (att.sum(dim=-1, keepdim=True) + 1e-6)
z = (att @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 8. GATED LINEAR ATTENTION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class GatedLinearAttention(nn.Module):
"""
Linear attention with gating for better gradient flow.
From "Gated Linear Attention Transformers" (2024)
"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.gate = nn.Linear(d, d)
self.proj = nn.Linear(d, d, bias=False)
self.eps = 1e-6
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
# Feature map (ELU + 1 for positivity)
q = F.elu(q) + 1
k = F.elu(k) + 1
if mask is None:
# Bidirectional
kv = torch.einsum('bhnd,bhnv->bhdv', k, v)
out = torch.einsum('bhnd,bhdv->bhnv', q, kv)
normalizer = torch.einsum('bhnd,bhd->bhn', q, k.sum(dim=2)).unsqueeze(-1).clamp(min=self.eps)
else:
# Causal
kv_cumsum = torch.cumsum(torch.einsum('bhnd,bhnv->bhndv', k, v), dim=2)
k_cumsum = torch.cumsum(k, dim=2)
out = torch.einsum('bhnd,bhndv->bhnv', q, kv_cumsum)
normalizer = torch.einsum('bhnd,bhnd->bhn', q, k_cumsum).unsqueeze(-1).clamp(min=self.eps)
out = out / normalizer
out = out.transpose(1, 2).reshape(B, N, -1)
# Gating
gate = torch.sigmoid(self.gate(x))
out = out * gate
return self.proj(out)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 9. RELU ATTENTION - Simpler activation
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class ReLUAttention(nn.Module):
"""
Replace softmax with ReLU + normalization.
Simpler, faster, sometimes works as well.
From "ReLU Attention" papers.
"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk)
att = att + alibi_bias(self.h, N)
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
# ReLU instead of softmax
att = F.relu(att)
att = att / (att.sum(dim=-1, keepdim=True) + 1e-6)
z = (att @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 10. SIGMOID ATTENTION - Bounded
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class SigmoidAttention(nn.Module):
"""
Sigmoid attention: each position independently decides attention weight.
Not normalized to sum to 1 - allows variable "total attention".
"""
def __init__(self, d: int, h: int):
super().__init__()
self.h, self.dk = h, d // h
self.qkv = nn.Linear(d, 3 * d, bias=False)
self.proj = nn.Linear(d, d, bias=False)
self.bias = nn.Parameter(torch.zeros(h, 1, 1))
def forward(self, x, mask=None):
B, N, _ = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.h, self.dk).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2]
att = (q @ k.transpose(-1, -2)) / math.sqrt(self.dk) + self.bias
if mask is not None:
att = att + mask.unsqueeze(0).unsqueeze(0)
# Sigmoid instead of softmax - each weight independent
att = torch.sigmoid(att)
# Optional: mask out future for AR
if mask is not None:
att = att * (mask == 0).float().unsqueeze(0).unsqueeze(0)
z = (att @ v).transpose(1, 2).reshape(B, N, -1)
return self.proj(z)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Block and Model
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ATTN_REGISTRY = {
"standard": StandardAttention,
"linear": LinearAttention,
"cosine": CosineAttention,
"differential": DifferentialAttention,
"mqa": MultiQueryAttention,
"gqa": GroupedQueryAttention,
"retention": RetentionAttention,
"gated_linear": GatedLinearAttention,
"relu": ReLUAttention,
"sigmoid": SigmoidAttention,
}
class Block(nn.Module):
def __init__(self, d: int, h: int, attn_type: str = "standard"):
super().__init__()
self.ln1, self.ln2 = nn.LayerNorm(d), nn.LayerNorm(d)
self.attn = ATTN_REGISTRY[attn_type](d, h)
self.ff = nn.Sequential(nn.Linear(d, 4*d), nn.GELU(), nn.Linear(4*d, d))
def forward(self, x, mask=None):
x = x + self.attn(self.ln1(x), mask)
return x + self.ff(self.ln2(x))
class FlexModel(nn.Module):
def __init__(self, d: int, layers: int, h: int, attn_type: str = "standard"):
super().__init__()
self.emb = nn.Embedding(VOCAB, d)
self.blocks = nn.ModuleList([Block(d, h, attn_type) for _ in range(layers)])
self.ln = nn.LayerNorm(d)
self.head = nn.Linear(d, VOCAB, bias=False)
self.head.weight = self.emb.weight
def forward(self, x, mask=None):
x = self.emb(x)
for b in self.blocks:
x = b(x, mask)
return self.head(self.ln(x))
def count_params(self):
return sum(p.numel() for p in self.parameters())
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Training with AR/SAT/NAR modes
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def train(attn_type: str, mode: str, d: int, layers: int, h: int,
batch: int, seq: int, steps: int, block_size: int = 4):
print(f"\n{'='*60}")
print(f"ATTENTION: {attn_type.upper()} | MODE: {mode.upper()}")
print(f"{'='*60}")
model = FlexModel(d, layers, h, attn_type).to(DEV)
print(f"Parameters: {model.count_params():,}")
opt = torch.optim.AdamW(model.parameters(), lr=1e-4)
losses, times = [], []
for step in range(steps):
ids = torch.randint(0, VOCAB, (batch, seq), device=DEV)
if mode == "ar":
# Standard AR: predict next token
target = ids[:, 1:]
input_ids = ids[:, :-1]
mask = get_mask(seq - 1, "ar")
elif mode == "sat":
# SAT: predict within blocks
target = ids[:, 1:]
input_ids = ids[:, :-1]
mask = get_mask(seq - 1, "sat", block_size)
else: # nar
# NAR: predict all from [MASK] or noisy input
target = ids
# Add noise to input for NAR (simple version)
noise_mask = torch.rand(batch, seq, device=DEV) < 0.15
input_ids = ids.clone()
input_ids[noise_mask] = torch.randint(0, VOCAB, (noise_mask.sum().item(),), device=DEV)
mask = get_mask(seq, "nar")
start = time.time()
opt.zero_grad()
try:
logits = model(input_ids, mask)
loss = F.cross_entropy(logits.view(-1, VOCAB), target.reshape(-1))
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
opt.step()
except Exception as e:
print(f"Step {step} failed: {e}")
return None
elapsed = time.time() - start
losses.append(loss.item())
times.append(elapsed)
if step % 20 == 0 or step == steps - 1:
tok_s = batch * seq / elapsed
print(f"Step {step:3d} | Loss {loss.item():.4f} | {tok_s:.0f} tok/s")
avg_loss = sum(losses[-20:]) / min(20, len(losses))
avg_toks = batch * seq / (sum(times[-20:]) / min(20, len(times)))
return {"attn": attn_type, "mode": mode, "loss": avg_loss, "tok_s": avg_toks}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--d", type=int, default=256)
parser.add_argument("--layers", type=int, default=4)
parser.add_argument("--heads", type=int, default=8)
parser.add_argument("--batch", type=int, default=16)
parser.add_argument("--seq", type=int, default=128)
parser.add_argument("--steps", type=int, default=100)
parser.add_argument("--mode", type=str, default="ar", choices=["ar", "sat", "nar", "all"])
parser.add_argument("--types", type=str, default="all")
args = parser.parse_args()
print(f"Device: {DEV}")
if torch.cuda.is_available():
print(f"GPU: {torch.cuda.get_device_name()}")
if args.types == "all":
types = list(ATTN_REGISTRY.keys())
else:
types = [t.strip() for t in args.types.split(",")]
modes = ["ar", "sat", "nar"] if args.mode == "all" else [args.mode]
results = []
for mode in modes:
for attn_type in types:
r = train(attn_type, mode, args.d, args.layers, args.heads,
args.batch, args.seq, args.steps)
if r:
results.append(r)
torch.cuda.empty_cache()
# Summary
print(f"\n{'='*60}")
print("SUMMARY")
print(f"{'='*60}")
for mode in modes:
print(f"\n--- MODE: {mode.upper()} ---")
mode_results = [r for r in results if r['mode'] == mode]
baseline = next((r for r in mode_results if r['attn'] == 'standard'), None)
for r in sorted(mode_results, key=lambda x: x['loss']):
rel = ""
if baseline and r['attn'] != 'standard':
loss_diff = (baseline['loss'] - r['loss']) / baseline['loss'] * 100
speed_ratio = r['tok_s'] / baseline['tok_s']
rel = f" | vs std: {loss_diff:+.1f}%, {speed_ratio:.2f}x"
print(f"{r['attn']:15s} | Loss {r['loss']:.4f} | {r['tok_s']:6.0f} tok/s{rel}")
if __name__ == "__main__":
main()
|