Spaces:
Running on Zero
Running on Zero
monkey patch _compute_timestep_embedding
Browse files
app.py
CHANGED
|
@@ -7,7 +7,73 @@ from diffusers.pipelines.prx import PRXPipeline
|
|
| 7 |
|
| 8 |
# monkey patch to add 1024 aspect ratios
|
| 9 |
import diffusers.pipelines.prx.pipeline_prx as prx_mod
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
CUSTOM_ASPECT_RATIO_512_BIN = {
|
| 12 |
"0.49": [704, 1440],
|
| 13 |
"0.52": [736, 1408],
|
|
@@ -45,6 +111,8 @@ pipe = PRXPipeline.from_pretrained(
|
|
| 45 |
torch_dtype=dtype
|
| 46 |
).to(device)
|
| 47 |
|
|
|
|
|
|
|
| 48 |
MAX_SEED = np.iinfo(np.int32).max
|
| 49 |
MAX_IMAGE_SIZE = 1024
|
| 50 |
|
|
|
|
| 7 |
|
| 8 |
# monkey patch to add 1024 aspect ratios
|
| 9 |
import diffusers.pipelines.prx.pipeline_prx as prx_mod
|
| 10 |
+
import math
|
| 11 |
+
|
| 12 |
+
def get_timestep_embedding(
|
| 13 |
+
timesteps: torch.Tensor,
|
| 14 |
+
embedding_dim: int,
|
| 15 |
+
flip_sin_to_cos: bool = False,
|
| 16 |
+
downscale_freq_shift: float = 1,
|
| 17 |
+
scale: float = 0,
|
| 18 |
+
max_period: int = 10000,
|
| 19 |
+
) -> torch.Tensor:
|
| 20 |
+
"""
|
| 21 |
+
This matches the implementation in Denoising Diffusion Probabilistic Models: Create sinusoidal timestep embeddings.
|
| 22 |
+
|
| 23 |
+
Args
|
| 24 |
+
timesteps (torch.Tensor):
|
| 25 |
+
a 1-D Tensor of N indices, one per batch element. These may be fractional.
|
| 26 |
+
embedding_dim (int):
|
| 27 |
+
the dimension of the output.
|
| 28 |
+
flip_sin_to_cos (bool):
|
| 29 |
+
Whether the embedding order should be `cos, sin` (if True) or `sin, cos` (if False)
|
| 30 |
+
downscale_freq_shift (float):
|
| 31 |
+
Controls the delta between frequencies between dimensions
|
| 32 |
+
scale (float):
|
| 33 |
+
Scaling factor applied to the embeddings.
|
| 34 |
+
max_period (int):
|
| 35 |
+
Controls the maximum frequency of the embeddings
|
| 36 |
+
Returns
|
| 37 |
+
torch.Tensor: an [N x dim] Tensor of positional embeddings.
|
| 38 |
+
"""
|
| 39 |
+
assert len(timesteps.shape) == 1, "Timesteps should be a 1d-array"
|
| 40 |
+
|
| 41 |
+
half_dim = embedding_dim // 2
|
| 42 |
+
exponent = -math.log(max_period) * torch.arange(
|
| 43 |
+
start=0, end=half_dim, dtype=torch.float32, device=timesteps.device
|
| 44 |
+
)
|
| 45 |
+
exponent = exponent / (half_dim - downscale_freq_shift)
|
| 46 |
+
|
| 47 |
+
emb = torch.exp(exponent)
|
| 48 |
+
emb = timesteps[:, None].float() * emb[None, :]
|
| 49 |
+
|
| 50 |
+
# scale embeddings
|
| 51 |
+
emb = scale * emb
|
| 52 |
+
|
| 53 |
+
# concat sine and cosine embeddings
|
| 54 |
+
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=-1)
|
| 55 |
+
|
| 56 |
+
# flip sine and cosine embeddings
|
| 57 |
+
if flip_sin_to_cos:
|
| 58 |
+
emb = torch.cat([emb[:, half_dim:], emb[:, :half_dim]], dim=-1)
|
| 59 |
+
|
| 60 |
+
# zero pad
|
| 61 |
+
if embedding_dim % 2 == 1:
|
| 62 |
+
emb = torch.nn.functional.pad(emb, (0, 1, 0, 0))
|
| 63 |
+
return emb
|
| 64 |
|
| 65 |
+
def _compute_timestep_embedding(self, timestep: torch.Tensor, dtype: torch.dtype) -> torch.Tensor:
|
| 66 |
+
return self.time_in(
|
| 67 |
+
get_timestep_embedding(
|
| 68 |
+
timesteps=timestep,
|
| 69 |
+
embedding_dim=256,
|
| 70 |
+
max_period=self.time_max_period,
|
| 71 |
+
scale=self.time_factor,
|
| 72 |
+
flip_sin_to_cos=True, # Match original cos, sin order
|
| 73 |
+
downscale_freq_shift=0.0,
|
| 74 |
+
).to(dtype)
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
CUSTOM_ASPECT_RATIO_512_BIN = {
|
| 78 |
"0.49": [704, 1440],
|
| 79 |
"0.52": [736, 1408],
|
|
|
|
| 111 |
torch_dtype=dtype
|
| 112 |
).to(device)
|
| 113 |
|
| 114 |
+
pipe.transformer._compute_timestep_embedding = _compute_timestep_embedding
|
| 115 |
+
|
| 116 |
MAX_SEED = np.iinfo(np.int32).max
|
| 117 |
MAX_IMAGE_SIZE = 1024
|
| 118 |
|