From be0683f561f1f742c9253d46325945f6251a0770 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Wed, 2 Nov 2022 16:53:50 +0100 Subject: [PATCH] Make GPU loading faster by removing all extra CPU copies. (#33) * 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. Revert "Removing unsafe GPU accesses." This reverts commit 5325ba2b73fffc16416130193da1690353e0a7db. Unsafe comments. Using GILOnceCell for module reuse. Finding the lib through the real python workload. Still requires transitive library parsing. Stable with global lib ref. Abort. Weird bug on torch 1.13. We **need** to get the data_ptr within the loop ? * Fixing speedup by loading directly the loaded C library within Python. * Cleanup. * Finishing touches. * Remove some code duplication. Some very weird errors in calling the `cudaMemcpy` that fail depending on order. Even aliasing the `Symbol` seem to be unsafe. * Adding some comments. * Better signature. Static symbol instead of lib. --- bindings/python/Cargo.toml | 1 + bindings/python/src/lib.rs | 267 +++++++++++++++++++++++++++++++++---- 2 files changed, 241 insertions(+), 27 deletions(-) diff --git a/bindings/python/Cargo.toml b/bindings/python/Cargo.toml index 488e376..76f2241 100644 --- a/bindings/python/Cargo.toml +++ b/bindings/python/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ["cdylib"] pyo3 = { version = "0.17.2", features = ["extension-module"] } memmap = "0.7" serde_json = "1.0" +libloading = "0.7" [dependencies.safetensors] version = "*" diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 91f8eee..02ad17d 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -1,5 +1,6 @@ #![deny(missing_docs)] //! Dummy doc +use libloading::{Library, Symbol}; use memmap::{Mmap, MmapOptions}; use pyo3::exceptions; use pyo3::once_cell::GILOnceCell; @@ -16,8 +17,23 @@ use std::iter::FromIterator; use std::ops::Bound; use std::sync::Arc; +#[repr(C)] +enum cudaMemcpyKind { + _HostToHost = 0, + HostToDevice = 1, + _DeviceToHost = 2, + _Default = 3, +} + +type MemcpyFn = unsafe extern "C" fn( + dst: *mut std::ffi::c_void, + src: *const std::ffi::c_void, + count: usize, + kind: cudaMemcpyKind, +) -> std::ffi::c_uint; static TORCH_MODULE: GILOnceCell> = GILOnceCell::new(); static NUMPY_MODULE: GILOnceCell> = GILOnceCell::new(); +static CUDA_MEMCPY: GILOnceCell>> = GILOnceCell::new(); fn prepare(tensor_dict: HashMap) -> PyResult>> { let mut tensors = BTreeMap::new(); @@ -266,6 +282,126 @@ struct safe_open { mmap: Arc, } +fn create_empty_tensor_pt<'a>( + module: &'a PyModule, + shape: &[usize], + dtype: Dtype, + device: &Device, +) -> PyResult<&'a PyAny> { + let py = module.py(); + let shape = shape.to_vec(); + let empty = module.getattr(intern!(py, "empty"))?; + let dtype: PyObject = get_pydtype(module, dtype)?; + let shape: PyObject = shape.into_py(py); + let device: PyObject = device.clone().into_py(py); + let kwargs = [ + (intern!(py, "dtype"), dtype), + (intern!(py, "device"), device), + ] + .into_py_dict(py); + let tensor = empty.call((shape,), Some(kwargs))?; + + Ok(tensor) +} + +fn find_cudart(module: &PyModule) -> Option { + let var = std::env::var("SAFETENSORS_FAST_GPU").ok()?; + if var != "1" { + return None; + } + let path: std::path::PathBuf = module + .getattr(intern!(module.py(), "_C")) + .ok()? + .getattr(intern!(module.py(), "__file__")) + .ok()? + .extract() + .ok()?; + // SAFETY: This is unsafe because the library might run arbitrary code + // So it's really important to make sure we are targeting the correct + // library. + let lib = unsafe { Library::new(path).ok()? }; + Some(lib) +} + +fn find_cuda_memcpy<'py>(module: &PyModule) -> Option> { + let cudart = find_cudart(module)?; + // Leaking the library so that the reference becomes static + let cudart = Box::leak(Box::new(cudart)); + // SAFETY: This is unsafe because the library might run arbitrary code + // So it's really important to make sure we are targeting the correct + // library. + let cuda_memcpy = unsafe { cudart.get(b"cudaMemcpy").ok() }; + cuda_memcpy +} + +fn create_cuda_unsafe_tensor( + module: &PyModule, + cuda_memcpy: &Symbol, + info: &TensorInfo, + device: &Device, + data: &[u8], +) -> PyResult { + let tensor = create_empty_tensor_pt(module, &info.shape, info.dtype, device)?; + + let data_ptr_fn = tensor.getattr("data_ptr")?; + let data_ptr: usize = data_ptr_fn.call0()?.extract()?; + check_cuda(cuda_memcpy, data_ptr, data, module); + let tensor: PyObject = tensor.into_py(module.py()); + Ok(tensor) +} + +fn check_cuda(cuda_memcpy: &Symbol, dst: usize, src: &[u8], module: &PyModule) { + let dst = dst as *mut std::ffi::c_void; + let count = src.len(); + let src_ptr = src.as_ptr() as *const std::ffi::c_void; + // SAFETY: Here we have a correct library, we successfully called memcpy, + // but somehow the call failed. This is really worrying since Pytorch is + // responsible for allocating the memory. + let out = unsafe { cuda_memcpy(dst, src_ptr, count, cudaMemcpyKind::HostToDevice) }; + if out != 0 { + let string = match get_error_string(module, out) { + Ok(string) => string, + Err(_) => format!("{}", out), + }; + println!( + "We tried to set your tensor fast, but there was a cuda error, This could have corrupted your GPU ram, aborting to prevent further errors {string:?}" + ); + std::process::abort(); + } +} + +fn create_cuda_unsafe_tensor_from_slice( + module: &PyModule, + cuda_memcpy: &Symbol, + shape: &[usize], + dtype: Dtype, + device: &Device, + iterator: safetensors::slice::SliceIterator, +) -> PyResult { + let tensor = create_empty_tensor_pt(module, shape, dtype, device)?; + + let mut offset = 0; + let data_ptr_fn = tensor.getattr("data_ptr")?; + let data_ptr: usize = data_ptr_fn.call0()?.extract()?; + for slice in iterator { + let len = slice.len(); + check_cuda(cuda_memcpy, data_ptr + offset, slice, module); + offset += len; + } + let tensor: PyObject = tensor.into_py(module.py()); + Ok(tensor) +} + +fn get_error_string(module: &PyModule, out: u32) -> PyResult { + module + .getattr(intern!(module.py(), "cuda"))? + .getattr(intern!(module.py(), "CudaError"))? + .call1((out.into_py(module.py()),))? + .getattr(intern!(module.py(), "__str__"))? + .call0()? + .extract() +} + #[pymethods] impl safe_open { #[new] @@ -301,6 +437,10 @@ impl safe_open { } }; + if let (Device::Cuda(_), Framework::Pytorch) = (&device, &framework) { + let module: &PyModule = get_module(py, &TORCH_MODULE)?; + CUDA_MEMCPY.get_or_init(py, || find_cuda_memcpy(module)); + } Ok(()) })?; @@ -359,15 +499,24 @@ impl safe_open { let data = &self.mmap[info.data_offsets.0 + self.offset..info.data_offsets.1 + self.offset]; Python::with_gil(|py| -> PyResult { - let array: PyObject = PyByteArray::new(py, data).into_py(py); - - create_tensor( - &self.framework, - info.dtype, - &info.shape, - array, - &self.device, - ) + match (&self.device, &self.framework, CUDA_MEMCPY.get(py)) { + (Device::Cuda(_), Framework::Pytorch, Some(Some(cuda_memcpy))) => { + let module = get_module(py, &TORCH_MODULE)?; + create_cuda_unsafe_tensor(module, cuda_memcpy, info, &self.device, data) + } + _ => { + 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, + ) + } + } }) } @@ -405,10 +554,47 @@ impl safe_open { } pub fn __enter__(slf: Py) -> Py { + // SAFETY: This code is extremely important to the GPU fast load. + // Cuda uses a context to select the device you are writing on. + // PyTorch uses this function to create and use said context. + // Without this, we instantiate the empty buffer on the correct GPU + // But we fail to override the proper memory location since the context + // is removed by python. + // Using this sets the Cuda context once and for all for the entirety + // of the context manager lifecycle. + Python::with_gil(|py| -> PyResult<()> { + let _self: &safe_open = &slf.borrow(py); + if let (Device::Cuda(_), Framework::Pytorch) = (&_self.device, &_self.framework) { + let module = get_module(py, &TORCH_MODULE)?; + let device: PyObject = _self.device.clone().into_py(py); + let torch_device = module + .getattr(intern!(py, "cuda"))? + .getattr(intern!(py, "device"))?; + let lock = torch_device.call1((device,))?; + lock.call_method0(intern!(py, "__enter__"))?; + } + Ok(()) + }) + .ok(); slf } - pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) {} + pub fn __exit__(&mut self, _exc_type: PyObject, _exc_value: PyObject, _traceback: PyObject) { + if let (Device::Cuda(_), Framework::Pytorch) = (&self.device, &self.framework) { + Python::with_gil(|py| -> PyResult<()> { + let module = get_module(py, &TORCH_MODULE)?; + let device: PyObject = self.device.clone().into_py(py); + let torch_device = module + .getattr(intern!(py, "cuda"))? + .getattr(intern!(py, "device"))?; + let none = py.None(); + let lock = torch_device.call1((device,))?; + lock.call_method1(intern!(py, "__exit__"), (&none, &none, &none))?; + Ok(()) + }) + .ok(); + } + } } #[pyclass] @@ -476,27 +662,54 @@ impl PySafeSlice { let mut offset = 0; let length = iterator.remaining_byte_len(); - Python::with_gil(|py| { - let array: PyObject = 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; + Python::with_gil( + |py| match (&self.device, &self.framework, CUDA_MEMCPY.get(py)) { + (Device::Cuda(_), Framework::Pytorch, Some(Some(cuda_memcpy))) => { + let module = get_module(py, &TORCH_MODULE)?; + create_cuda_unsafe_tensor_from_slice( + module, + cuda_memcpy, + &newshape, + self.info.dtype, + &self.device, + iterator, + ) } - Ok(()) - })? - .into_py(py); - create_tensor( - &self.framework, - self.info.dtype, - &newshape, - array, - &self.device, - ) - }) + _ => { + let array: PyObject = + 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(()) + })? + .into_py(py); + create_tensor( + &self.framework, + self.info.dtype, + &newshape, + array, + &self.device, + ) + } + }, + ) } } +fn get_module<'a>( + py: Python<'a>, + cell: &'static GILOnceCell>, +) -> PyResult<&'a PyModule> { + let module: &PyModule = cell + .get(py) + .ok_or_else(|| exceptions::PyException::new_err("Could not find module"))? + .as_ref(py); + Ok(module) +} + fn create_tensor( framework: &Framework, dtype: Dtype, -- 2.43.0