You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you trained a model on the latest Git branch, or, received model weights from someone who has a newer version of Diffusers than you do, the sharded checkpoints will not be loadable.
To merge them, use the following Python code snippet:
defmerge_safetensors_files(directory):
json_file_name="diffusion_pytorch_model.safetensors.index.json"json_file_path=os.path.join(directory, json_file_name)
ifnotos.path.exists(json_file_path):
return# Step 2: Load the JSON file and extract the weight mapwithopen(json_file_path, "r") asfile:
data=json.load(file)
weight_map=data.get("weight_map")
ifweight_mapisNone:
raiseKeyError("'weight_map' key not found in the JSON file.")
# Collect all unique safetensors files from weight_mapfiles_to_load=set(weight_map.values())
all_tensors= {}
# Load tensors from each unique fileforfile_nameinfiles_to_load:
part_file_path=os.path.join(directory, file_name)
ifnotos.path.exists(part_file_path):
raiseFileNotFoundError(f"Part file {file_name} not found.")
withsafe_open(part_file_path, framework="pt", device="cpu") asf:
fortensor_keyinf.keys():
iftensor_keyinweight_map:
all_tensors[tensor_key] =f.get_tensor(tensor_key)
# Step 4: Save all loaded tensors into a single new safetensors fileoutput_file_path=os.path.join(directory, "diffusion_pytorch_model.safetensors")
save_file(all_tensors, output_file_path)
# Step 5: If the file now exists, remove the index and part filesifos.path.exists(output_file_path):
os.remove(json_file_path)
forfile_nameinfiles_to_load:
os.remove(os.path.join(directory, file_name))
logger.info(f"All tensors have been merged and saved into {output_file_path}")
This can be invoked such as:
merge_safetensors_files('/path/to/checkpoint')
Keep a backup copy, as it will remove the sharded files once the merge is complete.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
If you trained a model on the latest Git branch, or, received model weights from someone who has a newer version of Diffusers than you do, the sharded checkpoints will not be loadable.
To merge them, use the following Python code snippet:
This can be invoked such as:
Keep a backup copy, as it will remove the sharded files once the merge is complete.
Beta Was this translation helpful? Give feedback.
All reactions