From: Nicolas Patry Date: Wed, 6 Jul 2022 22:33:40 +0000 (+0200) Subject: Convertion of bfloat16 enabled. X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e3a3419f7465ae43991fc636055a51fcd5fc2c69;p=safetensors.git Convertion of bfloat16 enabled. --- diff --git a/bindings/python/py_src/safetensors/torch.py b/bindings/python/py_src/safetensors/torch.py index d28dff2..fa106db 100644 --- a/bindings/python/py_src/safetensors/torch.py +++ b/bindings/python/py_src/safetensors/torch.py @@ -1,4 +1,5 @@ from safetensors import numpy +from .safetensors_rust import serialize_file import numpy as np from typing import Dict import torch @@ -21,9 +22,36 @@ def save(tensors: Dict[str, torch.Tensor]) -> bytes: return numpy.save(np_tensors) +SIZE = { + torch.bfloat16: 2, + torch.float16: 2, + torch.int32: 4, + torch.int64: 4, +} + + +def tobytes(tensor: torch.Tensor) -> bytes: + import ctypes + + length = np.prod(tensor.shape) + bytes_per_item = SIZE[tensor.dtype] + + total_bytes = length * bytes_per_item + + ptr = tensor.data_ptr() + newptr = ctypes.cast(ptr, ctypes.POINTER(ctypes.c_ubyte)) + + data = np.ctypeslib.as_array(newptr, (total_bytes,)) # no internal copy + + return data.tobytes() + + def save_file(tensors: Dict[str, torch.Tensor], filename: str): - np_tensors = pt2np(tensors) - return numpy.save_file(np_tensors, filename) + flattened = { + k: {"dtype": str(v.dtype).split(".")[-1], "shape": v.shape, "data": tobytes(v)} + for k, v in tensors.items() + } + serialize_file(flattened, filename) def load(buffer: bytes) -> Dict[str, torch.Tensor]: