]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Some PyO3 specifics. (#35)
authorNicolas Patry <patry.nicolas@protonmail.com>
Wed, 26 Oct 2022 13:49:10 +0000 (15:49 +0200)
committerGitHub <noreply@github.com>
Wed, 26 Oct 2022 13:49:10 +0000 (15:49 +0200)
* Faster gpu load.

* Use cuda-sys directly.

* For slice too.

* Fun unsafe.

* Reduce unsafe.

* Removing CPU unsafe.

* Using shared `cuda-sys` (temporary, we need to use torch's cuda version).

* Tmp rework

* Cleaner device.

* Adding some statistics...

* Warmup steps.

* Removing unsafe GPU accesses.

* Removing dead code.

* Removing libloading.

* Accepting `int` as device.

bindings/python/src/lib.rs
bindings/python/tests/test_pt_comparison.py

index d280c58e1832dc594662d40a6d7e1fc8e5c747f9..d2e204d02c60571ec6e26442bdf27a60eab0ae7b 100644 (file)
@@ -154,26 +154,75 @@ impl<'source> FromPyObject<'source> for Framework {
     }
 }
 
+#[derive(Debug, Clone, PartialEq, Eq)]
+enum Device {
+    Cpu,
+    Cuda(usize),
+    Mps,
+}
+
+impl<'source> FromPyObject<'source> for Device {
+    fn extract(ob: &'source PyAny) -> PyResult<Self> {
+        if let Ok(name) = ob.extract::<String>() {
+            match &name[..] {
+                "cpu" => Ok(Device::Cpu),
+                "cuda" => Ok(Device::Cuda(0)),
+                "mps" => Ok(Device::Mps),
+                name if name.starts_with("cuda:") => {
+                    let tokens: Vec<_> = name.split(':').collect();
+                    if tokens.len() == 2 {
+                        let device: usize = tokens[1].parse()?;
+                        Ok(Device::Cuda(device))
+                    } else {
+                        Err(exceptions::PyException::new_err(format!(
+                            "device {name} is invalid"
+                        )))
+                    }
+                }
+                name => Err(exceptions::PyException::new_err(format!(
+                    "device {name} is invalid"
+                ))),
+            }
+        } else if let Ok(number) = ob.extract::<usize>() {
+            Ok(Device::Cuda(number))
+        } else {
+            Err(exceptions::PyException::new_err(format!(
+                "device {ob} is invalid"
+            )))
+        }
+    }
+}
+
+impl IntoPy<PyObject> for Device {
+    fn into_py(self, py: Python<'_>) -> PyObject {
+        match self {
+            Device::Cpu => "cpu".into_py(py),
+            Device::Cuda(n) => format!("cuda:{n}").into_py(py),
+            Device::Mps => "mps".into_py(py),
+        }
+    }
+}
+
 #[pyclass]
 #[allow(non_camel_case_types)]
 struct safe_open {
     metadata: Metadata,
     offset: usize,
     framework: Framework,
-    device: String,
+    device: Device,
     mmap: Arc<Mmap>,
 }
 
 #[pymethods]
 impl safe_open {
     #[new]
-    fn new(filename: &str, framework: Framework, device: Option<String>) -> PyResult<Self> {
+    fn new(filename: &str, framework: Framework, device: Option<Device>) -> PyResult<Self> {
         let file = File::open(filename)?;
-        let device = device.unwrap_or_else(|| "cpu".to_string());
+        let device = device.unwrap_or(Device::Cpu);
 
-        if device != "cpu" && framework != Framework::Pytorch {
+        if device != Device::Cpu && framework != Framework::Pytorch {
             return Err(exceptions::PyException::new_err(format!(
-                "Device {device} is not support for framework {framework:?}",
+                "Device {device:?} is not support for framework {framework:?}",
             )));
         }
 
@@ -207,28 +256,21 @@ impl safe_open {
     }
 
     pub fn get_tensor(&self, name: &str) -> PyResult<PyObject> {
-        if let Some(info) = self.metadata.tensors().get(name) {
-            let data =
-                &self.mmap[info.data_offsets.0 + self.offset..info.data_offsets.1 + self.offset];
+        let info = self.metadata.tensors().get(name).ok_or_else(|| {
+            exceptions::PyException::new_err(format!("File does not contain tensor {name}",))
+        })?;
 
-            let tensor = TensorView::new(info.dtype, info.shape.clone(), data).map_err(|e| {
-                exceptions::PyException::new_err(format!("Error when creating TensorView {:?}", e))
-            })?;
-            let array: PyObject =
-                Python::with_gil(|py| PyByteArray::new(py, tensor.get_data()).into_py(py));
-
-            create_tensor(
-                &self.framework,
-                info.dtype,
-                &info.shape,
-                array,
-                &self.device,
-            )
-        } else {
-            Err(exceptions::PyException::new_err(format!(
-                "File does not contain tensor {name}",
-            )))
-        }
+        let data = &self.mmap[info.data_offsets.0 + self.offset..info.data_offsets.1 + self.offset];
+
+        let array: PyObject = Python::with_gil(|py| PyByteArray::new(py, data).into_py(py));
+
+        create_tensor(
+            &self.framework,
+            info.dtype,
+            &info.shape,
+            array,
+            &self.device,
+        )
     }
 
     pub fn get_slice(&self, name: &str) -> PyResult<PySafeSlice> {
@@ -259,7 +301,7 @@ struct PySafeSlice {
     info: TensorInfo,
     framework: Framework,
     offset: usize,
-    device: String,
+    device: Device,
     mmap: Arc<Mmap>,
 }
 
@@ -277,6 +319,7 @@ impl PySafeSlice {
         let shape: PyObject = shape.into_py(py);
         Ok(shape)
     }
+
     pub fn __getitem__(&self, slices: Slice) -> PyResult<PyObject> {
         let slices: Vec<&PySlice> = match slices {
             Slice::Slice(slice) => vec![slice],
@@ -293,6 +336,7 @@ impl PySafeSlice {
             .into_iter()
             .map(slice_to_indexer)
             .collect::<Result<_, _>>()?;
+
         let iterator = tensor.get_sliced_data(slices.clone()).map_err(|e| {
             exceptions::PyException::new_err(format!(
                 "Error during slicing {slices:?} vs {:?}:  {:?}",
@@ -303,18 +347,18 @@ impl PySafeSlice {
 
         let mut offset = 0;
         let length = iterator.remaining_byte_len();
-        let array: PyObject = Python::with_gil(|py| {
-            PyByteArray::new_with(py, length, |bytes: &mut [u8]| {
+
+        let array: PyObject = Python::with_gil(|py| -> PyResult<PyObject> {
+            Ok(PyByteArray::new_with(py, length, |bytes: &mut [u8]| {
                 for slice in iterator {
                     let len = slice.len();
                     bytes[offset..offset + slice.len()].copy_from_slice(slice);
                     offset += len;
                 }
                 Ok(())
-            })
-            .unwrap()
-            .into_py(py)
-        });
+            })?
+            .into_py(py))
+        })?;
         create_tensor(
             &self.framework,
             self.info.dtype,
@@ -330,56 +374,62 @@ fn create_tensor(
     dtype: Dtype,
     shape: &[usize],
     array: PyObject,
-    device: &str,
+    device: &Device,
 ) -> PyResult<PyObject> {
     match framework {
-        Framework::Pytorch | Framework::Numpy => {
+        Framework::Pytorch | Framework::Numpy => Python::with_gil(|py| -> PyResult<PyObject> {
             let module_name = match framework {
-                Framework::Numpy => "numpy",
-                Framework::Pytorch => "torch",
+                Framework::Numpy => intern!(py, "numpy"),
+                Framework::Pytorch => intern!(py, "torch"),
                 _ => unreachable!(),
             };
-            Python::with_gil(|py| {
-                let module = PyModule::import(py, module_name)?;
-                let frombuffer = module.getattr("frombuffer")?;
-                let dtype: PyObject = get_pydtype(module, dtype)?;
-                let kwargs = [("buffer", array), ("dtype", dtype)].into_py_dict(py);
-                let tensor = frombuffer.call((), Some(kwargs))?;
-                let shape = shape.to_vec();
-                let shape: PyObject = shape.into_py(py);
-                let mut tensor: &PyAny = tensor.getattr("reshape")?.call1((shape,))?;
-                if device != "cpu" {
-                    let device: PyObject = device.into_py(py);
-                    tensor = tensor.getattr("to")?.call1((device,))?;
-                }
-                let tensor: PyObject = tensor.into_py(py);
-                Ok(tensor)
-            })
-        }
+            let module = PyModule::import(py, module_name)?;
+            let frombuffer = module.getattr(intern!(py, "frombuffer"))?;
+            let dtype: PyObject = get_pydtype(module, dtype)?;
+            let kwargs = [
+                (intern!(py, "buffer"), array),
+                (intern!(py, "dtype"), dtype),
+            ]
+            .into_py_dict(py);
+            let tensor = frombuffer.call((), Some(kwargs))?;
+            let shape = shape.to_vec();
+            let shape: PyObject = shape.into_py(py);
+            let mut tensor: &PyAny = tensor.getattr(intern!(py, "reshape"))?.call1((shape,))?;
+            if device != &Device::Cpu {
+                let device: PyObject = device.clone().into_py(py);
+                let kwargs = PyDict::new(py);
+                tensor = tensor
+                    .getattr(intern!(py, "to"))?
+                    .call((device,), Some(kwargs))?;
+            }
+            let tensor = tensor.into_py(py);
+            Ok(tensor)
+        }),
         framework => todo!("{framework:?}"),
     }
 }
 
 fn get_pydtype(module: &PyModule, dtype: Dtype) -> PyResult<PyObject> {
-    let dtype: PyObject = match dtype {
-        Dtype::F64 => module.getattr("float64")?.into(),
-        Dtype::F32 => module.getattr("float32")?.into(),
-        Dtype::BF16 => module.getattr("bfloat16")?.into(),
-        Dtype::F16 => module.getattr("float16")?.into(),
-        Dtype::U64 => module.getattr("uint64")?.into(),
-        Dtype::I64 => module.getattr("int64")?.into(),
-        Dtype::U32 => module.getattr("uint32")?.into(),
-        Dtype::I32 => module.getattr("int32")?.into(),
-        Dtype::U16 => module.getattr("uint16")?.into(),
-        Dtype::I16 => module.getattr("int16")?.into(),
-        Dtype::U8 => module.getattr("uint8")?.into(),
-        Dtype::I8 => module.getattr("int8")?.into(),
-        Dtype::BOOL => module.getattr("bool")?.into(),
-        dtype => todo!("Dtype {dtype:?}"),
-    };
-    Ok(dtype)
+    Python::with_gil(|py| {
+        let dtype: PyObject = match dtype {
+            Dtype::F64 => module.getattr(intern!(py, "float64"))?.into(),
+            Dtype::F32 => module.getattr(intern!(py, "float32"))?.into(),
+            Dtype::BF16 => module.getattr(intern!(py, "bfloat16"))?.into(),
+            Dtype::F16 => module.getattr(intern!(py, "float16"))?.into(),
+            Dtype::U64 => module.getattr(intern!(py, "uint64"))?.into(),
+            Dtype::I64 => module.getattr(intern!(py, "int64"))?.into(),
+            Dtype::U32 => module.getattr(intern!(py, "uint32"))?.into(),
+            Dtype::I32 => module.getattr(intern!(py, "int32"))?.into(),
+            Dtype::U16 => module.getattr(intern!(py, "uint16"))?.into(),
+            Dtype::I16 => module.getattr(intern!(py, "int16"))?.into(),
+            Dtype::U8 => module.getattr(intern!(py, "uint8"))?.into(),
+            Dtype::I8 => module.getattr(intern!(py, "int8"))?.into(),
+            Dtype::BOOL => module.getattr(intern!(py, "bool"))?.into(),
+            dtype => todo!("Dtype {dtype:?}"),
+        };
+        Ok(dtype)
+    })
 }
-
 /// A Python module implemented in Rust.
 #[pymodule]
 fn safetensors_rust(_py: Python, m: &PyModule) -> PyResult<()> {
index 6bd000c122b6f2c47ae64e0c6c5febdbacce1a00..f329dfe462daf040f75e0d8e07dc0bc137b1eaf2 100644 (file)
@@ -53,38 +53,78 @@ class SpeedTestCase(unittest.TestCase):
             save_file(data.copy(), self.local)
 
     def test_deserialization_safe(self):
-        start = datetime.datetime.now()
+        W = 1
+        N = 1
+
+        for i in range(W):
+            torch.load(self.filename)
+        pt_timing = datetime.timedelta(0)
+        for i in range(N):
+            start = datetime.datetime.now()
+            tweights = torch.load(self.filename)
+            pt_time = datetime.datetime.now() - start
+            pt_timing += pt_time
+        pt_time = pt_timing / N
+
+        for i in range(W):
+            load_file(self.local)
+        safe_timing = datetime.timedelta(0)
+        for i in range(N):
+            start = datetime.datetime.now()
+            weights = load_file(self.local)
+            safe_time = datetime.datetime.now() - start
+            safe_timing += safe_time
+        safe_time = safe_timing / N
+
+        print()
+        print(f"Deserialization (Safe) took {safe_time}")
+        print(f"Deserialization (PT) took {pt_time} (Safe is {pt_time/safe_time} faster)")
+        for k, v in weights.items():
+            tv = tweights[k]
+            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_safe_gpu(self):
         # First time to hit disk
-        load_file(self.local)
+        load_file(self.local, device=0)
+        load_file(self.local, device="cuda:0")
         # Second time we should be in disk cache
         start = datetime.datetime.now()
-        weights = load_file(self.local)
+        weights = load_file(self.local, device="cuda:0")
         safe_time = datetime.datetime.now() - start
-        torch.load(self.filename)
+
+        # First time to hit disk
+        torch.load(self.filename, map_location="cuda:0")
         start = datetime.datetime.now()
-        tweights = torch.load(self.filename)
+        tweights = torch.load(self.filename, map_location="cuda:0")
         pt_time = datetime.datetime.now() - start
         print()
-        print(f"Deserialization (Safe) took {safe_time}")
-        print(f"Deserialization (PT) took {pt_time} (Safe is {pt_time/safe_time} faster)")
+        print(f"Deserialization (Safe - GPU) took {safe_time}")
+        print(f"Deserialization (PT - GPU) took {pt_time} (Safe is {pt_time/safe_time} faster)")
         for k, v in weights.items():
             tv = tweights[k]
             self.assertTrue(torch.allclose(v, tv))
-            self.assertEqual(v.device, torch.device("cpu"))
+            self.assertEqual(v.device, torch.device("cuda:0"))
 
     @unittest.skipIf(not torch.cuda.is_available(), "Cuda is not available")
-    def test_deserialization_safe_gpu(self):
+    def test_deserialization_safe_gpu_slice(self):
         # First time to hit disk
         load_file(self.local, device="cuda:0")
         # Second time we should be in disk cache
         start = datetime.datetime.now()
-        weights = load_file(self.local, device="cuda:0")
+
+        weights = {}
+        with safe_open(self.local, framework="pt", device="cuda:0") as f:
+            for k in f.keys():
+                weights[k] = f.get_slice(k)[:1]
         safe_time = datetime.datetime.now() - start
 
         # First time to hit disk
         torch.load(self.filename, map_location="cuda:0")
         start = datetime.datetime.now()
         tweights = torch.load(self.filename, map_location="cuda:0")
+        tweights = {k: v[:1] for k, v in tweights.items()}
         pt_time = datetime.datetime.now() - start
         print()
         print(f"Deserialization (Safe - GPU) took {safe_time}")
@@ -94,6 +134,30 @@ class SpeedTestCase(unittest.TestCase):
             self.assertTrue(torch.allclose(v, tv))
             self.assertEqual(v.device, torch.device("cuda:0"))
 
+    @unittest.skipIf(torch.cuda.device_count() < 2, "Only 1 device available")
+    def test_deserialization_safe_device_1(self):
+        # First time to hit disk
+        load_file(self.local, device=1)
+        load_file(self.local, device="cuda:1")
+        # Second time we should be in disk cache
+        start = datetime.datetime.now()
+
+        weights = load_file(self.local, device="cuda:1")
+        safe_time = datetime.datetime.now() - start
+
+        # First time to hit disk
+        torch.load(self.filename, map_location="cuda:1")
+        start = datetime.datetime.now()
+        tweights = torch.load(self.filename, map_location="cuda:1")
+        pt_time = datetime.datetime.now() - start
+        print()
+        print(f"Deserialization (Safe - GPU) took {safe_time}")
+        print(f"Deserialization (PT - GPU) took {pt_time} (Safe is {pt_time/safe_time} faster)")
+        for k, v in weights.items():
+            tv = tweights[k]
+            self.assertTrue(torch.allclose(v, tv))
+            self.assertEqual(v.device, torch.device("cuda:1"))
+
     def test_serialization_safe(self):
         data = torch.load(self.filename, map_location="cpu")
         start = datetime.datetime.now()
@@ -115,7 +179,7 @@ class SliceTestCase(unittest.TestCase):
     def setUp(self):
         self.tensor = torch.arange(6, dtype=torch.float32).reshape((1, 2, 3))
         self.data = {"test": self.tensor}
-        self.local = "./tests/data/out_safe_pt_mmap.safetensors"
+        self.local = "./tests/data/out_safe_pt_mmap_slice.safetensors"
         # Need to copy since that call mutates the tensors to numpy
         save_file(self.data.copy(), self.local)