Commit 21e9fe3
[CoreML EP] Add GatherND builder (#28598)
### Summary
New ML Program op builder: ONNX `GatherND` → CoreML `gather_nd`.
- `batch_dims` must be 0 — the iOS15 `gather_nd` op has no `batch_dims`
parameter;
`IsOpSupportedImpl` rejects other values.
- CoreML's `gather_nd` rejects a **bool `x`**, but transformer
attention-mask
graphs gather from bool tensors. For bool data the builder lowers the op
as
`cast(bool→int32) → gather_nd → cast(int32→bool)`; int32 represents 0/1
exactly,
so the round-trip is lossless.
- `validate_indices` is passed explicitly — the ML Program parser
rejects
`gather_nd` without it (the same quirk the `gather` builder works
around).
- ML-Program-only; `IsOpSupportedImpl` rejects the NeuralNetwork format.
### Indices handling (CoreML `gather_nd` quirks)
Two CoreML behaviours that differ from ONNX are handled in the builder:
- **`indices` must be a constant initializer.** CoreML's `gather_nd`
miscomputes
the result for some data/indices shape combinations when `indices` is a
runtime
(non-constant) input — it returns slice 0 regardless of the actual index
value.
With a constant `indices` it is correct, so non-constant cases fall back
to CPU.
Constant indices is also the common case (e.g. transformer attention
masks).
- **Negative indices are normalized at build time.** ONNX `GatherND`
wraps a
negative index by the corresponding data dim; CoreML's `gather_nd` does
not and
silently returns wrong values. Since `indices` is constant, the builder
wraps any
negatives into positive int32 indices while building the model (and
requires the
indexed data dims to be static, otherwise the node falls back to CPU).
This was
surfaced by fuzzing over randomized shapes/indices and verified
on-device
(negative indices, scalar outputs, ranks 2–4) against the CPU reference.
### Depends on the bool-Cast PR
The bool-data `GatherND` test needs `Cast` as the `int ↔ bool`
producer/consumer so
the bool tensors stay internal to the CoreML partition (a partition
cannot have
bool I/O). This branch is **stacked on `coreml-cast-bool`** — the
`cb43b7c75f`
commit in this PR is the bool-Cast PR and drops from this diff once that
one
merges.
### Tests (`coreml_basic_test.cc`)
- `GatherND_MLProgram` — a float `GatherND` runs on CoreML, matches CPU.
- `GatherNDBoolData_MLProgram` — a `Cast → GatherND → Cast` bool chain
runs fully
on CoreML, exercising the cast round-trip lowering.
- `GatherNDNeuralNetworkNotSupported` — `GatherND` falls back on the
NeuralNetwork
format.
- `GatherNDBatchDimsNotSupported` — `GatherND` with `batch_dims=1` falls
back to CPU.
Doc: `coreml_supported_mlprogram_ops.md` lists `GatherND`.
### Series — CoreML EP coverage for transformer / diffusion graphs
- #28595 — Support bool Cast in ML Program *(prerequisite)*
- #28596 — Add Sin and Cos unary ops *(independent)*
- #28597 — Add Where and And builders *(depends on #28595)*
- **#28598 — Add GatherND builder** *(this PR — depends on #28595)*
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.
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent d113549 commit 21e9fe3
8 files changed
Lines changed: 614 additions & 2 deletions
File tree
- onnxruntime
- core/providers/coreml
- builders
- impl
- model
- test/providers/coreml
- tools/ci_build/github/apple
Lines changed: 225 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
Lines changed: 110 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
917 | 917 | | |
918 | 918 | | |
919 | 919 | | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
920 | 926 | | |
921 | 927 | | |
922 | 928 | | |
| |||
932 | 938 | | |
933 | 939 | | |
934 | 940 | | |
| 941 | + | |
| 942 | + | |
935 | 943 | | |
936 | 944 | | |
937 | 945 | | |
938 | 946 | | |
939 | 947 | | |
940 | | - | |
| 948 | + | |
| 949 | + | |
941 | 950 | | |
942 | | - | |
| 951 | + | |
943 | 952 | | |
944 | 953 | | |
945 | 954 | | |
946 | 955 | | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
947 | 964 | | |
948 | 965 | | |
949 | 966 | | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
950 | 1056 | | |
| 1057 | + | |
951 | 1058 | | |
952 | 1059 | | |
953 | 1060 | | |
| |||
994 | 1101 | | |
995 | 1102 | | |
996 | 1103 | | |
| 1104 | + | |
997 | 1105 | | |
998 | 1106 | | |
999 | 1107 | | |
| |||
0 commit comments