Skip to content

Commit ab1c57e

Browse files
authored
[ONNXIFI]Add extension to be implementable (onnx#1796)
* add onnxifi extension function in onnxifi_op * add cmake summary
1 parent b92eee8 commit ab1c57e

7 files changed

+99
-1
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ option(ONNX_WERROR "Build with Werror" OFF)
2323
option(ONNX_COVERAGE "Build with coverage instrumentation" OFF)
2424
option(ONNX_BUILD_TESTS "Build ONNX C++ APIs Tests" OFF)
2525
option(ONNX_USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
26+
option(ONNXIFI_ENABLE_EXT "Enable onnxifi extensions." OFF)
2627
if(DEFINED ENV{ONNX_ML})
2728
set(DEFAULT_ONNX_ML $ENV{ONNX_ML})
2829
else()
@@ -543,6 +544,11 @@ endif()
543544

544545
# ---[ ONNXIFI dummy backend
545546
add_library(onnxifi_dummy SHARED onnx/onnxifi_dummy.c)
547+
548+
if(ONNXIFI_ENABLE_EXT)
549+
add_definitions(-DONNXIFI_ENABLE_EXT=ON)
550+
endif()
551+
546552
target_include_directories(onnxifi_dummy PRIVATE
547553
$<BUILD_INTERFACE:${ONNX_ROOT}>
548554
$<INSTALL_INTERFACE:include>)
@@ -584,9 +590,11 @@ if(NOT ANDROID AND NOT IOS)
584590
install(TARGETS onnxifi_wrapper
585591
EXPORT ONNXTargets DESTINATION lib)
586592
endif()
593+
587594
if(ONNXIFI_DUMMY_BACKEND)
588595
add_definitions(-DONNXIFI_DUMMY_BACKEND=1)
589596
endif()
597+
590598
if(ONNX_BUILD_TESTS)
591599
include(${ONNX_ROOT}/cmake/unittest.cmake)
592600
endif()

cmake/summary.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function (onnx_print_configuration_summary)
2121
message(STATUS " ONNX_BUILD_BENCHMARKS : ${ONNX_BUILD_BENCHMARKS}")
2222
message(STATUS " ONNX_USE_LITE_PROTO : ${ONNX_USE_LITE_PROTO}")
2323
message(STATUS " ONNXIFI_DUMMY_BACKEND : ${ONNXIFI_DUMMY_BACKEND}")
24+
message(STATUS " ONNXIFI_ENABLE_EXT : ${ONNXIFI_ENABLE_EXT}")
2425
message(STATUS "")
2526
message(STATUS " Protobuf compiler : ${PROTOBUF_PROTOC_EXECUTABLE}")
2627
message(STATUS " Protobuf includes : ${PROTOBUF_INCLUDE_DIRS}")

onnx/backend/test/cpp/driver_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "onnx/checker.h"
44
#include "onnx/onnxifi.h"
5+
#include "onnx/onnxifi_ext.h"
56
#include "onnx/onnxifi_loader.h"
67
#include "onnx/string_utils.h"
78

@@ -322,6 +323,17 @@ class ONNXCppDriverTest : public ::testing::TestWithParam<
322323
true);
323324
}
324325
}
326+
/*
327+
* Examine functions in onnxifi_ext
328+
*/
329+
#ifdef ONNXIFI_ENABLE_EXT
330+
onnxExtensionFunctionPointer* f;
331+
ASSERT_TRUE(IsGtestAssertMemorySafeSuccess(
332+
lib.onnxGetExtensionFunctionAddress(
333+
backendID, "onnxGetExtensionFunctionAddress", f),
334+
NULL,
335+
lib));
336+
#endif
325337
}
326338
ASSERT_TRUE(IsGtestAssertMemorySafeSuccess(
327339
lib.onnxReleaseGraph(graph), NULL, lib));

onnx/onnxifi_dummy.c

+58
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
#include <math.h>
88

99
#include "onnx/onnxifi.h"
10+
#include "onnx/onnxifi_ext.h"
11+
12+
/*
13+
* ONNXIFI Functions
14+
*/
1015

