@@ -54,6 +54,26 @@ public static partial class torch
54
54
static bool nativeBackendCudaLoaded = false ;
55
55
56
56
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
+ }
57
77
58
78
internal static bool TryLoadNativeLibraryFromFile ( string path , StringBuilder trace ) {
59
79
bool ok ;
@@ -168,14 +188,14 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
168
188
169
189
if ( torchsharpLoc ! . Contains ( "torchsharp" ) && torchsharpLoc . Contains ( "lib" ) && Directory . Exists ( packagesDir ) && Directory . Exists ( torchsharpHome ) ) {
170
190
171
- var torchSharpVersion = Path . GetFileName ( torchsharpHome ) ; // really GetDirectoryName
172
-
191
+ var torchSharpVersion = NormalizeNuGetVersion ( Path . GetFileName ( torchsharpHome ) ) ;
192
+ var normalizedLibtorchPackageVersion = NormalizeNuGetVersion ( libtorchPackageVersion ) ;
173
193
if ( useCudaBackend ) {
174
194
var consolidatedDir = Path . Combine ( torchsharpLoc , $ "cuda-{ cudaVersion } ") ;
175
195
176
196
trace . AppendLine ( $ " Trying dynamic load for .NET/F# Interactive by consolidating native { cudaRootPackage } -* binaries to { consolidatedDir } ...") ;
177
197
178
- var cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , $ "{ cudaRootPackage } -*", libtorchPackageVersion , consolidatedDir , trace ) ;
198
+ var cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , $ "{ cudaRootPackage } -*", normalizedLibtorchPackageVersion , consolidatedDir , trace ) ;
179
199
if ( cudaOk ) {
180
200
cudaOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , "torchsharp" , torchSharpVersion , consolidatedDir , trace ) ;
181
201
if ( cudaOk ) {
@@ -193,7 +213,7 @@ private static void LoadNativeBackend(bool useCudaBackend, out StringBuilder? tr
193
213
194
214
trace . AppendLine ( $ " Trying dynamic load for .NET/F# Interactive by consolidating native { cpuRootPackage } -* binaries to { consolidatedDir } ...") ;
195
215
196
- var cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , cpuRootPackage , libtorchPackageVersion , consolidatedDir , trace ) ;
216
+ var cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , cpuRootPackage , normalizedLibtorchPackageVersion , consolidatedDir , trace ) ;
197
217
if ( cpuOk ) {
198
218
cpuOk = CopyNativeComponentsIntoSingleDirectory ( packagesDir , "torchsharp" , torchSharpVersion , consolidatedDir , trace ) ;
199
219
if ( cpuOk ) {
0 commit comments