Commit 4dfbd4c
authored
Fix conv1d update state shift (sgl-project#428)
* fix(causal_conv1d_update): pre-shift conv_state to match rolling-buffer convention
The kernel's in-place sliding window only writes back to positions
[VAL .. state_len-1] (where VAL = state_len - seq_len), leaving the
older prefix state[0..VAL-1] stale. This diverges from the vLLM/SGLang
rolling-buffer convention where the final state should equal
[old_state[seq_len:state_len], x[0:seq_len]].
Fix at the host integration layer by pre-shifting
state[0..VAL-1] := state[seq_len..seq_len+VAL-1] before launching the
kernel. The kernel only reads/writes positions [VAL..state_len-1],
so the pre-shifted prefix is preserved through kernel execution and
the final conv_state matches the reference. Pad-slot entries are
filtered out, and the no-indices path is handled symmetrically.
Verified by tests/python/sgl_kernel_npu/test_conv1d_update.py:
output and conv_state both reach 100% match against the vLLM
reference (previously state was 66.70%).
* test(causal_conv1d_update): scale up NPU test config
Bump BSZ 1->32, SEQ_LEN 1->4, HIDDEN 4096->2048, CACHE_LEN 10->32 to
exercise the multi-batch / multi-token spec-decoding path that
previously masked the conv_state rolling-buffer mismatch.
* fix(causal_conv1d_update): tile along dim to avoid kernel UB overflow
The kernel allocates ~26*dim bytes of UB per AIV core (width*dim weight
plus several dim*sizeof operand/accumulator buffers). On Atlas A3 the
192KB UB budget is exceeded once dim is roughly above 7500, which
manifests as a vector core "ub address out of bounds" exception. Real
workloads such as Qwen3-Next call the op with conv_dim well past that,
crashing in hybrid_linear_attn_backend.forward_extend.
Fix at the host integration layer without touching the kernel:
* Extract the kernel-launch / tiling-data / cache-management code into
a per-tile lambda parameterized by tile_dim.
* When dim exceeds kMaxDimTile (4096), slice x / weight / conv_state /
bias along the dim axis, materialize contiguous tile copies (the
kernel still requires contiguous inputs), launch the kernel per tile,
and stitch y / conv_state back into the full tensors.
* The pre-shift that maintains the rolling-buffer convention runs once
on the full conv_state and is independent of tiling.
Verified locally with a sweep over HIDDEN in {4096, 6144, 8192, 10240,
12288}: bf16 max output diff is 6.25e-2 (one ULP at magnitude) and
conv_state matches exactly against the vLLM reference at every dim.
* fix(causal_conv1d_update): make op graph-capture compatible
1. Add mutation annotation Tensor(a!) for conv_state in op schema so
graph mode correctly tracks the in-place update.
2. Replace masked_select (dynamic-shape output) and data-dependent
branch in pre-shift logic with clamp_min(0), ensuring all tensor
shapes are statically known during graph capture.
* test(causal_conv1d_update): adjust test config parameters
* fix(causal_conv1d_update): replace aclrtMemcpy with CopyTensorHostToDevice for stream ordering
Raw aclrtMemcpy bypasses OpCommand task queue, breaking stream ordering
between pre-shift PyTorch ops and kernel launch in graph/non-blocking
mode (precision errors without ASCEND_LAUNCH_BLOCKING).
Use CopyTensorHostToDevice (pin_memory + .to(device)) for tiling cache
miss paths so all device ops go through the same dispatch queue.
* fix(causal_conv1d_update): fix stream ordering and reduce peak memory
1. Replace EXEC_KERNEL_CMD (RunOpApi) with OpCommand.SetCustomHandler
pattern (same as lora ops) to ensure kernel launch goes through the
same OpCommand task queue as pre-shift PyTorch ops. RunOpApi may
bypass the task queue, causing stream ordering issues in graph /
non-blocking mode (precision errors without ASCEND_LAUNCH_BLOCKING).
2. Optimize pre-shift memory: slice first then index_select to allocate
only [B, val_shift, dim] instead of full [B, state_len, dim].
Reduces peak memory for large batch sizes.
* Revert "fix(causal_conv1d_update): fix stream ordering and reduce peak memory"
This reverts commit 2f0fe83.
* fix(causal_conv1d_update): read padSlotId in kernel tiling parse
Kernel Init() and ParseTilingData() did not read padSlotId from the
tiling buffer, leaving it uninitialised. When the garbage value
happened to match a valid stateIdx the corresponding batch was
silently skipped, producing wrong output. The probability grows
with batch size, explaining the "large BSZ → bad precision" symptom.
* style: apply clang-format to causal_conv1d_update1 parent d64de59 commit 4dfbd4c
5 files changed
Lines changed: 146 additions & 67 deletions
File tree
- csrc
- causal_conv1d_update
- op_host
- op_kernel
- tests/python/sgl_kernel_npu
Lines changed: 138 additions & 61 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
106 | 131 | | |
107 | 132 | | |
108 | 133 | | |
| |||
111 | 136 | | |
112 | 137 | | |
113 | 138 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
158 | 231 | | |
159 | 232 | | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
177 | 254 | | |
178 | 255 | | |
179 | 256 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
247 | | - | |
248 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
249 | 249 | | |
250 | | - | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| |||
0 commit comments