1116
ONNXIFI_PUBLIC ONNXIFI_CHECK_RESULT onnxStatus ONNXIFI_ABI
1217
onnxGetBackendIDs(onnxBackendID* backendIDs, size_t* numBackends) {
@@ -136,3 +141,56 @@ ONNXIFI_PUBLIC ONNXIFI_CHECK_RESULT onnxStatus ONNXIFI_ABI
136141
onnxReleaseGraph(onnxGraph graph) {
137142
return ONNXIFI_STATUS_SUCCESS;
138143
}
144+
145+
/*
146+
* ONNXIFI Extension Functions
147+
*/
148+
149+
/*
150+
* This is the function list and the number of functions in onnxifi_ext
151+
* we have in this backend. It should be a subset of ALL_EXT_FUNCTION_LIST
152+
* in onnxifi_ext.h
153+
*/
154+
const int extension_function_number = 2;
155+
const char* extension_function_list[] = {"onnxGetExtensionFunctionAddress",
156+
"onnxSetIOAndRunGraph"};
157+
158+
ONNXIFI_PUBLIC ONNXIFI_CHECK_RESULT onnxStatus ONNXIFI_ABI
159+
onnxGetExtensionFunctionAddress(
160+
onnxBackendID backendID,
161+
const char* name,
162+
onnxExtensionFunctionPointer* function) {
163+
if (name == NULL || function == NULL) {
164+
return ONNXIFI_STATUS_INVALID_POINTER;
165+
}
166+
*function = NULL;
167+
int i;
168+
for (i = 0; i < extension_function_number; i++) {
169+
/* target function found */
170+
if (strcmp(name, extension_function_list[i]) == 0) {
171+
switch (i) {
172+
case 0:
173+
*function = &onnxGetExtensionFunctionAddress;
174+
break;
175+
case 1:
176+
*function = &onnxSetIOAndRunGraph;
177+
break;
178+
}
179+
}
180+
}
181+
182+
if (*function == NULL) {
183+
return ONNXIFI_STATUS_UNIDENTIFIED_NAME;
184+
}
185+
return ONNXIFI_STATUS_SUCCESS;
186+
}
187+
188+
ONNXIFI_PUBLIC ONNXIFI_CHECK_RESULT onnxStatus ONNXIFI_ABI onnxSetIOAndRunGraph(
189+
onnxGraph graph,
190+
uint32_t inputsCount,
191+
const onnxTensorDescriptorV1* inputDescriptors,
192+
uint32_t outputsCount,
193+
const onnxTensorDescriptorV1* outputDescriptors,
194+
onnxMemoryFenceV1* outputFence) {
195+
return ONNXIFI_STATUS_SUCCESS;
196+
}

onnx/onnxifi_ext.h

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
extern "C" {
88
#endif
99

10+
/*
11+
* This is the super set of all extension functions we support in onnxifi.
12+
* All backend should support a subset of function of this list.
13+
*/
14+
static const int ALL_EXT_FUNCTION_NUMBER = 2;
15+
static const char* ALL_EXT_FUNCTION_LIST[] = {"onnxGetExtensionFunctionAddress",
16+
"onnxSetIOAndRunGraph"};
17+
1018
/**
1119
* Generic ONNXIFI extension function pointer.
1220
*

onnx/onnxifi_loader.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ static const char onnxifi_function_names[] =
4848
"onnxInitGraph\0"
4949
"onnxSetGraphIO\0"
5050
"onnxRunGraph\0"
51-
"onnxReleaseGraph\0";
51+
"onnxReleaseGraph\0"
52+
#ifdef ONNXIFI_ENABLE_EXT
53+
"onnxGetExtensionFunctionAddress\0"
54+
#endif
55+
;
5256

5357
int ONNXIFI_ABI onnxifi_load(
5458
uint32_t flags,

onnx/onnxifi_loader.h

+7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
#include "onnx/onnxifi.h"
55
#include "onnx/onnxifi_ext.h"
66

7+
#ifdef ONNXIFI_ENABLE_EXT
8+
#define ONNXIFI_LOADER_FUNCTION_COUNT 16
9+
#else
710
#define ONNXIFI_LOADER_FUNCTION_COUNT 15
11+
#endif
812

913
#define ONNXIFI_LOADER_FLAG_VERSION_MASK 0xFF
1014
#define ONNXIFI_LOADER_FLAG_VERSION_1_0 0x01
@@ -49,6 +53,9 @@ struct onnxifi_library {
4953
onnxSetGraphIOFunction onnxSetGraphIO;
5054
onnxRunGraphFunction onnxRunGraph;
5155
onnxReleaseGraphFunction onnxReleaseGraph;
56+
#ifdef ONNXIFI_ENABLE_EXT
57+
onnxGetExtensionFunctionAddressFunction onnxGetExtensionFunctionAddress;
58+
#endif
5259
};
5360
void* functions[ONNXIFI_LOADER_FUNCTION_COUNT];
5461
};

0 commit comments

Comments
 (0)