]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Add support for xla devices (#494)
authorEric Meier <ericmeier@protonmail.com>
Tue, 30 Jul 2024 17:29:22 +0000 (10:29 -0700)
committerGitHub <noreply@github.com>
Tue, 30 Jul 2024 17:29:22 +0000 (19:29 +0200)
* Add support for xla devices

* :broom:

bindings/python/src/lib.rs

index e0a52524407b16828c9638fe840f7350ae028bc5..2c05ad56fa110544dd4afb373f44a88d5a18c77b 100644 (file)
@@ -266,6 +266,7 @@ enum Device {
     Mps,
     Npu(usize),
     Xpu(usize),
+    Xla(usize),
 }
 
 impl<'source> FromPyObject<'source> for Device {
@@ -277,6 +278,7 @@ impl<'source> FromPyObject<'source> for Device {
                 "mps" => Ok(Device::Mps),
                 "npu" => Ok(Device::Npu(0)),
                 "xpu" => Ok(Device::Xpu(0)),
+                "xla" => Ok(Device::Xla(0)),
                 name if name.starts_with("cuda:") => {
                     let tokens: Vec<_> = name.split(':').collect();
                     if tokens.len() == 2 {
@@ -310,6 +312,17 @@ impl<'source> FromPyObject<'source> for Device {
                         )))
                     }
                 }
+                name if name.starts_with("xla:") => {
+                    let tokens: Vec<_> = name.split(':').collect();
+                    if tokens.len() == 2 {
+                        let device: usize = tokens[1].parse()?;
+                        Ok(Device::Xla(device))
+                    } else {
+                        Err(SafetensorError::new_err(format!(
+                            "device {name} is invalid"
+                        )))
+                    }
+                }
                 name => Err(SafetensorError::new_err(format!(
                     "device {name} is invalid"
                 ))),
@@ -330,6 +343,7 @@ impl IntoPy<PyObject> for Device {
             Device::Mps => "mps".into_py(py),
             Device::Npu(n) => format!("npu:{n}").into_py(py),
             Device::Xpu(n) => format!("xpu:{n}").into_py(py),
+            Device::Xla(n) => format!("xla:{n}").into_py(py),
         }
     }
 }