# safetensors\_rust.SafetensError while deserializing header: InvalidHeaderDeserialization

**URL:** <https://discuss.huggingface.co/t/safetensors-rust-safetenserror-while-deserializing-header-invalidheaderdeserialization/68831>\
**Category:** Beginners\
**Created:** [January 11, 2024, 6:56pm UTC](https://discuss.huggingface.co/t/safetensors-rust-safetenserror-while-deserializing-header-invalidheaderdeserialization/68831 "2024-01-11T18:56:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvind-s](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@arvind-s](https://discuss.huggingface.co/u/arvind-s)\
**Post date:** [January 11, 2024, 6:56pm UTC](https://discuss.huggingface.co/t/safetensors-rust-safetenserror-while-deserializing-header-invalidheaderdeserialization/68831/1 "2024-01-11T18:56:15Z")

</div>

Hi:  
I am trying to load a checkpoint after fine-tuning a codeLlama model using PEFT.  
This gives me an error:  
File “/home/ubuntu/.cache/pypoetry/virtualenvs/llama-VNDZLnVB-py3.8/lib/python3.8/site-packages/safetensors/torch.py”, line 308, in load\_file  
with safe\_open(filename, framework=“pt”, device=device) as f:  
safetensors\_rust.SafetensorError: Error while deserializing header: InvalidHeaderDeserialization

Here is my rather simple code for loading:  
base\_model = “codellama/CodeLlama-7b-hf”  
model = AutoModelForCausalLM.from\_pretrained(  
base\_model,  
load\_in\_4bit=True,  
torch\_dtype=torch.float16,  
device\_map=“auto”,  
)  
tokenizer = AutoTokenizer.from\_pretrained(“codellama/CodeLlama-7b-hf”)  
output\_dir = “fine-tuned-code-llama/checkpoint-400”  
model = PeftModel.from\_pretrained(model, output\_dir)

The model was fine-tuned in 4-bit mode. I am wondering if this is a bug loading safetensors or something I am doing incorrectly.  
Thank you!

---

<div class="post-metadata">

**Author:** ![Sneekit](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@Sneekit](https://discuss.huggingface.co/u/Sneekit)\
**Post date:** [February 23, 2024, 1:53pm UTC](https://discuss.huggingface.co/t/safetensors-rust-safetenserror-while-deserializing-header-invalidheaderdeserialization/68831/2 "2024-02-23T13:53:07Z")

</div>

> <https://github.com/slai-labs/get-beam/issues/94>
>
> The fine-tuning \[script\](https://github.com/slai-labs/get-beam/blob/main/example…s/finetune-llama/training.py) provided does not work with custom datasets. It appears there is a \[bug\](https://github.com/huggingface/transformers/issues/27397) related to PEFT. The \[InvalidHeaderDeserialization\](https://github.com/huggingface/transformers/issues/27397) message has been reported by other users. Strangely, the recommended changes of not calling torch.compile didn’t work for me. However, after looking at this \[comment\](https://github.com/huggingface/transformers/issues/27397#issuecomment-1804313865) I removed the following \[code\](https://github.com/slai-labs/get-beam/blob/354e856c513394f551440d1d73c8cc298d1c0826/examples/finetune-llama/training.py#L294-L296) and training completed successfully.

There is currently an issue in the compatibility between PEFT and Pytorch while using custom data training sets.

I encountered this issue due to the following optimizations before training. Removing these optimizations allowed my models to be correctly saved and loaded, however it did increase the VRAM requirements of training.

```auto
model.state_dict = (
lambda self, *_, **__: get_peft_model_state_dict(
self, old_state_dict()
)
).get(model, type(model))

if torch. __version__ >= "2" and sys.platform != "win32":
    model = torch.compile(model)

```
