.bin to safetensors without publishing it on hub?

I have followed Train a diffusion model to create own model just for learning. In the end I got “.bin” file, but I cannot load it on Stable Diffusion.

I found out that there is a converter in Train a diffusion model but it asks me model_id on hub. Is there any way to convert this “.bin” to .safetensors locally without any kind of uploadings? If so, with what tools?

Hi! You can check the source code of the Convert to Safetensors space to find how to do this locally.

3 Likes

Thank you very much, this did it! I cloned the source and modified convert.py to suit my needs.

In case somebody else wants to make this work also then I changed the code to from main function to call convert_file(“diffusion_pytorch_model.bin”, “output.safetensors”) and commented out #os.makedirs(dirname, exist_ok=True) line.

I also removed lots of functions before I noticed that I could just call the convert_file directly.

6 Likes

I wonder does it works on llm models eg:Llama3.18B,I got a model with four.bin files

I think it will work if you change the code slightly. (Because the filename is a magic word.)
Or if you use huggingface_hub.save_torch_model/ save_torch_state_dict instead of safetensors.torch.save_file, you’ll be able to handle sharded files seamlessly.

But the load function is not yet implemented, so you’ll have to make your own it…
I see the author of the space above is also doing it manually.
If not safetensors, torch.save and torch.load should be fine.

with torch.no_grad():
    def load_sharded_safetensors(path: str):
        import glob
        sd = {}
        try:
            for filepath in glob.glob(f"{path}/*.safetensors"):
                sharded_sd = load_file(str(filepath), device="cpu")
                for k, v in sharded_sd.items():
                    sharded_sd[k] = v.to(device="cpu")
                sd = sd | sharded_sd.copy()
                del sharded_sd
        except Exception as e:
                print(e)
        return sd

If the model is too large to fit in RAM, it is better to load, convert, save, and repeat for each file.

can also be used this way

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from convert import convert_file

if __name__ == "__main__":
    convert_file("/path/to/unet/diffusion_pytorch_model.bin",
    "/path/to/unet/diffusion_pytorch_model.safetensors",
    discard_names=[])
  • And run on terminal
 python3 convert_bin_to_safetensor.py

:call_me_hand:

3 Likes

The load function of the sharded model of torch has started to be implemented in HF Hub.

Like this:

from diffusers import StableDiffusionXLPipeline, AutoencoderKL
import torch

load_path = "./single_file.safetensors"
device = "cuda" if torch.cuda.is_available() else "cpu"

pipe = StableDiffusionXLPipeline.from_single_file(load_path, torch_dtype=torch.float16).to(device)
pipe.vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device) # to avoid a bug of SDXL standard VAE