Upload folder using huggingface_hub
Browse files- fp16ify.py +55 -0
- model.safetensors +2 -2
fp16ify.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Convert all tensors that are not float16 in a .safetensors file
|
| 4 |
+
into float16 and save the result. Requires torch and safetensors.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from safetensors.torch import load_file, save_file
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def convert_to_f16(inp: Path, out: Path | None = None, dry_run: bool = False) -> None:
|
| 15 |
+
"""
|
| 16 |
+
Load `inp`, cast every non‑float16 tensor to float16, then save to `out`.
|
| 17 |
+
If `out` is None, appends `.f16.safetensors` to the original name.
|
| 18 |
+
"""
|
| 19 |
+
inp = Path(inp)
|
| 20 |
+
out = Path(out) if out is not None else inp.with_suffix(".f16.safetensors")
|
| 21 |
+
|
| 22 |
+
weights = load_file(str(inp))
|
| 23 |
+
new_weights: dict[str, torch.Tensor] = {}
|
| 24 |
+
changed = False
|
| 25 |
+
|
| 26 |
+
for name, tensor in weights.items():
|
| 27 |
+
if tensor.dtype is torch.float16:
|
| 28 |
+
new_weights[name] = tensor
|
| 29 |
+
else:
|
| 30 |
+
changed = True
|
| 31 |
+
new_weights[name] = tensor.half()
|
| 32 |
+
print(f"{name}: {tensor.dtype} -> float16")
|
| 33 |
+
|
| 34 |
+
if dry_run:
|
| 35 |
+
print("Dry run: no file written.")
|
| 36 |
+
return
|
| 37 |
+
|
| 38 |
+
save_file(new_weights, str(out))
|
| 39 |
+
print(f"Saved converted model to {out} ({'modified' if changed else 'no change needed'}).")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main() -> None:
|
| 43 |
+
parser = argparse.ArgumentParser(
|
| 44 |
+
description="Cast every non‑float16 tensor in a .safetensors file to float16."
|
| 45 |
+
)
|
| 46 |
+
parser.add_argument("input", help="Path to the input .safetensors file")
|
| 47 |
+
parser.add_argument("-o", "--output", help="Path for the output file")
|
| 48 |
+
parser.add_argument("--dry-run", action="store_true", help="Scan only; don’t write output")
|
| 49 |
+
args = parser.parse_args()
|
| 50 |
+
|
| 51 |
+
convert_to_f16(args.input, args.output, args.dry_run)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
main()
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a7653cd597f246b9240ff1253de6c1d4e9cd7e67181ba35211562d626aa277b9
|
| 3 |
+
size 12102726856
|