From: Nicolas Patry Date: Tue, 27 Dec 2022 08:13:28 +0000 (+0100) Subject: Making SAFETENSORS_FAST_GPU=1 work on Windows again. (#140) X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a104c5e153364d774569f47e4d7666ba1664ab03;p=safetensors.git Making SAFETENSORS_FAST_GPU=1 work on Windows again. (#140) * Making Windows load cudart differently. * Remove unsafe. * Misplaced comment --- diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index c3607e5..7f065ad 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -300,17 +300,45 @@ fn find_cudart(module: &PyModule) -> Option { if var != "1" { return None; } - let path: std::path::PathBuf = module + + let path: PyResult = 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) }