diff --git a/optimum/exporters/executorch/recipes/xnnpack.py b/optimum/exporters/executorch/recipes/xnnpack.py index 67b0601e..5657faaf 100644 --- a/optimum/exporters/executorch/recipes/xnnpack.py +++ b/optimum/exporters/executorch/recipes/xnnpack.py @@ -32,6 +32,7 @@ ExecutorchProgram, to_edge_transform_and_lower, ) +from executorch.exir.dialects._ops import ops from executorch.exir.passes import MemoryPlanningPass from optimum.executorch.passes.remove_padding_idx_embedding_pass import RemovePaddingIdxEmbeddingPass @@ -66,6 +67,17 @@ def _xnnpack_honors_enable_bf16() -> bool: return any(getattr(config, "enable_bf16", False) for config in configs) +def _has_accumulating_index_put(program: ExportedProgram) -> bool: + """The portable index_put_ kernel cannot handle accumulate=True.""" + for node in program.graph.nodes: + if node.op != "call_function" or node.target != ops.edge.aten.index_put.default: + continue + accumulate = node.kwargs.get("accumulate", node.args[3] if len(node.args) > 3 else False) + if accumulate is not False: + return True + return False + + @register_recipe("xnnpack") def export_to_executorch_with_xnnpack( model: Union[ @@ -151,6 +163,13 @@ def _lower_to_executorch( constant_methods=metadata, transform_passes=[RemovePaddingIdxEmbeddingPass()], ) + if run_reinplace_pass and any( + _has_accumulating_index_put(et_prog.exported_program(method)) for method in et_prog.methods + ): + # Reinplacing any of these nodes would fail at runtime. The pass is + # configured per export, so keep the whole program functional. + backend_config_dict["run_reinplace_pass"] = False + logging.warning("Skipping re-inplace: index_put with accumulate=True is not supported in-place") et_prog = et_prog.to_executorch( config=ExecutorchBackendConfig(**backend_config_dict), ) diff --git a/tests/test_xnnpack_reinplace.py b/tests/test_xnnpack_reinplace.py new file mode 100644 index 00000000..04fa1f2c --- /dev/null +++ b/tests/test_xnnpack_reinplace.py @@ -0,0 +1,48 @@ +# Copyright 2026 The HuggingFace Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from types import SimpleNamespace + +import pytest +import torch +from executorch.exir.dialects._ops import ops + +from optimum.exporters.executorch.recipes.xnnpack import _has_accumulating_index_put + + +@pytest.mark.parametrize( + ("accumulate", "use_kwargs", "unsafe"), + [(False, False, False), (True, False, True), (False, True, False), (True, True, True)], +) +def test_accumulating_index_put_guard(accumulate, use_kwargs, unsafe): + graph = torch.fx.Graph() + source = graph.placeholder("source") + indices = graph.placeholder("indices") + values = graph.placeholder("values") + args = (source, indices, values) + kwargs = {"accumulate": accumulate} if use_kwargs else {} + if not use_kwargs: + args += (accumulate,) + result = graph.call_function(ops.edge.aten.index_put.default, args=args, kwargs=kwargs) + graph.output(result) + + assert _has_accumulating_index_put(SimpleNamespace(graph=graph)) is unsafe + + +def test_other_ops_do_not_disable_reinplace(): + graph = torch.fx.Graph() + source = graph.placeholder("source") + graph.output(source) + + assert not _has_accumulating_index_put(SimpleNamespace(graph=graph))