From a41e7c7110a30c1998f9f953bd28594d8e5a5389 Mon Sep 17 00:00:00 2001 From: Steve Cvetko Date: Tue, 17 Sep 2024 16:03:21 -0600 Subject: [PATCH] changes to run on hololens https://developercommunity.visualstudio.com/t/ILT0005-error-code-2-microsoft-net-nativ/10455286 https://github.com/microsoft/onnxruntime/issues/19495 --- cmake/CMakeLists.txt | 22 ++--- .../Microsoft.ML.OnnxRuntime.csproj | 2 +- .../NativeMethods.shared.cs | 80 +++++++++++++------ .../SessionOptions.shared.cs | 5 ++ .../Training/NativeTrainingMethods.shared.cs | 22 ++++- tools/ci_build/build.py | 4 + 6 files changed, 96 insertions(+), 39 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 94d650f685235..25bfafcfa777f 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -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.") @@ -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) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj index 1d15383239baf..d52a11b38a21a 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj +++ b/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.csproj @@ -5,7 +5,7 @@ - true + false netstandard2.0;netcoreapp3.1;net6.0 diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs index 4128524b30483..cebaf27acef5d 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs @@ -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; @@ -17,7 +21,11 @@ public struct OrtApiBase // OrtApi ort_api_1_to_ (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; @@ -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(); @@ -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)); @@ -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( @@ -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 /// /// Creates native OrtTensorRTProviderOptions instance @@ -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; @@ -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( @@ -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); @@ -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); @@ -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); @@ -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( @@ -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 /// /// Create OrtIoBinding instance that is used to bind memory that is allocated @@ -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 /// /// Gets the ModelMetadata associated with an InferenceSession @@ -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); @@ -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 /// /// Queries all the execution providers supported in the native onnxruntime shared library @@ -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. diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs index 7a68246c9b67a..cfbedc92ec4b1 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.shared.cs @@ -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) ); diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/Training/NativeTrainingMethods.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/Training/NativeTrainingMethods.shared.cs index 68a399f8b9671..a6a905c037274 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/Training/NativeTrainingMethods.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/Training/NativeTrainingMethods.shared.cs @@ -53,8 +53,14 @@ 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); @@ -62,13 +68,25 @@ internal static class NativeTrainingMethods 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)); diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 31f242cce1a23..061bf323b9687 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -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"]