File size: 1,317 Bytes
b2ad782
aba2c0a
 
 
 
 
b2ad782
aba2c0a
 
 
 
 
 
b2ad782
 
 
 
aba2c0a
 
 
 
 
 
 
 
 
b2ad782
 
aba2c0a
b2ad782
 
 
aba2c0a
 
b2ad782
aba2c0a
 
 
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
"""run point-1. usage: python3 generate.py ["prompt"] [--temp T] [--n N] [--seed S]"""
import json, math, random, struct, sys

BYTES_VOCAB = [10] + list(range(32, 127))
H = {b: max(-1.0, min(1.0, (b - 79) / 47.0)) for b in BYTES_VOCAB}


def load_w(path="model.safetensors"):
    with open(path, "rb") as f:
        (hlen,) = struct.unpack("<Q", f.read(8))
        header = json.loads(f.read(hlen))
        f.seek(8 + hlen)
        (w,) = struct.unpack("<f", f.read(4))
    nparams = sum(math.prod(t["shape"]) for k, t in header.items() if not k.startswith("__"))
    assert nparams == 1, nparams
    return w


def main():
    args = sys.argv[1:]
    prompt = next((a for a in args if not a.startswith("-")), "")
    temp = float(args[args.index("--temp") + 1]) if "--temp" in args else 0.8
    count = int(args[args.index("--n") + 1]) if "--n" in args else 200
    seed = int(args[args.index("--seed") + 1]) if "--seed" in args else 42
    random.seed(seed)

    w = load_w()
    print(f"point-1 | parameters: 1 | w = {w:+.6f}")
    if prompt:
        print(f"prompt  : {prompt!r}")
    weights = [math.exp(w * H[b] / temp) for b in BYTES_VOCAB]
    out = "".join(chr(random.choices(BYTES_VOCAB, weights=weights)[0]) for _ in range(count))
    print("-" * 52)
    print(out)


if __name__ == "__main__":
    main()