diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs index df7d12ec5e..607f98b77a 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs @@ -48,6 +48,7 @@ namespace Microsoft.ML.OnnxRuntime private static SessionOptions MakeSessionOptionWithCpuProvider() { + CheckLibcVersionGreaterThanMinimum(); SessionOptions options = new SessionOptions(); NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options._nativePtr, 1); return options; @@ -69,6 +70,7 @@ namespace Microsoft.ML.OnnxRuntime /// A SessionsOptions() object configured for execution on deviceId public static SessionOptions MakeSessionOptionWithCudaProvider(int deviceId=0) { + CheckLibcVersionGreaterThanMinimum(); CheckCudaExecutionProviderDLLs(); SessionOptions options = new SessionOptions(); NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options._nativePtr, deviceId); @@ -103,6 +105,32 @@ namespace Microsoft.ML.OnnxRuntime return true; } + [DllImport("libc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr gnu_get_libc_version(); + + private static void CheckLibcVersionGreaterThanMinimum() + { + // require libc version 2.23 or higher + var minVersion = new Version(2, 23); + var curVersion = new Version(0, 0); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + try + { + curVersion = Version.Parse(Marshal.PtrToStringAnsi(gnu_get_libc_version())); + if (curVersion >= minVersion) + return; + } + catch (Exception) + { + // trap any obscure exception + } + throw new OnnxRuntimeException(ErrorCode.RuntimeException, + $"libc.so version={curVersion} does not meet the minimun of 2.23 required by OnnxRuntime. " + + "Linux distribution should be similar to Ubuntu 16.04 or higher"); + } + } + #region destructors disposers ~SessionOptions()