File size: 3,151 Bytes
6071ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Side-by-side conversation samples for the model card: the same everyday prompts and scripted multi-turn dialogues for
several checkpoints (the model's own replies are fed back; the user turns are fixed). Greedy + repetition penalty 1.1.
  python convo_examples.py <label>=<ckpt> [<label>=<ckpt> ...] --out results/convo/<name>.json
"""
import argparse
import json
from pathlib import Path

import torch
from tokenizers import Tokenizer

from chatfmt import Fmt
from model_io import generate, load

SINGLE = ["Cze艣膰! Co u ciebie s艂ycha膰?", "Nie mog臋 zasn膮膰, masz jaki艣 spos贸b?", "Polecisz mi jak膮艣 ksi膮偶k臋 na weekend?",
          "Jak ugotowa膰 dobr膮 jajecznic臋?", "Mam jutro rozmow臋 o prac臋 i troch臋 si臋 stresuj臋.", "Co warto zobaczy膰 w Krakowie w jeden dzie艅?",
          "Hi! What do you like to do on weekends?", "Can you explain what a black hole is, simply?", "I forgot my friend's birthday. What should I do?",
          "Give me a quick idea for a healthy dinner."]
DIALOGUES = [["Hej, planuj臋 wycieczk臋 w g贸ry w pa藕dzierniku.", "A co powinienem zabra膰 ze sob膮?", "Dzi臋ki! A jak d艂ugo trwa wej艣cie na Giewont?"],
             ["Ucz臋 si臋 programowa膰 w Pythonie od tygodnia.", "Co to jest p臋tla for? Wyja艣nij pro艣ciej.", "Mo偶esz da膰 ma艂y przyk艂ad?"],
             ["I want to start running, but I get tired after two minutes.", "How often should I train in the first month?", "Thanks! Any tips for staying motivated?"],
             ["M贸j kot ci膮gle drapie kanap臋.", "Pr贸bowa艂em drapaka, ale go ignoruje.", "Dobra, spr贸buj臋. Dzi臋ki!"]]


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("models", nargs="+")
    ap.add_argument("--out", required=True)
    ap.add_argument("--max-new", type=int, default=300)
    a = ap.parse_args()
    f = Fmt(Tokenizer.from_file("/workspace/ckpt/tokenizer.json"))
    stop = {f.ime, f.bos, f.ims}
    res = {"settings": {"decoding": "greedy", "repetition_penalty": 1.1, "max_new_tokens": a.max_new}, "single": {}, "dialogues": {}}
    for spec in a.models:
        lab, ck = spec.split("=", 1)
        m, _, _ = load(ck, device="cuda", dtype=torch.bfloat16)
        m.eval()

        def say(msgs):
            o, why = generate(m, f.prompt_ids(msgs, None), stop_ids=stop, max_new_tokens=a.max_new, repetition_penalty=1.1, device="cuda")
            return f.tok.decode(o, skip_special_tokens=False).strip() + ("" if why == "stop" else " [鈥ut at token limit]")
        res["single"][lab] = [{"user": p, "assistant": say([{"role": "user", "content": p}])} for p in SINGLE]
        res["dialogues"][lab] = []
        for d in DIALOGUES:
            msgs = []
            for u in d:
                msgs.append({"role": "user", "content": u})
                msgs.append({"role": "assistant", "content": say(msgs)})
            res["dialogues"][lab].append(msgs)
        del m
        torch.cuda.empty_cache()
        print("done", lab, flush=True)
    Path(a.out).parent.mkdir(parents=True, exist_ok=True)
    Path(a.out).write_text(json.dumps(res, indent=1, ensure_ascii=False), encoding="utf-8")


if __name__ == "__main__":
    main()