From 019edc926407d8ed4cc42df0203416d929a383da Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Mon, 30 Dec 2024 11:15:03 +1000 Subject: [PATCH] Fix minimal build. Fix some more old 'graph api' naming --- cmake/onnxruntime_session.cmake | 1 + .../core/session/onnxruntime_cxx_api.h | 26 ++++++++++++------- onnxruntime/core/graph/graph.cc | 20 +++++++------- onnxruntime/core/graph/model.cc | 6 ++--- onnxruntime/core/session/onnxruntime_c_api.cc | 5 ++++ .../test/shared_lib/test_model_builder_api.cc | 11 +++++++- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/cmake/onnxruntime_session.cmake b/cmake/onnxruntime_session.cmake index 47cf2dfc5e..c2fe5d23a2 100644 --- a/cmake/onnxruntime_session.cmake +++ b/cmake/onnxruntime_session.cmake @@ -22,6 +22,7 @@ endif() if (onnxruntime_MINIMAL_BUILD) set(onnxruntime_session_src_exclude "${ONNXRUNTIME_ROOT}/core/session/provider_bridge_ort.cc" + "${ONNXRUNTIME_ROOT}/core/session/model_builder_c_api.cc" ) list(REMOVE_ITEM onnxruntime_session_srcs ${onnxruntime_session_src_exclude}) diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 6e0f0acd12..715f61b171 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -148,7 +148,15 @@ std::vector GetAvailableProviders(); /// This returns a reference to the ORT C Model Builder API. Used if building or augmenting a model at runtime. /// /// ORT C Model Builder API reference -inline const OrtModelBuilderApi& GetModelBuilderApi() noexcept { return *GetApi().GetModelBuilderApi(); } +inline const OrtModelBuilderApi& GetModelBuilderApi() { + auto* api = GetApi().GetModelBuilderApi(); + if (api == nullptr) { + // minimal build + ORT_CXX_API_THROW("Model Builder API is not available in this build", ORT_FAIL); + } + + return *api; +} /** \brief IEEE 754 half-precision floating point data type * @@ -533,14 +541,14 @@ ORT_DEFINE_RELEASE(KernelInfo); #undef ORT_DEFINE_RELEASE -#define ORT_DEFINE_GRAPH_API_RELEASE(NAME) \ +#define ORT_DEFINE_MODELBUILDER_API_RELEASE(NAME) \ inline void OrtRelease(Ort##NAME* ptr) { GetModelBuilderApi().Release##NAME(ptr); } -ORT_DEFINE_GRAPH_API_RELEASE(ValueInfo); -ORT_DEFINE_GRAPH_API_RELEASE(Node); -ORT_DEFINE_GRAPH_API_RELEASE(Graph); -ORT_DEFINE_GRAPH_API_RELEASE(Model); -#undef ORT_DEFINE_GRAPH_API_RELEASE +ORT_DEFINE_MODELBUILDER_API_RELEASE(ValueInfo); +ORT_DEFINE_MODELBUILDER_API_RELEASE(Node); +ORT_DEFINE_MODELBUILDER_API_RELEASE(Graph); +ORT_DEFINE_MODELBUILDER_API_RELEASE(Model); +#undef ORT_DEFINE_MODELBUILDER_API_RELEASE /** \brief This is a tagging template type. Use it with Base to indicate that the C++ interface object * has no ownership of the underlying C object. @@ -1283,9 +1291,9 @@ struct TensorTypeAndShapeInfoImpl : Base { [[deprecated("use GetShape()")]] void GetDimensions(int64_t* values, size_t values_count) const; ///< Wraps OrtApi::GetDimensions void GetSymbolicDimensions(const char** values, size_t values_count) const; ///< Wraps OrtApi::GetSymbolicDimensions + std::vector GetSymbolicDimensions() const; std::vector GetShape() const; ///< Uses GetDimensionsCount & GetDimensions to return a std::vector of the shape - std::vector GetSymbolicDimensions() const; }; } // namespace detail @@ -2544,7 +2552,7 @@ struct CustomOpBase : OrtCustomOp { }; // -// Graph API C++ wrappers +// Model Builder API C++ wrappers // namespace ModelBuilderAPI { diff --git a/onnxruntime/core/graph/graph.cc b/onnxruntime/core/graph/graph.cc index 9b7dfb5071..8d1becdb24 100644 --- a/onnxruntime/core/graph/graph.cc +++ b/onnxruntime/core/graph/graph.cc @@ -3637,16 +3637,6 @@ Status Graph::InjectExternalInitializersFromFilesInMemory( return Status::OK(); } -bool Graph::GetOrtValueInitializer(const std::string& name, OrtValue& value) const { - auto it = ortvalue_initializers_.find(name); - if (it == ortvalue_initializers_.end()) { - return false; - } - - value = it->second; - return true; -} - #endif // DISABLE_EXTERNAL_INITIALIZERS #endif // !defined(ORT_MINIMAL_BUILD) @@ -3660,6 +3650,16 @@ bool Graph::GetInitializedTensor(const std::string& tensor_name, const TensorPro return true; } +bool Graph::GetOrtValueInitializer(const std::string& name, OrtValue& value) const { + auto it = ortvalue_initializers_.find(name); + if (it == ortvalue_initializers_.end()) { + return false; + } + + value = it->second; + return true; +} + void Graph::CleanAllInitializedTensors() noexcept { name_to_initial_tensor_.clear(); #if !defined(DISABLE_SPARSE_TENSORS) diff --git a/onnxruntime/core/graph/model.cc b/onnxruntime/core/graph/model.cc index 95773db942..01ef75af40 100644 --- a/onnxruntime/core/graph/model.cc +++ b/onnxruntime/core/graph/model.cc @@ -740,7 +740,7 @@ Status Model::Load(int fd, const PathString& model_path, std::shared_ptr& } // static -common::Status Model::LoadFromModelBuilderApiModel(const OrtModel& graph_api_model, +common::Status Model::LoadFromModelBuilderApiModel(const OrtModel& model_builder_api_model, const IOnnxRuntimeOpSchemaRegistryList* local_registries, const ModelOptions& options, const logging::Logger& logger, @@ -758,9 +758,9 @@ common::Status Model::LoadFromModelBuilderApiModel(const OrtModel& graph_api_mod } } - ORT_RETURN_IF_ERROR(Graph::LoadFromModelBuilderApiModel(*graph_api_model.graph, + ORT_RETURN_IF_ERROR(Graph::LoadFromModelBuilderApiModel(*model_builder_api_model.graph, *model, - graph_api_model.domain_to_version, + model_builder_api_model.domain_to_version, schema_registry, options.strict_shape_type_inference, logger, diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index a05309f9ec..706ddadb44 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -2385,7 +2385,12 @@ ORT_API(const OrtTrainingApi*, OrtApis::GetTrainingApi, uint32_t version) { } ORT_API(const OrtModelBuilderApi*, OrtApis::GetModelBuilderApi) { +#if !defined(ORT_MINIMAL_BUILD) return OrtModelBuilderAPI::GetModelBuilderApi(); +#else + fprintf(stderr, "The Model Builder API is not supported in a minimal build.\n"); + return nullptr; +#endif } static constexpr OrtApiBase ort_api_base = { diff --git a/onnxruntime/test/shared_lib/test_model_builder_api.cc b/onnxruntime/test/shared_lib/test_model_builder_api.cc index 48d5c91f7a..18b3e285e6 100644 --- a/onnxruntime/test/shared_lib/test_model_builder_api.cc +++ b/onnxruntime/test/shared_lib/test_model_builder_api.cc @@ -468,12 +468,21 @@ TEST(ModelBuilderAPITest, InvalidDimension) { input_dims); // invalid dim of -2 should cause exception TypeInfo::CreateTensorInfo(tensor_type_info.GetConst()); - FAIL(); + FAIL() << "Expected exception for invalid dimension"; } catch (const Ort::Exception& e) { ASSERT_STREQ(e.what(), "dim_values must be -1 (symbolic dimension) or larger."); } } +TEST(ModelBuilderAPITest, TestSqueezenet) { + static constexpr PATH_TYPE MODEL_URI = TSTR("testdata/squeezenet/model.onnx"); + + SessionOptions so; + Ort::Session session(*ort_env, MODEL_URI, so); + + auto in0 = session.GetInputTypeInfo(0); +} + /* Tests required