[CPP API]Fix constness in C++API (#16103)

### Description
`CreateMap` and `CreateSequence` should be able to take in const data.
This commit is contained in:
Dmitri Smirnov 2023-05-26 14:09:00 -07:00 committed by GitHub
parent 54fdb640fe
commit 9939092e71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 8 deletions

View file

@ -1317,6 +1317,7 @@ struct Value : detail::ValueImpl<OrtValue> {
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<OrtValue> {
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<OrtValue> {
template <typename T>
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<OrtValue> {
*/
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<Value>& 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<Value>& 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 <typename T>
static Value CreateOpaque(const char* domain, const char* type_name, const T&); ///< Wraps OrtApi::CreateOpaqueValue

View file

@ -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<Value>& values) {
inline Value Value::CreateSequence(const std::vector<Value>& values) {
OrtValue* out;
std::vector<OrtValue*> values_ort{values.data(), values.data() + values.size()};
std::vector<const OrtValue*> values_ort{values.data(), values.data() + values.size()};
ThrowOnError(GetApi().CreateValue(values_ort.data(), values_ort.size(), ONNX_TYPE_SEQUENCE, &out));
return Value{out};
}