Combine the split safetensors files into a single file.

この記事は約7分で読めます。
スポンサーリンク

Things I want to do

When you download a model from HuggingFace or similar software, it may be split into multiple files such as model-00001-of-00003.safetensors, model-00002-of-00003.safetensors, and model-00003-of-00003.safetensors.

Since some apps cannot use this file format (StableDiffusion WebUI Forge doesn’t seem to work), I’ll show you how to combine them into a single file.

Notice

Some models were unusable even after combining them. I couldn’t tell if it was a problem with the method or a problem with the original software and model.

Please use this at your own risk, as it may involve editing the model.

スポンサーリンク

Prepare

Download and install Python from the following page.

Download Python
The official home of the Python Programming Language
スポンサーリンク

Creating an environment

Create a working folder in your desired location.

Open the command prompt and execute the following command. (The following command is optional, but it is recommended to execute it if you don’t understand its meaning.)

python -mvenv venv
venv\scripts\activate.bat

Make sure that ‘(venv)’ is prefixed to the current folder in the command line.

Once you’ve confirmed that it says (venv), run the following command.

pip install safetensors
pip3 install torch torchvision torchaudio

The environment setup is now complete, but please do not close the command prompt as you will be using the same command prompt to execute the program.

スポンサーリンク

Code Creation

Create a file named combine.py in your working folder 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)
スポンサーリンク

execution

Place the split safetensors files into any folder. (It’s recommended to place only the target files in the folder for clarity.)

Execute the following command in the command prompt used to set up the environment.

Python combine.py

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

Next, ‘OUTPUT FILE:’ will be displayed, so enter the output file name.

After a short wait, the combined files will be saved in the INPUT FOLDER with the name specified in OUTPUT FILE.

If you see the message ‘Error saving combined file:’ and the process fails, please free up memory by closing other applications.

スポンサーリンク

Result

I was able to combine the split safetensors files into a single file.

コメント

タイトルとURLをコピーしました