Skip to content

Add dynamic shape support to CoreML #9094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 14, 2025
Merged

Conversation

metascroy
Copy link
Contributor

@metascroy metascroy commented Mar 10, 2025

Summary:
Dynamic shape currently lead to runtime errors in CoreML.

The AOT flow prepares the CoreML pte to work with dynamic shapes. These inputs are correctly evaluated by the CoreML model, but a runtime error occurs when copying the model outputs back to ET.

This is because the model outputs have shape that is smaller than the outputArg MultiArray and the copy fails.

This PR resizes the output args to account for dynamic shapes in the ET CoreML delegate.

Note that outputBackings appear to fail in the dynamic case, likely because the outputBacking tensor is bigger than it should be. Ideally CoreML would not fail as long as the outputBacking had enough storage to hold the output since we don't know the expected size until after the model runs. The output is resized after the model is run anyway.

In any case, dynamic shapes take the "ignoreOutputBackings" route.

cc @kimishpatel @YifanShenSZ @cymbalrush

Summary:
Dynamic shape currently lead to runtime errors in CoreML.

The AOT flow prepares the CoreML pte to work with dynamic shapes.  These inputs are correctly evaluated by the CoreML model, but a runtime error occurs when copying the model outputs back to ET.

This is because the model outputs have shape that is smaller than the outputArg MultiArray and the copy fails.

This PR resizes the output args to account for dynamic shapes in the ET CoreML delegate.

Note that outputBackings appear to fail in the dynamic case, likely because the outputBacking tensor is bigger than it should be.  Ideally CoreML would not fail as long as the outputBacking had enough storage to hold the output since we don't know the expected size until after the model runs.  The output is resized after the model is run anyway.

In any case, dynamic shapes take the "ignoreOutputBackings" route.
Copy link

pytorch-bot bot commented Mar 10, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/9094

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 2da75d5 with merge base db4a500 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 10, 2025
@metascroy metascroy requested a review from cymbalrush March 10, 2025 17:58
@facebook-github-bot
Copy link
Contributor

@metascroy has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@cymbalrush cymbalrush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thank you!

@cccclai
Copy link
Contributor

cccclai commented Mar 10, 2025

Thanks - lint error needs to be fixed and it will be good to have a test coverage.

@@ -903,6 +903,10 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
endif()
endif()

if(EXECUTORCH_BUILD_COREML)
list(APPEND _executor_runner_libs coremldelegate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it for the portable executor runner? Is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove, it was just very convenient to have a C++ runner that works with CoreML pte files for debugging the existing crash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess it's fine

@metascroy
Copy link
Contributor Author

Thanks - lint error needs to be fixed and it will be good to have a test coverage.

Will add

@metascroy
Copy link
Contributor Author

Thanks - lint error needs to be fixed and it will be good to have a test coverage.

Added new tests for resize. In doing so, I noticed one of the existing tests is broken on main (diabled it in this PR). We don't run these tests in CI, but I added an issue to enable them (#9115).

@facebook-github-bot
Copy link
Contributor

@metascroy has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@metascroy
Copy link
Contributor Author

@shoumikhin can you take a look at the error in building the benchmark app in the CI here. It's not clear to me what the issue is. Note that the frameworks build correctly.

@metascroy metascroy added the module: coreml Issues related to Apple's Core ML delegation and code under backends/apple/coreml/ label Mar 14, 2025
@metascroy
Copy link
Contributor Author

@pytorchbot label "topic: not user facing"

@facebook-github-bot facebook-github-bot merged commit 3c895f8 into main Mar 14, 2025
56 of 59 checks passed
@facebook-github-bot facebook-github-bot deleted the coreml-dynamic-shape branch March 14, 2025 18:28
DannyYuyang-quic pushed a commit to CodeLinaro/executorch that referenced this pull request Apr 2, 2025
Differential Revision: D70904915

Pull Request resolved: pytorch#9094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: coreml Issues related to Apple's Core ML delegation and code under backends/apple/coreml/ topic: not user facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants