use std::fs::File;
use std::iter::FromIterator;
use std::ops::Bound;
+use std::path::PathBuf;
use std::sync::Arc;
#[repr(C)]
/// Serializes raw data.
///
/// Args:
-/// tensor_dict (:obj:`Dict[str, Dict[Any]]`):
+/// tensor_dict (`Dict[str, Dict[Any]]`):
/// The tensor dict is like:
/// {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
-/// metadata (:obj:`Dict[str, str]`, *optional*):
+/// metadata (`Dict[str, str]`, *optional*):
/// The optional purely text annotations
///
/// Returns:
-/// (:obj:`bytes`):
+/// (`bytes`):
/// The serialized content.
#[pyfunction]
#[pyo3(text_signature = "(tensor_dict, metadata=None)")]
/// Serializes raw data.
///
/// Args:
-/// tensor_dict (:obj:`Dict[str, Dict[Any]]`):
+/// tensor_dict (`Dict[str, Dict[Any]]`):
/// The tensor dict is like:
/// {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
-/// filename (:obj:`str`):
+/// filename (`str`, or `os.PathLike`):
/// The name of the file to write into.
-/// metadata (:obj:`Dict[str, str]`, *optional*):
+/// metadata (`Dict[str, str]`, *optional*):
/// The optional purely text annotations
///
/// Returns:
-/// (:obj:`bytes`):
+/// (`bytes`):
/// The serialized content.
#[pyfunction]
#[pyo3(text_signature = "(tensor_dict, filename, metadata=None)")]
fn serialize_file(
tensor_dict: HashMap<String, &PyDict>,
- filename: &str,
+ filename: PathBuf,
metadata: Option<HashMap<String, String>>,
) -> PyResult<()> {
let tensors = prepare(tensor_dict)?;
let metadata_btreemap = metadata.map(|data| BTreeMap::from_iter(data.into_iter()));
- safetensors::tensor::serialize_to_file(&tensors, &metadata_btreemap, filename).map_err(
- |e| exceptions::PyException::new_err(format!("Error while serializing: {:?}", e)),
- )?;
+ safetensors::tensor::serialize_to_file(&tensors, &metadata_btreemap, filename.as_path())
+ .map_err(|e| {
+ exceptions::PyException::new_err(format!("Error while serializing: {:?}", e))
+ })?;
Ok(())
}
/// Opens a safetensors lazily and returns tensors as asked
///
/// Args:
-/// data (:obj:`bytes`):
+/// data (`bytes`):
/// The byte content of a file
///
/// Returns:
-/// (:obj:`List[str, Dict[str, Dict[str, any]]]`):
+/// (`List[str, Dict[str, Dict[str, any]]]`):
/// The deserialized content is like:
/// [("tensor_name", {"shape": [2, 3], "dtype": "F32", "data": b"\0\0.." }), (...)]
#[pyfunction]
/// Opens a safetensors lazily and returns tensors as asked
///
/// Args:
-/// filename (:obj:`str`):
+/// filename (`str`, or `os.PathLike`):
/// The filename to open
///
-/// framework (:obj:`str`):
+/// framework (`str`):
/// The framework you want you tensors in. Supported values:
/// `pt`, `tf`, `flax`, `numpy`.
///
-/// device (:obj:`str`, defaults to :obj:`"cpu"`):
+/// device (`str`, defaults to `"cpu"`):
/// The device on which you want the tensors.
#[pyclass]
#[allow(non_camel_case_types)]
#[pymethods]
impl safe_open {
#[new]
- fn new(filename: &str, framework: Framework, device: Option<Device>) -> PyResult<Self> {
- let file = File::open(filename)?;
+ fn new(filename: PathBuf, framework: Framework, device: Option<Device>) -> PyResult<Self> {
+ let file = File::open(&filename)?;
let device = device.unwrap_or(Device::Cpu);
if device != Device::Cpu && framework != Framework::Pytorch {
/// Return the special non tensor information in the header
///
/// Returns:
- /// (:obj:`Dict[str, str]`):
+ /// (`Dict[str, str]`):
/// The freeform metadata.
pub fn metadata(&self) -> Option<BTreeMap<String, String>> {
self.metadata.metadata().clone()
/// Returns the names of the tensors in the file.
///
/// Returns:
- /// (:obj:`List[str]`):
+ /// (`List[str]`):
/// The name of the tensors contained in that file
pub fn keys(&self) -> PyResult<Vec<String>> {
let mut keys: Vec<_> = self.metadata.tensors().keys().cloned().collect();
/// Returns a full tensor
///
/// Args:
- /// name (:obj:`str`):
+ /// name (`str`):
/// The name of the tensor you want
///
/// Returns:
- /// (:obj:`Tensor`):
+ /// (`Tensor`):
/// The tensor in the framework you opened the file for.
///
/// Example:
/// Returns a full slice view object
///
/// Args:
- /// name (:obj:`str`):
+ /// name (`str`):
/// The name of the tensor you want
///
/// Returns:
- /// (:obj:`PySafeSlice`):
+ /// (`PySafeSlice`):
/// A dummy object you can slice into to get a real tensor
/// Example:
/// ```python
/// Returns the shape of the full underlying tensor
///
/// Returns:
- /// (:obj:`List[int]`):
+ /// (`List[int]`):
/// The shape of the full tensor
///
/// Example: