From cbcbe14219835f0a05bd6377e8af737a4d591698 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Mon, 16 Jun 2025 13:23:05 +0000 Subject: [PATCH] Fixup into pyobject. (#622) --- bindings/python/src/lib.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index b5b0cc6..527767f 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -312,8 +312,8 @@ impl fmt::Display for Device { /// Parsing the device index. fn parse_device(name: &str) -> PyResult { 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.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()), + } } } -- 2.43.0