Upload extensions_built_in/diffusion_models/hidream/src/models/embeddings.py with huggingface_hub
Browse files
extensions_built_in/diffusion_models/hidream/src/models/embeddings.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
from typing import List
|
| 4 |
+
from diffusers.models.embeddings import Timesteps, TimestepEmbedding
|
| 5 |
+
|
| 6 |
+
# Copied from https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py
|
| 7 |
+
def rope(pos: torch.Tensor, dim: int, theta: int) -> torch.Tensor:
|
| 8 |
+
assert dim % 2 == 0, "The dimension must be even."
|
| 9 |
+
|
| 10 |
+
scale = torch.arange(0, dim, 2, dtype=torch.float64, device=pos.device) / dim
|
| 11 |
+
omega = 1.0 / (theta**scale)
|
| 12 |
+
|
| 13 |
+
batch_size, seq_length = pos.shape
|
| 14 |
+
out = torch.einsum("...n,d->...nd", pos, omega)
|
| 15 |
+
cos_out = torch.cos(out)
|
| 16 |
+
sin_out = torch.sin(out)
|
| 17 |
+
|
| 18 |
+
stacked_out = torch.stack([cos_out, -sin_out, sin_out, cos_out], dim=-1)
|
| 19 |
+
out = stacked_out.view(batch_size, -1, dim // 2, 2, 2)
|
| 20 |
+
return out.float()
|
| 21 |
+
|
| 22 |
+
# Copied from https://github.com/black-forest-labs/flux/blob/main/src/flux/modules/layers.py
|
| 23 |
+
class EmbedND(nn.Module):
|
| 24 |
+
def __init__(self, theta: int, axes_dim: List[int]):
|
| 25 |
+
super().__init__()
|
| 26 |
+
self.theta = theta
|
| 27 |
+
self.axes_dim = axes_dim
|
| 28 |
+
|
| 29 |
+
def forward(self, ids: torch.Tensor) -> torch.Tensor:
|
| 30 |
+
n_axes = ids.shape[-1]
|
| 31 |
+
emb = torch.cat(
|
| 32 |
+
[rope(ids[..., i], self.axes_dim[i], self.theta) for i in range(n_axes)],
|
| 33 |
+
dim=-3,
|
| 34 |
+
)
|
| 35 |
+
return emb.unsqueeze(2)
|
| 36 |
+
|
| 37 |
+
class PatchEmbed(nn.Module):
|
| 38 |
+
def __init__(
|
| 39 |
+
self,
|
| 40 |
+
patch_size=2,
|
| 41 |
+
in_channels=4,
|
| 42 |
+
out_channels=1024,
|
| 43 |
+
):
|
| 44 |
+
super().__init__()
|
| 45 |
+
self.patch_size = patch_size
|
| 46 |
+
self.out_channels = out_channels
|
| 47 |
+
self.proj = nn.Linear(in_channels * patch_size * patch_size, out_channels, bias=True)
|
| 48 |
+
self.apply(self._init_weights)
|
| 49 |
+
|
| 50 |
+
def _init_weights(self, m):
|
| 51 |
+
if isinstance(m, nn.Linear):
|
| 52 |
+
nn.init.xavier_uniform_(m.weight)
|
| 53 |
+
if m.bias is not None:
|
| 54 |
+
nn.init.constant_(m.bias, 0)
|
| 55 |
+
|
| 56 |
+
def forward(self, latent):
|
| 57 |
+
latent = self.proj(latent)
|
| 58 |
+
return latent
|
| 59 |
+
|
| 60 |
+
class PooledEmbed(nn.Module):
|
| 61 |
+
def __init__(self, text_emb_dim, hidden_size):
|
| 62 |
+
super().__init__()
|
| 63 |
+
self.pooled_embedder = TimestepEmbedding(in_channels=text_emb_dim, time_embed_dim=hidden_size)
|
| 64 |
+
self.apply(self._init_weights)
|
| 65 |
+
|
| 66 |
+
def _init_weights(self, m):
|
| 67 |
+
if isinstance(m, nn.Linear):
|
| 68 |
+
nn.init.normal_(m.weight, std=0.02)
|
| 69 |
+
if m.bias is not None:
|
| 70 |
+
nn.init.constant_(m.bias, 0)
|
| 71 |
+
|
| 72 |
+
def forward(self, pooled_embed):
|
| 73 |
+
return self.pooled_embedder(pooled_embed)
|
| 74 |
+
|
| 75 |
+
class TimestepEmbed(nn.Module):
|
| 76 |
+
def __init__(self, hidden_size, frequency_embedding_size=256):
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.time_proj = Timesteps(num_channels=frequency_embedding_size, flip_sin_to_cos=True, downscale_freq_shift=0)
|
| 79 |
+
self.timestep_embedder = TimestepEmbedding(in_channels=frequency_embedding_size, time_embed_dim=hidden_size)
|
| 80 |
+
self.apply(self._init_weights)
|
| 81 |
+
|
| 82 |
+
def _init_weights(self, m):
|
| 83 |
+
if isinstance(m, nn.Linear):
|
| 84 |
+
nn.init.normal_(m.weight, std=0.02)
|
| 85 |
+
if m.bias is not None:
|
| 86 |
+
nn.init.constant_(m.bias, 0)
|
| 87 |
+
|
| 88 |
+
def forward(self, timesteps, wdtype):
|
| 89 |
+
t_emb = self.time_proj(timesteps).to(dtype=wdtype)
|
| 90 |
+
t_emb = self.timestep_embedder(t_emb)
|
| 91 |
+
return t_emb
|
| 92 |
+
|
| 93 |
+
class OutEmbed(nn.Module):
|
| 94 |
+
def __init__(self, hidden_size, patch_size, out_channels):
|
| 95 |
+
super().__init__()
|
| 96 |
+
self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 97 |
+
self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True)
|
| 98 |
+
self.adaLN_modulation = nn.Sequential(
|
| 99 |
+
nn.SiLU(),
|
| 100 |
+
nn.Linear(hidden_size, 2 * hidden_size, bias=True)
|
| 101 |
+
)
|
| 102 |
+
self.apply(self._init_weights)
|
| 103 |
+
|
| 104 |
+
def _init_weights(self, m):
|
| 105 |
+
if isinstance(m, nn.Linear):
|
| 106 |
+
nn.init.zeros_(m.weight)
|
| 107 |
+
if m.bias is not None:
|
| 108 |
+
nn.init.constant_(m.bias, 0)
|
| 109 |
+
|
| 110 |
+
def forward(self, x, adaln_input):
|
| 111 |
+
shift, scale = self.adaLN_modulation(adaln_input).chunk(2, dim=1)
|
| 112 |
+
x = self.norm_final(x) * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
|
| 113 |
+
x = self.linear(x)
|
| 114 |
+
return x
|