Skip to content

Commit 860eb76

Browse files
authored
[Apple framework] Fix minimal build with training enabled. (#19858)
Fix some linker errors that come up when integrating the onnxruntime-training-c pod into another Xcode project. The problematic configuration is a minimal build with training APIs enabled. - training_op_defs.o had some unresolved references to ONNX functions. It should not be included at all in a minimal build. - tree_ensemble_helper.o also had unresolved references to ONNX ParseData. The containing function is unused in a minimal build. Added a test to cover this configuration.
1 parent 00c3cd4 commit 860eb76

File tree

6 files changed

+100
-26
lines changed

6 files changed

+100
-26
lines changed

cmake/onnxruntime_graph.cmake

+30-23
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@ file(GLOB_RECURSE onnxruntime_graph_src CONFIGURE_DEPENDS
77
"${ONNXRUNTIME_ROOT}/core/graph/*.cc"
88
)
99

10-
# create empty list for any excludes
10+
# start with empty training srcs list
11+
set(orttraining_graph_src)
12+
13+
if (onnxruntime_ENABLE_TRAINING_OPS AND NOT onnxruntime_ENABLE_TRAINING)
14+
set(orttraining_graph_src
15+
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
16+
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
17+
)
18+
endif()
19+
20+
if (onnxruntime_ENABLE_TRAINING)
21+
file(GLOB_RECURSE orttraining_graph_src CONFIGURE_DEPENDS
22+
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.h"
23+
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.cc"
24+
)
25+
endif()
26+
27+
# create empty lists for any excludes
1128
set(onnxruntime_graph_src_exclude_patterns)
29+
set(orttraining_graph_src_exclude_patterns)
1230

1331
if (onnxruntime_MINIMAL_BUILD)
1432
# remove schema registration support
@@ -22,11 +40,18 @@ if (onnxruntime_MINIMAL_BUILD)
2240
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/onnx_function_util.cc"
2341
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.h"
2442
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.cc"
43+
"${ONNXRUNTIME_ROOT}/core/graph/dml_ops/dml_defs.h"
44+
"${ONNXRUNTIME_ROOT}/core/graph/dml_ops/dml_defs.cc"
2545
"${ONNXRUNTIME_ROOT}/core/graph/function_template.h"
2646
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.h"
2747
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.cc"
2848
)
2949

50+
list(APPEND orttraining_graph_src_exclude_patterns
51+
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
52+
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
53+
)
54+
3055
# no Function support initially
3156
list(APPEND onnxruntime_graph_src_exclude_patterns
3257
"${ONNXRUNTIME_ROOT}/core/graph/function*"
@@ -64,30 +89,12 @@ endif()
6489
file(GLOB onnxruntime_graph_src_exclude ${onnxruntime_graph_src_exclude_patterns})
6590
list(REMOVE_ITEM onnxruntime_graph_src ${onnxruntime_graph_src_exclude})
6691

67-
file(GLOB_RECURSE onnxruntime_ir_defs_src CONFIGURE_DEPENDS
68-
"${ONNXRUNTIME_ROOT}/core/defs/*.cc"
69-
)
70-
71-
if (onnxruntime_ENABLE_TRAINING_OPS AND NOT onnxruntime_ENABLE_TRAINING)
72-
set(orttraining_graph_src
73-
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
74-
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
75-
)
76-
endif()
77-
78-
if (onnxruntime_ENABLE_TRAINING)
79-
file(GLOB_RECURSE orttraining_graph_src CONFIGURE_DEPENDS
80-
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.h"
81-
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.cc"
82-
)
83-
endif()
84-
85-
set(onnxruntime_graph_lib_src ${onnxruntime_graph_src} ${onnxruntime_ir_defs_src})
8692
if (onnxruntime_ENABLE_TRAINING_OPS)
87-
list(APPEND onnxruntime_graph_lib_src ${orttraining_graph_src})
93+
file(GLOB orttraining_graph_src_exclude ${orttraining_graph_src_exclude_patterns})
94+
list(REMOVE_ITEM orttraining_graph_src ${orttraining_graph_src_exclude})
8895
endif()
8996

90-
onnxruntime_add_static_library(onnxruntime_graph ${onnxruntime_graph_lib_src})
97+
onnxruntime_add_static_library(onnxruntime_graph ${onnxruntime_graph_src} ${orttraining_graph_src})
9198
add_dependencies(onnxruntime_graph onnx_proto flatbuffers::flatbuffers)
9299
onnxruntime_add_include_to_target(onnxruntime_graph onnxruntime_common ${WIL_TARGET} onnx onnx_proto ${PROTOBUF_LIB} flatbuffers::flatbuffers safeint_interface Boost::mp11)
93100

@@ -120,7 +127,7 @@ endif()
120127

121128
set_target_properties(onnxruntime_graph PROPERTIES FOLDER "ONNXRuntime")
122129
set_target_properties(onnxruntime_graph PROPERTIES LINKER_LANGUAGE CXX)
123-
source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_graph_src} ${onnxruntime_ir_defs_src})
130+
source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_graph_src})
124131
if (onnxruntime_ENABLE_TRAINING_OPS)
125132
source_group(TREE ${ORTTRAINING_ROOT} FILES ${orttraining_graph_src})
126133
endif()

