# Transformer's Trainer Memory shared Error

**URL:** <https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381>\
**Category:** 🤗Transformers\
**Created:** [December 5, 2024, 10:07am UTC](https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381 "2024-12-05T10:07:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spencer1129](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/spencer1129/32/37005_2.png) [@spencer1129](https://discuss.huggingface.co/u/spencer1129)\
**Post date:** [December 5, 2024, 10:07am UTC](https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381/1 "2024-12-05T10:07:56Z")

</div>

I performed fine tuning flan-t5-small using peft. here is my code

```auto
#loading dataset
huggingface_dataset_name = "knkarthick/dialogsum"

dataset = load_dataset(huggingface_dataset_name)

dataset

#loading original model

model_name='google/flan-t5-base'
original_model = AutoModelForSeq2SeqLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to('cuda')
tokenizer = AutoTokenizer.from_pretrained(model_name)

def tokenize_function(example):
    start_prompt = 'Summarize the following conversation.\n\n'
    end_prompt = '\n\nSummary: '
    prompt = [start_prompt + dialogue + end_prompt for dialogue in example["dialogue"]]
    example['input_ids'] = tokenizer(prompt, padding="max_length", truncation=True, return_tensors="pt").input_ids
    example['labels'] = tokenizer(example["summary"], padding="max_length", truncation=True, return_tensors="pt").input_ids
    return example

#The dataset actually contains 3 diff splits: train, validation, test.
#The tokenize_function code is handling all data across all splits in batches.

tokenized_datasets = dataset.map(tokenize_function, batched=True)
tokenized_datasets = tokenized_datasets.remove_columns(['id', 'topic', 'dialogue', 'summary',])

#peft config
from peft import LoraConfig, get_peft_model, TaskType

lora_config = LoraConfig(
    r=32, # Rank
    lora_alpha=32,
    target_modules=["q", "v"],
    lora_dropout=0.05,
    bias="none",
    task_type=TaskType.SEQ_2_SEQ_LM # FLAN-T5
)

peft_model = get_peft_model(original_model, 
                            lora_config).to('cuda')
#train config
output_dir = f'/kaggle/working/peft-dialogue-summary-lora-training-{str(int(time.time()))}'

peft_training_args = TrainingArguments(
    output_dir=output_dir,
    per_device_train_batch_size=8,
    gradient_accumulation_steps=4,
    learning_rate=1e-3, # Higher learning rate than full fine-tuning.
    num_train_epochs=1,
    save_strategy="epoch",
    logging_steps=15,
     
)
    
peft_trainer = Trainer(
    model=peft_model,
    args=peft_training_args,
    train_dataset=tokenized_datasets["train"],
)

#train and error happened
peft_trainer.train()

```

This train process was going on to the last steps but at last such runtime error happened.

RuntimeError: Some tensors share memory, this will lead to duplicate memory on disk and potential differences when loading them again: [{‘base\_model.model.decoder.embed\_tokens.weight’, ‘base\_model.model.shared.weight’, ‘base\_model.model.encoder.embed\_tokens.weight’}]. A potential way to correctly save your model is to use `save_model`. More information at [Torch shared tensors](https://huggingface.co/docs/safetensors/torch_shared_tensors)

---

<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:** [December 5, 2024, 10:21am UTC](https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381/2 "2024-12-05T10:21:44Z")

</div>

Perhaps same error?

> [@Transformer's trainer runtime error](https://discuss.huggingface.co/t/transformers-trainer-runtime-error/129375):
>
> I performed fine tuning flan-t5-base model using peft. here is my peft config code peft\_model = get\_peft\_model(original\_model, lora\_config).to('cuda') output\_dir = f'/kaggle/working/peft-dialogue-summary-lora-training-{str(int(time.time()))}' peft\_training\_args = TrainingArguments( output\_dir=output\_dir, per\_device\_train\_batch\_size=8, gradient\_accumulation\_steps=4, learning\_rate=1e-3, # Higher learning rate than full fine-tuning. num\_train\_epo…

Edit:

> <https://github.com/hiyouga/LLaMA-Factory/issues/3400>
>
> \### Reminder
> 
> \- \[X\] I have read the README and searched the existing issues.
> 
> \##…# Reproduction
> 
> WANDB\_DISABLED=1 NCCL\_P2P\_DISABLE=1 NCCL\_IB\_DISABLE=1 deepspeed --num\_gpus 2 --master\_port=9527 /workspace/projects/LLaMA-Factory/src/train\_bash.py \\
> --stage rm \\
> --do\_train \\
> --deepspeed xxxxxxxx/ds\_z3\_offload\_config.json \\
> --model\_name\_or\_path xxxxxxx/chatglm3-6b \\
> --adapter\_name\_or\_path /xxx/chatglm\_exp\_sft\_lora\_llamafactory \\
> --create\_new\_adapter \\
> --dataset comparison\_gpt4\_zh \\
> --dataset\_dir xxx/data \\
> --template chatglm3 \\
> --finetuning\_type lora \\
> --lora\_target query\_key\_value \\
> --output\_dir xxx/chatglm\_exp\_rm\_lora\_llamafactory \\
> --overwrite\_cache \\
> --overwrite\_output\_dir \\
> --cutoff\_len 1024 \\
> --preprocessing\_num\_workers 4 \\
> --per\_device\_train\_batch\_size 2 \\
> --per\_device\_eval\_batch\_size 1 \\
> --gradient\_accumulation\_steps 4 \\
> --lr\_scheduler\_type cosine \\
> --logging\_steps 10 \\
> --warmup\_steps 20 \\
> --save\_steps 10 \\
> --eval\_steps 20 \\
> --evaluation\_strategy steps \\
> --learning\_rate 1e-5 \\
> --num\_train\_epochs 2.0 \\
> --max\_samples 5000 \\
> --val\_size 0.1 \\
> --plot\_loss \\
> --fp16
> 可以正常训练，但是保存checkpoint时候提示如下错误：
> \[INFO|trainer.py:3305\] 2024-04-23 16:56:46,579 \>\> Saving model checkpoint to /workspace/models/huggingface/chatglm32k\_rm\_sft\_lora\_llamafactory/checkpoint-10
> \[INFO|trainer.py:3319\] 2024-04-23 16:56:46,587 \>\> Trainer.model is not a \`PreTrainedModel\`, only saving its state dict.
> Traceback (most recent call last):
> File "/workspace/projects/LLaMA-Factory/src/train\_bash.py", line 14, in \<module\>
> main()
> File "/workspace/projects/LLaMA-Factory/src/train\_bash.py", line 5, in main
> run\_exp()
> File "/workspace/projects/LLaMA-Factory/src/llmtuner/train/tuner.py", line 35, in run\_exp
> run\_rm(model\_args, data\_args, training\_args, finetuning\_args, callbacks)
> File "/workspace/projects/LLaMA-Factory/src/llmtuner/train/rm/workflow.py", line 50, in run\_rm
> train\_result = trainer.train(resume\_from\_checkpoint=training\_args.resume\_from\_checkpoint)
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 1859, in train
> return inner\_training\_loop(
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 2278, in \_inner\_training\_loop
> self.\_maybe\_log\_save\_evaluate(tr\_loss, grad\_norm, model, trial, epoch, ignore\_keys\_for\_eval)
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 2673, in \_maybe\_log\_save\_evaluate
> self.\_save\_checkpoint(model, trial, metrics=metrics)
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 2752, in \_save\_checkpoint
> self.save\_model(output\_dir, \_internal\_call=True)
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 3239, in save\_model
> self.\_save(output\_dir, state\_dict=state\_dict)
> File "/opt/conda/lib/python3.10/site-packages/transformers/trainer.py", line 3321, in \_save
> safetensors.torch.save\_file(
> File "/opt/conda/lib/python3.10/site-packages/safetensors/torch.py", line 284, in save\_file
> serialize\_file(\_flatten(tensors), filename, metadata=metadata)
> File "/opt/conda/lib/python3.10/site-packages/safetensors/torch.py", line 480, in \_flatten
> raise RuntimeError(
> RuntimeError: 
> Some tensors share memory, this will lead to duplicate memory on disk and potential differences when loading them again: \[{'pretrained\_model.base\_model.model.lm\_head.weight', 'pretrained\_model.base\_model.model.transformer.output\_layer.weight'}\].
> A potential way to correctly save your model is to use \`save\_model\`.
> More information at https://huggingface.co/docs/safetensors/torch\_shared\_tensors
> 
> \### Expected behavior
> 
> 能够保存rm的checkpoints并顺利完成训练
> 
> \### System Info
> 
> Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.
> 
> \- \`transformers\` version: 4.40.0
> \- Platform: Linux-5.15.0-101-generic-x86\_64-with-glibc2.31
> \- Python version: 3.10.11
> \- Huggingface\_hub version: 0.22.2
> \- Safetensors version: 0.4.3
> \- Accelerate version: 0.29.3
> \- Accelerate config: not found
> \- PyTorch version (GPU?): 2.0.1 (True)
> \- Tensorflow version (GPU?): not installed (NA)
> \- Flax version (CPU?/GPU?/TPU?): not installed (NA)
> \- Jax version: not installed
> \- JaxLib version: not installed
> \- Using GPU in script?: \<fill in\>
> \- Using distributed or parallel set-up in script?: \<fill in\>
> 
> \### Others
> 
> 无

```auto
--save_safetensors False

```

---

<div class="post-metadata">

**Author:** ![spencer1129](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/spencer1129/32/37005_2.png) [@spencer1129](https://discuss.huggingface.co/u/spencer1129)\
**Post date:** [December 5, 2024, 10:29am UTC](https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381/3 "2024-12-05T10:29:42Z")

</div>

Perfect. Cool!  
Thank you for your contribute.  
And I was wondering if you could give me brief explain about why it is working.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [December 5, 2024, 10:30pm UTC](https://discuss.huggingface.co/t/transformers-trainer-memory-shared-error/129381/4 "2024-12-05T22:30:19Z")

</div>

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
