Repository navigation
Bring NativeLibraryConfig.WithLibrary to netstandard2.0 (#960) - #1418
YoMosa2009 wants to merge 1 commit into
Conversation
Expose WithLibrary (and the NativeLibraryConfigContainer overload) on netstandard2.0, not just NET6_0_OR_GREATER. When an explicit path is set, netstandard2.0 now loads that file directly via a small platform-specific P/Invoke shim (LoadLibrary on Windows, dlopen on Linux/macOS) standing in for System.Runtime.InteropServices.NativeLibrary, which isn't available on netstandard2.0. The CUDA/Vulkan/AVX auto-detection backend-selection matrix is left NET6_0_OR_GREATER-only for now, since it depends on System.Runtime.Intrinsics and RuntimeInformation.RuntimeIdentifier, neither of which exist pre-.NET Core 3.0. When WithLibrary isn't called, netstandard2.0 behavior is unchanged from before this commit. Manually verified on Windows via a net48 console harness (forces netstandard2.0 asset selection through a project reference) loading a real system DLL through NativeLibraryConfig.WithLibrary + DryRun, and confirmed the no-explicit-path case still behaves exactly as before. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017JGeTGbj4FKnmWHPGU82yk
|
This pull request has been automatically marked as stale due to inactivity. If no further activity occurs, it will be closed in 7 days. |
|
Sorry for not reviewing this, I thought I had reviewed/replied to it ages ago! I'll try to make some time to review it soon. |
|
@YoMosa2009 this is still a draft, do you intend to add more work or is this ready for a final review and merge? |
It's ready for final review and merge 👍 |
martindevans
left a comment
There was a problem hiding this comment.
Looks good, just one small change requested. Thanks for working on this and sorry for the long delay!
| loadedLibrary = new NativeLibraryFromPath(config.LibraryPath!); | ||
| return handle; | ||
| } | ||
|
|
There was a problem hiding this comment.
I think this should check description.AllowFallback and throw (or not) based on that.
If you specify:
WithLibrary(xyz)AllowFallback(false)
Then if xyz fails to load, it should throw rather than log and fallback.
(You'll probably need to lift AllowFallback out of the NET6 specific block in NativeLibraryConfig)
| catch (DllNotFoundException) | ||
| { |
There was a problem hiding this comment.
On glibc older than 2.34, libc.so.6 exists but doesn't export dlopen (it lives in libdl there), so calling DlopenLibC throws EntryPointNotFoundException instead of DllNotFoundException. If the requested file fails to load through libdl.so.2, the code falls through to the libc candidate and that exception escapes TryLoad, so the caller crashes instead of seeing the "Failed loading" path. Catching EntryPointNotFoundException here too keeps the fallthrough safe.
| catch (DllNotFoundException) | |
| { | |
| catch (Exception e) when (e is DllNotFoundException || e is EntryPointNotFoundException) | |
| { |
Draft PR for #960, opening it early so it's easier to review as code instead of me typing paragraphs in the issue.
What this does
NativeLibraryConfig.WithLibrary(path)(and theNativeLibraryConfigContainerversion) now works on netstandard2.0, not just net6.0+. If you call it, netstandard2.0 will load that exact file itself via a small P/Invoke shim (LoadLibraryon Windows,dlopenon Linux/mac) that stands in forSystem.Runtime.InteropServices.NativeLibrary, which doesn't exist on netstandard2.0.If you don't call
WithLibrary, netstandard2.0 behaves exactly like it does onmasterright now, still a no-op,DryRunstill returnsfalsewith anUnknownNativeLibrary. So this is purely additive, no behavior change for anyone not opting in (including Unity's own plugin loader, which this doesn't touch at all).What this deliberately does NOT do
The CUDA/Vulkan/AVX auto-detection backend-selection matrix (
DefaultNativeLibrarySelectingPolicy,NativeLibraryWithCuda/Vulkan/Avx, etc.) stays net6.0+ only for now. Porting that for real means replacingSystem.Runtime.Intrinsics.X86.*/Arm.ArmBaseandRuntimeInformation.RuntimeIdentifier, neither of which exist pre-.NET Core 3.0, and it's a much bigger surface to get right and test. Wanted to scope this PR down to what the issue actually asked for. Happy to tackle that separately if it's wanted.Files touched
NativeLibraryConfig.cs: movedWithLibrary/the library path out of theNET6_0_OR_GREATER-only partial class into the shared one.NativeLibraryUtils.cs: netstandard2.0 branch ofTryLoadLibrarynow checks for an explicit path and loads it if set.PlatformNativeLibrary.cs(new): theLoadLibrary/dlopenshim, only compiled#if !NET6_0_OR_GREATER.UnknownNativeLibrary.cs: doc comment tweak to reflect the new explicit-path case.Testing
LLama.Unittestonly targets net8.0, so it can't exercise the netstandard2.0-only code path directly. I built a throwaway net48 console app (forces netstandard2.0 asset selection through the project reference) and confirmed:WithLibrary(path).DryRun(out lib)actually loads a real Windows DLL and returnsNativeLibraryFromPath.WithLibrarystill gives the old no-op behavior (false/UnknownNativeLibrary/LibraryHasLoadedstaysfalse).I don't have a Linux/macOS box handy to exercise the
dlopenpath, so that part is reviewed but not runtime-tested by me. Flagging that clearly rather than claiming more than I've verified.Open question from the issue thread I'd still like input on: should the explicit-path case also preload
ggml-base/ggmlfrom the same folder like the net6.0 path does (llama.cpp ships split shared libs), or is it fine to assume netstandard2.0/Unity users are pointing at one self-contained binary for now? Went with the simpler version for this PR, easy to add if wanted.Closes/relates to #960.
🤖 Generated with Claude Code