I am wondering how I can make the BERT tokenizer return tensors on the GPU rather than the CPU. I am following the sample code found here: BERT. The code is below.
My question is about the 5th line of code, specifically how I can make the tokenizer return a cuda tensor instead of having to add the line of code inputs = inputs.to("cuda").
from transformers import BertTokenizer, BertForPreTraining
import torch
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = BertForPreTraining.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs)
prediction_logits = outputs.prediction_logits
seq_relationship_logits = outputs.seq_relationship_logits
I found a class BatchEncoding which has a function to to allocate the result tensor to certain device.
It is inherited to BaseTokenizer but I think it is way better to cast input.to(device) instead of doing something to create BertTokenizer instance.