in bindings/python/src/lib.rs [176:197]
fn deserialize(py: Python, bytes: &[u8]) -> PyResult<Vec<(String, HashMap<String, PyObject>)>> {
let safetensor = SafeTensors::deserialize(bytes)
.map_err(|e| SafetensorError::new_err(format!("Error while deserializing: {e}")))?;
let tensors = safetensor.tensors();
let mut items = Vec::with_capacity(tensors.len());
for (tensor_name, tensor) in tensors {
let pyshape: PyObject = PyList::new(py, tensor.shape().iter())?.into();
let pydtype: PyObject = tensor.dtype().to_string().into_pyobject(py)?.into();
let pydata: PyObject = PyByteArray::new(py, tensor.data()).into();
let map = HashMap::from([
("shape".to_string(), pyshape),
("dtype".to_string(), pydtype),
("data".to_string(), pydata),
]);
items.push((tensor_name, map));
}
Ok(items)
}