# Adding compute\_metrics produces Cuda OutOfMemoryError

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fadding-compute-metrics-produces-cuda-outofmemoryerror%2F87938%3E%5C
**Category:** Beginners\
**Created:** [May 22, 2024, 11:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fadding-compute-metrics-produces-cuda-outofmemoryerror%2F87938 "2024-05-22T23:48:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RZJournal](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@RZJournal](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRZJournal)%5C
**Post date:** [May 22, 2024, 11:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fadding-compute-metrics-produces-cuda-outofmemoryerror%2F87938%2F1 "2024-05-22T23:48:06Z")

</div>

Hi all, currently training bert-base-uncased, max\_length 256, batch\_size 16 and Winogrande dataset on Google Colab. I’m trying to log training and validation accuracy and using a compute\_metrics function. However, when I set the compute\_metrics argument to the function and run Trainer, the Cuda OutOfMemoryError pops up. I’m able to train smoothly when I comment out compute\_metrics. I’ve tried changing the hyperparameters(e.g. batch\_size 16 → 8), but still no luck. Below is my code. If there is more information that I should give, please tell me:

```auto
metric = load_metric('accuracy')

def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    preds = np.argmax(predictions, axis=1)
    accuracy = metric.compute(predictions=preds, references=labels)
    return accuracy

training_args = TrainingArguments(
    output_dir="fine_tuned_bert_model",
    evaluation_strategy="epoch",
    learning_rate=LEARNING_RATE,
    per_device_train_batch_size=BATCH_SIZE,
    per_device_eval_batch_size=BATCH_SIZE,
    weight_decay=WEIGHT_DECAY,
    save_total_limit=3,
    num_train_epochs=NUM_EPOCHS,
    fp16=True,
    logging_steps=1,
)

trainer = Trainer(
    model=quantized_model,
    args=training_args,
    train_dataset=tokenized_dataset["train"],
    eval_dataset=tokenized_dataset["test"],
    tokenizer=tokenizer,
    data_collator=data_collator,
    # compute_metrics=compute_metrics <----------- This is the problem---------
)

trainer.train()

```

Thanks!
