File size: 10,246 Bytes
ba0c987 | 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 | """
inference_hf.py β Self-contained inference script for Erebus models on HuggingFace.
This file has zero dependency on the rest of the erebus repo.
Copy it anywhere and run it as long as you have:
pip install torch tiktoken huggingface_hub safetensors
Usage
-----
# From HuggingFace Hub
python inference_hf.py --hf_repo Rzoro/erebus-small --prompt "The future of AI"
# Interactive
python inference_hf.py --hf_repo Rzoro/erebus-small --interactive
"""
from __future__ import annotations
import argparse
import json
import math
from dataclasses import dataclass
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
# ββ Model definition (self-contained copy) ββββββββββββββββββββββββββββββββββββ
@dataclass
class ErebusConfig:
vocab_size: int = 50257
d_model: int = 768
n_heads: int = 12
n_layers: int = 12
d_ff: int = 3072
max_seq_len: int = 1024
dropout: float = 0.0
class RotaryPositionEmbedding(nn.Module):
def __init__(self, head_dim: int, max_seq_len: int = 4096):
super().__init__()
inv_freq = 1.0 / (10000 ** (torch.arange(0, head_dim, 2).float() / head_dim))
positions = torch.arange(max_seq_len).float()
freqs = torch.outer(positions, inv_freq)
cos = freqs.cos().repeat_interleave(2, dim=-1).unsqueeze(0).unsqueeze(0)
sin = freqs.sin().repeat_interleave(2, dim=-1).unsqueeze(0).unsqueeze(0)
self.register_buffer("cos_cached", cos, persistent=False)
self.register_buffer("sin_cached", sin, persistent=False)
@staticmethod
def _rotate_half(x):
x1, x2 = x[..., 0::2], x[..., 1::2]
return torch.stack([-x2, x1], dim=-1).flatten(-2)
def forward(self, q, k):
T = q.size(2)
cos, sin = self.cos_cached[:, :, :T], self.sin_cached[:, :, :T]
return q * cos + self._rotate_half(q) * sin, k * cos + self._rotate_half(k) * sin
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, n_heads, max_seq_len, dropout=0.0):
super().__init__()
self.n_heads = n_heads
self.head_dim = d_model // n_heads
self.q_proj = nn.Linear(d_model, d_model, bias=False)
self.k_proj = nn.Linear(d_model, d_model, bias=False)
self.v_proj = nn.Linear(d_model, d_model, bias=False)
self.out_proj = nn.Linear(d_model, d_model, bias=False)
self.rope = RotaryPositionEmbedding(self.head_dim, max_seq_len)
self._flash = hasattr(F, "scaled_dot_product_attention")
def forward(self, x):
B, T, C = x.shape
def split(t): return t.view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
Q, K, V = split(self.q_proj(x)), split(self.k_proj(x)), split(self.v_proj(x))
Q, K = self.rope(Q, K)
if self._flash:
out = F.scaled_dot_product_attention(Q, K, V, is_causal=True)
else:
scale = math.sqrt(self.head_dim)
scores = (Q @ K.transpose(-2, -1)) / scale
causal = torch.tril(torch.ones(T, T, device=x.device, dtype=torch.bool))
scores = scores.masked_fill(~causal, float("-inf"))
out = torch.softmax(scores, dim=-1) @ V
return self.out_proj(out.transpose(1, 2).contiguous().view(B, T, C))
class SwiGLU(nn.Module):
def __init__(self, d_model, d_ff):
super().__init__()
d_ff = (d_ff // 64) * 64
self.w1 = nn.Linear(d_model, d_ff, bias=False)
self.w3 = nn.Linear(d_model, d_ff, bias=False)
self.w2 = nn.Linear(d_ff, d_model, bias=False)
def forward(self, x):
return self.w2(F.silu(self.w1(x)) * self.w3(x))
class TransformerBlock(nn.Module):
def __init__(self, cfg: ErebusConfig):
super().__init__()
self.norm1 = nn.RMSNorm(cfg.d_model)
self.attn = MultiHeadAttention(cfg.d_model, cfg.n_heads, cfg.max_seq_len)
self.norm2 = nn.RMSNorm(cfg.d_model)
self.ffn = SwiGLU(cfg.d_model, cfg.d_ff)
def forward(self, x):
x = x + self.attn(self.norm1(x))
x = x + self.ffn(self.norm2(x))
return x
class Erebus(nn.Module):
def __init__(self, cfg: ErebusConfig):
super().__init__()
self.cfg = cfg
self.token_emb = nn.Embedding(cfg.vocab_size, cfg.d_model)
self.blocks = nn.ModuleList([TransformerBlock(cfg) for _ in range(cfg.n_layers)])
self.norm = nn.RMSNorm(cfg.d_model)
self.lm_head = nn.Linear(cfg.d_model, cfg.vocab_size, bias=False)
self.lm_head.weight = self.token_emb.weight
@torch.no_grad()
def generate(
self,
input_ids: torch.Tensor,
max_new_tokens: int = 200,
temperature: float = 0.8,
top_k: int = 50,
top_p: float = 0.95,
repetition_penalty: float = 1.2,
eos_token_id: Optional[int] = None,
) -> torch.Tensor:
self.eval()
for _ in range(max_new_tokens):
ctx = input_ids[:, -self.cfg.max_seq_len:]
x = self.token_emb(ctx)
for block in self.blocks:
x = block(x)
logits = self.lm_head(self.norm(x))[:, -1, :]
if repetition_penalty != 1.0:
for tok in input_ids[0].unique():
logits[0, tok] /= repetition_penalty
logits = logits / max(temperature, 1e-8)
if top_k > 0:
cutoff, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < cutoff[:, [-1]]] = float("-inf")
if top_p < 1.0:
sorted_logits, sorted_idx = torch.sort(logits, descending=True)
cum = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
sorted_logits[cum - F.softmax(sorted_logits, dim=-1) > top_p] = float("-inf")
logits.scatter_(1, sorted_idx, sorted_logits)
next_tok = torch.multinomial(F.softmax(logits, dim=-1), num_samples=1)
input_ids = torch.cat([input_ids, next_tok], dim=1)
if eos_token_id is not None and next_tok.item() == eos_token_id:
break
return input_ids
# ββ Loading helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def load_from_hf(repo_id: str, device: torch.device) -> Erebus:
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
print(f"Downloading {repo_id} from HuggingFace Hub β¦")
cfg_path = hf_hub_download(repo_id, "config.json")
weights_path = hf_hub_download(repo_id, "model.safetensors")
with open(cfg_path) as f:
cfg = ErebusConfig(**json.load(f))
model = Erebus(cfg)
model.load_state_dict(load_file(weights_path), strict=False)
model.eval().to(device)
n = sum(p.numel() for p in model.parameters())
print(f"Loaded : {repo_id} ({n/1e6:.1f} M params)\n")
return model
def load_from_checkpoint(path: str, device: torch.device) -> Erebus:
ckpt = torch.load(path, map_location="cpu", weights_only=False)
model = Erebus(ckpt["config"])
model.load_state_dict(ckpt["model_state_dict"])
model.eval().to(device)
n = sum(p.numel() for p in model.parameters())
print(f"Loaded : {path} ({n/1e6:.1f} M params, step={ckpt.get('step','?')})\n")
return model
# ββ CLI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def parse_args():
p = argparse.ArgumentParser(description="Erebus inference β works with local or HF weights.")
src = p.add_mutually_exclusive_group(required=True)
src.add_argument("--hf_repo", help="HuggingFace repo id e.g. Rzoro/erebus-small")
src.add_argument("--checkpoint", help="Local .pt checkpoint path")
inp = p.add_mutually_exclusive_group()
inp.add_argument("--prompt", default=None)
inp.add_argument("--interactive", action="store_true")
p.add_argument("--max_new_tokens", type=int, default=200)
p.add_argument("--temperature", type=float, default=0.8)
p.add_argument("--top_k", type=int, default=50)
p.add_argument("--top_p", type=float, default=0.95)
p.add_argument("--repetition_penalty", type=float, default=1.2)
p.add_argument("--device", default=None)
return p.parse_args()
def main():
import tiktoken
args = parse_args()
device = torch.device(
args.device if args.device
else ("cuda" if torch.cuda.is_available() else "cpu")
)
print(f"Device : {device}")
model = load_from_hf(args.hf_repo, device) if args.hf_repo \
else load_from_checkpoint(args.checkpoint, device)
enc = tiktoken.get_encoding("gpt2")
def run(prompt: str) -> str:
ids = torch.tensor([enc.encode(prompt)], dtype=torch.long).to(device)
out = model.generate(
ids,
max_new_tokens=args.max_new_tokens,
temperature=args.temperature,
top_k=args.top_k,
top_p=args.top_p,
repetition_penalty=args.repetition_penalty,
eos_token_id=enc.eot_token,
)
return enc.decode(out[0].tolist())
if args.interactive:
print("β" * 60)
print("Erebus β interactive mode (quit / Ctrl-C to exit)")
print("β" * 60)
while True:
try:
prompt = input("\nPrompt > ").strip()
except (EOFError, KeyboardInterrupt):
print("\nBye!"); break
if not prompt or prompt.lower() in ("quit", "exit", "q"):
print("Bye!"); break
print("\n" + "β" * 60)
print(run(prompt))
print("β" * 60)
else:
prompt = args.prompt or input("Prompt > ").strip()
print("\n" + "β" * 60)
print(run(prompt))
print("β" * 60)
if __name__ == "__main__":
main()
|