Pass datasets trust_remote_code
Author: albertvillanova
Pull request: yes

# What does this PR do?

Next release of `datasets` will raise an error for script-datasets if `trust_remote_code` is not set to True.

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_31406). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

I think this is more intricate than I first thought.

What about if I make another PR to pin upper version of datasets and then handle this PR more calmly?

For example, the "hf-internal-testing/librispeech_asr_dummy" dataset name is passed in this test: https://github.com/huggingface/transformers/blob/c624d5ba0b41542eb3daa0e2fdc985231551d776/examples/flax/test_flax_examples.py#L264
- We should also pass the trust_remote_code dataset argument

Should we define it in `DataTrainingArguments`?

> What about if I make another PR to pin upper version of datasets and then handle this PR more calmly?

Sounds good! 

> Should we define it in DataTrainingArguments?

Happy with that 👍 


> I guess the name trust_remote_dataset_code is because we have used trust_remote_code for ModelArguments, right? Would like @amyeroberts 's opinion on this too.

So if the argument already exists in `ModelArguments` we can either add `trust_remote_dataset_code` to `DataTrainingArguments` or just use the one from `ModelArguments`. I'd do that latter for simplicity: I don't really see why we'd trust the model code and not the datasets' code, and from the users' perspective on the CLI they don't see that the division of the arguments. 

> > I guess the name trust_remote_dataset_code is because we have used trust_remote_code for ModelArguments, right? Would like @amyeroberts 's opinion on this too.
> 
> So if the argument already exists in `ModelArguments` we can either add `trust_remote_dataset_code` to `DataTrainingArguments` or just use the one from `ModelArguments`. I'd do that latter for simplicity: I don't really see why we'd trust the model code and not the datasets' code, and from the users' perspective on the CLI they don't see that the division of the arguments.

Yeah, I would love the latter if `HfArgumentParser` could handle a single `trust_remote_code` and assign it to both `ModelArguments` and `DataTrainingArguments`

Thanks for your reviews, @amyeroberts and @ydshieh.

So, you would prefer to have just a single `trust_remote_code` arg that should be defined in `ModelArguments` (as it was already the case for some examples) but also used to load the dataset? Something like:
```python
load_dataset(
    data_args.dataset_name,
    data_args.dataset_config_name,
    trust_remote_code=model_args.trust_remote_code,
``` 

Just to be sure, because I find weird to use a `model_args` to `load_dataset`. And the refactoring will be tedious...
Thank you.

Hi @albertvillanova Not really. What I am thinking is:

- we only need to specify `--trust_remote_code` on the commandline
- have `trust_remote_code` defined in `ModelArguments` and/or `DataTrainingArguments` (could be only one or could be both depends on the usage)
- `HfArgumentParser` take `trust_remote_code` from commandline, and assign it to both `ModelArguments.trust_remote_code` and `DataTrainingArguments.trust_remote_code`
  - for this, I don't know if  `HfArgumentParser` already handle this, **need to verify**.
  - if this is not (currently) possible + if this PR needs to be merged **urgently**, we might need to use `trust_remote_dataset_code` for now and come back to it once merged. (But changing arguments afterwards is not so good)
- and later in the code,  we can use `data_args.trust_remote_code` for `load_dataset` (and `model_args.trust_remote_code` for model related code)

Thanks, @ydshieh.

I will check if `HfArgumentParser` can assign the argument to both.

On the other hand, for the cases where `ArgumentParser` is used directly, I think the change is trivial. See example commit: [8380ae5](https://github.com/huggingface/transformers/pull/31406/commits/8380ae57f53bdb7891b8754bb04d314e3db6bd81)

The assignment of the argument by `HfArgumentParser` to both is not trivial. I will leave that for a subsequent PR.

In this PR, I will use `trust_remote_code` as `ModelArguments` if it already existed before this PR, or as `DataTrainingArguments` if it is added in this PR and only used within `load_dataset`.
- Note that `ModelArguments` are already used in `load_dataset`, e.g. `token=model_args.token`

> The assignment of the argument by `HfArgumentParser` to both is not trivial. I will leave that for a subsequent PR.

Thanks for checking @albertvillanova. That part is rather on our side, but if you want to take it, that would be much appreciated.

> In this PR, I will use `trust_remote_code` as `ModelArguments` if it already existed before this PR, or as `DataTrainingArguments` if it is added in this PR and only used within `load_dataset`.
> 
>     * Note that `ModelArguments` are already used in `load_dataset`, e.g. `token=model_args.token`

Works for me :-)