onnxruntime
517d684c - [EP ABI] Use pre-allocated input buffers for APIs that return arrays. (#25247)

Commit
171 days ago
[EP ABI] Use pre-allocated input buffers for APIs that return arrays. (#25247) ### Description - Remove `OrtArrayOfConstObjects` from C API - Rework graph APIs that return an array of objects to take pre-allocated buffers as input. - Rename `Node_GetParentGraph` to `Node_GetGraph` - Fixes C/C++ API documentation generation: https://github.com/microsoft/onnxruntime/actions/runs/16029991022 ### Motivation and Context Make the graph C APIs easier to use. The `OrtArrayOfConstObjects` approach was too verbose to use and made the API harder to understand because the function signatures did not show the element data types. Example usage with `OrtArrayOfConstObjects`: ```c++ const OrtGraph* graph; // Assumed is initialized OrtArrayOfConstObjects* nodes = nullptr; RETURN_IF_ERROR(ort_api.Graph_GetNodes(graph, &nodes)); // Get array size_t num_nodes = 0; RETURN_IF_ERROR(ort_api.ArrayOfConstObjects_GetSize(nodes, &num_nodes)); // Get size // Use the nodes. for (size_t i = 0; i < num_nodes; i++) { const OrtNode* node = nullptr; RETURN_IF_ERROR(ort_api.ArrayOfConstObjects_GetElementAt(nodes, i, reinterpret_cast<const void**>(&node))); // Inspect OrtNode properties ... } // Have to manually release the OrtArrayOfConstObjects // A C++ ORT wrapper class would help via RAII, but the same C api calls are made under the hood. ort_api.ReleaseArrayOfConstObjects(nodes); ``` Example usage with "pre-allocated" buffers style: ```c++ const OrtGraph* graph; // Assumed is initialized // Get number of nodes. size_t num_nodes = 0; RETURN_IF_ERROR(ort_api.Graph_GetNumNodes(graph, &num_nodes)); // Pre-allocate buffer of OrtNode* and get nodes. std::vector<const OrtNode*> nodes(num_nodes); RETURN_IF_ERROR(ort_api.Graph_GetNodes(graph, nodes.data(), nodes.size())); // Use the nodes. for (size_t i = 0; i < num_nodes; i++) { const OrtNode* node = nodes[i]; // Inspect OrtNode properties. } // std::vector destructor cleans up for us. ``` --------- Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
Parents
Loading