[CoreML EP] Add Sin and Cos unary ops#28596
Draft
maxwbuckley wants to merge 2 commits into
Draft
Conversation
Lower ONNX Sin and Cos to the CoreML ML Program `sin` / `cos` elementwise ops via the existing UnaryOpBuilder, and register them in the op builder factory. Like Erf/Round/Exp, these have no NeuralNetwork lowering (UnaryFunctionLayerParams has no sin/cos), so IsOpSupportedImpl rejects them on the NeuralNetwork format. These appear in the timestep (sinusoidal position) embedding of diffusion UNets; supporting them lets that prologue stay on CoreML instead of splitting the graph into separate partitions. Tests (coreml_basic_test.cc): - SinCos_MLProgram: a Sin+Cos graph runs fully on CoreML and matches the CPU reference. - SinCosNeuralNetworkNotSupported: the same graph falls back to CPU on the NeuralNetwork format. Doc: coreml_supported_mlprogram_ops.md lists Sin and Cos. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 20, 2026
# Conflicts: # onnxruntime/core/providers/coreml/builders/impl/unary_op_builder.cc # onnxruntime/core/providers/coreml/builders/op_builder_factory.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lower ONNX
SinandCosto the CoreML ML Programsin/coselementwise opsvia the existing
UnaryOpBuilder, registered in the op builder factory. LikeErf/Round/Exp, these have no NeuralNetwork lowering(
UnaryFunctionLayerParamshas no sin/cos), soIsOpSupportedImplrejects them onthe NeuralNetwork format.
Why
Sin/Cosform the sinusoidal timestep embedding of diffusion UNets. Supportingthem keeps that prologue on CoreML instead of splitting the graph — a tiny
Stable-Diffusion UNet goes from 2 CoreML partitions → 1, zero graph breaks with
this change alone.
This PR is independent of the rest of the series (it touches only the unary
builder) and can be reviewed/merged in any order.
Tests (
coreml_basic_test.cc)SinCos_MLProgram— a Sin + Cos graph runs fully on CoreML and matches the CPUreference.
SinCosNeuralNetworkNotSupported— the same graph falls back to CPU on theNeuralNetwork format.
Doc:
coreml_supported_mlprogram_ops.mdlistsSinandCos.Series — CoreML EP coverage for transformer / diffusion graphs
Together with #28278 (scalar-
Gather), the series takes BERT / GPT-2 / ViT /diffusion-UNet graphs — tiny and full-size — from 2 CoreML partitions to 1, with
zero graph breaks.