Skip to content
Open
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
16 changes: 16 additions & 0 deletions optimum/exporters/executorch/recipes/metal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
METAL_BACKEND_AVAILABLE = False

if METAL_BACKEND_AVAILABLE:
import torch

from tabulate import tabulate
from torch.export import ExportedProgram

Expand All @@ -52,6 +54,13 @@
)
from ..recipe_registry import register_recipe

def _linear_bias_decomposition(input, weight, bias=None):
weight_t = torch.ops.aten.t.default(weight)
out = torch.ops.aten.matmul.default(input, weight_t)
if bias is not None:
return torch.ops.aten.add.Tensor(out, bias)
return out

@register_recipe("metal")
def export_to_executorch_with_metal(
model: Union[
Expand Down Expand Up @@ -89,6 +98,13 @@ def _lower_to_executorch(
if len(exported_programs) == 1:
exported_programs = {"forward": next(iter(exported_programs.values()))}

# Decompose linear+bias into matmul+add to avoid addmm,
# which the Metal backend doesn't support.
for key in exported_programs:
decomp_table = torch.export.default_decompositions()
decomp_table[torch.ops.aten.linear.default] = _linear_bias_decomposition
exported_programs[key] = exported_programs[key].run_decompositions(decomp_table)

partitioners = {
key: [MetalPartitioner([MetalBackend.generate_method_name_compile_spec(key)])]
for key in exported_programs.keys()
Expand Down