Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Builder API #23223

Merged
merged 20 commits into from
Feb 28, 2025
Merged

Model Builder API #23223

merged 20 commits into from
Feb 28, 2025

Conversation

skottmckay
Copy link
Contributor

@skottmckay skottmckay commented Dec 30, 2024

Description

Supports creating a model programmatically using the ORT C or C++ API.
Supports augmenting an existing model to add nodes.

Motivation and Context

Supports creating a model programmatically using the ORT C or C++ API.
Supports augmenting an existing model to add nodes.
#include "core/framework/error_code_helper.h"
#include "core/framework/execution_provider.h"
#include "core/session/abi_session_options_impl.h"
// #include "core/session/environment.h"

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
@Craigacp
Copy link
Contributor

Is it possible to save out the model from the builder via the C API? It'll be a nice alternative to building ONNX models with raw protobufs in languages which don't have a native ONNX library.

@skottmckay
Copy link
Contributor Author

Is it possible to save out the model from the builder via the C API? It'll be a nice alternative to building ONNX models with raw protobufs in languages which don't have a native ONNX library.

You can use the SessionOption that's typically used to save the optimized ONNX model.

ORT_API2_STATUS(SetOptimizedModelFilePath, _Inout_ OrtSessionOptions* options,
_In_ const ORTCHAR_T* optimized_model_filepath);

Caveat is that does not currently support saving tensors created with CreateTensorWithDataAsOrtValue or CreateTensorWithDataAndDeleterAsOrtValue, but could be updated to do so if required.

@Craigacp
Copy link
Contributor

Craigacp commented Jan 1, 2025

Is it possible to save out the model from the builder via the C API? It'll be a nice alternative to building ONNX models with raw protobufs in languages which don't have a native ONNX library.

You can use the SessionOption that's typically used to save the optimized ONNX model.

ORT_API2_STATUS(SetOptimizedModelFilePath, _Inout_ OrtSessionOptions* options,
_In_ const ORTCHAR_T* optimized_model_filepath);

Caveat is that does not currently support saving tensors created with CreateTensorWithDataAsOrtValue or CreateTensorWithDataAndDeleterAsOrtValue, but could be updated to do so if required.

Is the optimized model one which has had op fusion and other passes done so it's no longer using ONNX standard ops everywhere, or is that a different process?

@skottmckay
Copy link
Contributor Author

Is the optimized model one which has had op fusion and other passes done so it's no longer using ONNX standard ops everywhere, or is that a different process?

You can specify the optimization level. If you keep it to level 1 (GraphOptimizationLevel.ORT_ENABLE_BASIC) it will only use standard ONNX ops.

typedef enum GraphOptimizationLevel {
ORT_DISABLE_ALL = 0,
ORT_ENABLE_BASIC = 1,

/** \brief Set the optimization level to apply when loading a graph
*
* Please see https://onnxruntime.ai/docs/performance/model-optimizations/graph-optimizations.html for an in-depth explanation
* \param[in,out] options The session options object
* \param[in] graph_optimization_level The optimization level
*
* \snippet{doc} snippets.dox OrtStatus Return Value
*/
ORT_API2_STATUS(SetSessionGraphOptimizationLevel, _Inout_ OrtSessionOptions* options,
GraphOptimizationLevel graph_optimization_level);

https://onnxruntime.ai/docs/performance/model-optimizations/graph-optimizations.html

huningxin added a commit to huningxin/chromium-src that referenced this pull request Jan 2, 2025
huningxin added a commit to huningxin/chromium-src that referenced this pull request Jan 2, 2025
mingmingtasd pushed a commit to shiyi9801/chromium that referenced this pull request Jan 2, 2025
* Pass ORT_API_VERSION to `OrtApiBase::GetApi()`

Also removes the inclusion of onnx.pb.h header.

* Add third_party/onnxruntime_headers

Import https://github.com/microsoft/onnxruntime/tree/main/include

Commit is based on microsoft/onnxruntime#23223

* Use ORT Model Builder API

* Refactor scoped ORT type ptr

1. Rename to ScopedOrtTypePtr
2. Use macros
3. Introduce `operator T*()`
4. Introduce `Release()` method
5. Rename `get_ptr()` to `Get()`
6. Rename `get_pptr()` to `GetAddressOf()`

* Remove ONNX Runtime headers from third_party/microsoft_dxheaders
@huningxin
Copy link

Is it possible to save out the model from the builder via the C API? It'll be a nice alternative to building ONNX models with raw protobufs in languages which don't have a native ONNX library.

You can use the SessionOption that's typically used to save the optimized ONNX model.

ORT_API2_STATUS(SetOptimizedModelFilePath, _Inout_ OrtSessionOptions* options,
_In_ const ORTCHAR_T* optimized_model_filepath);

Caveat is that does not currently support saving tensors created with CreateTensorWithDataAsOrtValue or CreateTensorWithDataAndDeleterAsOrtValue, but could be updated to do so if required.

@skottmckay , Saving tensors created with CreateTensorWithDataAsOrtValue or CreateTensorWithDataAndDeleterAsOrtValue would be useful for debugging. Please support it if possible. Thanks!

- enforce 128 byte minimum for tensors with external data to avoid shape inferencing issues
  - update unit tests to use 128 byte initializer so external data can be tested
- support saving initializer with in-memory external data to ONNX model by copying into TensorProto's raw_data property.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Exclude model editor API funcs from minimal build. They require Graph::Resolve and therefore need a full build.
@skottmckay skottmckay marked this pull request as ready for review February 26, 2025 17:19
Cleanup some SAL annotations
Update comments.
@yuslepukhin
Copy link
Member

yuslepukhin commented Feb 26, 2025

    p_tensor = iter->second.release();

This appears to leak if DeserializeTensorProto below errors out. #Resolved


Refers to: onnxruntime/core/framework/session_state_utils.cc:405 in f66ee09. [](commit_id = f66ee09, deletion_comment = False)

yuslepukhin
yuslepukhin previously approved these changes Feb 28, 2025
Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@skottmckay skottmckay merged commit 1088a1e into main Feb 28, 2025
97 of 99 checks passed
@skottmckay skottmckay deleted the skottmckay/ModelBuilderAPI_PR branch February 28, 2025 23:09
guschmue pushed a commit that referenced this pull request Mar 6, 2025
### Description
<!-- Describe your changes. -->
Supports creating a model programmatically using the ORT C or C++ API. 
Supports augmenting an existing model to add nodes.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants