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engine.config import Config
|
| 6 |
+
from xtuner.registry import BUILDER
|
| 7 |
+
from torch.nn.utils.rnn import pad_sequence
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
from mmengine.logging import print_log
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def crop2square(pil_img):
|
| 14 |
+
width, height = pil_img.width, pil_img.height
|
| 15 |
+
short = min(width, height)
|
| 16 |
+
left = (width - short) // 2
|
| 17 |
+
upper = (height - short) // 2
|
| 18 |
+
return pil_img.crop((left, upper, left + short, upper + short))
|
| 19 |
+
|
| 20 |
+
def preprocess_image(image: Image.Image, image_size: int, dtype: torch.dtype):
|
| 21 |
+
"""将 PIL Image 缩放(使用邻近插值)、归一化并返回 [1, C, H, W] Tensor。"""
|
| 22 |
+
# if image.width != image_size or image.height != image_size:
|
| 23 |
+
# # 1) 裁剪中央正方
|
| 24 |
+
# img = crop2square(image)
|
| 25 |
+
# img = img.resize((image_size, image_size))
|
| 26 |
+
# else:
|
| 27 |
+
# img = image
|
| 28 |
+
|
| 29 |
+
img = crop2square(image)
|
| 30 |
+
img = img.resize((image_size, image_size))
|
| 31 |
+
|
| 32 |
+
arr = np.asarray(img).astype(np.float32) / 255.0
|
| 33 |
+
arr = 2 * arr - 1
|
| 34 |
+
tensor = torch.from_numpy(arr).to(dtype=dtype)
|
| 35 |
+
return rearrange(tensor, "h w c -> 1 c h w")
|
| 36 |
+
|
| 37 |
+
def expand2square(pil_img, target_size=1024, background_color=(127, 127, 127)):
|
| 38 |
+
"""
|
| 39 |
+
Resize an image to fit within a square of size target_size x target_size,
|
| 40 |
+
padding with background_color to make it exactly square.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
pil_img (PIL.Image.Image): The input image.
|
| 44 |
+
target_size (int): The desired square resolution.
|
| 45 |
+
background_color (tuple): RGB color to pad with.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
PIL.Image.Image: The resized and padded square image.
|
| 49 |
+
"""
|
| 50 |
+
original_width, original_height = pil_img.size
|
| 51 |
+
scale = min(target_size / original_width, target_size / original_height)
|
| 52 |
+
new_width = int(original_width * scale)
|
| 53 |
+
new_height = int(original_height * scale)
|
| 54 |
+
|
| 55 |
+
# Resize image
|
| 56 |
+
resized_img = pil_img.resize((new_width, new_height), resample=Image.Resampling.BICUBIC)
|
| 57 |
+
|
| 58 |
+
# Create new square background
|
| 59 |
+
new_img = Image.new(pil_img.mode, (target_size, target_size), background_color)
|
| 60 |
+
paste_position = ((target_size - new_width) // 2, (target_size - new_height) // 2)
|
| 61 |
+
new_img.paste(resized_img, paste_position)
|
| 62 |
+
|
| 63 |
+
return new_img
|
| 64 |
+
|
| 65 |
+
def _print_load_result(module_name, missing, unexpected):
|
| 66 |
+
print_log(
|
| 67 |
+
f"[INFO] Loaded {module_name}. missing={len(missing)}, unexpected={len(unexpected)}"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class Inferencer:
|
| 72 |
+
def __init__(
|
| 73 |
+
self, config_file, model_path, image_size=1024, cfg_prompt="Generate an image."
|
| 74 |
+
):
|
| 75 |
+
self.config_file = config_file
|
| 76 |
+
self.cfg = Config.fromfile(self.config_file)
|
| 77 |
+
|
| 78 |
+
self.model_path = model_path
|
| 79 |
+
self.device = "cuda"
|
| 80 |
+
self.image_size = image_size
|
| 81 |
+
self.image_shape = (image_size // 16, image_size // 16)
|
| 82 |
+
self.cfg_prompt = cfg_prompt
|
| 83 |
+
self.model = self.init_model()
|
| 84 |
+
|
| 85 |
+
def init_model(self):
|
| 86 |
+
# config = Config.fromfile(self.config_file)
|
| 87 |
+
# model = BUILDER.build(config.model)
|
| 88 |
+
|
| 89 |
+
model = BUILDER.build(self.cfg.model)
|
| 90 |
+
|
| 91 |
+
if os.path.isdir(self.model_path):
|
| 92 |
+
index_path = os.path.join(self.model_path, "pytorch_model.bin.index.json")
|
| 93 |
+
print_log(
|
| 94 |
+
f"[INFO] Loading sharded Harmon checkpoint from: {self.model_path}"
|
| 95 |
+
)
|
| 96 |
+
state_dict = {}
|
| 97 |
+
with open(index_path, "r") as f:
|
| 98 |
+
index = json.load(f)
|
| 99 |
+
for shard in sorted(set(index["weight_map"].values())):
|
| 100 |
+
shard_path = os.path.join(self.model_path, shard)
|
| 101 |
+
print_log(f"[INFO] Loading shard: {shard_path}")
|
| 102 |
+
state_dict.update(torch.load(shard_path, map_location=self.device))
|
| 103 |
+
else:
|
| 104 |
+
print_log(f"[INFO] Loading full Harmon checkpoint from: {self.model_path}")
|
| 105 |
+
state_dict = torch.load(self.model_path, map_location=self.device)
|
| 106 |
+
|
| 107 |
+
m, u = model.load_state_dict(state_dict, strict=False)
|
| 108 |
+
_print_load_result("Harmon", m, u)
|
| 109 |
+
|
| 110 |
+
# 载入siglip2 weight
|
| 111 |
+
# siglip_proj_path = "/mnt/data_vlm/wangxiaokun/Unified/Harmon_Siglip/Model/400w/stage1/9000/pytorch_model.bin"
|
| 112 |
+
# sl_state = torch.load(
|
| 113 |
+
# siglip_proj_path, map_location=self.device, weights_only=False
|
| 114 |
+
# )
|
| 115 |
+
# if isinstance(sl_state, dict) and "model" in sl_state:
|
| 116 |
+
# sl_state = sl_state["model"]
|
| 117 |
+
# m, u = model.siglip2_proj.load_state_dict(sl_state, strict=False)
|
| 118 |
+
# _print_load_result("SigLIP2", m, u)
|
| 119 |
+
|
| 120 |
+
model = model.to(self.device, dtype=model.dtype)
|
| 121 |
+
model.eval()
|
| 122 |
+
return model
|
| 123 |
+
|
| 124 |
+
def gen_image(
|
| 125 |
+
self,
|
| 126 |
+
raw_prompt,
|
| 127 |
+
images_to_generate=1,
|
| 128 |
+
cfg=3.0,
|
| 129 |
+
num_iter=64,
|
| 130 |
+
cfg_schedule="constant",
|
| 131 |
+
temperature=1.0,
|
| 132 |
+
):
|
| 133 |
+
prompt = self.model.prompt_template["INSTRUCTION"].format(
|
| 134 |
+
input=f"Generate an image: {raw_prompt.strip()}."
|
| 135 |
+
)
|
| 136 |
+
prompts = [prompt] * images_to_generate
|
| 137 |
+
if cfg != 1.0:
|
| 138 |
+
prompts += [
|
| 139 |
+
self.model.prompt_template["INSTRUCTION"].format(input=self.cfg_prompt)
|
| 140 |
+
] * images_to_generate
|
| 141 |
+
|
| 142 |
+
inputs = self.model.tokenizer(
|
| 143 |
+
prompts, add_special_tokens=True, return_tensors="pt", padding=True
|
| 144 |
+
).to(self.device)
|
| 145 |
+
|
| 146 |
+
print(prompts)
|
| 147 |
+
|
| 148 |
+
images = self.model.sample(
|
| 149 |
+
**inputs,
|
| 150 |
+
num_iter=num_iter,
|
| 151 |
+
cfg=cfg,
|
| 152 |
+
cfg_schedule=cfg_schedule,
|
| 153 |
+
temperature=temperature,
|
| 154 |
+
progress=False,
|
| 155 |
+
image_shape=self.image_shape,
|
| 156 |
+
)
|
| 157 |
+
images = rearrange(images, "(n b) c h w -> b n h w c", n=images_to_generate)
|
| 158 |
+
images = (
|
| 159 |
+
torch.clamp(127.5 * images + 128.0, 0, 255)
|
| 160 |
+
.to("cpu", dtype=torch.uint8)
|
| 161 |
+
.numpy()
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
return [Image.fromarray(img) for img in images[0]]
|
| 165 |
+
|
| 166 |
+
def query_image(self, img: Image.Image, prompt=""):
|
| 167 |
+
model = self.model
|
| 168 |
+
tokenizer = model.tokenizer
|
| 169 |
+
special_tokens_dict = {"additional_special_tokens": ["<image>"]}
|
| 170 |
+
tokenizer.add_special_tokens(special_tokens_dict)
|
| 171 |
+
image_token_idx = tokenizer.encode("<image>", add_special_tokens=False)[-1]
|
| 172 |
+
|
| 173 |
+
# preprocess image
|
| 174 |
+
image = img.convert("RGB")
|
| 175 |
+
image = expand2square(image)
|
| 176 |
+
image = torch.from_numpy(np.array(image)).to(
|
| 177 |
+
dtype=model.dtype, device=self.device
|
| 178 |
+
)
|
| 179 |
+
image = rearrange(image, "h w c -> c h w")[None]
|
| 180 |
+
image = 2 * (image / 255) - 1
|
| 181 |
+
|
| 182 |
+
# prepare prompt
|
| 183 |
+
full_prompt = model.prompt_template["INSTRUCTION"].format(
|
| 184 |
+
input="<image>\n" + prompt
|
| 185 |
+
)
|
| 186 |
+
image_length = (self.image_size // 16) ** 2 + 64
|
| 187 |
+
full_prompt = full_prompt.replace("<image>", "<image>" * image_length)
|
| 188 |
+
input_ids = tokenizer.encode(
|
| 189 |
+
full_prompt, add_special_tokens=True, return_tensors="pt"
|
| 190 |
+
).to(self.device)
|
| 191 |
+
|
| 192 |
+
# extract image embedding
|
| 193 |
+
with torch.no_grad():
|
| 194 |
+
_, z_enc = model.extract_visual_feature(model.encode(image))
|
| 195 |
+
inputs_embeds = z_enc.new_zeros(*input_ids.shape, model.llm.config.hidden_size)
|
| 196 |
+
inputs_embeds[input_ids == image_token_idx] = z_enc.flatten(0, 1)
|
| 197 |
+
inputs_embeds[input_ids != image_token_idx] = model.llm.get_input_embeddings()(
|
| 198 |
+
input_ids[input_ids != image_token_idx]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# generate text
|
| 202 |
+
with torch.no_grad():
|
| 203 |
+
output = model.llm.generate(
|
| 204 |
+
inputs_embeds=inputs_embeds,
|
| 205 |
+
use_cache=True,
|
| 206 |
+
do_sample=False,
|
| 207 |
+
max_new_tokens=4096,
|
| 208 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 209 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 210 |
+
)
|
| 211 |
+
# print(tokenizer.decode(output[0]))
|
| 212 |
+
return tokenizer.decode(output[0])
|
| 213 |
+
|
| 214 |
+
# def edit_image(
|
| 215 |
+
# self,
|
| 216 |
+
# img: Image.Image,
|
| 217 |
+
# prompt: str,
|
| 218 |
+
# cfg: float = 2.0,
|
| 219 |
+
# cfg_prompt: str = "Repeat this image.",
|
| 220 |
+
# cfg_schedule="constant",
|
| 221 |
+
# temperature: float = 1.0,
|
| 222 |
+
# grid_size: int = 2,
|
| 223 |
+
# num_iter: int = 64,
|
| 224 |
+
# mode: str = "conditional",
|
| 225 |
+
# ) -> list[Image.Image]:
|
| 226 |
+
|
| 227 |
+
# model = self.model
|
| 228 |
+
# tokenizer = model.tokenizer
|
| 229 |
+
# m = n = self.image_size // 16
|
| 230 |
+
# image_length = m * n + 64
|
| 231 |
+
|
| 232 |
+
# # preprocess image
|
| 233 |
+
# image = img.convert("RGB")
|
| 234 |
+
# original_size = image.size
|
| 235 |
+
# image = image.resize((self.image_size, self.image_size))
|
| 236 |
+
# image = torch.from_numpy(np.array(image)).to(
|
| 237 |
+
# dtype=model.dtype, device=self.device
|
| 238 |
+
# )
|
| 239 |
+
# image = rearrange(image, "h w c -> c h w")[None]
|
| 240 |
+
# image = 2 * (image / 255) - 1
|
| 241 |
+
|
| 242 |
+
# # prepare prompt
|
| 243 |
+
# special_tokens_dict = {"additional_special_tokens": ["<image>"]}
|
| 244 |
+
# tokenizer.add_special_tokens(special_tokens_dict)
|
| 245 |
+
# image_token_idx = tokenizer.encode("<image>", add_special_tokens=False)[-1]
|
| 246 |
+
|
| 247 |
+
# full_prompt = model.prompt_template["INSTRUCTION"].format(
|
| 248 |
+
# input="<image>\n" + prompt
|
| 249 |
+
# )
|
| 250 |
+
# full_prompt = full_prompt.replace("<image>", "<image>" * image_length)
|
| 251 |
+
# input_ids = tokenizer.encode(
|
| 252 |
+
# full_prompt, add_special_tokens=True, return_tensors="pt"
|
| 253 |
+
# )[0].to(self.device)
|
| 254 |
+
|
| 255 |
+
# if cfg != 1.0:
|
| 256 |
+
# null_prompt = model.prompt_template["INSTRUCTION"].format(
|
| 257 |
+
# input="<image>\n" + cfg_prompt
|
| 258 |
+
# )
|
| 259 |
+
# null_prompt = null_prompt.replace("<image>", "<image>" * image_length)
|
| 260 |
+
# null_input_ids = tokenizer.encode(
|
| 261 |
+
# null_prompt, add_special_tokens=True, return_tensors="pt"
|
| 262 |
+
# )[0].to(self.device)
|
| 263 |
+
# attention_mask = pad_sequence(
|
| 264 |
+
# [torch.ones_like(input_ids), torch.ones_like(null_input_ids)],
|
| 265 |
+
# batch_first=True,
|
| 266 |
+
# padding_value=0,
|
| 267 |
+
# ).to(torch.bool)
|
| 268 |
+
# input_ids = pad_sequence(
|
| 269 |
+
# [input_ids, null_input_ids],
|
| 270 |
+
# batch_first=True,
|
| 271 |
+
# padding_value=tokenizer.eos_token_id,
|
| 272 |
+
# )
|
| 273 |
+
# else:
|
| 274 |
+
# input_ids = input_ids[None]
|
| 275 |
+
# attention_mask = torch.ones_like(input_ids).to(torch.bool)
|
| 276 |
+
|
| 277 |
+
# with torch.no_grad():
|
| 278 |
+
# x_enc = model.encode(image).to(model.dtype)
|
| 279 |
+
# x_con, z_enc = model.extract_visual_feature(x_enc)
|
| 280 |
+
|
| 281 |
+
# if cfg != 1.0:
|
| 282 |
+
# z_enc = torch.cat([z_enc, z_enc], dim=0)
|
| 283 |
+
# x_con = torch.cat([x_con, x_con], dim=0)
|
| 284 |
+
|
| 285 |
+
# inputs_embeds = z_enc.new_zeros(*input_ids.shape, model.llm.config.hidden_size)
|
| 286 |
+
# inputs_embeds[input_ids == image_token_idx] = z_enc.flatten(0, 1)
|
| 287 |
+
# inputs_embeds[input_ids != image_token_idx] = model.llm.get_input_embeddings()(
|
| 288 |
+
# input_ids[input_ids != image_token_idx]
|
| 289 |
+
# )
|
| 290 |
+
|
| 291 |
+
# # repeat
|
| 292 |
+
# bsz = grid_size**2
|
| 293 |
+
# x_con = torch.cat([x_con] * bsz)
|
| 294 |
+
# if cfg != 1.0:
|
| 295 |
+
# inputs_embeds = torch.cat(
|
| 296 |
+
# [
|
| 297 |
+
# inputs_embeds[:1].expand(bsz, -1, -1),
|
| 298 |
+
# inputs_embeds[1:].expand(bsz, -1, -1),
|
| 299 |
+
# ]
|
| 300 |
+
# )
|
| 301 |
+
# attention_mask = torch.cat(
|
| 302 |
+
# [
|
| 303 |
+
# attention_mask[:1].expand(bsz, -1),
|
| 304 |
+
# attention_mask[1:].expand(bsz, -1),
|
| 305 |
+
# ]
|
| 306 |
+
# )
|
| 307 |
+
# else:
|
| 308 |
+
# inputs_embeds = inputs_embeds.expand(bsz, -1, -1)
|
| 309 |
+
# attention_mask = attention_mask.expand(bsz, -1)
|
| 310 |
+
|
| 311 |
+
# # sample
|
| 312 |
+
# with torch.no_grad():
|
| 313 |
+
# if mode == "conditional":
|
| 314 |
+
# samples = model.sample(
|
| 315 |
+
# inputs_embeds=inputs_embeds,
|
| 316 |
+
# attention_mask=attention_mask,
|
| 317 |
+
# num_iter=num_iter,
|
| 318 |
+
# cfg=cfg,
|
| 319 |
+
# cfg_schedule=cfg_schedule,
|
| 320 |
+
# temperature=temperature,
|
| 321 |
+
# progress=False,
|
| 322 |
+
# image_shape=(m, n),
|
| 323 |
+
# x_con=x_con,
|
| 324 |
+
# )
|
| 325 |
+
# else:
|
| 326 |
+
# samples = model.sample(
|
| 327 |
+
# inputs_embeds=inputs_embeds,
|
| 328 |
+
# attention_mask=attention_mask,
|
| 329 |
+
# num_iter=num_iter,
|
| 330 |
+
# cfg=cfg,
|
| 331 |
+
# cfg_schedule=cfg_schedule,
|
| 332 |
+
# temperature=temperature,
|
| 333 |
+
# progress=False,
|
| 334 |
+
# image_shape=(m, n),
|
| 335 |
+
# )
|
| 336 |
+
|
| 337 |
+
# samples = rearrange(
|
| 338 |
+
# samples, "(m n) c h w -> (m h) (n w) c", m=grid_size, n=grid_size
|
| 339 |
+
# )
|
| 340 |
+
# samples = (
|
| 341 |
+
# torch.clamp(127.5 * samples + 128.0, 0, 255)
|
| 342 |
+
# .to("cpu", dtype=torch.uint8)
|
| 343 |
+
# .numpy()
|
| 344 |
+
# )
|
| 345 |
+
|
| 346 |
+
# output_image = Image.fromarray(samples).resize(
|
| 347 |
+
# (original_size[0] * grid_size, original_size[1] * grid_size)
|
| 348 |
+
# )
|
| 349 |
+
# return [output_image]
|
| 350 |
+
|
| 351 |
+
def edit_image(
|
| 352 |
+
self,
|
| 353 |
+
source_image: Image.Image,
|
| 354 |
+
prompt: str,
|
| 355 |
+
num_iter: int = 48,
|
| 356 |
+
cfg: float = 3.0,
|
| 357 |
+
cfg_prompt: str = "Repeat this image.",
|
| 358 |
+
cfg_schedule: str = "constant",
|
| 359 |
+
temperature: float = 0.85,
|
| 360 |
+
grid_size: int = 1
|
| 361 |
+
) -> Image.Image:
|
| 362 |
+
"""Edit single image based on prompt."""
|
| 363 |
+
|
| 364 |
+
model = self.model
|
| 365 |
+
tokenizer = model.tokenizer
|
| 366 |
+
special_tokens_dict = {"additional_special_tokens": ["<image>"]}
|
| 367 |
+
tokenizer.add_special_tokens(special_tokens_dict)
|
| 368 |
+
image_token_idx = tokenizer.encode("<image>", add_special_tokens=False)[-1]
|
| 369 |
+
device = "cuda"
|
| 370 |
+
# 1) Preprocess source image
|
| 371 |
+
img_tensor = preprocess_image(source_image, self.image_size, model.dtype).to(device)
|
| 372 |
+
|
| 373 |
+
# 2) Encode image and extract features
|
| 374 |
+
with torch.no_grad():
|
| 375 |
+
x_enc = self.model.encode(img_tensor)
|
| 376 |
+
x_con, z_enc = self.model.extract_visual_feature(x_enc)
|
| 377 |
+
|
| 378 |
+
# 3) Prepare text prompts
|
| 379 |
+
m = n = self.image_size // 16
|
| 380 |
+
image_length = m * n + 64
|
| 381 |
+
|
| 382 |
+
if hasattr(self.cfg.model, 'prompt_template'):
|
| 383 |
+
prompt_str = self.cfg.model.prompt_template['INSTRUCTION'].format(
|
| 384 |
+
input="<image>\n" + prompt.strip()
|
| 385 |
+
)
|
| 386 |
+
cfg_prompt_str = self.cfg.model.prompt_template['INSTRUCTION'].format(
|
| 387 |
+
input="<image>\n" + cfg_prompt.strip()
|
| 388 |
+
)
|
| 389 |
+
else:
|
| 390 |
+
prompt_str = f"<image>\n{prompt.strip()}"
|
| 391 |
+
cfg_prompt_str = f"<image>\n{cfg_prompt.strip()}"
|
| 392 |
+
|
| 393 |
+
# Replace <image> token with multiple tokens
|
| 394 |
+
prompt_str = prompt_str.replace('<image>', '<image>' * image_length)
|
| 395 |
+
cfg_prompt_str = cfg_prompt_str.replace('<image>', '<image>' * image_length)
|
| 396 |
+
|
| 397 |
+
# 4) Tokenize and prepare inputs
|
| 398 |
+
input_ids = self.model.tokenizer.encode(
|
| 399 |
+
prompt_str, add_special_tokens=True, return_tensors='pt')[0].cuda()
|
| 400 |
+
|
| 401 |
+
if cfg != 1.0:
|
| 402 |
+
null_input_ids = self.model.tokenizer.encode(
|
| 403 |
+
cfg_prompt_str, add_special_tokens=True, return_tensors='pt')[0].cuda()
|
| 404 |
+
attention_mask = pad_sequence(
|
| 405 |
+
[torch.ones_like(input_ids), torch.ones_like(null_input_ids)],
|
| 406 |
+
batch_first=True, padding_value=0).to(torch.bool)
|
| 407 |
+
input_ids = pad_sequence(
|
| 408 |
+
[input_ids, null_input_ids],
|
| 409 |
+
batch_first=True, padding_value=self.model.tokenizer.eos_token_id)
|
| 410 |
+
else:
|
| 411 |
+
input_ids = input_ids[None]
|
| 412 |
+
attention_mask = torch.ones_like(input_ids).to(torch.bool)
|
| 413 |
+
|
| 414 |
+
# 5) Prepare embeddings
|
| 415 |
+
if cfg != 1.0:
|
| 416 |
+
z_enc = torch.cat([z_enc, z_enc], dim=0)
|
| 417 |
+
x_con = torch.cat([x_con, x_con], dim=0)
|
| 418 |
+
|
| 419 |
+
inputs_embeds = z_enc.new_zeros(*input_ids.shape, self.model.llm.config.hidden_size)
|
| 420 |
+
#debug:目前这里报错
|
| 421 |
+
inputs_embeds[input_ids == image_token_idx] = z_enc.flatten(0, 1)
|
| 422 |
+
inputs_embeds[input_ids != image_token_idx] = self.model.llm.get_input_embeddings()(
|
| 423 |
+
input_ids[input_ids != image_token_idx]
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
# 6) Repeat for grid sampling
|
| 427 |
+
bsz = grid_size ** 2
|
| 428 |
+
x_con = torch.cat([x_con] * bsz)
|
| 429 |
+
if cfg != 1.0:
|
| 430 |
+
inputs_embeds = torch.cat([
|
| 431 |
+
inputs_embeds[:1].expand(bsz, -1, -1),
|
| 432 |
+
inputs_embeds[1:].expand(bsz, -1, -1),
|
| 433 |
+
])
|
| 434 |
+
attention_mask = torch.cat([
|
| 435 |
+
attention_mask[:1].expand(bsz, -1),
|
| 436 |
+
attention_mask[1:].expand(bsz, -1),
|
| 437 |
+
])
|
| 438 |
+
else:
|
| 439 |
+
inputs_embeds = inputs_embeds.expand(bsz, -1, -1)
|
| 440 |
+
attention_mask = attention_mask.expand(bsz, -1)
|
| 441 |
+
|
| 442 |
+
# 7) Sampling
|
| 443 |
+
samples = self.model.sample(
|
| 444 |
+
inputs_embeds=inputs_embeds,
|
| 445 |
+
attention_mask=attention_mask,
|
| 446 |
+
num_iter=num_iter,
|
| 447 |
+
cfg=cfg,
|
| 448 |
+
cfg_schedule=cfg_schedule,
|
| 449 |
+
temperature=temperature,
|
| 450 |
+
progress=False,
|
| 451 |
+
image_shape=(m, n),
|
| 452 |
+
x_con=x_con
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# 9) Convert to PIL Image
|
| 456 |
+
samples = rearrange(samples, '(m n) c h w -> (m h) (n w) c', m=grid_size, n=grid_size)
|
| 457 |
+
samples = torch.clamp(127.5 * samples + 128.0, 0, 255)
|
| 458 |
+
out = samples.to("cpu", torch.uint8).numpy()
|
| 459 |
+
|
| 460 |
+
return [ Image.fromarray(out) ]
|
| 461 |
+
|
| 462 |
+
def query_text(self, prompt=""):
|
| 463 |
+
model = self.model
|
| 464 |
+
tokenizer = model.tokenizer
|
| 465 |
+
|
| 466 |
+
# 构造文本 prompt
|
| 467 |
+
full_prompt = model.prompt_template["INSTRUCTION"].format(input=prompt)
|
| 468 |
+
input_ids = tokenizer.encode(
|
| 469 |
+
full_prompt, add_special_tokens=True, return_tensors="pt"
|
| 470 |
+
).to(self.device)
|
| 471 |
+
|
| 472 |
+
# 生成回复
|
| 473 |
+
with torch.no_grad():
|
| 474 |
+
output = model.llm.generate(
|
| 475 |
+
input_ids=input_ids,
|
| 476 |
+
use_cache=True,
|
| 477 |
+
do_sample=True,
|
| 478 |
+
max_new_tokens=1024,
|
| 479 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 480 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
res = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 484 |
+
# print(f"Query Text Output: {res}")
|
| 485 |
+
return res
|