CUDA out of memory when using Trainer with compute_metrics

Hi

I have a related problem in view of what you mentioned here.

I am currently finetuning the NLLB translation model using GPU where I like to compute metrics and see the progress of the training process as it trains. The problem I face is that when I increase my dataset to approximately 50K (followed by a 0.2 train-test split), my trainer seems to be able to complete 1 epoch within 9mins but only completes the evaluation for the epochs 20 mins later.

I used simple print statements in my compute_metrics function and realised that the whole function ran in less than a minute, so I’m not so sure what went wrong. Is there something wrong with my compute_metrics function?

I understand that tokenization happens on the CPU? So I was wondering if the problem I face is because I am training on GPU and then evaluating on CPU? 20mins of evaluation doesnt seem like a big problem but when I increased my dataset to 190K, the trainer would have completed 1 epoch in 30mins but not even complete the evaluation 70mins later.

Here are some of the codes i use:

def compute_metrics(eval_preds):
    preds, labels = eval_preds
    if isinstance(preds, tuple):
        preds = preds[0]
    decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)

    labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)

    decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels)

    result = metric.compute(predictions=decoded_preds, references=decoded_labels)
    result = {"bleu": result["score"]}

    prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
    result["gen_len"] = np.mean(prediction_lens)
    result = {k: round(v, 4) for k, v in result.items()}
    logger.warning(result)
    return result
training_args = Seq2SeqTrainingArguments(
    output_dir="my_awesome_opus_books_model",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=32,
    per_device_eval_batch_size=32,
    weight_decay=0.01,
    save_total_limit=3,
    num_train_epochs=2,
    predict_with_generate=True,
    fp16=True,
)

trainer = Seq2SeqTrainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_books["train"],
    eval_dataset=tokenized_books["test"],
    tokenizer=tokenizer,
    data_collator=data_collator,
    compute_metrics=compute_metrics,
)

trainer.train()