# How to get the Trainer API to use GPU?

**URL:** <https://discuss.huggingface.co/t/how-to-get-the-trainer-api-to-use-gpu/6298>\
**Category:** Beginners\
**Created:** [May 21, 2021, 6:57pm UTC](https://discuss.huggingface.co/t/how-to-get-the-trainer-api-to-use-gpu/6298 "2021-05-21T18:57:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinmin](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@martinmin](https://discuss.huggingface.co/u/martinmin)\
**Post date:** [May 21, 2021, 6:57pm UTC](https://discuss.huggingface.co/t/how-to-get-the-trainer-api-to-use-gpu/6298/1 "2021-05-21T18:57:34Z")

</div>

I am following this pretrain example, but I always get the Cuda: out of memory error, although I have 2 GPU available with 16GB memory each.

> **[Fine-tune a pretrained model](https://huggingface.co/docs/transformers/training)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

And the code is below, exactly copied from the tutorial:  
from datasets import load\_dataset  
from transformers import AutoTokenizer  
from transformers import AutoModelForSequenceClassification  
from transformers import TrainingArguments  
from transformers import Trainer  
from torch.utils.data import DataLoader  
from datasets import load\_metric

```
    def tokenize_function(examples):
        return tokenizer(examples["text"], padding="max_length", truncation=True)

    raw_datasets = load_dataset("imdb")
    tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")

    tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)
    small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(1000))
    small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(1000))
    full_train_dataset = tokenized_datasets["train"]
    full_eval_dataset = tokenized_datasets["test"]

    model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=2)
    training_args = TrainingArguments("test_trainer")

    trainer = Trainer(
        model=model, args=training_args, train_dataset=small_train_dataset, eval_dataset=small_eval_dataset
    )

    trainer.train()

```

Is there any configuration to use the GPU with the Trainer API? If I use the native version of the PyTorch pretrain tutorial example, the GPU is used correctly.
