huggingface / safetensors

Simple, safe way to store and distribute tensors
https://huggingface.co/docs/safetensors
Apache License 2.0
2.79k stars 189 forks source link

fix(save_file): add force_contiguous(default=True) to save_file #530

Open dbyoung18 opened 1 week ago

dbyoung18 commented 1 week ago

What does this PR do?

Refering the inferface of save_model, add force_contiguous(default=True) to force the state_dict to be saved as contiguous tensors, and finally into raw bytes in safetensors format.

The fix could contribute such scenario, when running workloads w/ channels_last format in PyTorch, but need finally saved to .safetensor by calling transformers::safe_save_file. Meanwhile, it could also align the semantics between save_model & save_file.

A related issue encountered such scenario: https://github.com/huggingface/safetensors/issues/308

dbyoung18 commented 1 week ago

@Narsil Could you help review the PR?