Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions optimum/exporters/executorch/recipes/xnnpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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),
)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_xnnpack_reinplace.py
Original file line number Diff line number Diff line change
@@ -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))