Skip to content

Commit 1da50f0

Browse files
authored
Add OpAttr_GetName C API for EP ABI (#25224)
### Description This PR add additional OpAttr_GetName C API for EP ABI use.
1 parent 19614f0 commit 1da50f0

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

include/onnxruntime/core/session/onnxruntime_c_api.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5917,6 +5917,17 @@ struct OrtApi {
59175917
*/
59185918
ORT_API2_STATUS(OpAttr_GetType, _In_ const OrtOpAttr* attribute, _Out_ OrtOpAttrType* type);
59195919

5920+
/** \brief Get the attribute name from an OrtOpAttr.
5921+
*
5922+
* \param[in] attribute The OrtOpAttr instance.
5923+
* \param[out] name Output parameter set to the attribute's name. The name is a null-terminated string.
5924+
*
5925+
* \snippet{doc} snippets.dox OrtStatus Return Value
5926+
*
5927+
* \since Version 1.23.
5928+
*/
5929+
ORT_API2_STATUS(OpAttr_GetName, _In_ const OrtOpAttr* attribute, _Outptr_ const char** name);
5930+
59205931
/** \brief Get the subgraphs, as OrtGraph instances, contained by the given node.
59215932
*
59225933
* Certain operator types (e.g., If and Loop) contain nested subgraphs.

onnxruntime/core/session/onnxruntime_c_api.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,20 @@ ORT_API_STATUS_IMPL(OrtApis::OpAttr_GetType, _In_ const OrtOpAttr* attribute, _O
29352935
API_IMPL_END
29362936
}
29372937

2938+
ORT_API_STATUS_IMPL(OrtApis::OpAttr_GetName, _In_ const OrtOpAttr* attribute, _Outptr_ const char** name) {
2939+
API_IMPL_BEGIN
2940+
if (name == nullptr) {
2941+
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "name argument is null");
2942+
}
2943+
if (attribute == nullptr) {
2944+
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "attribute argument is null");
2945+
}
2946+
2947+
*name = attribute->attr_proto.name().c_str();
2948+
return nullptr;
2949+
API_IMPL_END
2950+
}
2951+
29382952
ORT_API_STATUS_IMPL(OrtApis::Node_GetSubgraphs, _In_ const OrtNode* node, _Outptr_ OrtArrayOfConstObjects** subgraphs) {
29392953
API_IMPL_BEGIN
29402954
if (subgraphs == nullptr) {
@@ -3638,6 +3652,7 @@ static constexpr OrtApi ort_api_1_to_23 = {
36383652
&OrtApis::Node_GetAttributes,
36393653
&OrtApis::Node_GetAttributeByName,
36403654
&OrtApis::OpAttr_GetType,
3655+
&OrtApis::OpAttr_GetName,
36413656
&OrtApis::Node_GetSubgraphs,
36423657
&OrtApis::Node_GetParentGraph,
36433658

onnxruntime/core/session/ort_apis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ ORT_API_STATUS_IMPL(Node_GetImplicitInputs, _In_ const OrtNode* node, _Outptr_ O
665665
ORT_API_STATUS_IMPL(Node_GetAttributes, _In_ const OrtNode* node, _Outptr_ OrtArrayOfConstObjects** attrs);
666666
ORT_API_STATUS_IMPL(Node_GetAttributeByName, _In_ const OrtNode* node, _In_ const char* attribute_name, _Outptr_ const OrtOpAttr** attribute);
667667
ORT_API_STATUS_IMPL(OpAttr_GetType, _In_ const OrtOpAttr* attribute, _Out_ OrtOpAttrType* type);
668+
ORT_API_STATUS_IMPL(OpAttr_GetName, _In_ const OrtOpAttr* attribute, _Outptr_ const char** name);
668669
ORT_API_STATUS_IMPL(Node_GetSubgraphs, _In_ const OrtNode* node, _Outptr_ OrtArrayOfConstObjects** subgraphs);
669670
ORT_API_STATUS_IMPL(Node_GetParentGraph, _In_ const OrtNode* node,
670671
_Outptr_result_maybenull_ const OrtGraph** parent_graph);

onnxruntime/test/ep_graph/test_ep_graph.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ static void CheckGraphCApi(const GraphViewer& graph_viewer, const OrtGraph& api_
473473
ASSERT_ORTSTATUS_OK(ort_api.Node_GetAttributeByName(api_node, node_attr.first.c_str(), &api_node_attr));
474474
ASSERT_NE(api_node_attr, nullptr);
475475

476+
const char* api_node_attr_name = nullptr;
477+
ASSERT_ORTSTATUS_OK(ort_api.OpAttr_GetName(api_node_attr, &api_node_attr_name));
478+
ASSERT_STREQ(api_node_attr_name, node_attr.first.c_str());
479+
476480
OrtOpAttrType api_node_attr_type = OrtOpAttrType::ORT_OP_ATTR_UNDEFINED;
477481

478482
// It's possible that the type is defined in ONNX::AttributeProto_AttributeType but not in OrtOpAttrType, since the two are not in a 1:1 mapping.

0 commit comments

Comments
 (0)