diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index 949b2a91d2..467e66e279 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -1317,6 +1317,7 @@ struct Value : detail::ValueImpl { static Value CreateTensor(const OrtMemoryInfo* info, T* p_data, size_t p_data_element_count, const int64_t* shape, size_t shape_len); /** \brief Creates a tensor with a user supplied buffer. Wraps OrtApi::CreateTensorWithDataAsOrtValue. + * * \param info Memory description of where the p_data buffer resides (CPU vs GPU etc). * \param p_data Pointer to the data buffer. * \param p_data_byte_count The number of bytes in the data buffer. @@ -1327,7 +1328,12 @@ struct Value : detail::ValueImpl { static Value CreateTensor(const OrtMemoryInfo* info, void* p_data, size_t p_data_byte_count, const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type); - /** \brief Creates a tensor using a supplied OrtAllocator. Wraps OrtApi::CreateTensorAsOrtValue. + /** \brief Creates an OrtValue with a tensor using a supplied OrtAllocator. Wraps OrtApi::CreateTensorAsOrtValue. + * This overload will allocate the buffer for the tensor according to the supplied shape and data type. + * The allocated buffer will be owned by the returned OrtValue and will be freed when the OrtValue is released. + * The input data would need to be copied into the allocated buffer. + * This API is not suitable for strings. + * * \tparam T The numeric datatype. This API is not suitable for strings. * \param allocator The allocator to use. * \param shape Pointer to the tensor shape dimensions. @@ -1336,7 +1342,12 @@ struct Value : detail::ValueImpl { template static Value CreateTensor(OrtAllocator* allocator, const int64_t* shape, size_t shape_len); - /** \brief Creates a tensor using a supplied OrtAllocator. Wraps OrtApi::CreateTensorAsOrtValue. + /** \brief Creates an OrtValue with a tensor using the supplied OrtAllocator. + * Wraps OrtApi::CreateTensorAsOrtValue. + * The allocated buffer will be owned by the returned OrtValue and will be freed when the OrtValue is released. + * The input data would need to be copied into the allocated buffer. + * This API is not suitable for strings. + * * \param allocator The allocator to use. * \param shape Pointer to the tensor shape dimensions. * \param shape_len The number of tensor shape dimensions. @@ -1344,9 +1355,33 @@ struct Value : detail::ValueImpl { */ static Value CreateTensor(OrtAllocator* allocator, const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type); - static Value CreateMap(Value& keys, Value& values); ///< Wraps OrtApi::CreateValue - static Value CreateSequence(std::vector& values); ///< Wraps OrtApi::CreateValue + /** \brief Creates an OrtValue with a Map Onnx type representation. + * The API would ref-count the supplied OrtValues and they will be released + * when the returned OrtValue is released. The caller may release keys and values after the call + * returns. + * + * \param keys an OrtValue containing a tensor with primitive data type keys. + * \param values an OrtValue that may contain a tensor. Ort currently supports only primitive data type values. + */ + static Value CreateMap(const Value& keys, const Value& values); ///< Wraps OrtApi::CreateValue + /** \brief Creates an OrtValue with a Sequence Onnx type representation. + * The API would ref-count the supplied OrtValues and they will be released + * when the returned OrtValue is released. The caller may release the values after the call + * returns. + * + * \param values a vector of OrtValues that must have the same Onnx value type. + */ + static Value CreateSequence(const std::vector& values); ///< Wraps OrtApi::CreateValue + + /** \brief Creates an OrtValue wrapping an Opaque type. + * This is used for experimental support of non-tensor types. + * + * \tparam T - the type of the value. + * \param domain - zero terminated utf-8 string. Domain of the type. + * \param type_name - zero terminated utf-8 string. Name of the type. + * \param value - the value to be wrapped. + */ template static Value CreateOpaque(const char* domain, const char* type_name, const T&); ///< Wraps OrtApi::CreateOpaqueValue diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h index b72bcd35fa..73dfd1b3a1 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_inline.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_inline.h @@ -1434,16 +1434,16 @@ inline Value Value::CreateSparseTensor(OrtAllocator* allocator, const Shape& den } #endif // !defined(DISABLE_SPARSE_TENSORS) -inline Value Value::CreateMap(Value& keys, Value& values) { +inline Value Value::CreateMap(const Value& keys, const Value& values) { OrtValue* out; - OrtValue* inputs[2] = {keys, values}; + const OrtValue* inputs[2] = {keys, values}; ThrowOnError(GetApi().CreateValue(inputs, 2, ONNX_TYPE_MAP, &out)); return Value{out}; } -inline Value Value::CreateSequence(std::vector& values) { +inline Value Value::CreateSequence(const std::vector& values) { OrtValue* out; - std::vector values_ort{values.data(), values.data() + values.size()}; + std::vector values_ort{values.data(), values.data() + values.size()}; ThrowOnError(GetApi().CreateValue(values_ort.data(), values_ort.size(), ONNX_TYPE_SEQUENCE, &out)); return Value{out}; }