]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Add support for Intel Gaudi hpu accelerators (#566)
authorAsaf Karnieli <akarnieli@habana.ai>
Tue, 4 Feb 2025 08:06:26 +0000 (10:06 +0200)
committerGitHub <noreply@github.com>
Tue, 4 Feb 2025 08:06:26 +0000 (09:06 +0100)
* Add support for Intel Gaudi hpu accelerators

* Fixing the `find_spec` dep.

* Fixing unused import.

---------

Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
bindings/python/src/lib.rs
bindings/python/tests/test_pt_comparison.py

index 2ecd9a7fdc7c53f49232805e33d4b3378ad04d20..c9ac26c39aa911dad950fcb043b57723fda903dc 100644 (file)
@@ -267,7 +267,8 @@ enum Device {
     Xpu(usize),
     Xla(usize),
     Mlu(usize),
-    /// User didn't specify acceletor, torch
+    Hpu,
+    /// User didn't specify accelerator, torch
     /// is responsible for choosing.
     Anonymous(usize),
 }
@@ -296,6 +297,7 @@ impl<'source> FromPyObject<'source> for Device {
                 "xpu" => Ok(Device::Xpu(0)),
                 "xla" => Ok(Device::Xla(0)),
                 "mlu" => Ok(Device::Mlu(0)),
+                "hpu" => Ok(Device::Hpu),
                 name if name.starts_with("cuda:") => parse_device(name).map(Device::Cuda),
                 name if name.starts_with("npu:") => parse_device(name).map(Device::Npu),
                 name if name.starts_with("xpu:") => parse_device(name).map(Device::Xpu),
@@ -327,6 +329,7 @@ impl<'py> IntoPyObject<'py> for Device {
             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 => "hpu".into_pyobject(py).map(|x| x.into_any()),
             Device::Anonymous(n) => n.into_pyobject(py).map(|x| x.into_any()),
         }
     }
index 6e569f50ee6c568e0a54c2a7fa2f8205f9d41943..fc61644570d2706a5ea6cdf1f7fabad1a815e0c3 100644 (file)
@@ -170,6 +170,24 @@ class TorchTestCase(unittest.TestCase):
         for k, v in reloaded.items():
             self.assertTrue(torch.allclose(data[k], reloaded[k]))
 
+    def test_hpu(self):
+        # must be run to load torch with Intel Gaudi bindings
+        try:
+            import habana_frameworks.torch.core as htcore
+        except ImportError:
+            self.skipTest("HPU is not available")
+
+        data = {
+            "test1": torch.zeros((2, 2), dtype=torch.float32).to("hpu"),
+            "test2": torch.zeros((2, 2), dtype=torch.float16).to("hpu"),
+        }
+        local = "./tests/data/out_safe_pt_mmap_small_hpu.safetensors"
+        save_file(data, local)
+
+        reloaded = load_file(local, device="hpu")
+        for k, v in reloaded.items():
+            self.assertTrue(torch.allclose(data[k], reloaded[k]))
+
     @unittest.skipIf(not torch.cuda.is_available(), "Cuda is not available")
     def test_anonymous_accelerator(self):
         data = {