-
Notifications
You must be signed in to change notification settings - Fork 255
Add RAII wrappers for ORT Model Editor API types #1953
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds RAII (Resource Acquisition Is Initialization) wrappers for ONNX Runtime Model Editor API types and refactors the graph builder code to use these wrappers for improved resource management. Additionally, it removes mutex protection from the graph session cache based on the assumption that ort-genai execution is single-threaded.
Changes:
- Added RAII wrapper structs (OrtGraph, OrtModel, OrtValueInfo, OrtNode) in onnxruntime_api.h following the existing OrtOpAttr pattern
- Removed SessionCache struct and mutex from graph_session_cache_ in generators.h, replacing it with a simple unordered_map
- Refactored graph_builder.cpp Build() function to use std::unique_ptr for automatic resource cleanup instead of manual try/catch blocks
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/models/onnxruntime_api.h | Adds RAII wrapper structs for OrtGraph, OrtModel, OrtValueInfo, and OrtNode with custom delete operators for automatic cleanup |
| src/generators.h | Removes SessionCache struct with mutex, replaces with simple unordered_map based on single-threaded execution assumption |
| src/models/graph_executor.cpp | Removes mutex include and simplifies cache access after SessionCache removal |
| src/models/graph_builder.cpp | Refactors Build() to use RAII patterns with std::unique_ptr instead of manual try/catch exception handling |
|
@baijumeswani This is the follow-up PR to resolve your remaining comments in #1895. The pipeline failures may not related with my changes. My another PR #1952 meets the similar issue after I rebase the code to latest without any code changes. However, it passed before. |
The pipeline failures are because Hugging Face released |
src/models/graph_builder.cpp
Outdated
|
|
||
| // Create graph | ||
| OrtGraph* graph_ptr = nullptr; | ||
| Ort::ThrowOnError(model_editor_api.CreateGraph(&graph_ptr)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest moving all the RAII into onnxruntime_api.h to avoid this transferring ownership like we are doing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the latest one to see if it's in the right way.
| Ort::ThrowOnError(model_editor_api.CreateValueInfo(output.name.c_str(), type_info, &value_info)); | ||
| Ort::api->ReleaseTypeInfo(type_info); | ||
| auto value_info = OrtValueInfo::Create(output.name.c_str(), tensor_info); | ||
| Ort::api->ReleaseTensorTypeAndShapeInfo(tensor_info); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this one also be made to use RAII?
| for (const auto& input : config.inputs) { | ||
| OrtTensorTypeAndShapeInfo* tensor_info = nullptr; | ||
| Ort::ThrowOnError(Ort::api->CreateTensorTypeAndShapeInfo(&tensor_info)); | ||
| Ort::ThrowOnError(Ort::api->SetTensorElementType(tensor_info, input.elem_type)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the Ort::api-> calls inside onnxruntime_inline.h and call the wrappers?
No description provided.