Download native/modules.py from gatilin/showo2_1_5b_visual_flow_components: direct link, hf CLI and curl.
- Browser
- Download file 39.7 kB
-
https://huggingface.co/gatilin/showo2_1_5b_visual_flow_components/resolve/main/native/modules.py
- Command line
-
hf download hf://gatilin/showo2_1_5b_visual_flow_components/native/modules.py
-
curl -L -o modules.py https://huggingface.co/gatilin/showo2_1_5b_visual_flow_components/resolve/main/native/modules.py
39.7 kB
| import math | |
| import warnings | |
| from typing import List, Optional, Tuple, Union, TypedDict | |
| import torch | |
| import torch.nn.functional as F | |
| import torch.utils.checkpoint | |
| from torch import nn | |
| from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss | |
| from transformers.activations import ACT2FN | |
| from transformers.cache_utils import Cache, DynamicCache, StaticCache | |
| from transformers.modeling_attn_mask_utils import AttentionMaskConverter | |
| from transformers.generation import GenerationMixin | |
| from transformers.modeling_outputs import ( | |
| BaseModelOutputWithPast, | |
| CausalLMOutputWithPast, | |
| QuestionAnsweringModelOutput, | |
| SequenceClassifierOutputWithPast, | |
| TokenClassifierOutput, | |
| ) | |
| from transformers.modeling_flash_attention_utils import _flash_attention_forward | |
| from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS | |
| from transformers.modeling_utils import PreTrainedModel | |
| from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS | |
| from transformers.processing_utils import Unpack | |
| from transformers.utils import ( | |
| add_start_docstrings, | |
| add_start_docstrings_to_model_forward, | |
| is_flash_attn_2_available, | |
| is_flash_attn_greater_or_equal_2_10, | |
| logging, | |
| replace_return_docstrings, | |
| ) | |
| from transformers.configuration_utils import PretrainedConfig | |
| from torch.nn.attention.flex_attention import flex_attention, create_block_mask | |
| from torch.nn.attention.flex_attention import BlockMask | |
| if torch.cuda.is_available(): | |
| flex_attention = torch.compile(flex_attention) | |
| logger = logging.get_logger(__name__) | |
| class UndTransConfig(PretrainedConfig): | |
| def __init__(self): | |
| self.attn_implementation = 'sdpa' | |
| self.max_position_embeddings = 131072 | |
| self.hidden_size = 1024 | |
| self.intermediate_size = 4096 | |
| # self.hidden_size = 1152 | |
| # self.intermediate_size = 4608 | |
| self.num_attention_heads = 32 | |
| self.num_key_value_heads = 8 | |
| self.hidden_act = 'silu' | |
| self.rms_norm_eps = 1e-05 | |
| self.mlp_bias = False | |
| self.head_dim = 64 | |
| self.attention_bias = False | |
| self.attention_dropout = 0.0 | |
| self.rope_theta = 500000.0 | |
| self.rope_scaling = { | |
| "factor": 32.0, | |
| "high_freq_factor": 4.0, | |
| "low_freq_factor": 1.0, | |
| "original_max_position_embeddings": 8192, | |
| "rope_type": "llama3" | |
| } | |
| super().__init__() | |
| class DiffusionHeadConfig(PretrainedConfig): | |
| def __init__(self, hidden_size=2048, head_dim=64, intermediate_size=8192): | |
| self.attn_implementation = 'sdpa' | |
| self.max_position_embeddings = 131072 | |
| self.hidden_size = hidden_size | |
| self.intermediate_size = intermediate_size | |
| self.num_attention_heads = 32 | |
| self.num_key_value_heads = 8 | |
| self.hidden_act = 'silu' | |
| self.rms_norm_eps = 1e-05 | |
| self.mlp_bias = False | |
| self.head_dim = head_dim | |
| self.attention_bias = False | |
| self.attention_dropout = 0.0 | |
| self.rope_theta = 500000.0 | |
| self.rope_scaling = { | |
| "factor": 32.0, | |
| "high_freq_factor": 4.0, | |
| "low_freq_factor": 1.0, | |
| "original_max_position_embeddings": 8192, | |
| "rope_type": "llama3" | |
| } | |
| self.qk_norm = True | |
| super().__init__() | |
| def modulate(x, shift, scale): | |
| input_dtype = x.dtype | |
| x = x.to(torch.float32) | |
| shift = shift.to(torch.float32) | |
| scale = scale.to(torch.float32) | |
| if len(x.shape) != len(shift.shape): | |
| return (x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)).to(input_dtype) | |
| else: | |
| return (x * (1 + scale) + shift).to(input_dtype) | |
| class ModulatedAttentionBlock(nn.Module): | |
| def __init__(self, config: DiffusionHeadConfig, layer_idx: int): | |
| super().__init__() | |
| self.hidden_size = config.hidden_size | |
| self.self_attn = ATTENTION_CLASSES[config.attn_implementation](config=config, layer_idx=layer_idx) | |
| self.mlp = MLP(config) | |
| self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.adaLN_modulation = nn.Sequential( | |
| nn.SiLU(), | |
| nn.Linear( | |
| config.hidden_size, | |
| 6 * config.hidden_size, | |
| bias=True, | |
| ), | |
| ) | |
| nn.init.zeros_(self.adaLN_modulation[1].weight) | |
| nn.init.zeros_(self.adaLN_modulation[1].bias) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| adaln_input: torch.Tensor, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| position_ids: Optional[torch.LongTensor] = None, | |
| past_key_value: Optional[Cache] = None, | |
| output_attentions: Optional[bool] = False, | |
| use_cache: Optional[bool] = False, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 | |
| modality_positions: Optional[torch.Tensor] = None, | |
| **kwargs, | |
| ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: | |
| """ | |
| Args: | |
| hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` | |
| attention_mask (`torch.FloatTensor`, *optional*): | |
| attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, | |
| query_sequence_length, key_sequence_length)` if default attention is used. | |
| output_attentions (`bool`, *optional*): | |
| Whether or not to return the attentions tensors of all attention layers. See `attentions` under | |
| returned tensors for more detail. | |
| use_cache (`bool`, *optional*): | |
| If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding | |
| (see `past_key_values`). | |
| past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states | |
| cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): | |
| Indices depicting the position of the input sequence tokens in the sequence | |
| position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): | |
| Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, | |
| with `head_dim` being the embedding dimension of each attention head. | |
| kwargs (`dict`, *optional*): | |
| Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code | |
| into the model | |
| """ | |
| shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(adaln_input).chunk(6, | |
| dim=1) | |
| # We only modulate the image embeddings | |
| shift_msa_new, scale_msa_new, gate_msa_new, shift_mlp_new, scale_mlp_new, gate_mlp_new = \ | |
| torch.zeros_like(hidden_states), torch.zeros_like(hidden_states), torch.ones_like(hidden_states), \ | |
| torch.zeros_like(hidden_states), torch.zeros_like(hidden_states), torch.ones_like(hidden_states) | |
| for i, modality_batch in enumerate(modality_positions): | |
| for j, (offset, length) in enumerate(modality_batch): | |
| idx = i * modality_positions.size(1) + j | |
| shift_msa_new[i, offset: offset + length] = shift_msa[idx] | |
| scale_msa_new[i, offset: offset + length] = scale_msa[idx] | |
| gate_msa_new[i, offset: offset + length] = gate_msa[idx] | |
| shift_mlp_new[i, offset: offset + length] = shift_mlp[idx] | |
| scale_mlp_new[i, offset: offset + length] = scale_mlp[idx] | |
| gate_mlp_new[i, offset: offset + length] = gate_mlp[idx] | |
| # We only modulate the image embeddings | |
| residual = hidden_states | |
| hidden_states = modulate(self.input_layernorm(hidden_states), shift_msa_new, scale_msa_new) | |
| # Self Attention | |
| hidden_states, self_attn_weights, present_key_value = self.self_attn( | |
| hidden_states=hidden_states, | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_value=past_key_value, | |
| output_attentions=output_attentions, | |
| use_cache=use_cache, | |
| cache_position=cache_position, | |
| position_embeddings=position_embeddings, | |
| **kwargs, | |
| ) | |
| hidden_states = residual + gate_msa_new * hidden_states | |
| # Fully Connected | |
| residual = hidden_states | |
| hidden_states = modulate(self.post_attention_layernorm(hidden_states), shift_mlp_new, scale_mlp_new) | |
| hidden_states = self.mlp(hidden_states) | |
| hidden_states = residual + gate_mlp_new * hidden_states | |
| outputs = (hidden_states,) | |
| if output_attentions: | |
| outputs += (self_attn_weights,) | |
| if use_cache: | |
| outputs += (present_key_value,) | |
| return outputs | |
| def rotate_half(x): | |
| """Rotates half the hidden dims of the input.""" | |
| x1 = x[..., : x.shape[-1] // 2] | |
| x2 = x[..., x.shape[-1] // 2:] | |
| return torch.cat((-x2, x1), dim=-1) | |
| def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): | |
| """Applies Rotary Position Embedding to the query and key tensors. | |
| Args: | |
| q (`torch.Tensor`): The query tensor. | |
| k (`torch.Tensor`): The key tensor. | |
| cos (`torch.Tensor`): The cosine part of the rotary embedding. | |
| sin (`torch.Tensor`): The sine part of the rotary embedding. | |
| position_ids (`torch.Tensor`, *optional*): | |
| Deprecated and unused. | |
| unsqueeze_dim (`int`, *optional*, defaults to 1): | |
| The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and | |
| sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note | |
| that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and | |
| k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes | |
| cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have | |
| the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. | |
| Returns: | |
| `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. | |
| """ | |
| cos = cos.unsqueeze(unsqueeze_dim) | |
| sin = sin.unsqueeze(unsqueeze_dim) | |
| q_embed = (q * cos) + (rotate_half(q) * sin) | |
| k_embed = (k * cos) + (rotate_half(k) * sin) | |
| return q_embed, k_embed | |
| class RotaryEmbedding(nn.Module): | |
| def __init__( | |
| self, | |
| dim=None, | |
| max_position_embeddings=2048, | |
| base=10000, | |
| device=None, | |
| scaling_factor=1.0, | |
| rope_type="default", | |
| config: Optional[DiffusionHeadConfig] = None, | |
| ): | |
| super().__init__() | |
| # TODO (joao): remove the `if` below, only used for BC | |
| self.rope_kwargs = {} | |
| if config is None: | |
| logger.warning_once( | |
| "`RotaryEmbedding` can now be fully parameterized by passing the model config through the " | |
| "`config` argument. All other arguments will be removed in v4.46" | |
| ) | |
| self.rope_kwargs = { | |
| "rope_type": rope_type, | |
| "factor": scaling_factor, | |
| "dim": dim, | |
| "base": base, | |
| "max_position_embeddings": max_position_embeddings, | |
| } | |
| self.rope_type = rope_type | |
| self.max_seq_len_cached = max_position_embeddings | |
| self.original_max_seq_len = max_position_embeddings | |
| else: | |
| # BC: "rope_type" was originally "type" | |
| if config.rope_scaling is not None: | |
| self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type")) | |
| else: | |
| self.rope_type = "default" | |
| self.max_seq_len_cached = config.max_position_embeddings | |
| self.original_max_seq_len = config.max_position_embeddings | |
| self.config = config | |
| self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type] | |
| inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device, **self.rope_kwargs) | |
| self.register_buffer("inv_freq", inv_freq, persistent=False) | |
| self.original_inv_freq = self.inv_freq | |
| def _dynamic_frequency_update(self, position_ids, device): | |
| """ | |
| dynamic RoPE layers should recompute `inv_freq` in the following situations: | |
| 1 - growing beyond the cached sequence length (allow scaling) | |
| 2 - the current sequence length is in the original scale (avoid losing precision with small sequences) | |
| """ | |
| seq_len = torch.max(position_ids) + 1 | |
| if seq_len > self.max_seq_len_cached: # growth | |
| inv_freq, self.attention_scaling = self.rope_init_fn( | |
| self.config, device, seq_len=seq_len, **self.rope_kwargs | |
| ) | |
| self.register_buffer("inv_freq", inv_freq, persistent=False) # TODO joao: may break with compilation | |
| self.max_seq_len_cached = seq_len | |
| if seq_len < self.original_max_seq_len and self.max_seq_len_cached > self.original_max_seq_len: # reset | |
| self.register_buffer("inv_freq", self.original_inv_freq, persistent=False) | |
| self.max_seq_len_cached = self.original_max_seq_len | |
| def forward(self, x, position_ids): | |
| if "dynamic" in self.rope_type: | |
| self._dynamic_frequency_update(position_ids, device=x.device) | |
| # Core RoPE block | |
| inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) | |
| position_ids_expanded = position_ids[:, None, :].float() | |
| # Force float32 (see https://github.com/huggingface/transformers/pull/29285) | |
| device_type = x.device.type | |
| device_type = device_type if isinstance(device_type, str) and device_type != "mps" else "cpu" | |
| with torch.autocast(device_type=device_type, enabled=False): | |
| freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2) | |
| emb = torch.cat((freqs, freqs), dim=-1) | |
| cos = emb.cos() | |
| sin = emb.sin() | |
| # Advanced RoPE types (e.g. yarn) apply a post-processing scaling factor, equivalent to scaling attention | |
| cos = cos * self.attention_scaling | |
| sin = sin * self.attention_scaling | |
| return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype) | |
| def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: | |
| """ | |
| This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, | |
| num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) | |
| """ | |
| batch, num_key_value_heads, slen, head_dim = hidden_states.shape | |
| if n_rep == 1: | |
| return hidden_states | |
| hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) | |
| return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) | |
| class Attention(nn.Module): | |
| """Multi-headed attention from 'Attention Is All You Need' paper""" | |
| def __init__(self, config: DiffusionHeadConfig, layer_idx: Optional[int] = None): | |
| super().__init__() | |
| self.config = config | |
| self.layer_idx = layer_idx | |
| if layer_idx is None: | |
| logger.warning_once( | |
| f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will " | |
| "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` " | |
| "when creating this class." | |
| ) | |
| self.attention_dropout = config.attention_dropout | |
| self.hidden_size = config.hidden_size | |
| self.num_heads = config.num_attention_heads | |
| self.head_dim = getattr(config, "head_dim", self.hidden_size // self.num_heads) | |
| self.num_key_value_heads = config.num_key_value_heads | |
| self.num_key_value_groups = self.num_heads // self.num_key_value_heads | |
| self.max_position_embeddings = config.max_position_embeddings | |
| self.rope_theta = config.rope_theta | |
| self.is_causal = True | |
| self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=config.attention_bias) | |
| self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) | |
| self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=config.attention_bias) | |
| self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=config.attention_bias) | |
| self.qk_norm = config.qk_norm if "qk_norm" in config else False | |
| if self.qk_norm: | |
| self.q_norm = RMSNorm(config.hidden_size // self.num_heads) | |
| self.k_norm = RMSNorm(config.hidden_size // self.num_heads) | |
| # TODO (joao): remove in v4.46 (RoPE is computed in the model, not in the decoder layers) | |
| self.rotary_emb = RotaryEmbedding(config=self.config) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| position_ids: Optional[torch.LongTensor] = None, | |
| past_key_value: Optional[Cache] = None, | |
| output_attentions: bool = False, | |
| use_cache: bool = False, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 | |
| **kwargs, | |
| ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | |
| bsz, q_len, _ = hidden_states.size() | |
| query_states = self.q_proj(hidden_states) | |
| key_states = self.k_proj(hidden_states) | |
| value_states = self.v_proj(hidden_states) | |
| # use -1 to infer num_heads and num_key_value_heads as they may vary if tensor parallel is used | |
| query_states = query_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| key_states = key_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| value_states = value_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| if self.qk_norm: | |
| query_states = self.q_norm(query_states) | |
| key_states = self.k_norm(key_states) | |
| if position_embeddings is None: | |
| logger.warning_once( | |
| "The attention layers in this model are transitioning from computing the RoPE embeddings internally " | |
| "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " | |
| "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " | |
| "removed and `position_embeddings` will be mandatory." | |
| ) | |
| cos, sin = self.rotary_emb(value_states, position_ids) | |
| else: | |
| cos, sin = position_embeddings | |
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | |
| if past_key_value is not None: | |
| # sin and cos are specific to RoPE models; cache_position needed for the static cache | |
| cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} | |
| key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) | |
| key_states = repeat_kv(key_states, self.num_key_value_groups) | |
| value_states = repeat_kv(value_states, self.num_key_value_groups) | |
| attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim) | |
| if type(attention_mask) == BlockMask: | |
| raise NotImplementedError | |
| causal_mask = attention_mask | |
| attn_weights = attn_weights + causal_mask | |
| # if attention_mask is not None: # no matter the length, we just slice it | |
| # causal_mask = attention_mask[:, :, :, : key_states.shape[-2]] | |
| # attn_weights = attn_weights + causal_mask | |
| # upcast attention to fp32 | |
| attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype) | |
| attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training) | |
| attn_output = torch.matmul(attn_weights, value_states) | |
| if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): | |
| raise ValueError( | |
| f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" | |
| f" {attn_output.size()}" | |
| ) | |
| attn_output = attn_output.transpose(1, 2).contiguous() | |
| attn_output = attn_output.reshape(bsz, q_len, -1) | |
| attn_output = self.o_proj(attn_output) | |
| if not output_attentions: | |
| attn_weights = None | |
| return attn_output, attn_weights, past_key_value | |
| class SdpaAttention(Attention): | |
| """ | |
| Llama attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from | |
| `Attention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to | |
| SDPA API. | |
| """ | |
| # Adapted from Attention.forward | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| position_ids: Optional[torch.LongTensor] = None, | |
| past_key_value: Optional[Cache] = None, | |
| output_attentions: bool = False, | |
| use_cache: bool = False, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 | |
| **kwargs, | |
| ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | |
| if output_attentions: | |
| # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. | |
| logger.warning_once( | |
| "LlamaModel is using SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, " | |
| 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.' | |
| ) | |
| return super().forward( | |
| hidden_states=hidden_states, | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_value=past_key_value, | |
| output_attentions=output_attentions, | |
| use_cache=use_cache, | |
| cache_position=cache_position, | |
| position_embeddings=position_embeddings, | |
| ) | |
| bsz, q_len, _ = hidden_states.size() | |
| query_states = self.q_proj(hidden_states) | |
| key_states = self.k_proj(hidden_states) | |
| value_states = self.v_proj(hidden_states) | |
| # use -1 to infer num_heads and num_key_value_heads as they may vary if tensor parallel is used | |
| query_states = query_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| key_states = key_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| value_states = value_states.view(bsz, q_len, -1, self.head_dim).transpose(1, 2) | |
| if self.qk_norm: | |
| query_states = self.q_norm(query_states) | |
| key_states = self.k_norm(key_states) | |
| if position_embeddings is None: | |
| logger.warning_once( | |
| "The attention layers in this model are transitioning from computing the RoPE embeddings internally " | |
| "through `position_ids` (2D tensor with the indexes of the tokens), to using externally computed " | |
| "`position_embeddings` (Tuple of tensors, containing cos and sin). In v4.46 `position_ids` will be " | |
| "removed and `position_embeddings` will be mandatory." | |
| ) | |
| cos, sin = self.rotary_emb(value_states, position_ids) | |
| else: | |
| cos, sin = position_embeddings | |
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | |
| if past_key_value is not None: | |
| # sin and cos are specific to RoPE models; cache_position needed for the static cache | |
| cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} | |
| key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) | |
| key_states = repeat_kv(key_states, self.num_key_value_groups) | |
| value_states = repeat_kv(value_states, self.num_key_value_groups) | |
| causal_mask = attention_mask | |
| # if attention_mask is not None: | |
| # causal_mask = causal_mask[:, :, :, : key_states.shape[-2]] | |
| # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask, | |
| # Reference: https://github.com/pytorch/pytorch/issues/112577. | |
| if query_states.device.type == "cuda" and causal_mask is not None: | |
| query_states = query_states.contiguous() | |
| key_states = key_states.contiguous() | |
| value_states = value_states.contiguous() | |
| # We dispatch to SDPA's Flash Attention or Efficient kernels via this `is_causal` if statement instead of an inline conditional assignment | |
| # in SDPA to support both torch.compile's dynamic shapes and full graph options. An inline conditional prevents dynamic shapes from compiling. | |
| is_causal = True if causal_mask is None and q_len > 1 else False | |
| query_states = query_states.to(value_states.dtype) | |
| key_states = key_states.to(value_states.dtype) | |
| if type(attention_mask) == BlockMask: | |
| attn_output = flex_attention(query_states, key_states, value_states, block_mask=attention_mask) | |
| else: | |
| attn_output = torch.nn.functional.scaled_dot_product_attention( | |
| query_states, | |
| key_states, | |
| value_states, | |
| attn_mask=causal_mask, | |
| dropout_p=self.attention_dropout if self.training else 0.0, | |
| is_causal=is_causal, | |
| ) | |
| attn_output = attn_output.transpose(1, 2).contiguous() | |
| attn_output = attn_output.view(bsz, q_len, -1) | |
| attn_output = self.o_proj(attn_output) | |
| return attn_output, None, past_key_value | |
| class RMSNorm(nn.Module): | |
| def __init__(self, hidden_size, eps=1e-6): | |
| """ | |
| RMSNorm is equivalent to T5LayerNorm | |
| """ | |
| super().__init__() | |
| self.weight = nn.Parameter(torch.ones(hidden_size)) | |
| self.variance_epsilon = eps | |
| def forward(self, hidden_states): | |
| input_dtype = hidden_states.dtype | |
| hidden_states = hidden_states.to(torch.float32) | |
| variance = hidden_states.pow(2).mean(-1, keepdim=True) | |
| hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) | |
| return self.weight * hidden_states.to(input_dtype) | |
| def extra_repr(self): | |
| return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" | |
| ALL_LAYERNORM_LAYERS.append(RMSNorm) | |
| class MLP(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.config = config | |
| self.hidden_size = config.hidden_size | |
| self.intermediate_size = config.intermediate_size | |
| self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias) | |
| self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=config.mlp_bias) | |
| self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=config.mlp_bias) | |
| self.act_fn = ACT2FN[config.hidden_act] | |
| def forward(self, x): | |
| down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) | |
| return down_proj | |
| ATTENTION_CLASSES = { | |
| "eager": Attention, | |
| "sdpa": SdpaAttention, | |
| } | |
| class DecoderLayer(nn.Module): | |
| def __init__(self, config, layer_idx: int): | |
| super().__init__() | |
| self.hidden_size = config.hidden_size | |
| self.self_attn = ATTENTION_CLASSES[config.attn_implementation](config=config, layer_idx=layer_idx) | |
| self.mlp = MLP(config) | |
| self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| attention_mask: Optional[torch.Tensor] = None, | |
| position_ids: Optional[torch.LongTensor] = None, | |
| past_key_value: Optional[Cache] = None, | |
| output_attentions: Optional[bool] = False, | |
| use_cache: Optional[bool] = False, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 | |
| **kwargs, | |
| ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: | |
| """ | |
| Args: | |
| hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` | |
| attention_mask (`torch.FloatTensor`, *optional*): | |
| attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1, | |
| query_sequence_length, key_sequence_length)` if default attention is used. | |
| output_attentions (`bool`, *optional*): | |
| Whether or not to return the attentions tensors of all attention layers. See `attentions` under | |
| returned tensors for more detail. | |
| use_cache (`bool`, *optional*): | |
| If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding | |
| (see `past_key_values`). | |
| past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states | |
| cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): | |
| Indices depicting the position of the input sequence tokens in the sequence | |
| position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): | |
| Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, | |
| with `head_dim` being the embedding dimension of each attention head. | |
| kwargs (`dict`, *optional*): | |
| Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code | |
| into the model | |
| """ | |
| residual = hidden_states | |
| hidden_states = self.input_layernorm(hidden_states) | |
| # Self Attention | |
| hidden_states, self_attn_weights, present_key_value = self.self_attn( | |
| hidden_states=hidden_states, | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_value=past_key_value, | |
| output_attentions=output_attentions, | |
| use_cache=use_cache, | |
| cache_position=cache_position, | |
| position_embeddings=position_embeddings, | |
| **kwargs, | |
| ) | |
| hidden_states = residual + hidden_states | |
| # Fully Connected | |
| residual = hidden_states | |
| hidden_states = self.post_attention_layernorm(hidden_states) | |
| hidden_states = self.mlp(hidden_states) | |
| hidden_states = residual + hidden_states | |
| outputs = (hidden_states,) | |
| if output_attentions: | |
| outputs += (self_attn_weights,) | |
| if use_cache: | |
| outputs += (present_key_value,) | |
| return outputs | |
| from timm.layers.helpers import to_2tuple | |
| class PatchEmbed(nn.Module): | |
| """2D Image to Patch Embedding""" | |
| def __init__( | |
| self, | |
| patch_size=16, | |
| in_chans=3, | |
| embed_dim=768, | |
| kernel_size=None, | |
| padding=0, | |
| norm_layer=None, | |
| flatten=True, | |
| bias=True, | |
| ): | |
| super().__init__() | |
| kernel_size = kernel_size or patch_size | |
| patch_size = to_2tuple(patch_size) | |
| self.patch_size = patch_size | |
| self.flatten = flatten | |
| self.proj = nn.Conv2d( | |
| in_chans, embed_dim, kernel_size=kernel_size, stride=patch_size, bias=bias | |
| ) | |
| self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity() | |
| def forward(self, x): | |
| x = self.proj(x) | |
| if self.flatten: | |
| x = x.flatten(2).transpose(1, 2) # BCHW -> BNC | |
| 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 | |
| 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].float() * 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])], dim=-1) | |
| return embedding | |
| def forward(self, t, dtype): | |
| t_freq = self.timestep_embedding(t, self.frequency_embedding_size).to(dtype) | |
| t_emb = self.mlp(t_freq) | |
| return t_emb | |
| class FinalLayer(nn.Module): | |
| """ | |
| The final layer of DiT. | |
| """ | |
| def __init__(self, hidden_size, patch_size, out_channels): | |
| super().__init__() | |
| self.norm_final = RMSNorm(hidden_size) | |
| self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True) | |
| self.adaLN_modulation = nn.Sequential( | |
| nn.SiLU(), | |
| nn.Linear(hidden_size, 2 * hidden_size, bias=True) | |
| ) | |
| def forward(self, x, adaln_input, modality_positions): | |
| shift, scale = self.adaLN_modulation(adaln_input).chunk(2, dim=1) | |
| # We only modulate the image embeddings | |
| shift_new, scale_new = torch.zeros_like(x), torch.zeros_like(x) | |
| for i, modality_batch in enumerate(modality_positions): | |
| for j, (offset, length) in enumerate(modality_batch): | |
| idx = i * modality_positions.size(1) + j | |
| shift_new[i, offset: offset + length] = shift[idx] | |
| scale_new[i, offset: offset + length] = scale[idx] | |
| # We only modulate the image embeddings | |
| x = modulate(self.norm_final(x), shift_new, scale_new) | |
| x = self.linear(x) | |
| return x | |
| class UpdatedVisionTransformer(nn.Module): | |
| def __init__(self, model, del_last_layer=True): | |
| super().__init__() | |
| self.model = model | |
| if del_last_layer: | |
| del self.model.transformer.resblocks[-1] | |
| def forward(self, x: torch.Tensor): | |
| x = self.model.conv1(x) # shape = [*, width, grid, grid] | |
| x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] | |
| x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] | |
| x = torch.cat([self.model.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, | |
| device=x.device), x], | |
| dim=1) # shape = [*, grid ** 2 + 1, width] | |
| x = x + self.model.positional_embedding.to(x.dtype) | |
| x = self.model.ln_pre(x) | |
| x = x.permute(1, 0, 2) # NLD -> LND | |
| x = self.model.transformer(x) | |
| x = x.permute(1, 0, 2)[:, 1:] # LND -> NLD | |
| return x | |
| class CLIPVisionEncoder(nn.Module): | |
| def __init__(self, model, del_last_layer=False): | |
| super().__init__() | |
| self.model = model | |
| if del_last_layer: | |
| del self.model.transformer.resblocks[-1] | |
| def forward(self, x: torch.Tensor): | |
| x = self.model.conv1(x) # shape = [*, width, grid, grid] | |
| x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] | |
| x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] | |
| x = torch.cat([self.model.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, | |
| device=x.device), x], | |
| dim=1) # shape = [*, grid ** 2 + 1, width] | |
| x = x + self.model.positional_embedding.to(x.dtype) | |
| x = self.model.ln_pre(x) | |
| x = x.permute(1, 0, 2) # NLD -> LND | |
| x = self.model.transformer(x) | |
| x = x.permute(1, 0, 2)[:, 1:] # LND -> NLD | |
| return x | |
| class SigLipVisionEncoder(nn.Module): | |
| def __init__(self, model, del_last_layer=True): | |
| """ | |
| A wrapper for extracting features from the penultimate layer of a vision transformer model. | |
| Args: | |
| model: The pre-trained model (e.g., CLIP or SigLIP). | |
| del_last_layer (bool): Whether to delete the last layer of the vision transformer. | |
| """ | |
| super().__init__() | |
| self.model = model | |
| # Remove the text model (if not needed) | |
| if hasattr(self.model, "text_model"): | |
| del self.model.text_model | |
| # Remove the last layer of the vision transformer | |
| if del_last_layer and hasattr(self.model.vision_model, "encoder"): | |
| del self.model.vision_model.encoder.layers[-1] | |
| # Replace the classification head (if it exists) with an identity layer | |
| if hasattr(self.model.vision_model, "head"): | |
| self.model.vision_model.head = nn.Identity() | |
| if hasattr(self.model.vision_model, "post_layernorm"): | |
| self.model.vision_model.post_layernorm = nn.Identity() | |
| def forward(self, x): | |
| """ | |
| Forward pass to extract features from the penultimate layer. | |
| Args: | |
| x: Input image tensor (pixel values). | |
| Returns: | |
| Tensor: Features from the penultimate layer. | |
| """ | |
| return self.model.get_image_features(pixel_values=x) | |