+20









Zhou Taichang
Claude Opus 4.7
NumberWan
dsinghvi
Dnoob
Yueqian Lin
Hongsheng Liu
knlnguyen1802
Samit
rein yang
Nick Cao
zhumingjue138
Ricardo Noriega
lyj-jjj
Cursor
gcanlin
wangyu
weizhoublue
weizhoublue
dengyunyang
汪志鹏
WeiQing Chen
Jie Liu
Yueqian Lin
NATURE
bjf-frz
amy-why-3459
Y. Fisher
Zeyu Huang | 黃澤宇
Copilot Autofix powered by AI
eb4e60ee64
Signed-off-by: tzhouam <tzhouam@connect.ust.hk> Signed-off-by: Zhou Taichang <tzhouam@connect.ust.hk> Signed-off-by: NumberWan <wantszkin2003@gmail.com> Signed-off-by: Divyansh Singhvi <divyanshsinghvi@gmail.com> Signed-off-by: dsinghvi <divyanshsinghvi@gmail.com> Signed-off-by: Dnoob <dxpouo@gmail.com> Signed-off-by: Yueqian Lin <70319226+linyueqian@users.noreply.github.com> Signed-off-by: knlnguyen1802 <knlnguyen1802@gmail.com> Signed-off-by: rein yang <ruiruyang2@gmail.com> Signed-off-by: Nick Cao <ncao@redhat.com> Signed-off-by: zhumingjue <zhumingjue@huawei.com> Signed-off-by: Ricardo Noriega De Soto <rnoriega@redhat.com> Signed-off-by: lyj-jjj <liuyingjun5@huawei.com> Signed-off-by: gcanlin <canlinguosdu@gmail.com> Signed-off-by: wangyu <410167048@qq.com> Signed-off-by: weizhoublue <weizhoublue@github.com> Signed-off-by: weizhou.lan@daocloud.io <weizhou.lan@daocloud.io> Signed-off-by: dengyunyang <584797741@qq.com> Signed-off-by: 汪志鹏 <wangzhipeng628@gmail.com> Signed-off-by: David Chen <530634352@qq.com> Signed-off-by: Jie Liu <33612777+keeper-jie@users.noreply.github.com> Signed-off-by: Yueqian Lin <linyueqian@outlook.com> Signed-off-by: princepride <wangzhipeng628@gmail.com> Signed-off-by: natureofnature <wzliu@connect.hku.hk> Signed-off-by: bjf-frz <frz123db@gmail.com> Signed-off-by: amy-why-3459 <wuhaiyan17@huawei.com> Signed-off-by: KexiongYu <yukexiong1@huawei.com> Signed-off-by: Huang, Zeyu <11222265+fhfuih@users.noreply.github.com> Signed-off-by: Zeyu Huang | 黃澤宇 <11222265+fhfuih@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: NumberWan <wantszkin2003@gmail.com> Co-authored-by: dsinghvi <divyanshsinghvi@gmail.com> Co-authored-by: Dnoob <dxpouo@gmail.com> Co-authored-by: Yueqian Lin <70319226+linyueqian@users.noreply.github.com> Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com> Co-authored-by: knlnguyen1802 <knlnguyen1802@gmail.com> Co-authored-by: Samit <285365963@qq.com> Co-authored-by: rein yang <73573651+R2-Y@users.noreply.github.com> Co-authored-by: Nick Cao <ncao@redhat.com> Co-authored-by: zhumingjue138 <zhumingjue@huawei.com> Co-authored-by: Ricardo Noriega <rnoriega@redhat.com> Co-authored-by: lyj-jjj <liuyingjun5@huawei.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: gcanlin <canlinguosdu@gmail.com> Co-authored-by: wangyu <53896905+yenuo26@users.noreply.github.com> Co-authored-by: weizhoublue <45163302+weizhoublue@users.noreply.github.com> Co-authored-by: weizhoublue <weizhoublue@github.com> Co-authored-by: dengyunyang <584797741@qq.com> Co-authored-by: 汪志鹏 <wangzhipeng628@gmail.com> Co-authored-by: WeiQing Chen <40507679+david6666666@users.noreply.github.com> Co-authored-by: Jie Liu <33612777+keeper-jie@users.noreply.github.com> Co-authored-by: Yueqian Lin <linyueqian@outlook.com> Co-authored-by: NATURE <wzliu@connect.hku.hk> Co-authored-by: bjf-frz <frz123db@gmail.com> Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com> Co-authored-by: Y. Fisher <yukexiong1@huawei.com> Co-authored-by: Zeyu Huang | 黃澤宇 <11222265+fhfuih@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
137 lines
4.8 KiB
Python
137 lines
4.8 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||
# Copyright 2025 The Qwen team.
|
||
"""Shared SnakeBeta activation for speech decoders (Qwen3-TTS, Qwen3-Omni Code2Wav)."""
|
||
|
||
import torch
|
||
from torch import nn
|
||
from torch.nn import Parameter
|
||
from vllm.logger import init_logger
|
||
|
||
logger = init_logger(__name__)
|
||
|
||
|
||
class SnakeBeta(nn.Module):
|
||
"""
|
||
A modified Snake function which uses separate parameters for the magnitude of the periodic components
|
||
Shape:
|
||
- Input: (B, C, T)
|
||
- Output: (B, C, T), same shape as the input
|
||
Parameters:
|
||
- alpha - trainable parameter that controls frequency
|
||
- beta - trainable parameter that controls magnitude
|
||
References:
|
||
- This activation function is a modified version based on this paper
|
||
by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
|
||
https://huggingface.co/papers/2006.08195
|
||
"""
|
||
|
||
_triton_kernel = None # None = untried, False = unavailable, callable = ready
|
||
_TRITON_MAX_BLOCK_T = 4096 # upper bound for time-axis tile size
|
||
|
||
@staticmethod
|
||
def _init_triton():
|
||
"""Load and JIT-compile the fused Triton kernel (once)."""
|
||
if SnakeBeta._triton_kernel is not None:
|
||
return SnakeBeta._triton_kernel is not False
|
||
try:
|
||
import triton
|
||
import triton.language as tl
|
||
except ImportError:
|
||
SnakeBeta._triton_kernel = False
|
||
return False
|
||
|
||
@triton.jit
|
||
def _kernel( # noqa: N803
|
||
x_ptr,
|
||
exp_alpha_ptr,
|
||
inv_beta_ptr,
|
||
out_ptr,
|
||
stride_b,
|
||
stride_c,
|
||
t_len,
|
||
block_t: tl.constexpr,
|
||
):
|
||
"""Fused SnakeBeta using precomputed exp(α) and 1/(exp(β)+ε)."""
|
||
bid = tl.program_id(0)
|
||
cid = tl.program_id(1)
|
||
t_off = tl.program_id(2) * block_t + tl.arange(0, block_t)
|
||
mask = t_off < t_len
|
||
|
||
x = tl.load(x_ptr + bid * stride_b + cid * stride_c + t_off, mask=mask, other=0.0)
|
||
ea = tl.load(exp_alpha_ptr + cid)
|
||
ib = tl.load(inv_beta_ptr + cid)
|
||
x_float = x.to(tl.float32)
|
||
sin_val = tl.sin(x_float * ea)
|
||
result = x + (ib * sin_val * sin_val).to(x.dtype)
|
||
|
||
tl.store(out_ptr + bid * stride_b + cid * stride_c + t_off, result, mask=mask)
|
||
|
||
SnakeBeta._triton_kernel = _kernel
|
||
return True
|
||
|
||
def __init__(self, in_features, alpha=1.0):
|
||
super().__init__()
|
||
self.in_features = in_features
|
||
|
||
self.alpha = Parameter(torch.zeros(in_features) * alpha)
|
||
self.beta = Parameter(torch.zeros(in_features) * alpha)
|
||
|
||
self.no_div_by_zero = 0.000000001
|
||
|
||
# Precomputed buffers (populated by precompute_exp_cache)
|
||
self.register_buffer("_exp_alpha", None, persistent=False)
|
||
self.register_buffer("_inv_beta", None, persistent=False)
|
||
|
||
def precompute_exp_cache(self):
|
||
"""Materialize exp(alpha) and 1/(exp(beta)+eps) as frozen buffers."""
|
||
with torch.no_grad():
|
||
self._exp_alpha = torch.exp(self.alpha).contiguous()
|
||
self._inv_beta = (1.0 / (torch.exp(self.beta) + self.no_div_by_zero)).contiguous()
|
||
|
||
@property
|
||
def _cached(self):
|
||
return self._exp_alpha is not None
|
||
|
||
def forward(self, hidden_states):
|
||
"""SnakeBeta := x + 1/b * sin^2(x*a)"""
|
||
if hidden_states.is_cuda and not torch.is_grad_enabled() and self._init_triton():
|
||
try:
|
||
return self._triton_forward(hidden_states)
|
||
except Exception:
|
||
logger.warning("Triton SnakeBeta failed, falling back to eager", exc_info=True)
|
||
SnakeBeta._triton_kernel = False
|
||
return self._eager_forward(hidden_states)
|
||
|
||
def _eager_forward(self, hidden_states):
|
||
if self._cached:
|
||
exp_alpha = self._exp_alpha.unsqueeze(0).unsqueeze(-1)
|
||
inv_beta = self._inv_beta.unsqueeze(0).unsqueeze(-1)
|
||
else:
|
||
exp_alpha = torch.exp(self.alpha).unsqueeze(0).unsqueeze(-1)
|
||
inv_beta = (1.0 / (torch.exp(self.beta) + self.no_div_by_zero)).unsqueeze(0).unsqueeze(-1)
|
||
hidden_states = hidden_states + inv_beta * torch.pow(torch.sin(hidden_states * exp_alpha), 2)
|
||
return hidden_states
|
||
|
||
def _triton_forward(self, x):
|
||
import triton
|
||
|
||
if not self._cached:
|
||
self.precompute_exp_cache()
|
||
|
||
x = x.contiguous()
|
||
B, C, T = x.shape
|
||
out = torch.empty_like(x)
|
||
block_t = min(triton.next_power_of_2(T), self._TRITON_MAX_BLOCK_T)
|
||
self._triton_kernel[(B, C, triton.cdiv(T, block_t))](
|
||
x,
|
||
self._exp_alpha,
|
||
self._inv_beta,
|
||
out,
|
||
x.stride(0),
|
||
x.stride(1),
|
||
t_len=T,
|
||
block_t=block_t,
|
||
)
|
||
return out
|