forked from karpathy/llama2.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_wrap.py
33 lines (28 loc) · 826 Bytes
/
run_wrap.py
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
"""
wrapper around run.c
mostly deals with the sentencepiece encoding/decoding
C code does all the transformer inference of the individual tokens
"""
from tokenizer import Tokenizer
import subprocess
import time
# specify your command
command = ["./run", "out/model.bin"]
# Start the process
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
enc = Tokenizer()
t0 = time.time()
tokens = []
last = ''
for line in proc.stdout:
token = int(line.decode('utf-8').strip())
dec = enc.decode(tokens + [token])
chunk = dec[len(last):]
print(chunk, end='',flush=True)
tokens.append(token)
last = dec
t1 = time.time()
# seeking help: how can we do streaming inference in sentencepiece properly?
# or even delete sentencepiece entirely?
print(f"\nachieved tok/s: {len(tokens) / (t1 - t0)}")
proc.wait()