]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Pass device to torch.asarray in get_tensor (#588)
authorMick van Gelderen <mickvangelderen@gmail.com>
Mon, 17 Mar 2025 09:14:17 +0000 (02:14 -0700)
committerGitHub <noreply@github.com>
Mon, 17 Mar 2025 09:14:17 +0000 (10:14 +0100)
* Pass device to torch.asarray in get_tensor

* Fixing default device.

---------

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

index 4d1d5f82f29004819d186934600b7a90912c769e..b6a0f49c16dc0bf06ae3980188f0d7dec474787e 100644 (file)
@@ -500,7 +500,12 @@ impl Open {
                     let torch = get_module(py, &TORCH_MODULE)?;
                     let dtype: PyObject = get_pydtype(torch, info.dtype, false)?;
                     let torch_uint8: PyObject = get_pydtype(torch, Dtype::U8, false)?;
-                    let kwargs = [(intern!(py, "dtype"), torch_uint8)].into_py_dict(py)?;
+                    let device: PyObject = self.device.clone().into_pyobject(py)?.into();
+                    let kwargs = [
+                        (intern!(py, "dtype"), torch_uint8),
+                        (intern!(py, "device"), device),
+                    ]
+                    .into_py_dict(py)?;
                     let view_kwargs = [(intern!(py, "dtype"), dtype)].into_py_dict(py)?;
                     let shape = info.shape.to_vec();
                     let shape: PyObject = shape.into_pyobject(py)?.into();
@@ -560,13 +565,7 @@ impl Open {
                     }
 
                     tensor = tensor.getattr(intern!(py, "reshape"))?.call1((shape,))?;
-                    if self.device != Device::Cpu {
-                        let device: PyObject = self.device.clone().into_pyobject(py)?.into();
-                        let kwargs = PyDict::new(py);
-                        tensor = tensor.call_method("to", (device,), Some(&kwargs))?;
-                    }
                     Ok(tensor.into_pyobject(py)?.into())
-                    // torch.asarray(storage[start + n : stop + n], dtype=torch.uint8).view(dtype=dtype).reshape(shape)
                 })
             }
         }
index fc61644570d2706a5ea6cdf1f7fabad1a815e0c3..10e8cc0565c45174a1db8c5a71336b3d39aaa147 100644 (file)
@@ -173,7 +173,7 @@ class TorchTestCase(unittest.TestCase):
     def test_hpu(self):
         # must be run to load torch with Intel Gaudi bindings
         try:
-            import habana_frameworks.torch.core as htcore
+            import habana_frameworks.torch.core as htcore  # noqa: F401
         except ImportError:
             self.skipTest("HPU is not available")
 
@@ -255,6 +255,16 @@ class LoadTestCase(unittest.TestCase):
             self.assertTrue(torch.allclose(v, tv))
             self.assertEqual(v.device, torch.device("cpu"))
 
+    @unittest.skipIf(not torch.cuda.is_available(), "Cuda is not available")
+    def test_deserialization_device(self):
+        with torch.device("cuda:0"):
+            weights = load_file(self.sf_filename)
+            self.assertEqual(weights["test"].device, torch.device("cpu"))
+
+        torch.set_default_device(torch.device("cuda:0"))
+        weights = load_file(self.sf_filename)
+        self.assertEqual(weights["test"].device, torch.device("cpu"))
+
     @unittest.skipIf(not torch.cuda.is_available(), "Cuda is not available")
     def test_deserialization_safe_gpu(self):
         # First time to hit disk