]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Fixup into pyobject. (#622)
authorNicolas Patry <patry.nicolas@protonmail.com>
Mon, 16 Jun 2025 13:23:05 +0000 (13:23 +0000)
committerGitHub <noreply@github.com>
Mon, 16 Jun 2025 13:23:05 +0000 (15:23 +0200)
bindings/python/src/lib.rs

index b5b0cc68b2d863572920a0b9e3f50c81e532dc41..527767f4e12a783226ea728ffe3bc5c5b9e2458a 100644 (file)
@@ -312,8 +312,8 @@ impl fmt::Display for Device {
 /// Parsing the device index.
 fn parse_device(name: &str) -> PyResult<usize> {
     let tokens: Vec<_> = name.split(':').collect();
-    if let Ok([_, token]) = <[_; 2]>::try_from(tokens) {
-        Ok(token.parse()?)
+    if tokens.len() == 2 {
+        Ok(tokens[1].parse()?)
     } else {
         Err(SafetensorError::new_err(format!(
             "device {name} is invalid"
@@ -357,9 +357,17 @@ impl<'py> IntoPyObject<'py> for Device {
     type Error = std::convert::Infallible;
 
     fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
-        self.to_string()
-            .into_pyobject(py)
-            .map(pyo3::BoundObject::into_any)
+        match self {
+            Device::Cpu => "cpu".into_pyobject(py).map(|x| x.into_any()),
+            Device::Cuda(n) => format!("cuda:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Mps => "mps".into_pyobject(py).map(|x| x.into_any()),
+            Device::Npu(n) => format!("npu:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Xpu(n) => format!("xpu:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Xla(n) => format!("xla:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Mlu(n) => format!("mlu:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Hpu(n) => format!("hpu:{n}").into_pyobject(py).map(|x| x.into_any()),
+            Device::Anonymous(n) => n.into_pyobject(py).map(|x| x.into_any()),
+        }
     }
 }