Skip to content

Commit eece52e

Browse files
[AOTI] Add BC-safe c_shim v2 for _scaled_dot_product_attention_math_for_mps enable_gqa (#181549)
[AOTI] Add BC-safe c_shim v2 for _scaled_dot_product_attention_math_for_mps enable_gqa (#181448) PR #179309 added enable_gqa to the _scaled_dot_product_attention_math_for_mps op schema, which broke the AOTI c_shim ABI for downstream runtimes that provide their own implementation (e.g. ExecuTorch Metal backend). Add the op to fallback_ops.py with a v2 entry so the codegen produces a BC-safe pair: - _v2: new signature taking enable_gqa - unversioned: old signature without enable_gqa, which calls the underlying op with the schema default (false) at the call site To make this work for non-optional args with defaults, extend the codegen in gen_aoti_c_shim.py to emit the arg's schema default via default_expr for skipped args, instead of always passing std::nullopt. std::nullopt remains the fallback for args with no default. Authored with Claude. Pull Request resolved: #181448 Approved by: https://github.com/desertfire ghstack dependencies: #181298, #181458 (cherry picked from commit 22790c5) Co-authored-by: Manuel Candales <[email protected]>
1 parent b39330b commit eece52e

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎torch/csrc/inductor/aoti_torch/generated/c_shim_mps.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__fused_moving_avg_obs_fq_helper_
2525
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__fused_rms_norm(AtenTensorHandle input, const int64_t* normalized_shape, int64_t normalized_shape_len_, AtenTensorHandle* weight, double* eps, AtenTensorHandle* ret0, AtenTensorHandle* ret1);
2626
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__grouped_mm(AtenTensorHandle self, AtenTensorHandle mat2, AtenTensorHandle* offs, AtenTensorHandle* bias, int32_t* out_dtype, AtenTensorHandle* ret0);
2727
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__histogramdd_from_bin_cts(AtenTensorHandle self, const int64_t* bins, int64_t bins_len_, const double** range, int64_t range_len_, AtenTensorHandle* weight, int32_t density, AtenTensorHandle* ret0);
28-
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__scaled_dot_product_attention_math_for_mps(AtenTensorHandle query, AtenTensorHandle key, AtenTensorHandle value, AtenTensorHandle* attn_mask, double dropout_p, int32_t is_causal, AtenTensorHandle* dropout_mask, double* scale, int32_t enable_gqa, AtenTensorHandle* ret0, AtenTensorHandle* ret1);
28+
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__scaled_dot_product_attention_math_for_mps_v2(AtenTensorHandle query, AtenTensorHandle key, AtenTensorHandle value, AtenTensorHandle* attn_mask, double dropout_p, int32_t is_causal, AtenTensorHandle* dropout_mask, double* scale, int32_t enable_gqa, AtenTensorHandle* ret0, AtenTensorHandle* ret1);
29+
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__scaled_dot_product_attention_math_for_mps(AtenTensorHandle query, AtenTensorHandle key, AtenTensorHandle value, AtenTensorHandle* attn_mask, double dropout_p, int32_t is_causal, AtenTensorHandle* dropout_mask, double* scale, AtenTensorHandle* ret0, AtenTensorHandle* ret1);
2930
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__scaled_dot_product_fused_attention_overrideable(AtenTensorHandle query, AtenTensorHandle key, AtenTensorHandle value, AtenTensorHandle* attn_bias, double dropout_p, int32_t is_causal, int32_t return_debug_mask, double* scale, AtenTensorHandle* ret0, AtenTensorHandle* ret1, AtenTensorHandle* ret2, AtenTensorHandle* ret3, int64_t* ret4, int64_t* ret5, AtenTensorHandle* ret6, AtenTensorHandle* ret7, AtenTensorHandle* ret8);
3031
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__scaled_dot_product_fused_attention_overrideable_backward(AtenTensorHandle grad_out, AtenTensorHandle query, AtenTensorHandle key, AtenTensorHandle value, AtenTensorHandle attn_bias, const int32_t* grad_input_mask, int64_t grad_input_mask_len_, AtenTensorHandle out, AtenTensorHandle logsumexp, AtenTensorHandle cum_seq_q, AtenTensorHandle cum_seq_k, int64_t max_q, int64_t max_k, double dropout_p, int32_t is_causal, AtenTensorHandle philox_seed, AtenTensorHandle philox_offset, double* scale, AtenTensorHandle* ret0, AtenTensorHandle* ret1, AtenTensorHandle* ret2, AtenTensorHandle* ret3);
3132
AOTI_TORCH_EXPORT AOTITorchError aoti_torch_mps__to_sparse(AtenTensorHandle self, int32_t* layout, const int64_t** blocksize, int64_t blocksize_len_, int64_t* dense_dim, AtenTensorHandle* ret0);

‎torchgen/aoti/fallback_ops.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"aten._int_mm.out": {},
4848
"aten._pdist_backward.default": {},
4949
"aten._pdist_forward.default": {},
50-
"aten._scaled_dot_product_attention_math_for_mps.default": {},
50+
"aten._scaled_dot_product_attention_math_for_mps.default": {"v2": ["enable_gqa"]},
5151
"aten._scaled_dot_product_cudnn_attention_backward.default": {},
5252
"aten._scaled_dot_product_cudnn_attention.default": {},
5353
"aten._scaled_dot_product_efficient_attention_backward.default": {},

‎torchgen/gen_aoti_c_shim.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,16 @@ def gen_arguments(
214214
callsite_exprs: list[str] = []
215215
for arg in flat_arguments:
216216
if arg.name in skipped_args:
217-
callsite_exprs.append("std::nullopt")
217+
# Pass the arg's schema default when available (e.g. "false" for
218+
# a bool arg with default=False), so non-optional args with defaults
219+
# can be versioned too. Fall back to std::nullopt for optional args
220+
# with no default (matches historical behavior).
221+
if arg.default is not None:
222+
from torchgen.api.cpp import default_expr
223+
224+
callsite_exprs.append(default_expr(arg.default, arg.type, symint=False))
225+
else:
226+
callsite_exprs.append("std::nullopt")
218227
continue
219228
new_types, names, _, new_callsite_exprs = convert_arg_type_and_name(
220229
arg.type, arg.name, arg.is_write

0 commit comments

Comments
 (0)