[Cleanup] Extract shared SinusPositionEmbedding/DiTTimestepEmbedding/timestep_embedding layer (#3285)
Signed-off-by: Nick Cao <ncao@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
704b6bb6e6
commit
9de3806624
9 files changed
+109
-199
No files matched your search
@@ -21,7 +21,6 @@
|
||||
# MAE: https://github.com/facebookresearch/mae/blob/main/models_mae.py
|
||||
# --------------------------------------------------------
|
||||
import logging
|
||||
import math
|
||||
from functools import cached_property
|
||||
from queue import Queue
|
||||
from threading import Lock
|
||||
@@ -33,6 +32,8 @@ from transformers import PreTrainedTokenizerBase, Qwen2Config, Qwen2Model, Stati
|
||||
from vllm.logger import init_logger
|
||||
from x_transformers.x_transformers import RotaryEmbedding, apply_rotary_pos_emb
|
||||
|
||||
from vllm_omni.model_executor.layers.timestep_embedding import DiTTimestepEmbedding
|
||||
|
||||
from .audio_vae import AudioVAE
|
||||
|
||||
logger = init_logger(__name__)
|
||||
@@ -231,34 +232,6 @@ class FinalLayer(nn.Module):
|
||||
return x
|
||||
|
||||
|
||||
class SinusPositionEmbedding(nn.Module):
|
||||
def __init__(self, dim: int):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
|
||||
def forward(self, x: torch.Tensor, scale: float = 1000) -> torch.Tensor:
|
||||
device = x.device
|
||||
half_dim = self.dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, device=device).float() * -emb)
|
||||
emb = scale * x.unsqueeze(1) * emb.unsqueeze(0)
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||
return emb
|
||||
|
||||
|
||||
class TimestepEmbedder(nn.Module):
|
||||
def __init__(self, dim: int, freq_embed_dim: int = 256):
|
||||
super().__init__()
|
||||
self.time_embed = SinusPositionEmbedding(freq_embed_dim)
|
||||
self.time_mlp = nn.Sequential(nn.Linear(freq_embed_dim, dim), nn.SiLU(), nn.Linear(dim, dim))
|
||||
|
||||
def forward(self, timestep: torch.Tensor) -> torch.Tensor:
|
||||
time_hidden = self.time_embed(timestep)
|
||||
time_hidden = time_hidden.to(timestep.dtype)
|
||||
time = self.time_mlp(time_hidden)
|
||||
return time
|
||||
|
||||
|
||||
class CondEmbedder(nn.Module):
|
||||
"""Embeds LLM hidden states with optional CFG dropout."""
|
||||
|
||||
@@ -288,7 +261,7 @@ class DiT(nn.Module):
|
||||
self.out_channels = in_channels
|
||||
self.num_heads = num_heads
|
||||
|
||||
self.t_embedder = TimestepEmbedder(hidden_size)
|
||||
self.t_embedder = DiTTimestepEmbedding(hidden_size)
|
||||
self.x_embedder = nn.Linear(in_channels, hidden_size)
|
||||
self.c_embedder = CondEmbedder(llm_cond_dim, hidden_size)
|
||||
if "spk_dim" in kwargs:
|
||||
|
||||
@@ -34,6 +34,7 @@ from vllm.v1.outputs import SamplerOutput
|
||||
from vllm.v1.sample.metadata import SamplingMetadata
|
||||
from vllm.v1.sample.sampler import Sampler
|
||||
|
||||
from vllm_omni.model_executor.layers.timestep_embedding import DiTTimestepEmbedding
|
||||
from vllm_omni.model_executor.models.qwen2_5_omni.audio_length import cap_and_align_mel_length, resolve_max_mel_frames
|
||||
from vllm_omni.platforms import current_omni_platform
|
||||
|
||||
@@ -598,36 +599,6 @@ class DiTAttention(nn.Module):
|
||||
return attention_output
|
||||
|
||||
|
||||
# time step conditioning embedding
|
||||
class SinusPositionEmbedding(nn.Module):
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
|
||||
def forward(self, hidden_states, scale=1000):
|
||||
device = hidden_states.device
|
||||
half_dim = self.dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, device=device).float() * -emb)
|
||||
emb = scale * hidden_states.unsqueeze(1) * emb.unsqueeze(0)
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||
return emb.type_as(hidden_states)
|
||||
|
||||
|
||||
class DiTTimestepEmbedding(nn.Module):
|
||||
def __init__(self, dim, freq_embed_dim=256):
|
||||
super().__init__()
|
||||
self.time_embed = SinusPositionEmbedding(freq_embed_dim)
|
||||
self.time_mlp = nn.ModuleList([nn.Linear(freq_embed_dim, dim), nn.SiLU(), nn.Linear(dim, dim)])
|
||||
|
||||
def forward(self, timestep): # noqa: F821
|
||||
time_hidden = self.time_embed(timestep)
|
||||
time_hidden = time_hidden.to(timestep.dtype)
|
||||
for layer in self.time_mlp:
|
||||
time_hidden = layer(time_hidden) # b d
|
||||
return time_hidden
|
||||
|
||||
|
||||
class DiTDecoderLayer(nn.Module):
|
||||
def __init__(self, config: Qwen2_5OmniDiTConfig, look_ahead_block=0, look_backward_block=0):
|
||||
super().__init__()
|
||||
|
||||
+2
-30
@@ -26,6 +26,8 @@ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
|
||||
from transformers.utils import ModelOutput, auto_docstring, logging
|
||||
from transformers.utils.hub import cached_file
|
||||
|
||||
from vllm_omni.model_executor.layers.timestep_embedding import DiTTimestepEmbedding
|
||||
|
||||
from .configuration_qwen3_tts_tokenizer_v1 import (
|
||||
Qwen3TTSTokenizerV1Config,
|
||||
Qwen3TTSTokenizerV1DecoderBigVGANConfig,
|
||||
@@ -626,36 +628,6 @@ class DiTAttention(nn.Module):
|
||||
return attention_output
|
||||
|
||||
|
||||
# time step conditioning embedding
|
||||
class SinusPositionEmbedding(nn.Module):
|
||||
def __init__(self, dim):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
|
||||
def forward(self, hidden_states, scale=1000):
|
||||
device = hidden_states.device
|
||||
half_dim = self.dim // 2
|
||||
emb = math.log(10000) / (half_dim - 1)
|
||||
emb = torch.exp(torch.arange(half_dim, device=device).float() * -emb)
|
||||
emb = scale * hidden_states.unsqueeze(1) * emb.unsqueeze(0)
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
|
||||
return emb.type_as(hidden_states)
|
||||
|
||||
|
||||
class DiTTimestepEmbedding(nn.Module):
|
||||
def __init__(self, dim, freq_embed_dim=256):
|
||||
super().__init__()
|
||||
self.time_embed = SinusPositionEmbedding(freq_embed_dim)
|
||||
self.time_mlp = nn.ModuleList([nn.Linear(freq_embed_dim, dim), nn.SiLU(), nn.Linear(dim, dim)])
|
||||
|
||||
def forward(self, timestep):
|
||||
time_hidden = self.time_embed(timestep)
|
||||
time_hidden = time_hidden.to(timestep.dtype)
|
||||
for layer in self.time_mlp:
|
||||
time_hidden = layer(time_hidden) # b d
|
||||
return time_hidden
|
||||
|
||||
|
||||
class DiTDecoderLayer(nn.Module):
|
||||
def __init__(self, config: Qwen3TTSTokenizerV1DecoderBigVGANConfig, look_ahead_block=0, look_backward_block=0):
|
||||
super().__init__()
|
||||
|
||||
Reference in new issue
Block a user