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,137 @@
1
+ import os
2
+ from typing import Dict, Optional, Union
3
+
4
+ import numpy as np
5
+ import tensorflow as tf
6
+
7
+ from safetensors import numpy, safe_open
8
+
9
+
10
+ def save(tensors: Dict[str, tf.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, tf.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.tensorflow import save
29
+ import tensorflow as tf
30
+
31
+ tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
32
+ byte_data = save(tensors)
33
+ ```
34
+ """
35
+ np_tensors = _tf2np(tensors)
36
+ return numpy.save(np_tensors, metadata=metadata)
37
+
38
+
39
+ def save_file(
40
+ tensors: Dict[str, tf.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, tf.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.tensorflow import save_file
64
+ import tensorflow as tf
65
+
66
+ tensors = {"embedding": tf.zeros((512, 1024)), "attention": tf.zeros((256, 256))}
67
+ save_file(tensors, "model.safetensors")
68
+ ```
69
+ """
70
+ np_tensors = _tf2np(tensors)
71
+ return numpy.save_file(np_tensors, filename, metadata=metadata)
72
+
73
+
74
+ def load(data: bytes) -> Dict[str, tf.Tensor]:
75
+ """
76
+ Loads a safetensors file into tensorflow format from pure bytes.
77
+
78
+ Args:
79
+ data (`bytes`):
80
+ The content of a safetensors file
81
+
82
+ Returns:
83
+ `Dict[str, tf.Tensor]`: dictionary that contains name as key, value as `tf.Tensor` on cpu
84
+
85
+ Example:
86
+
87
+ ```python
88
+ from safetensors.tensorflow 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 _np2tf(flat)
99
+
100
+
101
+ def load_file(filename: Union[str, os.PathLike]) -> Dict[str, tf.Tensor]:
102
+ """
103
+ Loads a safetensors file into tensorflow 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, tf.Tensor]`: dictionary that contains name as key, value as `tf.Tensor`
111
+
112
+ Example:
113
+
114
+ ```python
115
+ from safetensors.tensorflow 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="tf") as f:
123
+ for k in f.keys():
124
+ result[k] = f.get_tensor(k)
125
+ return result
126
+
127
+
128
+ def _np2tf(numpy_dict: Dict[str, np.ndarray]) -> Dict[str, tf.Tensor]:
129
+ for k, v in numpy_dict.items():
130
+ numpy_dict[k] = tf.convert_to_tensor(v)
131
+ return numpy_dict
132
+
133
+
134
+ def _tf2np(tf_dict: Dict[str, tf.Tensor]) -> Dict[str, np.array]:
135
+ for k, v in tf_dict.items():
136
+ tf_dict[k] = v.numpy()
137
+ return tf_dict
safetensors/torch.py ADDED
@@ -0,0 +1,503 @@
1
+ import os
2
+ import sys
3
+ from collections import defaultdict
4
+ from typing import Any, Dict, List, Optional, Set, Tuple, Union
5
+
6
+ import torch
7
+
8
+ from safetensors import deserialize, safe_open, serialize, serialize_file
9
+
10
+
11
+ def storage_ptr(tensor: torch.Tensor) -> int:
12
+ try:
13
+ return tensor.untyped_storage().data_ptr()
14
+ except Exception:
15
+ # Fallback for torch==1.10
16
+ try:
17
+ return tensor.storage().data_ptr()
18
+ except NotImplementedError:
19
+ # Fallback for meta storage
20
+ return 0
21
+
22
+
23
+ def _end_ptr(tensor: torch.Tensor) -> int:
24
+ if tensor.nelement():
25
+ stop = tensor.view(-1)[-1].data_ptr() + _SIZE[tensor.dtype]
26
+ else:
27
+ stop = tensor.data_ptr()
28
+ return stop
29
+
30
+
31
+ def storage_size(tensor: torch.Tensor) -> int:
32
+ try:
33
+ return tensor.untyped_storage().nbytes()
34
+ except AttributeError:
35
+ # Fallback for torch==1.10
36
+ try:
37
+ return tensor.storage().size() * _SIZE[tensor.dtype]
38
+ except NotImplementedError:
39
+ # Fallback for meta storage
40
+ # On torch >=2.0 this is the tensor size
41
+ return tensor.nelement() * _SIZE[tensor.dtype]
42
+
43
+
44
+ def _filter_shared_not_shared(tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]) -> List[Set[str]]:
45
+ filtered_tensors = []
46
+ for shared in tensors:
47
+ if len(shared) < 2:
48
+ filtered_tensors.append(shared)
49
+ continue
50
+
51
+ areas = []
52
+ for name in shared:
53
+ tensor = state_dict[name]
54
+ areas.append((tensor.data_ptr(), _end_ptr(tensor), name))
55
+ areas.sort()
56
+
57
+ _, last_stop, last_name = areas[0]
58
+ filtered_tensors.append({last_name})
59
+ for start, stop, name in areas[1:]:
60
+ if start >= last_stop:
61
+ filtered_tensors.append({name})
62
+ else:
63
+ filtered_tensors[-1].add(name)
64
+ last_stop = stop
65
+
66
+ return filtered_tensors
67
+
68
+
69
+ def _find_shared_tensors(state_dict: Dict[str, torch.Tensor]) -> List[Set[str]]:
70
+ tensors = defaultdict(set)
71
+ for k, v in state_dict.items():
72
+ if v.device != torch.device("meta") and storage_ptr(v) != 0 and storage_size(v) != 0:
73
+ # Need to add device as key because of multiple GPU.
74
+ tensors[(v.device, storage_ptr(v), storage_size(v))].add(k)
75
+ tensors = list(sorted(tensors.values()))
76
+ tensors = _filter_shared_not_shared(tensors, state_dict)
77
+ return tensors
78
+
79
+
80
+ def _is_complete(tensor: torch.Tensor) -> bool:
81
+ return tensor.data_ptr() == storage_ptr(tensor) and tensor.nelement() * _SIZE[tensor.dtype] == storage_size(tensor)
82
+
83
+
84
+ def _remove_duplicate_names(
85
+ state_dict: Dict[str, torch.Tensor],
86
+ *,
87
+ preferred_names: Optional[List[str]] = None,
88
+ discard_names: Optional[List[str]] = None,
89
+ ) -> Dict[str, List[str]]:
90
+ if preferred_names is None:
91
+ preferred_names = []
92
+ preferred_names = set(preferred_names)
93
+ if discard_names is None:
94
+ discard_names = []
95
+ discard_names = set(discard_names)
96
+
97
+ shareds = _find_shared_tensors(state_dict)
98
+ to_remove = defaultdict(list)
99
+ for shared in shareds:
100
+ complete_names = set([name for name in shared if _is_complete(state_dict[name])])
101
+ if not complete_names:
102
+ raise RuntimeError(
103
+ "Error while trying to find names to remove to save state dict, but found no suitable name to keep"
104
+ f" for saving amongst: {shared}. None is covering the entire storage.Refusing to save/load the model"
105
+ " since you could be storing much more memory than needed. Please refer to"
106
+ " https://huggingface.co/docs/safetensors/torch_shared_tensors for more information. Or open an"
107
+ " issue."
108
+ )
109
+
110
+ keep_name = sorted(list(complete_names))[0]
111
+
112
+ # Mechanism to preferentially select keys to keep
113
+ # coming from the on-disk file to allow
114
+ # loading models saved with a different choice
115
+ # of keep_name
116
+ preferred = complete_names.difference(discard_names)
117
+ if preferred:
118
+ keep_name = sorted(list(preferred))[0]
119
+
120
+ if preferred_names:
121
+ preferred = preferred_names.intersection(complete_names)
122
+ if preferred:
123
+ keep_name = sorted(list(preferred))[0]
124
+ for name in sorted(shared):
125
+ if name != keep_name:
126
+ to_remove[keep_name].append(name)
127
+ return to_remove
128
+
129
+
130
+ def save_model(
131
+ model: torch.nn.Module, filename: str, metadata: Optional[Dict[str, str]] = None, force_contiguous: bool = True
132
+ ):
133
+ """
134
+ Saves a given torch model to specified filename.
135
+ This method exists specifically to avoid tensor sharing issues which are
136
+ not allowed in `safetensors`. [More information on tensor sharing](../torch_shared_tensors)
137
+
138
+ Args:
139
+ model (`torch.nn.Module`):
140
+ The model to save on disk.
141
+ filename (`str`):
142
+ The filename location to save the file
143
+ metadata (`Dict[str, str]`, *optional*):
144
+ Extra information to save along with the file.
145
+ Some metadata will be added for each dropped tensors.
146
+ This information will not be enough to recover the entire
147
+ shared structure but might help understanding things
148
+ force_contiguous (`boolean`, *optional*, defaults to True):
149
+ Forcing the state_dict to be saved as contiguous tensors.
150
+ This has no effect on the correctness of the model, but it
151
+ could potentially change performance if the layout of the tensor
152
+ was chosen specifically for that reason.
153
+ """
154
+ state_dict = model.state_dict()
155
+ to_removes = _remove_duplicate_names(state_dict)
156
+
157
+ for kept_name, to_remove_group in to_removes.items():
158
+ for to_remove in to_remove_group:
159
+ if metadata is None:
160
+ metadata = {}
161
+
162
+ if to_remove not in metadata:
163
+ # Do not override user data
164
+ metadata[to_remove] = kept_name
165
+ del state_dict[to_remove]
166
+ if force_contiguous:
167
+ state_dict = {k: v.contiguous() for k, v in state_dict.items()}
168
+ try:
169
+ save_file(state_dict, filename, metadata=metadata)
170
+ except ValueError as e:
171
+ msg = str(e)
172
+ msg += " Or use save_model(..., force_contiguous=True), read the docs for potential caveats."
173
+ raise ValueError(msg)
174
+
175
+
176
+ def load_model(
177
+ model: torch.nn.Module, filename: Union[str, os.PathLike], strict: bool = True, device: Union[str, int] = "cpu"
178
+ ) -> Tuple[List[str], List[str]]:
179
+ """
180
+ Loads a given filename onto a torch model.
181
+ This method exists specifically to avoid tensor sharing issues which are
182
+ not allowed in `safetensors`. [More information on tensor sharing](../torch_shared_tensors)
183
+
184
+ Args:
185
+ model (`torch.nn.Module`):
186
+ The model to load onto.
187
+ filename (`str`, or `os.PathLike`):
188
+ The filename location to load the file from.
189
+ strict (`bool`, *optional*, defaults to True):
190
+ Whether to fail if you're missing keys or having unexpected ones.
191
+ When false, the function simply returns missing and unexpected names.
192
+ device (`Union[str, int]`, *optional*, defaults to `cpu`):
193
+ The device where the tensors need to be located after load.
194
+ available options are all regular torch device locations.
195
+
196
+ Returns:
197
+ `(missing, unexpected): (List[str], List[str])`
198
+ `missing` are names in the model which were not modified during loading
199
+ `unexpected` are names that are on the file, but weren't used during
200
+ the load.
201
+ """
202
+ state_dict = load_file(filename, device=device)
203
+ model_state_dict = model.state_dict()
204
+ to_removes = _remove_duplicate_names(model_state_dict, preferred_names=state_dict.keys())
205
+ missing, unexpected = model.load_state_dict(state_dict, strict=False)
206
+ missing = set(missing)
207
+ for to_remove_group in to_removes.values():
208
+ for to_remove in to_remove_group:
209
+ if to_remove not in missing:
210
+ unexpected.append(to_remove)
211
+ else:
212
+ missing.remove(to_remove)
213
+ if strict and (missing or unexpected):
214
+ missing_keys = ", ".join([f'"{k}"' for k in sorted(missing)])
215
+ unexpected_keys = ", ".join([f'"{k}"' for k in sorted(unexpected)])
216
+ error = f"Error(s) in loading state_dict for {model.__class__.__name__}:"
217
+ if missing:
218
+ error += f"\n Missing key(s) in state_dict: {missing_keys}"
219
+ if unexpected:
220
+ error += f"\n Unexpected key(s) in state_dict: {unexpected_keys}"
221
+ raise RuntimeError(error)
222
+ return missing, unexpected
223
+
224
+
225
+ def save(tensors: Dict[str, torch.Tensor], metadata: Optional[Dict[str, str]] = None) -> bytes:
226
+ """
227
+ Saves a dictionary of tensors into raw bytes in safetensors format.
228
+
229
+ Args:
230
+ tensors (`Dict[str, torch.Tensor]`):
231
+ The incoming tensors. Tensors need to be contiguous and dense.
232
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
233
+ Optional text only metadata you might want to save in your header.
234
+ For instance it can be useful to specify more about the underlying
235
+ tensors. This is purely informative and does not affect tensor loading.
236
+
237
+ Returns:
238
+ `bytes`: The raw bytes representing the format
239
+
240
+ Example:
241
+
242
+ ```python
243
+ from safetensors.torch import save
244
+ import torch
245
+
246
+ tensors = {"embedding": torch.zeros((512, 1024)), "attention": torch.zeros((256, 256))}
247
+ byte_data = save(tensors)
248
+ ```
249
+ """
250
+ serialized = serialize(_flatten(tensors), metadata=metadata)
251
+ result = bytes(serialized)
252
+ return result
253
+
254
+
255
+ def save_file(
256
+ tensors: Dict[str, torch.Tensor],
257
+ filename: Union[str, os.PathLike],
258
+ metadata: Optional[Dict[str, str]] = None,
259
+ ):
260
+ """
261
+ Saves a dictionary of tensors into raw bytes in safetensors format.
262
+
263
+ Args:
264
+ tensors (`Dict[str, torch.Tensor]`):
265
+ The incoming tensors. Tensors need to be contiguous and dense.
266
+ filename (`str`, or `os.PathLike`)):
267
+ The filename we're saving into.
268
+ metadata (`Dict[str, str]`, *optional*, defaults to `None`):
269
+ Optional text only metadata you might want to save in your header.
270
+ For instance it can be useful to specify more about the underlying
271
+ tensors. This is purely informative and does not affect tensor loading.
272
+
273
+ Returns:
274
+ `None`
275
+
276
+ Example:
277
+
278
+ ```python
279
+ from safetensors.torch import save_file
280
+ import torch
281
+
282
+ tensors = {"embedding": torch.zeros((512, 1024)), "attention": torch.zeros((256, 256))}
283
+ save_file(tensors, "model.safetensors")
284
+ ```
285
+ """
286
+ serialize_file(_flatten(tensors), filename, metadata=metadata)
287
+
288
+
289
+ def load_file(filename: Union[str, os.PathLike], device: Union[str, int] = "cpu") -> Dict[str, torch.Tensor]:
290
+ """
291
+ Loads a safetensors file into torch format.
292
+
293
+ Args:
294
+ filename (`str`, or `os.PathLike`):
295
+ The name of the file which contains the tensors
296
+ device (`Union[str, int]`, *optional*, defaults to `cpu`):
297
+ The device where the tensors need to be located after load.
298
+ available options are all regular torch device locations.
299
+
300
+ Returns:
301
+ `Dict[str, torch.Tensor]`: dictionary that contains name as key, value as `torch.Tensor`
302
+
303
+ Example:
304
+
305
+ ```python
306
+ from safetensors.torch import load_file
307
+
308
+ file_path = "./my_folder/bert.safetensors"
309
+ loaded = load_file(file_path)
310
+ ```
311
+ """
312
+ result = {}
313
+ with safe_open(filename, framework="pt", device=device) as f:
314
+ for k in f.keys():
315
+ result[k] = f.get_tensor(k)
316
+ return result
317
+
318
+
319
+ def load(data: bytes) -> Dict[str, torch.Tensor]:
320
+ """
321
+ Loads a safetensors file into torch format from pure bytes.
322
+
323
+ Args:
324
+ data (`bytes`):
325
+ The content of a safetensors file
326
+
327
+ Returns:
328
+ `Dict[str, torch.Tensor]`: dictionary that contains name as key, value as `torch.Tensor` on cpu
329
+
330
+ Example:
331
+
332
+ ```python
333
+ from safetensors.torch import load
334
+
335
+ file_path = "./my_folder/bert.safetensors"
336
+ with open(file_path, "rb") as f:
337
+ data = f.read()
338
+
339
+ loaded = load(data)
340
+ ```
341
+ """
342
+ flat = deserialize(data)
343
+ return _view2torch(flat)
344
+
345
+
346
+ # torch.float8 formats require 2.1; we do not support these dtypes on earlier versions
347
+ _float8_e4m3fn = getattr(torch, "float8_e4m3fn", None)
348
+ _float8_e5m2 = getattr(torch, "float8_e5m2", None)
349
+
350
+ _SIZE = {
351
+ torch.int64: 8,
352
+ torch.float32: 4,
353
+ torch.int32: 4,
354
+ torch.bfloat16: 2,
355
+ torch.float16: 2,
356
+ torch.int16: 2,
357
+ torch.uint8: 1,
358
+ torch.int8: 1,
359
+ torch.bool: 1,
360
+ torch.float64: 8,
361
+ _float8_e4m3fn: 1,
362
+ _float8_e5m2: 1,
363
+ }
364
+
365
+ _TYPES = {
366
+ "F64": torch.float64,
367
+ "F32": torch.float32,
368
+ "F16": torch.float16,
369
+ "BF16": torch.bfloat16,
370
+ "I64": torch.int64,
371
+ # "U64": torch.uint64,
372
+ "I32": torch.int32,
373
+ # "U32": torch.uint32,
374
+ "I16": torch.int16,
375
+ # "U16": torch.uint16,
376
+ "I8": torch.int8,
377
+ "U8": torch.uint8,
378
+ "BOOL": torch.bool,
379
+ "F8_E4M3": _float8_e4m3fn,
380
+ "F8_E5M2": _float8_e5m2,
381
+ }
382
+
383
+
384
+ def _getdtype(dtype_str: str) -> torch.dtype:
385
+ return _TYPES[dtype_str]
386
+
387
+
388
+ def _view2torch(safeview) -> Dict[str, torch.Tensor]:
389
+ result = {}
390
+ for k, v in safeview:
391
+ dtype = _getdtype(v["dtype"])
392
+ if len(v["data"]) == 0:
393
+ # Workaround because frombuffer doesn't accept zero-size tensors
394
+ assert any(x == 0 for x in v["shape"])
395
+ arr = torch.empty(v["shape"], dtype=dtype)
396
+ else:
397
+ arr = torch.frombuffer(v["data"], dtype=dtype).reshape(v["shape"])
398
+ if sys.byteorder == "big":
399
+ arr = torch.from_numpy(arr.numpy().byteswap(inplace=False))
400
+ result[k] = arr
401
+
402
+ return result
403
+
404
+
405
+ def _tobytes(tensor: torch.Tensor, name: str) -> bytes:
406
+ if tensor.layout != torch.strided:
407
+ raise ValueError(
408
+ f"You are trying to save a sparse tensor: `{name}` which this library does not support."
409
+ " You can make it a dense tensor before saving with `.to_dense()` but be aware this might"
410
+ " make a much larger file than needed."
411
+ )
412
+
413
+ if not tensor.is_contiguous():
414
+ raise ValueError(
415
+ f"You are trying to save a non contiguous tensor: `{name}` which is not allowed. It either means you"
416
+ " are trying to save tensors which are reference of each other in which case it's recommended to save"
417
+ " only the full tensors, and reslice at load time, or simply call `.contiguous()` on your tensor to"
418
+ " pack it before saving."
419
+ )
420
+ if tensor.device.type != "cpu":
421
+ # Moving tensor to cpu before saving
422
+ tensor = tensor.to("cpu")
423
+
424
+ import ctypes
425
+
426
+ import numpy as np
427
+
428
+ # When shape is empty (scalar), np.prod returns a float
429
+ # we need a int for the following calculations
430
+ length = int(np.prod(tensor.shape).item())
431
+ bytes_per_item = _SIZE[tensor.dtype]
432
+
433
+ total_bytes = length * bytes_per_item
434
+
435
+ ptr = tensor.data_ptr()
436
+ if ptr == 0:
437
+ return b""
438
+ newptr = ctypes.cast(ptr, ctypes.POINTER(ctypes.c_ubyte))
439
+ data = np.ctypeslib.as_array(newptr, (total_bytes,)) # no internal copy
440
+ if sys.byteorder == "big":
441
+ NPDTYPES = {
442
+ torch.int64: np.int64,
443
+ torch.float32: np.float32,
444
+ torch.int32: np.int32,
445
+ # XXX: This is ok because both have the same width
446
+ torch.bfloat16: np.float16,
447
+ torch.float16: np.float16,
448
+ torch.int16: np.int16,
449
+ torch.uint8: np.uint8,
450
+ torch.int8: np.int8,
451
+ torch.bool: bool,
452
+ torch.float64: np.float64,
453
+ # XXX: This is ok because both have the same width and byteswap is a no-op anyway
454
+ _float8_e4m3fn: np.uint8,
455
+ _float8_e5m2: np.uint8,
456
+ }
457
+ npdtype = NPDTYPES[tensor.dtype]
458
+ # Not in place as that would potentially modify a live running model
459
+ data = data.view(npdtype).byteswap(inplace=False)
460
+ return data.tobytes()
461
+
462
+
463
+ def _flatten(tensors: Dict[str, torch.Tensor]) -> Dict[str, Dict[str, Any]]:
464
+ if not isinstance(tensors, dict):
465
+ raise ValueError(f"Expected a dict of [str, torch.Tensor] but received {type(tensors)}")
466
+
467
+ invalid_tensors = []
468
+ for k, v in tensors.items():
469
+ if not isinstance(v, torch.Tensor):
470
+ raise ValueError(f"Key `{k}` is invalid, expected torch.Tensor but received {type(v)}")
471
+
472
+ if v.layout != torch.strided:
473
+ invalid_tensors.append(k)
474
+ if invalid_tensors:
475
+ raise ValueError(
476
+ f"You are trying to save a sparse tensors: `{invalid_tensors}` which this library does not support."
477
+ " You can make it a dense tensor before saving with `.to_dense()` but be aware this might"
478
+ " make a much larger file than needed."
479
+ )
480
+
481
+ shared_pointers = _find_shared_tensors(tensors)
482
+ failing = []
483
+ for names in shared_pointers:
484
+ if len(names) > 1:
485
+ failing.append(names)
486
+
487
+ if failing:
488
+ raise RuntimeError(
489
+ f"""
490
+ Some tensors share memory, this will lead to duplicate memory on disk and potential differences when loading them again: {failing}.
491
+ A potential way to correctly save your model is to use `save_model`.
492
+ More information at https://huggingface.co/docs/safetensors/torch_shared_tensors
493
+ """
494
+ )
495
+
496
+ return {
497
+ k: {
498
+ "dtype": str(v.dtype).split(".")[-1],
499
+ "shape": v.shape,
500
+ "data": _tobytes(v, k),
501
+ }
502
+ for k, v in tensors.items()
503
+ }