from typing import Optional import torch import torch.nn.functional as F from torch import nn from x_transformers.x_transformers import apply_rotary_pos_emb class RMSNorm(nn.Module): def __init__(self, dim: int, eps: float): super().__init__() self.eps = eps self.weight = nn.Parameter(torch.ones(dim)) _tv = torch.__version__.split("+")[0].split(".")[:2] self.native_rms_norm = tuple(map(int, _tv)) >= (2, 4) def forward(self, x): if self.native_rms_norm: if self.weight.dtype in [torch.float16, torch.bfloat16]: x = x.to(self.weight.dtype) x = F.rms_norm(x, normalized_shape=(x.shape[-1],), weight=self.weight, eps=self.eps) else: variance = x.to(torch.float32).pow(2).mean(-1, keepdim=True) x = x * torch.rsqrt(variance + self.eps) if self.weight.dtype in [torch.float16, torch.bfloat16]: x = x.to(self.weight.dtype) x = x * self.weight return x class FeedForward(nn.Module): def __init__(self, dim, dim_out=None, mult=4, dropout=0.0, approximate: str = "none"): super().__init__() inner_dim = int(dim * mult) dim_out = dim_out if dim_out is not None else dim activation = nn.GELU(approximate=approximate) project_in = nn.Sequential(nn.Linear(dim, inner_dim), activation) self.ff = nn.Sequential(project_in, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out)) def forward(self, x): return self.ff(x) class Attention(nn.Module): def __init__( self, dim: int, heads: int = 8, dim_head: int = 64, dropout: float = 0.0, qk_norm: Optional[str] = None, pe_attn_head: int | None = None, # number of attention head to apply rope, None for all attn_backend: str = "torch", # "torch" or "flash_attn" attn_mask_enabled: bool = True, ): super().__init__() if not hasattr(F, "scaled_dot_product_attention"): raise ImportError("Attention equires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.") self.dim = dim self.heads = heads self.inner_dim = dim_head * heads self.dropout = dropout self.to_q = nn.Linear(dim, self.inner_dim) self.to_k = nn.Linear(dim, self.inner_dim) self.to_v = nn.Linear(dim, self.inner_dim) if qk_norm is None: self.q_norm = None self.k_norm = None elif qk_norm == "rms_norm": self.q_norm = RMSNorm(dim_head, eps=1e-6) self.k_norm = RMSNorm(dim_head, eps=1e-6) else: raise ValueError(f"Unimplemented qk_norm: {qk_norm}") self.to_out = nn.ModuleList([]) self.to_out.append(nn.Linear(self.inner_dim, dim)) self.to_out.append(nn.Dropout(dropout)) # if attn_backend == "flash_attn": # assert is_package_available("flash_attn"), "Please install flash-attn first." self.pe_attn_head = pe_attn_head self.attn_backend = attn_backend self.attn_mask_enabled = attn_mask_enabled def forward( self, x: float, # noised input x mask=None, rope=None, # rotary position embedding for x ) -> torch.Tensor: batch_size = x.shape[0] query = self.to_q(x) key = self.to_k(x) value = self.to_v(x) # attention inner_dim = key.shape[-1] head_dim = inner_dim // self.heads query = query.view(batch_size, -1, self.heads, head_dim).transpose(1, 2) key = key.view(batch_size, -1, self.heads, head_dim).transpose(1, 2) value = value.view(batch_size, -1, self.heads, head_dim).transpose(1, 2) # qk norm if self.q_norm is not None: query = self.q_norm(query) if self.k_norm is not None: key = self.k_norm(key) # apply rotary position embedding if rope is not None: freqs, xpos_scale = rope q_xpos_scale, k_xpos_scale = (xpos_scale, xpos_scale**-1.0) if xpos_scale is not None else (1.0, 1.0) if self.pe_attn_head is not None: pn = self.pe_attn_head query[:, :pn, :, :] = apply_rotary_pos_emb(query[:, :pn, :, :], freqs, q_xpos_scale) key[:, :pn, :, :] = apply_rotary_pos_emb(key[:, :pn, :, :], freqs, k_xpos_scale) else: query = apply_rotary_pos_emb(query, freqs, q_xpos_scale) key = apply_rotary_pos_emb(key, freqs, k_xpos_scale) x = F.scaled_dot_product_attention(query, key, value, attn_mask=None, dropout_p=0.0, is_causal=False) x = x.transpose(1, 2).reshape(batch_size, -1, self.heads * head_dim) x = x.to(query.dtype) x = self.to_out[0](x) x = self.to_out[1](x) if mask is not None: mask = mask.unsqueeze(-1) x = x.masked_fill(~mask, 0.0) return x class DiTBlock(nn.Module): def __init__( self, hidden_size, num_heads, mlp_ratio=4.0, dropout=0.1, qk_norm=None, pe_attn_head=None, attn_backend="flash_attn", # "torch" or "flash_attn" attn_mask_enabled=True, **kwargs ): super().__init__() self.norm1 = RMSNorm(hidden_size, eps=1e-6) self.attn = Attention( dim=hidden_size, heads=num_heads, dim_head=hidden_size // num_heads, dropout=dropout, qk_norm=qk_norm, pe_attn_head=pe_attn_head, attn_backend=attn_backend, attn_mask_enabled=attn_mask_enabled, ) self.norm2 = RMSNorm(hidden_size, eps=1e-6) self.mlp = FeedForward(dim=hidden_size, mult=mlp_ratio, dropout=dropout, approximate="tanh") def forward(self, x, mask, rope): x = x + self.attn(self.norm1(x), mask=mask, rope=rope) x = x + self.mlp(self.norm2(x)) return x class FinalLayer(nn.Module): """ The final layer of DiT. """ def __init__(self, hidden_size, out_channels): super().__init__() self.norm_final = RMSNorm(hidden_size, eps=1e-6) self.linear = nn.Linear(hidden_size, out_channels, bias=True) def forward(self, x): x = self.norm_final(x) x = self.linear(x) return x