# RuntimeError When Saving Phi 3.5 Vision Due to Shared Tensors

**URL:** <https://discuss.huggingface.co/t/runtimeerror-when-saving-phi-3-5-vision-due-to-shared-tensors/116457>\
**Category:** 🤗Transformers\
**Created:** [November 9, 2024, 1:34am UTC](https://discuss.huggingface.co/t/runtimeerror-when-saving-phi-3-5-vision-due-to-shared-tensors/116457 "2024-11-09T01:34:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hobscrk777](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hobscrk777](https://discuss.huggingface.co/u/hobscrk777)\
**Post date:** [November 9, 2024, 1:34am UTC](https://discuss.huggingface.co/t/runtimeerror-when-saving-phi-3-5-vision-due-to-shared-tensors/116457/1 "2024-11-09T01:34:09Z")

</div>

I’m trying to fine-tune Phi 3.5 Vision using `transformers`. However, I’m running into an issue trying to save the model during or after training. See below for a minimal reproducible example.

Does anyone have any pointers? This issue has been reported in a few other locations - see below.

1. [Saving Phi 3 vision fails due to tensor sharing · Issue #32354 · huggingface/transformers · GitHub](https://github.com/huggingface/transformers/issues/32354)
2. [Using Trainer to save a Bartforsequenceclassification model](https://discuss.huggingface.co/t/using-trainer-to-save-a-bartforsequenceclassification-model/81606)

The error suggests “saving using `safe_serialization=False`”…but I’m not sure what the implications of that are.

### Minimal Reproducible Example

```auto
from transformers import AutoModelForCausalLM
model_id = "microsoft/Phi-3.5-vision-instruct"
model = AutoModelForCausalLM.from_pretrained(
    model_id, device_map="cuda", trust_remote_code=True, torch_dtype="auto"
)
model.save_pretrained("out", safe_serialization=True)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/AWSBedrockScienceModelDistillationTraining/.venv/lib/python3.12/site-packages/transformers/modeling_utils.py", line 2958, in save_pretrained
    raise RuntimeError(
RuntimeError: The weights trying to be saved contained shared tensors [{'model.embed_tokens.weight', 'model.vision_embed_tokens.wte.weight'}] that are mismatching the transformers base configuration. Try saving using `safe_serialization=False` or remove this tensor sharing.

```

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [November 9, 2024, 1:48am UTC](https://discuss.huggingface.co/t/runtimeerror-when-saving-phi-3-5-vision-due-to-shared-tensors/116457/2 "2024-11-09T01:48:32Z")

</div>

It seems that the problem is with the model itself.

> <https://github.com/kazuar/Phi3-Vision-ft/issues/2>
>
> When running the \`finetune.sh\` script with my own dataset, I encountered the fol…lowing error during checkpoint / saving the model:
> \`\`\`
> RuntimeError: The weights trying to be saved contained shared tensors \[{'model.vision\_embed\_tokens.wte.weight', 
> 'model.embed\_tokens.weight'}\] that are mismatching the transformers base configuration.
> Try saving using \`safe\_serialization=False\` or remove this tensor sharing.
> \`\`\`
> 
> Setting \`safe\_serialization=False\` resulted in a model that wasn't able to load.
> @2U1 did you encounter this error? (opened it here because https://github.com/2U1/Phi3-Vision-ft doesn't have an issues tab)
