-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
75 lines (61 loc) · 2.65 KB
/
Copy pathgenerate.py
File metadata and controls
75 lines (61 loc) · 2.65 KB
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
"""One-shot generation with nanoserve: cached decode + sampling, from the CLI.
Run from the repo root with the venv python:
cd ~/nanoserve && .venv/bin/python generate.py "The test of a"
cd ~/nanoserve && .venv/bin/python generate.py "Once upon a time" \
--temperature 0.8 --top-p 0.95 --seed 0 --max-new-tokens 80
This is the non-interactive sibling of chat.py. It prefills the prompt into the
KV cache (Day 11) once, then streams sampled tokens (Day 10) to stdout. Pass
`--temperature 0` for greedy decode (the zero-temperature corner of the sampler).
"""
import argparse
import sys
import time
import torch
from nanoserve.config import ModelConfig
from nanoserve.loader import load_weights
from nanoserve.model import LlamaModel
try:
from transformers import AutoTokenizer
except ImportError:
sys.exit("transformers not installed in this interpreter; use .venv/bin/python")
def main() -> None:
p = argparse.ArgumentParser(description="one-shot nanoserve generation")
p.add_argument("prompt", help="the text to continue")
p.add_argument("--max-new-tokens", type=int, default=60)
p.add_argument("--temperature", type=float, default=0.8, help="0 = greedy")
p.add_argument("--top-k", type=int, default=0, help="0 = off")
p.add_argument("--top-p", type=float, default=0.95, help="1.0 = off")
p.add_argument("--seed", type=int, default=None, help="reproducible sampling")
p.add_argument("--weights", default="weights", help="path to the weights dir")
args = p.parse_args()
print("loading nanoserve (Llama-3.2-1B)...", file=sys.stderr, flush=True)
model = LlamaModel(ModelConfig.from_json(args.weights), load_weights(args.weights))
tok = AutoTokenizer.from_pretrained(args.weights)
ids = tok(args.prompt, return_tensors="pt").input_ids
sys.stdout.write(tok.decode(ids[0], skip_special_tokens=True))
sys.stdout.flush()
n = 0
t0 = time.perf_counter()
out = ids
for nxt in model.generate_stream(
ids,
max_new_tokens=args.max_new_tokens,
temperature=args.temperature,
top_k=args.top_k,
top_p=args.top_p,
eos_id=tok.eos_token_id,
seed=args.seed,
):
if nxt == tok.eos_token_id:
break
prev = tok.decode(out[0], skip_special_tokens=True)
out = torch.cat([out, torch.tensor([[nxt]])], dim=1)
full = tok.decode(out[0], skip_special_tokens=True)
sys.stdout.write(full[len(prev):])
sys.stdout.flush()
n += 1
dt = time.perf_counter() - t0
rate = n / dt if dt else 0.0
print(f"\n\n[{n} tokens in {dt:.1f}s, {rate:.2f} tok/s]", file=sys.stderr)
if __name__ == "__main__":
main()