Replies: 4 comments 4 replies
-
To merge the split import safetensors.torch
merge_state_dict ={}
files = ["name1.safetensors", "name2.safetensors"...] #file you want to be merged
merged_file = "merged_file.safetensors"
for file in files:
load_files_dict = safetensors.torch.load_file(your.safetensors files)
merge_state_dict.update(load_files_dict)
# save
safetensors.torch.save_file(merge_state_dict, merged_file)
this merged_file will be now saved to the combined state dictionary with .safetensors file |
Beta Was this translation helpful? Give feedback.
-
While finetuning an LLM on a local cluster, I needed to reshard safetensor files, so I created a code that merges them then resplits them into N parts. This might prove useful to you if you're still looking for a solution, or for other people who might refer to this page when facing the same problem. Here is the code i made for it : https://github.com/NotTheStallion/reshard-safetensors |
Beta Was this translation helpful? Give feedback.
-
None of the non-indexed methods will work well, because many models rely on a index.json file to map the different layers, etc to the relevant files. You will end up with a corrupted or "functional", but damaged model-file (depending on if the layer order is sequential or not to each file, which is unlikely for larger models).
I havent found a unified cli tool that is official to huggingface/diffusers yet, but the solution just consists of loading the model and running ".save_pretrained" on it. General Solution (For any Huggingface Pipeline compatible model)
You can set If it has errors, make sure you load the correct pipeline, for diffusers: All i i know is UPDATE: Errata: |
Beta Was this translation helpful? Give feedback.
-
SolutionThe simplest, most reliable,officially supported method ive found (for purposes of un-sharding models) is to load and save the model with a really high Note: Your model does not need to be on HF, for this to work, as it relies on the core Mixins/Helpers. Method
ResultThen you get this:
Whole model repo is un-sharded, (VAE, Transformers, etc) Note: You can easily make it only do one component, but for some users this way may actually be better, as all included components(VAE, Transformers, text-encoders) required for it to word are now each(separately) in a single file. Note: if you want to read from local filesystem, look at the https://huggingface.co/docs/huggingface_hub/main/en/guides/integrations#frompretrained Codefrom diffusers import DiffusionPipeline
# Sharded diffusers to single file example, Details: HuggingFace library/pipeline agnostic, read the HF docs: https://huggingface.co/docs
model = DiffusionPipeline.from_pretrained("Wan-AI/Wan2.1-T2V-14B-Diffusers")
print("Loaded model, saving...")
model.save_pretrained("./unsharded-out-dir", max_shard_size="2TB", safe_serialization=True)
print("Saved Model...") Areas for improvementIf someone could find a pipeline/task/framework agnostic solution, that would be nice, lossless interoperable weight format's like Pipeline agnostic hack (Transformers, Diffusers, or any of these)HowTo: Replace with any pipeline/model-type, from model page's1. Visit Model Page and click on
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there, I got diffusion_pytorch_model-00001-of-00003.safetensors, diffusion_pytorch_model-00002-of-00003.safetensors and diffusion_pytorch_model-00003-of-00003.safetensors after full finetuned Flux with Ostris AI tool kit, I guess at this point I will have to fusion/merge theses 3 parts for created the whole checkpoint usable for a1111 Forge . Is anyone know a script or a tool who can allow this last step ?
Thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions