< Skip to content

Convert pth And ckpt To Safetensors

Why you should consider converting your files to .safetensors. If you have played around with a locally installed version of generative AI you have probably downloaded one or a few checkpoints. Checkpoints can be described as fine-tuned (or merged) versions of the original AI model it’s based off.

Let’s say you download the checkpoint ZavyChromaXL to use with your locally installed UI of choice, then that checkpoint is originally based off the SDXL 1.0 checkpoint. And between SDXL 1.0 and ZavyChromaXL are a lot of trained, fine-tuned and merged models, that have been fine-tuned, merged or trained again and again in order to produce the type of images the creator wants.

The size of a checkpoint varies, but are usually several gigabyte and contains a lot of data. It would be hard for most of us to detect if any malicious code were baked in to a checkpoint. Checkpoints have previously (and to some extent still are) available with the file extensions ckpt (checkpoint file) or pth (PyTorch file), both which contain open code that could possibly be malicious and be executed,

Safetensors save the information binary instead, which means that you’re not only less likely to get exposed my malicious code but the files themselves are usually smaller and are faster to use. According to Huggingface safetensors are 77 times faster when using CPU and about twice as fast when using GPU.

But what to do with all the pth and ckpt files that are already out there? You could stop using them, or wait until someone have converted them for you (which probably is going to take a lot of time) or you can convert them yourself.

Converting to safetensors

I was searching for ways to convert some of the checkpoints I’m using, but the conversion on Huggingface only accepts .bin files and all other conversions I found were in Python script form, which requires you to know at least some Python to use. Which I don’t. I tried to get a couple of script to work, but failed.

Then I figured out that Swarm will convert both ckpt, pth and bin files to safetensors. As a bonus Swarm is also a working UI for Stable Diffusion 3 and also works as a UI for SDXL and SD 1.5. Even though Swarm isn’t that impressive when it comes to versatility it’s a good start for a beginner.

Once Swarm is installed it will search (some) directories for files that can be converted. The folders that will be searched is mainly the folder where you keep your checkpoints, VAE, LoRa and ControlNet. If you have a file you want to convert that isn’t located in any of these folders, you can temporarily move it to your checkpoint folder.

Once you’ve opened Swarm in your web browser, go to utilities –> pickle to safetensors.

You will see an option to convert the files to FP16 as well. I have done this with large checkpoints such as the base model for PixArt without any issues. However, when converting smaller files such as upscalers, I find that it’s better to not tick the box for FP16 conversion. It might just be my imagination, but it seems to me that the upscalers loses quality when converting to FP16. Besides, the upscalers aren’t that big anyway, so you won’t be saving a lot of space on it anyway.

All files you have converted will be saved in a backup folder. Make sure your converted files are working as intended, and on the platform you plan to use them on before deleting the backup folder. I have noticed that the upscalers I have converted won’t load in Forge and Automatic1111 UI, but loads without any issue in ComfyUI.

Dela med dina vänner
Published inAI ImagesEnglishTech