avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Transitioning to Safety: Converting .ckpt to .safetensors

Introduction

In the era of evolving machine learning models, safeguarding data integrity and security is of paramount importance. One such stride towards ensuring a secure tensor storage is the adoption of the .safetensors format over the traditional .ckpt (checkpoint) files. SafeTensors provide a more secure means of storing tensor data by preventing the inclusion of arbitrary and potentially malicious Python code, a risk associated with other formats such as pickles. Here’s how you can make the transition from .ckpt to .safetensors and step up your model’s safety game:

Converting .ckpt to .safetensors: A Manual Approach

If you prefer a hands-on approach, a snippet of Python code can get the job done. Here's how you could go about it:

import torch
from safetensors.torch import save_file

# Load the checkpoint
weights = torch.load("sd-v1-4.ckpt")["state_dict"]

# Save the weights in the safetensors format
save_file(weights, "model.safetensors")

This code utilizes the torch library to load the weights from your Stable Diffusion checkpoint and the safetensors library to save these weights securely in the .safetensors format.

Leveraging Tools for Conversion

For those who seek a more user-friendly method, tools like "Safe & Stable" offer a graphical user interface (GUI) to convert .ckpt files to .safetensors effortlessly. Additionally, the Model Converter extension simplifies the conversion process further. Here's a simplified step-by-step procedure using the Model Converter extension:

  1. Install and open the Model Converter extension.
  2. Pick your model from the dropdown menu within the extension.
  3. Optionally, provide a custom name for your model.
  4. Select the desired precision (FP32 is recommended for full precision).
  5. Under "Pruning Methods," select "disabled" to convert without pruning.
  6. Under "Checkpoint Format," tick "safetensors" and untick "ckpt."
  7. Press "Run" and wait for your model to be saved securely in the .safetensors format.

Why Make The Switch?

SafeTensors come with the promise of heightened security which is quintessential in the modern digital landscape. Unlike the pickle format which can include arbitrary and potentially harmful Python code, SafeTensors store tensor data as pure data, significantly reducing the risk of malicious code injection. Moreover, the .safetensors format is designed to accommodate a streamlined conversion process, making it a sensible choice for developers and data scientists alike.

Conclusion

Transitioning to SafeTensors not only fortifies the security of your tensor data but also paves the way for a safer machine learning ecosystem. Whether you choose a manual conversion or leverage available tools, the move to .safetensors is a prudent step towards enhanced data security and integrity in your ML projects.