Skip to content

Commit 7444fee

Browse files
authored
Cleanup CoreML EP's code to remove COREML_ENABLE_MLPROGRAM (microsoft#23490)
### Description Cleanup CoreML EP's code to remove the COREML_ENABLE_MLPROGRAM macro. Also, increase MINIMUM_COREML_VERSION(first version we support) to 5 .
1 parent 3488ba3 commit 7444fee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+55
-201
lines changed

onnxruntime/core/providers/coreml/builders/impl/activation_op_builder.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ Status ActivationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
9797
const logging::Logger& logger) const {
9898
const auto& op_type(node.OpType());
9999

100-
#if defined(COREML_ENABLE_MLPROGRAM)
101100
if (model_builder.CreateMLProgram()) {
102101
using namespace CoreML::Specification::MILSpec;
103102
// https://apple.github.io/coremltools/source/coremltools.converters.mil.mil.ops.defs.html#module-coremltools.converters.mil.mil.ops.defs.iOS15.activation
@@ -166,9 +165,7 @@ Status ActivationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
166165

167166
model_builder.AddOperation(std::move(op));
168167

169-
} else
170-
#endif // (COREML_ENABLE_MLPROGRAM)
171-
{
168+
} else {
172169
std::unique_ptr<COREML_SPEC::NeuralNetworkLayer> layer = model_builder.CreateNNLayer(node);
173170

174171
if (op_type == "Sigmoid") {

onnxruntime/core/providers/coreml/builders/impl/argmax_op_builder.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Status ArgMaxOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
3232
const int64_t keepdims = helper.Get("keepdims", 1);
3333
const bool removedim = keepdims != 1;
3434

35-
#if defined(COREML_ENABLE_MLPROGRAM)
3635
if (model_builder.CreateMLProgram()) {
3736
using namespace CoreML::Specification::MILSpec;
3837
// https://apple.github.io/coremltools/source/coremltools.converters.mil.mil.ops.defs.html#module-coremltools.converters.mil.mil.ops.defs.iOS15.reduction
@@ -46,9 +45,7 @@ Status ArgMaxOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
4645
// the output of ArgMax must be int32
4746
AddOperationOutput(*op, *node.OutputDefs()[0], output_datatype);
4847
model_builder.AddOperation(std::move(op));
49-
} else
50-
#endif // (COREML_ENABLE_MLPROGRAM)
51-
{
48+
} else {
5249
auto* coreml_argmax = layer->mutable_argmax();
5350
coreml_argmax->set_axis(axis);
5451
coreml_argmax->set_removedim(removedim);
@@ -91,11 +88,9 @@ bool ArgMaxOpBuilder::IsOpSupportedImpl(const Node& node,
9188
return false;
9289
}
9390

94-
#if defined(COREML_ENABLE_MLPROGRAM)
9591
if (input_params.create_mlprogram) {
9692
return true;
9793
}
98-
#endif
9994

10095
// If there are multiple downstream nodes and cast (toint32) is one of them
10196
// not supported, exit here

onnxruntime/core/providers/coreml/builders/impl/batch_norm_op_builder.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Status BatchNormalizationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_bu
5757
const auto eps = helper.Get("epsilon", 1e-5f);
5858
const auto channels = scale_tensor.dims()[0];
5959

60-
#if defined(COREML_ENABLE_MLPROGRAM)
6160
if (model_builder.CreateMLProgram()) {
6261
using namespace CoreML::Specification::MILSpec;
6362
// https://apple.github.io/coremltools/source/coremltools.converters.mil.mil.ops.defs.html#coremltools.converters.mil.mil.ops.defs.iOS15.normalization.batch_norm
@@ -78,9 +77,7 @@ Status BatchNormalizationOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_bu
7877

7978
AddOperationOutput(*op, *node.OutputDefs()[0]);
8079
model_builder.AddOperation(std::move(op));
81-
} else
82-
#endif // (COREML_ENABLE_MLPROGRAM)
83-
{
80+
} else {
8481
auto* coreml_batch_norm = layer->mutable_batchnorm();
8582
coreml_batch_norm->set_channels(channels);
8683
coreml_batch_norm->set_epsilon(eps);

onnxruntime/core/providers/coreml/builders/impl/binary_op_builder.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ bool CheckIfBothInputShapesMatch(const Node& node, const logging::Logger& logger
5656
}
5757
} // namespace
5858

59-
#if defined(COREML_ENABLE_MLPROGRAM)
6059
static std::vector<int64_t> InferOutputShape(const std::vector<int64_t>& a, const std::vector<int64_t>& b) {
6160
std::vector<int64_t> output_shape;
6261
int64_t i_a = 0, j_b = 0;
@@ -112,14 +111,12 @@ static void AddVariadicInputs(std::unique_ptr<CoreML::Specification::MILSpec::Op
112111
}
113112
*op = std::move(op_prev);
114113
}
115-
#endif
116114

117115
Status BinaryOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const Node& node,
118116
const logging::Logger& logger) const {
119117
const auto& op_type(node.OpType());
120118
const auto& input_defs(node.InputDefs());
121119

122-
#if defined(COREML_ENABLE_MLPROGRAM)
123120
if (model_builder.CreateMLProgram()) {
124121
using namespace CoreML::Specification::MILSpec;
125122

@@ -153,9 +150,7 @@ Status BinaryOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const
153150
}
154151
AddOperationOutput(*op, *node.OutputDefs()[0]);
155152
model_builder.AddOperation(std::move(op));
156-
} else
157-
#endif // defined (COREML_ENABLE_MLPROGRAM)
158-
{
153+
} else {
159154
std::unique_ptr<COREML_SPEC::NeuralNetworkLayer> layer = model_builder.CreateNNLayer(node);
160155

161156
if (op_type == "Add") {

onnxruntime/core/providers/coreml/builders/impl/builder_utils.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ void CreateCoreMLWeight(CoreML::Specification::WeightParams& weight, gsl::span<c
150150
CreateCoreMLWeightConvertingDataToFloats(weight, data);
151151
}
152152

153-
#if defined(COREML_ENABLE_MLPROGRAM)
154153
//
155154
// ML Program Utils
156155
//
@@ -448,6 +447,5 @@ void AddPadTypeAndPads(COREML_SPEC::MILSpec::Operation& op, ModelBuilder& model_
448447
}
449448
}
450449
}
451-
#endif // defined(COREML_ENABLE_MLPROGRAM)
452450
} // namespace coreml
453451
} // namespace onnxruntime

onnxruntime/core/providers/coreml/builders/impl/builder_utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ void CreateCoreMLWeight(CoreML::Specification::WeightParams& weight, gsl::span<c
5050
// Copy the int64_t array to a coreml weight
5151
void CreateCoreMLWeight(CoreML::Specification::WeightParams& weight, gsl::span<const int64_t> data);
5252

53-
#if defined(COREML_ENABLE_MLPROGRAM)
5453
//
5554
// MLProgram utils
5655
//
@@ -174,6 +173,5 @@ void AddOperationOutput(COREML_SPEC::MILSpec::Operation& op, const NodeArg& outp
174173
/// <param name="num_spatial_dims">Number of spatial dims in input. Generally rank - 2 (ignore N and C dims).</param>
175174
void AddPadTypeAndPads(COREML_SPEC::MILSpec::Operation& op, ModelBuilder& model_builder, std::string_view op_type,
176175
const NodeAttrHelper& helper, int num_spatial_dims);
177-
#endif // defined(COREML_ENABLE_MLPROGRAM)
178176
} // namespace coreml
179177
} // namespace onnxruntime

onnxruntime/core/providers/coreml/builders/impl/cast_op_builder.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ class CastOpBuilder : public BaseOpBuilder {
2727
Status CastOpBuilder::AddToModelBuilderImpl([[maybe_unused]] ModelBuilder& model_builder,
2828
[[maybe_unused]] const Node& node,
2929
[[maybe_unused]] const logging::Logger& logger) const {
30-
// This is a special handling case for ArgMax Op, where argmax is followed by a cast to int32 type.
31-
// The ArgMax is fused with the Cast node and produces an int32 output.
32-
#if defined(COREML_ENABLE_MLPROGRAM)
30+
// This is a special handling case for ArgMax Op, where argmax is followed by a cast to int32 type.
31+
// The ArgMax is fused with the Cast node and produces an int32 output.
3332
if (model_builder.CreateMLProgram()) {
3433
using namespace CoreML::Specification::MILSpec;
3534
// https://apple.github.io/coremltools/source/coremltools.converters.mil.mil.ops.defs.html#coremltools.converters.mil.mil.ops.defs.iOS15.elementwise_unary.cast
@@ -73,7 +72,6 @@ Status CastOpBuilder::AddToModelBuilderImpl([[maybe_unused]] ModelBuilder& model
7372
AddOperationOutput(*op, *node.OutputDefs()[0], cast_to_type);
7473
model_builder.AddOperation(std::move(op));
7574
}
76-
#endif
7775

7876
return Status::OK();
7977
}
@@ -134,7 +132,6 @@ bool CastOpBuilder::HasSupportedInputsImpl(const Node& node, [[maybe_unused]] co
134132
return false;
135133
}
136134

137-
#if defined(COREML_ENABLE_MLPROGRAM)
138135
if (input_params.create_mlprogram) {
139136
if ((input_type == ONNX_NAMESPACE::TensorProto_DataType_INT32 ||
140137
input_type == ONNX_NAMESPACE::TensorProto_DataType_INT64 ||
@@ -152,7 +149,6 @@ bool CastOpBuilder::HasSupportedInputsImpl(const Node& node, [[maybe_unused]] co
152149
return false;
153150
}
154151
}
155-
#endif
156152

157153
// only support int64 coming from ArgMax (check for ArgMax is done in IsOpSupportedImpl())
158154
if (input_type != ONNX_NAMESPACE::TensorProto_DataType_INT64) {

onnxruntime/core/providers/coreml/builders/impl/clip_op_builder.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Status ClipOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
6464
bool has_min = min != std::numeric_limits<float>::lowest();
6565
bool has_max = max != std::numeric_limits<float>::max();
6666

67-
#if defined(COREML_ENABLE_MLPROGRAM)
6867
if (model_builder.CreateMLProgram()) {
6968
using namespace CoreML::Specification::MILSpec;
7069

@@ -121,9 +120,7 @@ Status ClipOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
121120

122121
AddOperationOutput(*op, output);
123122
model_builder.AddOperation(std::move(op));
124-
} else
125-
#endif // defined(COREML_ENABLE_MLPROGRAM)
126-
{
123+
} else {
127124
// TODO: CoreML has a Clip layer for NeuralNetwork. Added in CoreML 4. We could potentially use that if available
128125
// to simplify.
129126
// https://apple.github.io/coremltools/mlmodel/Format/NeuralNetwork.html#cliplayerparams

onnxruntime/core/providers/coreml/builders/impl/concat_op_builder.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ConcatOpBuilder : public BaseOpBuilder {
2626
Status ConcatOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
2727
const Node& node,
2828
const logging::Logger& logger) const {
29-
#if defined(COREML_ENABLE_MLPROGRAM)
3029
if (model_builder.CreateMLProgram()) {
3130
using namespace CoreML::Specification::MILSpec; // NOLINT
3231

@@ -45,7 +44,6 @@ Status ConcatOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder,
4544
AddOperationOutput(*op, *node.OutputDefs()[0]);
4645
model_builder.AddOperation(std::move(op));
4746
} else // NOLINT
48-
#endif // defined(COREML_ENABLE_MLPROGRAM)
4947
{
5048
std::unique_ptr<COREML_SPEC::NeuralNetworkLayer> layer = model_builder.CreateNNLayer(node);
5149

onnxruntime/core/providers/coreml/builders/impl/conv_op_builder.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
5252

5353
NodeAttrHelper helper(node);
5454

55-
#if defined(COREML_ENABLE_MLPROGRAM)
5655
if (model_builder.CreateMLProgram()) {
5756
using namespace CoreML::Specification::MILSpec;
5857

@@ -89,9 +88,7 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
8988
AddOperationOutput(*conv_op, *node.OutputDefs()[0]);
9089

9190
model_builder.AddOperation(std::move(conv_op));
92-
} else
93-
#endif // defined(COREML_ENABLE_MLPROGRAM)
94-
{
91+
} else {
9592
std::unique_ptr<COREML_SPEC::NeuralNetworkLayer> layer = model_builder.CreateNNLayer(node);
9693

9794
auto strides = helper.Get("strides", std::vector<int64_t>{1, 1});
@@ -225,14 +222,11 @@ bool ConvOpBuilder::IsOpSupportedImpl(const Node& node, const OpBuilderInputPara
225222
const auto& weight_name = input_defs[1]->Name();
226223
const auto* weight = input_params.graph_viewer.GetConstantInitializer(weight_name);
227224

228-
#if defined(COREML_ENABLE_MLPROGRAM)
229225
if (input_params.create_mlprogram) {
230226
// ML Program supports non-const weight, 1D, 2D and 3D.
231227
// keep to 1D and 2D for consistency with the NeuralNetwork implementation for now.
232228
// add 3D support as/when needed.
233-
} else
234-
#endif // defined (COREML_ENABLE_MLPROGRAM)
235-
{
229+
} else {
236230
if (!weight) {
237231
LOGS(logger, VERBOSE) << "The weight of Conv [" << name << "] must be a constant initializer";
238232
return false;
@@ -257,7 +251,6 @@ bool ConvOpBuilder::IsOpSupportedImpl(const Node& node, const OpBuilderInputPara
257251

258252
NodeAttrHelper helper(node);
259253

260-
#if defined(COREML_ENABLE_MLPROGRAM)
261254
// spec says same_lower is supported in CoreML 5. it lies. CoreML 6 is required otherwise you get
262255
// `Unexpected value for parameter pad_type[0] "same_lower" not in ("custom", "same", "valid").`
263256
// We _could_ manually calculate the pads, but not implementing that until we have a real use case to justify
@@ -269,7 +262,6 @@ bool ConvOpBuilder::IsOpSupportedImpl(const Node& node, const OpBuilderInputPara
269262
return false;
270263
}
271264
}
272-
#endif
273265

274266
// there's no equivalent to allow a manual kernel shape in CoreML.
275267
// it's OK if a specified kernel_shape matches kH and kW dims of the weight input.

0 commit comments

Comments
 (0)