]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Making SAFETENSORS_FAST_GPU=1 work on Windows again. (#140)
authorNicolas Patry <patry.nicolas@protonmail.com>
Tue, 27 Dec 2022 08:13:28 +0000 (09:13 +0100)
committerGitHub <noreply@github.com>
Tue, 27 Dec 2022 08:13:28 +0000 (09:13 +0100)
* Making Windows load cudart differently.

* Remove unsafe.

* Misplaced comment

bindings/python/src/lib.rs

index c3607e5292f776314942d335b446c1a85906c543..7f065ad7a1576784c036aeb7094c22a4d859c45c 100644 (file)
@@ -300,17 +300,45 @@ fn find_cudart(module: &PyModule) -> Option<Library> {
     if var != "1" {
         return None;
     }
-    let path: std::path::PathBuf = module
+
+    let path: PyResult<std::path::PathBuf> = module
         .getattr(intern!(module.py(), "_C"))
         .ok()?
         .getattr(intern!(module.py(), "__file__"))
         .ok()?
-        .extract()
-        .ok()?;
-    // SAFETY: This is unsafe because the library might run arbitrary code
-    // So it's really important to make sure we are targeting the correct
-    // library.
-    let lib = unsafe { Library::new(path).ok()? };
+        .extract();
+
+    #[cfg(not(target_os = "windows"))]
+    let lib = {
+        let path = path.ok()?;
+        // SAFETY: This is unsafe because the library might run arbitrary code
+        // So it's really important to make sure we are targeting the correct
+        // library.
+        unsafe { Library::new(path).ok()? }
+    };
+
+    #[cfg(target_os = "windows")]
+    let lib = {
+        let mut path = path.ok()?;
+        path.pop();
+        path.push("lib");
+        let paths = std::fs::read_dir(path).ok()?;
+        let cudart_paths: Vec<_> = paths
+            .into_iter()
+            .filter_map(|p| {
+                let p = p.ok()?.path();
+                let filename = p.file_name()?.to_string_lossy();
+                if filename.starts_with("cudart") {
+                    Some(p)
+                } else {
+                    None
+                }
+            })
+            .collect();
+        let cudart_path = cudart_paths.get(0)?;
+        Library::from(libloading::os::windows::Library::open_already_loaded(cudart_path).ok()?)
+    };
+
     Some(lib)
 }