Create convert.py
Browse files- convert.py +18 -0
convert.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file, save_file
|
| 3 |
+
|
| 4 |
+
def lazy_load_and_convert(input_file_path, output_file_path):
|
| 5 |
+
# Load the safetensors file lazily
|
| 6 |
+
lazy_tensors = load_file(input_file_path, device="cuda:0")
|
| 7 |
+
|
| 8 |
+
# Convert each tensor to torch.float8_e4m3fn
|
| 9 |
+
converted_tensors = {key: value.to(torch.float8_e4m3fn) for key, value in lazy_tensors.items()}
|
| 10 |
+
|
| 11 |
+
# Save the converted tensors to a new safetensors file
|
| 12 |
+
save_file(converted_tensors, output_file_path)
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
input_file_path = "path/to/your/input_file.safetensors"
|
| 16 |
+
output_file_path = "path/to/your/output_file.safetensors"
|
| 17 |
+
|
| 18 |
+
lazy_load_and_convert(input_file_path, output_file_path)
|