# Molformer model training error

**URL:** <https://discuss.huggingface.co/t/molformer-model-training-error/147291>\
**Category:** 🤗Transformers\
**Created:** [March 25, 2025, 6:45am UTC](https://discuss.huggingface.co/t/molformer-model-training-error/147291 "2025-03-25T06:45:45Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [March 25, 2025, 7:46am UTC](https://discuss.huggingface.co/t/molformer-model-training-error/147291/3 "2025-03-25T07:46:55Z")

</div>

It’s probably possible to get around this, but it would be even better if you could find out why it’s no longer contiguous and fix it.

> **[Trainer](https://huggingface.co/docs/transformers/en/main_classes/trainer#transformers.TrainingArguments.save_safetensors)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

```py
training_args = TrainingArguments(
    output_dir="./results",
    per_device_train_batch_size=1,
    num_train_epochs=10,
    logging_dir="./logs",
    logging_steps=1,
    save_safetensors=False, # added
    #################save_strategy = "epoch"
)

```

> [@Using Trainer to save a Bartforsequenceclassification model](https://discuss.huggingface.co/t/using-trainer-to-save-a-bartforsequenceclassification-model/81606):
>
> trainer.train() trainer.save\_model() It gives me the error: modeling\_utils.py", line 2546, in save\_pretrained raise RuntimeError( RuntimeError: The weights trying to be saved contained shared tensors [{‘model.shared.weight’, ‘model.encoder.embed\_tokens.weight’, ‘model.decoder.embed\_tokens.weight’}] that are mismatching the transformers base configuration. Try saving using safe\_serialization=False or remove this tensor sharing. Could anyone please tell me why is it happening? and how to so…

> <https://github.com/huggingface/transformers/issues/28293>
>
> \### System Info
> 
> Transformers version: 4.36.2
> pytorch version: 2.1.1
> Python… version: 3.10.13
> 
> \### Who can help?
> 
> \_No response\_
> 
> \### Information
> 
> \- \[\] The official example scripts
> \- \[X\] My own modified scripts
> 
> \### Tasks
> 
> \- \[\] An officially supported task in the \`examples\` folder (such as GLUE/SQuAD, ...)
> \- \[X\] My own task or dataset (give details below)
> 
> \### Reproduction
> 
> 
> Fine-tuning mt5 model on a task using transformers trainer, and try to save the model, then the following error occurs. 
>  
> \`\`\`
> File "/home/xxx/xxx/xxx/run.py", line 17, in main
> experiment.run()
> File "/home/xxx/xxx/xxx/experiments.py", line 162, in run
> self.train()
> File "/home/xxx/xxx/xxx/experiments.py", line 207, in train
> trainer.save\_model() # Saves the tokenizer too for easy upload
> File "/home/xxx/.local/lib/python3.10/site-packages/transformers/trainer.py", line 2849, in save\_model
> self.\_save(output\_dir)
> File "/home/xxx/.local/lib/python3.10/site-packages/transformers/trainer.py", line 2909, in \_save
> self.model.save\_pretrained(
> File "/home/xxx/.local/lib/python3.10/site-packages/transformers/modeling\_utils.py", line 2376, in save\_pretrained
> safe\_save\_file(shard, os.path.join(save\_directory, shard\_file), metadata={"format": "pt"})
> File "/home/xxx/.local/lib/python3.10/site-packages/safetensors/torch.py", line 281, in save\_file
> serialize\_file(\_flatten(tensors), filename, metadata=metadata)
> File "/home/xxx/.local/lib/python3.10/site-packages/safetensors/torch.py", line 475, in \_flatten
> return {
> File "/home/xxx/.local/lib/python3.10/site-packages/safetensors/torch.py", line 479, in \<dictcomp\>
> "data": \_tobytes(v, k),
> File "/home/xxx/.local/lib/python3.10/site-packages/safetensors/torch.py", line 396, in \_tobytes
> raise ValueError(
> ValueError: You are trying to save a non contiguous tensor: \`encoder\_decoder.encoder.block.0.layer.0.SelfAttention.q.weight\` which is not allowed. It either means you are trying to save tensors which are reference of each other in which case it's recommended to save only the full tensors, and reslice at load time, or simply call \`.contiguous()\` on your tensor to pack it before saving.
> \`\`\`
> 
> \### Expected behavior
> 
> Fine-tune mt5 model, and try to save the fine-tuned model, it renders the above error, and modifying \`transformers/modeling\_utils.py\` file with \`state\_dict= {k:v.contiguous() for k,v in state\_dict.items()}\` solves the problem.

---

_[View the full topic](https://discuss.huggingface.co/t/molformer-model-training-error/147291)._