onnxruntime/core/providers/cpu/ml/tree_ensemble_helper.cc

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
#if !defined(ORT_MINIMAL_BUILD)
5+
46
#include "core/providers/cpu/ml/tree_ensemble_helper.h"
57
#include "core/common/common.h"
68
#include "onnx/defs/tensor_proto_util.h"
@@ -64,3 +66,5 @@ Status GetVectorAttrsOrDefault(const OpKernelInfo& info, const std::string& name
6466

6567
} // namespace ml
6668
} // namespace onnxruntime
69+
70+
#endif // !defined(ORT_MINIMAL_BUILD)

onnxruntime/core/providers/cpu/ml/tree_ensemble_helper.h

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Licensed under the MIT License.
33

44
#pragma once
5+
6+
#if !defined(ORT_MINIMAL_BUILD)
7+
58
#include "core/common/common.h"
69
#include "core/framework/op_kernel.h"
710

@@ -13,3 +16,5 @@ Status GetVectorAttrsOrDefault(const OpKernelInfo& info, const std::string& name
1316

1417
} // namespace ml
1518
} // namespace onnxruntime
19+
20+
#endif // !defined(ORT_MINIMAL_BUILD)

onnxruntime/core/session/environment.cc

-2
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,10 @@ Status Environment::Initialize(std::unique_ptr<logging::LoggingManager> logging_
240240
// Register contributed schemas.
241241
// The corresponding kernels are registered inside the appropriate execution provider.
242242
#ifndef DISABLE_CONTRIB_OPS
243-
#ifndef ORT_MINIMAL_BUILD
244243
RegisterOpSetSchema<contrib::OpSet_Microsoft_ver1>();
245244
RegisterOpSetSchema<contrib::OpSet_ONNX_Deprecated>();
246245
// internal opset that has NHWC versions of ONNX operators
247246
RegisterOpSetSchema<internal_nhwc_onnx::OpSet_Internal_NHWC_ONNX>();
248-
#endif
249247
contrib::RegisterContribSchemas();
250248
#endif
251249

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"build_osx_archs": {
3+
"iphonesimulator": [
4+
"x86_64"
5+
]
6+
},
7+
"build_params": {
8+
"base": [
9+
"--parallel",
10+
"--use_xcode",
11+
"--build_apple_framework",
12+
"--minimal_build=extended",
13+
"--enable_training_apis",
14+
"--skip_tests",
15+
"--cmake_extra_defines=onnxruntime_BUILD_UNIT_TESTS=OFF"
16+
],
17+
"iphonesimulator": [
18+
"--ios",
19+
"--apple_deploy_target=12.0"
20+
]
21+
}
22+
}

tools/ci_build/github/azure-pipelines/post-merge-jobs.yml

+39-1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ stages:
417417
- template: templates/use-xcode-version.yml
418418
parameters:
419419
xcodeVersion: 14.3
420+
420421
- script: |
421422
pip install -r tools/ci_build/github/apple/ios_packaging.requirements.txt
422423
displayName: "Install Python requirements"
@@ -433,4 +434,41 @@ stages:
433434
--framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
434435
--c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
435436
--variant Mobile
436-
displayName: "Test pod with iOS dynamic framework"
437+
displayName: "Test pod with iOS framework"
438+
439+
- stage: IosMinimalTrainingBuild
440+
dependsOn: []
441+
jobs:
442+
- job: IosMinimalTrainingBuild
443+
timeoutInMinutes: 120
444+
pool:
445+
vmImage: "macOS-13"
446+
447+
steps:
448+
- task: UsePythonVersion@0
449+
inputs:
450+
versionSpec: "3.9"
451+
addToPath: true
452+
architecture: "x64"
453+
454+
- template: templates/use-xcode-version.yml
455+
parameters:
456+
xcodeVersion: 14.3
457+
458+
- script: |
459+
pip install -r tools/ci_build/github/apple/ios_packaging.requirements.txt
460+
displayName: "Install Python requirements"
461+
462+
- script: |
463+
python tools/ci_build/github/apple/build_apple_framework.py \
464+
--build_dir "$(Build.BinariesDirectory)/ios_framework" \
465+
tools/ci_build/github/apple/test_minimal_training_ios_simulator_framework_build_settings.json
466+
displayName: "Build iOS framework with minimal build and training enabled"
467+
468+
- script: |
469+
python tools/ci_build/github/apple/test_apple_packages.py \
470+
--framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
471+
--c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
472+
--variant Training \
473+
--skip_macos_test
474+
displayName: "Test pod with iOS framework"

0 commit comments

Comments
 (0)