-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: support int64 IdType for RoPE part argument in rope_quantize_fp8_append_paged_kv_cache
#2255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yzh119
merged 3 commits into
flashinfer-ai:main
from
elvischenv:elvischenv/fix-rope-idtype
Dec 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -450,6 +450,24 @@ void rope_quantize_append_paged_kv_cache( | |
| CHECK_INPUT(batch_indices); | ||
| CHECK_INPUT(positions); | ||
|
|
||
| // Validate that all index tensors have the same dtype as pos_ids | ||
| if (kv_indices.dtype() != pos_ids.dtype()) { | ||
| TVM_FFI_LOG_AND_THROW(TypeError) << "kv_indices dtype (" << kv_indices.dtype() | ||
| << ") must match pos_ids dtype (" << pos_ids.dtype() << ")"; | ||
| } | ||
| if (kv_indptr.dtype() != pos_ids.dtype()) { | ||
| TVM_FFI_LOG_AND_THROW(TypeError) << "kv_indptr dtype (" << kv_indptr.dtype() | ||
| << ") must match pos_ids dtype (" << pos_ids.dtype() << ")"; | ||
| } | ||
| if (batch_indices.dtype() != pos_ids.dtype()) { | ||
| TVM_FFI_LOG_AND_THROW(TypeError) << "batch_indices dtype (" << batch_indices.dtype() | ||
| << ") must match pos_ids dtype (" << pos_ids.dtype() << ")"; | ||
| } | ||
| if (positions.dtype() != pos_ids.dtype()) { | ||
| TVM_FFI_LOG_AND_THROW(TypeError) << "positions dtype (" << positions.dtype() | ||
| << ") must match pos_ids dtype (" << pos_ids.dtype() << ")"; | ||
| } | ||
|
|
||
| // Extract dimensions | ||
| uint32_t rope_dim = q_rope_in.size(-1); | ||
| uint32_t no_rope_dim = q_nope_in.size(-1); | ||
|
|
@@ -547,71 +565,77 @@ void rope_quantize_append_paged_kv_cache( | |
|
|
||
| DISPATCH_DLPACK_DTYPE_TO_CTYPE_FP16(q_rope_in.dtype(), c_type, [&] { | ||
| return DISPATCH_DLPACK_DTYPE_TO_CTYPE_FP8(q_rope_out.dtype(), c_quant_type, [&] { | ||
| cudaError_t status; | ||
|
|
||
| if (is_mla) { | ||
| // MLA: Construct paged_kv_mla_t struct | ||
| auto ckv_strides = ckv_cache.strides(); | ||
| auto kpe_strides = kpe_cache.strides(); | ||
|
|
||
| paged_kv_mla_t<c_quant_type, int32_t> paged_kv_mla( | ||
| page_size, no_rope_dim, rope_dim, batch_size, | ||
| static_cast<c_quant_type*>(ckv_cache.data_ptr()), ckv_strides.data(), | ||
| static_cast<c_quant_type*>(kpe_cache.data_ptr()), kpe_strides.data(), | ||
| static_cast<int32_t*>(kv_indices.data_ptr()), | ||
| static_cast<int32_t*>(kv_indptr.data_ptr()), | ||
| nullptr // last_page_len not needed for this kernel | ||
| ); | ||
|
|
||
| status = RopeQuantizeAppendPagedMLACache( | ||
| static_cast<c_type*>(q_rope_in.data_ptr()), static_cast<c_type*>(k_rope_in.data_ptr()), | ||
| static_cast<c_type*>(q_nope_in.data_ptr()), static_cast<c_type*>(k_nope_in.data_ptr()), | ||
| static_cast<c_quant_type*>(q_rope_out.data_ptr()), | ||
| static_cast<c_quant_type*>(q_nope_out.data_ptr()), paged_kv_mla, | ||
| static_cast<int32_t*>(batch_indices.data_ptr()), | ||
| static_cast<int32_t*>(positions.data_ptr()), | ||
| static_cast<float*>(cos_sin_cache.data_ptr()), | ||
| static_cast<int32_t*>(pos_ids.data_ptr()), nnz, num_qo_heads, rope_dim, no_rope_dim, | ||
| q_rope_in_stride_n, q_rope_in_stride_h, q_nope_in_stride_n, q_nope_in_stride_h, | ||
| q_rope_out_stride_n, q_rope_out_stride_h, q_nope_out_stride_n, q_nope_out_stride_h, | ||
| k_rope_in_stride, k_nope_in_stride, quant_scale_q, quant_scale_kv, interleave, | ||
| enable_pdl, stream); | ||
|
|
||
| } else { | ||
| // GQA/MHA: Construct paged_kv_t struct | ||
| auto k_strides = k_cache.strides(); | ||
| auto v_strides = v_cache.strides(); | ||
| uint32_t head_dim = rope_dim + no_rope_dim; | ||
|
|
||
| paged_kv_t<c_quant_type, int32_t> paged_kv( | ||
| num_kv_heads, page_size, head_dim, batch_size, kv_layout, | ||
| static_cast<c_quant_type*>(k_cache.data_ptr()), | ||
| static_cast<c_quant_type*>(v_cache.data_ptr()), k_strides.data(), | ||
| static_cast<int32_t*>(kv_indices.data_ptr()), | ||
| static_cast<int32_t*>(kv_indptr.data_ptr()), | ||
| nullptr // last_page_len not needed for this kernel | ||
| ); | ||
|
|
||
| status = RopeQuantizeAppendPagedKVCache( | ||
| static_cast<c_type*>(q_rope_in.data_ptr()), static_cast<c_type*>(k_rope_in.data_ptr()), | ||
| static_cast<c_type*>(q_nope_in.data_ptr()), static_cast<c_type*>(k_nope_in.data_ptr()), | ||
| static_cast<c_type*>(v_in.data_ptr()), | ||
| static_cast<c_quant_type*>(q_rope_out.data_ptr()), | ||
| static_cast<c_quant_type*>(q_nope_out.data_ptr()), paged_kv, | ||
| static_cast<int32_t*>(batch_indices.data_ptr()), | ||
| static_cast<int32_t*>(positions.data_ptr()), | ||
| static_cast<float*>(cos_sin_cache.data_ptr()), | ||
| static_cast<int32_t*>(pos_ids.data_ptr()), nnz, num_qo_heads, num_kv_heads, rope_dim, | ||
| no_rope_dim, q_rope_in_stride_n, q_rope_in_stride_h, q_nope_in_stride_n, | ||
| q_nope_in_stride_h, q_rope_out_stride_n, q_rope_out_stride_h, q_nope_out_stride_n, | ||
| q_nope_out_stride_h, k_rope_in_stride, k_rope_in_stride_h, k_nope_in_stride, | ||
| k_nope_in_stride_h, v_in_stride, v_in_stride_h, quant_scale_q, quant_scale_kv, | ||
| interleave, enable_pdl, stream); | ||
| } | ||
| return DISPATCH_DLPACK_IDTYPE_TO_CTYPE(pos_ids.dtype(), c_idtype, [&] { | ||
| cudaError_t status; | ||
|
|
||
| if (is_mla) { | ||
| // MLA: Construct paged_kv_mla_t struct | ||
| auto ckv_strides = ckv_cache.strides(); | ||
| auto kpe_strides = kpe_cache.strides(); | ||
|
|
||
| paged_kv_mla_t<c_quant_type, c_idtype> paged_kv_mla( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| page_size, no_rope_dim, rope_dim, batch_size, | ||
| static_cast<c_quant_type*>(ckv_cache.data_ptr()), ckv_strides.data(), | ||
| static_cast<c_quant_type*>(kpe_cache.data_ptr()), kpe_strides.data(), | ||
| static_cast<c_idtype*>(kv_indices.data_ptr()), | ||
| static_cast<c_idtype*>(kv_indptr.data_ptr()), | ||
| nullptr // last_page_len not needed for this kernel | ||
| ); | ||
|
elvischenv marked this conversation as resolved.
|
||
|
|
||
| status = RopeQuantizeAppendPagedMLACache( | ||
| static_cast<c_type*>(q_rope_in.data_ptr()), | ||
| static_cast<c_type*>(k_rope_in.data_ptr()), | ||
| static_cast<c_type*>(q_nope_in.data_ptr()), | ||
| static_cast<c_type*>(k_nope_in.data_ptr()), | ||
| static_cast<c_quant_type*>(q_rope_out.data_ptr()), | ||
| static_cast<c_quant_type*>(q_nope_out.data_ptr()), paged_kv_mla, | ||
| static_cast<c_idtype*>(batch_indices.data_ptr()), | ||
| static_cast<c_idtype*>(positions.data_ptr()), | ||
| static_cast<float*>(cos_sin_cache.data_ptr()), | ||
| static_cast<c_idtype*>(pos_ids.data_ptr()), nnz, num_qo_heads, rope_dim, no_rope_dim, | ||
| q_rope_in_stride_n, q_rope_in_stride_h, q_nope_in_stride_n, q_nope_in_stride_h, | ||
| q_rope_out_stride_n, q_rope_out_stride_h, q_nope_out_stride_n, q_nope_out_stride_h, | ||
| k_rope_in_stride, k_nope_in_stride, quant_scale_q, quant_scale_kv, interleave, | ||
| enable_pdl, stream); | ||
|
|
||
| } else { | ||
| // GQA/MHA: Construct paged_kv_t struct | ||
| auto k_strides = k_cache.strides(); | ||
| auto v_strides = v_cache.strides(); | ||
| uint32_t head_dim = rope_dim + no_rope_dim; | ||
|
|
||
| paged_kv_t<c_quant_type, c_idtype> paged_kv( | ||
| num_kv_heads, page_size, head_dim, batch_size, kv_layout, | ||
| static_cast<c_quant_type*>(k_cache.data_ptr()), | ||
| static_cast<c_quant_type*>(v_cache.data_ptr()), k_strides.data(), | ||
| static_cast<c_idtype*>(kv_indices.data_ptr()), | ||
| static_cast<c_idtype*>(kv_indptr.data_ptr()), | ||
| nullptr // last_page_len not needed for this kernel | ||
| ); | ||
|
elvischenv marked this conversation as resolved.
|
||
|
|
||
| status = RopeQuantizeAppendPagedKVCache( | ||
| static_cast<c_type*>(q_rope_in.data_ptr()), | ||
| static_cast<c_type*>(k_rope_in.data_ptr()), | ||
| static_cast<c_type*>(q_nope_in.data_ptr()), | ||
| static_cast<c_type*>(k_nope_in.data_ptr()), static_cast<c_type*>(v_in.data_ptr()), | ||
| static_cast<c_quant_type*>(q_rope_out.data_ptr()), | ||
| static_cast<c_quant_type*>(q_nope_out.data_ptr()), paged_kv, | ||
| static_cast<c_idtype*>(batch_indices.data_ptr()), | ||
| static_cast<c_idtype*>(positions.data_ptr()), | ||
| static_cast<float*>(cos_sin_cache.data_ptr()), | ||
| static_cast<c_idtype*>(pos_ids.data_ptr()), nnz, num_qo_heads, num_kv_heads, rope_dim, | ||
| no_rope_dim, q_rope_in_stride_n, q_rope_in_stride_h, q_nope_in_stride_n, | ||
| q_nope_in_stride_h, q_rope_out_stride_n, q_rope_out_stride_h, q_nope_out_stride_n, | ||
| q_nope_out_stride_h, k_rope_in_stride, k_rope_in_stride_h, k_nope_in_stride, | ||
| k_nope_in_stride_h, v_in_stride, v_in_stride_h, quant_scale_q, quant_scale_kv, | ||
| interleave, enable_pdl, stream); | ||
| } | ||
|
|
||
| TVM_FFI_ICHECK(status == cudaSuccess) | ||
| << "RopeQuantizeAppendPagedKVCache failed with error code " << cudaGetErrorString(status); | ||
| return true; | ||
| TVM_FFI_ICHECK(status == cudaSuccess) | ||
| << "RopeQuantizeAppendPagedKVCache failed with error code " | ||
| << cudaGetErrorString(status); | ||
| return true; | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yzh119 This won't work.
pos_idsis for RoPE part:flashinfer/flashinfer/rope.py
Lines 1298 to 1314 in df82616
batch_indices,positions,kv_indices,kv_indptrare for KV cache update part:flashinfer/flashinfer/page.py
Lines 255 to 265 in df82616
rope_quantize_fp8_append_paged_kv_cacheis a merged version that haspos_ids,batch_indices,positions,kv_indices,kv_indptr:flashinfer/flashinfer/rope.py
Lines 1438 to 1460 in df82616
typename RoPEIdTypeforpos_ids. For KV update part, we need another standalone IdTypetypename PagedKVIdTypeforbatch_indices,positions,kv_indices,kv_indptr.pos_idsin int64, andbatch_indices,positions,kv_indices,kv_indptrin int32.