Inspect · Source & workspace
Requested workspace inspection related to source & workspace.
1 call · #0001–#0001
echo <val>ls -l -a <val> <n> > <path>Kimi K3 · Serving
SGLang LFM2-1.2B model adapt serving 1dev · Model Adaptation
01 / PROCESS MAP
Each row shows one agent’s tool events in its own recorded order. Colors show broad activities; bar widths do not represent duration.
Main agentKimi K3 · 168 visible calls
02 / EVENT SEQUENCE
Requests are grouped by agent, activity and topic. Delegations open into child branches. Summaries describe visible requests; tool errors and missing responses are flagged. A recorded response does not establish command success or a performance improvement.
Scroll within this panel to view all events. Command sketches retain familiar programs and options. Values become <val> or <n>; paths, addresses and script bodies are hidden as <path>, <url> and <script>.
Requested workspace inspection related to source & workspace.
1 call · #0001–#0001
echo <val>ls -l -a <val> <n> > <path>Requested file and log inspection related to task requirements.
2 calls · #0002–#0003
Read <path>Requested workspace inspection and file and log inspection related to runtime processes.
1 call · #0004–#0004
find <path> -type <val>head -n <n>echo <flag>cat <path> <n> > <path>Requested file and log inspection related to source & workspace.
3 calls · #0005–#0007
Read <path>Requested environment setup or inspection and source searches related to source & workspace.
1 call · #0008–#0008
envgrep -E <val>sortRequested file and log inspection related to source & workspace.
1 call · #0009–#0009
Read <path>Requested workspace inspection, runtime inspection and file and log inspection related to device memory.
1 call · #0010–#0010
ls <path> <n> > <path>echo <flag>npu-smi info <n> >& <path>head -n <n>Requested source searches, file and log inspection and repository inspection related to source & workspace.
6 calls · #0011–#0016
grep -n <val> <path>Read <path>cd <path>git log --oneline <flag>git status <flag>head -n <n>ls <path>grep -i <val>echo <flag>grep -r -n <val> <path> <n> > <path>headRequested file and log inspection related to benchmark & validation setup.
1 call · #0017–#0017
cd <path><command> <args>cat <val>Requested file and log inspection related to source & workspace.
1 call · #0018–#0018
Read <path>Requested file and log inspection related to task requirements.
1 call · #0019–#0019
cd <path>head -c <n> <path>echoecho <val>Requested file and log inspection related to source & workspace.
1 call · #0020–#0020
Read <path>Requested file and log inspection related to task requirements.
1 call · #0021–#0021
cd <path>cat <path>echo <val>head -c <n> <path>Requested file and log inspection and source searches related to source & workspace.
4 calls · #0022–#0025
Read <path>cd <path>grep -r -n <val> <path> --include <path>head -n <n>Requested file and log inspection related to attention kernels.
3 calls · #0026–#0028
Read <path>Requested script or workload execution related to source & workspace.
1 call · #0029–#0029
python3 -c <script>Requested script or workload execution, file and log inspection and dependency and build commands related to dependencies & build setup.
1 call · #0030–#0030
python3 -c <script> <n> >& <path>headpip show <val> <n> > <path>head -n <n>ls <path>grep -i -E <val>Requested workspace inspection and file and log inspection related to attention kernels.
2 calls · #0031–#0032
ls <path> <path>Read <path>Requested workspace inspection, source searches and file and log inspection related to convolution & sequence boundaries.
4 calls · #0033–#0036
ls <path>echo <flag>ls <path> <n> > <path>grep -r -n <val> <path> --include <path> -lheadRead <path>cd <path>grep -r -n <val> <path> --include <path>grep -v <val>head -n <n>Requested source searches and file and log inspection related to attention kernels.
2 calls · #0037–#0038
cd <path>grep -n <val> <path>head -n <n>Read <path>Requested source searches and file and log inspection related to source & workspace.
3 calls · #0039–#0041
cd <path>grep -n <val> <path>headecho <val>grep -r -n <val> <path> <path> <n> > <path>head -n <n>Read <path>Requested file and log inspection and source searches related to attention kernels.
2 calls · #0042–#0043
Read <path>cd <path>grep -r -n <val> <path> <path>head -n <n>Requested file and log inspection related to source & workspace.
1 call · #0044–#0044
cd <path>sed -n <val> <path>Requested workspace file updates and environment setup or inspection related to prefix caching.
1 call · #0045–#0045
mkdir -p <path>cd <path>env <vars> nohup <path> -m <val> <flag> <args>echo <val>Requested execution waits, file and log inspection and source searches related to runtime processes.
3 calls · #0046–#0048
sleep <n>tail -n <n> <path>echo <val>grep -n -E <val> <path>tail -n <n>headtail -c <n> <path>head -n <n>Requested workspace inspection, repository inspection and file and log inspection related to source & workspace.
1 call · #0049–#0049
ls <path> <n> > <path>echo <flag>cd <path>git log --oneline <flag> <args>git status <flag> <args>headRequested source searches and file and log inspection related to runtime processes.
1 call · #0050–#0050
grep -n -E <val> <path>grep -v <val>head -n <n>echo <val>tail -c <n> <path>Requested source searches and file and log inspection related to convolution & sequence boundaries.
1 call · #0051–#0051
cd <path>grep -r -n <val> <path> <path>head -n <n>echo <val>sed -n <val> <path>diff <path> <path>Requested source searches and file and log inspection related to source & workspace.
3 calls · #0052–#0054
cd <path>grep -r -n <val> <path> <path> <path> <n> > <path>grep -v <val>head -n <n>sed -n <val> <path>echo <val>grep -n <val> <path>grep -r -n <val> <path> --include <path>headgrep -r -n <val> <path> --include <path> -lRequested file and log inspection related to attention kernels.
2 calls · #0055–#0056
Read <path>Requested code edits related to attention kernels.
3 calls · #0057–#0059
Edit <path>Requested source searches and file and log inspection related to device memory.
1 call · #0060–#0060
cd <path>grep -n <val> -A <n> <path>grep -n <val> <path> <path> <path> <path> <n> > <path>headRequested code edits related to attention kernels.
3 calls · #0061–#0063
Edit <path>Requested script or workload execution and file and log inspection related to attention kernels.
1 call · #0064–#0064
cd <path>python3 -c <script> <n> >& <path>tail -n <n>Requested process termination, execution waits and environment setup or inspection related to prefix caching.
1 call · #0065–#0065
pkill -f <val> <n> > <path>sleep <n>cd <path>env <vars> nohup <path> -m <val> <flag> <args>echo <val>Requested execution waits, runtime inspection and file and log inspection related to runtime processes.
2 calls · #0066–#0067
sleep <n>pgrep -f <val>echo <val>tail -c <n> <path> <n> > <path>tail -c <n> <path>echogrep <flag> <args>Requested runtime inspection, source searches and file and log inspection related to source & workspace.
1 call · #0068–#0068
ps <val>grep -E <val>grep -v <val>headls -l -a <path> <n> > <path>Requested environment setup or inspection related to prefix caching.
1 call · #0069–#0069
cd <path>env <vars> python3 -m <module> <flag> <args>Requested file and log inspection related to source & workspace.
1 call · #0070–#0070
Read <path>Requested file and log inspection and source searches related to runtime processes.
1 call · #0071–#0071
tail -c <n> <path>echogrep -n -E <val> <path>tail -n <n>Requested execution waits, file and log inspection and source searches related to runtime processes.
1 call · #0072–#0072
sleep <n>tail -c <n> <path>echoecho <val>grep -n -E <val> <path>grep -v -E <val>tail -n <n>Requested file and log inspection related to runtime processes.
1 call · #0073–#0073
sed -n <val> <path>Requested file and log inspection and source searches related to source & workspace.
2 calls · #0074–#0075
cd <path>sed -n <val> <path>grep -n <val> -A <n> <path>head -n <n>Requested environment setup or inspection and source searches related to source & workspace.
1 call · #0076–#0076
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested source searches and file and log inspection related to device memory.
1 call · #0077–#0077
cd <path>grep -n <val> -A <n> <path>grep -n -E <val>head -n <n>Requested environment setup or inspection and source searches related to convolution & sequence boundaries.
1 call · #0078–#0078
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested environment setup or inspection, source searches and file and log inspection related to convolution & sequence boundaries.
1 call · #0079–#0079
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>head -n <n>Requested file and log inspection related to attention kernels.
1 call · #0080–#0080
Read <path>Requested code edits related to attention kernels.
1 call · #0081–#0081
Edit <path>Requested environment setup or inspection related to prefix caching.
1 call · #0082–#0082
cd <path>env <vars> python3 -m <module> <flag> <args>Requested file and log inspection related to device memory.
1 call · #0083–#0083
cd <path>sed -n <val> <path>Requested execution waits, source searches and file and log inspection related to convolution & sequence boundaries.
1 call · #0084–#0084
sleep <n>grep -n <val> <path>head -n <n>grep -n -E <val> <path>tail -n <n>tail -c <n> <path>Requested file and log inspection and source searches related to device memory.
2 calls · #0085–#0086
cd <path>sed -n <val> <path>grep -n <val> -A <n> <path>head -n <n>Requested source searches and file and log inspection related to attention kernels.
6 calls · #0087–#0092
cd <path>grep -n <val> <path>head -n <n>sed -n <val> <path>Read <path>grep -i -E <val>headecho <val>grep -r -n <val> <path> --include <path>Requested code edits related to attention kernels.
3 calls · #0093–#0095
Edit <path>Requested environment setup or inspection and source searches related to attention kernels.
2 calls · #0096–#0097
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested process termination, execution waits and environment setup or inspection related to prefix caching.
1 call · #0098–#0098
pkill -f <val> <n> > <path>sleep <n>cd <path>env <vars> python3 -m <module> <flag> <args>Requested execution waits, source searches and file and log inspection related to runtime processes.
1 call · #0099–#0099
sleep <n>grep -n -E <val> <path>tail -n <n>curl -s -m <n> <url> <n> > <path>head -c <n>Requested runtime inspection and source searches related to prefix caching.
1 call · #0100–#0100
pgrep <flag> <args>grep -v <val>echo <flag>cd <path><command> <args>Requested execution waits, source searches and file and log inspection related to runtime processes.
1 call · #0101–#0101
sleep <n>grep -n -E <val> <path>tail -n <n>curl -s -m <n> <url> <n> > <path>head -c <n>Requested service requests and file and log inspection related to rollout generation.
1 call · #0102–#0102
curl -s -m <n> <url> -H <path> -d <val>head -c <n>echoRequested task coordination related to source & workspace.
1 call · #0103–#0103
Stop task <task>Requested execution waits, runtime inspection and file and log inspection related to runtime processes.
1 call · #0104–#0104
sleep <n>pgrep <flag> <args>headcurl -s -m <n> <url> > <path> <n> >& <path>echo <val>Requested script or workload execution related to benchmark & validation setup.
1 call · #0105–#0105
cd <path>bash <script>Requested file and log inspection related to source & workspace.
1 call · #0106–#0106
Read <path>Requested workspace inspection, file and log inspection and execution waits related to benchmark & validation setup.
2 calls · #0107–#0108
echo <val>ls <path> <n> > <path>echo <flag>tail -c <n> <path> <n> > <path>sleep <n>tail -c <n> <path>echols <path>Requested execution waits related to source & workspace.
1 call · #0109–#0109
Wait for <task>Requested script or workload execution and file and log inspection related to benchmark & validation setup.
1 call · #0110–#0110
cd <path>bash <script> <n> >& <path>tail -n <n>echo <val>python3 -c <script>Requested execution waits related to source & workspace.
1 call · #0111–#0111
Wait for <task>Requested script or workload execution related to benchmark & validation setup.
1 call · #0112–#0112
cd <path>bash <script> > <path> <n> >& <path>echo <val>Requested source searches and file and log inspection related to attention kernels.
2 calls · #0113–#0114
cd <path>grep -n <val> <path>head -n <n>echo <val>wc -l <path>sed -n <val> <path>Requested file and log inspection and workspace inspection related to source & workspace.
1 call · #0115–#0115
tail -c <n> <path> <n> > <path>ls <path> <n> > <path>Requested execution waits related to source & workspace.
1 call · #0116–#0116
Wait for <task>Requested script or workload execution related to benchmark & validation setup.
1 call · #0117–#0117
python3 -c <script>Requested source searches and file and log inspection related to attention kernels.
2 calls · #0118–#0119
cd <path>grep -n <val> <path>head -n <n>echo <val>sed -n <val> <path>Requested source searches and file and log inspection related to convolution & sequence boundaries.
1 call · #0120–#0120
cd <path>grep -r -n <val> -A <n> <path>head -n <n>echo <val>grep -n <val> <path>Requested source searches and file and log inspection related to attention kernels.
1 call · #0121–#0121
cd <path>grep -n <val> <path>head -n <n>echo <val>grep -n <val> <path> <path>headRequested source searches and file and log inspection related to device memory.
1 call · #0122–#0122
cd <path>grep -n <val> -A <n> <path>head -n <n>echo <val>sed -n <val> <path>Requested environment setup or inspection and source searches related to source & workspace.
1 call · #0123–#0123
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested file and log inspection related to attention kernels.
1 call · #0124–#0124
Read <path>Requested code edits related to attention kernels.
2 calls · #0125–#0126
Edit <path>Requested file and log inspection related to attention kernels.
2 calls · #0127–#0128
Read <path>Requested code edits related to attention kernels.
1 call · #0129–#0129
Edit <path>Requested file and log inspection related to attention kernels.
1 call · #0130–#0130
Read <path>Requested code edits related to attention kernels.
1 call · #0131–#0131
Edit <path>Requested file and log inspection related to attention kernels.
1 call · #0132–#0132
Read <path>Requested code edits related to attention kernels.
2 calls · #0133–#0134
Edit <path>Requested code edits related to source & workspace.
1 call · #0135–#0135
Edit <path>Requested code edits related to attention kernels.
1 call · #0136–#0136
Edit <path>Requested file and log inspection related to source & workspace.
1 call · #0137–#0137
cd <path>sed -n <val> <path>Requested code edits related to source & workspace.
1 call · #0138–#0138
Edit <path>Requested file and log inspection related to source & workspace.
1 call · #0139–#0139
Read <path>Requested code edits related to source & workspace.
1 call · #0140–#0140
Edit <path>Requested source searches and file and log inspection related to convolution & sequence boundaries.
1 call · #0141–#0141
cd <path>grep -n <val> <path>headRequested environment setup or inspection and source searches related to attention kernels.
1 call · #0142–#0142
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested script or workload execution related to benchmark & validation setup.
1 call · #0143–#0143
cd <path>bash <script>Requested file and log inspection related to source & workspace.
1 call · #0144–#0144
cd <path>sed -n <val> <path>Requested execution waits related to source & workspace.
1 call · #0145–#0145
Wait for <task>Requested tool activity related to prefix caching.
1 call · #0146–#0146
cd <path><command> <args>Requested execution waits, service requests and file and log inspection related to runtime processes.
1 call · #0147–#0147
sleep <n>curl -s -m <n> <url> > <path>echo <val>tail -c <n> <path>Requested environment setup or inspection, file and log inspection and service requests related to task requirements.
1 call · #0148–#0148
cd <path><command> <args>env <vars> python3 -m <module> <flag> <args>tail -n <n>curl -s -X <val> <url> <val>Requested execution waits related to source & workspace.
2 calls · #0149–#0150
Wait for <task>Requested file and log inspection and source searches related to runtime processes.
1 call · #0151–#0151
tail -c <n> <path>echogrep <flag> <args>grep -E <val> <path>tail -n <n>Requested file and log inspection and execution waits related to source & workspace.
2 calls · #0152–#0153
Read task output <task>Wait for <task>Requested workspace inspection and file and log inspection related to source & workspace.
1 call · #0154–#0154
ls -l -a <path> <n> > <path>headfind <path> -name <val> <n> > <path>Requested task coordination related to source & workspace.
1 call · #0155–#0155
Stop task <task>Requested task coordination related to source & workspace.
1 call · #0156–#0156
Stop task <task>Requested execution waits, service requests and runtime inspection related to device memory.
1 call · #0157–#0157
sleep <n>curl -s -m <n> <url> > <path> <n> >& <path>echo <val>npu-smi infogrep -E <val>head -n <n>Requested script or workload execution related to benchmark & validation setup.
1 call · #0158–#0158
cd <path>bash <script> > <path> <n> >& <path>echo <val>Requested workspace inspection and file and log inspection related to source & workspace.
1 call · #0159–#0159
ls -l -a <path> <n> > <path>headdu <flag> <args>Requested file and log inspection related to source & workspace.
2 calls · #0160–#0161
head -n <n> <path>echo <path><command> <args>head -n <n>cat <path>Requested environment setup or inspection and source searches related to source & workspace.
1 call · #0162–#0162
cd <path>env <vars> python3 -c <script> <n> >& <path>grep -v -E <val>Requested file and log inspection, script or workload execution and source searches related to source & workspace.
3 calls · #0163–#0165
head -n <n> <path>wc -l <path>cd <path>python3 -c <script>grep -r -n <val> <path>head -n <n>grep -r -n <val> <path> <path> <n> > <path>Requested dependency and build commands, source searches and file and log inspection related to dependencies & build setup.
1 call · #0166–#0166
which <val>pip list <n> > <path>grep -i -E <val>headRequested file and log inspection related to source & workspace.
1 call · #0167–#0167
Read task output <task>Requested execution waits, workspace inspection and source searches related to benchmark & validation setup.
1 call · #0168–#0168
sleep <n>ls <path>grep -E <val>tail -c <n> <path> <n> > <path>03 / CANDIDATE ARTIFACT
Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
@@ -1,5 +1,5 @@ import logging-from typing import Optional, Union+from typing import Any, List, NamedTuple, Optional, Union import torch from sgl_kernel_npu.mamba.mamba_state_update_triton import (@@ -12,10 +12,15 @@ from sglang.srt.layers.attention.hybrid_linear_attn_backend import ( HybridLinearAttnBackend, MambaAttnBackendBase, )+from sglang.srt.layers.attention.linear.short_conv_backend import ShortConvAttnBackend+from sglang.srt.layers.attention.mamba.causal_conv1d import (+ NpuConvPrefillPlan,+ _npu_build_prefill_plan,+) from sglang.srt.layers.attention.mamba.mamba2_metadata import ( ForwardMetadata, )-from sglang.srt.model_executor.forward_batch_info import ForwardMode+from sglang.srt.model_executor.forward_batch_info import ForwardBatch, ForwardMode from sglang.srt.model_executor.model_runner import ModelRunner from sglang.srt.speculative.eagle_info import EagleDraftInput, EagleVerifyInput from sglang.srt.speculative.spec_info import SpecInput@@ -217,6 +222,78 @@ class AscendMamba2AttnBackend(AscendMambaAttnBackendBase): pass +class AscendShortConvAttnBackend(ShortConvAttnBackend, AscendMambaAttnBackendBase):+ """Short-conv state sidecar for Ascend (LFM2 / LFM2-MoE on NPU).++ Combines the device-agnostic conv-state plumbing of+ :class:`ShortConvAttnBackend` (resolved once per step and handed to the+ model's conv layers via ``conv_state_metadata``) with the Ascend graph-state+ buffers of :class:`AscendMambaAttnBackendBase`. The conv kernels themselves+ come from ``sgl_kernel_npu.mamba.causal_conv1d`` (dispatched in+ ``sglang.srt.layers.attention.mamba.causal_conv1d``).+ """++ # The NPU causal_conv1d kernels take int32 slot indices directly; keep the+ # per-step index view int32 so no per-layer cast is needed.+ cache_indices_dtype: torch.dtype = torch.int32++ def __init__(self, model_runner: ModelRunner):+ super().__init__(model_runner)+ # Only ZAYA1's extend path consumes the host mirrors; LFM2-family conv+ # layers read device tensors only, so skip the per-step D2H sync.+ from sglang.srt.configs.zaya import ZayaConfig++ self.needs_extend_host_mirrors = isinstance(+ model_runner.model_config.hf_config, ZayaConfig+ )+ # Per-extend-step host-computed conv plan (shared by all conv layers);+ # built in init_forward_metadata and handed out via conv_state_metadata.+ self._prefill_plan: Optional[NpuConvPrefillPlan] = None++ def _reset_step_state(self):+ super()._reset_step_state()+ self._prefill_plan = None++ def init_forward_metadata(self, forward_batch: ForwardBatch):+ super().init_forward_metadata(forward_batch)+ mode = forward_batch.forward_mode+ if (+ mode.is_extend()+ and not mode.is_target_verify()+ and not mode.is_draft_extend_v2()+ and forward_batch.extend_seq_lens_cpu is not None+ and forward_batch.extend_prefix_lens_cpu is not None+ ):+ # conv pool layout on NPU: (num_slots, window, dim) -- window axis+ # is second-to-last.+ window = self.conv_states_shape[-2]+ self._prefill_plan = _npu_build_prefill_plan(+ [int(v) for v in forward_batch.extend_seq_lens_cpu],+ [int(v) for v in forward_batch.extend_prefix_lens_cpu],+ window,+ self.device,+ )++ def conv_state_metadata(+ self, layer_id: int, forward_batch: ForwardBatch+ ) -> "AscendShortConvMetadata":+ base = super().conv_state_metadata(layer_id, forward_batch)+ return AscendShortConvMetadata(*base, prefill_plan=self._prefill_plan)+++class AscendShortConvMetadata(NamedTuple):+ """ShortConvMetadata plus the NPU per-step prefill plan (see+ :class:`NpuConvPrefillPlan`). Field order must match ShortConvMetadata."""++ layer_cache: Any+ cache_indices: torch.Tensor+ query_start_loc: Optional[torch.Tensor] = None+ has_initial_state: Optional[torch.Tensor] = None+ slot_ids_cpu: Optional[List[int]] = None+ has_prefix_cpu: Optional[List[bool]] = None+ prefill_plan: Optional[NpuConvPrefillPlan] = None++ class AscendHybridLinearAttnBackend(HybridLinearAttnBackend): def __init__( self,@@ -436,26 +436,21 @@ def attn_backend_wrapper(runner: "ModelRunner", full_attn_backend: "AttentionBac ) 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."+ from sglang.srt.hardware_backend.npu.attention.ascend_hybrid_linear_attn_backend import (+ AscendShortConvAttnBackend, )++ linear_attn_backend = AscendShortConvAttnBackend(runner)+ else:+ from sglang.srt.layers.attention.linear.short_conv_backend import (+ ShortConvAttnBackend,+ )++ linear_attn_backend = ShortConvAttnBackend(runner) from sglang.srt.layers.attention.hybrid_linear_attn_backend import ( ShortConvHybridAttnBackend, )- from sglang.srt.layers.attention.linear.short_conv_backend import (- ShortConvAttnBackend,- ) - linear_attn_backend = ShortConvAttnBackend(runner) hybrid_backend_cls = ShortConvHybridAttnBackend else: linear_attn_backend = Mamba2AttnBackend(runner)@@ -5,9 +5,10 @@ # Copyright (c) 2024, Tri Dao. # Adapted from https://github.com/Dao-AILab/causal-conv1d/blob/main/causal_conv1d/causal_conv1d_interface.py -from typing import Optional+from typing import List, NamedTuple, Optional import torch+import torch.nn.functional as F from sglang.kernels.ops.mamba.causal_conv1d_triton import ( PAD_SLOT_ID,@@ -18,6 +19,193 @@ from sglang.kernels.ops.mamba.causal_conv1d_triton import ( from sglang.kernels.ops.mamba.causal_conv1d_triton import ( causal_conv1d_update as _causal_conv1d_update_triton, )+from sglang.srt.utils import is_npu++_IS_NPU = is_npu()++if _IS_NPU:+ # NPU has its own causal_conv1d kernels (Triton-Ascend / native torch) in+ # sgl_kernel_npu; neither the CUDA sgl_kernel ops nor the CUDA Triton+ # kernels below run on Ascend.+ from sgl_kernel_npu.mamba.causal_conv1d import (+ causal_conv1d_fn_npu,+ causal_conv1d_update_npu,+ )+++def _npu_conv_states_to_kernel_layout(conv_states: torch.Tensor, width: int):+ """Adapt the NPU pool's conv-state layout for the sgl_kernel_npu kernels.++ On Ascend the MambaPool stores per-slot conv state as+ ``(num_slots, width - 1, dim)`` (feature dim last; see+ ``_init_npu_conv_state``), while ``causal_conv1d_fn_npu`` /+ ``causal_conv1d_update_npu`` operate on ``(num_slots, dim, width - 1)``.+ Returns a contiguous kernel-layout working copy plus a write-back closure+ that folds the updated state into the pool tensor.+ """+ window = width - 1+ # Pool layout: (..., window, dim); slice keeps the speculative decode+ # window tail (extra draft columns live before the last window entries).+ pool_view = conv_states[..., -window:, :]+ work = pool_view.transpose(-1, -2).contiguous() # (..., dim, window)++ def write_back() -> None:+ pool_view.copy_(work.transpose(-1, -2))++ return work, write_back+++def _npu_conv_states_need_adapt(conv_states: torch.Tensor, width: int) -> bool:+ """True when the pool tensor uses the NPU dim-last conv-state layout."""+ return conv_states.shape[-1] != width - 1+++class NpuConvPrefillPlan(NamedTuple):+ """Host-computed per-step constants for the NPU varlen prefill conv.++ Built once per extend step by the attention backend (shared by every conv+ layer) so the prefill conv never has to sync the device to derive shapes or+ gather/scatter indices. All values are exact equivalents of what+ ``causal_conv1d_fn_npu`` derives on-device from ``query_start_loc``.+ """++ # Position in the padded (batch * max_seq_len) token axis for each valid+ # token, i.e. b * max_seq_len + t for t < seq_len[b]. int32, device.+ scatter_indices: torch.Tensor+ # Per-request gather positions of the final conv window within the+ # [initial_state..., tokens...] padded layout. (batch, width - 1), device.+ final_state_positions: torch.Tensor+ batch: int+ max_seq_len: int+ has_initial_state_any: bool+++def _npu_build_prefill_plan(+ seq_lens: List[int],+ prefix_lens: List[int],+ window: int,+ device,+) -> "NpuConvPrefillPlan":+ batch = len(seq_lens)+ max_seq_len = max(seq_lens)+ seq_lens_t = torch.tensor(seq_lens, dtype=torch.int64)+ base = torch.arange(batch, dtype=torch.int64) * max_seq_len+ offsets = torch.arange(max_seq_len, dtype=torch.int64).unsqueeze(0)+ mask = offsets < seq_lens_t.unsqueeze(1)+ scatter = (base.unsqueeze(1) + offsets)[mask].to(torch.int32)+ positions_base = seq_lens_t - window * (+ torch.tensor(prefix_lens, dtype=torch.int64) == 0+ )+ positions = positions_base.unsqueeze(1) + torch.arange(+ window, dtype=torch.int64+ ).unsqueeze(0)+ return NpuConvPrefillPlan(+ scatter_indices=scatter.to(device),+ final_state_positions=positions.to(device),+ batch=batch,+ max_seq_len=max_seq_len,+ has_initial_state_any=any(int(p) > 0 for p in prefix_lens),+ )+++def _npu_causal_conv1d_fn_dim_last(+ x: torch.Tensor,+ weight: torch.Tensor,+ bias: Optional[torch.Tensor],+ cache_indices: torch.Tensor,+ has_initial_state: Optional[torch.Tensor],+ conv_states: torch.Tensor,+ activation: Optional[str],+ plan: NpuConvPrefillPlan,+):+ """Varlen prefill conv against the NPU dim-last pool layout.++ Reproduces ``causal_conv1d_fn_npu`` (and its ``causal_conv1d_fn_native``+ core) op-for-op -- same padded depthwise ``F.conv1d``, same final-state+ gather, same unpadded output -- but driven by the host-computed ``plan`` so+ no device sync is needed, and reading/writing the dim-last pool tensor+ directly (transposes are views; values are bit-identical).++ x: (dim, cu_seq_len) varlen-concatenated tokens.+ conv_states: (num_slots, width - 1, dim) pool tensor, updated in place.+ """+ dim, width = weight.shape+ window = width - 1+ batch, max_seq_len = plan.batch, plan.max_seq_len++ if has_initial_state is not None and plan.has_initial_state_any:+ initial_states = (+ conv_states.index_select(0, cache_indices)+ * has_initial_state[:, None, None]+ ).transpose(-1, -2) # (batch, dim, window) view+ else:+ initial_states = None++ x_flat = torch.zeros(+ size=(dim, batch * max_seq_len), dtype=weight.dtype, device=weight.device+ )+ x_flat.index_copy_(1, plan.scatter_indices, x)+ x_pad = x_flat.view(dim, batch, max_seq_len).transpose(0, 1).contiguous()++ if initial_states is None:+ conv_input = x_pad+ out = F.conv1d(+ conv_input, weight.unsqueeze(1), bias, padding=window, groups=dim+ )+ else:+ conv_input = torch.cat([initial_states, x_pad], dim=-1)+ out = F.conv1d(conv_input, weight.unsqueeze(1), bias, padding=0, groups=dim)+ out = out[..., :max_seq_len]++ index = plan.final_state_positions.unsqueeze(1).expand(-1, dim, -1)+ final_states = conv_input.gather(2, index.clamp(min=0)).masked_fill(index < 0, 0)+ conv_states.index_copy_(0, cache_indices, final_states.transpose(-1, -2))++ if activation in ("silu", "swish"):+ out = F.silu(out)+ out = out.to(dtype=x.dtype)++ out = out.transpose(1, 2).contiguous().view(batch * max_seq_len, dim)+ out = torch.index_select(out, 0, plan.scatter_indices)+ return out.transpose(0, 1).contiguous() # (dim, cu_seq_len)+++def _npu_causal_conv1d_update_dim_last(+ x: torch.Tensor,+ conv_state: torch.Tensor,+ weight: torch.Tensor,+ bias: Optional[torch.Tensor],+ activation: Optional[str],+ conv_state_indices: Optional[torch.Tensor],+):+ """Single-token decode conv against the NPU dim-last pool layout.++ Same math as ``causal_conv1d_update_npu``'s continuous-batching path+ (gather state -> append token -> window * weight -> sum over the window+ axis -> shift the state), but touches only the active slots instead of+ cloning the whole state table per layer.+ """+ dim, width = weight.shape+ if x.dim() == 3:+ x = x.squeeze(-1)+ if conv_state_indices is not None:+ sel = conv_state.index_select(0, conv_state_indices)+ else:+ sel = conv_state+ new_window = torch.cat([sel.transpose(-1, -2), x.unsqueeze(-1)], dim=-1)+ if new_window.dtype != weight.dtype:+ new_window = new_window.to(weight.dtype)+ out = torch.sum(new_window * weight, dim=-1)+ if bias is not None:+ out = out + bias+ if activation in ("silu", "swish"):+ out = F.silu(out)+ new_state = new_window[..., 1:].transpose(-1, -2)+ if conv_state_indices is not None:+ conv_state.index_copy_(0, conv_state_indices, new_state)+ else:+ conv_state.copy_(new_state)+ return out.to(x.dtype) try: from sgl_kernel import causal_conv1d_fwd@@ -76,14 +264,71 @@ def causal_conv1d_fn( out: (batch, dim, seqlen) """+ if _IS_NPU:+ width = weight.shape[1]+ if conv_states is not None and _npu_conv_states_need_adapt(+ conv_states, width+ ):+ # NPU dim-last pool layout. Prefer the plan-driven path: the+ # backend hands over host-computed shapes/indices (shared by all+ # conv layers in the step) so the prefill conv runs with no device+ # syncs and no whole-table state clones.+ plan = kwargs.get("prefill_plan")+ if plan is None and kwargs.get("seq_lens_cpu") is not None:+ prefix_lens_cpu = kwargs.get("prefix_lens_cpu")+ if prefix_lens_cpu is not None:+ plan = _npu_build_prefill_plan(+ [int(v) for v in kwargs["seq_lens_cpu"]],+ [int(v) for v in prefix_lens_cpu],+ width - 1,+ x.device,+ )+ if plan is not None:+ return _npu_causal_conv1d_fn_dim_last(+ x,+ weight,+ bias,+ cache_indices=cache_indices,+ has_initial_state=has_initial_state,+ conv_states=conv_states,+ activation=activation,+ plan=plan,+ )+ work, write_back = _npu_conv_states_to_kernel_layout(conv_states, width)+ out = causal_conv1d_fn_npu(+ x,+ weight,+ bias,+ query_start_loc=query_start_loc,+ cache_indices=cache_indices,+ has_initial_state=has_initial_state,+ conv_states=work,+ activation=activation,+ pad_slot_id=pad_slot_id,+ )+ write_back()+ return out+ return causal_conv1d_fn_npu(+ x,+ weight,+ bias,+ query_start_loc=query_start_loc,+ cache_indices=cache_indices,+ has_initial_state=has_initial_state,+ conv_states=conv_states,+ activation=activation,+ pad_slot_id=pad_slot_id,+ ) # Use Triton when: (1) sgl_kernel not available, or (2) input is # non-contiguous and seq_lens_cpu is already pre-computed by caller. # The Triton kernel accepts arbitrary strides, avoiding a .contiguous() # copy that can cost >0.6 ms/layer on large prefill batches. use_triton = not _HAS_SGL_KERNEL or (x.stride(-1) != 1 and "seq_lens_cpu" in kwargs) if use_triton:- if "seq_lens_cpu" not in kwargs:+ if kwargs.get("seq_lens_cpu") is None: kwargs["seq_lens_cpu"] = _get_seq_lens_cpu(query_start_loc, x)+ kwargs.pop("prefix_lens_cpu", None)+ kwargs.pop("prefill_plan", None) return _causal_conv1d_fn_triton( x, weight,@@ -150,6 +395,43 @@ def causal_conv1d_update( indices 0 and 3 out: (batch, dim) or (batch, dim, seqlen) """+ if _IS_NPU:+ width = weight.shape[1]+ if _npu_conv_states_need_adapt(conv_state, width):+ # NPU dim-last pool layout.+ is_single_token = x.dim() == 2 or (x.dim() == 3 and x.shape[-1] == 1)+ if cache_seqlens is None and is_single_token:+ return _npu_causal_conv1d_update_dim_last(+ x,+ conv_state,+ weight,+ bias=bias,+ activation=activation,+ conv_state_indices=conv_state_indices,+ )+ work, write_back = _npu_conv_states_to_kernel_layout(conv_state, width)+ out = causal_conv1d_update_npu(+ x,+ work,+ weight,+ bias=bias,+ activation=activation,+ cache_seqlens=cache_seqlens,+ conv_state_indices=conv_state_indices,+ pad_slot_id=pad_slot_id,+ )+ write_back()+ return out+ return causal_conv1d_update_npu(+ x,+ conv_state,+ weight,+ bias=bias,+ activation=activation,+ cache_seqlens=cache_seqlens,+ conv_state_indices=conv_state_indices,+ pad_slot_id=pad_slot_id,+ ) use_triton = not _HAS_SGL_KERNEL if use_triton: return _causal_conv1d_update_triton(@@ -301,6 +301,9 @@ class Lfm2ShortConv(nn.Module): has_initial_state=meta.has_initial_state, conv_states=conv_state, activation=None,+ seq_lens_cpu=forward_batch.extend_seq_lens_cpu,+ prefix_lens_cpu=forward_batch.extend_prefix_lens_cpu,+ prefill_plan=getattr(meta, "prefill_plan", None), ).transpose(0, 1) output, _ = self.out_proj(C_gate * conv_out)@@ -358,6 +358,9 @@ class Lfm2MoeShortConv(nn.Module): has_initial_state=meta.has_initial_state, conv_states=conv_state, activation=None,+ seq_lens_cpu=forward_batch.extend_seq_lens_cpu,+ prefix_lens_cpu=forward_batch.extend_prefix_lens_cpu,+ prefill_plan=getattr(meta, "prefill_plan", None), ).transpose(0, 1) output, _ = self.out_proj(C_gate * conv_out)