-
Notifications
You must be signed in to change notification settings - Fork 19
/
latency_profile.py
174 lines (153 loc) · 5.66 KB
/
latency_profile.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import argparse
import copy
import datetime
import os
import random
import time
import numpy as np
import torch
import torchvision
from PIL import Image
from tqdm import tqdm
from transformers import (
AutoConfig,
AutoModel,
AutoTokenizer,
HfArgumentParser,
set_seed,
)
from hart.modules.models.transformer import HARTForT2I
from hart.utils import default_prompts, encode_prompts, llm_system_prompt
def save_images(sample_imgs, sample_folder_dir, store_separately, prompts):
if not store_separately and len(sample_imgs) > 1:
grid = torchvision.utils.make_grid(sample_imgs, nrow=12)
grid_np = grid.to(torch.float16).permute(1, 2, 0).mul_(255).cpu().numpy()
os.makedirs(sample_folder_dir, exist_ok=True)
grid_np = Image.fromarray(grid_np.astype(np.uint8))
grid_np.save(os.path.join(sample_folder_dir, f"sample_images.png"))
print(f"Example images are saved to {sample_folder_dir}")
else:
# bs, 3, r, r
sample_imgs_np = sample_imgs.mul_(255).cpu().numpy()
num_imgs = sample_imgs_np.shape[0]
os.makedirs(sample_folder_dir, exist_ok=True)
for img_idx in range(num_imgs):
cur_img = sample_imgs_np[img_idx]
cur_img = cur_img.transpose(1, 2, 0).astype(np.uint8)
cur_img_store = Image.fromarray(cur_img)
cur_img_store.save(os.path.join(sample_folder_dir, f"{img_idx:06d}.png"))
print(f"Image {img_idx} saved.")
with open(os.path.join(sample_folder_dir, "prompt.txt"), "w") as f:
f.write("\n".join(prompts))
def main(args):
device = torch.device("cuda")
model = AutoModel.from_pretrained(args.model_path)
model = model.to(device)
model.eval()
if args.use_ema:
ema_model = copy.deepcopy(model)
ema_model.load_state_dict(
torch.load(os.path.join(args.model_path, "ema_model.bin"))
)
text_tokenizer = AutoTokenizer.from_pretrained(args.text_model_path)
text_model = AutoModel.from_pretrained(args.text_model_path).to(device)
text_model.eval()
text_tokenizer_max_length = args.max_token_length
prompts = random.sample(default_prompts, args.batch_size)
with torch.inference_mode():
with torch.autocast(
"cuda", enabled=True, dtype=torch.float16, cache_enabled=True
):
(
context_tokens,
context_mask,
context_position_ids,
context_tensor,
) = encode_prompts(
prompts,
text_model,
text_tokenizer,
args.max_token_length,
llm_system_prompt,
args.use_llm_system_prompt,
)
infer_func = (
ema_model.autoregressive_infer_cfg
if args.use_ema
else model.autoregressive_infer_cfg
)
# warmup
for _ in tqdm(range(args.warmup_iter)):
output_imgs = infer_func(
B=context_tensor.size(0),
label_B=context_tensor,
cfg=args.cfg,
g_seed=args.seed,
more_smooth=args.more_smooth,
context_position_ids=context_position_ids,
context_mask=context_mask,
)
# latency profile
start_time = time.time()
for _ in tqdm(range(args.profile_iter)):
(
context_tokens,
context_mask,
context_position_ids,
context_tensor,
) = encode_prompts(
prompts,
text_model,
text_tokenizer,
args.max_token_length,
llm_system_prompt,
args.use_llm_system_prompt,
)
output_imgs = infer_func(
B=context_tensor.size(0),
label_B=context_tensor,
cfg=args.cfg,
g_seed=args.seed,
more_smooth=args.more_smooth,
context_position_ids=context_position_ids,
context_mask=context_mask,
)
total_time = time.time() - start_time
average_time = total_time / args.profile_iter
print(
f"Generation with batch_size = {args.batch_size} take {average_time:2f}s per step."
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_path",
type=str,
help="The path to HART model.",
default="pretrained_models/HART-1024",
)
parser.add_argument(
"--text_model_path",
type=str,
help="The path to text model, we employ Qwen2-VL-1.5B-Instruct by default.",
default="Qwen2-VL-1.5B-Instruct",
)
parser.add_argument(
"--batch_size", type=int, help="Generation batch size", default=1
)
parser.add_argument("--seed", type=int, default=1)
parser.add_argument("--use_ema", type=bool, default=True)
parser.add_argument("--max_token_length", type=int, default=300)
parser.add_argument("--use_llm_system_prompt", type=bool, default=True)
parser.add_argument(
"--cfg", type=float, help="Classifier-free guidance scale.", default=4.5
)
parser.add_argument(
"--more_smooth",
type=bool,
help="Turn on for more visually smooth samples.",
default=True,
)
parser.add_argument("--warmup_iter", type=int, default=50)
parser.add_argument("--profile_iter", type=int, default=100)
args = parser.parse_args()
main(args)