Skip to content

Commit 743a400

Browse files
[fusilli] Use flex attention for custom scale SDPA custom op
Emit torch.hop_flex_attention from the SDPA custom-op sample when a custom scale is present and the case does not require explicit mask, dropout, or causal handling. Remove the stale xfail for the custom-op custom-scale sample while keeping the regular SDPA custom-scale xfail on the legacy path. Co-authored-by: GPT-5 Codex <noreply@openai.com> Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha@gmail.com>
1 parent 0bb3c82 commit 743a400

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

samples/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ add_fusilli_samples(
199199
Catch2::Catch2WithMain
200200
)
201201

202-
# XFAIL: TODO(#404): Remove once the compiler supports non-default SDPA scales.
202+
# XFAIL: TODO(#404): Remove once regular SDPA supports non-default scales.
203203
set_tests_properties(
204-
fusilli_sdpa_custom_op_samples_sdpa_fprop_custom_scale
205204
fusilli_sdpa_samples_sdpa_fprop_custom_scale
206205
PROPERTIES WILL_FAIL TRUE)

samples/sdpa_utils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ std::string buildSdpaMlir(bool hasAttnMask, float dropoutP, bool isCausal,
3636
: "torch.constant.none";
3737
std::string scaleTypeStr = scale.has_value() ? "!torch.float" : "!torch.none";
3838
std::string enableGqaStr = enableGqa ? "true" : "false";
39+
if (!hasAttnMask && dropoutP == 0.0f && !isCausal && scale.has_value()) {
40+
std::string enableGqaAttr = enableGqa ? " {enable_gqa = true}" : "";
41+
return std::vformat(kFlexSdpaNoMask,
42+
std::make_format_args(scaleConstStr, // {0}
43+
scaleTypeStr, // {1}
44+
enableGqaAttr)); // {2}
45+
}
3946

4047
return std::vformat(hasAttnMask ? kSdpaWithMask : kSdpaNoMask,
4148
std::make_format_args(dropoutStr, // {0} DROPOUT_P

samples/sdpa_utils.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
using namespace fusilli;
2020

21-
// SDPA MLIR templates for torch.aten.scaled_dot_product_attention.
21+
// SDPA MLIR templates.
2222
//
2323
// Templates are stored as R-string literals so the MLIR structure is
2424
// directly readable in source. Standard CustomOp placeholders
@@ -27,10 +27,29 @@ using namespace fusilli;
2727
// ({DROPOUT_P}, {IS_CAUSAL}, {SCALE_CONST}, {SCALE_TYPE}, {ENABLE_GQA})
2828
// are resolved by buildSdpaMlir().
2929

30+
// clang-format off
31+
// Flex attention template: 3 tensor inputs (Q, K, V), attention mask is none.
32+
// Positional args: {0}=SCALE_CONST, {1}=SCALE_TYPE, {2}=ENABLE_GQA_ATTR
33+
inline constexpr std::string_view kFlexSdpaNoMask = R"mlir(
34+
func.func private @{{FUNC_NAME}}(
35+
%arg0: {{IN0_TYPE}},
36+
%arg1: {{IN1_TYPE}},
37+
%arg2: {{IN2_TYPE}})
38+
-> {{OUT0_TYPE}} {{
39+
%scale = {0}
40+
%return_lse = torch.constant.bool false
41+
%return_max_scores = torch.constant.bool false
42+
%0, %logsumexp, %max_scores = torch.hop_flex_attention %arg0, %arg1, %arg2,
43+
%scale, %return_lse, %return_max_scores{2} :
44+
{{IN0_TYPE}}, {{IN1_TYPE}}, {{IN2_TYPE}},
45+
{1}, !torch.bool, !torch.bool -> {{OUT0_TYPE}}, !torch.none, !torch.none
46+
return %0 : {{OUT0_TYPE}}
47+
}}
48+
)mlir";
49+
3050
// SDPA template: 3 tensor inputs (Q, K, V), attention mask is none.
3151
// Positional args: {0}=DROPOUT_P, {1}=IS_CAUSAL, {2}=SCALE_CONST,
3252
// {3}=SCALE_TYPE, {4}=ENABLE_GQA
33-
// clang-format off
3453
inline constexpr std::string_view kSdpaNoMask = R"mlir(
3554
func.func private @{{FUNC_NAME}}(
3655
%arg0: {{IN0_TYPE}},

0 commit comments

Comments
 (0)