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?
1 Like
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.