safetensors 0.4.6.dev0__cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of safetensors might be problematic. Click here for more details.

@@ -0,0 +1,9 @@
1
+ # Re-export this
2
+ from ._safetensors_rust import ( # noqa: F401
3
+ SafetensorError,
4
+ __version__,
5
+ deserialize,
6
+ safe_open,
7
+ serialize,
8
+ serialize_file,
9
+ )
@@ -0,0 +1,73 @@
1
+ # Generated content DO NOT EDIT
2
+ @staticmethod
3
+ def deserialize(bytes):
4
+ """
5
+ Opens a safetensors lazily and returns tensors as asked
6
+
7
+ Args:
8
+ data (:obj:`bytes`):
9
+ The byte content of a file
10
+
11
+ Returns:
12
+ (:obj:`List[str, Dict[str, Dict[str, any]]]`):
13
+ The deserialized content is like:
14
+ [("tensor_name", {"shape": [2, 3], "dtype": "F32", "data": b"\0\0.." }), (...)]
15
+ """
16
+ pass
17
+
18
+ @staticmethod
19
+ def serialize(tensor_dict, metadata=None):
20
+ """
21
+ Serializes raw data.
22
+
23
+ Args:
24
+ tensor_dict (:obj:`Dict[str, Dict[Any]]`):
25
+ The tensor dict is like:
26
+ {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
27
+ metadata (:obj:`Dict[str, str]`, *optional*):
28
+ The optional purely text annotations
29
+
30
+ Returns:
31
+ (:obj:`bytes`):
32
+ The serialized content.
33
+ """
34
+ pass
35
+
36
+ @staticmethod
37
+ def serialize_file(tensor_dict, filename, metadata=None):
38
+ """
39
+ Serializes raw data.
40
+
41
+ Args:
42
+ tensor_dict (:obj:`Dict[str, Dict[Any]]`):
43
+ The tensor dict is like:
44
+ {"tensor_name": {"dtype": "F32", "shape": [2, 3], "data": b"\0\0"}}
45
+ filename (:obj:`str`):
46
+ The name of the file to write into.
47
+ metadata (:obj:`Dict[str, str]`, *optional*):
48
+ The optional purely text annotations
49
+
50
+ Returns:
51
+ (:obj:`bytes`):
52
+ The serialized content.
53
+ """
54
+ pass
55
+
56
+ class safe_open:
57
+ """
58
+ Opens a safetensors lazily and returns tensors as asked
59
+
60
+ Args:
61
+ filename (:obj:`str`):
62
+ The filename to open
63
+
64
+ framework (:obj:`str`):
65
+ The framework you want your tensors in. Supported values:
66
+ `pt`, `tf`, `flax`, `numpy`.
67
+
68
+ device (:obj:`str`, defaults to :obj:`"cpu"`):
69
+ The device on which you want the tensors.
70
+ """
71
+
72
+ def __init__(self, filename, framework, device="cpu"):
73
+ pass
Binary file
safetensors/flax.py ADDED
@@ -0,0 +1,138 @@
1
+ import os
2
+ from typing import Dict, Optional, Union
3
+
4
+ import numpy as np
5
+
6
+ import jax.numpy as jnp
7
+ from jax import Array
8
+ from safetensors import numpy, safe_open
9
+
10
+
11
+ def save(tensors: Dict[str, Array], metadata: Optional[Dict[str, str]] = None) -> bytes:
12
+ """
13
+ Saves a dictionary of tensors into raw bytes in safetensors format.
14
+
15
+ Args:
16
+ tensors (`Dict[str, Array]`):
17
+ The incoming tensors. Tensors need to be contiguous and dense.
18
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
19
+ Optional text only metadata you might want to save in your header.
20
+ For instance it can be useful to specify more about the underlying
21
+ tensors. This is purely informative and does not affect tensor loading.
22
+
23
+ Returns:
24
+ `bytes`: The raw bytes representing the format
25
+
26
+ Example:
27
+
28
+ ```python
29
+ from safetensors.flax import save
30
+ from jax import numpy as jnp
31
+
32
+ tensors = {"embedding": jnp.zeros((512, 1024)), "attention": jnp.zeros((256, 256))}
33
+ byte_data = save(tensors)
34
+ ```
35
+ """
36
+ np_tensors = _jnp2np(tensors)
37
+ return numpy.save(np_tensors, metadata=metadata)
38
+
39
+
40
+ def save_file(
41
+ tensors: Dict[str, Array],
42
+ filename: Union[str, os.PathLike],
43
+ metadata: Optional[Dict[str, str]] = None,
44
+ ) -> None:
45
+ """
46
+ Saves a dictionary of tensors into raw bytes in safetensors format.
47
+
48
+ Args:
49
+ tensors (`Dict[str, Array]`):
50
+ The incoming tensors. Tensors need to be contiguous and dense.
51
+ filename (`str`, or `os.PathLike`)):
52
+ The filename we're saving into.
53
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
54
+ Optional text only metadata you might want to save in your header.
55
+ For instance it can be useful to specify more about the underlying
56
+ tensors. This is purely informative and does not affect tensor loading.
57
+
58
+ Returns:
59
+ `None`
60
+
61
+ Example:
62
+
63
+ ```python
64
+ from safetensors.flax import save_file
65
+ from jax import numpy as jnp
66
+
67
+ tensors = {"embedding": jnp.zeros((512, 1024)), "attention": jnp.zeros((256, 256))}
68
+ save_file(tensors, "model.safetensors")
69
+ ```
70
+ """
71
+ np_tensors = _jnp2np(tensors)
72
+ return numpy.save_file(np_tensors, filename, metadata=metadata)
73
+
74
+
75
+ def load(data: bytes) -> Dict[str, Array]:
76
+ """
77
+ Loads a safetensors file into flax format from pure bytes.
78
+
79
+ Args:
80
+ data (`bytes`):
81
+ The content of a safetensors file
82
+
83
+ Returns:
84
+ `Dict[str, Array]`: dictionary that contains name as key, value as `Array` on cpu
85
+
86
+ Example:
87
+
88
+ ```python
89
+ from safetensors.flax import load
90
+
91
+ file_path = "./my_folder/bert.safetensors"
92
+ with open(file_path, "rb") as f:
93
+ data = f.read()
94
+
95
+ loaded = load(data)
96
+ ```
97
+ """
98
+ flat = numpy.load(data)
99
+ return _np2jnp(flat)
100
+
101
+
102
+ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, Array]:
103
+ """
104
+ Loads a safetensors file into flax format.
105
+
106
+ Args:
107
+ filename (`str`, or `os.PathLike`)):
108
+ The name of the file which contains the tensors
109
+
110
+ Returns:
111
+ `Dict[str, Array]`: dictionary that contains name as key, value as `Array`
112
+
113
+ Example:
114
+
115
+ ```python
116
+ from safetensors.flax import load_file
117
+
118
+ file_path = "./my_folder/bert.safetensors"
119
+ loaded = load_file(file_path)
120
+ ```
121
+ """
122
+ result = {}
123
+ with safe_open(filename, framework="flax") as f:
124
+ for k in f.keys():
125
+ result[k] = f.get_tensor(k)
126
+ return result
127
+
128
+
129
+ def _np2jnp(numpy_dict: Dict[str, np.ndarray]) -> Dict[str, Array]:
130
+ for k, v in numpy_dict.items():
131
+ numpy_dict[k] = jnp.array(v)
132
+ return numpy_dict
133
+
134
+
135
+ def _jnp2np(jnp_dict: Dict[str, Array]) -> Dict[str, np.array]:
136
+ for k, v in jnp_dict.items():
137
+ jnp_dict[k] = np.asarray(v)
138
+ return jnp_dict
safetensors/mlx.py ADDED
@@ -0,0 +1,138 @@
1
+ import os
2
+ from typing import Dict, Optional, Union
3
+
4
+ import numpy as np
5
+
6
+ import mlx.core as mx
7
+ from safetensors import numpy, safe_open
8
+
9
+
10
+ def save(tensors: Dict[str, mx.array], metadata: Optional[Dict[str, str]] = None) -> bytes:
11
+ """
12
+ Saves a dictionary of tensors into raw bytes in safetensors format.
13
+
14
+ Args:
15
+ tensors (`Dict[str, mx.array]`):
16
+ The incoming tensors. Tensors need to be contiguous and dense.
17
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
18
+ Optional text only metadata you might want to save in your header.
19
+ For instance it can be useful to specify more about the underlying
20
+ tensors. This is purely informative and does not affect tensor loading.
21
+
22
+ Returns:
23
+ `bytes`: The raw bytes representing the format
24
+
25
+ Example:
26
+
27
+ ```python
28
+ from safetensors.mlx import save
29
+ import mlx.core as mx
30
+
31
+ tensors = {"embedding": mx.zeros((512, 1024)), "attention": mx.zeros((256, 256))}
32
+ byte_data = save(tensors)
33
+ ```
34
+ """
35
+ np_tensors = _mx2np(tensors)
36
+ return numpy.save(np_tensors, metadata=metadata)
37
+
38
+
39
+ def save_file(
40
+ tensors: Dict[str, mx.array],
41
+ filename: Union[str, os.PathLike],
42
+ metadata: Optional[Dict[str, str]] = None,
43
+ ) -> None:
44
+ """
45
+ Saves a dictionary of tensors into raw bytes in safetensors format.
46
+
47
+ Args:
48
+ tensors (`Dict[str, mx.array]`):
49
+ The incoming tensors. Tensors need to be contiguous and dense.
50
+ filename (`str`, or `os.PathLike`)):
51
+ The filename we're saving into.
52
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
53
+ Optional text only metadata you might want to save in your header.
54
+ For instance it can be useful to specify more about the underlying
55
+ tensors. This is purely informative and does not affect tensor loading.
56
+
57
+ Returns:
58
+ `None`
59
+
60
+ Example:
61
+
62
+ ```python
63
+ from safetensors.mlx import save_file
64
+ import mlx.core as mx
65
+
66
+ tensors = {"embedding": mx.zeros((512, 1024)), "attention": mx.zeros((256, 256))}
67
+ save_file(tensors, "model.safetensors")
68
+ ```
69
+ """
70
+ np_tensors = _mx2np(tensors)
71
+ return numpy.save_file(np_tensors, filename, metadata=metadata)
72
+
73
+
74
+ def load(data: bytes) -> Dict[str, mx.array]:
75
+ """
76
+ Loads a safetensors file into MLX format from pure bytes.
77
+
78
+ Args:
79
+ data (`bytes`):
80
+ The content of a safetensors file
81
+
82
+ Returns:
83
+ `Dict[str, mx.array]`: dictionary that contains name as key, value as `mx.array`
84
+
85
+ Example:
86
+
87
+ ```python
88
+ from safetensors.mlx import load
89
+
90
+ file_path = "./my_folder/bert.safetensors"
91
+ with open(file_path, "rb") as f:
92
+ data = f.read()
93
+
94
+ loaded = load(data)
95
+ ```
96
+ """
97
+ flat = numpy.load(data)
98
+ return _np2mx(flat)
99
+
100
+
101
+ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, mx.array]:
102
+ """
103
+ Loads a safetensors file into MLX format.
104
+
105
+ Args:
106
+ filename (`str`, or `os.PathLike`)):
107
+ The name of the file which contains the tensors
108
+
109
+ Returns:
110
+ `Dict[str, mx.array]`: dictionary that contains name as key, value as `mx.array`
111
+
112
+ Example:
113
+
114
+ ```python
115
+ from safetensors.flax import load_file
116
+
117
+ file_path = "./my_folder/bert.safetensors"
118
+ loaded = load_file(file_path)
119
+ ```
120
+ """
121
+ result = {}
122
+ with safe_open(filename, framework="mlx") as f:
123
+ for k in f.keys():
124
+ result[k] = f.get_tensor(k)
125
+ return result
126
+
127
+
128
+ def _np2mx(numpy_dict: Dict[str, np.ndarray]) -> Dict[str, mx.array]:
129
+ for k, v in numpy_dict.items():
130
+ numpy_dict[k] = mx.array(v)
131
+ return numpy_dict
132
+
133
+
134
+ def _mx2np(mx_dict: Dict[str, mx.array]) -> Dict[str, np.array]:
135
+ new_dict = {}
136
+ for k, v in mx_dict.items():
137
+ new_dict[k] = np.asarray(v)
138
+ return new_dict
safetensors/numpy.py ADDED
@@ -0,0 +1,176 @@
1
+ import os
2
+ import sys
3
+ from typing import Dict, Optional, Union
4
+
5
+ import numpy as np
6
+
7
+ from safetensors import deserialize, safe_open, serialize, serialize_file
8
+
9
+
10
+ def _tobytes(tensor: np.ndarray) -> bytes:
11
+ if not _is_little_endian(tensor):
12
+ tensor = tensor.byteswap(inplace=False)
13
+ return tensor.tobytes()
14
+
15
+
16
+ def save(tensor_dict: Dict[str, np.ndarray], metadata: Optional[Dict[str, str]] = None) -> bytes:
17
+ """
18
+ Saves a dictionary of tensors into raw bytes in safetensors format.
19
+
20
+ Args:
21
+ tensor_dict (`Dict[str, np.ndarray]`):
22
+ The incoming tensors. Tensors need to be contiguous and dense.
23
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
24
+ Optional text only metadata you might want to save in your header.
25
+ For instance it can be useful to specify more about the underlying
26
+ tensors. This is purely informative and does not affect tensor loading.
27
+
28
+ Returns:
29
+ `bytes`: The raw bytes representing the format
30
+
31
+ Example:
32
+
33
+ ```python
34
+ from safetensors.numpy import save
35
+ import numpy as np
36
+
37
+ tensors = {"embedding": np.zeros((512, 1024)), "attention": np.zeros((256, 256))}
38
+ byte_data = save(tensors)
39
+ ```
40
+ """
41
+ flattened = {k: {"dtype": v.dtype.name, "shape": v.shape, "data": _tobytes(v)} for k, v in tensor_dict.items()}
42
+ serialized = serialize(flattened, metadata=metadata)
43
+ result = bytes(serialized)
44
+ return result
45
+
46
+
47
+ def save_file(
48
+ tensor_dict: Dict[str, np.ndarray], filename: Union[str, os.PathLike], metadata: Optional[Dict[str, str]] = None
49
+ ) -> None:
50
+ """
51
+ Saves a dictionary of tensors into raw bytes in safetensors format.
52
+
53
+ Args:
54
+ tensor_dict (`Dict[str, np.ndarray]`):
55
+ The incoming tensors. Tensors need to be contiguous and dense.
56
+ filename (`str`, or `os.PathLike`)):
57
+ The filename we're saving into.
58
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
59
+ Optional text only metadata you might want to save in your header.
60
+ For instance it can be useful to specify more about the underlying
61
+ tensors. This is purely informative and does not affect tensor loading.
62
+
63
+ Returns:
64
+ `None`
65
+
66
+ Example:
67
+
68
+ ```python
69
+ from safetensors.numpy import save_file
70
+ import numpy as np
71
+
72
+ tensors = {"embedding": np.zeros((512, 1024)), "attention": np.zeros((256, 256))}
73
+ save_file(tensors, "model.safetensors")
74
+ ```
75
+ """
76
+ flattened = {k: {"dtype": v.dtype.name, "shape": v.shape, "data": _tobytes(v)} for k, v in tensor_dict.items()}
77
+ serialize_file(flattened, filename, metadata=metadata)
78
+
79
+
80
+ def load(data: bytes) -> Dict[str, np.ndarray]:
81
+ """
82
+ Loads a safetensors file into numpy format from pure bytes.
83
+
84
+ Args:
85
+ data (`bytes`):
86
+ The content of a safetensors file
87
+
88
+ Returns:
89
+ `Dict[str, np.ndarray]`: dictionary that contains name as key, value as `np.ndarray` on cpu
90
+
91
+ Example:
92
+
93
+ ```python
94
+ from safetensors.numpy import load
95
+
96
+ file_path = "./my_folder/bert.safetensors"
97
+ with open(file_path, "rb") as f:
98
+ data = f.read()
99
+
100
+ loaded = load(data)
101
+ ```
102
+ """
103
+ flat = deserialize(data)
104
+ return _view2np(flat)
105
+
106
+
107
+ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, np.ndarray]:
108
+ """
109
+ Loads a safetensors file into numpy format.
110
+
111
+ Args:
112
+ filename (`str`, or `os.PathLike`)):
113
+ The name of the file which contains the tensors
114
+
115
+ Returns:
116
+ `Dict[str, np.ndarray]`: dictionary that contains name as key, value as `np.ndarray`
117
+
118
+ Example:
119
+
120
+ ```python
121
+ from safetensors.numpy import load_file
122
+
123
+ file_path = "./my_folder/bert.safetensors"
124
+ loaded = load_file(file_path)
125
+ ```
126
+ """
127
+ result = {}
128
+ with safe_open(filename, framework="np") as f:
129
+ for k in f.keys():
130
+ result[k] = f.get_tensor(k)
131
+ return result
132
+
133
+
134
+ _TYPES = {
135
+ "F64": np.float64,
136
+ "F32": np.float32,
137
+ "F16": np.float16,
138
+ "I64": np.int64,
139
+ "U64": np.uint64,
140
+ "I32": np.int32,
141
+ "U32": np.uint32,
142
+ "I16": np.int16,
143
+ "U16": np.uint16,
144
+ "I8": np.int8,
145
+ "U8": np.uint8,
146
+ "BOOL": bool,
147
+ }
148
+
149
+
150
+ def _getdtype(dtype_str: str) -> np.dtype:
151
+ return _TYPES[dtype_str]
152
+
153
+
154
+ def _view2np(safeview) -> Dict[str, np.ndarray]:
155
+ result = {}
156
+ for k, v in safeview:
157
+ dtype = _getdtype(v["dtype"])
158
+ arr = np.frombuffer(v["data"], dtype=dtype).reshape(v["shape"])
159
+ result[k] = arr
160
+ return result
161
+
162
+
163
+ def _is_little_endian(tensor: np.ndarray) -> bool:
164
+ byteorder = tensor.dtype.byteorder
165
+ if byteorder == "=":
166
+ if sys.byteorder == "little":
167
+ return True
168
+ else:
169
+ return False
170
+ elif byteorder == "|":
171
+ return True
172
+ elif byteorder == "<":
173
+ return True
174
+ elif byteorder == ">":
175
+ return False
176
+ raise ValueError(f"Unexpected byte order {byteorder}")
safetensors/paddle.py ADDED
@@ -0,0 +1,138 @@
1
+ import os
2
+ from typing import Dict, Optional, Union
3
+
4
+ import numpy as np
5
+
6
+ import paddle
7
+ from safetensors import numpy
8
+
9
+
10
+ def save(tensors: Dict[str, paddle.Tensor], metadata: Optional[Dict[str, str]] = None) -> bytes:
11
+ """
12
+ Saves a dictionary of tensors into raw bytes in safetensors format.
13
+
14
+ Args:
15
+ tensors (`Dict[str, paddle.Tensor]`):
16
+ The incoming tensors. Tensors need to be contiguous and dense.
17
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
18
+ Optional text only metadata you might want to save in your header.
19
+ For instance it can be useful to specify more about the underlying
20
+ tensors. This is purely informative and does not affect tensor loading.
21
+
22
+ Returns:
23
+ `bytes`: The raw bytes representing the format
24
+
25
+ Example:
26
+
27
+ ```python
28
+ from safetensors.paddle import save
29
+ import paddle
30
+
31
+ tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
32
+ byte_data = save(tensors)
33
+ ```
34
+ """
35
+ np_tensors = _paddle2np(tensors)
36
+ return numpy.save(np_tensors, metadata=metadata)
37
+
38
+
39
+ def save_file(
40
+ tensors: Dict[str, paddle.Tensor],
41
+ filename: Union[str, os.PathLike],
42
+ metadata: Optional[Dict[str, str]] = None,
43
+ ) -> None:
44
+ """
45
+ Saves a dictionary of tensors into raw bytes in safetensors format.
46
+
47
+ Args:
48
+ tensors (`Dict[str, paddle.Tensor]`):
49
+ The incoming tensors. Tensors need to be contiguous and dense.
50
+ filename (`str`, or `os.PathLike`)):
51
+ The filename we're saving into.
52
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
53
+ Optional text only metadata you might want to save in your header.
54
+ For instance it can be useful to specify more about the underlying
55
+ tensors. This is purely informative and does not affect tensor loading.
56
+
57
+ Returns:
58
+ `None`
59
+
60
+ Example:
61
+
62
+ ```python
63
+ from safetensors.paddle import save_file
64
+ import paddle
65
+
66
+ tensors = {"embedding": paddle.zeros((512, 1024)), "attention": paddle.zeros((256, 256))}
67
+ save_file(tensors, "model.safetensors")
68
+ ```
69
+ """
70
+ np_tensors = _paddle2np(tensors)
71
+ return numpy.save_file(np_tensors, filename, metadata=metadata)
72
+
73
+
74
+ def load(data: bytes, device: str = "cpu") -> Dict[str, paddle.Tensor]:
75
+ """
76
+ Loads a safetensors file into paddle format from pure bytes.
77
+
78
+ Args:
79
+ data (`bytes`):
80
+ The content of a safetensors file
81
+
82
+ Returns:
83
+ `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor` on cpu
84
+
85
+ Example:
86
+
87
+ ```python
88
+ from safetensors.paddle import load
89
+
90
+ file_path = "./my_folder/bert.safetensors"
91
+ with open(file_path, "rb") as f:
92
+ data = f.read()
93
+
94
+ loaded = load(data)
95
+ ```
96
+ """
97
+ flat = numpy.load(data)
98
+ return _np2paddle(flat, device)
99
+
100
+
101
+ def load_file(filename: Union[str, os.PathLike], device="cpu") -> Dict[str, paddle.Tensor]:
102
+ """
103
+ Loads a safetensors file into paddle format.
104
+
105
+ Args:
106
+ filename (`str`, or `os.PathLike`)):
107
+ The name of the file which contains the tensors
108
+ device (`Union[Dict[str, any], str]`, *optional*, defaults to `cpu`):
109
+ The device where the tensors need to be located after load.
110
+ available options are all regular paddle device locations
111
+
112
+ Returns:
113
+ `Dict[str, paddle.Tensor]`: dictionary that contains name as key, value as `paddle.Tensor`
114
+
115
+ Example:
116
+
117
+ ```python
118
+ from safetensors.paddle import load_file
119
+
120
+ file_path = "./my_folder/bert.safetensors"
121
+ loaded = load_file(file_path)
122
+ ```
123
+ """
124
+ flat = numpy.load_file(filename)
125
+ output = _np2paddle(flat, device)
126
+ return output
127
+
128
+
129
+ def _np2paddle(numpy_dict: Dict[str, np.ndarray], device: str = "cpu") -> Dict[str, paddle.Tensor]:
130
+ for k, v in numpy_dict.items():
131
+ numpy_dict[k] = paddle.to_tensor(v, place=device)
132
+ return numpy_dict
133
+
134
+
135
+ def _paddle2np(paddle_dict: Dict[str, paddle.Tensor]) -> Dict[str, np.array]:
136
+ for k, v in paddle_dict.items():
137
+ paddle_dict[k] = v.detach().cpu().numpy()
138
+ return paddle_dict
safetensors/py.typed ADDED
File without changes