Skip to content

CoreML missing partitioner constraint for topk with sorted=False #11698

Closed
@GregoryComer

Description

@GregoryComer

🐛 Describe the bug

CoreML partitioner is missing a constraint for sorted=False on iOS < 16. It errors out during lowering, but should not partition to allow it to fall back to portable or another backend.

Repro:

import torch

from executorch.backends.apple.coreml.partition import CoreMLPartitioner
from executorch.exir import to_edge_transform_and_lower
from executorch.extension.pybindings.portable_lib import _load_for_executorch_from_buffer

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()
        
    def forward(self, x):
        return torch.topk(x, 5, sorted=False)

model = Model()
inputs = (
    torch.randn(1, 1024),
)

eager_outputs = model(*inputs)
#print(f"Eager: {eager_outputs.shape} {eager_outputs}")

ep = torch.export.export(model, inputs)
lowered = to_edge_transform_and_lower(
    ep,
    partitioner=[CoreMLPartitioner()],
).to_executorch()

print(ep)

et_model = _load_for_executorch_from_buffer(lowered.buffer)
et_outputs = et_model([*inputs])[0]

et_outputs - eager_outputs

Output:

File [~/miniconda3/envs/executorch/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py:7306](http://localhost:8888/lab/tree/~/miniconda3/envs/executorch/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py#line=7305), in topk.<locals>._translate_torch_args(dim, largest, sorted)
   7304     sorted = sorted.val
   7305 if not sorted and not is_current_opset_version_compatible_with(target.iOS16):
-> 7306     raise Exception("For opset <= iOS16, only sorted=True supported for the topk")
   7308 return dim, not largest, sorted

Exception: For opset <= iOS16, only sorted=True supported for the topk

Versions

coremltools version 8.3
executorch commit 67b6009 (Jun 14)

cc @kimishpatel @YifanShenSZ @cymbalrush @metascroy

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend testerThis bug was found by the backend test suite.module: coremlIssues related to Apple's Core ML delegation and code under backends/apple/coreml/

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions