run_lm_finetuning.py for GPT2 throw error "Using pad_token, but it is not set yet."
Author: DayuanJiang
Pull request: no

I used the official setting.
```bash
python transformers/examples/run_lm_finetuning.py \
    --output_dir=gpt2_q_model \
    --model_type=gpt2 \
    --model_name_or_path=gpt2 \
    --do_train \
    --train_data_file=txt/{q_files[0]} \
```

But it says the padding id was not set.

```python
ERROR - transformers.tokenization_utils -   Using pad_token, but it is not set yet.
Traceback (most recent call last):
  File "transformers/examples/run_lm_finetuning.py", line 785, in <module>
    main()
  File "transformers/examples/run_lm_finetuning.py", line 735, in main
    global_step, tr_loss = train(args, train_dataset, model, tokenizer)
  File "transformers/examples/run_lm_finetuning.py", line 330, in train
    for step, batch in enumerate(epoch_iterator):
  File "/usr/local/lib/python3.6/dist-packages/tqdm/_tqdm.py", line 979, in __iter__
    for obj in iterable:
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 346, in __next__
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "transformers/examples/run_lm_finetuning.py", line 231, in collate
    return pad_sequence(examples, batch_first=True, padding_value=tokenizer.pad_token_id)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/utils/rnn.py", line 384, in pad_sequence
    out_tensor = sequences[0].data.new(*out_dims).fill_(padding_value)
TypeError: fill_() received an invalid combination of arguments - got (NoneType), but expected one of:
 * (Tensor value)
      didn't match because some of the arguments have invalid types: (NoneType)
 * (Number value)
      didn't match because some of the arguments have invalid types: (NoneType)
```

Comments:
I am having same error as well. Did you manage to fix it or any other updates?

Can you let me know if 6b4c3ee234db010ae2fb0554c0099fbf1f7f1f51 fixes your issue?

I encountered this issue and sure enough it is fixed with `6b4c3ee`.

Thanks @julien-c. It's mind blowing that I found the error 15 mins ago, searched here, found that you'd just patched it, and am now able to continue.

Thanks @julien-c , that fixes it.

Thanks guys! (and hat/tip @LysandreJik)

![pad_token_not_Set](https://user-images.githubusercontent.com/6847024/79693641-65e72900-8289-11ea-864b-96d8dd37c6b2.PNG)


ValueError: Unable to set proper padding strategy as the tokenizer does not have a padding token. In this case please set the `pad_token` `(tokenizer.pad_token = tokenizer.eos_token e.g.)` or add a new pad token via the function add_special_tokens if you want to use a padding strategy


python3 on kaggle
```
from datasets import load_dataset
from transformers import GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments
import torch
import numpy as np

# Function to shift the labels for language modeling
def shift_labels(examples):
    examples["labels"] = examples["input_ids"].copy()
    return examples

# Load the dataset
dataset = load_dataset("wikipedia", "20220301.simple")

# Get the total number of entries in the dataset
total_entries = len(dataset["train"])

# Split the dataset into training and test sets
train_dataset = dataset["train"].select(range(300))  # taking only the first 300 for example

# Ensure that there are enough entries for the test set
if total_entries < 60:
    raise ValueError("Not enough data to extract a test set of 60 entries.")

start_index = total_entries - 60
test_dataset = dataset["train"].select(range(start_index, total_entries))  # Now this will work



# Load the tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

# Explicitly set the padding token if it's not already defined
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# Function to tokenize the input
def tokenize_function(examples):
    return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)

# Tokenize the datasets
train_dataset = train_dataset.map(tokenize_function, batched=True)
test_dataset = test_dataset.map(tokenize_function, batched=True)


# Tokenize the datasets
def tokenize_function(examples):
    return tokenizer(examples["text"], padding="max_length", truncation=True)

train_dataset = train_dataset.map(tokenize_function, batched=True)
test_dataset = test_dataset.map(tokenize_function, batched=True)

# Shift labels for the language modeling task
train_dataset = train_dataset.map(shift_labels, batched=True)
test_dataset = test_dataset.map(shift_labels, batched=True)

# Define the data collator
data_collator = lambda data: {'input_ids': torch.stack([f['input_ids'] for f in data]), 
                              'attention_mask': torch.stack([f['attention_mask'] for f in data]), 
                              'labels': torch.stack([f['labels'] for f in data])}

# Set the PyTorch format for the dataset
train_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
test_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])

# Load the model
model = GPT2LMHeadModel.from_pretrained("gpt2")

# Define the compute_metrics function for evaluation
def compute_metrics(eval_pred):
    logits, labels = eval_pred
    shift_logits = logits[..., :-1, :].contiguous()
    shift_labels = labels[..., 1:].contiguous()
    # Flatten the outputs and labels
    loss_fct = torch.nn.CrossEntropyLoss(reduction='none')
    loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
    perplexity = torch.exp(torch.mean(loss))
    return {"perplexity": perplexity.item()}

# Define training arguments
training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=10,
    per_device_train_batch_size=4,
    per_device_eval_batch_size=4,
    warmup_steps=500,
    weight_decay=0.01,
    logging_dir='./logs',
    logging_steps=10,
    evaluation_strategy='epoch',
    save_strategy='epoch',
    load_best_model_at_end=True,
    metric_for_best_model="perplexity",
    greater_is_better=False
)

# Initialize the Trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=test_dataset,
    compute_metrics=compute_metrics,
    data_collator=data_collator,
    tokenizer=tokenizer
)

# Start training
trainer.train()

# Save the best model
trainer.save_model("/kaggle/working/best_model_wiki")



from transformers import GPT2LMHeadModel

# Make sure to provide the correct path where the best model is saved
model_path = "/kaggle/working/best_model_wiki"
``` 
i get the output  Using pad_token, but it is not set yet.
![pad](https://github.com/huggingface/transformers/assets/20457308/7401d6f6-8b67-4a4d-a65a-46076c881c9a)