Update all files for BitDance-14B-64x-diffusers
Browse files
bitdance_diffusers/pipeline_bitdance.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
from contextlib import nullcontext
|
| 4 |
-
from typing import List, Optional,
|
| 5 |
|
| 6 |
import torch
|
| 7 |
from einops import rearrange
|
|
@@ -17,6 +15,13 @@ from .constants import SUPPORTED_IMAGE_SIZES
|
|
| 17 |
PromptType = Union[str, List[str]]
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class BitDanceDiffusionPipeline(DiffusionPipeline):
|
| 21 |
model_cpu_offload_seq = "text_encoder->projector->diffusion_head->autoencoder"
|
| 22 |
|
|
@@ -27,7 +32,8 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 27 |
autoencoder,
|
| 28 |
diffusion_head,
|
| 29 |
projector,
|
| 30 |
-
supported_image_sizes: Optional[
|
|
|
|
| 31 |
) -> None:
|
| 32 |
super().__init__()
|
| 33 |
self.register_modules(
|
|
@@ -130,6 +136,8 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 130 |
def _decode_tokens_to_image(self, image_latents: torch.Tensor, image_size: Tuple[int, int], ps: int = 1) -> torch.Tensor:
|
| 131 |
h, w = image_size
|
| 132 |
image_latents = rearrange(image_latents, "b (h w p1 p2) c -> b c (h p1) (w p2)", h=h // ps, w=w // ps, p1=ps, p2=ps)
|
|
|
|
|
|
|
| 133 |
return self.autoencoder.decode(image_latents)
|
| 134 |
|
| 135 |
@torch.no_grad()
|
|
@@ -183,7 +191,7 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 183 |
pkv_c = outputs_c.past_key_values
|
| 184 |
|
| 185 |
bi_attn_mask = torch.ones(
|
| 186 |
-
(input_embeds_cond.shape[0], 1, step_width, step_width + pkv_c
|
| 187 |
dtype=torch.bool,
|
| 188 |
device=device,
|
| 189 |
)
|
|
@@ -201,11 +209,16 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 201 |
if guidance_scale > 1.0 and input_embeds_uncond is not None:
|
| 202 |
outputs_u = model(inputs_embeds=input_embeds_uncond[:, :-step_width, :], use_cache=True)
|
| 203 |
pkv_u = outputs_u.past_key_values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
outputs_u = model(
|
| 205 |
inputs_embeds=input_embeds_uncond[:, -step_width:, :],
|
| 206 |
past_key_values=pkv_u,
|
| 207 |
use_cache=True,
|
| 208 |
-
attention_mask=
|
| 209 |
)
|
| 210 |
pkv_u = outputs_u.past_key_values
|
| 211 |
hidden_u = outputs_u.last_hidden_state[:, -step_width:]
|
|
@@ -235,7 +248,7 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 235 |
|
| 236 |
model_input = curr_embeds + pos_slice
|
| 237 |
bi_attn_mask = torch.ones(
|
| 238 |
-
(model_input.shape[0], 1, model_input.shape[1], model_input.shape[1] + pkv_c
|
| 239 |
dtype=torch.bool,
|
| 240 |
device=device,
|
| 241 |
)
|
|
@@ -249,11 +262,16 @@ class BitDanceDiffusionPipeline(DiffusionPipeline):
|
|
| 249 |
hidden_c = outputs_c.last_hidden_state[:, -step_width:]
|
| 250 |
|
| 251 |
if guidance_scale > 1.0 and hidden_u is not None and pkv_u is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
outputs_u = model(
|
| 253 |
inputs_embeds=model_input[num_images_per_prompt:],
|
| 254 |
past_key_values=pkv_u,
|
| 255 |
use_cache=True,
|
| 256 |
-
attention_mask=
|
| 257 |
)
|
| 258 |
pkv_u = outputs_u.past_key_values
|
| 259 |
hidden_u = outputs_u.last_hidden_state[:, -step_width:]
|
|
|
|
|
|
|
|
|
|
| 1 |
from contextlib import nullcontext
|
| 2 |
+
from typing import List, Optional, Tuple, Union
|
| 3 |
|
| 4 |
import torch
|
| 5 |
from einops import rearrange
|
|
|
|
| 15 |
PromptType = Union[str, List[str]]
|
| 16 |
|
| 17 |
|
| 18 |
+
def _get_pkv_seq_len(past_key_values) -> int:
|
| 19 |
+
"""Get cached sequence length from past_key_values (supports tuple and DynamicCache)."""
|
| 20 |
+
if hasattr(past_key_values, "get_seq_length"):
|
| 21 |
+
return past_key_values.get_seq_length()
|
| 22 |
+
return past_key_values[0][0].shape[2]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
class BitDanceDiffusionPipeline(DiffusionPipeline):
|
| 26 |
model_cpu_offload_seq = "text_encoder->projector->diffusion_head->autoencoder"
|
| 27 |
|
|
|
|
| 32 |
autoencoder,
|
| 33 |
diffusion_head,
|
| 34 |
projector,
|
| 35 |
+
supported_image_sizes: Optional[List[List[int]]] = None,
|
| 36 |
+
dtype: Optional[torch.dtype] = None,
|
| 37 |
) -> None:
|
| 38 |
super().__init__()
|
| 39 |
self.register_modules(
|
|
|
|
| 136 |
def _decode_tokens_to_image(self, image_latents: torch.Tensor, image_size: Tuple[int, int], ps: int = 1) -> torch.Tensor:
|
| 137 |
h, w = image_size
|
| 138 |
image_latents = rearrange(image_latents, "b (h w p1 p2) c -> b c (h p1) (w p2)", h=h // ps, w=w // ps, p1=ps, p2=ps)
|
| 139 |
+
ae_dtype = next(self.autoencoder.parameters()).dtype
|
| 140 |
+
image_latents = image_latents.to(dtype=ae_dtype)
|
| 141 |
return self.autoencoder.decode(image_latents)
|
| 142 |
|
| 143 |
@torch.no_grad()
|
|
|
|
| 191 |
pkv_c = outputs_c.past_key_values
|
| 192 |
|
| 193 |
bi_attn_mask = torch.ones(
|
| 194 |
+
(input_embeds_cond.shape[0], 1, step_width, step_width + _get_pkv_seq_len(pkv_c)),
|
| 195 |
dtype=torch.bool,
|
| 196 |
device=device,
|
| 197 |
)
|
|
|
|
| 209 |
if guidance_scale > 1.0 and input_embeds_uncond is not None:
|
| 210 |
outputs_u = model(inputs_embeds=input_embeds_uncond[:, :-step_width, :], use_cache=True)
|
| 211 |
pkv_u = outputs_u.past_key_values
|
| 212 |
+
bi_attn_mask_u = torch.ones(
|
| 213 |
+
(input_embeds_uncond.shape[0], 1, step_width, step_width + _get_pkv_seq_len(pkv_u)),
|
| 214 |
+
dtype=torch.bool,
|
| 215 |
+
device=device,
|
| 216 |
+
)
|
| 217 |
outputs_u = model(
|
| 218 |
inputs_embeds=input_embeds_uncond[:, -step_width:, :],
|
| 219 |
past_key_values=pkv_u,
|
| 220 |
use_cache=True,
|
| 221 |
+
attention_mask=bi_attn_mask_u,
|
| 222 |
)
|
| 223 |
pkv_u = outputs_u.past_key_values
|
| 224 |
hidden_u = outputs_u.last_hidden_state[:, -step_width:]
|
|
|
|
| 248 |
|
| 249 |
model_input = curr_embeds + pos_slice
|
| 250 |
bi_attn_mask = torch.ones(
|
| 251 |
+
(model_input.shape[0], 1, model_input.shape[1], model_input.shape[1] + _get_pkv_seq_len(pkv_c)),
|
| 252 |
dtype=torch.bool,
|
| 253 |
device=device,
|
| 254 |
)
|
|
|
|
| 262 |
hidden_c = outputs_c.last_hidden_state[:, -step_width:]
|
| 263 |
|
| 264 |
if guidance_scale > 1.0 and hidden_u is not None and pkv_u is not None:
|
| 265 |
+
bi_attn_mask_u = torch.ones(
|
| 266 |
+
(model_input.shape[0], 1, model_input.shape[1], model_input.shape[1] + _get_pkv_seq_len(pkv_u)),
|
| 267 |
+
dtype=torch.bool,
|
| 268 |
+
device=device,
|
| 269 |
+
)
|
| 270 |
outputs_u = model(
|
| 271 |
inputs_embeds=model_input[num_images_per_prompt:],
|
| 272 |
past_key_values=pkv_u,
|
| 273 |
use_cache=True,
|
| 274 |
+
attention_mask=bi_attn_mask_u[num_images_per_prompt:],
|
| 275 |
)
|
| 276 |
pkv_u = outputs_u.past_key_values
|
| 277 |
hidden_u = outputs_u.last_hidden_state[:, -step_width:]
|