Spaces:
Running
Running
Simplify code
Browse files- diffusers_helper/hunyuan.py +111 -111
diffusers_helper/hunyuan.py
CHANGED
|
@@ -1,111 +1,111 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
from diffusers.pipelines.hunyuan_video.pipeline_hunyuan_video import DEFAULT_PROMPT_TEMPLATE
|
| 4 |
-
from diffusers_helper.utils import crop_or_pad_yield_mask
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
@torch.no_grad()
|
| 8 |
-
def encode_prompt_conds(prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2, max_length=256):
|
| 9 |
-
assert isinstance(prompt, str)
|
| 10 |
-
|
| 11 |
-
prompt = [prompt]
|
| 12 |
-
|
| 13 |
-
# LLAMA
|
| 14 |
-
|
| 15 |
-
prompt_llama = [DEFAULT_PROMPT_TEMPLATE["template"].format(p) for p in prompt]
|
| 16 |
-
crop_start = DEFAULT_PROMPT_TEMPLATE["crop_start"]
|
| 17 |
-
|
| 18 |
-
llama_inputs = tokenizer(
|
| 19 |
-
prompt_llama,
|
| 20 |
-
padding="max_length",
|
| 21 |
-
max_length=max_length + crop_start,
|
| 22 |
-
truncation=True,
|
| 23 |
-
return_tensors="pt",
|
| 24 |
-
return_length=False,
|
| 25 |
-
return_overflowing_tokens=False,
|
| 26 |
-
return_attention_mask=True,
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
llama_input_ids = llama_inputs.input_ids.to(text_encoder.device)
|
| 30 |
-
llama_attention_mask = llama_inputs.attention_mask.to(text_encoder.device)
|
| 31 |
-
llama_attention_length = int(llama_attention_mask.sum())
|
| 32 |
-
|
| 33 |
-
llama_outputs = text_encoder(
|
| 34 |
-
input_ids=llama_input_ids,
|
| 35 |
-
attention_mask=llama_attention_mask,
|
| 36 |
-
output_hidden_states=True,
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
llama_vec = llama_outputs.hidden_states[-3][:, crop_start:llama_attention_length]
|
| 40 |
-
# llama_vec_remaining = llama_outputs.hidden_states[-3][:, llama_attention_length:]
|
| 41 |
-
llama_attention_mask = llama_attention_mask[:, crop_start:llama_attention_length]
|
| 42 |
-
|
| 43 |
-
assert torch.all(llama_attention_mask.bool())
|
| 44 |
-
|
| 45 |
-
# CLIP
|
| 46 |
-
|
| 47 |
-
clip_l_input_ids = tokenizer_2(
|
| 48 |
-
prompt,
|
| 49 |
-
padding="max_length",
|
| 50 |
-
max_length=77,
|
| 51 |
-
truncation=True,
|
| 52 |
-
return_overflowing_tokens=False,
|
| 53 |
-
return_length=False,
|
| 54 |
-
return_tensors="pt",
|
| 55 |
-
).input_ids
|
| 56 |
-
clip_l_pooler = text_encoder_2(clip_l_input_ids.to(text_encoder_2.device), output_hidden_states=False).pooler_output
|
| 57 |
-
|
| 58 |
-
return llama_vec, clip_l_pooler
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@torch.no_grad()
|
| 62 |
-
def vae_decode_fake(latents):
|
| 63 |
-
latent_rgb_factors = [
|
| 64 |
-
[-0.0395, -0.0331, 0.0445],
|
| 65 |
-
[0.0696, 0.0795, 0.0518],
|
| 66 |
-
[0.0135, -0.0945, -0.0282],
|
| 67 |
-
[0.0108, -0.0250, -0.0765],
|
| 68 |
-
[-0.0209, 0.0032, 0.0224],
|
| 69 |
-
[-0.0804, -0.0254, -0.0639],
|
| 70 |
-
[-0.0991, 0.0271, -0.0669],
|
| 71 |
-
[-0.0646, -0.0422, -0.0400],
|
| 72 |
-
[-0.0696, -0.0595, -0.0894],
|
| 73 |
-
[-0.0799, -0.0208, -0.0375],
|
| 74 |
-
[0.1166, 0.1627, 0.0962],
|
| 75 |
-
[0.1165, 0.0432, 0.0407],
|
| 76 |
-
[-0.2315, -0.1920, -0.1355],
|
| 77 |
-
[-0.0270, 0.0401, -0.0821],
|
| 78 |
-
[-0.0616, -0.0997, -0.0727],
|
| 79 |
-
[0.0249, -0.0469, -0.1703]
|
| 80 |
-
] # From comfyui
|
| 81 |
-
|
| 82 |
-
latent_rgb_factors_bias = [0.0259, -0.0192, -0.0761]
|
| 83 |
-
|
| 84 |
-
weight = torch.tensor(latent_rgb_factors, device=latents.device, dtype=latents.dtype).transpose(0, 1)[:, :, None, None, None]
|
| 85 |
-
bias = torch.tensor(latent_rgb_factors_bias, device=latents.device, dtype=latents.dtype)
|
| 86 |
-
|
| 87 |
-
images = torch.nn.functional.conv3d(latents, weight, bias=bias, stride=1, padding=0, dilation=1, groups=1)
|
| 88 |
-
images = images.clamp(0.0, 1.0)
|
| 89 |
-
|
| 90 |
-
return images
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
@torch.no_grad()
|
| 94 |
-
def vae_decode(latents, vae, image_mode=False):
|
| 95 |
-
latents = latents / vae.config.scaling_factor
|
| 96 |
-
|
| 97 |
-
if
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
image =
|
| 103 |
-
|
| 104 |
-
return image
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
@torch.no_grad()
|
| 108 |
-
def vae_encode(image, vae):
|
| 109 |
-
latents = vae.encode(image.to(device=vae.device, dtype=vae.dtype)).latent_dist.sample()
|
| 110 |
-
latents = latents * vae.config.scaling_factor
|
| 111 |
-
return latents
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from diffusers.pipelines.hunyuan_video.pipeline_hunyuan_video import DEFAULT_PROMPT_TEMPLATE
|
| 4 |
+
from diffusers_helper.utils import crop_or_pad_yield_mask
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@torch.no_grad()
|
| 8 |
+
def encode_prompt_conds(prompt, text_encoder, text_encoder_2, tokenizer, tokenizer_2, max_length=256):
|
| 9 |
+
assert isinstance(prompt, str)
|
| 10 |
+
|
| 11 |
+
prompt = [prompt]
|
| 12 |
+
|
| 13 |
+
# LLAMA
|
| 14 |
+
|
| 15 |
+
prompt_llama = [DEFAULT_PROMPT_TEMPLATE["template"].format(p) for p in prompt]
|
| 16 |
+
crop_start = DEFAULT_PROMPT_TEMPLATE["crop_start"]
|
| 17 |
+
|
| 18 |
+
llama_inputs = tokenizer(
|
| 19 |
+
prompt_llama,
|
| 20 |
+
padding="max_length",
|
| 21 |
+
max_length=max_length + crop_start,
|
| 22 |
+
truncation=True,
|
| 23 |
+
return_tensors="pt",
|
| 24 |
+
return_length=False,
|
| 25 |
+
return_overflowing_tokens=False,
|
| 26 |
+
return_attention_mask=True,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
llama_input_ids = llama_inputs.input_ids.to(text_encoder.device)
|
| 30 |
+
llama_attention_mask = llama_inputs.attention_mask.to(text_encoder.device)
|
| 31 |
+
llama_attention_length = int(llama_attention_mask.sum())
|
| 32 |
+
|
| 33 |
+
llama_outputs = text_encoder(
|
| 34 |
+
input_ids=llama_input_ids,
|
| 35 |
+
attention_mask=llama_attention_mask,
|
| 36 |
+
output_hidden_states=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
llama_vec = llama_outputs.hidden_states[-3][:, crop_start:llama_attention_length]
|
| 40 |
+
# llama_vec_remaining = llama_outputs.hidden_states[-3][:, llama_attention_length:]
|
| 41 |
+
llama_attention_mask = llama_attention_mask[:, crop_start:llama_attention_length]
|
| 42 |
+
|
| 43 |
+
assert torch.all(llama_attention_mask.bool())
|
| 44 |
+
|
| 45 |
+
# CLIP
|
| 46 |
+
|
| 47 |
+
clip_l_input_ids = tokenizer_2(
|
| 48 |
+
prompt,
|
| 49 |
+
padding="max_length",
|
| 50 |
+
max_length=77,
|
| 51 |
+
truncation=True,
|
| 52 |
+
return_overflowing_tokens=False,
|
| 53 |
+
return_length=False,
|
| 54 |
+
return_tensors="pt",
|
| 55 |
+
).input_ids
|
| 56 |
+
clip_l_pooler = text_encoder_2(clip_l_input_ids.to(text_encoder_2.device), output_hidden_states=False).pooler_output
|
| 57 |
+
|
| 58 |
+
return llama_vec, clip_l_pooler
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@torch.no_grad()
|
| 62 |
+
def vae_decode_fake(latents):
|
| 63 |
+
latent_rgb_factors = [
|
| 64 |
+
[-0.0395, -0.0331, 0.0445],
|
| 65 |
+
[0.0696, 0.0795, 0.0518],
|
| 66 |
+
[0.0135, -0.0945, -0.0282],
|
| 67 |
+
[0.0108, -0.0250, -0.0765],
|
| 68 |
+
[-0.0209, 0.0032, 0.0224],
|
| 69 |
+
[-0.0804, -0.0254, -0.0639],
|
| 70 |
+
[-0.0991, 0.0271, -0.0669],
|
| 71 |
+
[-0.0646, -0.0422, -0.0400],
|
| 72 |
+
[-0.0696, -0.0595, -0.0894],
|
| 73 |
+
[-0.0799, -0.0208, -0.0375],
|
| 74 |
+
[0.1166, 0.1627, 0.0962],
|
| 75 |
+
[0.1165, 0.0432, 0.0407],
|
| 76 |
+
[-0.2315, -0.1920, -0.1355],
|
| 77 |
+
[-0.0270, 0.0401, -0.0821],
|
| 78 |
+
[-0.0616, -0.0997, -0.0727],
|
| 79 |
+
[0.0249, -0.0469, -0.1703]
|
| 80 |
+
] # From comfyui
|
| 81 |
+
|
| 82 |
+
latent_rgb_factors_bias = [0.0259, -0.0192, -0.0761]
|
| 83 |
+
|
| 84 |
+
weight = torch.tensor(latent_rgb_factors, device=latents.device, dtype=latents.dtype).transpose(0, 1)[:, :, None, None, None]
|
| 85 |
+
bias = torch.tensor(latent_rgb_factors_bias, device=latents.device, dtype=latents.dtype)
|
| 86 |
+
|
| 87 |
+
images = torch.nn.functional.conv3d(latents, weight, bias=bias, stride=1, padding=0, dilation=1, groups=1)
|
| 88 |
+
images = images.clamp(0.0, 1.0)
|
| 89 |
+
|
| 90 |
+
return images
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
@torch.no_grad()
|
| 94 |
+
def vae_decode(latents, vae, image_mode=False):
|
| 95 |
+
latents = latents / vae.config.scaling_factor
|
| 96 |
+
|
| 97 |
+
if image_mode:
|
| 98 |
+
latents = latents.to(device=vae.device, dtype=vae.dtype).unbind(2)
|
| 99 |
+
image = [vae.decode(l.unsqueeze(2)).sample for l in latents]
|
| 100 |
+
image = torch.cat(image, dim=2)
|
| 101 |
+
else:
|
| 102 |
+
image = vae.decode(latents.to(device=vae.device, dtype=vae.dtype)).sample
|
| 103 |
+
|
| 104 |
+
return image
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@torch.no_grad()
|
| 108 |
+
def vae_encode(image, vae):
|
| 109 |
+
latents = vae.encode(image.to(device=vae.device, dtype=vae.dtype)).latent_dist.sample()
|
| 110 |
+
latents = latents * vae.config.scaling_factor
|
| 111 |
+
return latents
|