Skip to content

Commit

Permalink
[Apple framework] Fix minimal build with training enabled. (#19858)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
edgchen1 authored and rachguo committed Mar 21, 2024
1 parent ee6d01c commit 24e83fa
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 26 deletions.
53 changes: 30 additions & 23 deletions cmake/onnxruntime_graph.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ file(GLOB_RECURSE onnxruntime_graph_src CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/graph/*.cc"
)

# create empty list for any excludes
# start with empty training srcs list
set(orttraining_graph_src)

if (onnxruntime_ENABLE_TRAINING_OPS AND NOT onnxruntime_ENABLE_TRAINING)
set(orttraining_graph_src
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
)
endif()

if (onnxruntime_ENABLE_TRAINING)
file(GLOB_RECURSE orttraining_graph_src CONFIGURE_DEPENDS
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.h"
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.cc"
)
endif()

# create empty lists for any excludes
set(onnxruntime_graph_src_exclude_patterns)
set(orttraining_graph_src_exclude_patterns)

if (onnxruntime_MINIMAL_BUILD)
# remove schema registration support
Expand All @@ -22,11 +40,18 @@ if (onnxruntime_MINIMAL_BUILD)
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/onnx_function_util.cc"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.h"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.cc"
"${ONNXRUNTIME_ROOT}/core/graph/dml_ops/dml_defs.h"
"${ONNXRUNTIME_ROOT}/core/graph/dml_ops/dml_defs.cc"
"${ONNXRUNTIME_ROOT}/core/graph/function_template.h"
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.h"
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.cc"
)

list(APPEND orttraining_graph_src_exclude_patterns
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
)

# no Function support initially
list(APPEND onnxruntime_graph_src_exclude_patterns
"${ONNXRUNTIME_ROOT}/core/graph/function*"
Expand Down Expand Up @@ -64,30 +89,12 @@ endif()
file(GLOB onnxruntime_graph_src_exclude ${onnxruntime_graph_src_exclude_patterns})
list(REMOVE_ITEM onnxruntime_graph_src ${onnxruntime_graph_src_exclude})

file(GLOB_RECURSE onnxruntime_ir_defs_src CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/defs/*.cc"
)

if (onnxruntime_ENABLE_TRAINING_OPS AND NOT onnxruntime_ENABLE_TRAINING)
set(orttraining_graph_src
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.cc"
"${ORTTRAINING_SOURCE_DIR}/core/graph/training_op_defs.h"
)
endif()

if (onnxruntime_ENABLE_TRAINING)
file(GLOB_RECURSE orttraining_graph_src CONFIGURE_DEPENDS
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.h"
"${ORTTRAINING_SOURCE_DIR}/core/graph/*.cc"
)
endif()

set(onnxruntime_graph_lib_src ${onnxruntime_graph_src} ${onnxruntime_ir_defs_src})
if (onnxruntime_ENABLE_TRAINING_OPS)
list(APPEND onnxruntime_graph_lib_src ${orttraining_graph_src})
file(GLOB orttraining_graph_src_exclude ${orttraining_graph_src_exclude_patterns})
list(REMOVE_ITEM orttraining_graph_src ${orttraining_graph_src_exclude})
endif()

onnxruntime_add_static_library(onnxruntime_graph ${onnxruntime_graph_lib_src})
onnxruntime_add_static_library(onnxruntime_graph ${onnxruntime_graph_src} ${orttraining_graph_src})
add_dependencies(onnxruntime_graph onnx_proto flatbuffers::flatbuffers)
onnxruntime_add_include_to_target(onnxruntime_graph onnxruntime_common ${WIL_TARGET} onnx onnx_proto ${PROTOBUF_LIB} flatbuffers::flatbuffers safeint_interface Boost::mp11)

Expand Down Expand Up @@ -120,7 +127,7 @@ endif()

set_target_properties(onnxruntime_graph PROPERTIES FOLDER "ONNXRuntime")
set_target_properties(onnxruntime_graph PROPERTIES LINKER_LANGUAGE CXX)
source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_graph_src} ${onnxruntime_ir_defs_src})
source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_graph_src})
if (onnxruntime_ENABLE_TRAINING_OPS)
source_group(TREE ${ORTTRAINING_ROOT} FILES ${orttraining_graph_src})
endif()
Expand Down
4 changes: 4 additions & 0 deletions onnxruntime/core/providers/cpu/ml/tree_ensemble_helper.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#if !defined(ORT_MINIMAL_BUILD)

#include "core/providers/cpu/ml/tree_ensemble_helper.h"
#include "core/common/common.h"
#include "onnx/defs/tensor_proto_util.h"
Expand Down Expand Up @@ -64,3 +66,5 @@ Status GetVectorAttrsOrDefault(const OpKernelInfo& info, const std::string& name

} // namespace ml
} // namespace onnxruntime

#endif // !defined(ORT_MINIMAL_BUILD)
5 changes: 5 additions & 0 deletions onnxruntime/core/providers/cpu/ml/tree_ensemble_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Licensed under the MIT License.

#pragma once

#if !defined(ORT_MINIMAL_BUILD)

#include "core/common/common.h"
#include "core/framework/op_kernel.h"

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

} // namespace ml
} // namespace onnxruntime

#endif // !defined(ORT_MINIMAL_BUILD)
2 changes: 0 additions & 2 deletions onnxruntime/core/session/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,10 @@ Status Environment::Initialize(std::unique_ptr<logging::LoggingManager> logging_
// Register contributed schemas.
// The corresponding kernels are registered inside the appropriate execution provider.
#ifndef DISABLE_CONTRIB_OPS
#ifndef ORT_MINIMAL_BUILD
RegisterOpSetSchema<contrib::OpSet_Microsoft_ver1>();
RegisterOpSetSchema<contrib::OpSet_ONNX_Deprecated>();
// internal opset that has NHWC versions of ONNX operators
RegisterOpSetSchema<internal_nhwc_onnx::OpSet_Internal_NHWC_ONNX>();
#endif
contrib::RegisterContribSchemas();
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"build_osx_archs": {
"iphonesimulator": [
"x86_64"
]
},
"build_params": {
"base": [
"--parallel",
"--use_xcode",
"--build_apple_framework",
"--minimal_build=extended",
"--enable_training_apis",
"--skip_tests",
"--cmake_extra_defines=onnxruntime_BUILD_UNIT_TESTS=OFF"
],
"iphonesimulator": [
"--ios",
"--apple_deploy_target=12.0"
]
}
}
40 changes: 39 additions & 1 deletion tools/ci_build/github/azure-pipelines/post-merge-jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ stages:
- template: templates/use-xcode-version.yml
parameters:
xcodeVersion: 14.3

- script: |
pip install -r tools/ci_build/github/apple/ios_packaging.requirements.txt
displayName: "Install Python requirements"
Expand All @@ -478,4 +479,41 @@ stages:
--framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
--c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
--variant Mobile
displayName: "Test pod with iOS dynamic framework"
displayName: "Test pod with iOS framework"
- stage: IosMinimalTrainingBuild
dependsOn: []
jobs:
- job: IosMinimalTrainingBuild
timeoutInMinutes: 120
pool:
vmImage: "macOS-13"

steps:
- task: UsePythonVersion@0
inputs:
versionSpec: "3.9"
addToPath: true
architecture: "x64"

- template: templates/use-xcode-version.yml
parameters:
xcodeVersion: 14.3

- script: |
pip install -r tools/ci_build/github/apple/ios_packaging.requirements.txt
displayName: "Install Python requirements"
- script: |
python tools/ci_build/github/apple/build_apple_framework.py \
--build_dir "$(Build.BinariesDirectory)/ios_framework" \
tools/ci_build/github/apple/test_minimal_training_ios_simulator_framework_build_settings.json
displayName: "Build iOS framework with minimal build and training enabled"
- script: |
python tools/ci_build/github/apple/test_apple_packages.py \
--framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
--c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
--variant Training \
--skip_macos_test
displayName: "Test pod with iOS framework"

0 comments on commit 24e83fa

Please sign in to comment.