Skip to content

Commit 36a2aef

Browse files
committed
Use unfused SDPA for short sequences (q_len <= 128 or kv_len <= 128)
Pull Request resolved: #18651 ATT ghstack-source-id: 376092495 @exported-using-ghexport Differential Revision: [D96044308](https://our.internmc.facebook.com/intern/diff/D96044308/)
1 parent 79f7fdf commit 36a2aef

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

.ci/scripts/test_lora.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ else
133133
fi
134134

135135
### QUANTIZATION & PROGRAM DATA SEPARATION ###
136-
EXPECTED_QUANT_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant:
136+
EXPECTED_QUANT_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: me
137+
<think>
137138
<think>
138139
Okay, so I need to calculate 15% of 80."
139140
EXPECTED_QUANT_LORA_PREFIX="

.ci/scripts/test_lora_multimethod.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ EXPECTED_LORA_PREFIX="
6767
<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant
6868
To calculate 15% of 80"
6969

70-
EXPECTED_BASE_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant:
70+
EXPECTED_BASE_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: me
71+
<think>
7172
<think>
7273
Okay, so I need to calculate 15% of 80."
7374

extension/llm/custom_ops/op_sdpa.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,12 @@ Tensor& custom_sdpa_out_impl(
412412
InvalidArgument,
413413
output);
414414

415-
bool use_unfused_sdpa = seq_len == 1;
415+
// Quantized GEMM kernels may not handle non-contiguous per-head strides
416+
// correctly when seq_dim=ONE and seq_len > 1, so keep the conservative
417+
// condition for quantized inputs.
418+
bool is_quantized = q.scalar_type() == ScalarType::Char;
419+
bool use_unfused_sdpa = (!is_quantized) &&
420+
(seq_len <= 128 || num_keys_for_causal_attention <= 128);
416421
if (use_unfused_sdpa) {
417422
ET_SWITCH_FLOAT_TYPES(output.scalar_type(), ctx, "sdpa", CTYPE, [&] {
418423
sdpa::impl::cpu_sdpa<CTYPE>(

0 commit comments

Comments
 (0)