Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
sgl_register_operator(
ID fused_quant_scatter_copy_sparse_flash_attention
EXECUTION_UNIT AICORE
CANN_OP_TYPE SglFusedQuantScatterCopySparseFlashAttention
TORCH_ADAPTER torch_adapter.cpp
SUPPORTED_ARCHS 3510
OPDEF_SOURCES op_host/fused_quant_scatter_copy_sparse_flash_attention_def.cpp
INFER_SOURCES op_host/fused_quant_scatter_copy_sparse_flash_attention_def.cpp
TILING_SOURCES op_host/fused_quant_scatter_copy_sparse_flash_attention_def.cpp
KERNEL_SOURCES op_kernel/fused_quant_scatter_copy_sparse_flash_attention.cpp
KERNEL_INCLUDE_DIRECTORIES
op_kernel
op_kernel/arch35
../../sparse_flash_attention/op_kernel
../../sparse_flash_attention/op_kernel/common
../../utils/kernel
KERNEL_COMPILE_DEFINITIONS HAVE_WORKSPACE HAVE_TILING
KERNEL_COMPILE_OPTIONS
--cce-auto-sync=off
-Wno-deprecated-declarations
-mllvm
-cce-vf-remove-membar=false
-mllvm
-cce-aicore-hoist-movemask=false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# A5 C8 scatter copy + sparse flash attention

`fused_quant_scatter_copy_sparse_flash_attention_v2` is an A5-only
selective CANN operator ported from [vllm-ascend #18490](https://github.com/vllm-project/vllm-ascend/pull/18490),
commit `1b180deb840484c2fe33cbdd7abb68a615085172`. Donor source hashes and retained
licenses are recorded in [`UPSTREAM_PROVENANCE.json`](UPSTREAM_PROVENANCE.json).
See the [public API and example](../../../docs/api/en/operators/attention/fused_quant_scatter_copy_sparse_flash_attention_v2.md).
Build the Ascend950 operator independently.

```bash
python3 setup.py --arch-version 3510 --ops fused_quant_scatter_copy_sparse_flash_attention --without-third-party
```

The packed-query ABI also accepts the following arguments.

```text
query, hbm_kv_bytes, dram_kv_bytes, dram_kv_dev_ptr,
hbm_block_table, dram_block_table, topk_dst_slots, topk_src_ids,
topk_miss_counts, actual_seq_lengths_query, actual_seq_lengths_kv,
num_cache_tokens, miss_src_ids, miss_dst_slots, miss_counts,
scale_value, attention_out
```

Query is BF16/FP16 `[T,H,576]`; output is `[T,H,512]` with the same dtype.
Supported head counts are 8, 16, 32, 64, and 128. Both KV tensors are opaque INT8
byte views `[pages,128,1,656]`: 512 FP8 NoPE bytes, 128 BF16 RoPE bytes, then
16 FP32 scale bytes. Copy preserves all 656 bytes.

The AscendC implementation accepts a CPU tensor and its registered device-visible
address. HBM, query, metadata and output remain on one NPU. The caller must
retain registered storage throughout capture/replay and synchronize before
releasing it. The host retains tensor owners through launch submission.
The ACLNN workspace is allocated and reused in stream order.

`num_cache_tokens=0` means dense, using the real sequence length. Otherwise it is
the exposed HBM logical address-space length. `actual_seq_lengths_kv` remains
the real source length. TopK destination slots and both physical page tables
are authoritative: the operator does not derive a tail formula, invent a tail
mapping, or append tail tokens. Any caller-managed tail slots must already be
present in the supplied TopK destination list and covered by the HBM table.

TopK arrays are `[T,1,2048]`; miss counts are `[T]`. Request miss arrays are
`[B,1,32768]` with counts `[B]`. LIM's contiguous `[B,32768]` outputs can be
unsqueezed without copying. Metadata drives the fused miss path for at most 512
misses, or a complete union copy followed by attention inside the same kernel.
`actual_seq_lengths_query` contains the strictly increasing query end for each
of the `B` requests, and every segment may contain 1–16 rows. Thus Q4 MTP uses
four consecutive TopK rows for one request without changing the ABI. The
512/513 path decision is read from device metadata by the kernel, so a captured
graph can switch between fused miss gather and request-level union copy.

HBM/DRAM tables are `[B, pages_per_request]`; each entry is a physical block ID,
not an implicit contiguous-request offset. Rows may therefore be randomized or
share physical DRAM pages. They have at most 2048 pages per request, so the
effective source capacity is at most 262144 tokens at page size 128.
For a padded TND request, keep the positive query width and set the KV
length, cache size and miss counts to zero, with `-1` slots. With this
contract the kernel initializes the output and skips attention for the
padded request. An empty slot list alone does not trigger initialization.
Query/output storage stays BF16/FP16 even though KV storage is C8.

The fully visible entry `fused_quant_scatter_copy_sparse_flash_attention_v2`
replaces the packed query with separate BF16/FP16 `[T,H,512]` and `[T,H,64]`
inputs and accepts **`[B,32768]`** request miss arrays. The
fully visible A5 LIM
produces the causal TopK and copy plan in one launch. Long rows share one
request-union copy list across the query window, including retained slots
whose current KV must be refreshed. In steady state, routes with at most 512
ordinary misses use the source-aware SFA gather pipeline. If the union count
equals the largest route miss count, it contains no refresh entries. Otherwise
the kernel pre-copies the last (at most Q) union entries to cover all retained
refreshes. Cold rows, wider miss
routes, and legacy callers keep the complete-copy barrier. The overlap path is
enabled only when the caller also passes `native_lim_plan=True`, which asserts
the native LIM ordinary-prefix/refresh-suffix contract. Arbitrary-order callers
remain on the complete-copy path. Pass `current`, `positions`, `current_valid`,
`request_slots`, `req_to_token`, and the per-layer `resident_end` together to
enable resident staging. CopySFA restores missing
short history into identity slots and stages current KV. Plan entries for valid current positions
copy directly from the current layer's device buffer, including ordinary
misses gathered during attention; Host is used only for non-current sources.
Callers that stage short/current KV themselves can omit the resident tensors.

For a sparse row, set `actual_seq_lengths_kv` to the virtual scheduling
length `C + Q - 1` and `num_cache_tokens` to `C`. Supply `C / 128`
HBM pages. The v2 kernel accepts LI order within each query.

```bash
python3 -m pytest -q tests/python/operators/attention/test_fused_quant_scatter_copy_sparse_flash_attention_v2.py
```

The tests cover the eager PyTorch reference, host scheduling rules, and
Ascend950 Q1/Q4 short-resident staging in eager mode and graph replay.
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"url": "https://github.com/vllm-project/vllm-ascend/pull/18490",
"commit": "1b180deb840484c2fe33cbdd7abb68a615085172",
"files": [
{
"path": "fused_quant_scatter_copy_sparse_flash_attention.cpp",
"sha256": "9b8eee3e95aa638aed503a01801c0d1d5a483e8dfae60c922c81ac7481b6c4ac"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_contract.h",
"sha256": "ed8bfaf12471605aa97335f1d79179e54bca6b1a2fe3145b06137515f32add1c"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_copy.h",
"sha256": "d12fedf78ad0fa3de29ee4fefbaa671a0f48c45d7f973117fb945e4759ebe512"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_schedule.h",
"sha256": "3bc2668a10f6eef65d07aa6b1fa67224a9ed85b9dff0d2e800ee99b42590a4a3"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_template_tiling_key.h",
"sha256": "4ac7d212f829403a05189c8c3aeef5b0eff04231163dba82472adf834bbe9234"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_common_arch35.h",
"sha256": "998dad982cc88b5e65fbda5eb68a87892ba29cb783d33a10b4365094ece49360"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_kernel_mla.h",
"sha256": "7488a08c20f2209983c4bd647dd967aa2f98637cc21b55fe6b18e8123d710b2b"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_kvcache.h",
"sha256": "1e9c6dfab9671f4408867ad24203b08fe475e26b72047194815987a26ba88193"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_service_cube_mla.h",
"sha256": "0a378478d2eb1ab893a910148cadee09dcad7b6d51fc65edf1b600d3b3ed0ff5"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_service_vector_mla.h",
"sha256": "67a758e78a0316460ca8ca2432838f2ad303ac3bc6edfdf411ff7aa3a1898455"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_source_gather.h",
"sha256": "3ed6c52137f05011da6c155ac293c5c7c93fe8365be04a113ae512bddeb74c99"
},
{
"path": "arch35/util.h",
"sha256": "881e792473825acfd839fd269a3c997ff05ec301bc174d1728726d8ea26f5567"
},
{
"path": "arch35/util_regbase.h",
"sha256": "a3dd733c5e6de526097a4e685494bdee9e4d9275bab08a6f14d8bffe1914f640"
},
{
"path": "common/buffer.h",
"sha256": "29b3f30336d6cf744ad775ce829ee33fd3b166cf553736f28ce198298da6f65d"
},
{
"path": "common/buffers_policy.h",
"sha256": "6a03eb001b53335c865ddf52a9581ddd51d6b45260f8d994676fbd6b7469f79a"
},
{
"path": "common/buffer_manager.h",
"sha256": "946944e5b032259c176d9b841bd84f139c8eaf9fd44aa6fb83b0ad8529f161d8"
},
{
"path": "common/buffer_mix_core.h",
"sha256": "b57e3cd6ed710400e5ede680300a5dcc0b8c9e0859f2625d61bc023a832063af"
},
{
"path": "common/CopyInL1.h",
"sha256": "97ed4f8686d00ffdd8555e37596a51da5f67b1f3ed66f76084fa4e2e5a4ce3da"
},
{
"path": "common/FixpipeOut.h",
"sha256": "12600e1718f0b53377387301a8493f8ce34ce77d0dd8464379ef23daadffaaff"
},
{
"path": "common/matmul.h",
"sha256": "10b62f1218000686e6260ed3cf9ae4fb5a5bb0fdf7802913b93250f3c67c7fd4"
},
{
"path": "common/offset_calculator.h",
"sha256": "0ab85b78da7e3c4be75142bd2bc7c8d7b41fe564e34bc2dbe1dd4be84b142892"
},
{
"path": "common/memcopy/fa_gm_tensor.h",
"sha256": "b3a245fd740607933bb6d2d8cd2698862517c1146461bd448ceaff545a93b961"
},
{
"path": "common/memcopy/fa_l1_tensor.h",
"sha256": "8eb170da3a99874ca9bb020ea91f6326e30486495c6ec4e4bfbf9082ff69673f"
},
{
"path": "common/memcopy/gm_coord.h",
"sha256": "9888e757f2c50e8efb109babd09f6fe1a3a8c2ba98051d72744f8f17931af4a1"
},
{
"path": "common/memcopy/gm_layout.h",
"sha256": "648c999f8bf552624b1ac57a4476caf7d89ee25451e404f78e4f7b09848b9b9e"
},
{
"path": "common/memcopy/offset_calculator_v2.h",
"sha256": "61367917e054c33cf5d03cab3cdce4493096e3e11bba350faa3fc4befe726732"
},
{
"path": "common/memcopy/parser.h",
"sha256": "4f47ec4a6633a0bc45b0c0e244e4ce2c74da3940e93b974d6280b84b6e8461ce"
},
{
"path": "common/arch35/vf/vf_basic_block_aligned128_no_update_sfa.h",
"sha256": "7ec0f45a92cbfde57e4a50f859eb14372fe3ab95d80eed7c4b5f3a6473aea66b"
},
{
"path": "common/arch35/vf/vf_basic_block_aligned128_update_sfa.h",
"sha256": "cf4b5c9b941b8c7634380a9524cdea52934694860bf950ca5eeb2fc36fcbf8cf"
},
{
"path": "common/arch35/vf/vf_basic_block_unaligned128_no_update_sfa.h",
"sha256": "48c0f5035ba1ed29e2e893ae67956270ec3f578cd73e89c01fef865d5e53fd4a"
},
{
"path": "common/arch35/vf/vf_basic_block_unaligned128_update_sfa.h",
"sha256": "ea7e370b7b1bd311a13c89d565eb8225a7659ef809b33b99bc2f34c737b1b5b1"
},
{
"path": "common/arch35/vf/vf_basic_block_unaligned64_no_update_sfa.h",
"sha256": "9dca28c4402b0add93ed70f3e4fb78b06347a551a4d8f7e8a3cc79fdac422d90"
},
{
"path": "common/arch35/vf/vf_basic_block_unaligned64_update_sfa.h",
"sha256": "f890045b07720a2d22aece3c2a33acf795bd06dcf82909f47753fe34df9d25b6"
},
{
"path": "common/arch35/vf/vf_basic_block_utils.h",
"sha256": "7fbcfa5946fa89cb767f4bfc97893b9a2b19a8fc6f1b3710ec89f2e9c81fe52a"
},
{
"path": "common/arch35/vf/vf_flashupdate_new.h",
"sha256": "2f3bea7a06fdada65f52f0959af3b77aa92895a9f76fc61ea5e6307f7bc71774"
},
{
"path": "common/arch35/vf/vf_mul_sel_softmaxflashv2_cast_nz_sfa.h",
"sha256": "8ddafeda9671ac77f9fe1bc86bad512c1a821bd4229b19a825a43525e6e63d10"
}
],
"adaptations": [
"A5-only normal-wheel direct launch",
"explicit tiling serialization",
"registered DRAM pointer with owning CPU tensor for Copy-SFA",
"four concrete BF16/FP16 kernel entries with split-head dispatch",
"split-query v2 entry points consume fully visible causal TopK and request-union miss lists",
"resident rows contain C slots; current and short history rows are staged by the caller"
],
"adapted_files": [
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_kernel_mla.h",
"ported_sha256": "3d38fa64f29e13199ab17913461410e3d1ae576cd217b9fe03b0ea0f484f4e18"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_kvcache.h",
"ported_sha256": "856effe5432a9b8672177804e0b41aee5326b1fff691390deff3a05fdf6ddd5a"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_service_cube_mla.h",
"ported_sha256": "12723c785cc38ed3346a712974835706d7f6081cbb3fb4ba8e580faa5dc163ad"
},
{
"path": "arch35/fused_quant_scatter_copy_sparse_flash_attention_service_vector_mla.h",
"ported_sha256": "832e3b187b3216267e53b7deb6885118ef2d281837a7aad7f8042f1d08e3e7d6"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention.cpp",
"ported_sha256": "4179a5c1863a4ee9ca30f4a0964961ca4aec9b9f614c97d98f5e891b57ba194a"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_contract.h",
"ported_sha256": "9a4677952f8b3085d50eae5e31bce4b96631be910c1de2e695cea732627f7236"
},
{
"path": "fused_quant_scatter_copy_sparse_flash_attention_copy.h",
"ported_sha256": "838bc86b236fc0d87eeb605a1487f9a2c4a8a629ad0e0c5b1085b9231776a882"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2026 Huawei Technologies Co., Ltd.
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include <ATen/ATen.h>
#include <cstdint>
#include <optional>

namespace sglang::npu_kernel {

void fused_quant_scatter_copy_sparse_flash_attention(
const at::Tensor &query, at::Tensor hbm_kv_bytes, const at::Tensor &dram_kv_bytes, int64_t dram_kv_dev_ptr,
const at::Tensor &hbm_block_table, const at::Tensor &dram_block_table, const at::Tensor &topk_dst_slots,
const at::Tensor &topk_src_ids, const at::Tensor &topk_miss_counts, const at::Tensor &actual_seq_lengths_query,
const at::Tensor &actual_seq_lengths_kv, const at::Tensor &num_cache_tokens, const at::Tensor &miss_src_ids,
const at::Tensor &miss_dst_slots, const at::Tensor &miss_counts, double scale_value, at::Tensor attention_out);

void fused_quant_scatter_copy_sparse_flash_attention_v2(
const at::Tensor &query_nope, const at::Tensor &query_rope, at::Tensor hbm_kv_bytes,
const at::Tensor &dram_kv_bytes, int64_t dram_kv_dev_ptr, const at::Tensor &hbm_block_table,
const at::Tensor &dram_block_table, const at::Tensor &topk_dst_slots, const at::Tensor &topk_src_ids,
const at::Tensor &topk_miss_counts, const at::Tensor &actual_seq_lengths_query,
const at::Tensor &actual_seq_lengths_kv, const at::Tensor &num_cache_tokens, const at::Tensor &miss_src_ids,
const at::Tensor &miss_dst_slots, const at::Tensor &miss_counts, double scale_value, at::Tensor attention_out,
const std::optional<at::Tensor> &current, const std::optional<at::Tensor> &positions,
const std::optional<at::Tensor> &current_valid, const std::optional<at::Tensor> &request_slots,
const std::optional<at::Tensor> &req_to_token, const std::optional<at::Tensor> &resident_end, bool native_lim_plan);

} // namespace sglang::npu_kernel
Loading