Skip to content
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
6ef9d9f
fuse in_proj_ba and in_proj_qkvz if both quantized
mqhc2020 Jul 31, 2026
d31537f
Address GDN split kernel rows by stride, not by logical width
mqhc2020 Jul 31, 2026
a30fa2a
Merge the GDN in_proj_ba into in_proj_qkvz
mqhc2020 Jul 31, 2026
20937c6
Drop the fused AR+quant carry-over so this stands alone
mqhc2020 Aug 4, 2026
52a1df0
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Aug 4, 2026
124a03a
lint: use get_lora instead of get_server_args
mqhc2020 Aug 5, 2026
8e59f71
AMD gating
mqhc2020 Aug 7, 2026
1431c29
modify CI test script
mqhc2020 Aug 7, 2026
cba0bda
add test for linear merge
mqhc2020 Aug 13, 2026
bdb5eda
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Aug 13, 2026
8c1bd47
add a test case for #34421
mqhc2020 Aug 13, 2026
3b20731
trigger ci
bingxche Aug 14, 2026
cfa8fe7
Merge branch 'main' into marv/fuse_gdn_in_proj
sogalin Aug 19, 2026
aa8b7b8
Resolve conflict and lint
sogalin Aug 20, 2026
972202d
Fix conflict issue.
sogalin Aug 20, 2026
f31caa8
Remove the CPU unit test for this AMD-only feature
mqhc2020 Aug 24, 2026
54cd08e
Merge branch 'main' into marv/fuse_gdn_in_proj
bingxche Aug 26, 2026
b8056b0
add more gating: other platform can't even touch our dict and function
mqhc2020 Aug 28, 2026
93e88d3
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Aug 29, 2026
1dd8796
Cover the merged projection's strided column views in the decode fusi…
mqhc2020 Aug 30, 2026
5d9b677
fix Lean decode crash on the EAGLE path
mqhc2020 Aug 30, 2026
5607e2c
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Aug 31, 2026
668430d
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 1, 2026
76f74b9
change test method to improve accuracy
mqhc2020 Sep 2, 2026
c0aad7e
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 2, 2026
b637e9a
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 3, 2026
01891c9
remove unnecessary code
mqhc2020 Sep 4, 2026
e04983c
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 4, 2026
a5b75d1
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 4, 2026
1bb76db
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 4, 2026
8e9ad8a
refactoring to isolate AMD's own optimization code
mqhc2020 Sep 8, 2026
08d9735
add more gating in common code and avoid adding indents
mqhc2020 Sep 9, 2026
6d19b67
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 9, 2026
2a55c79
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 16, 2026
930dece
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 17, 2026
6d29cf5
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 22, 2026
e715237
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 24, 2026
15fdba9
Merge branch 'main' into marv/fuse_gdn_in_proj
yichiche Sep 25, 2026
09b4205
Merge branch 'main' into marv/fuse_gdn_in_proj
mqhc2020 Sep 29, 2026
9f21f20
Merge branch 'main' into marv/fuse_gdn_in_proj
yichiche Oct 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions python/sglang/kernels/ops/attention/triton_gdn_fused_proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def fused_qkvzba_split_reshape_cat_contiguous_kernel(
NUM_HEADS_V: tl.constexpr,
HEAD_QK: tl.constexpr,
HEAD_V: tl.constexpr,
QKVZ_STRIDE: tl.constexpr,
BA_STRIDE: tl.constexpr,
V_POW2: tl.constexpr,
):
i_bs, i_qk = tl.program_id(0), tl.program_id(1)
Expand All @@ -185,19 +187,17 @@ def fused_qkvzba_split_reshape_cat_contiguous_kernel(
TOTAL_Q: tl.constexpr = NUM_HEADS_QK * HEAD_QK
TOTAL_K: tl.constexpr = NUM_HEADS_QK * HEAD_QK
TOTAL_V: tl.constexpr = NUM_HEADS_V * HEAD_V
TOTAL_QKVZ: tl.constexpr = TOTAL_Q + TOTAL_K + TOTAL_V + TOTAL_V
TOTAL_BA: tl.constexpr = NUM_HEADS_V * 2

# ── Output dimensions ──
QKV_DIM_T: tl.constexpr = TOTAL_Q + TOTAL_K + TOTAL_V

# ── Read from contiguous input ──
# q for head group i_qk: in the all_q region, offset i_qk * HEAD_QK
blk_q_ptr = mixed_qkvz + i_bs * TOTAL_QKVZ + i_qk * HEAD_QK + tl.arange(0, HEAD_QK)
blk_q_ptr = mixed_qkvz + i_bs * QKVZ_STRIDE + i_qk * HEAD_QK + tl.arange(0, HEAD_QK)
# k for head group i_qk: in the all_k region
blk_k_ptr = (
mixed_qkvz
+ i_bs * TOTAL_QKVZ
+ i_bs * QKVZ_STRIDE
+ TOTAL_Q
+ i_qk * HEAD_QK
+ tl.arange(0, HEAD_QK)
Expand All @@ -209,7 +209,11 @@ def fused_qkvzba_split_reshape_cat_contiguous_kernel(
# vector access. V_POW2 arrives as a wrapper-computed constexpr so the
# dead branch is pruned before tl.arange validation.
v_ld_base = (
mixed_qkvz + i_bs * TOTAL_QKVZ + TOTAL_Q + TOTAL_K + i_qk * V_PER_GROUP * HEAD_V
mixed_qkvz
+ i_bs * QKVZ_STRIDE
+ TOTAL_Q
+ TOTAL_K
+ i_qk * V_PER_GROUP * HEAD_V
)
z_ld_base = v_ld_base + TOTAL_V

Expand Down Expand Up @@ -250,12 +254,12 @@ def fused_qkvzba_split_reshape_cat_contiguous_kernel(

# ── b and a from contiguous [all_b | all_a] ──
for i in tl.static_range(V_PER_GROUP):
blk_b_ptr = mixed_ba + i_bs * TOTAL_BA + i_qk * V_PER_GROUP + i
blk_b_ptr = mixed_ba + i_bs * BA_STRIDE + i_qk * V_PER_GROUP + i
blk_b_st_ptr = b + i_bs * NUM_HEADS_V + i_qk * V_PER_GROUP + i
tl.store(blk_b_st_ptr, tl.load(blk_b_ptr))

for i in tl.static_range(V_PER_GROUP):
blk_a_ptr = mixed_ba + i_bs * TOTAL_BA + NUM_HEADS_V + i_qk * V_PER_GROUP + i
blk_a_ptr = mixed_ba + i_bs * BA_STRIDE + NUM_HEADS_V + i_qk * V_PER_GROUP + i
blk_a_st_ptr = a + i_bs * NUM_HEADS_V + i_qk * V_PER_GROUP + i
tl.store(blk_a_st_ptr, tl.load(blk_a_ptr))

Expand All @@ -270,6 +274,9 @@ def fused_qkvzba_split_reshape_cat_contiguous(
):
"""Fused split/reshape/cat for CONTIGUOUS input format (Qwen3.5).

Rows are addressed by each tensor's own stride, so inputs may be column slices of
a wider projection.

Input layout:
mixed_qkvz: [all_q | all_k | all_v | all_z]
mixed_ba: [all_b | all_a]
Expand Down Expand Up @@ -323,6 +330,8 @@ def fused_qkvzba_split_reshape_cat_contiguous(
num_heads_v,
head_qk,
head_v,
mixed_qkvz.stride(0),
mixed_ba.stride(0),
V_POW2=(v_per_group & (v_per_group - 1)) == 0,
num_warps=num_warps,
num_stages=3,
Expand Down
190 changes: 154 additions & 36 deletions python/sglang/srt/models/qwen3_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
from sglang.srt.runtime_context import (
get_exec,
get_forward,
get_lora,
get_parallel,
get_stream,
)
Expand Down Expand Up @@ -136,9 +137,16 @@
_qknorm_use_alt_stream = _is_cuda or (
get_bool_env_var("SGLANG_QK_NORM_ALT_STREAM", "False") and _hip_use_alt_stream
)

# in_proj_ba is tiny and shares in_proj_qkvz's input; merging saves a launch. AMD-only.
_fuse_gdn_qkvzba = get_bool_env_var("SGLANG_GDN_FUSE_QKVZBA", "False") and _is_hip

# Tile-granularity heuristic for the a8w8 GEMM; correctness only needs N % 16.
_GEMM_N_ALIGN = 128
_gdn_decode_fused_proj_conv = (
_is_cuda and envs.SGLANG_ENABLE_GDN_DECODE_FUSED_PROJ_CONV.get()
)

_is_amx_available = cpu_has_amx_support()
_is_xpu = is_xpu()

Expand Down Expand Up @@ -270,6 +278,25 @@ def _linear_accepts_fp8_tuple(linear: nn.Module) -> bool:
)


def _gdn_input_proj_stacked_mapping(model: nn.Module):
separate = [
("in_proj_qkvz.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvz.", "in_proj_z.", 3),
("in_proj_ba.", "in_proj_b.", 0),
("in_proj_ba.", "in_proj_a.", 1),
]
if not _fuse_gdn_qkvzba:
return separate
if any("in_proj_qkvzba." in name for name, _ in model.named_parameters()):
return [
("in_proj_qkvzba.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvzba.", "in_proj_z.", 3),
("in_proj_qkvzba.", "in_proj_b.", 4),
("in_proj_qkvzba.", "in_proj_a.", 5),
]
return separate


def _select_fused_ar_input_for_linear(hidden_states, linear: nn.Module):
if not isinstance(hidden_states, tuple):
return hidden_states
Expand Down Expand Up @@ -362,33 +389,53 @@ def __init__(
self.conv1d.weight.data = self.conv1d.weight.data.unsqueeze(1)

# projection of the input hidden states
self.in_proj_qkvz = self.create_qkvz_proj(
hidden_size=self.hidden_size,
key_dim=self.key_dim,
value_dim=self.value_dim,
quant_config=quant_config,
prefix=add_prefix("in_proj_qkvz", prefix),
tp_rank=self.attn_tp_rank,
tp_size=self.attn_tp_size,
)
self.qkvz_width = (2 * self.key_dim + 2 * self.value_dim) // self.attn_tp_size
self.ba_width = (2 * self.num_v_heads) // self.attn_tp_size
self.in_proj_qkvz = None
self.in_proj_ba = None
self.in_proj_qkvzba = None
if _fuse_gdn_qkvzba:
self.in_proj_qkvzba = self.create_qkvzba_proj(
hidden_size=self.hidden_size,
key_dim=self.key_dim,
value_dim=self.value_dim,
num_v_heads=self.num_v_heads,
quant_config=quant_config,
prefix=add_prefix("in_proj_qkvzba", prefix),
tp_rank=self.attn_tp_rank,
tp_size=self.attn_tp_size,
)
if self.in_proj_qkvzba is None:
self.in_proj_qkvz = self.create_qkvz_proj(
hidden_size=self.hidden_size,
key_dim=self.key_dim,
value_dim=self.value_dim,
quant_config=quant_config,
prefix=add_prefix("in_proj_qkvz", prefix),
tp_rank=self.attn_tp_rank,
tp_size=self.attn_tp_size,
)

self.in_proj_ba = self.create_ba_proj(
hidden_size=self.hidden_size,
num_v_heads=self.num_v_heads,
quant_config=quant_config,
prefix=add_prefix("in_proj_ba", prefix),
tp_rank=self.attn_tp_rank,
tp_size=self.attn_tp_size,
)
self.in_proj_ba = self.create_ba_proj(
hidden_size=self.hidden_size,
num_v_heads=self.num_v_heads,
quant_config=quant_config,
prefix=add_prefix("in_proj_ba", prefix),
tp_rank=self.attn_tp_rank,
tp_size=self.attn_tp_size,
)

# Override weight loaders for packed checkpoint format.
# Important: for FP8, this must cover not only `.weight` but also
# `weight_scale_inv` / `weight_scale` / `input_scale` if present.
self._bind_packed_weight_loaders(self.in_proj_qkvz)
self._bind_packed_weight_loaders(self.in_proj_ba)
for proj in (self.in_proj_qkvzba, self.in_proj_qkvz, self.in_proj_ba):
if proj is not None:
self._bind_packed_weight_loaders(proj)
self._fused_input_proj_cpu_enabled = LazyValue(
lambda: (
_is_cpu
and self.in_proj_qkvz is not None
and self.in_proj_ba is not None
and self.in_proj_qkvz._parameters.get("weight") is not None
and self.in_proj_ba._parameters.get("weight") is not None
and self.in_proj_qkvz._parameters["weight"].dtype == torch.bfloat16
Expand Down Expand Up @@ -622,6 +669,72 @@ def create_ba_proj(
tp_size=tp_size,
)

def create_qkvzba_proj(
self,
hidden_size: int,
key_dim: int,
value_dim: int,
num_v_heads: int,
quant_config: QuantizationConfig | None,
prefix: str,
tp_rank: Optional[int] = None,
tp_size: Optional[int] = None,
) -> Optional[MergedColumnParallelLinear]:
"""Both input projections as one GEMM, or None to keep them separate."""
if not _fuse_gdn_qkvzba:
return None
lora = get_lora()
if bool(lora.lora_paths) or lora.enable_lora:
# supported_lora_modules names in_proj_qkvz, which the merge removes.
return None

output_sizes = [
key_dim,
key_dim,
value_dim,
value_dim,
num_v_heads,
num_v_heads,
]
shards = tp_size if tp_size is not None else 1
pad = (-(sum(output_sizes) // shards)) % _GEMM_N_ALIGN * shards
if pad:
output_sizes.append(pad)
try:
merged = MergedColumnParallelLinear(
input_size=hidden_size,
output_sizes=output_sizes,
bias=False,
quant_config=quant_config,
prefix=prefix,
tp_rank=tp_rank,
tp_size=tp_size,
)
except (ValueError, NotImplementedError) as e:
logger.info_once(f"in_proj_qkvz and in_proj_ba kept separate: {e}")
return None
weight = getattr(merged, "weight", None)
if pad and weight is not None:
# Nothing loads these rows.
weight.data[-(pad // shards) :].zero_()
return merged

@property
def qkvz_proj(self) -> nn.Module:
return (
self.in_proj_qkvzba
if self.in_proj_qkvzba is not None
else self.in_proj_qkvz
)

def _split_qkvzba(self, projected: torch.Tensor):
"""qkvz and ba views of the merged output, without the padding."""
ba_end = self.qkvz_width + self.ba_width
return (
projected[..., : self.qkvz_width],
projected[..., self.qkvz_width : ba_end],
)

def fix_query_key_value_ordering(
self,
mixed_qkvz: torch.Tensor,
Expand All @@ -645,6 +758,11 @@ def fix_query_key_value_ordering(
return query, key, value, z, b, a

def _forward_input_proj(self, hidden_states: torch.Tensor):
if self.in_proj_qkvzba is not None:
hs = _select_fused_ar_input_for_linear(hidden_states, self.in_proj_qkvzba)
projected_states, _ = self.in_proj_qkvzba(hs)
return self._split_qkvzba(projected_states)

# AMD/aiter fused AR+RMSNorm+per-group-quant path ships a
# ``(bf16, fp8, scale)`` 3-tuple so the FP8 ``in_proj_qkvz`` can
# consume ``(fp8, scale)`` (skipping its internal quant) while the
Expand Down Expand Up @@ -934,7 +1052,7 @@ def __init__(
# it. Otherwise, stay on the plain AR+RMSNorm path.
enable_fused_ar_quant = (
_enable_qwen35_fused_ar_quant()
and _linear_accepts_fp8_tuple(self.linear_attn.in_proj_qkvz)
and _linear_accepts_fp8_tuple(self.linear_attn.qkvz_proj)
)
self.layer_communicator = _layer_communicator_class(config, is_nextn)(
layer_scatter_modes=self.layer_scatter_modes,
Expand Down Expand Up @@ -1481,6 +1599,18 @@ class Qwen3_5ForCausalLM(nn.Module):
"gate_up_proj": ["gate_proj", "up_proj"],
"in_proj_qkvz": ["in_proj_qkv", "in_proj_z"],
"in_proj_ba": ["in_proj_b", "in_proj_a"],
**(
{
"in_proj_qkvzba": [
"in_proj_qkv",
"in_proj_z",
"in_proj_b",
"in_proj_a",
]
}
if _is_hip
else {}
),
}

supported_lora_modules = [
Expand Down Expand Up @@ -1810,10 +1940,7 @@ def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1),
# GDN
("in_proj_qkvz.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvz.", "in_proj_z.", 3),
("in_proj_ba.", "in_proj_b.", 0),
("in_proj_ba.", "in_proj_a.", 1),
*_gdn_input_proj_stacked_mapping(self),
]

loaded_params: Set[str] = set()
Expand Down Expand Up @@ -1899,10 +2026,7 @@ def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1),
# GDN
("in_proj_qkvz.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvz.", "in_proj_z.", 3),
("in_proj_ba.", "in_proj_b.", 0),
("in_proj_ba.", "in_proj_a.", 1),
*_gdn_input_proj_stacked_mapping(self),
]

# Params for weights, fp8 weight scales, fp8 activation scales
Expand Down Expand Up @@ -2165,10 +2289,7 @@ def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1),
# GDN fused projections
("in_proj_qkvz.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvz.", "in_proj_z.", 3),
("in_proj_ba.", "in_proj_b.", 0),
("in_proj_ba.", "in_proj_a.", 1),
*_gdn_input_proj_stacked_mapping(self),
]

loaded_params: Set[str] = set()
Expand Down Expand Up @@ -2335,10 +2456,7 @@ def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]):
("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1),
# GDN fused projections
("in_proj_qkvz.", "in_proj_qkv.", (0, 1, 2)),
("in_proj_qkvz.", "in_proj_z.", 3),
("in_proj_ba.", "in_proj_b.", 0),
("in_proj_ba.", "in_proj_a.", 1),
*_gdn_input_proj_stacked_mapping(self),
]

num_experts = self.config.num_experts
Expand Down
Loading
Loading