# Copyright 2026 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Self-contained FiT Hub module (generated by scripts/bundle_fit_hub_modules.py).""" import torch from torch import Tensor from typing import List, Tuple import torch.nn as nn import math from math import pi from typing import Optional, Any, Union, Tuple from torch import nn from einops import rearrange, repeat from functools import lru_cache import numpy as np import torch.nn.functional as F from torch import nn, Tensor from torch.jit import Final from timm.layers.mlp import SwiGLU, Mlp from typing import Any, Callable, Dict, List, Optional, Union, Tuple from functools import partial from typing import Optional from einops import rearrange from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.models.modeling_utils import ModelMixin try: from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.models.modeling_utils import ModelMixin except Exception: # pragma: no cover class ConfigMixin: def register_to_config(self, **kwargs): if not hasattr(self, "_config"): self._config = {} self._config.update(kwargs) @property def config(self): return self._config def register_to_config(func): return func class ModelMixin(nn.Module): pass def modulate(x, shift, scale): return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1) def get_parameter_dtype(parameter: torch.nn.Module): try: params = tuple(parameter.parameters()) if len(params) > 0: return params[0].dtype buffers = tuple(parameter.buffers()) if len(buffers) > 0: return buffers[0].dtype except StopIteration: # For torch.nn.DataParallel compatibility in PyTorch 1.5 def find_tensor_attributes(module: torch.nn.Module) -> List[Tuple[str, Tensor]]: tuples = [(k, v) for k, v in module.__dict__.items() if torch.is_tensor(v)] return tuples gen = parameter._named_members(get_members_fn=find_tensor_attributes) first_tuple = next(gen) return first_tuple[1].dtype def create_norm(norm_type: str, dim: int, eps: float = 1e-6): if norm_type is None or norm_type == "": return nn.Identity() norm_type = norm_type.lower() if norm_type == "w_layernorm": return nn.LayerNorm(dim, eps=eps, bias=False) elif norm_type == "layernorm": return nn.LayerNorm(dim, eps=eps, elementwise_affine=False, bias=False) elif norm_type == "w_rmsnorm": return RMSNorm(dim, eps=eps) elif norm_type == "rmsnorm": return RMSNorm(dim, include_weight=False, eps=eps) elif norm_type == "none": return nn.Identity() else: raise NotImplementedError(f"Unknown norm_type: '{norm_type}'") class RMSNorm(nn.Module): def __init__(self, dim: int, include_weight: bool = True, eps: float = 1e-6): super().__init__() self.eps = eps self.include_weight = include_weight self.weight = nn.Parameter(torch.ones(dim)) if include_weight else None def _norm(self, x: torch.Tensor): return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) def forward(self, x: torch.Tensor): output = self._norm(x.float()).type_as(x) if self.weight is not None: return output * self.weight return output def reset_parameters(self): if self.weight is not None: torch.nn.init.ones_(self.weight) # -------------------------------------------------------- # FiT: A Flexible Vision Transformer for Image Generation # # Based on the following repository # https://github.com/lucidrains/rotary-embedding-torch # https://github.com/jquesnelle/yarn/blob/HEAD/scaled_rope # https://colab.research.google.com/drive/1VI2nhlyKvd5cw4-zHvAIk00cAVj2lCCC#scrollTo=b80b3f37 # -------------------------------------------------------- ################################################################################# # NTK Operations # ################################################################################# def find_correction_factor(num_rotations, dim, base=10000, max_position_embeddings=2048): return (dim * math.log(max_position_embeddings/(num_rotations * 2 * math.pi)))/(2 * math.log(base)) #Inverse dim formula to find number of rotations def find_correction_range(low_rot, high_rot, dim, base=10000, max_position_embeddings=2048): low = math.floor(find_correction_factor(low_rot, dim, base, max_position_embeddings)) high = math.ceil(find_correction_factor(high_rot, dim, base, max_position_embeddings)) return max(low, 0), min(high, dim-1) #Clamp values just in case def linear_ramp_mask(min, max, dim): if min == max: max += 0.001 #Prevent singularity linear_func = (torch.arange(dim, dtype=torch.float32) - min) / (max - min) ramp_func = torch.clamp(linear_func, 0, 1) return ramp_func def find_newbase_ntk(dim, base=10000, scale=1): # Base change formula return base * scale ** (dim / (dim-2)) def get_mscale(scale=torch.Tensor): # if scale <= 1: # return 1.0 # return 0.1 * math.log(scale) + 1.0 return torch.where(scale <= 1., torch.tensor(1.0), 0.1 * torch.log(scale) + 1.0) def get_proportion(L_test, L_train): L_test = L_test * 2 return torch.where(torch.tensor(L_test/L_train) <= 1., torch.tensor(1.0), torch.sqrt(torch.log(torch.tensor(L_test))/torch.log(torch.tensor(L_train)))) # return torch.sqrt(torch.log(torch.tensor(L_test))/torch.log(torch.tensor(L_train))) ################################################################################# # Rotate Q or K # ################################################################################# def rotate_half(x): x = rearrange(x, '... (d r) -> ... d r', r = 2) x1, x2 = x.unbind(dim = -1) x = torch.stack((-x2, x1), dim = -1) return rearrange(x, '... d r -> ... (d r)') ################################################################################# # Core Vision RoPE # ################################################################################# class VisionRotaryEmbedding(nn.Module): def __init__( self, head_dim: int, # embed dimension for each head custom_freqs: str = 'normal', theta: int = 10000, online_rope: bool = False, max_cached_len: int = 256, max_pe_len_h: Optional[int] = None, max_pe_len_w: Optional[int] = None, decouple: bool = False, ori_max_pe_len: Optional[int] = None, ): super().__init__() dim = head_dim // 2 assert dim % 2 == 0 # accually, this is important self.dim = dim self.custom_freqs = custom_freqs.lower() self.theta = theta self.decouple = decouple self.ori_max_pe_len = ori_max_pe_len self.custom_freqs = custom_freqs.lower() if not online_rope: if self.custom_freqs == 'normal': freqs_h = 1. / (theta ** (torch.arange(0, dim, 2).float() / dim)) freqs_w = 1. / (theta ** (torch.arange(0, dim, 2).float() / dim)) else: if decouple: freqs_h = self.get_1d_rope_freqs(theta, dim, max_pe_len_h, ori_max_pe_len) freqs_w = self.get_1d_rope_freqs(theta, dim, max_pe_len_w, ori_max_pe_len) else: max_pe_len = max(max_pe_len_h, max_pe_len_w) freqs_h = self.get_1d_rope_freqs(theta, dim, max_pe_len, ori_max_pe_len) freqs_w = self.get_1d_rope_freqs(theta, dim, max_pe_len, ori_max_pe_len) attn_factor = 1.0 scale = torch.clamp_min(torch.tensor(max(max_pe_len_h, max_pe_len_w)) / ori_max_pe_len, 1.0) # dynamic scale self.mscale = get_mscale(scale).to(scale) * attn_factor # Get n-d magnitude scaling corrected for interpolation self.proportion1 = get_proportion(max(max_pe_len_h, max_pe_len_w), ori_max_pe_len) self.proportion2 = get_proportion(max_pe_len_h * max_pe_len_w, ori_max_pe_len ** 2) self.register_buffer('freqs_h', freqs_h, persistent=False) self.register_buffer('freqs_w', freqs_w, persistent=False) freqs_h_cached = torch.einsum('..., f -> ... f', torch.arange(max_cached_len), self.freqs_h) freqs_h_cached = repeat(freqs_h_cached, '... n -> ... (n r)', r = 2) self.register_buffer('freqs_h_cached', freqs_h_cached, persistent=False) freqs_w_cached = torch.einsum('..., f -> ... f', torch.arange(max_cached_len), self.freqs_w) freqs_w_cached = repeat(freqs_w_cached, '... n -> ... (n r)', r = 2) self.register_buffer('freqs_w_cached', freqs_w_cached, persistent=False) def get_1d_rope_freqs(self, theta, dim, max_pe_len, ori_max_pe_len): # scaling operations for extrapolation assert isinstance(ori_max_pe_len, int) # scale = max_pe_len / ori_max_pe_len if not isinstance(max_pe_len, torch.Tensor): max_pe_len = torch.tensor(max_pe_len) scale = torch.clamp_min(max_pe_len / ori_max_pe_len, 1.0) # dynamic scale if self.custom_freqs == 'linear': # equal to position interpolation freqs = 1. / torch.einsum('..., f -> ... f', scale, theta ** (torch.arange(0, dim, 2).float() / dim)) elif self.custom_freqs == 'ntk-aware' or self.custom_freqs == 'ntk-aware-pro1' or self.custom_freqs == 'ntk-aware-pro2': freqs = 1. / torch.pow( find_newbase_ntk(dim, theta, scale).view(-1, 1), (torch.arange(0, dim, 2).to(scale).float() / dim) ).squeeze() elif self.custom_freqs == 'ntk-by-parts': #Interpolation constants found experimentally for LLaMA (might not be totally optimal though) #Do not change unless there is a good reason for doing so! beta_0 = 1.25 beta_1 = 0.75 gamma_0 = 16 gamma_1 = 2 ntk_factor = 1 extrapolation_factor = 1 #Three RoPE extrapolation/interpolation methods freqs_base = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim)) freqs_linear = 1.0 / torch.einsum('..., f -> ... f', scale, (theta ** (torch.arange(0, dim, 2).to(scale).float() / dim))) freqs_ntk = 1. / torch.pow( find_newbase_ntk(dim, theta, scale).view(-1, 1), (torch.arange(0, dim, 2).to(scale).float() / dim) ).squeeze() #Combine NTK and Linear low, high = find_correction_range(beta_0, beta_1, dim, theta, ori_max_pe_len) freqs_mask = (1 - linear_ramp_mask(low, high, dim // 2).to(scale)) * ntk_factor freqs = freqs_linear * (1 - freqs_mask) + freqs_ntk * freqs_mask #Combine Extrapolation and NTK and Linear low, high = find_correction_range(gamma_0, gamma_1, dim, theta, ori_max_pe_len) freqs_mask = (1 - linear_ramp_mask(low, high, dim // 2).to(scale)) * extrapolation_factor freqs = freqs * (1 - freqs_mask) + freqs_base * freqs_mask elif self.custom_freqs == 'yarn': #Interpolation constants found experimentally for LLaMA (might not be totally optimal though) #Do not change unless there is a good reason for doing so! beta_fast = 32 beta_slow = 1 extrapolation_factor = 1 freqs_extrapolation = 1.0 / (theta ** (torch.arange(0, dim, 2).to(scale).float() / dim)) freqs_interpolation = 1.0 / torch.einsum('..., f -> ... f', scale, (theta ** (torch.arange(0, dim, 2).to(scale).float() / dim))) low, high = find_correction_range(beta_fast, beta_slow, dim, theta, ori_max_pe_len) freqs_mask = (1 - linear_ramp_mask(low, high, dim // 2).to(scale).float()) * extrapolation_factor # Get n-d rotational scaling corrected for extrapolation freqs = freqs_interpolation * (1 - freqs_mask) + freqs_extrapolation * freqs_mask else: raise ValueError(f'Unknown modality {self.custom_freqs}. Only support normal, linear, ntk-aware, ntk-by-parts, yarn!') return freqs def online_get_2d_rope_from_grid(self, grid, size): ''' grid: (B, 2, N) N = H * W the first dimension represents width, and the second reprensents height e.g., [0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3.] [0. 0. 0. 0. 1. 1. 1. 1. 2. 2. 2. 2.] size: (B, 1, 2), h goes first and w goes last ''' size = size.squeeze() # (B, 1, 2) -> (B, 2) if self.decouple: size_h = size[:, 0] size_w = size[:, 1] freqs_h = self.get_1d_rope_freqs(self.theta, self.dim, size_h, self.ori_max_pe_len) freqs_w = self.get_1d_rope_freqs(self.theta, self.dim, size_w, self.ori_max_pe_len) else: size_max = torch.max(size[:, 0], size[:, 1]) freqs_h = self.get_1d_rope_freqs(self.theta, self.dim, size_max, self.ori_max_pe_len) freqs_w = self.get_1d_rope_freqs(self.theta, self.dim, size_max, self.ori_max_pe_len) freqs_w = grid[:, 0][..., None] * freqs_w[:, None, :] freqs_w = repeat(freqs_w, '... n -> ... (n r)', r = 2) freqs_h = grid[:, 1][..., None] * freqs_h[:, None, :] freqs_h = repeat(freqs_h, '... n -> ... (n r)', r = 2) freqs = torch.cat([freqs_h, freqs_w], dim=-1) # (B, N, D) if self.custom_freqs == 'yarn': freqs_cos = freqs.cos() * self.mscale[:, None, None] freqs_sin = freqs.sin() * self.mscale[:, None, None] elif self.custom_freqs == 'ntk-aware-pro1': freqs_cos = freqs.cos() * self.proportion1[:, None, None] freqs_sin = freqs.sin() * self.proportion1[:, None, None] elif self.custom_freqs == 'ntk-aware-pro2': freqs_cos = freqs.cos() * self.proportion2[:, None, None] freqs_sin = freqs.sin() * self.proportion2[:, None, None] else: freqs_cos = freqs.cos() freqs_sin = freqs.sin() return freqs_cos, freqs_sin @lru_cache() def get_2d_rope_from_grid(self, grid): ''' grid: (B, 2, N) N = H * W the first dimension represents width, and the second reprensents height e.g., [0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3.] [0. 0. 0. 0. 1. 1. 1. 1. 2. 2. 2. 2.] ''' freqs_w = torch.einsum('..., f -> ... f', grid[:, 0], self.freqs_w) freqs_w = repeat(freqs_w, '... n -> ... (n r)', r = 2) freqs_h = torch.einsum('..., f -> ... f', grid[:, 1], self.freqs_h) freqs_h = repeat(freqs_h, '... n -> ... (n r)', r = 2) freqs = torch.cat([freqs_h, freqs_w], dim=-1) # (B, N, D) if self.custom_freqs == 'yarn': freqs_cos = freqs.cos() * self.mscale freqs_sin = freqs.sin() * self.mscale elif self.custom_freqs == 'ntk-aware-pro1': freqs_cos = freqs.cos() * self.proportion1 freqs_sin = freqs.sin() * self.proportion1 elif self.custom_freqs == 'ntk-aware-pro2': freqs_cos = freqs.cos() * self.proportion2 freqs_sin = freqs.sin() * self.proportion2 else: freqs_cos = freqs.cos() freqs_sin = freqs.sin() return freqs_cos, freqs_sin @lru_cache() def get_cached_2d_rope_from_grid(self, grid: torch.Tensor): ''' grid: (B, 2, N) N = H * W the first dimension represents width, and the second reprensents height e.g., [0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3.] [0. 0. 0. 0. 1. 1. 1. 1. 2. 2. 2. 2.] ''' freqs_w, freqs_h = self.freqs_w_cached[grid[:, 0]], self.freqs_h_cached[grid[:, 1]] freqs = torch.cat([freqs_h, freqs_w], dim=-1) # (B, N, D) if self.custom_freqs == 'yarn': freqs_cos = freqs.cos() * self.mscale freqs_sin = freqs.sin() * self.mscale elif self.custom_freqs == 'ntk-aware-pro1': freqs_cos = freqs.cos() * self.proportion1 freqs_sin = freqs.sin() * self.proportion1 elif self.custom_freqs == 'ntk-aware-pro2': freqs_cos = freqs.cos() * self.proportion2 freqs_sin = freqs.sin() * self.proportion2 else: freqs_cos = freqs.cos() freqs_sin = freqs.sin() return freqs_cos, freqs_sin @lru_cache() def get_cached_21d_rope_from_grid(self, grid: torch.Tensor): # for 3d rope formulation 2 ! ''' grid: (B, 3, N) N = H * W * T the first dimension represents width, and the second reprensents height, and the third reprensents time e.g., [0. 1. 2. 3. 0. 1. 2. 3. 0. 1. 2. 3.] [0. 0. 0. 0. 1. 1. 1. 1. 2. 2. 2. 2.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] ''' freqs_w, freqs_h = self.freqs_w_cached[grid[:, 0]+grid[:, 2]], self.freqs_h_cached[grid[:, 1]+grid[:, 2]] freqs = torch.cat([freqs_h, freqs_w], dim=-1) # (B, N, D) if self.custom_freqs == 'yarn': freqs_cos = freqs.cos() * self.mscale freqs_sin = freqs.sin() * self.mscale elif self.custom_freqs == 'ntk-aware-pro1': freqs_cos = freqs.cos() * self.proportion1 freqs_sin = freqs.sin() * self.proportion1 elif self.custom_freqs == 'ntk-aware-pro2': freqs_cos = freqs.cos() * self.proportion2 freqs_sin = freqs.sin() * self.proportion2 else: freqs_cos = freqs.cos() freqs_sin = freqs.sin() return freqs_cos, freqs_sin def forward(self, x, grid): ''' x: (B, n_head, N, D) grid: (B, 2, N) ''' # freqs_cos, freqs_sin = self.get_2d_rope_from_grid(grid) # freqs_cos, freqs_sin = freqs_cos.unsqueeze(1), freqs_sin.unsqueeze(1) # using cache to accelerate, this is the same with the above codes: freqs_cos, freqs_sin = self.get_cached_2d_rope_from_grid(grid) freqs_cos, freqs_sin = freqs_cos.unsqueeze(1), freqs_sin.unsqueeze(1) return x * freqs_cos + rotate_half(x) * freqs_sin ################################################################################# # Embedding Layers for Patches, Timesteps and Class Labels # ################################################################################# class PatchEmbedder(nn.Module): """ Embeds latent features into vector representations """ def __init__(self, input_dim, embed_dim, bias: bool = True, norm_layer: Optional[Callable] = None, ): super().__init__() self.proj = nn.Linear(input_dim, embed_dim, bias=bias) self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity() def forward(self, x): x = self.proj(x) # (B, L, patch_size ** 2 * C) -> (B, L, D) x = self.norm(x) return x class TimestepEmbedder(nn.Module): """ Embeds scalar timesteps into vector representations. """ def __init__(self, hidden_size, frequency_embedding_size=256): super().__init__() self.mlp = nn.Sequential( nn.Linear(frequency_embedding_size, hidden_size, bias=True), nn.SiLU(), nn.Linear(hidden_size, hidden_size, bias=True), ) self.frequency_embedding_size = frequency_embedding_size @staticmethod def timestep_embedding(t, dim, max_period=10000): """ Create sinusoidal timestep embeddings. :param t: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an (N, D) Tensor of positional embeddings. """ # https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py half = dim // 2 freqs = torch.exp( -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half ).to(device=t.device) args = t[:, None] * freqs[None] embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) if dim % 2: embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1]).to(device=t.device)], dim=-1) return embedding.to(dtype=t.dtype) def forward(self, t): t_freq = self.timestep_embedding(t, self.frequency_embedding_size) t_emb = self.mlp(t_freq) return t_emb class LabelEmbedder(nn.Module): """ Embeds class labels into vector representations. Also handles label dropout for classifier-free guidance. """ def __init__(self, num_classes, hidden_size, dropout_prob): super().__init__() use_cfg_embedding = dropout_prob > 0 self.embedding_table = nn.Embedding(num_classes + use_cfg_embedding, hidden_size) self.num_classes = num_classes self.dropout_prob = dropout_prob def token_drop(self, labels, force_drop_ids=None): """ Drops labels to enable classifier-free guidance. """ if force_drop_ids is None: drop_ids = torch.rand(labels.shape[0], device=labels.device) < self.dropout_prob else: drop_ids = force_drop_ids == 1 labels = torch.where(drop_ids, self.num_classes, labels) return labels def forward(self, labels, train, force_drop_ids=None): use_dropout = self.dropout_prob > 0 if (train and use_dropout) or (force_drop_ids is not None): labels = self.token_drop(labels, force_drop_ids) embeddings = self.embedding_table(labels) return embeddings ################################################################################# # Attention # ################################################################################# # modified from timm and eva-02 # https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.py # https://github.com/baaivision/EVA/blob/master/EVA-02/asuka/modeling_finetune.py class Attention(nn.Module): def __init__(self, dim: int, num_heads: int = 8, qkv_bias: bool = False, q_norm: Optional[str] = None, k_norm: Optional[str] = None, qk_norm_weight: bool = False, attn_drop: float = 0., proj_drop: float = 0., rel_pos_embed: Optional[str] = None, add_rel_pe_to_v: bool = False, ) -> None: super().__init__() assert dim % num_heads == 0, 'dim should be divisible by num_heads' self.num_heads = num_heads self.head_dim = dim // num_heads self.scale = self.head_dim ** -0.5 self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) if q_norm == 'layernorm' and qk_norm_weight == True: q_norm = 'w_layernorm' if k_norm == 'layernorm' and qk_norm_weight == True: k_norm = 'w_layernorm' self.q_norm = create_norm(q_norm, self.head_dim) self.k_norm = create_norm(k_norm, self.head_dim) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) self.rel_pos_embed = None if rel_pos_embed==None else rel_pos_embed.lower() self.add_rel_pe_to_v = add_rel_pe_to_v def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None, freqs_cos: Optional[torch.Tensor] = None, freqs_sin: Optional[torch.Tensor] = None, ) -> torch.Tensor: B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) # (B, n_h, N, D_h) q, k = self.q_norm(q), self.k_norm(k) if self.rel_pos_embed in ['rope', 'xpos']: # multiplicative rel_pos_embed if self.add_rel_pe_to_v: v = v * freqs_cos + rotate_half(v) * freqs_sin q = q * freqs_cos + rotate_half(q) * freqs_sin k = k * freqs_cos + rotate_half(k) * freqs_sin attn_mask = mask[:, None, None, :] # (B, N) -> (B, 1, 1, N) attn_mask = (attn_mask == attn_mask.transpose(-2, -1)) # (B, 1, 1, N) x (B, 1, N, 1) -> (B, 1, N, N) mask = torch.not_equal(mask, torch.zeros_like(mask)).to(mask) # (B, N) -> (B, N) if x.device.type == "cpu": x = F.scaled_dot_product_attention( q, k, v, attn_mask=attn_mask, dropout_p=self.attn_drop.p if self.training else 0., ) else: with torch.backends.cuda.sdp_kernel(enable_flash=True): ''' F.scaled_dot_product_attention is the efficient implementation equivalent to the following: attn_mask = torch.ones(L, S, dtype=torch.bool).tril(diagonal=0) if is_causal else attn_mask attn_mask = attn_mask.masked_fill(not attn_mask, -float('inf')) if attn_mask.dtype==torch.bool else attn_mask attn_weight = torch.softmax((Q @ K.transpose(-2, -1) / math.sqrt(Q.size(-1))) + attn_mask, dim=-1) attn_weight = torch.dropout(attn_weight, dropout_p) return attn_weight @ V In conclusion: boolean attn_mask will mask the attention matrix where attn_mask is False non-boolean attn_mask will be directly added to Q@K.T ''' x = F.scaled_dot_product_attention( q, k, v, attn_mask=attn_mask, dropout_p=self.attn_drop.p if self.training else 0., ) x = x.transpose(1, 2).reshape(B, N, C) x = x * mask[..., None] # mask: (B, N) -> (B, N, 1) x = self.proj(x) x = self.proj_drop(x) return x ################################################################################# # Basic FiT Module # ################################################################################# class FiTBlock(nn.Module): """ A DiT block with adaptive layer norm zero (adaLN-Zero) conditioning. """ def __init__(self, hidden_size, num_heads, mlp_ratio=4.0, swiglu=True, swiglu_large=False, rel_pos_embed=None, add_rel_pe_to_v=False, norm_layer: str = 'layernorm', q_norm: Optional[str] = None, k_norm: Optional[str] = None, qk_norm_weight: bool = False, qkv_bias=True, ffn_bias=True, adaln_bias=True, adaln_type='normal', adaln_lora_dim: int = None, **block_kwargs ): super().__init__() self.norm1 = create_norm(norm_layer, hidden_size) self.norm2 = create_norm(norm_layer, hidden_size) self.attn = Attention( hidden_size, num_heads=num_heads, rel_pos_embed=rel_pos_embed, q_norm=q_norm, k_norm=k_norm, qk_norm_weight=qk_norm_weight, qkv_bias=qkv_bias, add_rel_pe_to_v=add_rel_pe_to_v, **block_kwargs ) mlp_hidden_dim = int(hidden_size * mlp_ratio) if swiglu: if swiglu_large: self.mlp = SwiGLU(in_features=hidden_size, hidden_features=mlp_hidden_dim, bias=ffn_bias) else: self.mlp = SwiGLU(in_features=hidden_size, hidden_features=(mlp_hidden_dim*2)//3, bias=ffn_bias) else: self.mlp = Mlp(in_features=hidden_size, hidden_features=mlp_hidden_dim, act_layer=lambda: nn.GELU(approximate="tanh"), bias=ffn_bias) if adaln_type == 'normal': self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=adaln_bias) ) elif adaln_type == 'lora': self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(hidden_size, adaln_lora_dim, bias=adaln_bias), nn.Linear(adaln_lora_dim, 6 * hidden_size, bias=adaln_bias) ) elif adaln_type == 'swiglu': self.adaLN_modulation = SwiGLU( in_features=hidden_size, hidden_features=(hidden_size//4)*3, out_features=6*hidden_size, bias=adaln_bias ) def forward(self, x, c, mask, freqs_cos, freqs_sin, global_adaln=0.0): shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = (self.adaLN_modulation(c) + global_adaln).chunk(6, dim=1) x = x + gate_msa.unsqueeze(1) * self.attn(modulate(self.norm1(x), shift_msa, scale_msa), mask, freqs_cos, freqs_sin) x = x + gate_mlp.unsqueeze(1) * self.mlp(modulate(self.norm2(x), shift_mlp, scale_mlp)) return x class FinalLayer(nn.Module): """ The final layer of DiT. """ def __init__(self, hidden_size, patch_size, out_channels, norm_layer: str = 'layernorm', adaln_bias=True, adaln_type='normal'): super().__init__() self.norm_final = create_norm(norm_type=norm_layer, dim=hidden_size) self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True) if adaln_type == 'swiglu': self.adaLN_modulation = SwiGLU(in_features=hidden_size, hidden_features=hidden_size//2, out_features=2*hidden_size, bias=adaln_bias) else: # adaln_type in ['normal', 'lora'] self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=adaln_bias) ) def forward(self, x, c): shift, scale = self.adaLN_modulation(c).chunk(2, dim=1) x = modulate(self.norm_final(x), shift, scale) x = self.linear(x) return x class FiTTransformer2DModel(ModelMixin, ConfigMixin): """ FiT backbone as a Hugging Face Diffusers `ModelMixin` / `ConfigMixin` module. Checkpoints from the original FiT layout load with identical state dict keys. """ config_name = "config.json" _supports_gradient_checkpointing = True @register_to_config def __init__( self, context_size: int = 256, patch_size: int = 2, in_channels: int = 4, hidden_size: int = 1152, depth: int = 28, num_heads: int = 16, mlp_ratio: float = 4.0, class_dropout_prob: float = 0.1, num_classes: int = 1000, learn_sigma: bool = True, use_sit: bool = False, use_checkpoint: bool = False, use_swiglu: bool = False, use_swiglu_large: bool = False, rel_pos_embed: Optional[str] = "rope", norm_type: str = "layernorm", q_norm: Optional[str] = None, k_norm: Optional[str] = None, qk_norm_weight: bool = False, qkv_bias: bool = True, ffn_bias: bool = True, adaln_bias: bool = True, adaln_type: str = "normal", adaln_lora_dim: Optional[int] = None, rope_theta: float = 10000.0, custom_freqs: str = "normal", max_pe_len_h: Optional[int] = None, max_pe_len_w: Optional[int] = None, decouple: bool = False, ori_max_pe_len: Optional[int] = None, online_rope: bool = False, add_rel_pe_to_v: bool = False, pretrain_ckpt: Optional[str] = None, ignore_keys: Optional[list] = None, finetune: Optional[str] = None, time_shifting: int = 1, ): super().__init__() self.context_size = context_size self.hidden_size = hidden_size assert not (learn_sigma and use_sit) self.learn_sigma = learn_sigma self.use_sit = use_sit self.use_checkpoint = use_checkpoint self.depth = depth self.mlp_ratio = mlp_ratio self.class_dropout_prob = class_dropout_prob self.num_classes = num_classes self.in_channels = in_channels self.out_channels = self.in_channels * 2 if learn_sigma else in_channels self.patch_size = patch_size self.num_heads = num_heads self.adaln_type = adaln_type self.online_rope = online_rope self.time_shifting = time_shifting self.x_embedder = PatchEmbedder(in_channels * patch_size**2, hidden_size, bias=True) self.t_embedder = TimestepEmbedder(hidden_size) self.y_embedder = LabelEmbedder(num_classes, hidden_size, class_dropout_prob) self.rope_embedder = VisionRotaryEmbedding( head_dim=hidden_size // num_heads, theta=rope_theta, custom_freqs=custom_freqs, online_rope=online_rope, max_pe_len_h=max_pe_len_h, max_pe_len_w=max_pe_len_w, decouple=decouple, ori_max_pe_len=ori_max_pe_len, ) if adaln_type == "lora": self.global_adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=adaln_bias), ) else: self.global_adaLN_modulation = None self.blocks = nn.ModuleList( [ FiTBlock( hidden_size, num_heads, mlp_ratio=mlp_ratio, swiglu=use_swiglu, swiglu_large=use_swiglu_large, rel_pos_embed=rel_pos_embed, add_rel_pe_to_v=add_rel_pe_to_v, norm_layer=norm_type, q_norm=q_norm, k_norm=k_norm, qk_norm_weight=qk_norm_weight, qkv_bias=qkv_bias, ffn_bias=ffn_bias, adaln_bias=adaln_bias, adaln_type=adaln_type, adaln_lora_dim=adaln_lora_dim, ) for _ in range(depth) ] ) self.final_layer = FinalLayer( hidden_size, patch_size, self.out_channels, norm_layer=norm_type, adaln_bias=adaln_bias, adaln_type=adaln_type, ) self.initialize_weights(pretrain_ckpt=pretrain_ckpt, ignore=ignore_keys) if finetune is not None: self.apply_finetune(finetune_type=finetune, unfreeze=ignore_keys) def initialize_weights(self, pretrain_ckpt=None, ignore=None): def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.xavier_uniform_(module.weight) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init) w = self.x_embedder.proj.weight.data nn.init.xavier_uniform_(w.view([w.shape[0], -1])) nn.init.constant_(self.x_embedder.proj.bias, 0) nn.init.normal_(self.y_embedder.embedding_table.weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02) for block in self.blocks: if self.adaln_type in ["normal", "lora"]: nn.init.constant_(block.adaLN_modulation[-1].weight, 0) nn.init.constant_(block.adaLN_modulation[-1].bias, 0) elif self.adaln_type == "swiglu": nn.init.constant_(block.adaLN_modulation.fc2.weight, 0) nn.init.constant_(block.adaLN_modulation.fc2.bias, 0) if self.adaln_type == "lora": nn.init.constant_(self.global_adaLN_modulation[-1].weight, 0) nn.init.constant_(self.global_adaLN_modulation[-1].bias, 0) if self.adaln_type == "swiglu": nn.init.constant_(self.final_layer.adaLN_modulation.fc2.weight, 0) nn.init.constant_(self.final_layer.adaLN_modulation.fc2.bias, 0) else: nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0) nn.init.constant_(self.final_layer.linear.weight, 0) nn.init.constant_(self.final_layer.linear.bias, 0) keys = list(self.state_dict().keys()) ignore_keys = [] if ignore is not None: for ign in ignore: for key in keys: if ign in key: ignore_keys.append(key) ignore_keys = list(set(ignore_keys)) def unpatchify(self, x, hw): h, w = hw p = self.patch_size if self.use_sit: x = rearrange(x, "b (h w) c -> b h w c", h=h // p, w=w // p) x = rearrange(x, "b h w (c p1 p2) -> b c (h p1) (w p2)", p1=p, p2=p) else: x = rearrange(x, "b c (h w) -> b c h w", h=h // p, w=w // p) x = rearrange(x, "b (c p1 p2) h w -> b c (h p1) (w p2)", p1=p, p2=p) return x def forward(self, x, t, y, grid, mask, size=None): dtype = self.x_embedder.proj.weight.dtype x = x.to(dtype=dtype) mask = mask.to(dtype=dtype) # Flow-matching (FiTv2 / use_sit) expects t in [0, 1]. Improved diffusion (FiTv1) # passes integer timesteps 0..T-1 directly to TimestepEmbedder, like DiT. if self.use_sit: t = torch.clamp(self.time_shifting * t / (1 + (self.time_shifting - 1) * t), max=1.0) t = t.float().to(dtype) if not self.use_sit: x = rearrange(x, "B C N -> B N C") x = self.x_embedder(x) t = self.t_embedder(t) y = self.y_embedder(y, self.training) c = t + y if self.online_rope: freqs_cos, freqs_sin = self.rope_embedder.online_get_2d_rope_from_grid(grid, size) freqs_cos, freqs_sin = freqs_cos.unsqueeze(1), freqs_sin.unsqueeze(1) else: freqs_cos, freqs_sin = self.rope_embedder.get_cached_2d_rope_from_grid(grid) freqs_cos, freqs_sin = freqs_cos.unsqueeze(1), freqs_sin.unsqueeze(1) freqs_cos = freqs_cos.to(dtype=dtype) freqs_sin = freqs_sin.to(dtype=dtype) if self.global_adaLN_modulation is not None: global_adaln = self.global_adaLN_modulation(c) else: global_adaln = 0.0 if not self.use_checkpoint: for block in self.blocks: x = block(x, c, mask, freqs_cos, freqs_sin, global_adaln) else: for block in self.blocks: x = torch.utils.checkpoint.checkpoint( self.ckpt_wrapper(block), x, c, mask, freqs_cos, freqs_sin, global_adaln, use_reentrant=False ) x = self.final_layer(x, c) x = x * mask[..., None] if not self.use_sit: x = rearrange(x, "B N C -> B C N") return x def forward_with_cfg(self, x, t, y, grid, mask, size, cfg_scale, scale_pow=0.0): half = x[: len(x) // 2] combined = torch.cat([half, half], dim=0) model_out = self.forward(combined, t, y, grid, mask, size) C_cfg = 3 * self.patch_size * self.patch_size if self.use_sit: eps, rest = model_out[:, :, :C_cfg], model_out[:, :, C_cfg:] else: eps, rest = model_out[:, :C_cfg], model_out[:, C_cfg:] cond_eps, uncond_eps = torch.split(eps, len(eps) // 2, dim=0) if scale_pow == 0.0: real_cfg_scale = cfg_scale else: scale_step = (1 - torch.cos(((1 - torch.clamp_max(t, 1.0)) ** scale_pow) * torch.pi)) * 1 / 2 real_cfg_scale = (cfg_scale - 1) * scale_step + 1 real_cfg_scale = real_cfg_scale[: len(x) // 2].view(-1, 1, 1) if self.use_sit: t = t / (self.time_shifting + (1 - self.time_shifting) * t) half_eps = uncond_eps + real_cfg_scale * (cond_eps - uncond_eps) eps = torch.cat([half_eps, half_eps], dim=0) if self.use_sit: return torch.cat([eps, rest], dim=2) return torch.cat([eps, rest], dim=1) def ckpt_wrapper(self, module): def ckpt_forward(*inputs): return module(*inputs) return ckpt_forward def apply_finetune(self, finetune_type, unfreeze): if finetune_type == "full": return for _, param in self.named_parameters(): param.requires_grad = False if unfreeze is None: return for unf in unfreeze: for name, param in self.named_parameters(): if unf in name: param.requires_grad = True