Skip to content

Commit

Permalink
changes to run on hololens
Browse files Browse the repository at this point in the history
  • Loading branch information
scvetko committed Sep 17, 2024
1 parent 8f5c79c commit a41e7c7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 39 deletions.
22 changes: 11 additions & 11 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ if (onnxruntime_USE_ROCM)
endif()
endif()


add_definitions(-D_CRT_SECURE_NO_WARNINGS)

# Single output director for all binaries
set(RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "Single output directory for all binaries.")
Expand Down Expand Up @@ -1725,16 +1725,16 @@ if(onnxruntime_BUILD_KERNEL_EXPLORER)
endif()

# When GDK_PLATFORM is set then WINAPI_FAMILY is defined in gdk_toolchain.cmake (along with other relevant flags/definitions).
if (WIN32 AND NOT GDK_PLATFORM)
if (NOT CMAKE_CXX_STANDARD_LIBRARIES MATCHES kernel32.lib)
# On onecore, link to the onecore build of the MSVC runtime
get_filename_component(msvc_path "${CMAKE_C_COMPILER}/../../../.." ABSOLUTE)
link_directories(BEFORE "${msvc_path}/lib/onecore/${onnxruntime_target_platform}")
# The .lib files in the MSVC runtime have a DEFAULITLIB entry for onecore.lib, which in turn links to reverse forwarders.
# We ignore that entry and use onecore_apiset.lib instead, since system components must not rely on reverse forwarders.
add_link_options("/NODEFAULTLIB:onecore.lib")
endif()
endif()
#if (WIN32 AND NOT GDK_PLATFORM)
# if (NOT CMAKE_CXX_STANDARD_LIBRARIES MATCHES kernel32.lib)
# # On onecore, link to the onecore build of the MSVC runtime
# get_filename_component(msvc_path "${CMAKE_C_COMPILER}/../../../.." ABSOLUTE)
# link_directories(BEFORE "${msvc_path}/lib/onecore/${onnxruntime_target_platform}")
# # The .lib files in the MSVC runtime have a DEFAULITLIB entry for onecore.lib, which in turn links to reverse forwarders.
# # We ignore that entry and use onecore_apiset.lib instead, since system components must not rely on reverse forwarders.
# add_link_options("/NODEFAULTLIB:onecore.lib")
# endif()
#endif()

