Skip to content

Commit ef60e38

Browse files
authored
[CXX] Introduce C++ API for new C entry points (microsoft#25897)
### Description <!-- Describe your changes. --> Create or augment existing C++ API for new entry points ### 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. --> Enable exception safe coding in C++ codebase.
1 parent 45ffd99 commit ef60e38

File tree

11 files changed

+1646
-1271
lines changed

11 files changed

+1646
-1271
lines changed

include/onnxruntime/core/providers/utils/ort_graph_to_proto.h

Lines changed: 366 additions & 516 deletions
Large diffs are not rendered by default.

include/onnxruntime/core/session/onnxruntime_cxx_api.h

Lines changed: 230 additions & 43 deletions
Large diffs are not rendered by default.

include/onnxruntime/core/session/onnxruntime_cxx_inline.h

Lines changed: 597 additions & 133 deletions
Large diffs are not rendered by default.

onnxruntime/test/autoep/library/ep.cc

Lines changed: 262 additions & 267 deletions
Large diffs are not rendered by default.

onnxruntime/test/autoep/library/ep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ExampleEp : public OrtEp, public ApiPtrs {
5454
OrtStatus* CreateEpContextNodes(gsl::span<const OrtNode*> fused_nodes,
5555
/*out*/ gsl::span<OrtNode*> ep_context_nodes);
5656

57-
OrtStatus* ExampleEp::SaveConstantInitializers(const OrtGraph* graph);
57+
OrtStatus* SaveConstantInitializers(const OrtGraph* graph);
5858

5959
ExampleEpFactory& factory_;
6060
std::string name_;

onnxruntime/test/autoep/library/example_plugin_ep.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
#define ORT_API_MANUAL_INIT
5+
#include "onnxruntime_cxx_api.h"
6+
#undef ORT_API_MANUAL_INIT
7+
48
#include "ep_factory.h"
59

610
// To make symbols visible on macOS/iOS
@@ -21,6 +25,9 @@ EXPORT_SYMBOL OrtStatus* CreateEpFactories(const char* registration_name, const
2125
const OrtEpApi* ep_api = ort_api->GetEpApi();
2226
const OrtModelEditorApi* model_editor_api = ort_api->GetModelEditorApi();
2327

28+
// Manual init for the C++ API
29+
Ort::InitApi(ort_api);
30+
2431
// Factory could use registration_name or define its own EP name.
2532
std::unique_ptr<OrtEpFactory> factory = std::make_unique<ExampleEpFactory>(registration_name,
2633
ApiPtrs{*ort_api, *ep_api,

onnxruntime/test/autoep/library/example_plugin_ep_utils.cc

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,33 @@
55

66
#include <string>
77

8-
OrtStatus* GetSessionConfigEntryOrDefault(const OrtApi& ort_api, const OrtSessionOptions& session_options,
8+
OrtStatus* GetSessionConfigEntryOrDefault(const OrtApi& /* ort_api */, const OrtSessionOptions& session_options,
99
const char* config_key, const std::string& default_val,
1010
/*out*/ std::string& config_val) {
11-
int has_config = 0;
12-
RETURN_IF_ERROR(ort_api.HasSessionConfigEntry(&session_options, config_key, &has_config));
13-
14-
if (has_config != 1) {
15-
config_val = default_val;
16-
return nullptr;
11+
try {
12+
Ort::ConstSessionOptions sess_opt{&session_options};
13+
config_val = sess_opt.GetConfigEntryOrDefault(config_key, default_val);
14+
} catch (const Ort::Exception& ex) {
15+
Ort::Status status(ex);
16+
return status.release();
1717
}
1818

19-
size_t size = 0;
20-
RETURN_IF_ERROR(ort_api.GetSessionConfigEntry(&session_options, config_key, nullptr, &size));
21-
22-
config_val.resize(size);
23-
RETURN_IF_ERROR(ort_api.GetSessionConfigEntry(&session_options, config_key, config_val.data(), &size));
24-
config_val.resize(size - 1); // remove the terminating '\0'
25-
2619
return nullptr;
2720
}
2821

29-
OrtStatus* IsFloatTensor(const OrtApi& ort_api, const OrtValueInfo* value_info, bool& result) {
22+
void IsFloatTensor(Ort::ConstValueInfo value_info, bool& result) {
3023
result = false;
3124

32-
const OrtTypeInfo* type_info = nullptr;
33-
RETURN_IF_ERROR(ort_api.GetValueInfoTypeInfo(value_info, &type_info));
34-
35-
ONNXType onnx_type = ONNX_TYPE_UNKNOWN;
36-
RETURN_IF_ERROR(ort_api.GetOnnxTypeFromTypeInfo(type_info, &onnx_type));
25+
auto type_info = value_info.TypeInfo();
26+
ONNXType onnx_type = type_info.GetONNXType();
3727
if (onnx_type != ONNX_TYPE_TENSOR) {
38-
return nullptr;
28+
return;
3929
}
4030

41-
const OrtTensorTypeAndShapeInfo* type_shape = nullptr;
42-
RETURN_IF_ERROR(ort_api.CastTypeInfoToTensorInfo(type_info, &type_shape));
43-
44-
ONNXTensorElementDataType elem_type = ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
45-
RETURN_IF_ERROR(ort_api.GetTensorElementType(type_shape, &elem_type));
31+
auto type_shape = type_info.GetTensorTypeAndShapeInfo();
32+
ONNXTensorElementDataType elem_type = type_shape.GetElementType();
4633
if (elem_type != ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) {
47-
return nullptr;
34+
return;
4835
}
49-
5036
result = true;
51-
return nullptr;
5237
}

onnxruntime/test/autoep/library/example_plugin_ep_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ OrtStatus* GetSessionConfigEntryOrDefault(const OrtApi& ort_api, const OrtSessio
107107
/*out*/ std::string& config_val);
108108

109109
// Returns true (via output parameter) if the given OrtValueInfo represents a float tensor.
110-
OrtStatus* IsFloatTensor(const OrtApi& ort_api, const OrtValueInfo* value_info, bool& result);
110+
void IsFloatTensor(Ort::ConstValueInfo value_info, bool& result);

0 commit comments

Comments
 (0)