Skip to content

Instantly share code, notes, and snippets.

@marduk191
Forked from silveroxides/pt_to_safe.py
Last active July 9, 2025 00:44
Show Gist options
  • Select an option

  • Save marduk191/d0f6285e73fd4615e2fe3d61bd24ea42 to your computer and use it in GitHub Desktop.

Select an option

Save marduk191/d0f6285e73fd4615e2fe3d61bd24ea42 to your computer and use it in GitHub Desktop.
Pytorch Model to Safetensors converter
# created by https://gist.github.com/silveroxides
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)
@marduk191
Copy link
Author

batch script to batch folders lol

`call I:\AI\ComfyUI\venv\Scripts\activate
@echo off
setlocal enabledelayedexpansion

:: Set the input directory
set "input_dir=%~dp0in"

:: Check if input directory exists
if not exist "%input_dir%" (
echo Input directory does not exist: %input_dir%
mkdir %input_dir%
)

echo Scanning directory: %input_dir%

:: Loop through all files in the input directory
for %%f in ("%input_dir%*") do (
:: Check if extension is .pth
if /i "%%~xf"==".pth" (
echo .pth file detected: %%~nxf
echo Converting to safetensors format.
call python %~dp0tools\pt_to_safe.py "%%f"
) else (
echo File extension is %%~xf, continuing with normal processing...
:: Add your normal processing code here
echo Skipping %%~nxf
)
)

echo Done converting files.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment