name 'tokenized_val_dataset' is not defined but it is defined
Author: pow9791
Pull request: no

### System Info

RTX A4000
ubuntu
pytorch

### 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

````tokenizer.pad_token = tokenizer.eos_token
# Data collator compatible with SciBERT's output
tokenizer.add_special_tokens({'pad_token': '[PAD]'})

tokenized_train_dataset = tokenized_data["train"]  # Access the "train" split
#tokenized_val_dataset = tokenized_data["validation"]  # Access the "validation" split (if it exists)
if "validation" in tokenized_data:
    tokenized_val_dataset = tokenized_data["validation"]

trainer = transformers.Trainer(
    model=model,
    train_dataset=tokenized_train_dataset,
    eval_dataset=tokenized_val_dataset,  # Use tokenized_val_dataset for evaluation
    args=training_args,
    data_collator=data_collator
)


# train model
model.config.use_cache = False # silence the warnings. Please re-enable for inference!
trainer.train()````
 if "validation" in tokenized_data:
      8     tokenized_val_dataset = tokenized_data["validation"]
     10 trainer = transformers.Trainer(
     11     model=model,
     12     train_dataset=tokenized_train_dataset,
---> 13     eval_dataset=tokenized_val_dataset,  # Use tokenized_val_dataset for evaluation
     14     args=training_args,
     15     data_collator=data_collator
     16 )
     19 # train model
     20 model.config.use_cache = False # silence the warnings. Please re-enable for inference!

NameError: name 'tokenized_val_dataset' is not defined

### Expected behavior

IT should train

Comments:
You seemed to have used: 
```python
#tokenized_val_dataset = tokenized_data["validation"]  # Access the "validation" split (if it exists)
if "validation" in tokenized_data:
    tokenized_val_dataset = tokenized_data["validation"]
```

but 
```python
tokenized_val_dataset = None
if "validation" in tokenized_data:
    tokenized_val_dataset = tokenized_data["validation"]
```
should work

> You seemed to have used:
> 
> ```python
> #tokenized_val_dataset = tokenized_data["validation"]  # Access the "validation" split (if it exists)
> if "validation" in tokenized_data:
>     tokenized_val_dataset = tokenized_data["validation"]
> ```
> 
> but
> 
> ```python
> tokenized_val_dataset = None
> if "validation" in tokenized_data:
>     tokenized_val_dataset = tokenized_data["validation"]
> ```
> 
> should work
Im got another error when I used your code 


``` from transformers import AutoTokenizer

# Load the SciBERT tokenizer
tokenizer = AutoTokenizer.from_pretrained("allenai/scibert_scivocab_uncased")

def preprocess_function(example):
    text = example["text"]  # Assuming the key for text is "text"
    return tokenizer(
        text,
        return_tensors="np",  # SciBERT returns NumPy arrays
        truncation=True,
        max_length=512
    )

# Map the preprocess_function to the dataset
tokenized_data = data.map(preprocess_function, batched=True)

# SciBERT uses 
 for both padding and EOS tokens
tokenizer.pad_token = tokenizer.eos_token = tokenizer.bos_token  # 


# Data collator compatible with SciBERT's output
data_collator = transformers.DataCollatorForLanguageModeling(
    tokenizer, mlm=False, return_tensors="np"
)


# Data collator compatible with SciBERT's output
data_collator = transformers.DataCollatorForLanguageModeling(
    tokenizer, mlm=False, return_tensors="np"
)


lr = 2e-4
batch_size = 4
num_epochs = 10

# define training arguments
training_args = transformers.TrainingArguments(
    output_dir= "workspace",
    learning_rate=lr,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    num_train_epochs=num_epochs,
    weight_decay=0.01,
    logging_strategy="epoch",
    evaluation_strategy="epoch",
    save_strategy="epoch",
    load_best_model_at_end=True,
    gradient_accumulation_steps=4,
    warmup_steps=2,
    fp16=True,
    optim="paged_adamw_8bit",

)


tokenizer.add_special_tokens({'pad_token': '[PAD]'})

tokenized_train_dataset = tokenized_data["train"]  # Access the "train" split
#tokenized_val_dataset = tokenized_data["validation"]  # Access the "validation" split (if it exists)
tokenized_val_dataset = None
if "validation" in tokenized_data:
    tokenized_val_dataset = tokenized_data["validation"]

trainer = transformers.Trainer(
    model=model,
    train_dataset=tokenized_train_dataset,
    eval_dataset=tokenized_val_dataset,  # Use tokenized_val_dataset for evaluation
    args=training_args,
    data_collator=data_collator
)


# train model
model.config.use_cache = False # silence the warnings. Please re-enable for inference!
trainer.train(). 
`` /opt/conda/lib/python3.10/site-packages/accelerate/accelerator.py:432: FutureWarning: Passing the following arguments to `Accelerator` is deprecated and will be removed in version 1.0 of Accelerate: dict_keys(['dispatch_batches', 'split_batches', 'even_batches', 'use_seedable_sampler']). Please pass an `accelerate.DataLoaderConfiguration` instead: 
dataloader_config = DataLoaderConfiguration(dispatch_batches=None, split_batches=False, even_batches=True, use_seedable_sampler=True)
  warnings.warn(
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/accelerate/utils/operations.py:155, in send_to_device(tensor, device, non_blocking, skip_keys)
    154 try:
--> 155     return tensor.to(device, non_blocking=non_blocking)
    156 except TypeError:  # .to() doesn't accept non_blocking as kwarg

TypeError: BatchEncoding.to() got an unexpected keyword argument 'non_blocking'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[21], line 21
     19 # train model
     20 model.config.use_cache = False # silence the warnings. Please re-enable for inference!
---> 21 trainer.train()

File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:1780, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
   1778         hf_hub_utils.enable_progress_bars()
   1779 else:
-> 1780     return inner_training_loop(
   1781         args=args,
   1782         resume_from_checkpoint=resume_from_checkpoint,
   1783         trial=trial,
   1784         ignore_keys_for_eval=ignore_keys_for_eval,
   1785     )

File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:2085, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
   2082     rng_to_sync = True
   2084 step = -1
-> 2085 for step, inputs in enumerate(epoch_iterator):
   2086     total_batched_samples += 1
   2088     if self.args.include_num_input_tokens_seen:

File /opt/conda/lib/python3.10/site-packages/accelerate/data_loader.py:461, in DataLoaderShard.__iter__(self)
    458 try:
    459     # But we still move it to the device so it is done before `StopIteration` is reached
    460     if self.device is not None:
--> 461         current_batch = send_to_device(current_batch, self.device)
    462     next_batch = next(dataloader_iter)
    463     if batch_index >= self.skip_batches:

File /opt/conda/lib/python3.10/site-packages/accelerate/utils/operations.py:157, in send_to_device(tensor, device, non_blocking, skip_keys)
    155     return tensor.to(device, non_blocking=non_blocking)
    156 except TypeError:  # .to() doesn't accept non_blocking as kwarg
--> 157     return tensor.to(device)
    158 except AssertionError as error:
    159     # `torch.Tensor.to(<int num>)` is not supported by `torch_npu` (see this [issue](https://github.com/Ascend/pytorch/issues/16)).
    160     # This call is inside the try-block since is_npu_available is not supported by torch.compile.
    161     if is_npu_available():

File /opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:800, in BatchEncoding.to(self, device)
    796 # This check catches things like APEX blindly calling "to" on all inputs to a module
    797 # Otherwise it passes the casts down and casts the LongTensor containing the token idxs
    798 # into a HalfTensor
    799 if isinstance(device, str) or is_torch_device(device) or isinstance(device, int):
--> 800     self.data = {k: v.to(device=device) for k, v in self.data.items()}
    801 else:
    802     logger.warning(f"Attempting to cast a BatchEncoding to type {str(device)}. This is not supported.")

File /opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:800, in <dictcomp>(.0)
    796 # This check catches things like APEX blindly calling "to" on all inputs to a module
    797 # Otherwise it passes the casts down and casts the LongTensor containing the token idxs
    798 # into a HalfTensor
    799 if isinstance(device, str) or is_torch_device(device) or isinstance(device, int):
--> 800     self.data = {k: v.to(device=device) for k, v in self.data.items()}
    801 else:
    802     logger.warning(f"Attempting to cast a BatchEncoding to type {str(device)}. This is not supported.")

AttributeError: 'numpy.ndarray' object has no attribute 'to'. ```

You datacollators are returning `np` tensors. 

> You datacollators are returning `np` tensors.

What should I do, change it to torch tensors, so 'pt'?
I changed data collator to 'pt', but now im getting this error
```data_collator = transformers.DataCollatorForLanguageModeling(
    tokenizer, mlm=False, return_tensors="pt"
)
# Create tokenize function (assuming data is a dictionary with "example" key)
def tokenize_function(examples):
    text = examples["example"]  # Extract text from the "example" key
    tokenized_inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=512
    )
    return tokenized_inputs


insertionError                            Traceback (most recent call last)
Cell In[22], line 21
     19 # train model
     20 model.config.use_cache = False # silence the warnings. Please re-enable for inference!
---> 21 trainer.train()

File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:1780, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
   1778         hf_hub_utils.enable_progress_bars()
   1779 else:
-> 1780     return inner_training_loop(
   1781         args=args,
   1782         resume_from_checkpoint=resume_from_checkpoint,
   1783         trial=trial,
   1784         ignore_keys_for_eval=ignore_keys_for_eval,
   1785     )

File /opt/conda/lib/python3.10/site-packages/transformers/trainer.py:2181, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
   2178         grad_norm = _grad_norm
   2180 # Optimizer step
-> 2181 self.optimizer.step()
   2182 optimizer_was_run = not self.accelerator.optimizer_step_was_skipped
   2183 if optimizer_was_run:
   2184     # Delay optimizer scheduling until metrics are generated

File /opt/conda/lib/python3.10/site-packages/accelerate/optimizer.py:136, in AcceleratedOptimizer.step(self, closure)
    133 if self.scaler is not None:
    134     self.optimizer.step = self._optimizer_patched_step_method
--> 136     self.scaler.step(self.optimizer, closure)
    137     self.scaler.update()
    139     if not self._accelerate_step_called:
    140         # If the optimizer step was skipped, gradient overflow was detected.

File /opt/conda/lib/python3.10/site-packages/torch/cuda/amp/grad_scaler.py:448, in GradScaler.step(self, optimizer, *args, **kwargs)
    445 if optimizer_state["stage"] is OptState.READY:
    446     self.unscale_(optimizer)
--> 448 assert (
    449     len(optimizer_state["found_inf_per_device"]) > 0
    450 ), "No inf checks were recorded for this optimizer."
    452 retval = self._maybe_opt_step(optimizer, optimizer_state, *args, **kwargs)
    454 optimizer_state["stage"] = OptState.STEPPED

AssertionError: No inf checks were recorded for this optimizer.

We try to keep the github issues for bugs/feature requests, sorry but won't have the bandwidth to debug your custom code! 

Could you ask your question on the [forum](https://discuss.huggingface.co/) instead? I'm sure the community will be of help!

Thanks!

> We try to keep the github issues for bugs/feature requests, sorry but won't have the bandwidth to debug your custom code!
> 
> Could you ask your question on the [forum](https://discuss.huggingface.co/) instead? I'm sure the community will be of help!
> 
> Thanks!

I tried but no response yet!

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.