Created
June 28, 2025 10:18
-
-
Save silveroxides/d6fd22d65c2bd3cf80bad6e81ff1c9c8 to your computer and use it in GitHub Desktop.
Pytorch Model to Safetensors converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| from safetensors.torch import save_file | |
| import argparse | |
| import os | |
| def convert_pt_to_safetensors(model_path, safe_path): | |
| model = torch.load(model_path, map_location="cpu", weights_only=False) | |
| metadata = {"format":"pt"} | |
| save_file(model, safe_path, metadata) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Converts a PyTorch model to Safetensors.") | |
| parser.add_argument("model_path", type=str, help="Path to the PyTorch model file") | |
| args = parser.parse_args() | |
| model_path = args.model_path | |
| file_path = os.path.splitext(model_path)[0] | |
| safe_path = file_path + ".safetensors" | |
| convert_pt_to_safetensors(model_path, safe_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment