-
Notifications
You must be signed in to change notification settings - Fork 6
/
tipo-test.py
160 lines (136 loc) · 4.36 KB
/
tipo-test.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
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
import re
import random
from time import time
import torch
from transformers import AutoTokenizer, logging
import kgen.models as models
import kgen.executor.tipo as tipo
from kgen.formatter import seperate_tags, apply_format
from kgen.generate import generate
DEFAULT_FORMAT = """<|special|>, <|characters|>, <|copyrights|>,
<|artist|>,
<|extended|>.
<|general|>,
<|generated|>.
<|quality|>, <|meta|>, <|rating|>
"""
tipo.BAN_TAGS = [
"background",
"name",
"text",
"joke",
"costume",
"alternative",
"speech",
"stickers",
"hat",
]
logging.set_verbosity_error()
print(f"threads: {torch.get_num_threads()} {torch.get_num_interop_threads()}")
clip_tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32")
t5_tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pile-t5-large")
# models.load_model("Amber-River/tipo", device="cuda", subfolder="500M-epoch3")
models.load_model(
"TIPO-500M_epoch5-F16.gguf",
gguf=True,
device="cuda",
main_gpu=0,
)
generate(max_new_tokens=4)
# tracer = VizTracer()
# tracer.start()
# generate(max_new_tokens=16)
# tracer.stop()
# tracer.save()
# with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], record_shapes=True) as prof:
# generate(
# max_new_tokens=16,
# )
# prof.export_chrome_trace("tipo-test.json")
# exit()
tags = nl_prompt = ""
tags = """
""".strip()
# nl_prompt = ""
# tags = """
# masterpiece, scenery, absurdres, safe, newest, no humans, cyberpunk
# """
nl_prompt = """
An illustration of a girl
""".strip()
def task(tags, nl_prompt):
width = 832
height = 1216
meta, operations, general, nl_prompt = tipo.parse_tipo_request(
seperate_tags(tags.split(",")),
nl_prompt,
tag_length_target="long",
generate_extra_nl_prompt=not nl_prompt,
)
meta["aspect_ratio"] = f"{width / height:.1f}"
result, timing = tipo.tipo_runner(meta, operations, general, nl_prompt)
formatted = re.sub(r"([()\[\]])", r"\\\1", apply_format(result, DEFAULT_FORMAT))
return formatted, timing
if __name__ == "__main__":
clip_tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32")
t5_tokenizer = AutoTokenizer.from_pretrained("EleutherAI/pile-t5-large")
test = 1
start = time()
for _ in range(test):
t0 = time()
formatted, timing = task(tags, nl_prompt)
t1 = time()
finish = time()
print(f"Total cost {(finish - start)}s | {test} iter")
print(timing)
print("=" * 87)
print("=" * 40, "INPUT", "=" * 40)
print()
if tags.strip().strip("\n"):
print(tags.strip().strip("\n"))
print()
if nl_prompt.strip().strip("\n"):
print(nl_prompt.strip().strip("\n"))
print()
print("=" * 40, "OUTPUT", "=" * 39)
print()
print(formatted.strip())
print()
print("=" * 87)
print()
timing["total"] = t1 - t0
total = timing["total"]
generate_pass = timing["generate_pass"]
print(
f"""Process Time:
Total || {total:5.2f} sec / {generate_pass:5} Passes | {generate_pass/total:7.2f} Passes Per Second
"""
)
if "generated_tokens" in timing:
total_generated_tokens = timing["generated_tokens"]
total_input_tokens = timing["input_tokens"]
print(
f"""Processed Tokens:
{total_input_tokens:} Input Tokens
{total_generated_tokens:} Output Tokens
"""
)
if "generated_tokens" in timing and "total_sampling" in timing:
sampling_time = timing["total_sampling"] / 1000
process_time = timing["prompt_process"] / 1000
model_time = timing["total_eval"] / 1000
print(
f""" Process || {process_time:5.2f} sec / {total_input_tokens:5} Tokens | {total_input_tokens/process_time:7.2f} Tokens Per Second
Sampling || {sampling_time:5.2f} sec / {total_generated_tokens:5} Tokens | {total_generated_tokens/sampling_time:7.2f} Tokens Per Second
Eval || {model_time:5.2f} sec / {total_generated_tokens:5} Tokens | {total_generated_tokens/model_time:7.2f} Tokens Per Second
"""
)
formatted_clip_tokens = len(clip_tokenizer(formatted)["input_ids"])
formatted_t5_tokens = len(t5_tokenizer(formatted)["input_ids"])
print(
f"""Length of Formatted Prompt:
{formatted_clip_tokens} CLIP tokens
{formatted_t5_tokens} T5 tokens
"""
)
print("=" * 87)