Skip to content

Commit 540bab5

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

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

.ci/scripts/test_lora.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,17 @@ 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:
137-
<think>
136+
EXPECTED_QUANT_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: me
138137
Okay, so I need to calculate 15% of 80."
139138
EXPECTED_QUANT_LORA_PREFIX="
140139
<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant
141-
To calculate 15% of 80, we can multiply 80 by 15/100 and then simplify the fraction.
142-
So, 15% of 80 is equal to (80 * 15) / 100 = 1200 / 100 = 12.
140+
To calculate 15% of 80, we can multiply 80 by 15/100.
141+
So, 15% of 80 is equal to 80 * 15/100 = 12.
143142
#### 12
144143
The answer is: 12<|im_end|>"
145144

146145

146+
147147
# Export Quantized PTE, PTD file, no LoRA.
148148
# override base.lora_config=null to avoid creating a lora model
149149
# and loading lora weights.
@@ -203,7 +203,7 @@ fi
203203
NOW=$(date +"%H:%M:%S")
204204
echo "Test 4: Quantized, program-data separation lora. Starting to run llama runner at ${NOW}"
205205
# shellcheck source=/dev/null
206-
cmake-out/examples/models/llama/llama_main --model_path=qwen_lora_math_q.pte --data_paths="qwen_foundation_q.ptd,qwen_lora_math_q.ptd" --prompt="${PROMPT}" ${RUNTIME_ARGS} --seq_len=104 > result.txt
206+
cmake-out/examples/models/llama/llama_main --model_path=qwen_lora_math_q.pte --data_paths="qwen_foundation_q.ptd,qwen_lora_math_q.ptd" --prompt="${PROMPT}" ${RUNTIME_ARGS} > result.txt
207207
NOW=$(date +"%H:%M:%S")
208208
echo "Finished at ${NOW}"
209209

.ci/scripts/test_lora_multimethod.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ 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:
71-
<think>
70+
EXPECTED_BASE_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: me
7271
Okay, so I need to calculate 15% of 80."
7372

7473
### TEST 1: Run lora_forward method ###

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)