Skip to content

Commit 29d9b2f

Browse files
authored
Tweak external resource importer handle structs (#27040)
### Description <!-- Describe your changes. --> Use the descriptor struct in the external resource handle. We were copying most fields, but the setup is a little more intuitive when the descriptor is used directly. ### 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. -->
1 parent cfccd64 commit 29d9b2f

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

include/onnxruntime/core/session/onnxruntime_ep_c_api.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ ORT_RUNTIME_CLASS(ExternalResourceImporterImpl);
4343
* \since Version 1.24.
4444
*/
4545
struct OrtExternalMemoryHandle {
46-
uint32_t version; ///< Must be ORT_API_VERSION
47-
const OrtEpDevice* ep_device; ///< EP device that created this handle
48-
OrtExternalMemoryHandleType handle_type; ///< Original handle type for tracking
49-
size_t size_bytes; ///< Size of the imported memory
50-
size_t offset_bytes; ///< Offset into the imported memory
46+
uint32_t version; ///< Must be ORT_API_VERSION
47+
const OrtEpDevice* ep_device; ///< EP device that created this handle
48+
OrtExternalMemoryDescriptor descriptor; ///< External memory descriptor
5149

5250
/** \brief Release callback for this handle. EP sets this to its release function.
5351
*
@@ -72,9 +70,9 @@ struct OrtExternalMemoryHandle {
7270
* \since Version 1.24.
7371
*/
7472
struct OrtExternalSemaphoreHandle {
75-
uint32_t version; ///< Must be ORT_API_VERSION
76-
const OrtEpDevice* ep_device; ///< EP device that created this handle
77-
OrtExternalSemaphoreType type; ///< Original semaphore type
73+
uint32_t version; ///< Must be ORT_API_VERSION
74+
const OrtEpDevice* ep_device; ///< EP device that created this handle
75+
OrtExternalSemaphoreDescriptor descriptor; ///< External semaphore descriptor
7876

7977
/** \brief Release callback for this handle. EP sets this to its release function.
8078
*

onnxruntime/test/autoep/library/example_plugin_ep/ep_external_resource_importer.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ OrtStatus* ORT_API_CALL ExampleExternalResourceImporter::ImportMemoryImpl(
6565
// For testing purposes, we simulate this by allocating CPU memory
6666
// that mirrors the size of the external allocation.
6767

68-
auto* handle = new (std::nothrow) ExampleExternalMemoryHandle();
68+
auto* handle = new (std::nothrow) ExampleExternalMemoryHandle(*desc);
6969
if (handle == nullptr) {
7070
return impl.apis_.ort_api.CreateStatus(ORT_FAIL, "Failed to allocate external memory handle");
7171
}
@@ -74,10 +74,6 @@ OrtStatus* ORT_API_CALL ExampleExternalResourceImporter::ImportMemoryImpl(
7474
size_t effective_size = desc->size_bytes - desc->offset_bytes;
7575
handle->simulated_ptr = std::make_unique<char[]>(effective_size);
7676

77-
handle->size_bytes = desc->size_bytes;
78-
handle->offset_bytes = desc->offset_bytes;
79-
handle->handle_type = desc->handle_type;
80-
8177
*out_handle = handle;
8278
return nullptr;
8379
}
@@ -132,7 +128,7 @@ OrtStatus* ORT_API_CALL ExampleExternalResourceImporter::CreateTensorFromMemoryI
132128
// 1. Calculate actual tensor size from shape + element_type
133129
// 2. Validate it fits within available memory region
134130
// 3. Use that validated size rather than subtracting offsets
135-
size_t buffer_size = handle->size_bytes - handle->offset_bytes - tensor_desc->offset_bytes;
131+
size_t buffer_size = handle->descriptor.size_bytes - handle->descriptor.offset_bytes - tensor_desc->offset_bytes;
136132

137133
// Create tensor with pre-allocated memory
138134
status = impl.apis_.ort_api.CreateTensorWithDataAsOrtValue(
@@ -180,12 +176,11 @@ OrtStatus* ORT_API_CALL ExampleExternalResourceImporter::ImportSemaphoreImpl(
180176
//
181177
// For testing purposes, we create a simulated semaphore using an atomic counter
182178

183-
auto* handle = new (std::nothrow) ExampleExternalSemaphoreHandle();
179+
auto* handle = new (std::nothrow) ExampleExternalSemaphoreHandle(*desc);
184180
if (handle == nullptr) {
185181
return impl.apis_.ort_api.CreateStatus(ORT_FAIL, "Failed to allocate external semaphore handle");
186182
}
187183

188-
handle->type = desc->type;
189184
handle->value.store(0);
190185

191186
*out_handle = handle;

onnxruntime/test/autoep/library/example_plugin_ep/ep_external_resource_importer.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
struct ExampleExternalMemoryHandle : OrtExternalMemoryHandle {
2020
std::unique_ptr<char[]> simulated_ptr; ///< Simulated mapped pointer (CPU memory for testing)
2121

22-
ExampleExternalMemoryHandle()
22+
ExampleExternalMemoryHandle(const OrtExternalMemoryDescriptor& descriptor_in)
2323
: simulated_ptr(nullptr) {
2424
// Initialize base struct fields
2525
version = ORT_API_VERSION;
2626
ep_device = nullptr;
27-
handle_type = ORT_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE;
28-
size_bytes = 0;
29-
offset_bytes = 0;
27+
descriptor = descriptor_in;
3028
Release = ReleaseCallback;
3129
}
3230

@@ -48,12 +46,12 @@ struct ExampleExternalMemoryHandle : OrtExternalMemoryHandle {
4846
struct ExampleExternalSemaphoreHandle : OrtExternalSemaphoreHandle {
4947
std::atomic<uint64_t> value; ///< Simulated fence value for testing
5048

51-
ExampleExternalSemaphoreHandle()
49+
ExampleExternalSemaphoreHandle(const OrtExternalSemaphoreDescriptor& descriptor_in)
5250
: value(0) {
5351
// Initialize base struct fields
5452
version = ORT_API_VERSION;
5553
ep_device = nullptr;
56-
type = ORT_EXTERNAL_SEMAPHORE_D3D12_FENCE;
54+
descriptor = descriptor_in;
5755
Release = ReleaseCallback;
5856
}
5957

0 commit comments

Comments
 (0)