Merge split safetensors files into one file

This article can be read in about 7 minutes.
PR

The purpose

Sometimes when you download a model from Hugging Face, it comes in multiple files, like model-00001-of-00003.safetensors, model-00002-of-00003.safetensors, and model-00003-of-00003.safetensors.

Some apps, including Stable Diffusion WebUI Forge, can’t use this format. Here’s how to merge them into one single file.

Caution

Some models didn’t work even after I merged them. I couldn’t figure out if the process was wrong or if there was a compatibility issue with the software and the models themselves.

Also, since this could be considered model editing, please proceed at your own risk.

PR

Preparation

Install Python by downloading it from the following page.

Download Python
The official home of the Python Programming Language
PR

Environment Setup

Create a working directory wherever you like.

Open a command prompt and run the following command. (This command is optional, but we recommend you run it if you’re unsure what it does.)

python -mvenv venv
venv\scripts\activate.bat

Check to make sure (venv) appears before the current directory in the command line.

After you’ve confirmed it’s there, execute the following command.

pip install safetensors
pip3 install torch torchvision torchaudio

The environment has been set up, but don’t close this command prompt. We’ll be using the same one to run the program.

PR

Generating Code

In your working directory, create a file named combine.py with the following content.

import os
import torch
from safetensors.torch import save_file, load_file

def combine_safetensors(input_dir, output_file_name="combined_model.safetensors"):
    print(f"Searching for safetensors files in: {input_dir}")

    files_to_load = sorted([f for f in os.listdir(input_dir) if f.endswith('.safetensors')])

    if not files_to_load:
        print("No .safetensors files found in the specified directory.")
        return

    print(f"Found files: {files_to_load}")

    combined_state_dict = {}
    for i, filename in enumerate(files_to_load):
        file_path = os.path.join(input_dir, filename)
        print(f"Loading part {i+1}/{len(files_to_load)}: {filename}")
        try:
            state_dict_part = load_file(file_path)
            combined_state_dict.update(state_dict_part)
            del state_dict_part
        except Exception as e:
            print(f"Error loading {filename}: {e}")
            return

    output_path = os.path.join(input_dir, output_file_name)
    print(f"Saving combined model to: {output_path}")

    try:
        save_file(combined_state_dict, output_path)
        print("Combination successful!")
    except Exception as e:
        print(f"Error saving combined file: {e}")

if __name__ == "__main__":
    input_folder = input("INPUT FOLDER: ")

    output_name = input("OUTPUT FILE: ")
    if not output_name:
        output_name = "combined_model.safetensors"
    elif not output_name.endswith('.safetensors'):
        output_name += '.safetensors'

    combine_safetensors(input_folder, output_name)
PR

Execution

Put the split safetensors into any folder you like. (It’s a good idea to put only the target files in the folder to keep things simple.)

Then, run the following command in the Command Prompt you used for the setup.

Python combine.py

When “INPUT FOLDER:” is displayed, enter the folder where you placed the split files.

Then, enter the desired name for the output file at the “OUTPUT FILE:” prompt.

After a short wait, a combined file will be saved with the specified name inside the input folder.

If you see the error ‘Error saving combined file:’ and it fails, try freeing up memory by closing other applications.

PR

Result

I successfully combined the split safetensors files into one.

comment

Copied title and URL