Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 2 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/srt/layers/attention/attention_registry.py b/python/sglang/srt/layers/attention/attention_registry.pyindex 9af985d73765d55f812d40af43986ebdeb783619..f103c0543bc12993e699e5666b0584b07580f01a 100644--- a/python/sglang/srt/layers/attention/attention_registry.py+++ b/python/sglang/srt/layers/attention/attention_registry.py@@ -435,19 +435,10 @@ def attn_backend_wrapper(runner: "ModelRunner", full_attn_backend: "AttentionBac Lfm2VlConfig, ) if isinstance(mamba2_config(runner.model_config), short_conv_cfgs):- if is_npu():- # The model conv layers call- # get_attn_backend().conv_state_metadata() unconditionally,- # but the Ascend hybrid/mamba backend has no such method.- # Fail here (before model execution) with a clear message- # rather than an AttributeError deep in the first conv layer.- raise NotImplementedError(- "Short-conv hybrid models (ZAYA1 CCA, LFM2 / LFM2-MoE) "- "are not yet supported on NPU: the conv-state sidecar "- "(ShortConvAttnBackend.conv_state_metadata) has no Ascend "- "implementation. Add an Ascend conv-state backend before "- "serving these models on NPU."- )+ # On NPU, the model's conv kernel needs to transpose the+ # per-layer conv_state view (NPU pool stores it as+ # (slots, state_len, dim); the kernel wants (slots, dim, state_len)).+ # The plumbing backend itself is identical across CUDA and NPU. from sglang.srt.layers.attention.hybrid_linear_attn_backend import ( ShortConvHybridAttnBackend, )diff --git a/python/sglang/srt/models/lfm2.py b/python/sglang/srt/models/lfm2.pyindex 765bc6b39dd9b9aa95784f906aa89db9f8ded0ea..ae3b3150e313e35e8780f20a418c7f471f926041 100644--- a/python/sglang/srt/models/lfm2.py+++ b/python/sglang/srt/models/lfm2.py@@ -47,11 +47,28 @@ from sglang.srt.model_loader.weight_utils import ( sharded_weight_loader, ) from sglang.srt.runtime_context import get_parallel-from sglang.srt.utils import add_prefix, make_layers, set_weight_attrs+from sglang.srt.utils import add_prefix, is_npu, make_layers, set_weight_attrs logger = logging.getLogger(__name__) +def _npu_short_conv_view(conv_state: torch.Tensor) -> torch.Tensor:+ """Re-view the NPU conv-state so the causal_conv1d kernels see the layout they+ expect. CUDA pools store ``(num_slots, dim, state_len)`` directly; the NPU+ hybrid mamba pool stores ``(num_slots, state_len, dim)``. The kernel+ contract is the CUDA layout, so on NPU we hand back a transposed view and+ let the kernel scatter its writes through it (no copy needed).+ """+ if (+ is_npu()+ and conv_state.dim() == 3+ and conv_state.shape[2] > conv_state.shape[1]+ ):+ # (slots, state_len, dim) -> (slots, dim, state_len)+ return conv_state.transpose(1, 2)+ return conv_state++ class Lfm2MLP(nn.Module): """MLP with SwiGLU activation.""" @@ -274,6 +291,13 @@ class Lfm2ShortConv(nn.Module): meta = get_attn_backend().conv_state_metadata(self.layer_idx, forward_batch) conv_state = meta.layer_cache.conv[0] + # On NPU, the hybrid mamba pool stores conv_state as+ # ``(num_slots, state_len, dim)`` (the last two dims are swapped vs.+ # CUDA). The causal_conv1d kernels read ``(num_slots, dim, state_len)``+ # and write per-slot state back in place. A transpose view lets the+ # kernel scatter through the original memory without an explicit copy.+ conv_state_kernel = _npu_short_conv_view(conv_state)+ # Project and split into gates: B (pre-conv), C (post-conv), x (input) proj, _ = self.in_proj(hidden_states) B_gate, C_gate, x = proj.chunk(3, dim=-1)@@ -283,7 +307,7 @@ class Lfm2ShortConv(nn.Module): # Decode: single token per request, use optimized update kernel conv_out = causal_conv1d_update( Bx,- conv_state,+ conv_state_kernel, self.conv_weight, self.conv_bias, activation=None,@@ -299,7 +323,7 @@ class Lfm2ShortConv(nn.Module): query_start_loc=meta.query_start_loc, cache_indices=meta.cache_indices, has_initial_state=meta.has_initial_state,- conv_states=conv_state,+ conv_states=conv_state_kernel, activation=None, ).transpose(0, 1)