@@ -54,6 +54,26 @@ public static partial class torch
5454 static bool nativeBackendCudaLoaded = false ;
5555
5656 public static string __version__ => libtorchPackageVersion ;
57+ public static string NormalizeNuGetVersion ( string versionString )
58+ {
59+ if ( string . IsNullOrWhiteSpace ( versionString ) )
60+ throw new ArgumentException ( $ "Invalid NuGet version: { versionString } . Version string is null, empty or only contains whitespaces") ;
61+
62+ string [ ] parts = versionString . Split ( '-' , '+' ) ;
63+ string [ ] versionParts = parts [ 0 ] . Split ( '.' ) ;
64+
65+ if ( versionParts . Length < 2 || versionParts . Length > 4 || ! versionParts . All ( v => int . TryParse ( v , out _ ) ) )
66+ throw new ArgumentException ( $ "Invalid NuGet version: { versionString } . Please check: https://learn.microsoft.com/en-us/nuget/concepts/package-versioning") ;
67+
68+ string normalizedVersion = versionParts [ 0 ] + "." + versionParts [ 1 ] ;
69+ if ( versionParts . Length > 2 ) normalizedVersion += "." + versionParts [ 2 ] ;
70+ if ( versionParts . Length > 3 && int . Parse ( versionParts [ 3 ] ) != 0 ) normalizedVersion += "." + versionParts [ 3 ] ;
71+
72+ if ( parts . Length > 1 )
73+ normalizedVersion += "-" + parts [ 1 ] ;
74+
75+ return normalizedVersion ;
76+ }
5777
5878 internal static bool TryLoadNativeLibraryFromFile ( string path , StringBuilder trace ) {
5979 bool ok ;
@@ -168,14 +188,14 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
168188
169189 if ( torchsharpLoc ! . Contains ( "torchsharp" ) && torchsharpLoc . Contains ( "lib" ) && Directory . Exists ( packagesDir ) && Directory . Exists ( torchsharpHome ) ) {
170190
171- var torchSharpVersion = Path . GetFileName ( torchsharpHome ) ; // really GetDirectoryName
172-
191+ var torchSharpVersion = NormalizeNuGetVersion ( Path . GetFileName ( torchsharpHome ) ) ;
192+ var normalizedLibtorchPackageVersion = NormalizeNuGetVersion ( libtorchPackageVersion ) ;
173193 if ( useCudaBackend ) {
174194 var consolidatedDir = Path . Combine ( torchsharpLoc , $ "cuda-{ cudaVersion } ") ;
175195
176196 trace . AppendLine ( $ " Trying dynamic load for .NET/F# Interactive by consolidating native { cudaRootPackage } -* binaries to { consolidatedDir } ...") ;
177197
178- var cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , $ "{ cudaRootPackage } -*", libtorchPackageVersion , consolidatedDir , trace ) ;
198+ var cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , $ "{ cudaRootPackage } -*", normalizedLibtorchPackageVersion , consolidatedDir , trace ) ;
179199 if ( cudaOk ) {
180200 cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , "torchsharp" , torchSharpVersion , consolidatedDir , trace ) ;
181201 if ( cudaOk ) {
@@ -193,7 +213,7 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
193213
194214 trace . AppendLine ( $ " Trying dynamic load for .NET/F# Interactive by consolidating native { cpuRootPackage } -* binaries to { consolidatedDir } ...") ;
195215
196- var cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , cpuRootPackage , libtorchPackageVersion , consolidatedDir , trace ) ;
216+ var cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , cpuRootPackage , normalizedLibtorchPackageVersion , consolidatedDir , trace ) ;
197217 if ( cpuOk ) {
198218 cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , "torchsharp" , torchSharpVersion , consolidatedDir , trace ) ;
199219 if ( cpuOk ) {
0 commit comments