]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Re-adding support for u16, u32, u64. (#629)
authorNicolas Patry <patry.nicolas@protonmail.com>
Thu, 3 Jul 2025 18:22:13 +0000 (20:22 +0200)
committerGitHub <noreply@github.com>
Thu, 3 Jul 2025 18:22:13 +0000 (20:22 +0200)
bindings/python/py_src/safetensors/torch.py

index 568d4aa71590764cdd41a3849ed56a398abcc484..4f5c801ff65cef8006b8464f5075c1522355841e 100644 (file)
@@ -41,7 +41,9 @@ def storage_size(tensor: torch.Tensor) -> int:
             return tensor.nelement() * _SIZE[tensor.dtype]
 
 
-def _filter_shared_not_shared(tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]) -> List[Set[str]]:
+def _filter_shared_not_shared(
+    tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]
+) -> List[Set[str]]:
     filtered_tensors = []
     for shared in tensors:
         if len(shared) < 2:
@@ -69,7 +71,11 @@ def _filter_shared_not_shared(tensors: List[Set[str]], state_dict: Dict[str, tor
 def _find_shared_tensors(state_dict: Dict[str, torch.Tensor]) -> List[Set[str]]:
     tensors = defaultdict(set)
     for k, v in state_dict.items():
-        if v.device != torch.device("meta") and storage_ptr(v) != 0 and storage_size(v) != 0:
+        if (
+            v.device != torch.device("meta")
+            and storage_ptr(v) != 0
+            and storage_size(v) != 0
+        ):
             # Need to add device as key because of multiple GPU.
             tensors[(v.device, storage_ptr(v), storage_size(v))].add(k)
     tensors = list(sorted(tensors.values()))
@@ -78,7 +84,9 @@ def _find_shared_tensors(state_dict: Dict[str, torch.Tensor]) -> List[Set[str]]:
 
 
 def _is_complete(tensor: torch.Tensor) -> bool:
-    return tensor.data_ptr() == storage_ptr(tensor) and tensor.nelement() * _SIZE[tensor.dtype] == storage_size(tensor)
+    return tensor.data_ptr() == storage_ptr(tensor) and tensor.nelement() * _SIZE[
+        tensor.dtype
+    ] == storage_size(tensor)
 
 
 def _remove_duplicate_names(
@@ -97,7 +105,9 @@ def _remove_duplicate_names(
     shareds = _find_shared_tensors(state_dict)
     to_remove = defaultdict(list)
     for shared in shareds:
-        complete_names = set([name for name in shared if _is_complete(state_dict[name])])
+        complete_names = set(
+            [name for name in shared if _is_complete(state_dict[name])]
+        )
         if not complete_names:
             raise RuntimeError(
                 "Error while trying to find names to remove to save state dict, but found no suitable name to keep"
@@ -207,7 +217,9 @@ def load_model(
     """
     state_dict = load_file(filename, device=device)
     model_state_dict = model.state_dict()
-    to_removes = _remove_duplicate_names(model_state_dict, preferred_names=state_dict.keys())
+    to_removes = _remove_duplicate_names(
+        model_state_dict, preferred_names=state_dict.keys()
+    )
 
     reverse_to_remove = {}
     for key, to_remove_group in to_removes.items():
@@ -273,7 +285,9 @@ def load_model(
     return missing, unexpected
 
 
-def save(tensors: Dict[str, torch.Tensor], metadata: Optional[Dict[str, str]] = None) -> bytes:
+def save(
+    tensors: Dict[str, torch.Tensor], metadata: Optional[Dict[str, str]] = None
+) -> bytes:
     """
     Saves a dictionary of tensors into raw bytes in safetensors format.
 
@@ -337,7 +351,9 @@ def save_file(
     serialize_file(_flatten(tensors), filename, metadata=metadata)
 
 
-def load_file(filename: Union[str, os.PathLike], device: Union[str, int] = "cpu") -> Dict[str, torch.Tensor]:
+def load_file(
+    filename: Union[str, os.PathLike], device: Union[str, int] = "cpu"
+) -> Dict[str, torch.Tensor]:
     """
     Loads a safetensors file into torch format.
 
@@ -402,11 +418,14 @@ _float4_e2m1_x2 = getattr(torch, "float4_e2m1fn_x2", None)
 
 _SIZE = {
     torch.int64: 8,
+    torch.uint64: 8,
     torch.float32: 4,
     torch.int32: 4,
+    torch.uint32: 4,
     torch.bfloat16: 2,
     torch.float16: 2,
     torch.int16: 2,
+    torch.uint16: 2,
     torch.uint8: 1,
     torch.int8: 1,
     torch.bool: 1,
@@ -423,11 +442,11 @@ _TYPES = {
     "F16": torch.float16,
     "BF16": torch.bfloat16,
     "I64": torch.int64,
-    # "U64": torch.uint64,
+    "U64": torch.uint64,
     "I32": torch.int32,
-    # "U32": torch.uint32,
+    "U32": torch.uint32,
     "I16": torch.int16,
-    # "U16": torch.uint16,
+    "U16": torch.uint16,
     "I8": torch.int8,
     "U8": torch.uint8,
     "BOOL": torch.bool,
@@ -517,12 +536,16 @@ def _tobytes(tensor: torch.Tensor, name: str) -> bytes:
 
 def _flatten(tensors: Dict[str, torch.Tensor]) -> Dict[str, Dict[str, Any]]:
     if not isinstance(tensors, dict):
-        raise ValueError(f"Expected a dict of [str, torch.Tensor] but received {type(tensors)}")
+        raise ValueError(
+            f"Expected a dict of [str, torch.Tensor] but received {type(tensors)}"
+        )
 
     invalid_tensors = []
     for k, v in tensors.items():
         if not isinstance(v, torch.Tensor):
-            raise ValueError(f"Key `{k}` is invalid, expected torch.Tensor but received {type(v)}")
+            raise ValueError(
+                f"Key `{k}` is invalid, expected torch.Tensor but received {type(v)}"
+            )
 
         if v.layout != torch.strided:
             invalid_tensors.append(k)