Skip to content

Commit 024ae9e

Browse files
authored
Update some new API functions to directly return the value if they're simple getters. (#25459)
### Description <!-- Describe your changes. --> Update some new API functions to directly return the value if they're simple getters. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Simplify API usage.
1 parent 47312d6 commit 024ae9e

File tree

11 files changed

+32
-54
lines changed

11 files changed

+32
-54
lines changed

include/onnxruntime/core/session/onnxruntime_c_api.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5397,24 +5397,20 @@ struct OrtApi {
53975397
/** \brief Get the device memory type from ::OrtMemoryInfo
53985398
*
53995399
* \param[in] ptr The OrtMemoryInfo instance to query.
5400-
* \param[out] out The device memory type.
5401-
*
5402-
* \snippet{doc} snippets.dox OrtStatus Return Value
5400+
* \return The device memory type.
54035401
*
54045402
* \since Version 1.23
54055403
*/
5406-
ORT_API2_STATUS(MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr, _Out_ OrtDeviceMemoryType* out);
5404+
ORT_API_T(OrtDeviceMemoryType, MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr);
54075405

54085406
/** \brief Get the vendor id from ::OrtMemoryInfo
54095407
*
54105408
* \param[in] ptr The OrtMemoryInfo instance to query.
5411-
* \param[out] out The vendor id.
5412-
*
5413-
* \snippet{doc} snippets.dox OrtStatus Return Value
5409+
* \return The vendor id.
54145410
*
54155411
* \since Version 1.23
54165412
*/
5417-
ORT_API2_STATUS(MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr, _Out_ uint32_t* out);
5413+
ORT_API_T(uint32_t, MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr);
54185414

54195415
/// \name OrtValueInfo
54205416
/// @{
@@ -6084,15 +6080,14 @@ struct OrtApi {
60846080
*
60856081
* \param[in] options The OrtRunOptions instance.
60866082
* \param[in] config_key The configuration entry key. A null-terminated string.
6087-
* \param[out] config_value Output parameter set to the configuration entry value. Either a null-terminated string if
6088-
* a configuration entry exists or NULL otherwise.
6089-
* Do not free this value. It is owned by `options` and will be invalidated if another call
6090-
* to `AddRunConfigEntry()` overwrites it.
6083+
* \return The configuration entry value. Either a null-terminated string if the entry was found. nullptr otherwise.
60916084
*
60926085
* \snippet{doc} snippets.dox OrtStatus Return Value
6086+
*
6087+
* \since Version 1.23
60936088
*/
6094-
ORT_API2_STATUS(GetRunConfigEntry, _In_ const OrtRunOptions* options,
6095-
_In_z_ const char* config_key, _Outptr_result_maybenull_z_ const char** config_value);
6089+
ORT_API_T(const char*, GetRunConfigEntry, _In_ const OrtRunOptions* options,
6090+
_In_z_ const char* config_key);
60966091

60976092
/// @}
60986093

@@ -6176,7 +6171,7 @@ struct OrtApi {
61766171
/** \brief Get a const pointer to the raw data inside a tensor
61776172
*
61786173
* Used to read the internal tensor data directly.
6179-
* \note The returned pointer is valid until the \p value is destroyed.
6174+
* \note The returned pointer is valid until the OrtValue is destroyed.
61806175
*
61816176
* \param[in] value A tensor type (string tensors are not supported)
61826177
* \param[out] out Filled in with a pointer to the internal storage

include/onnxruntime/core/session/onnxruntime_cxx_inline.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,7 @@ inline RunOptions& RunOptions::AddConfigEntry(const char* config_key, const char
768768
}
769769

770770
inline const char* RunOptions::GetConfigEntry(const char* config_key) {
771-
const char* out{};
772-
ThrowOnError(GetApi().GetRunConfigEntry(p_, config_key, &out));
773-
return out;
771+
return GetApi().GetRunConfigEntry(p_, config_key);
774772
}
775773

776774
inline RunOptions& RunOptions::SetTerminate() {

include/onnxruntime/core/session/onnxruntime_ep_c_api.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,11 @@ struct OrtEpApi {
366366
/** \brief Get the OrtMemoryDevice from an OrtValue instance if it contains a Tensor.
367367
*
368368
* \param[in] value The OrtValue instance to get the memory device from.
369-
* \param[out] device The OrtMemoryDevice associated with the OrtValue instance.
370-
* \return Status Success if OrtValue contains a Tensor. Otherwise, an error status is returned.
369+
* \return Memory device if OrtValue contains a Tensor, nullptr otherwise.
371370
*
372371
* \since Version 1.23.
373372
*/
374-
ORT_API2_STATUS(Value_GetMemoryDevice, _In_ const OrtValue* value, _Out_ const OrtMemoryDevice** device);
373+
ORT_API_T(const OrtMemoryDevice*, Value_GetMemoryDevice, _In_ const OrtValue* value);
375374

376375
/** \brief Compare two OrtMemoryDevice instances for equality.
377376
*

onnxruntime/core/framework/allocator.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,10 @@ ORT_API(void, OrtApis::MemoryInfoGetDeviceType, _In_ const OrtMemoryInfo* info,
315315
*out = static_cast<OrtMemoryInfoDeviceType>(info->device.Type());
316316
}
317317

318-
ORT_API_STATUS_IMPL(OrtApis::MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr, _Out_ OrtDeviceMemoryType* out) {
319-
*out = static_cast<OrtDeviceMemoryType>(ptr->device.MemType());
320-
return nullptr;
318+
ORT_API(OrtDeviceMemoryType, OrtApis::MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr) {
319+
return static_cast<OrtDeviceMemoryType>(ptr->device.MemType());
321320
}
322321

323-
ORT_API_STATUS_IMPL(OrtApis::MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr, _Out_ uint32_t* out) {
324-
*out = ptr->device.Vendor();
325-
return nullptr;
322+
ORT_API(uint32_t, OrtApis::MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr) {
323+
return ptr->device.Vendor();
326324
}

onnxruntime/core/framework/run_options.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,13 @@ ORT_API_STATUS_IMPL(OrtApis::AddRunConfigEntry, _Inout_ OrtRunOptions* options,
6363
return onnxruntime::ToOrtStatus(options->config_options.AddConfigEntry(config_key, config_value));
6464
}
6565

66-
ORT_API_STATUS_IMPL(OrtApis::GetRunConfigEntry, _In_ const OrtRunOptions* options,
67-
_In_z_ const char* config_key, _Outptr_result_maybenull_z_ const char** config_value_out) {
68-
API_IMPL_BEGIN
66+
ORT_API(const char*, OrtApis::GetRunConfigEntry, _In_ const OrtRunOptions* options, _In_z_ const char* config_key) {
6967
const auto& config_options = options->config_options.GetConfigOptionsMap();
7068
if (auto it = config_options.find(config_key); it != config_options.end()) {
71-
*config_value_out = it->second.c_str();
69+
return it->second.c_str();
7270
} else {
73-
*config_value_out = nullptr;
71+
return nullptr;
7472
}
75-
return nullptr;
76-
API_IMPL_END
7773
}
7874

7975
ORT_API_STATUS_IMPL(OrtApis::RunOptionsAddActiveLoraAdapter, _Inout_ OrtRunOptions* options,

onnxruntime/core/providers/cuda/cuda_provider_factory.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,8 @@ struct CudaDataTransferImpl : OrtDataTransferImpl {
436436
OrtValue* dst_tensor = dst_tensors[idx];
437437
OrtSyncStream* stream = streams ? streams[idx] : nullptr;
438438

439-
const OrtMemoryDevice *src_device = nullptr, *dst_device = nullptr;
440-
RETURN_IF_ERROR(impl.ep_api.Value_GetMemoryDevice(src_tensor, &src_device));
441-
RETURN_IF_ERROR(impl.ep_api.Value_GetMemoryDevice(dst_tensor, &dst_device));
439+
const OrtMemoryDevice* src_device = impl.ep_api.Value_GetMemoryDevice(src_tensor);
440+
const OrtMemoryDevice* dst_device = impl.ep_api.Value_GetMemoryDevice(dst_tensor);
442441

443442
size_t bytes;
444443
RETURN_IF_ERROR(impl.ort_api.GetTensorSizeInBytes(src_tensor, &bytes));

onnxruntime/core/session/ep_api.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,13 @@ ORT_API(const OrtMemoryDevice*, MemoryInfo_GetMemoryDevice, _In_ const OrtMemory
130130
return static_cast<const OrtMemoryDevice*>(&memory_info->device);
131131
}
132132

133-
ORT_API_STATUS_IMPL(Value_GetMemoryDevice, _In_ const OrtValue* value, _Out_ const OrtMemoryDevice** device) {
134-
*device = nullptr;
133+
ORT_API(const OrtMemoryDevice*, Value_GetMemoryDevice, _In_ const OrtValue* value) {
135134
if (value == nullptr || value->IsTensor() == false) {
136-
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "OrtValue does not contain an allocated tensor.");
135+
return nullptr; // Tensor always has a device, so we don't need a more specific error here.
137136
}
138137

139138
auto& tensor = value->Get<Tensor>();
140-
*device = static_cast<const OrtMemoryDevice*>(&tensor.Location().device);
141-
142-
return nullptr;
139+
return static_cast<const OrtMemoryDevice*>(&tensor.Location().device);
143140
}
144141

145142
ORT_API(bool, MemoryDevice_AreEqual, _In_ const OrtMemoryDevice* a, _In_ const OrtMemoryDevice* b) {

onnxruntime/core/session/ep_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ORT_API_STATUS_IMPL(EpDevice_AddAllocatorInfo, _In_ OrtEpDevice* ep_device,
2828
_In_ const OrtMemoryInfo* allocator_memory_info);
2929

3030
ORT_API(const OrtMemoryDevice*, MemoryInfo_GetMemoryDevice, _In_ const OrtMemoryInfo* memory_info);
31-
ORT_API_STATUS_IMPL(Value_GetMemoryDevice, _In_ const OrtValue* value, _Out_ const OrtMemoryDevice** device);
31+
ORT_API(const OrtMemoryDevice*, Value_GetMemoryDevice, _In_ const OrtValue* value);
3232

3333
ORT_API(bool, MemoryDevice_AreEqual, _In_ const OrtMemoryDevice* a, _In_ const OrtMemoryDevice* b);
3434
ORT_API(OrtMemoryInfoDeviceType, MemoryDevice_GetDeviceType, _In_ const OrtMemoryDevice* memory_device);

onnxruntime/core/session/ort_apis.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ ORT_API_STATUS_IMPL(CreateMemoryInfo_V2, _In_ const char* name, _In_ enum OrtMem
608608
_In_ size_t alignment, enum OrtAllocatorType allocator_type,
609609
_Outptr_ OrtMemoryInfo** out);
610610

611-
ORT_API_STATUS_IMPL(MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr, _Out_ OrtDeviceMemoryType* out);
612-
ORT_API_STATUS_IMPL(MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr, _Out_ uint32_t* out);
611+
ORT_API(OrtDeviceMemoryType, MemoryInfoGetDeviceMemType, _In_ const OrtMemoryInfo* ptr);
612+
ORT_API(uint32_t, MemoryInfoGetVendorId, _In_ const OrtMemoryInfo* ptr);
613613

614614
// OrtValueInfo
615615
ORT_API_STATUS_IMPL(ValueInfo_GetValueProducer, _In_ const OrtValueInfo* value_info,
@@ -685,8 +685,7 @@ ORT_API_STATUS_IMPL(Node_GetSubgraphs, _In_ const OrtNode* node,
685685
ORT_API_STATUS_IMPL(Node_GetGraph, _In_ const OrtNode* node, _Outptr_result_maybenull_ const OrtGraph** graph);
686686
ORT_API_STATUS_IMPL(Node_GetEpName, _In_ const OrtNode* node, _Outptr_result_maybenull_ const char** out);
687687

688-
ORT_API_STATUS_IMPL(GetRunConfigEntry, _In_ const OrtRunOptions* options,
689-
_In_z_ const char* config_key, _Outptr_result_maybenull_z_ const char** config_value);
688+
ORT_API(const char*, GetRunConfigEntry, _In_ const OrtRunOptions* options, _In_z_ const char* config_key);
690689

691690
ORT_API(const OrtMemoryInfo*, EpDevice_MemoryInfo, _In_ const OrtEpDevice* ep_device,
692691
_In_ OrtDeviceMemoryType memory_type);

onnxruntime/test/autoep/library/ep_data_transfer.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ OrtStatus* ORT_API_CALL ExampleDataTransfer::CopyTensorsImpl(OrtDataTransferImpl
6565
for (size_t i = 0; i < num_tensors; ++i) {
6666
// the implementation for a 'real' EP would be something along these lines.
6767
// See CudaDataTransferImpl in onnxruntime\core\providers\cuda\cuda_provider_factory.cc
68-
const OrtMemoryDevice* src_device = nullptr;
69-
const OrtMemoryDevice* dst_device = nullptr;
70-
RETURN_IF_ERROR(impl.ep_api.Value_GetMemoryDevice(src_tensors[i], &src_device));
71-
RETURN_IF_ERROR(impl.ep_api.Value_GetMemoryDevice(dst_tensors[i], &dst_device));
68+
const OrtMemoryDevice* src_device = impl.ep_api.Value_GetMemoryDevice(src_tensors[i]);
69+
const OrtMemoryDevice* dst_device = impl.ep_api.Value_GetMemoryDevice(dst_tensors[i]);
7270

7371
OrtMemoryInfoDeviceType src_device_type = impl.ep_api.MemoryDevice_GetDeviceType(src_device);
7472
OrtMemoryInfoDeviceType dst_device_type = impl.ep_api.MemoryDevice_GetDeviceType(dst_device);

0 commit comments

Comments
 (0)