Skip to content

Commit d3076d3

Browse files
authored
Add BasicPluginEp example (#545)
Add a basic example plugin EP library. Add Android package and a simple Android app that uses it. Add Python package and simple example usage script.
1 parent 5b73e75 commit d3076d3

67 files changed

Lines changed: 2497 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BasedOnStyle: Google
44

55
# Setting ColumnLimit to 0 so developer choices about where to break lines are maintained.
66
# Developers are responsible for adhering to the 120 character maximum.
7-
ColumnLimit: 120
7+
ColumnLimit: 0
88
SortIncludes: false
99
DerivePointerAlignment: false
1010
# Avoid adding spaces between tokens in GSL_SUPPRESS arguments.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
cmake_minimum_required(VERSION 3.31)
5+
6+
project(BasicPluginEp)
7+
8+
set(CMAKE_CXX_STANDARD 20)
9+
10+
set(plugin_ep_common_dir ${CMAKE_SOURCE_DIR}/../common)
11+
12+
include(${plugin_ep_common_dir}/cmake/onnxruntime_library_utils.cmake)
13+
14+
# Set ORT_HOME (e.g., with cmake -DORT_HOME=/path/to/ort_home) to specify the directory with the ONNX Runtime header
15+
# and library files to use.
16+
# This is optional. If unspecified, the ONNX Runtime files will be downloaded.
17+
18+
set_onnxruntime_paths(
19+
ORT_HOME ${ORT_HOME}
20+
DEFAULT_ORT_VERSION "1.23.2"
21+
ORT_INCLUDE_DIR_VAR ORT_INCLUDE_DIR
22+
ORT_LIBRARY_DIR_VAR ORT_LIBRARY_DIR)
23+
24+
message(STATUS "ORT_LIBRARY_DIR: ${ORT_LIBRARY_DIR}")
25+
message(STATUS "ORT_INCLUDE_DIR: ${ORT_INCLUDE_DIR}")
26+
27+
#
28+
# basic_plugin_ep
29+
#
30+
block()
31+
32+
add_library(basic_plugin_ep MODULE)
33+
34+
target_sources(basic_plugin_ep PRIVATE
35+
${CMAKE_SOURCE_DIR}/src/ep_factory.cc
36+
${CMAKE_SOURCE_DIR}/src/ep_factory.h
37+
${CMAKE_SOURCE_DIR}/src/ep_lib_entry.cc
38+
${CMAKE_SOURCE_DIR}/src/ep.cc
39+
${CMAKE_SOURCE_DIR}/src/ep.h
40+
${plugin_ep_common_dir}/src/plugin_ep_utils.h
41+
)
42+
43+
target_include_directories(basic_plugin_ep PRIVATE
44+
${ORT_INCLUDE_DIR}
45+
${plugin_ep_common_dir}/src
46+
)
47+
48+
target_link_directories(basic_plugin_ep PRIVATE ${ORT_LIBRARY_DIR})
49+
target_link_libraries(basic_plugin_ep PRIVATE onnxruntime)
50+
51+
set(basic_plugin_ep_link_options)
52+
if(MSVC)
53+
list(APPEND basic_plugin_ep_link_options
54+
"-DEF:${CMAKE_SOURCE_DIR}/src/ep_lib.def")
55+
else()
56+
if(UNIX)
57+
if(APPLE)
58+
list(APPEND basic_plugin_ep_link_options
59+
"LINKER:-dead_strip")
60+
else()
61+
list(APPEND basic_plugin_ep_link_options
62+
"LINKER:--version-script=${CMAKE_SOURCE_DIR}/src/ep_lib.lds"
63+
"LINKER:--no-undefined"
64+
"LINKER:--gc-sections"
65+
"-z" "noexecstack")
66+
endif()
67+
endif()
68+
endif()
69+
70+
target_link_options(basic_plugin_ep PRIVATE ${basic_plugin_ep_link_options})
71+
72+
endblock()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Specifies a directory containing ONNX Runtime libraries and headers to use. This directory
2+
// should match the onnxruntime-android AAR structure.
3+
// Specifying `ortHome` is optional. If unspecified, the ONNX Runtime files will be downloaded.
4+
val ortHome: String? = System.getProperty("ortHome")
5+
6+
plugins {
7+
alias(libs.plugins.android.library)
8+
alias(libs.plugins.kotlin.android)
9+
}
10+
11+
android {
12+
namespace = "ai.onnxruntime.example.basicpluginep"
13+
compileSdk {
14+
version = release(36)
15+
}
16+
17+
defaultConfig {
18+
minSdk = 26
19+
20+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21+
consumerProguardFiles("consumer-rules.pro")
22+
externalNativeBuild {
23+
cmake {
24+
if (ortHome != null) {
25+
arguments += listOf("-DORT_HOME=${ortHome}")
26+
}
27+
}
28+
}
29+
}
30+
31+
buildTypes {
32+
release {
33+
isMinifyEnabled = false
34+
proguardFiles(
35+
getDefaultProguardFile("proguard-android-optimize.txt"),
36+
"proguard-rules.pro"
37+
)
38+
}
39+
}
40+
41+
externalNativeBuild {
42+
cmake {
43+
path("../../CMakeLists.txt")
44+
version = "3.31.6"
45+
}
46+
}
47+
compileOptions {
48+
sourceCompatibility = JavaVersion.VERSION_11
49+
targetCompatibility = JavaVersion.VERSION_11
50+
}
51+
kotlinOptions {
52+
jvmTarget = "11"
53+
}
54+
}
55+
56+
dependencies {
57+
implementation(libs.androidx.core.ktx)
58+
implementation(libs.androidx.appcompat)
59+
implementation(libs.material)
60+
testImplementation(libs.junit)
61+
androidTestImplementation(libs.androidx.junit)
62+
androidTestImplementation(libs.androidx.espresso.core)
63+
}

plugin_execution_providers/basic/android/basicpluginep/consumer-rules.pro

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ai.onnxruntime.example.basicpluginep
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("ai.onnxruntime.example.basicpluginep.test", appContext.packageName)
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ai.onnxruntime.example.basicpluginep
2+
3+
private const val basicPluginEpLibraryName = "basic_plugin_ep"
4+
private const val basicPluginEpName = "BasicPluginExecutionProvider"
5+
6+
/**
7+
* Returns the path to the basic plugin EP library.
8+
* This path can be used with `OrtEnvironment.registerExecutionProviderLibrary()`.
9+
*/
10+
fun getLibraryPath() : String {
11+
return "lib${basicPluginEpLibraryName}.so"
12+
}
13+
14+
/**
15+
* Returns the name of the EP provided by the basic plugin EP library.
16+
* This name can be used to select an appropriate `OrtEpDevice`.
17+
*/
18+
fun getEpName() : String {
19+
return basicPluginEpName
20+
}
21+
22+
/**
23+
* Returns the names of the EPs provided by the basic plugin EP library. There is only one.
24+
* These names can be used to select an appropriate `OrtEpDevice`.
25+
*/
26+
fun getEpNames() : Array<String> {
27+
return arrayOf(getEpName())
28+
}

0 commit comments

Comments
 (0)