[CoreML EP] Add Where and And builders#28597
Draft
maxwbuckley wants to merge 6 commits into
Draft
Conversation
Two changes to the ML Program Cast builder: 1. Accept BOOL as a source and target dtype in HasSupportedInputsImpl. The ML Program `cast` op already handles bool, and AddToModelBuilderImpl already maps `to == BOOL`; only the input/output type gate omitted it. This lets int64<->bool<->float casts (transformer attention-mask graphs) stay on CoreML. 2. Move the "no preceding node" check after the ML Program early-return. It was legacy gating for the NeuralNetwork ArgMax-only path (which dereferences InputEdgesBegin()); on the ML Program path a Cast fed directly by a graph input is fine, and rejecting it forced needless CPU fallback. Tests (coreml_basic_test.cc): - CastBoolRoundTrip_MLProgram: an int64->bool->float cast chain runs fully on CoreML and matches the CPU reference. The bool tensor is internal (a CoreML partition cannot have bool I/O) and the first Cast is graph-input fed. - CastNonArgMaxNeuralNetworkNotSupported: the same chain falls back to CPU on the NeuralNetwork format, guarding the IsOpSupportedImpl reordering. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Two new ML Program op builders, both produced by transformer attention-mask graphs: - Where -> ML Program `select`. WhereOpBuilder gates X/Y to float / float16 (CoreML `select` operates on float branches) and requires the `cond` input to be bool. - And -> ML Program `logical_and`, via a new LogicalOpBuilder. Inputs must be bool. Both are ML-Program-only; IsOpSupportedImpl rejects them on the NeuralNetwork format so such nodes fall back to CPU. Stacked on the bool-Cast branch: `And`'s inputs and output are bool, and a CoreML partition cannot have bool I/O, so a meaningful `And` test sandwiches it between int<->bool casts (the bool stays internal). `Where` needs no such scaffolding -- its `cond` can be a constant initializer and X/Y/output are float. Tests (coreml_basic_test.cc): - Where_MLProgram: Where with a constant bool cond runs on CoreML, matches CPU. - WhereNeuralNetworkNotSupported: Where falls back on the NeuralNetwork format. - WhereNonFloatBranchesNotSupported: an int32 Where falls back to CPU. - And_MLProgram: a Cast->And->Cast chain runs fully on CoreML, matches CPU. - AndNeuralNetworkNotSupported: the chain falls back on the NeuralNetwork format. Doc: coreml_supported_mlprogram_ops.md lists And and Where. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 20, 2026
CastBoolRoundTrip_MLProgram exercised int64 -> Cast(bool) -> Cast(float). CoreML's compiler fuses the two back-to-back `cast` ops and drops the bool clamp (cast(cast(x,bool),fp32) collapses to cast(x,fp32)), so the round-trip produces the raw input value instead of 0/1 -- the test can't be numerically verified standalone. The bool-Cast support itself is correct: it is exercised end to end by the dependent PRs, where a non-Cast op sits between the int<->bool casts so no fusion occurs -- Cast->And->Cast (Where/And PR) and Cast->GatherND->Cast (GatherND PR), both numerically verified against the CPU EP. CastNonArgMaxNeuralNetworkNotSupported (the NeuralNetwork-format negative test) is kept; it guards the IsOpSupportedImpl reordering. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
# Conflicts: # onnxruntime/core/providers/coreml/builders/op_builder_factory.h
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
Two new ML Program op builders, both produced by transformer attention-mask
graphs:
Where→ ML Programselect.WhereOpBuildergates the X/Y branches tofloat / float16 and requires
condto be bool.And→ ML Programlogical_and, via a newLogicalOpBuilder. Inputs mustbe bool.
Both are ML-Program-only;
IsOpSupportedImplrejects them on the NeuralNetworkformat so such nodes fall back to CPU.
Depends on the bool-Cast PR
And's inputs and output are all bool, and a CoreML partition cannot have boolI/O, so a meaningful
Andtest sandwiches it betweenint ↔ boolcasts (the boolstays internal). This branch is therefore stacked on
coreml-cast-bool— thecb43b7c75fcommit in this PR is the bool-Cast PR and will drop from this diffonce that one merges (via
git merge main).Whereneeds no such scaffolding:its
condcan be a constant initializer and X/Y/output are float.Tests (
coreml_basic_test.cc)Where_MLProgram— Where with a constant boolcondruns on CoreML, matches CPU.WhereNeuralNetworkNotSupported— Where falls back on the NeuralNetwork format.WhereNonFloatBranchesNotSupported— an int32 Where falls back to CPU.And_MLProgram— aCast → And → Castchain runs fully on CoreML, matches CPU.AndNeuralNetworkNotSupported— the chain falls back on the NeuralNetwork format.Doc:
coreml_supported_mlprogram_ops.mdlistsAndandWhere.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.