Grid search ProgressCallback leads to encoding issue on Windows
Author: BramVanroy
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.20.0
- Platform: Windows-10-10.0.19041-SP0
- Python version: 3.8.8
- Huggingface_hub version: 0.7.0
- PyTorch version (GPU?): 1.12.0+cu116 (True)



### Who can help?

@richardliaw @amogkam

### Information

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

### Tasks

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

### Reproduction

When I use `ray[tune]` grid search on Windows, the script crashes due to encoding issues. The problem seems to originate in tqdm - so when the progress bar is reaching a certain point (right after 1%) the script will crash. The reason is that to plot the progress, TQDM makes use of different characters - some small/large blocks. But apparently not all of these work well on Windows.

```
(pid=9060, ip=127.0.0.1, repr=_objective)
  File "python\ray\_raylet.pyx", line 665, in ray._raylet.execute_task
  File "python\ray\_raylet.pyx", line 669, in ray._raylet.execute_task
  File "python\ray\_raylet.pyx", line 616, in ray._raylet.execute_task.function_executor
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\_private\function_manager.py", line 675, in actor_method_executor
    return method(__ray_actor, *args, **kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\util\tracing\tracing_helper.py", line 462, in _resume_span
    return method(self, *_args, **_kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\trainable.py", line 360, in train
    result = self.step()
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\util\tracing\tracing_helper.py", line 462, in _resume_span
    return method(self, *_args, **_kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\function_runner.py", line 404, in step
    self._report_thread_runner_error(block=True)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\util\tracing\tracing_helper.py", line 462, in _resume_span
    return method(self, *_args, **_kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\function_runner.py", line 574, in _report_thread_runner_error
    raise e
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\function_runner.py", line 277, in run
    self._entrypoint()
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\function_runner.py", line 349, in entrypoint
    return self._trainable_func(
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\util\tracing\tracing_helper.py", line 462, in _resume_span
    return method(self, *_args, **_kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\function_runner.py", line 645, in _trainable_func
    output = fn()
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\integrations.py", line 288, in dynamic_modules_import_trainable
    return trainable(*args, **kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\utils\trainable.py", line 410, in inner
    trainable(config, **fn_kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\integrations.py", line 189, in _objective
    local_trainer.train(resume_from_checkpoint=checkpoint, trial=trial)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\trainer.py", line 1409, in train
    return inner_training_loop(
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\trainer.py", line 1726, in _inner_training_loop
    self.control = self.callback_handler.on_step_end(args, self.state, self.control)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\trainer_callback.py", line 369, in on_step_end
    return self.call_event("on_step_end", args, state, control)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\trainer_callback.py", line 388, in call_event
    result = getattr(callback, event)(
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\transformers\trainer_callback.py", line 472, in on_step_end
    self.training_bar.update(state.global_step - self.current_step)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\std.py", line 1256, in update
    self.refresh(lock_args=self.lock_args)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\std.py", line 1361, in refresh
    self.display()
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\std.py", line 1509, in display
    self.sp(self.__str__() if msg is None else msg)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\std.py", line 350, in print_status
    fp_write('\r' + s + (' ' * max(last_len[0] - len_s, 0)))
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\std.py", line 343, in fp_write
    fp.write(_unicode(s))
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\tqdm\utils.py", line 145, in inner
    return func(*args, **kwargs)
  File "C:\Users\bramv\.virtualenvs\transformers-finetuner-ah-81wJc\lib\site-packages\ray\tune\utils\util.py", line 228, in write
    self.stream2.write(*args, **kwargs)
  File "C:\Users\bramv\AppData\Local\Programs\Python\Python38\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u258f' in position 6: character maps to <undefined>
```

The reason that I post this in `transformers` is that I have never had any issues with the progress bars in the rest of the library, but something odd is going on with the ones that are present during grid search. I don't know the integration code base enough to find what might be causing this. Interestingly, in the rest of  the library, a tqdm progress bar always takes up the whole screen and no issues happen (because for progress, tqdm can use large block characters). But for grid search, the progress bars seem a fixed, small width. That's why it requires different characters to plot the progress (smaller increments/blocks -> different characters). So if we can change the TQDM that is being used during grid search to be the same as the other ones in the library, than there should not be any issues I believe.

### Expected behavior

No encoding issues, like in the rest of the library. This issue only occurs with grid search.

Comments:
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.

This is a sort of Ray issue. Solution is to set the environment variable to force UTF8 encode. See details here: https://peps.python.org/pep-0540/.