[diffusion] kernel: timestep embedding kernel implementation (#12995)

Co-authored-by: 戚余航 <qiyuhang@bytedance.com>
Co-authored-by: Qi Yuhang <45795032+HydraQYH@users.noreply.github.com>
This commit is contained in:
66RING
2025-12-19 20:59:50 +08:00
committed by GitHub
parent 1c65802648
commit 46be74b4b4
8 changed files with 369 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ from sgl_kernel.elementwise import (
rmsnorm,
rotary_embedding,
silu_and_mul,
timestep_embedding,
)
from sgl_kernel.expert_specialization import (
es_fp8_blockwise_scaled_grouped_mm,

View File

@@ -404,3 +404,35 @@ def concat_mla_absorb_q(
)
torch.ops.sgl_kernel.concat_mla_absorb_q(a, b, out)
return out
def timestep_embedding(
t: torch.Tensor,
dim: int,
flip_sin_to_cos: bool = False,
downscale_freq_shift: float = 0.0,
scale: float = 1,
max_period: int = 10000,
dtype: torch.dtype = torch.float32,
):
"""
Create sinusoidal timestep embeddings.
# TODO: review, output dtype always be float32. According to python code:
# sglang/python/sglang/multimodal_gen/runtime/layers/visual_embedding.py
Args:
t: Tensor of shape [B] with timesteps
dim: Embedding dimension
max_period: Controls the minimum frequency of the embeddings
Returns:
Tensor of shape [B, dim] with embeddings
"""
dtype = torch.float32
batch_size = t.shape[0]
output = torch.empty((batch_size, dim), dtype=dtype, device=t.device)
return torch.ops.sgl_kernel.timestep_embedding(
t, output, dim, flip_sin_to_cos, downscale_freq_shift, scale, max_period
)