Upload Diffusion/DiT/dit.py with huggingface_hub
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324import torchimport torch.nn as nnimport torch.nn.functional as Ffrom huggingface_hub import PyTorchModelHubMixinfrom torch import Tensorfrom torch.nn import RMSNormimport numpy as np #from .config import DiaConfigfrom state import DecoderInferenceState, EncoderInferenceState, KVCachefrom transformers.modeling_outputs import CausalLMOutput,CausalLMOutputWithCrossAttentionsfrom safetensors.torch import load_filefrom config import Config import os import math from typing import Optionalfrom dataclasses import dataclass from transformers.modeling_outputs import ModelOutputfrom typing import Optional, Tuple from transformers import T5EncoderModelimport mathfrom einops import rearrangefrom convnext.convnext import ConvNeXtV2, IdentityConvNeXtV2from text_encoder.model import T5Encoderfrom scipy import statsfrom diffloss import DiffLoss
@dataclassclass QuoteTTSOutput(ModelOutput): """ Base class for masked language models outputs.
Args: loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided): Masked language modeling (MLM) loss. logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.
Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`): Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`.
Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. """
loss: Optional[torch.FloatTensor] = None mask_loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None labels: Optional[torch.FloatTensor] = None expressive_latents: Optional[torch.FloatTensor] = None labels_latents: Optional[torch.FloatTensor] = None hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None attentions: Optional[Tuple[torch.FloatTensor, ...]] = None cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None target_mask: Optional[Tuple[torch.FloatTensor, ...]] = None mu: Optional[torch.FloatTensor] = None logvar: Optional[torch.FloatTensor] = None
class SinusoidalPosEmb(nn.Module): def __init__(self, dim): super().__init__() self.dim = dim
def forward(self, x): device = x.device half_dim = self.dim // 2 emb = math.log(10000) / (half_dim - 1) emb = torch.exp(torch.arange(half_dim, device=device) * -emb) emb = x[:, None] * emb[None, :] * 1.0 emb = torch.cat((emb.sin(), emb.cos()), dim=-1) return emb
def _normalize_axes(axes: tuple[int, ...], ndim: int) -> tuple[int, ...]: return tuple(ax if ax >= 0 else ndim + ax for ax in axes)
class DenseGeneral(nn.Module): """ PyTorch equivalent of flax.linen.DenseGeneral with shapes defined at init.
Stores weights (`kernel`) in the same layout as Jax and uses torch.tensordot for the generalized matrix multiplication. Weight/bias shapes are calculated and parameters created during initialization based on config. `load_weights` validates shapes and copies data.
Attributes: axis (Tuple[int, ...]): Input axis or axes to contract. in_shapes (Tuple[int, ...]): Sizes of the input dimensions specified by `axis`. out_features (Tuple[int, ...]): Shape of the output features (non-contracted dims). use_bias (bool): Whether to add a bias term. weight (nn.Parameter): The kernel parameter. bias (Optional[nn.Parameter]): The bias parameter (if use_bias=True). """
def __init__( self, in_shapes: tuple[int, ...], out_features: tuple[int, ...], axis: tuple[int, ...] = (-1,), #weight_dtype: torch.dtype = None, #device: torch.device = None, ): super().__init__() self.in_shapes = in_shapes self.out_features = out_features self.axis = axis self.kernel_shape = self.in_shapes + self.out_features
# factory_kwargs = {"device": device, "dtype": weight_dtype} self.weight = nn.Parameter(torch.empty(self.kernel_shape)) torch.nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5)) # torch.nn.init.normal_(self.weight, std=.02) def forward(self, inputs: Tensor) -> Tensor: norm_axis = _normalize_axes(self.axis, inputs.ndim) kernel_contract_axes = tuple(range(len(norm_axis)))
output = torch.tensordot( inputs.to(self.weight.dtype), self.weight, dims=(norm_axis, kernel_contract_axes), ).to(inputs.dtype) return output
class MlpBlock(nn.Module): """MLP block using DenseGeneral."""
def __init__(self, embed_dim: int, intermediate_dim: int, out_dim:int=None): super().__init__()
self.wi_fused = DenseGeneral( in_shapes=(embed_dim,), out_features=(2, intermediate_dim), axis=(-1,), ) if out_dim is None : out_dim = embed_dim self.wo = DenseGeneral( in_shapes=(intermediate_dim,), out_features=(out_dim,), axis=(-1,), )
def forward(self, x: torch.Tensor) -> torch.Tensor: """Forward pass.""" fused_x = self.wi_fused(x)
gate = fused_x[..., 0, :] up = fused_x[..., 1, :]
hidden = torch.mul(F.silu(gate), up)
output = self.wo(hidden) return output
class LlamaAdaptiveRMSNorm(nn.Module): def __init__(self, hidden_size=1024, eps=1e-6, dim_cond=1024): super().__init__() self.to_weight = nn.Linear(dim_cond, hidden_size) nn.init.zeros_(self.to_weight.weight) nn.init.ones_(self.to_weight.bias) self.variance_epsilon = eps self._is_hf_initialized = True # disable automatic init
def forward(self, hidden_states, cond_embedding): input_dtype = hidden_states.dtype variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
weight = self.to_weight(cond_embedding) if len(weight.shape) == 2: weight = weight.unsqueeze(1)
return (weight * hidden_states).to(input_dtype)
class RotaryEmbedding(nn.Module): """Rotary Position Embedding (RoPE) implementation in PyTorch."""
def __init__( self, embedding_dims: int, min_timescale: int = 1, max_timescale: int = 10000, ): super().__init__() if embedding_dims % 2 != 0: raise ValueError("Embedding dim must be even for RoPE.") self.embedding_dims = embedding_dims self.min_timescale = min_timescale self.max_timescale = max_timescale
half_embedding_dim = embedding_dims // 2 fraction = (2.0 * torch.arange(0, half_embedding_dim)) / embedding_dims timescale = (self.min_timescale * (self.max_timescale / self.min_timescale) ** fraction).to(torch.float32) self.register_buffer("timescale", timescale, persistent=False)
def forward(self, inputs: torch.Tensor, position: torch.Tensor): """Applies RoPE.""" position = position.unsqueeze(-1).unsqueeze(-1) sinusoid_inp = position / self.timescale sin = torch.sin(sinusoid_inp) cos = torch.cos(sinusoid_inp) first_half, second_half = torch.chunk(inputs.to(torch.float32), 2, dim=-1) first_part = first_half * cos - second_half * sin second_part = second_half * cos + first_half * sin return torch.cat((first_part, second_part), dim=-1)
def apply_rope(self, inputs: torch.Tensor, sin: torch.Tensor, cos: torch.Tensor): first_half, second_half = torch.chunk(inputs.to(torch.float32), 2, dim=-1) first_part = first_half * cos - second_half * sin second_part = second_half * cos + first_half * sin return torch.cat((first_part, second_part), dim=-1) class selfAttention(nn.Module): """Attention using DenseGeneral."""
def __init__( self, config, q_embed_dim: int, kv_embed_dim: int, num_query_heads: int, num_kv_heads: int, head_dim: int, is_cross_attn: bool = False, out_embed_dim: int = None, output_attentions=False, ): super().__init__() self.num_query_heads = num_query_heads self.num_kv_heads = num_kv_heads self.head_dim = head_dim self.is_cross_attn = is_cross_attn self.output_dim = out_embed_dim if out_embed_dim is not None else q_embed_dim self.projected_query_dim = num_query_heads * head_dim if num_query_heads % num_kv_heads != 0: raise ValueError(f"num_query_heads ({num_query_heads}) must be divisible by num_kv_heads ({num_kv_heads})") self.num_gqa_groups = num_query_heads // num_kv_heads self.kv_embed_dim = kv_embed_dim self.q_embed_dim = q_embed_dim self.output_attentions = output_attentions self.dropout_rate = config.model.dropout_rate # self.dropout = nn.Dropout(config.dropout_rate) # --- Projection Layers using DenseGeneral --- self.q_proj = DenseGeneral( in_shapes=(q_embed_dim,), out_features=(num_query_heads, head_dim), axis=(-1,), ) self.k_proj = DenseGeneral( in_shapes=(kv_embed_dim,), out_features=(num_kv_heads, head_dim), axis=(-1,), ) self.v_proj = DenseGeneral( in_shapes=(kv_embed_dim,), out_features=(num_kv_heads, head_dim), axis=(-1,), ) self.o_proj = DenseGeneral( in_shapes=(num_query_heads, head_dim), out_features=(self.output_dim,), axis=(-2, -1), )
# --- Rotary Embedding --- self.rotary_emb = RotaryEmbedding( embedding_dims=self.head_dim, min_timescale=config.model.rope_min_timescale, max_timescale=config.model.rope_max_timescale, )
self.is_fused_qkv = False
def forward( self, X: torch.Tensor, # (B, T, D) T = 1 in AR generation q_positions: torch.Tensor, # (B, T) kv_positions: torch.Tensor = None, # (B, S) attn_mask: torch.Tensor = None, # None in Decoder self Attention, Valid mask in Others cache: KVCache = None, # None in Encoder, KVCache in Decoder prefill: bool = False, is_causal: bool = False, ) : """ Performs attention calculation with optional KV caching.
Args: Xq: Query tensor (B, T, D). T=1 during single-step decoding. Xkv: Key/Value source tensor (B, S, E). S=1 during single-step decoding for self-attn. q_positions: Positions for queries (B, T). kv_positions: Positions for keys/values (B, S). If None, uses q_positions. attn_mask: Attention mask. cache: KVCache. prefill: If True, use prefill mode.
Returns: A tuple containing: - output: The attention output tensor (B, T, output_dim). - present_kv: The K/V state to be cached for the next step ((B, N, S_new, H), (B, N, S_new, H)). For self-attn, S_new = S_past + S. For cross-attn, S_new = S_kv. """ if kv_positions is None: kv_positions = q_positions
original_dtype = X.dtype
Xq_BxTxNxH = self.q_proj(X) Xk_BxSxKxH = self.k_proj(X) Xv_BxSxKxH = self.v_proj(X)
position = q_positions.unsqueeze(-1).unsqueeze(-1) sinusoid_inp = position / self.rotary_emb.timescale sin = torch.sin(sinusoid_inp) cos = torch.cos(sinusoid_inp)
Xq_BxTxNxH = self.rotary_emb.apply_rope(Xq_BxTxNxH, sin, cos) Xk_BxSxKxH = self.rotary_emb.apply_rope(Xk_BxSxKxH, sin, cos)
Xq_BxNxTxH = Xq_BxTxNxH.transpose(1, 2)
attn_k = None attn_v = None
Xk_BxKxSxH = Xk_BxSxKxH.transpose(1, 2) # (B, K, S, H) Xv_BxKxSxH = Xv_BxSxKxH.transpose(1, 2) # (B, K, S, H)
if cache is None: attn_k = Xk_BxKxSxH attn_v = Xv_BxKxSxH else: attn_k, attn_v = cache.update(Xk_BxKxSxH, Xv_BxKxSxH, current_idx)
# print(Xq_BxNxTxH.size(), attn_k.size(), attn_v.size())
# print(attn_mask) attn_output = F.scaled_dot_product_attention( Xq_BxNxTxH, attn_k, attn_v, attn_mask=attn_mask if not is_causal else None, scale=None, enable_gqa=self.num_gqa_groups > 1, is_causal=is_causal, dropout_p=self.dropout_rate if self.training else 0.0 )
attn_output = attn_output.transpose(1, 2).contiguous() # (B, T, N, H) output = self.o_proj(attn_output)
return output.to(original_dtype) class CrossAttention(nn.Module): """Cross-Attention using DenseGeneral."""
def __init__( self, config, q_embed_dim: int, kv_embed_dim: int, num_query_heads: int, num_kv_heads: int, head_dim: int, out_embed_dim: int = None, output_attentions=False ): super().__init__() self.num_query_heads = num_query_heads self.num_kv_heads = num_kv_heads self.head_dim = head_dim self.output_dim = out_embed_dim if out_embed_dim is not None else q_embed_dim self.projected_query_dim = num_query_heads * head_dim if num_query_heads % num_kv_heads != 0: raise ValueError(f"num_query_heads ({num_query_heads}) must be divisible by num_kv_heads ({num_kv_heads})") self.num_gqa_groups = num_query_heads // num_kv_heads self.output_attentions=output_attentions self.dropout_rate = config.model.dropout_rate # --- Projection Layers using DenseGeneral --- self.q_proj = DenseGeneral( in_shapes=(q_embed_dim,), out_features=(num_query_heads, head_dim), axis=(-1,), ) self.k_proj = DenseGeneral( in_shapes=(kv_embed_dim,), out_features=(num_kv_heads, head_dim), axis=(-1,), ) self.v_proj = DenseGeneral( in_shapes=(kv_embed_dim,), out_features=(num_kv_heads, head_dim), axis=(-1,), ) self.o_proj = DenseGeneral( in_shapes=(num_query_heads, head_dim), out_features=(self.output_dim,), axis=(-2, -1), )
# --- Rotary Embedding --- self.rotary_emb = RotaryEmbedding( embedding_dims=self.head_dim, min_timescale=config.model.rope_min_timescale, max_timescale=config.model.rope_max_timescale, )
def forward( self, Xq: torch.Tensor, # (B, T, D) T = 1 in AR generation q_positions: torch.Tensor, # (B, T), Xkv: torch.Tensor = None, # (B, S) kv_positions: torch.Tensor = None, # (B, S) attn_mask: torch.Tensor = None, # None in Decoder self Attention, Valid mask in Others cache: KVCache = None, # None in Encoder, KVCache in Decoder is_causal: bool = False, ): """ Performs attention calculation with optional KV caching.
Args: Xq: Query tensor (B, T, D). T=1 during single-step decoding. Xkv: Key/Value source tensor (B, S, E). S=1 during single-step decoding for self-attn. q_positions: Positions for queries (B, T). kv_positions: Positions for keys/values (B, S). If None, uses q_positions. attn_mask: Attention mask. cache: KVCache.
Returns: A tuple containing: - output: The attention output tensor (B, T, output_dim). - present_kv: The K/V state to be cached for the next step ((B, N, S_new, H), (B, N, S_new, H)). For self-attn, S_new = S_past + S. For cross-attn, S_new = S_kv. """ if kv_positions is None: kv_positions = q_positions original_dtype = Xq.dtype
Xq_BxTxNxH = self.q_proj(Xq) Xq_BxTxNxH = self.rotary_emb(Xq_BxTxNxH, position=q_positions) Xq_BxNxTxH = Xq_BxTxNxH.transpose(1, 2)
attn_k = None attn_v = None if cache is not None : attn_k, attn_v = cache.k, cache.v else : attn_k = self.k_proj(Xkv) attn_v = self.v_proj(Xkv) attn_k = self.rotary_emb(attn_k, position=kv_positions) attn_k = attn_k.transpose(1, 2) attn_v = attn_v.transpose(1, 2)
attn_output = F.scaled_dot_product_attention( Xq_BxNxTxH, attn_k, attn_v, attn_mask=attn_mask if not is_causal else None, scale=None, enable_gqa=self.num_gqa_groups > 1, is_causal=is_causal, dropout_p=self.dropout_rate if self.training else 0.0 ) if self.output_attentions : attn_weight = attn_output @ torch.linalg.pinv(attn_v) attn_output = attn_output.transpose(1, 2).contiguous() # (B, T, N, H) output = self.o_proj(attn_output) if self.output_attentions : return output, attn_weight return output.to(original_dtype)
class EncoderLayer(nn.Module): """Transformer Encoder Layer using DenseGeneral."""
def __init__(self, config): super().__init__() self.config = config model_config = config.model enc_config = config.model.encoder embed_dim = enc_config.n_embd
# self.pre_sa_norm = RMSNorm( # embed_dim, # eps=model_config.normalization_layer_epsilon, # dtype=torch.float32, # ) self.pre_sa_norm = LlamaAdaptiveRMSNorm( hidden_size=embed_dim, dim_cond=embed_dim ) self.self_attention = selfAttention( config, q_embed_dim=embed_dim, kv_embed_dim=embed_dim, num_query_heads=enc_config.n_head, num_kv_heads=enc_config.n_head, head_dim=enc_config.head_dim, is_cross_attn=False, out_embed_dim=embed_dim, ) # self.post_sa_norm = RMSNorm( # embed_dim, # eps=model_config.normalization_layer_epsilon, # dtype=torch.float32, # ) self.post_sa_norm = LlamaAdaptiveRMSNorm( hidden_size=embed_dim, dim_cond=embed_dim ) self.mlp = MlpBlock(embed_dim=embed_dim, intermediate_dim=enc_config.n_hidden) self.dropout = nn.Dropout(config.model.dropout_rate)
def forward( self, x: torch.Tensor, state: EncoderInferenceState, cond_emb: torch.Tensor = None ) -> torch.Tensor: residual = x x_norm = self.pre_sa_norm(x, cond_embedding=cond_emb)
sa_out = self.self_attention( X=x_norm, q_positions=state.positions, kv_positions=state.positions, attn_mask=state.attn_mask, ) x = residual + self.dropout(sa_out)
residual = x x_norm = self.post_sa_norm(x, cond_embedding=cond_emb) mlp_out = self.mlp(x_norm) x = residual + self.dropout(mlp_out)
return x
class Decoder(nn.Module): """Transformer Decoder Stack using DenseGeneral."""
def __init__(self, config): super().__init__() self.config = config model_config = config.model dec_config = config.model.decoder data_config = config.data self.num_layers = dec_config.n_layer # self.embeddings = nn.Embedding(model_config.tgt_vocab_size, dec_config.n_embd) self.mask_ratio_generator = stats.truncnorm((config.model.mask_ratio_min - 1.0) / 0.25, 0, loc=1.0, scale=0.25)
self.sep_emb = nn.Parameter(torch.zeros(1, 1, dec_config.n_embd))# nn.Embedding(1, dec_config.n_embd) torch.nn.init.normal_(self.sep_emb, std=.02) self.mask_emb = nn.Parameter(torch.zeros(1, config.model.inp_dim))# nn.Embedding(1, config.model.inp_dim) torch.nn.init.normal_(self.mask_emb, std=.02)
self.embedding_dense = DenseGeneral( in_shapes=(dec_config.inp_dim,), out_features=(1, dec_config.n_embd), axis=(-1,), )
self.layers = nn.ModuleList( [DecoderLayer(config=config) for _ in range(self.num_layers)] )
# self.norm = RMSNorm( # dec_config.n_embd, # eps=model_config.normalization_layer_epsilon, # dtype=torch.float32, # ) self.norm = LlamaAdaptiveRMSNorm( hidden_size=embed_dim, dim_cond=embed_dim ) self.dropout = nn.Dropout(config.model.dropout_rate)
self.reconstructor = MlpBlock( embed_dim=dec_config.n_embd, intermediate_dim=dec_config.n_hidden, out_dim = dec_config.inp_dim ) def get_ids(self, text_input_ids, max_len, pad_value=1): bs, seq_len = text_input_ids.size() padding_size = max_len - seq_len padding_tensor = torch.empty(bs, padding_size, device=text_input_ids.device).fill_(pad_value).long() return torch.cat((text_input_ids, padding_tensor), dim=1) def mask_prob(self, t): return torch.sin(t * np.pi / 2).to(t.device) def get_t(self, x0) : t = torch.rand(x0.shape[0], device=x0.device, requires_grad=False) t = torch.clamp(t, 1e-5, 1.0) return t def mask_tgt_embeddings(self, x0): # x0: semantic tokens (B, T) t = self.get_t(x0) new_t = t mask_prob = self.mask_prob(new_t) # (B,) # if mask_prob[i] < 0.2, mask_prob[i] = 0.2 mask_prob = torch.where( mask_prob < 0.2, torch.ones_like(mask_prob) * 0.2, mask_prob ) # Add mask target_mask = torch.bernoulli(torch.ones_like(x0[:, :, 0]) * mask_prob[..., None]) # mask = torch.cat((torch.zeros_like(prefix), target_mask), dim=1) # mask xt = x0.clone()
# replace by pad embedding # pad_emb = self.mask_emb.repeat(x0.shape[0], x0.shape[1], 1).to(x0.dtype) #self.pad_emb(torch.zeros(1, dtype=torch.int32, device=x0.device)).squeeze(0) # torch.zeros(1, device=x0.device) xt[(target_mask==1)] = self.mask_emb.to(xt.dtype)
return xt, target_mask def random_masking(self, x, orders): # generate token mask bsz, seq_len, embed_dim = x.shape mask_rate = self.mask_ratio_generator.rvs(1)[0] num_masked_tokens = int(np.ceil(seq_len * mask_rate)) mask = torch.zeros(bsz, seq_len, device=x.device) mask = torch.scatter(mask, dim=-1, index=orders[:, :num_masked_tokens].to(x.device), src=torch.ones(bsz, seq_len, device=x.device)) return mask.long() def sample_orders(self, bsz, seq_len =32 ): # generate a batch of random generation orders orders = [] for _ in range(bsz): order = np.array(list(range(seq_len))) np.random.shuffle(order) orders.append(order) orders = torch.Tensor(np.array(orders)).long() return orders
def mask_input(self, x) : bs, seq_len, _ = x.size() orders = self.sample_orders(bs,seq_len) mask = self.random_masking(x, orders) xt = x.clone() xt[(mask==1)] = self.mask_emb.to(xt.dtype) return xt, mask def forward( self, enc_state: EncoderInferenceState, enc_out: torch.Tensor, quote_embs: torch.Tensor, dec_in : torch.Tensor, text_input_ids: torch.Tensor, labels:torch.Tensor = None) -> torch.Tensor: if self.training : # x_orig = dec_in # x, target_mask = self.mask_tgt_embeddings(dec_in) x, target_mask = self.mask_input(dec_in) # x = self.embedding_dense(x).squeeze(2) else : # x_orig = self.embedding_dense(dec_in).squeeze(2) # full of pad tokens # bs, seq_len, _ = dec_in.size() # x = self.mask_emb.unsqueeze(1).repeat(bs, seq_len,1)
# x = # x = self.embedding_dense(x.unsqueeze(1))#.squeeze(2) # x = pad_emb.unsqueeze(1).expand(dec_in.size(0),dec_in.size(1),1) target_mask=None x = dec_in x = self.embedding_dense(x).squeeze(2) # sep_emb = self.sep_emb(torch.zeros(1, dtype=torch.int32, device=dec_in.device)).expand(dec_in.size(0), 1, -1)
x = torch.cat((quote_embs, self.sep_emb.repeat(x.size(0), 1,1).to(x.dtype), x), dim = 1)
dec_in_dummy = self.get_ids(text_input_ids, max_len = x.size(1)) # print(dec_in_dummy) state = DecoderInferenceState.new( self.config, enc_state, enc_out, dec_in_dummy ) # print(state.attn_mask[1,0, 8:, 8:]) cross_attentions = () for i, layer in enumerate(self.layers): x, cattns = layer(x, state) cross_attentions += (cattns,) # Final Norm x = self.norm(x) # print(x[:,-32:].size())
# reconstructed_input = self.reconstructor(self.dropout(x[:,-32:]).unsqueeze(1)) reconstructed_input = self.reconstructor(self.dropout(x[:,-32:])).squeeze(2)
if self.training : loss1 = F.mse_loss( reconstructed_input[:,-32:][(target_mask==1)], dec_in[(target_mask==1)], reduction="mean", ) loss2 = F.l1_loss( reconstructed_input[:,-32:][(target_mask==1)], dec_in[(target_mask==1)], reduction="mean", ) mask_loss = loss1 + loss2 else : if labels is not None : loss1 = F.mse_loss( reconstructed_input[:,-32:], labels, reduction="mean", ) loss2 = F.l1_loss( reconstructed_input[:,-32:], labels, reduction="mean", ) mask_loss = loss1 + loss2 else : mask_loss = None
out = QuoteTTSOutput( logits=x, mask_loss=mask_loss, cross_attentions=cross_attentions, expressive_latents=reconstructed_input, target_mask=target_mask)#, kl_div_loss=loss_kl, mu=mu, logvar=logvar) return out
class Encoder(nn.Module): """Transformer Decoder Stack using DenseGeneral."""
def __init__(self, config): super().__init__() self.config = config model_config = config.model dec_config = config.model.decoder data_config = config.data self.num_layers = dec_config.n_layer # self.embeddings = nn.Embedding(model_config.tgt_vocab_size, dec_config.n_embd)
# self.embedding_dense = DenseGeneral( # in_shapes=(dec_config.inp_dim,), # out_features=(1, dec_config.n_embd), # axis=(-1,), # ) self.embedding_dense = nn.Linear(dec_config.inp_dim, dec_config.n_embd, bias=True) torch.nn.init.xavier_uniform_(self.embedding_dense.weight) torch.nn.init.constant_(self.embedding_dense.bias, 0) self.sep_emb = nn.Parameter(torch.zeros(1, 1, dec_config.n_embd))# nn.Embedding(1, dec_config.n_embd) torch.nn.init.normal_(self.sep_emb, std=.02)
# self.z_proj_ln = nn.LayerNorm(dec_config.n_embd, eps=1e-6) # self.encoder_pos_embed_learned = nn.Embedding(1024, dec_config.n_embd) # torch.nn.init.normal_(self.encoder_pos_embed_learned.weight.data, std=.02)
# self.ref_dense = DenseGeneral( # in_shapes=(dec_config.inp_dim * 32,), # out_features=(1, dec_config.n_embd), # axis=(-1,), # ) # self.embedding_dense = IdentityConvNeXtV2(in_chans=1, depths=[3, 3, 9, 3], dims=[96, 192, 384, 768], num_classes=1024)
self.layers = nn.ModuleList( [EncoderLayer(config=config) for _ in range(self.num_layers)] )
# self.norm = RMSNorm( # dec_config.n_embd, # eps=model_config.normalization_layer_epsilon, # dtype=torch.float32, # ) self.norm = LlamaAdaptiveRMSNorm( hidden_size=embed_dim, dim_cond=embed_dim ) self.dropout = nn.Dropout(config.model.dropout_rate)
def get_ids(self, text_input_ids, max_len, pad_value=1): bs, seq_len = text_input_ids.size() padding_size = max_len - seq_len padding_tensor = torch.empty(bs, padding_size, device=text_input_ids.device).fill_(pad_value).long() return torch.cat((text_input_ids, padding_tensor), dim=1) def forward( self, x : torch.Tensor, c: torch.Tensor) -> torch.Tensor:
bsz, seq_len, embed_dim = context_embs.shape x = self.embedding_dense(audio_in).squeeze(2) # ref_x = self.ref_dense(ref_in.view(x.size(0), -1)).squeeze(2) # print(context_embs.size(), x.size()) x = torch.cat((context_embs, self.sep_emb.repeat(bsz, 1,1).to(x.dtype), x), dim = 1) # x = x + ref_x mask_with_buffer = torch.cat([torch.zeros(bsz, seq_len + 1, device=x.device), mask], dim=1)
# positional embeddings to let the model know the initial positions # positions = torch.arange(x.size(1), device=x.device).unsqueeze(0).repeat(bsz,1).long() # x = x + self.encoder_pos_embed_learned(positions) # x = self.z_proj_ln(x) # dropping x = x[(1-mask_with_buffer).nonzero(as_tuple=True)].reshape(bsz, -1, embed_dim)
# state enc_in_dummy = self.get_ids(text_input_ids, max_len = x.size(1)) state = EncoderInferenceState.new( self.config, enc_in_dummy ) # print(state.attn_mask[1,0, 8:, 8:]) # cross_attentions = () for i, layer in enumerate(self.layers): x = layer(x, state) # cross_attentions += (cattns,) # Final Norm x = self.norm(x) # reconstructed_input = self.reconstructor(self.dropout(x[:,-32:])).squeeze(2) # gt_latents = audio_in.clone().detach() # x = x[:,-32:] + self.diffusion_pos_embed_learned # loss = self.forward_loss(x, gt_latents, target_mask) # out = QuoteTTSOutput( # logits=x, # mask_loss=None, # expressive_latents=None) #, kl_div_loss=loss_kl, mu=mu, logvar=logvar) 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].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): t_freq = self.timestep_embedding(t, self.frequency_embedding_size) t_emb = self.mlp(t_freq) return t_emb
class Conv1DFinalLayer(nn.Module): """ The final layer of CrossAttnDiT. """ def __init__(self, hidden_size, out_channels): super().__init__() self.norm_final = nn.GroupNorm(16,hidden_size) self.conv1d = nn.Conv1d(hidden_size, out_channels,kernel_size=1)
def forward(self, x): # x:(B,C,T) x = self.norm_final(x) x = self.conv1d(x) return x class Conv1DFinalLayer(nn.Module): """ The final layer of DiT. """ def __init__(self, hidden_size, out_channels): super().__init__() self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) # self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True) self.conv1d = nn.Conv1d(hidden_size, out_channels, kernel_size=1) self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True) ) def modulate(self, x, shift, scale): return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
def forward(self, x, c): shift, scale = self.adaLN_modulation(c).chunk(2, dim=1) x = self.modulate(self.norm_final(x), shift, scale) x = self.conv1d(x.transpose(1,2)) return x#.transpose(1,2) class EncoderDecoder( nn.Module,): def __init__(self, config): super().__init__() self.config = config
# self.encoder = T5Encoder.from_pretrained(config.model.base_encoder_path, config.model.ft_encoder_path).encoder self.context_encoder = T5EncoderModel.from_pretrained(config.model.base_encoder_path) # for p in self.context_encoder.parameters(): # p.requires_grad = False # self.context_encoder = self.context_encoder.eval() kernel_size=5 self.proj_in = self.proj_in = nn.Conv1d(config.model.inp_dim,config.model.encoder.n_embd,kernel_size=kernel_size,padding=kernel_size//2) self.layers = nn.ModuleList( [EncoderLayer(config=config) for _ in range(config.model.encoder.n_layer)] ) self.sep_emb = nn.Parameter(torch.zeros(1, 1, config.model.encoder.n_embd))# nn.Embedding(1, dec_config.n_embd) self.norm = LlamaAdaptiveRMSNorm( hidden_size=config.model.encoder.n_embd, dim_cond=config.model.encoder.n_embd )
self.timestep_embedding = TimestepEmbedder(config.model.encoder.n_embd) # here we *2 because we also learn sigma self.final_layer = Conv1DFinalLayer(config.model.encoder.n_embd, config.model.inp_dim * 2)
self.initialize_weights() def initialize_weights(self): # Initialize transformer layers: # 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) for n,m in self.named_modules() : if (isinstance(m, nn.Linear)) & ("context_encoder" not in n) : torch.nn.init.xavier_uniform_(m.weight) if m.bias is not None: nn.init.constant_(m.bias, 0) # self.apply(_basic_init) # Initialize timestep embedding MLP: nn.init.normal_(self.timestep_embedding.mlp[0].weight, std=0.02) nn.init.normal_(self.timestep_embedding.mlp[2].weight, std=0.02) nn.init.normal_(self.sep_emb, std=0.02)
nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0)
def get_ids(self, text_input_ids, max_len, pad_value=1): bs, seq_len = text_input_ids.size() padding_size = max_len - seq_len padding_tensor = torch.empty(bs, padding_size, device=text_input_ids.device).fill_(pad_value).long() return torch.cat((text_input_ids, padding_tensor), dim=1) def forward( self, x : torch.Tensor, t : torch.Tensor, y: torch.Tensor, ) :
# x: [B, inp_dim ,32] bsz, _ , _ = x.size() # diffusion step embedding diffusion_step_emb = self.timestep_embedding(t) # [B, H]
# context enc_state = EncoderInferenceState.new(self.context_encoder.config, y) c_emb = self.context_encoder(input_ids=y,attention_mask=enc_state.padding_mask).last_hidden_state # [ B, S, H] c = diffusion_step_emb + c_emb[:, -1] # audio in x = self.proj_in(x).transpose(1,2) # [ B, 32, H]
# transformer input # x = torch.cat((c_emb, self.sep_emb.repeat(bsz, 1,1).to(x.dtype), x), dim = 1) # [B, S+1+32, H] # get the right mask for encoder to avoid attend to pad tokens from context # enc_in_dummy = self.get_ids(y, max_len = x.size(1)) enc_in_dummy = torch.ones(bsz, 32, device=x.device) state = EncoderInferenceState.new( self.config, enc_in_dummy ) for i, layer in enumerate(self.layers): x = layer(x, state, cond_emb=c)
x = self.norm(x, cond_embedding=c)
x = x[:, -32:] x = self.final_layer(x, c=c) # [N, inp_dim * 2, 32] # print(x.size()) return x # gt_latents = transformer_in.clone().detach() # loss = self.forward_loss(z, gt_latents, mask) # out = QuoteTTSOutput( # logits=z, # loss=loss, # expressive_latents=None) # return out
@classmethod def from_pretrained(cls, path: str, config_path: str= None): if config_path: with open(config_path) as f : config = yaml.safe_load(f) else : config = Config() model = cls(config) model.load_state_dict(torch.load(os.path.join(path, "pytorch_model.bin"), map_location="cpu")) return model
# @torch.no_grad() # def sample_all(self, context, quote,num_iter=10, seq_len=32): # expressive_latents = self.sample_tokens(context,quote,num_iter,seq_len) # out = self.decoder()
def mask_by_order(mask_len, order, bsz, seq_len): masking = torch.zeros(bsz, seq_len) masking = torch.scatter(masking, dim=-1, index=order[:, :mask_len.long()], src=torch.ones(bsz, seq_len)).bool() return masking
def top_p_sample(logits, thres=0.9): k = math.ceil((1 - thres) * logits.shape[-1]) val, ind = logits.topk(k, dim=-1) probs = torch.full_like(logits, float("-inf")) probs.scatter_(2, ind, val) return probs
def log(t, eps=1e-10): return torch.log(t + eps)
def gumbel_noise(t): noise = torch.zeros_like(t).uniform_(0, 1) return -log(-log(noise))
def gumbel_sample(t, temperature=1.0, dim=-1): return ((t / max(temperature, 1e-10)) + gumbel_noise(t)).argmax(dim=dim)
def apply_top_k_only( logits: torch.Tensor, k: torch.Tensor,) -> torch.Tensor: """ Apply top-k mask to the logits.
This implementation doesn't involve sorting the entire vocab.
The logits tensor may be updated in-place. """ no_top_k_mask = k == logits.shape[1] # Set non-top-k rows to 1 so that we can gather. k = k.masked_fill(no_top_k_mask, 1) max_top_k = k.max() # topk.values tensor has shape [batch_size, max_top_k]. # Convert top k to 0-based index in range [0, max_top_k). k_index = k.sub_(1).unsqueeze(1) top_k_mask = logits.topk(max_top_k, dim=1).values.gather(1, k_index.long()) # Handle non-topk rows. top_k_mask.masked_fill_(no_top_k_mask.unsqueeze(1), -float("inf")) logits.masked_fill_(logits < top_k_mask, -float("inf")) return logits def apply_top_k_top_p( logits: torch.Tensor, k: Optional[torch.Tensor], p: Optional[torch.Tensor],) -> torch.Tensor: """Apply top-k and top-p masks to the logits.
If a top-p is used, this function will sort the logits tensor, which can be slow for large batches.
The logits tensor may be updated in-place. """ if p is None: if k is None: return logits
# Avoid sorting vocab for top-k only case. return apply_top_k_only(logits, k)
logits_sort, logits_idx = logits.sort(dim=-1, descending=False)
if k is not None: # Apply top-k. top_k_mask = logits_sort.size(1) - k.to(torch.long) # shape: B # Get all the top_k values. top_k_mask = logits_sort.gather(1, top_k_mask.unsqueeze(dim=1)) top_k_mask = logits_sort < top_k_mask logits_sort.masked_fill_(top_k_mask, -float("inf"))
if p is not None: # Apply top-p. probs_sort = logits_sort.softmax(dim=-1) probs_sum = torch.cumsum(probs_sort, dim=-1, out=probs_sort) top_p_mask = probs_sum <= 1 - p.unsqueeze(dim=1) # at least one top_p_mask[:, -1] = False logits_sort.masked_fill_(top_p_mask, -float("inf"))
# Re-sort the probabilities. logits = logits_sort.scatter(dim=-1, index=logits_idx, src=logits_sort) return logits
def _sample_next_token( logits_BCxV: torch.Tensor, temperature: float, top_p: float, top_k: int): if temperature in [0, None]: return torch.argmax(logits_BCxV, dim=-1) logits_BCxV = logits_BCxV / temperature logits = apply_top_k_top_p(logits_BCxV, torch.tensor([top_k]), torch.tensor([top_p])) final_probs_BCxV = torch.softmax(logits, dim=-1)
sampled_indices_BC = torch.multinomial(final_probs_BCxV, num_samples=1) sampled_indices_C = sampled_indices_BC.squeeze(-1) return sampled_indices_C # def _sample_next_token(# logits_BCxV: torch.Tensor,# temperature: float,# top_p: float,# top_k: int,# audio_eos_value: int,# ) -> torch.Tensor:# if temperature == 0.0:# return torch.argmax(logits_BCxV, dim=-1)
# logits_BCxV = logits_BCxV / temperature
# if audio_eos_value is not None and audio_eos_value >= 0:# top_logit_indices_BC = torch.argmax(logits_BCxV, dim=-1)# eos_not_highest_mask_BC = top_logit_indices_BC != audio_eos_value# mask_eos_unless_highest_BCxV = torch.zeros_like(logits_BCxV, dtype=torch.bool)# mask_eos_unless_highest_BCxV[eos_not_highest_mask_BC, audio_eos_value] = True# logits_BCxV = logits_BCxV.masked_fill(mask_eos_unless_highest_BCxV, -torch.inf)
# if top_k is not None:# _, top_k_indices_BCxV = torch.topk(logits_BCxV, k=top_k, dim=-1)# mask = torch.ones_like(logits_BCxV, dtype=torch.bool)# mask = mask.scatter(dim=-1, index=top_k_indices_BCxV, value=False)# logits_BCxV = logits_BCxV.masked_fill(mask, -torch.inf)
# if top_p < 1.0:# probs_BCxV = torch.softmax(logits_BCxV, dim=-1)# sorted_probs_BCxV, sorted_indices_BCxV = torch.sort(probs_BCxV, dim=-1, descending=True)# cumulative_probs_BCxV = torch.cumsum(sorted_probs_BCxV, dim=-1)
# sorted_indices_to_remove_BCxV = cumulative_probs_BCxV > top_p# sorted_indices_to_remove_BCxV = torch.roll(sorted_indices_to_remove_BCxV, shifts=1, dims=-1)# sorted_indices_to_remove_BCxV[..., 0] = torch.zeros_like(sorted_indices_to_remove_BCxV[..., 0])
# indices_to_remove_BCxV = torch.zeros_like(sorted_indices_to_remove_BCxV)# indices_to_remove_BCxV = indices_to_remove_BCxV.scatter(# dim=-1, index=sorted_indices_BCxV, src=sorted_indices_to_remove_BCxV# )# logits_BCxV = logits_BCxV.masked_fill(indices_to_remove_BCxV, -torch.inf)
# final_probs_BCxV = torch.softmax(logits_BCxV, dim=-1)
# sampled_indices_BC = torch.multinomial(final_probs_BCxV, num_samples=1)# sampled_indices_C = sampled_indices_BC.squeeze(-1)# return sampled_indices_C
# @torch.no_grad()# def generate(# self,# enc_in: torch.Tensor,# temperature: float,# top_p: float,# top_k: int,# output_attentions=False) :
# enc_in_uncond = torch.zeros_like(enc_in)# enc_in = torch.cat((enc_in, enc_in_uncond), dim=0) # [B, T]# enc_state = EncoderInferenceState.new(self.config, enc_in)# enc_out = self.encoder(enc_in, enc_state) # dec_in = torch.tensor([4096], device=enc_in.device).long().unsqueeze(0)# dec_in_uncond = torch.tensor([4096], device=enc_in.device).long().unsqueeze(0)# dec_in = torch.cat((dec_in, dec_in_uncond), dim=0) # dec_state = DecoderInferenceState.new(# self.config, enc_state, enc_out, dec_in# )# dec_state.cross_attn_mask = dec_state.cross_attn_mask[:,:,[0], :]# # Masking CA for CFG# dec_state.cross_attn_mask[-1,:,:, :] = False
# cnt = 0# cross_attns = ()# all_logits = ()# while cnt < 34 : # dec_state.prepare_step(0, cnt+1)# # Masking CA for CFG# dec_state.cross_attn_mask[-1,:,:, :] = False# out = self.decoder(dec_in, dec_state)# cross_attns += (out.cross_attentions, )# logits = out['logits']# # print(logits.size())# # print(logits[0])# # print(logits[1])# logits = logits[0] + self.config.model.cfg_val * (logits[0] - logits[1])# # logits = logits[0]# # print(logits.size())# all_logits += (logits,)
# ntp = _sample_next_token(# logits.squeeze(1)[-1],# temperature=temperature,# top_k=top_k,# top_p=top_p,# audio_eos_value=4097)# # print(dec_in.size(), ntp.size(), ntp)# # dec_in = torch.cat((dec_in, ntp.unsqueeze(0).view(-1,1)), dim=1)
# dec_in = torch.cat((dec_in, torch.stack((ntp.unsqueeze(0), ntp.unsqueeze(0)))), dim=1) # if ntp.item() == 4097 : # break# cnt += 1# return dec_in, all_logits, cross_attns # def _sample_next_token(# logits_BCxV: torch.Tensor,# temperature: float,# top_p: float,# top_k: int,# audio_eos_value: int,# ) -> torch.Tensor:# if temperature == 0.0:# return torch.argmax(logits_BCxV, dim=-1)
# logits_BCxV = logits_BCxV / temperature
# if audio_eos_value is not None and audio_eos_value >= 0:# top_logit_indices_BC = torch.argmax(logits_BCxV, dim=-1)# eos_not_highest_mask_BC = top_logit_indices_BC != audio_eos_value# mask_eos_unless_highest_BCxV = torch.zeros_like(logits_BCxV, dtype=torch.bool)# mask_eos_unless_highest_BCxV[eos_not_highest_mask_BC, audio_eos_value] = True# logits_BCxV = logits_BCxV.masked_fill(mask_eos_unless_highest_BCxV, -torch.inf)
# if top_k is not None:# _, top_k_indices_BCxV = torch.topk(logits_BCxV, k=top_k, dim=-1)# mask = torch.ones_like(logits_BCxV, dtype=torch.bool)# mask = mask.scatter(dim=-1, index=top_k_indices_BCxV, value=False)# logits_BCxV = logits_BCxV.masked_fill(mask, -torch.inf)
# if top_p < 1.0:# probs_BCxV = torch.softmax(logits_BCxV, dim=-1)# sorted_probs_BCxV, sorted_indices_BCxV = torch.sort(probs_BCxV, dim=-1, descending=True)# cumulative_probs_BCxV = torch.cumsum(sorted_probs_BCxV, dim=-1)
# sorted_indices_to_remove_BCxV = cumulative_probs_BCxV > top_p# sorted_indices_to_remove_BCxV = torch.roll(sorted_indices_to_remove_BCxV, shifts=1, dims=-1)# sorted_indices_to_remove_BCxV[..., 0] = torch.zeros_like(sorted_indices_to_remove_BCxV[..., 0])
# indices_to_remove_BCxV = torch.zeros_like(sorted_indices_to_remove_BCxV)# indices_to_remove_BCxV = indices_to_remove_BCxV.scatter(# dim=-1, index=sorted_indices_BCxV, src=sorted_indices_to_remove_BCxV# )# logits_BCxV = logits_BCxV.masked_fill(indices_to_remove_BCxV, -torch.inf)
# final_probs_BCxV = torch.softmax(logits_BCxV, dim=-1)
# sampled_indices_BC = torch.multinomial(final_probs_BCxV, num_samples=1)# sampled_indices_C = sampled_indices_BC.squeeze(-1)# return sampled_indices_C
Diffusion/DiT/dit.pyAdded+1324