Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
446 changes: 446 additions & 0 deletions docs/contrib_ops/cuda/gqa.md

Large diffs are not rendered by default.

173 changes: 0 additions & 173 deletions docs/contrib_ops/gqa.md

This file was deleted.

14 changes: 9 additions & 5 deletions onnxruntime/contrib_ops/cuda/bert/group_query_attention.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ Status GroupQueryAttention<T, U>::ComputeInternal(OpKernelContext* context) cons
// 4. Past and Present KV cache share the same buffer (required for XQA specific memory access).
// 5. No Softcap (XQA doesn't support softcap).
// 6. Standard Softmax, or smooth softmax represented by a head_sink tensor.
// 7. No local window attention (global attention only).
// 7. Local window (sliding window) attention is supported on the non-quantized path; the
// quantized (INT8/FP8) paths remain global-only (see the per-variant checks below).
const bool use_xqa_attention_sinks = head_sink != nullptr && !is_inputs_quantized;
const bool is_xqa_smooth_softmax_supported = !parameters.use_smooth_softmax || use_xqa_attention_sinks;
// XQA is enabled when enable_xqa_=true; ineligible shapes/group sizes fall back via data.use_xqa below.
Expand All @@ -399,19 +400,22 @@ Status GroupQueryAttention<T, U>::ComputeInternal(OpKernelContext* context) cons
parameters.kv_sequence_length > 0 && // Shared KV (kv_seq=0) has no new K/V to append
parameters.past_present_share_buffer &&
parameters.softcap == 0.0f &&
is_xqa_smooth_softmax_supported &&
parameters.local_window_size == -1) {
is_xqa_smooth_softmax_supported) {
int group_size = parameters.num_heads / parameters.kv_num_heads;

bool is_int8_quantized_supported = is_int8 &&
// Sliding window (local_window_size > 0) is only wired into the non-quantized XQA kernels.
// The quantized variants stay restricted to global attention.
const bool is_global_attention = parameters.local_window_size == -1;

bool is_int8_quantized_supported = is_int8 && is_global_attention &&
(k_quant_type_ == KVQuantizationType::PER_TENSOR &&
v_quant_type_ == KVQuantizationType::PER_TENSOR &&
data.k_scale == data.v_scale && // XQA requires k_scale and v_scale to be the same. Here requires k_scale and v_scale are same tensor.
(parameters.head_size == 256 || parameters.head_size == 128 || parameters.head_size == 64) &&
(group_size == 4 || group_size == 8 || group_size == 16 || group_size == 32));

#ifdef USE_FP8_KV_CACHE
bool is_fp8_quantized_supported = is_fp8 &&
bool is_fp8_quantized_supported = is_fp8 && is_global_attention &&
(k_quant_type_ == KVQuantizationType::PER_TENSOR &&
v_quant_type_ == KVQuantizationType::PER_TENSOR &&
data.k_scale == data.v_scale &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ Status ExtremeDecoding(
parameters.head_size,
parameters.seqlen_present_kv_cache, // max_seq_len (Capacity)
scale,
parameters.local_window_size, // -1 means global attention; >0 enables sliding window
past_bsnh,
data.past_seq_lens,
data.xqa_head_sink,
Expand Down
17 changes: 16 additions & 1 deletion onnxruntime/contrib_ops/cuda/bert/xqa/xqa_impl_gen.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ inline Status Launch(
[[maybe_unused]] const float* attention_sinks,
[[maybe_unused]] const float* kv_cache_scale,
[[maybe_unused]] void* workspace,
[[maybe_unused]] size_t workspace_size) {
[[maybe_unused]] size_t workspace_size,
[[maybe_unused]] const int local_window_size = -1) {
#ifdef XQA_HAS_SM80_TARGET
const InputHead* q_ptr = reinterpret_cast<const InputHead*>(query);
GMemKVCacheHead* k_ptr = reinterpret_cast<GMemKVCacheHead*>(const_cast<void*>(key_cache));
Expand Down Expand Up @@ -92,9 +93,23 @@ inline Status Launch(
cudaMemsetAsync(semaphores, 0, semaphore_size, stream);
}

#if SLIDING_WINDOW
Comment thread
tianleiwu marked this conversation as resolved.
// ORT local_window_size semantics: -1 => global attention; >0 => each query attends to the
// last local_window_size tokens (including the current one). XQA's slidingWinSize uses the
// same "last N tokens incl. current" definition, so pass it through directly. For global
// attention, use max_seq_len so the kernel's runtime guard (cacheSeqLen > slidingWinSize) is
// never taken and no masking work is performed (numerically identical to the global kernel).
uint32_t const sliding_win_size = (local_window_size > 0)
? static_cast<uint32_t>(local_window_size)
: static_cast<uint32_t>(max_seq_len);
#endif
Comment thread
tianleiwu marked this conversation as resolved.

launchMHA(
device_prop,
static_cast<uint32_t>(kv_num_heads),
#if SLIDING_WINDOW
sliding_win_size,
#endif
scale,
out_ptr,
q_ptr,
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/contrib_ops/cuda/bert/xqa/xqa_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Status LaunchXQAKernel(
const int head_size,
const int max_seq_len, // Max sequence length of cache
const float scale,
const int local_window_size, // Sliding window size; -1 means global attention (no sliding window)
const bool is_bsnh, // Layout of KV cache
const int* past_seq_lens, // Past sequence lengths [BatchSize]
const float* attention_sinks, // Attention sink per query head, nullptr if not used
Expand Down
10 changes: 7 additions & 3 deletions onnxruntime/contrib_ops/cuda/bert/xqa/xqa_loader_bf16.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Status LaunchXQAKernelImpl(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand All @@ -48,6 +49,7 @@ Status LaunchXQAKernelImpl(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand All @@ -72,6 +74,7 @@ Status LaunchXQAKernelImpl(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand Down Expand Up @@ -119,6 +122,7 @@ Status LaunchXQAKernel<__nv_bfloat16>(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand All @@ -136,15 +140,15 @@ Status LaunchXQAKernel<__nv_bfloat16>(
if (head_size == 256) {
return H256::LaunchXQAKernelImpl<__nv_bfloat16>(
device_prop, stream, query, key_cache, value_cache, output, batch_size, num_heads, kv_num_heads, head_size,
max_seq_len, scale, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
max_seq_len, scale, local_window_size, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
} else if (head_size == 128) {
return H128::LaunchXQAKernelImpl<__nv_bfloat16>(
device_prop, stream, query, key_cache, value_cache, output, batch_size, num_heads, kv_num_heads, head_size,
max_seq_len, scale, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
max_seq_len, scale, local_window_size, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
} else if (head_size == 64) {
return H64::LaunchXQAKernelImpl<__nv_bfloat16>(
device_prop, stream, query, key_cache, value_cache, output, batch_size, num_heads, kv_num_heads, head_size,
max_seq_len, scale, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
max_seq_len, scale, local_window_size, is_bsnh, past_seq_lens, attention_sinks, kv_cache_scale, kv_quant_type, workspace, workspace_size);
} else {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "XQA only supports head_size=64, 128, or 256. Input has ", head_size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ template Status HEAD_DIM_NAMESPACE::LaunchXQAKernelImpl<__nv_bfloat16>(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ template Status HEAD_DIM_NAMESPACE::LaunchXQAKernelImpl<__nv_bfloat16>(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ template Status HEAD_DIM_NAMESPACE::LaunchXQAKernelImpl<__nv_bfloat16>(
const int head_size,
const int max_seq_len,
const float scale,
const int local_window_size,
const bool is_bsnh,
const int* past_seq_lens,
const float* attention_sinks,
Expand Down
Loading
Loading