# CUDA out of memory when using the trainer model\_init

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-when-using-the-trainer-model-init%2F67550%3E%5C
**Category:** 🤗Transformers\
**Created:** [December 31, 2023, 11:49am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-when-using-the-trainer-model-init%2F67550 "2023-12-31T11:49:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaBrThesis2023](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@MaBrThesis2023](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FMaBrThesis2023)%5C
**Post date:** [December 31, 2023, 11:49am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fcuda-out-of-memory-when-using-the-trainer-model-init%2F67550%2F1 "2023-12-31T11:49:44Z")

</div>

I am running out of memory when using the trainer model\_init argument as follows:

```auto
def model_init():
    model = AutoModelForSequenceClassification.from_pretrained(BASE_MODEL_ID,num_labels=3,
                                                              output_attentions = False, # Whether the model returns attentions weights.
                                                              output_hidden_states = False,
                                                              return_dict=True,
                                                              torch_dtype=torch.float16,
                                                              )
    model.config.pad_token_id = 2
    model.config.use_cache = False  
    model.gradient_checkpointing_enable()
    for name, param in model.named_parameters():
        param.requires_grad = False
    for name, param in model.score.named_parameters():
        param.requires_grad = True
    for name, param in model.named_parameters():
        print(f"{name} - {param.requires_grad}")
    return model

run_name = BASE_MODEL_ID.split("/")[-1] + PROJECT_NAME
output_dir = "./" + run_name
trainer_args = TrainingArguments(
               output_dir=output_dir,
               #warmup_steps=1,
               per_device_train_batch_size=4,
               gradient_accumulation_steps=8,
               gradient_checkpointing=True,
               max_steps=200,
               warmup_ratio=0.1,
               learning_rate=2e-5, 
               bf16=True,
               optim="paged_adamw_32bit",
               load_best_model_at_end=True,
               metric_for_best_model="eval_loss",
               logging_steps=4,             
               logging_dir="./logs",        
               save_strategy="steps",       
               save_steps=8,               
               evaluation_strategy="steps", 
               eval_steps=8,
               seed=SEED,            
               do_eval=True
              )

trainer = Trainer(
    model_init = model_init,
    train_dataset=train_ds,
    eval_dataset=test_ds,
    args=trainer_args,
    compute_metrics=compute_metrics,
    data_collator=DataCollatorWithPadding(tokenizer=tokenizer), 
    callbacks=[EarlyStoppingCallback(early_stopping_patience=4)]
)

```

It seems like the trainer calll places the model on the GPU, when calling trainer.train() the model is placed on the GPU again and the GPU runs out of memory. Is there a way to prevent this while being able to init the model with the same random weights?