foreach(target_name ${ONNXRUNTIME_CMAKE_FILES})
include(${target_name}.cmake)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<IncludeMobileTargets>true</IncludeMobileTargets>
<IncludeMobileTargets>false</IncludeMobileTargets>
<BaseTargets>netstandard2.0;netcoreapp3.1;net6.0</BaseTargets>
<MobileTargets></MobileTargets>
</PropertyGroup>
Expand Down
80 changes: 55 additions & 25 deletions csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
namespace Microsoft.ML.OnnxRuntime
{
[StructLayout(LayoutKind.Sequential)]
#if NETSTANDARD2_0
public class OrtApiBase
#else
public struct OrtApiBase
#endif
{
public IntPtr GetApi;
public IntPtr GetVersionString;
Expand All @@ -17,7 +21,11 @@ public struct OrtApiBase
// OrtApi ort_api_1_to_<latest_version> (onnxruntime/core/session/onnxruntime_c_api.cc)
// If syncing your new C API, any other C APIs before yours also need to be synced here if haven't
[StructLayout(LayoutKind.Sequential)]
#if NETSTANDARD2_0
public class OrtApi
#else
public struct OrtApi
#endif
{
public IntPtr CreateStatus;
public IntPtr GetErrorCode;
Expand Down Expand Up @@ -300,8 +308,13 @@ internal static class NativeMethods
{
static OrtApi api_;

#if NETSTANDARD2_0
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr DOrtGetApi(UInt32 version);
#else
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate ref OrtApi DOrtGetApi(UInt32 version);
#endif

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr DOrtGetVersionString();
Expand All @@ -310,11 +323,24 @@ internal static class NativeMethods

static NativeMethods()
{
#if NETSTANDARD2_0
IntPtr ortApiBasePtr = OrtGetApiBase();
OrtApiBase ortApiBase = (OrtApiBase)Marshal.PtrToStructure(ortApiBasePtr, typeof(OrtApiBase));
DOrtGetApi OrtGetApi = (DOrtGetApi)Marshal.GetDelegateForFunctionPointer(ortApiBase.GetApi, typeof(DOrtGetApi));
#else
DOrtGetApi OrtGetApi = (DOrtGetApi)Marshal.GetDelegateForFunctionPointer(OrtGetApiBase().GetApi, typeof(DOrtGetApi));
#endif

const uint ORT_API_VERSION = 14;
#if NETSTANDARD2_0
IntPtr ortApiPtr = OrtGetApi(ORT_API_VERSION);
api_ = (OrtApi)Marshal.PtrToStructure(ortApiPtr, typeof(OrtApi));
OrtGetVersionString = (DOrtGetVersionString)Marshal.GetDelegateForFunctionPointer(ortApiBase.GetVersionString, typeof(DOrtGetVersionString));
#else
// TODO: Make this save the pointer, and not copy the whole structure across
api_ = (OrtApi)OrtGetApi(14 /*ORT_API_VERSION*/);
api_ = (OrtApi)OrtGetApi(ORT_API_VERSION);
OrtGetVersionString = (DOrtGetVersionString)Marshal.GetDelegateForFunctionPointer(OrtGetApiBase().GetVersionString, typeof(DOrtGetVersionString));
#endif

OrtCreateEnv = (DOrtCreateEnv)Marshal.GetDelegateForFunctionPointer(api_.CreateEnv, typeof(DOrtCreateEnv));
OrtCreateEnvWithCustomLogger = (DOrtCreateEnvWithCustomLogger)Marshal.GetDelegateForFunctionPointer(api_.CreateEnvWithCustomLogger, typeof(DOrtCreateEnvWithCustomLogger));
Expand Down Expand Up @@ -529,9 +555,13 @@ internal class NativeLib
}

[DllImport(NativeLib.DllName, CharSet = CharSet.Ansi)]
#if NETSTANDARD2_0
public static extern IntPtr OrtGetApiBase();
#else
public static extern ref OrtApiBase OrtGetApiBase();
#endif

#region Runtime / Environment API
#region Runtime / Environment API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /* OrtStatus* */ DOrtCreateEnv(
Expand Down Expand Up @@ -588,9 +618,9 @@ internal class NativeLib
public delegate IntPtr /* OrtStatus* */ DOrtUpdateEnvWithCustomLogLevel(IntPtr /*(OrtEnv*)*/ env, OrtLoggingLevel custom_log_level);
public static DOrtUpdateEnvWithCustomLogLevel OrtUpdateEnvWithCustomLogLevel;

#endregion Runtime / Environment API
#endregion Runtime / Environment API

#region Provider Options API
#region Provider Options API

/// <summary>
/// Creates native OrtTensorRTProviderOptions instance
Expand Down Expand Up @@ -724,9 +754,9 @@ internal class NativeLib
public delegate void DOrtReleaseROCMProviderOptions(IntPtr /*(OrtROCMProviderOptions*)*/ rocmProviderOptionsInstance);
public static DOrtReleaseROCMProviderOptions OrtReleaseROCMProviderOptions;

#endregion
#endregion

#region Status API
#region Status API
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate ErrorCode DOrtGetErrorCode(IntPtr /*(OrtStatus*)*/ status);
public static DOrtGetErrorCode OrtGetErrorCode;
Expand All @@ -741,9 +771,9 @@ internal class NativeLib
public delegate void DOrtReleaseStatus(IntPtr /*(OrtStatus*)*/ statusPtr);
public static DOrtReleaseStatus OrtReleaseStatus;

#endregion Status API
#endregion Status API

#region InferenceSession API
#region InferenceSession API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /* OrtStatus* */ DOrtCreateSession(
Expand Down Expand Up @@ -943,9 +973,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca
IntPtr /*(void*)*/ user_data);
public static DOrtRunAsync OrtRunAsync;

#endregion InferenceSession API
#endregion InferenceSession API

#region SessionOptions API
#region SessionOptions API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /*(OrtStatus*)*/ DOrtCreateSessionOptions(out IntPtr /*(OrtSessionOptions**)*/ sessionOptions);
Expand Down Expand Up @@ -1226,9 +1256,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DSessionOptionsAppendExecutionProvider SessionOptionsAppendExecutionProvider;

#endregion
#endregion

#region RunOptions API
#region RunOptions API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /*(OrtStatus*)*/ DOrtCreateRunOptions(out IntPtr /* OrtRunOptions** */ runOptions);
Expand Down Expand Up @@ -1285,9 +1315,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca
byte[] /* const char* */ configValue);
public static DOrtAddRunConfigEntry OrtAddRunConfigEntry;

#endregion
#endregion

#region ThreadingOptions API
#region ThreadingOptions API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /*(OrtStatus*)*/ DOrtCreateThreadingOptions(out IntPtr /* OrtCreateThreadingOptions** */ threadingOptions);
Expand All @@ -1312,9 +1342,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /*(OrtStatus*)*/ DOrtThreadingOptionsSetGlobalSpinControl(IntPtr /* OrtThreadingOptions* */ threadingOptions, int allowSpinning);
public static DOrtThreadingOptionsSetGlobalSpinControl OrtThreadingOptionsSetGlobalSpinControl;
#endregion
#endregion

#region Allocator / MemoryInfo API
#region Allocator / MemoryInfo API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /* (OrtStatus*)*/ DOrtCreateMemoryInfo(
Expand Down Expand Up @@ -1451,9 +1481,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DOrtAllocatorFree OrtAllocatorFree;

#endregion Allocator / MemoryInfo API
#endregion Allocator / MemoryInfo API

#region IoBinding API
#region IoBinding API

/// <summary>
/// Create OrtIoBinding instance that is used to bind memory that is allocated
Expand Down Expand Up @@ -1642,9 +1672,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DOrtSetLanguageProjection OrtSetLanguageProjection;

#endregion IoBinding API
#endregion IoBinding API

#region ModelMetadata API
#region ModelMetadata API

/// <summary>
/// Gets the ModelMetadata associated with an InferenceSession
Expand Down Expand Up @@ -1762,9 +1792,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DOrtReleaseModelMetadata OrtReleaseModelMetadata;

#endregion ModelMetadata API
#endregion ModelMetadata API

#region OrtValue API
#region OrtValue API

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /*(OrtStatus*)*/ DOrtHasValue(IntPtr /*(OrtValue*)*/ value, out IntPtr /*(int*)*/ hasValue);
Expand Down Expand Up @@ -2024,9 +2054,9 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DOrtReleaseValue OrtReleaseValue;

#endregion
#endregion

#region Misc API
#region Misc API

/// <summary>
/// Queries all the execution providers supported in the native onnxruntime shared library
Expand Down Expand Up @@ -2066,7 +2096,7 @@ IntPtr[] outputValues /* An array of output value pointers. Array must be alloca

public static DOrtReleasePrepackedWeightsContainer OrtReleasePrepackedWeightsContainer;

#endregion
#endregion
} // class NativeMethods

// onnxruntime-extensions helpers to make usage simpler.
Expand Down
5 changes: 5 additions & 0 deletions csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,12 @@ public void RegisterOrtExtensions()
{
try
{
#if NETSTANDARD2_0
var ortApiBasePtr = NativeMethods.OrtGetApiBase();
var ortApiBase = (OrtApiBase)Marshal.PtrToStructure(ortApiBasePtr, typeof(OrtApiBase));
#else
var ortApiBase = NativeMethods.OrtGetApiBase();
#endif
NativeApiStatus.VerifySuccess(
OrtExtensionsNativeMethods.RegisterCustomOps(this.handle, ref ortApiBase)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,40 @@ internal static class NativeTrainingMethods
static OrtTrainingApi trainingApi_;
static IntPtr trainingApiPtr;

#if NETSTANDARD2_0
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr DOrtGetApi(UInt32 version);
#else
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate ref OrtApi DOrtGetApi(UInt32 version);
#endif


[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate IntPtr /* OrtTrainingApi* */ DOrtGetTrainingApi(UInt32 version);
public static DOrtGetTrainingApi OrtGetTrainingApi;

static NativeTrainingMethods()
{
#if NETSTANDARD2_0
IntPtr ortApiBasePtr = NativeMethods.OrtGetApiBase();
OrtApiBase ortApiBase = (OrtApiBase)Marshal.PtrToStructure(ortApiBasePtr, typeof(OrtApiBase));
DOrtGetApi OrtGetApi = (DOrtGetApi)Marshal.GetDelegateForFunctionPointer(ortApiBase.GetApi, typeof(DOrtGetApi));
#else
DOrtGetApi OrtGetApi = (DOrtGetApi)Marshal.GetDelegateForFunctionPointer(NativeMethods.OrtGetApiBase().GetApi, typeof(DOrtGetApi));
#endif

const uint ORT_API_VERSION = 17;
#if NETSTANDARD2_0
IntPtr ortApiPtr = OrtGetApi(ORT_API_VERSION);
api_ = (OrtApi)Marshal.PtrToStructure(ortApiPtr, typeof(OrtApi));
#else
// TODO: Make this save the pointer, and not copy the whole structure across
api_ = (OrtApi)OrtGetApi(17 /*ORT_API_VERSION*/);
api_ = (OrtApi)OrtGetApi(ORT_API_VERSION);
#endif

OrtGetTrainingApi = (DOrtGetTrainingApi)Marshal.GetDelegateForFunctionPointer(api_.GetTrainingApi, typeof(DOrtGetTrainingApi));
trainingApiPtr = OrtGetTrainingApi(17 /*ORT_API_VERSION*/);
trainingApiPtr = OrtGetTrainingApi(ORT_API_VERSION);
if (trainingApiPtr != IntPtr.Zero)
{
trainingApi_ = (OrtTrainingApi)Marshal.PtrToStructure(trainingApiPtr, typeof(OrtTrainingApi));
Expand Down
4 changes: 4 additions & 0 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ def generate_build_tree(
log.info("Generating CMake build tree")
cmake_dir = os.path.join(source_dir, "cmake")
cmake_args = [cmake_path, cmake_dir]
cmake_args += [
"-DCMAKE_SYSTEM_NAME=WindowsStore",
"-DCMAKE_SYSTEM_VERSION=10.0"
]
if not use_dev_mode(args):
cmake_args += ["--compile-no-warning-as-error"]

Expand Down

0 comments on commit a41e7c7

Please sign in to comment.