onnxruntime
149d2089 - Support Optional shapes (#26164)

Commit
125 days ago
Support Optional shapes (#26164) This pull request introduces a new mechanism for handling tensor shape information in ONNX Runtime, improving the representation and querying of shape and symbolic dimension data. The changes add support for tensors that may not have shape information, update APIs to reflect this, and refactor internal shape handling logic for improved consistency and extensibility. ### API and Core Logic Improvements * Added a new API method `TensorTypeAndShape_HasShape` to `OrtApi` and corresponding C++ wrapper `HasShape()` in `TensorTypeAndShapeInfoImpl`, allowing users to check if a tensor has shape information. [[1]](diffhunk://#diff-5845a5c76fb64abdc8f0cffe21b37f8da1712674eb3abc4cd87190891be1bd48R6583-R6590) [[2]](diffhunk://#diff-17f64e8b38fcdcd25e90abcabeec4b420956b15fe63868a5d0b270c376bde209R1785) [[3]](diffhunk://#diff-cc93f5f9d8078d3d3af14c9bb4c0c59e25a99f3ec75d7772ea20111ed7eb6ddeL1985-R1990) * Updated all shape-related API implementations (e.g., `GetDimensionsCount`, `GetDimensions`, `GetSymbolicDimensions`, `GetTensorShapeElementCount`) to properly handle the case when a tensor does not have shape information, returning zero or empty values as appropriate. ### Refactoring and Consistency * Refactored `OrtTensorTypeAndShapeInfo` to store shape information in an optional structure, and updated all related methods to use pointers instead of direct objects, improving consistency and future extensibility. [[1]](diffhunk://#diff-4756b499067c0b1f266a64e07fcdc7e994fef5cef7f3bd68fe413d673cac7477L223-R282) [[2]](diffhunk://#diff-e5038e85df9dca9aed186026cb784ccd367130f37ac046ffbc75958be81d343aR6) * Updated methods for constructing shape/type info to accept pointers and handle cases where shape may be absent, including changes in `onnxruntime_typeinfo.cc` and related API calls. [[1]](diffhunk://#diff-b149047a296b19c4da5ade443da01adc5fd9f1bf3458516f5d6ebd0940e0b0fbL173-R173) [[2]](diffhunk://#diff-b149047a296b19c4da5ade443da01adc5fd9f1bf3458516f5d6ebd0940e0b0fbL184-R184) [[3]](diffhunk://#diff-b149047a296b19c4da5ade443da01adc5fd9f1bf3458516f5d6ebd0940e0b0fbL197-R197) [[4]](diffhunk://#diff-b149047a296b19c4da5ade443da01adc5fd9f1bf3458516f5d6ebd0940e0b0fbL306-R307) [[5]](diffhunk://#diff-4756b499067c0b1f266a64e07fcdc7e994fef5cef7f3bd68fe413d673cac7477L279-R320) [[6]](diffhunk://#diff-4756b499067c0b1f266a64e07fcdc7e994fef5cef7f3bd68fe413d673cac7477L305-R339) [[7]](diffhunk://#diff-4756b499067c0b1f266a64e07fcdc7e994fef5cef7f3bd68fe413d673cac7477L347-R381) ### Graph and Proto Serialization * Modified graph-to-proto conversion logic to propagate and respect the presence or absence of shape information, ensuring that ONNX graph serialization accurately reflects tensor shape status. [[1]](diffhunk://#diff-43bd1f5614c593153ace5847907605341284e6025ce60dd9ec22b1fa7434b275L228-R229) [[2]](diffhunk://#diff-43bd1f5614c593153ace5847907605341284e6025ce60dd9ec22b1fa7434b275R394-R397) [[3]](diffhunk://#diff-43bd1f5614c593153ace5847907605341284e6025ce60dd9ec22b1fa7434b275L496-R511) [[4]](diffhunk://#diff-43bd1f5614c593153ace5847907605341284e6025ce60dd9ec22b1fa7434b275R522) [[5]](diffhunk://#diff-43bd1f5614c593153ace5847907605341284e6025ce60dd9ec22b1fa7434b275R539-R551) These changes collectively improve the robustness and flexibility of ONNX Runtime's handling of tensor shape information, and lay the groundwork for supporting more dynamic and symbolic tensor shapes in future development.
Author
Parents
Loading