Skip to content

Commit ffb29f7

Browse files
committed
Add support for seq at dim=2
This makes custom sdpa and quantized sdpa on feature parity and allows for transposed kv cache Differential Revision: [D93870394](https://our.internmc.facebook.com/intern/diff/D93870394/) [ghstack-poisoned]
1 parent c0e8395 commit ffb29f7

6 files changed

Lines changed: 394 additions & 13 deletions

File tree

examples/models/llama/source_transformation/sdpa.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def forward(
6969
0, # dropout probability. Ignored by the code
7070
True, # is_causal
7171
)
72+
if self.is_seq_at_dim_2:
73+
output = output.transpose(1, 2).contiguous()
7274
return output.view(bsz, seqlen, self.dim).to(dtype=input_dtype)
7375

7476

@@ -198,6 +200,8 @@ def forward(
198200
v_scale_fp32,
199201
)
200202

203+
if self.is_seq_at_dim_2:
204+
output = output.transpose(1, 2).contiguous()
201205
return output.view(bsz, seqlen, self.dim)
202206

203207

extension/llm/custom_ops/custom_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ def custom_sdpa(
167167
drpout_p=0.0,
168168
is_causal=False,
169169
scale=None,
170+
is_seq_dim_2=False,
170171
):
171-
seq_len = query.size(1)
172+
seq_len = query.size(2) if is_seq_dim_2 else query.size(1)
172173
_validate_params(
173174
query,
174175
key_cache,

extension/llm/custom_ops/op_sdpa.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,13 @@ Tensor& custom_sdpa_out_impl(
360360
output,
361361
"Invalid arguments");
362362

363-
int64_t seq_len = q.size(1);
364363
SeqDim seq_dim{SeqDim::TWO};
365364
if (!is_seq_at_dim_2) {
366365
seq_dim = SeqDim::ONE;
367366
}
367+
int64_t seq_len = q.size(static_cast<int64_t>(seq_dim));
368368

369369
if (q.scalar_type() == ScalarType::Char) {
370-
if (seq_dim == SeqDim::TWO) {
371-
seq_len = q.size(2);
372-
}
373370
ET_KERNEL_CHECK_MSG(
374371
ctx,
375372
q_scales.has_value() && q_zero_points.has_value() &&
@@ -564,9 +561,26 @@ Tensor& custom_sdpa_out(
564561
const bool is_causal,
565562
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
566563
const optional<double> scale,
564+
const bool is_seq_dim_2,
567565
Tensor& output) {
568566
return custom_sdpa_out_impl(
569-
ctx, q, k, v, start_pos, attn_mask, dropout_p, is_causal, scale, output);
567+
ctx,
568+
q,
569+
k,
570+
v,
571+
start_pos,
572+
attn_mask,
573+
dropout_p,
574+
is_causal,
575+
scale,
576+
output,
577+
nullopt,
578+
nullopt,
579+
nullopt,
580+
nullopt,
581+
nullopt,
582+
nullopt,
583+
is_seq_dim_2);
570584
}
571585
/*
572586
Input params
@@ -621,6 +635,7 @@ Tensor& sdpa_with_kv_cache_out(
621635
dropout_p,
622636
is_causal,
623637
scale,
638+
false, // is_seq_dim_2 - default to false for backward compatibility
624639
output);
625640

626641
return output;

extension/llm/custom_ops/op_sdpa.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Tensor& custom_sdpa_out(
4242
const bool is_causal,
4343
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
4444
const optional<double> scale,
45+
const bool is_seq_dim_2,
4546
Tensor& output);
4647

4748
Tensor& flash_attention_kernel_out(

extension/llm/custom_ops/op_sdpa_aot.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Tensor& custom_sdpa_out_no_context(
6262
const bool is_causal,
6363
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
6464
const optional<double> scale,
65+
const bool is_seq_dim_2,
6566
Tensor& output);
6667

6768
at::Tensor custom_sdpa_aten(
@@ -75,7 +76,8 @@ at::Tensor custom_sdpa_aten(
7576
const double dropout_p,
7677
const bool is_causal,
7778
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
78-
const std::optional<double> scale);
79+
const std::optional<double> scale,
80+
const bool is_seq_dim_2);
7981

8082
Tensor& custom_quantized_sdpa_out_no_context(
8183
const Tensor& q,
@@ -224,6 +226,7 @@ Tensor& custom_sdpa_out_no_context(
224226
const bool is_causal,
225227
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
226228
const optional<double> scale,
229+
const bool is_seq_dim_2,
227230
Tensor& output) {
228231
executorch::aten::RuntimeContext context{};
229232
return torch::executor::native::custom_sdpa_out(
@@ -236,6 +239,7 @@ Tensor& custom_sdpa_out_no_context(
236239
dropout_p,
237240
is_causal,
238241
scale,
242+
is_seq_dim_2,
239243
output);
240244
}
241245

@@ -250,10 +254,20 @@ at::Tensor custom_sdpa_aten(
250254
const double dropout_p,
251255
const bool is_causal,
252256
// @lint-ignore CLANGTIDY facebook-hte-ParameterMightThrowOnCopy
253-
const std::optional<double> scale) {
257+
const std::optional<double> scale,
258+
const bool is_seq_dim_2) {
254259
auto output = at::empty(q.sizes());
255-
WRAP_TO_ATEN(custom_sdpa_out_no_context, 8)
256-
(q, k, v, start_pos, attn_mask, dropout_p, is_causal, scale, output);
260+
WRAP_TO_ATEN(custom_sdpa_out_no_context, 9)
261+
(q,
262+
k,
263+
v,
264+
start_pos,
265+
attn_mask,
266+
dropout_p,
267+
is_causal,
268+
scale,
269+
is_seq_dim_2,
270+
output);
257271
return output;
258272
}
259273

@@ -401,11 +415,11 @@ TORCH_LIBRARY_FRAGMENT(llama, m) {
401415
m.def(
402416
"custom_sdpa(Tensor query, Tensor key, Tensor value, SymInt start_pos, "
403417
"Tensor? attn_mask=None, float drpout_p=0.0, bool is_causal=False, "
404-
"float? scale=None) -> Tensor");
418+
"float? scale=None, bool is_seq_dim_2=False) -> Tensor");
405419
m.def(
406420
"custom_sdpa.out(Tensor query, Tensor key, Tensor value, SymInt start_pos, "
407421
"Tensor? attn_mask=None, float drpout_p=0.0, bool is_causal=False, "
408-
"float? scale=None, *, Tensor(a!) out) -> Tensor(a!)");
422+
"float? scale=None, bool is_seq_dim_2=False, *, Tensor(a!) out) -> Tensor(a!)");
409423
m.def(
410424
"update_cache(Tensor value, Tensor(a!) cache, "
411425
"SymInt start_pos, bool is_seq_dim_2=False) -> Tensor");
@@ -443,7 +457,7 @@ TORCH_LIBRARY_IMPL(llama, CompositeExplicitAutograd, m) {
443457
m.impl("custom_sdpa", torch::executor::native::custom_sdpa_aten);
444458
m.impl(
445459
"custom_sdpa.out",
446-
WRAP_TO_ATEN(torch::executor::native::custom_sdpa_out_no_context, 8));
460+
WRAP_TO_ATEN(torch::executor::native::custom_sdpa_out_no_context, 9));
447461
m.impl("update_cache", torch::executor::native::update_cache_aten);
448462
m.impl(
449463
"update_cache.out",

0 commit comments

Comments
 (0)