Repository: transformers
Issue: TPU Initialization Error with Transformers in Kaggle TPU VM v3-8
Labels: bug

### System Info

Title: Persistent TPU Initialization Error on Kaggle with Transformers Library
Issue Description
While running a script using the transformers library in the Kaggle environment with TPU enabled, we consistently encounter a TPU initialization error. Despite verifying all settings and configurations, the error persists. The script runs without issues on GPU, but fails on TPU.

Environment Details
Transformers Version: [Specify the version, e.g., 4.35.0]
PyTorch Version: [Specify the version, e.g., 2.0.1]
TPU Hardware: Kaggle TPU VM v3-8
Python Version: [e.g., 3.10]
Platform: Kaggle
Error Traceback
RuntimeError: Bad StatusOr access: UNKNOWN: TPU initialization failed: open(/dev/accel0): Operation not permitted: Couldn't open device: /dev/accel0; Unable to create Node RegisterInterface for node 0...
Steps to Reproduce
Enable TPU in the Kaggle environment.
Run the following command:
%cd aidiff
%cd improved-diffusion
!ls
!python transformers/examples/pytorch/language-modeling/run_clm.py \
--output_dir=classifier_models/e2e-tgt-tree_e=6_b=10_m=bert-base-uncased_wikitext-103-raw-v1_101_wp_None \
--model_name_or_path=bert-base-uncased \
--tokenizer_name=bert-base-uncased \
--per_device_train_batch_size=10 \
--per_device_eval_batch_size=10 \
--save_steps=50000 \
--num_train_epochs=6 \
--do_train \
--eval_steps=10000 \
--eval_strategy=steps \
--do_eval \
--dataloader_num_workers=4 \
--save_total_limit=1 \
--overwrite_output_dir \
--logging_dir=classifier_models/runs/e2e-tgt-tree_e=6_b=10_m=bert-base-uncased_wikitext-103-raw-v1_101_wp_None \
--block_size=100 \
--disable_tqdm=True \
--model_type=gpt2 \
--gradient_accumulation_steps=1 \
--seed=101 \
--dataset_name=wikitext \
--dataset_config_name=wikitext-103-raw-v1 \
--tpu_num_cores=8 \
--tpu_name=node-1 \
--use_tpu=True
Expected Behavior
The script should run seamlessly with TPU without encountering initialization errors.

Observed Behavior
TPU is correctly detected in the Kaggle environment.
TPU initialization fails with the provided error message.
Switching to GPU resolves the issue, but significantly increases runtime.
Debugging Efforts
Verified TPU configurations and compatibility with Kaggle.
Ensured the required libraries and dependencies are installed and up-to-date.
Performed manual debugging and consulted external resources (e.g., GPT-4).
Confirmed the learning step works perfectly on GPU but fails on TPU.
Current Workaround
Using GPU instead of TPU bypasses the issue but is not an ideal solution due to increased runtime and resource costs.

Request for Assistance
We believe this is a bug or a compatibility issue between the transformers library and Kaggle's TPU environment or a bug in TPU initialization. Any insights, fixes, or guidance to resolve this would be greatly appreciated.

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Reproduction
Kaggle Notebook Link:
The issue occurs when running the script in the Kaggle environment. Here’s the notebook:
[Kaggle Notebook](https://www.kaggle.com/code/mautlej/aidiff)

GitHub Repository:
The repository being used for this implementation is:
[Diffusion-LM GitHub Repository](https://github.com/XiangLi1999/Diffusion-LM)
Steps to reproduce the behavior:
1. Clone the GitHub repository and navigate to the working directory:
   ```bash
   !git clone https://github.com/XiangLi1999/Diffusion-LM.git
   %cd Diffusion-LM
!pip install -r requirements.txt
Enable TPU in the Kaggle environment by selecting TPU VM v3-8 in notebook settings.
Run the training script using the following command:
python train_run.py --experiment e2e-tgt-tree --app "--init_emb diffusion_models/diff_roc_pad_rand128_transformer_lr0.0001_0.0_2000_sqrt_Lsimple_h128_s2_d0.1_sd101_xstart_e2e --n_embd 16 --learned_emb yes" --pretrained_model bert-base-uncased --epoch 6 --bsz 10
Expected Behavior: The script should run seamlessly in the Kaggle environment with TPU enabled.

Observed Behavior: The TPU initialization fails with the following error:
RuntimeError: Bad StatusOr access: UNKNOWN: TPU initialization failed: open(/dev/accel0): Operation not permitted: Couldn't open device: /dev/accel0; Unable to create Node RegisterInterface for node 0...
ebugging Efforts:

Verified Kaggle TPU configurations and ensured the TPU VM v3-8 is enabled.
Ensured all dependencies were installed using the requirements.txt file.
Debugged manually and with external resources (e.g., GPT-4) to check compatibility.
Confirmed the script works perfectly on GPU, but the TPU-specific initialization consistently fails.
from transformers import Trainer, TrainingArguments
import torch_xla.core.xla_model as xm

device = xm.xla_device()
training_args = TrainingArguments(
    output_dir="./results",
    per_device_train_batch_size=10,
    num_train_epochs=6,
    do_train=True,
    evaluation_strategy="steps",
    logging_dir="./logs",
)

trainer = Trainer(model=model, args=training_args, train_dataset=train_dataset)
trainer.train()



### Expected behavior

Expected Behavior
The script should execute seamlessly on Kaggle's TPU environment with the following outcomes:

TPU Initialization:
The TPU hardware should initialize successfully without errors.

Training Process:
The training process should begin and complete the specified number of epochs using TPU acceleration.

Performance:
The script should leverage the TPU's computational power, resulting in faster execution compared to GPU.

Output:
The model checkpoints, logs, and results should be saved to the specified directories as per the script's configuration.