From 145109382a972d53ed3eac17216dc509d1d19262 Mon Sep 17 00:00:00 2001 From: Jackmin801 <56836461+Jackmin801@users.noreply.github.com> Date: Mon, 7 Aug 2023 22:32:25 +0800 Subject: [PATCH] Allow `trust_remote_code` in example scripts (#25248) * pytorch examples * pytorch mim no trainer * cookiecutter * flax examples * missed line in pytorch run_glue * tensorflow examples * tensorflow run_clip * tensorflow run_mlm * tensorflow run_ner * tensorflow run_clm * pytorch example from_configs * pytorch no trainer examples * Revert "tensorflow run_clip" This reverts commit 261f86ac1f1c9e05dd3fd0291e1a1f8e573781d5. * fix: duplicated argument --- .../run_image_captioning_flax.py | 13 ++++++ .../flax/language-modeling/run_clm_flax.py | 16 ++++++++ .../flax/language-modeling/run_mlm_flax.py | 16 ++++++++ examples/flax/question-answering/run_qa.py | 13 ++++++ .../summarization/run_summarization_flax.py | 16 ++++++++ .../flax/text-classification/run_flax_glue.py | 13 ++++++ .../flax/token-classification/run_flax_ner.py | 14 +++++++ .../flax/vision/run_image_classification.py | 14 +++++++ .../run_audio_classification.py | 13 ++++++ .../contrastive-image-text/run_clip.py | 12 ++++++ .../run_image_classification.py | 13 ++++++ .../run_image_classification_no_trainer.py | 17 +++++++- examples/pytorch/image-pretraining/run_mim.py | 14 ++++++- .../image-pretraining/run_mim_no_trainer.py | 17 +++++++- examples/pytorch/language-modeling/run_clm.py | 15 ++++++- .../language-modeling/run_clm_no_trainer.py | 31 +++++++++++--- examples/pytorch/language-modeling/run_mlm.py | 15 ++++++- .../language-modeling/run_mlm_no_trainer.py | 25 +++++++++--- examples/pytorch/multiple-choice/run_swag.py | 13 ++++++ .../multiple-choice/run_swag_no_trainer.py | 25 +++++++++--- examples/pytorch/question-answering/run_qa.py | 13 ++++++ .../question-answering/run_qa_no_trainer.py | 25 +++++++++--- .../question-answering/run_seq2seq_qa.py | 13 ++++++ .../run_semantic_segmentation.py | 13 ++++++ .../run_semantic_segmentation_no_trainer.py | 22 ++++++++-- .../run_speech_recognition_ctc.py | 14 +++++++ .../run_speech_recognition_ctc_adapter.py | 15 +++++++ .../run_speech_recognition_seq2seq.py | 14 +++++++ .../summarization/run_summarization.py | 13 ++++++ .../run_summarization_no_trainer.py | 25 +++++++++--- .../text-classification/run_classification.py | 13 ++++++ .../pytorch/text-classification/run_glue.py | 13 ++++++ .../run_glue_no_trainer.py | 22 +++++++++- .../pytorch/text-classification/run_xnli.py | 13 ++++++ .../pytorch/token-classification/run_ner.py | 14 +++++++ .../run_ner_no_trainer.py | 29 +++++++++++--- .../pytorch/translation/run_translation.py | 13 ++++++ .../translation/run_translation_no_trainer.py | 25 +++++++++--- .../run_image_classification.py | 13 ++++++ .../tensorflow/language-modeling/run_clm.py | 37 +++++++++++++---- .../tensorflow/language-modeling/run_mlm.py | 36 +++++++++++++---- .../tensorflow/multiple-choice/run_swag.py | 13 ++++++ .../tensorflow/question-answering/run_qa.py | 13 ++++++ .../summarization/run_summarization.py | 13 ++++++ .../text-classification/run_glue.py | 13 ++++++ .../run_text_classification.py | 14 +++++++ .../token-classification/run_ner.py | 40 ++++++++++++++++--- .../tensorflow/translation/run_translation.py | 13 ++++++ .../run_{{cookiecutter.example_shortcut}}.py | 16 ++++++++ 49 files changed, 790 insertions(+), 65 deletions(-) diff --git a/examples/flax/image-captioning/run_image_captioning_flax.py b/examples/flax/image-captioning/run_image_captioning_flax.py index f71641e30f..bbc79977a4 100644 --- a/examples/flax/image-captioning/run_image_captioning_flax.py +++ b/examples/flax/image-captioning/run_image_captioning_flax.py @@ -198,6 +198,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -489,17 +499,20 @@ def main(): seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) image_processor = AutoImageProcessor.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer.pad_token = tokenizer.convert_ids_to_tokens(model.config.pad_token_id) diff --git a/examples/flax/language-modeling/run_clm_flax.py b/examples/flax/language-modeling/run_clm_flax.py index 7c4206a0c7..1a296a4fa9 100755 --- a/examples/flax/language-modeling/run_clm_flax.py +++ b/examples/flax/language-modeling/run_clm_flax.py @@ -185,6 +185,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -477,12 +487,14 @@ def main(): model_args.config_name, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: config = AutoConfig.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: config = CONFIG_MAPPING[model_args.model_type]() @@ -494,6 +506,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: tokenizer = AutoTokenizer.from_pretrained( @@ -501,6 +514,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: raise ValueError( @@ -515,12 +529,14 @@ def main(): seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: model = FlaxAutoModelForCausalLM.from_config( config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), + trust_remote_code=model_args.trust_remote_code, ) # Preprocessing the datasets. diff --git a/examples/flax/language-modeling/run_mlm_flax.py b/examples/flax/language-modeling/run_mlm_flax.py index d5e44feaf3..0c49a2cff7 100755 --- a/examples/flax/language-modeling/run_mlm_flax.py +++ b/examples/flax/language-modeling/run_mlm_flax.py @@ -190,6 +190,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -509,12 +519,14 @@ def main(): model_args.config_name, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: config = AutoConfig.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: config = CONFIG_MAPPING[model_args.model_type]() @@ -526,6 +538,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: tokenizer = AutoTokenizer.from_pretrained( @@ -533,6 +546,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: raise ValueError( @@ -652,12 +666,14 @@ def main(): seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: model = FlaxAutoModelForMaskedLM.from_config( config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), + trust_remote_code=model_args.trust_remote_code, ) if training_args.gradient_checkpointing: diff --git a/examples/flax/question-answering/run_qa.py b/examples/flax/question-answering/run_qa.py index 925e182e7e..77bd37bab8 100644 --- a/examples/flax/question-answering/run_qa.py +++ b/examples/flax/question-answering/run_qa.py @@ -171,6 +171,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) dtype: Optional[str] = field( default="float32", metadata={ @@ -534,6 +544,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -541,6 +552,7 @@ def main(): use_fast=True, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion @@ -888,6 +900,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), ) diff --git a/examples/flax/summarization/run_summarization_flax.py b/examples/flax/summarization/run_summarization_flax.py index 83af31d8d2..fc1595ac7e 100644 --- a/examples/flax/summarization/run_summarization_flax.py +++ b/examples/flax/summarization/run_summarization_flax.py @@ -204,6 +204,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -517,12 +527,14 @@ def main(): model_args.config_name, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: config = AutoConfig.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: config = CONFIG_MAPPING[model_args.model_type]() @@ -534,6 +546,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: tokenizer = AutoTokenizer.from_pretrained( @@ -541,6 +554,7 @@ def main(): cache_dir=model_args.cache_dir, use_fast=model_args.use_fast_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: raise ValueError( @@ -555,12 +569,14 @@ def main(): seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: model = FlaxAutoModelForSeq2SeqLM.from_config( config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), + trust_remote_code=model_args.trust_remote_code, ) if training_args.gradient_checkpointing: diff --git a/examples/flax/text-classification/run_flax_glue.py b/examples/flax/text-classification/run_flax_glue.py index 3f6d0a5eb6..b9a23f9dd5 100755 --- a/examples/flax/text-classification/run_flax_glue.py +++ b/examples/flax/text-classification/run_flax_glue.py @@ -117,6 +117,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -425,16 +435,19 @@ def main(): num_labels=num_labels, finetuning_task=data_args.task_name, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.model_name_or_path, use_fast=not model_args.use_slow_tokenizer, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = FlaxAutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path, config=config, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # Preprocessing the datasets diff --git a/examples/flax/token-classification/run_flax_ner.py b/examples/flax/token-classification/run_flax_ner.py index f66d9b3128..e0a0cb1eff 100644 --- a/examples/flax/token-classification/run_flax_ner.py +++ b/examples/flax/token-classification/run_flax_ner.py @@ -165,6 +165,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -504,6 +514,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer_name_or_path = model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path if config.model_type in {"gpt2", "roberta"}: @@ -512,6 +523,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, add_prefix_space=True, ) else: @@ -520,6 +532,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = FlaxAutoModelForTokenClassification.from_pretrained( model_args.model_name_or_path, @@ -527,6 +540,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # Preprocessing the datasets diff --git a/examples/flax/vision/run_image_classification.py b/examples/flax/vision/run_image_classification.py index 63b638423a..d3454f2672 100644 --- a/examples/flax/vision/run_image_classification.py +++ b/examples/flax/vision/run_image_classification.py @@ -175,6 +175,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -352,6 +362,7 @@ def main(): image_size=data_args.image_size, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) elif model_args.model_name_or_path: config = AutoConfig.from_pretrained( @@ -360,6 +371,7 @@ def main(): image_size=data_args.image_size, cache_dir=model_args.cache_dir, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: config = CONFIG_MAPPING[model_args.model_type]() @@ -372,12 +384,14 @@ def main(): seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: model = FlaxAutoModelForImageClassification.from_config( config, seed=training_args.seed, dtype=getattr(jnp, model_args.dtype), + trust_remote_code=model_args.trust_remote_code, ) # Store some constant diff --git a/examples/pytorch/audio-classification/run_audio_classification.py b/examples/pytorch/audio-classification/run_audio_classification.py index c1568867e2..8be727200a 100644 --- a/examples/pytorch/audio-classification/run_audio_classification.py +++ b/examples/pytorch/audio-classification/run_audio_classification.py @@ -167,6 +167,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) freeze_feature_extractor: Optional[bool] = field( default=None, metadata={"help": "Whether to freeze the feature extractor layers of the model."} ) @@ -293,6 +303,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # `datasets` takes care of automatically loading and resampling the audio, @@ -353,6 +364,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForAudioClassification.from_pretrained( model_args.model_name_or_path, @@ -361,6 +373,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) diff --git a/examples/pytorch/contrastive-image-text/run_clip.py b/examples/pytorch/contrastive-image-text/run_clip.py index dad10ea274..86612b8b7e 100644 --- a/examples/pytorch/contrastive-image-text/run_clip.py +++ b/examples/pytorch/contrastive-image-text/run_clip.py @@ -102,6 +102,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) freeze_vision_model: bool = field( default=False, metadata={"help": "Whether to freeze the vision model parameters or not."} ) @@ -350,6 +360,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModel.from_pretrained( @@ -357,6 +368,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) config = model.config diff --git a/examples/pytorch/image-classification/run_image_classification.py b/examples/pytorch/image-classification/run_image_classification.py index 35a74b2532..a7fe144da2 100644 --- a/examples/pytorch/image-classification/run_image_classification.py +++ b/examples/pytorch/image-classification/run_image_classification.py @@ -158,6 +158,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -290,6 +300,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForImageClassification.from_pretrained( model_args.model_name_or_path, @@ -298,6 +309,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) image_processor = AutoImageProcessor.from_pretrained( @@ -305,6 +317,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # Define torchvision transforms to be applied to each image. diff --git a/examples/pytorch/image-classification/run_image_classification_no_trainer.py b/examples/pytorch/image-classification/run_image_classification_no_trainer.py index c416273132..62434649c3 100644 --- a/examples/pytorch/image-classification/run_image_classification_no_trainer.py +++ b/examples/pytorch/image-classification/run_image_classification_no_trainer.py @@ -146,6 +146,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -300,13 +310,18 @@ def main(): i2label=id2label, label2id=label2id, finetuning_task="image-classification", + trust_remote_code=args.trust_remote_code, + ) + image_processor = AutoImageProcessor.from_pretrained( + args.model_name_or_path, + trust_remote_code=args.trust_remote_code, ) - image_processor = AutoImageProcessor.from_pretrained(args.model_name_or_path) model = AutoModelForImageClassification.from_pretrained( args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, ignore_mismatched_sizes=args.ignore_mismatched_sizes, + trust_remote_code=args.trust_remote_code, ) # Preprocessing the datasets diff --git a/examples/pytorch/image-pretraining/run_mim.py b/examples/pytorch/image-pretraining/run_mim.py index 25e780ab48..9fa3a1b68a 100644 --- a/examples/pytorch/image-pretraining/run_mim.py +++ b/examples/pytorch/image-pretraining/run_mim.py @@ -169,6 +169,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) image_size: Optional[int] = field( default=None, metadata={ @@ -319,6 +329,7 @@ def main(): "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "token": model_args.token, + "trust_remote_code": model_args.trust_remote_code, } if model_args.config_name_or_path: config = AutoConfig.from_pretrained(model_args.config_name_or_path, **config_kwargs) @@ -371,10 +382,11 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForMaskedImageModeling.from_config(config) + model = AutoModelForMaskedImageModeling.from_config(config, trust_remote_code=model_args.trust_remote_code) if training_args.do_train: column_names = ds["train"].column_names diff --git a/examples/pytorch/image-pretraining/run_mim_no_trainer.py b/examples/pytorch/image-pretraining/run_mim_no_trainer.py index 44066daee5..5053dabf91 100644 --- a/examples/pytorch/image-pretraining/run_mim_no_trainer.py +++ b/examples/pytorch/image-pretraining/run_mim_no_trainer.py @@ -195,6 +195,16 @@ def parse_args(): "with private models)." ), ) + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--image_size", type=int, @@ -448,6 +458,7 @@ def main(): "cache_dir": args.cache_dir, "revision": args.model_revision, "use_auth_token": True if args.use_auth_token else None, + "trust_remote_code": args.trust_remote_code, } if args.config_name_or_path: config = AutoConfig.from_pretrained(args.config_name_or_path, **config_kwargs) @@ -498,10 +509,14 @@ def main(): cache_dir=args.cache_dir, revision=args.model_revision, token=True if args.use_auth_token else None, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForMaskedImageModeling.from_config(config) + model = AutoModelForMaskedImageModeling.from_config( + config, + trust_remote_code=args.trust_remote_code, + ) column_names = ds["train"].column_names diff --git a/examples/pytorch/language-modeling/run_clm.py b/examples/pytorch/language-modeling/run_clm.py index c1ed1c3412..3d491f784c 100755 --- a/examples/pytorch/language-modeling/run_clm.py +++ b/examples/pytorch/language-modeling/run_clm.py @@ -127,6 +127,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) torch_dtype: Optional[str] = field( default=None, metadata={ @@ -387,6 +397,7 @@ def main(): "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "token": model_args.token, + "trust_remote_code": model_args.trust_remote_code, } if model_args.config_name: config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs) @@ -405,6 +416,7 @@ def main(): "use_fast": model_args.use_fast_tokenizer, "revision": model_args.model_revision, "token": model_args.token, + "trust_remote_code": model_args.trust_remote_code, } if model_args.tokenizer_name: tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs) @@ -429,11 +441,12 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, torch_dtype=torch_dtype, low_cpu_mem_usage=model_args.low_cpu_mem_usage, ) else: - model = AutoModelForCausalLM.from_config(config) + model = AutoModelForCausalLM.from_config(config, trust_remote_code=model_args.trust_remote_code) n_params = sum({p.data_ptr(): p.numel() for p in model.parameters()}.values()) logger.info(f"Training new model from scratch - Total size={n_params/2**20:.2f}M params") diff --git a/examples/pytorch/language-modeling/run_clm_no_trainer.py b/examples/pytorch/language-modeling/run_clm_no_trainer.py index 58954bd853..b195e33cc9 100755 --- a/examples/pytorch/language-modeling/run_clm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_clm_no_trainer.py @@ -193,6 +193,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -362,17 +372,27 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name) + config = AutoConfig.from_pretrained( + args.config_name, + trust_remote_code=args.trust_remote_code, + ) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained( + args.model_name_or_path, + trust_remote_code=args.trust_remote_code, + ) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -385,10 +405,11 @@ def main(): from_tf=bool(".ckpt" in args.model_name_or_path), config=config, low_cpu_mem_usage=args.low_cpu_mem_usage, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForCausalLM.from_config(config) + model = AutoModelForCausalLM.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/language-modeling/run_mlm.py b/examples/pytorch/language-modeling/run_mlm.py index d6c756edc3..84347804db 100755 --- a/examples/pytorch/language-modeling/run_mlm.py +++ b/examples/pytorch/language-modeling/run_mlm.py @@ -123,6 +123,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) low_cpu_mem_usage: bool = field( default=False, metadata={ @@ -380,6 +390,7 @@ def main(): "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "token": model_args.token, + "trust_remote_code": model_args.trust_remote_code, } if model_args.config_name: config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs) @@ -398,6 +409,7 @@ def main(): "use_fast": model_args.use_fast_tokenizer, "revision": model_args.model_revision, "token": model_args.token, + "trust_remote_code": model_args.trust_remote_code, } if model_args.tokenizer_name: tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs) @@ -417,11 +429,12 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, low_cpu_mem_usage=model_args.low_cpu_mem_usage, ) else: logger.info("Training new model from scratch") - model = AutoModelForMaskedLM.from_config(config) + model = AutoModelForMaskedLM.from_config(config, trust_remote_code=model_args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/language-modeling/run_mlm_no_trainer.py b/examples/pytorch/language-modeling/run_mlm_no_trainer.py index af1e8d5209..2fe8beca1b 100755 --- a/examples/pytorch/language-modeling/run_mlm_no_trainer.py +++ b/examples/pytorch/language-modeling/run_mlm_no_trainer.py @@ -200,6 +200,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -367,17 +377,21 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name) + config = AutoConfig.from_pretrained(args.config_name, trust_remote_code=args.trust_remote_code) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -390,10 +404,11 @@ def main(): from_tf=bool(".ckpt" in args.model_name_or_path), config=config, low_cpu_mem_usage=args.low_cpu_mem_usage, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForMaskedLM.from_config(config) + model = AutoModelForMaskedLM.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/multiple-choice/run_swag.py b/examples/pytorch/multiple-choice/run_swag.py index 61f72fd605..d5f4be5699 100755 --- a/examples/pytorch/multiple-choice/run_swag.py +++ b/examples/pytorch/multiple-choice/run_swag.py @@ -95,6 +95,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -328,6 +338,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -335,6 +346,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForMultipleChoice.from_pretrained( model_args.model_name_or_path, @@ -343,6 +355,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # When using your own dataset or a different dataset from swag, you will probably need to change this. diff --git a/examples/pytorch/multiple-choice/run_swag_no_trainer.py b/examples/pytorch/multiple-choice/run_swag_no_trainer.py index 26ae2d4944..e7ec89a429 100755 --- a/examples/pytorch/multiple-choice/run_swag_no_trainer.py +++ b/examples/pytorch/multiple-choice/run_swag_no_trainer.py @@ -182,6 +182,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -374,17 +384,21 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -396,10 +410,11 @@ def main(): args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForMultipleChoice.from_config(config) + model = AutoModelForMultipleChoice.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/question-answering/run_qa.py b/examples/pytorch/question-answering/run_qa.py index d823b28256..76f836ece6 100755 --- a/examples/pytorch/question-answering/run_qa.py +++ b/examples/pytorch/question-answering/run_qa.py @@ -95,6 +95,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -336,6 +346,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -343,6 +354,7 @@ def main(): use_fast=True, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForQuestionAnswering.from_pretrained( model_args.model_name_or_path, @@ -351,6 +363,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # Tokenizer check: this script requires a fast tokenizer. diff --git a/examples/pytorch/question-answering/run_qa_no_trainer.py b/examples/pytorch/question-answering/run_qa_no_trainer.py index 2a7cdc7273..0fc438d70b 100755 --- a/examples/pytorch/question-answering/run_qa_no_trainer.py +++ b/examples/pytorch/question-answering/run_qa_no_trainer.py @@ -273,6 +273,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -415,17 +425,21 @@ def main(): # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name) + config = AutoConfig.from_pretrained(args.config_name, trust_remote_code=args.trust_remote_code) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=True) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=True, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=True) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=True, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -437,10 +451,11 @@ def main(): args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForQuestionAnswering.from_config(config) + model = AutoModelForQuestionAnswering.from_config(config, trust_remote_code=args.trust_remote_code) # Preprocessing the datasets. # Preprocessing is slighlty different for training and evaluation. diff --git a/examples/pytorch/question-answering/run_seq2seq_qa.py b/examples/pytorch/question-answering/run_seq2seq_qa.py index ac3fb64974..404a27ba3a 100644 --- a/examples/pytorch/question-answering/run_seq2seq_qa.py +++ b/examples/pytorch/question-answering/run_seq2seq_qa.py @@ -96,6 +96,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -381,6 +391,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -388,6 +399,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSeq2SeqLM.from_pretrained( model_args.model_name_or_path, @@ -396,6 +408,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch diff --git a/examples/pytorch/semantic-segmentation/run_semantic_segmentation.py b/examples/pytorch/semantic-segmentation/run_semantic_segmentation.py index ce63e15fac..a7bde9eb21 100644 --- a/examples/pytorch/semantic-segmentation/run_semantic_segmentation.py +++ b/examples/pytorch/semantic-segmentation/run_semantic_segmentation.py @@ -257,6 +257,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) def main(): @@ -393,6 +403,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSemanticSegmentation.from_pretrained( model_args.model_name_or_path, @@ -401,12 +412,14 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) image_processor = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # Define torchvision transforms to be applied to each image + target. diff --git a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py index af9880901c..f0518ada5d 100644 --- a/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py +++ b/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py @@ -273,6 +273,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -400,9 +410,15 @@ def main(): label2id = {v: k for k, v in id2label.items()} # Load pretrained model and image processor - config = AutoConfig.from_pretrained(args.model_name_or_path, id2label=id2label, label2id=label2id) - image_processor = AutoImageProcessor.from_pretrained(args.model_name_or_path) - model = AutoModelForSemanticSegmentation.from_pretrained(args.model_name_or_path, config=config) + config = AutoConfig.from_pretrained( + args.model_name_or_path, id2label=id2label, label2id=label2id, trust_remote_code=args.trust_remote_code + ) + image_processor = AutoImageProcessor.from_pretrained( + args.model_name_or_path, trust_remote_code=args.trust_remote_code + ) + model = AutoModelForSemanticSegmentation.from_pretrained( + args.model_name_or_path, config=config, trust_remote_code=args.trust_remote_code + ) # Preprocessing the datasets # Define torchvision transforms to be applied to each image + target. diff --git a/examples/pytorch/speech-recognition/run_speech_recognition_ctc.py b/examples/pytorch/speech-recognition/run_speech_recognition_ctc.py index bff864ddb3..3674dd7d41 100755 --- a/examples/pytorch/speech-recognition/run_speech_recognition_ctc.py +++ b/examples/pytorch/speech-recognition/run_speech_recognition_ctc.py @@ -244,6 +244,16 @@ class DataTrainingArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) unk_token: str = field( default="[UNK]", metadata={"help": "The unk token for the tokenizer"}, @@ -505,6 +515,7 @@ def main(): model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) # 4. Next, if no tokenizer file is defined, @@ -561,12 +572,14 @@ def main(): tokenizer = AutoTokenizer.from_pretrained( tokenizer_name_or_path, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, **tokenizer_kwargs, ) feature_extractor = AutoFeatureExtractor.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) # adapt config @@ -595,6 +608,7 @@ def main(): cache_dir=model_args.cache_dir, config=config, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) # freeze encoder diff --git a/examples/pytorch/speech-recognition/run_speech_recognition_ctc_adapter.py b/examples/pytorch/speech-recognition/run_speech_recognition_ctc_adapter.py index be9021874a..b91fc87d05 100755 --- a/examples/pytorch/speech-recognition/run_speech_recognition_ctc_adapter.py +++ b/examples/pytorch/speech-recognition/run_speech_recognition_ctc_adapter.py @@ -247,6 +247,16 @@ class DataTrainingArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) unk_token: str = field( default="[UNK]", metadata={"help": "The unk token for the tokenizer"}, @@ -501,6 +511,7 @@ def main(): model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) # 4. Next, if no tokenizer file is defined, @@ -517,6 +528,7 @@ def main(): tokenizer = AutoTokenizer.from_pretrained( tokenizer_name_or_path, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) vocab_dict = tokenizer.vocab.copy() if tokenizer.target_lang is None: @@ -584,12 +596,14 @@ def main(): tokenizer = AutoTokenizer.from_pretrained( tokenizer_name_or_path, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, **tokenizer_kwargs, ) feature_extractor = AutoFeatureExtractor.from_pretrained( model_args.model_name_or_path, cache_dir=model_args.cache_dir, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ) # adapt config @@ -615,6 +629,7 @@ def main(): cache_dir=model_args.cache_dir, config=config, token=data_args.token, + trust_remote_code=data_args.trust_remote_code, ignore_mismatched_sizes=True, ) diff --git a/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py b/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py index aea7d3b7fd..f9744994ad 100755 --- a/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py +++ b/examples/pytorch/speech-recognition/run_speech_recognition_seq2seq.py @@ -101,6 +101,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) freeze_feature_encoder: bool = field( default=True, metadata={"help": "Whether to freeze the feature encoder layers of the model."} ) @@ -384,6 +394,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) config.update({"forced_decoder_ids": model_args.forced_decoder_ids, "suppress_tokens": model_args.suppress_tokens}) @@ -397,6 +408,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -404,6 +416,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSpeechSeq2Seq.from_pretrained( model_args.model_name_or_path, @@ -411,6 +424,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) if model.config.decoder_start_token_id is None: diff --git a/examples/pytorch/summarization/run_summarization.py b/examples/pytorch/summarization/run_summarization.py index 4a27df7582..d145a8549d 100755 --- a/examples/pytorch/summarization/run_summarization.py +++ b/examples/pytorch/summarization/run_summarization.py @@ -115,6 +115,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) resize_position_embeddings: Optional[bool] = field( default=None, metadata={ @@ -431,6 +441,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -438,6 +449,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSeq2SeqLM.from_pretrained( model_args.model_name_or_path, @@ -446,6 +458,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch diff --git a/examples/pytorch/summarization/run_summarization_no_trainer.py b/examples/pytorch/summarization/run_summarization_no_trainer.py index 543b6bfaa2..65a924a800 100644 --- a/examples/pytorch/summarization/run_summarization_no_trainer.py +++ b/examples/pytorch/summarization/run_summarization_no_trainer.py @@ -266,6 +266,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -406,17 +416,21 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name) + config = AutoConfig.from_pretrained(args.config_name, trust_remote_code=args.trust_remote_code) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -428,10 +442,11 @@ def main(): args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForSeq2SeqLM.from_config(config) + model = AutoModelForSeq2SeqLM.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/text-classification/run_classification.py b/examples/pytorch/text-classification/run_classification.py index a5a4ad49f8..68d2475fd5 100755 --- a/examples/pytorch/text-classification/run_classification.py +++ b/examples/pytorch/text-classification/run_classification.py @@ -243,6 +243,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -482,6 +492,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) if is_regression: @@ -500,6 +511,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path, @@ -508,6 +520,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) diff --git a/examples/pytorch/text-classification/run_glue.py b/examples/pytorch/text-classification/run_glue.py index 3f8bcc2fbb..1d702ef524 100755 --- a/examples/pytorch/text-classification/run_glue.py +++ b/examples/pytorch/text-classification/run_glue.py @@ -204,6 +204,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -375,6 +385,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -382,6 +393,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path, @@ -390,6 +402,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) diff --git a/examples/pytorch/text-classification/run_glue_no_trainer.py b/examples/pytorch/text-classification/run_glue_no_trainer.py index e137f87638..1ab8bb0db8 100644 --- a/examples/pytorch/text-classification/run_glue_no_trainer.py +++ b/examples/pytorch/text-classification/run_glue_no_trainer.py @@ -156,6 +156,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -309,13 +319,21 @@ def main(): # # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. - config = AutoConfig.from_pretrained(args.model_name_or_path, num_labels=num_labels, finetuning_task=args.task_name) - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + config = AutoConfig.from_pretrained( + args.model_name_or_path, + num_labels=num_labels, + finetuning_task=args.task_name, + trust_remote_code=args.trust_remote_code, + ) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) model = AutoModelForSequenceClassification.from_pretrained( args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, ignore_mismatched_sizes=args.ignore_mismatched_sizes, + trust_remote_code=args.trust_remote_code, ) # Preprocessing the datasets diff --git a/examples/pytorch/text-classification/run_xnli.py b/examples/pytorch/text-classification/run_xnli.py index 459a59282e..1fae632518 100755 --- a/examples/pytorch/text-classification/run_xnli.py +++ b/examples/pytorch/text-classification/run_xnli.py @@ -168,6 +168,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -292,6 +302,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -300,6 +311,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path, @@ -308,6 +320,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) diff --git a/examples/pytorch/token-classification/run_ner.py b/examples/pytorch/token-classification/run_ner.py index 0e9d160412..fae5142c0c 100755 --- a/examples/pytorch/token-classification/run_ner.py +++ b/examples/pytorch/token-classification/run_ner.py @@ -95,6 +95,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -362,6 +372,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer_name_or_path = model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path @@ -372,6 +383,7 @@ def main(): use_fast=True, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, add_prefix_space=True, ) else: @@ -381,6 +393,7 @@ def main(): use_fast=True, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForTokenClassification.from_pretrained( @@ -390,6 +403,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) diff --git a/examples/pytorch/token-classification/run_ner_no_trainer.py b/examples/pytorch/token-classification/run_ner_no_trainer.py index dc57b514e7..a38f0e47d0 100755 --- a/examples/pytorch/token-classification/run_ner_no_trainer.py +++ b/examples/pytorch/token-classification/run_ner_no_trainer.py @@ -210,6 +210,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -388,9 +398,13 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name, num_labels=num_labels) + config = AutoConfig.from_pretrained( + args.config_name, num_labels=num_labels, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path, num_labels=num_labels) + config = AutoConfig.from_pretrained( + args.model_name_or_path, num_labels=num_labels, trust_remote_code=args.trust_remote_code + ) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") @@ -403,9 +417,13 @@ def main(): ) if config.model_type in {"bloom", "gpt2", "roberta"}: - tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path, use_fast=True, add_prefix_space=True) + tokenizer = AutoTokenizer.from_pretrained( + tokenizer_name_or_path, use_fast=True, add_prefix_space=True, trust_remote_code=args.trust_remote_code + ) else: - tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path, use_fast=True) + tokenizer = AutoTokenizer.from_pretrained( + tokenizer_name_or_path, use_fast=True, trust_remote_code=args.trust_remote_code + ) if args.model_name_or_path: model = AutoModelForTokenClassification.from_pretrained( @@ -413,10 +431,11 @@ def main(): from_tf=bool(".ckpt" in args.model_name_or_path), config=config, ignore_mismatched_sizes=args.ignore_mismatched_sizes, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForTokenClassification.from_config(config) + model = AutoModelForTokenClassification.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/pytorch/translation/run_translation.py b/examples/pytorch/translation/run_translation.py index 179ef71ad3..1e39ab31ba 100755 --- a/examples/pytorch/translation/run_translation.py +++ b/examples/pytorch/translation/run_translation.py @@ -105,6 +105,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -380,6 +390,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -387,6 +398,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSeq2SeqLM.from_pretrained( model_args.model_name_or_path, @@ -395,6 +407,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch diff --git a/examples/pytorch/translation/run_translation_no_trainer.py b/examples/pytorch/translation/run_translation_no_trainer.py index 77efa14f4f..b61e715d37 100644 --- a/examples/pytorch/translation/run_translation_no_trainer.py +++ b/examples/pytorch/translation/run_translation_no_trainer.py @@ -257,6 +257,16 @@ def parse_args(): "--hub_model_id", type=str, help="The name of the repository to keep in sync with the local `output_dir`." ) parser.add_argument("--hub_token", type=str, help="The token to use to push to the Model Hub.") + parser.add_argument( + "--trust_remote_code", + type=bool, + default=False, + help=( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ), + ) parser.add_argument( "--checkpointing_steps", type=str, @@ -386,17 +396,21 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if args.config_name: - config = AutoConfig.from_pretrained(args.config_name) + config = AutoConfig.from_pretrained(args.config_name, trust_remote_code=args.trust_remote_code) elif args.model_name_or_path: - config = AutoConfig.from_pretrained(args.model_name_or_path) + config = AutoConfig.from_pretrained(args.model_name_or_path, trust_remote_code=args.trust_remote_code) else: config = CONFIG_MAPPING[args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_name, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.tokenizer_name, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) elif args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=not args.use_slow_tokenizer) + tokenizer = AutoTokenizer.from_pretrained( + args.model_name_or_path, use_fast=not args.use_slow_tokenizer, trust_remote_code=args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -408,10 +422,11 @@ def main(): args.model_name_or_path, from_tf=bool(".ckpt" in args.model_name_or_path), config=config, + trust_remote_code=args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = AutoModelForSeq2SeqLM.from_config(config) + model = AutoModelForSeq2SeqLM.from_config(config, trust_remote_code=args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/tensorflow/image-classification/run_image_classification.py b/examples/tensorflow/image-classification/run_image_classification.py index 23e04c9318..5223b7db0b 100644 --- a/examples/tensorflow/image-classification/run_image_classification.py +++ b/examples/tensorflow/image-classification/run_image_classification.py @@ -173,6 +173,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) ignore_mismatched_sizes: bool = field( default=False, metadata={"help": "Will enable to load a pretrained model whose head dimensions are different."}, @@ -323,12 +333,14 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) image_processor = AutoImageProcessor.from_pretrained( model_args.image_processor_name or model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # If we don't have a validation split, split off a percentage of train as validation. @@ -449,6 +461,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ignore_mismatched_sizes=model_args.ignore_mismatched_sizes, ) num_replicas = training_args.strategy.num_replicas_in_sync diff --git a/examples/tensorflow/language-modeling/run_clm.py b/examples/tensorflow/language-modeling/run_clm.py index 7c068d5c8c..43d5a1d8a3 100755 --- a/examples/tensorflow/language-modeling/run_clm.py +++ b/examples/tensorflow/language-modeling/run_clm.py @@ -128,6 +128,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) def __post_init__(self): if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): @@ -366,17 +376,26 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: - config = AutoConfig.from_pretrained(model_args.config_name) + config = AutoConfig.from_pretrained( + model_args.config_name, + trust_remote_code=model_args.trust_remote_code, + ) elif model_args.model_name_or_path: - config = AutoConfig.from_pretrained(model_args.model_name_or_path) + config = AutoConfig.from_pretrained( + model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code + ) else: config = CONFIG_MAPPING[model_args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if model_args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name) + tokenizer = AutoTokenizer.from_pretrained( + model_args.tokenizer_name, trust_remote_code=model_args.trust_remote_code + ) elif model_args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path) + tokenizer = AutoTokenizer.from_pretrained( + model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -479,12 +498,16 @@ def main(): with training_args.strategy.scope(): # region Prepare model if checkpoint is not None: - model = TFAutoModelForCausalLM.from_pretrained(checkpoint, config=config) + model = TFAutoModelForCausalLM.from_pretrained( + checkpoint, config=config, trust_remote_code=model_args.trust_remote_code + ) elif model_args.model_name_or_path: - model = TFAutoModelForCausalLM.from_pretrained(model_args.model_name_or_path, config=config) + model = TFAutoModelForCausalLM.from_pretrained( + model_args.model_name_or_path, config=config, trust_remote_code=model_args.trust_remote_code + ) else: logger.info("Training new model from scratch") - model = TFAutoModelForCausalLM.from_config(config) + model = TFAutoModelForCausalLM.from_config(config, trust_remote_code=model_args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/tensorflow/language-modeling/run_mlm.py b/examples/tensorflow/language-modeling/run_mlm.py index 7ea1a2f853..75edfa954a 100755 --- a/examples/tensorflow/language-modeling/run_mlm.py +++ b/examples/tensorflow/language-modeling/run_mlm.py @@ -126,6 +126,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) def __post_init__(self): if self.config_overrides is not None and (self.config_name is not None or self.model_name_or_path is not None): @@ -348,19 +358,25 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if checkpoint is not None: - config = AutoConfig.from_pretrained(checkpoint) + config = AutoConfig.from_pretrained(checkpoint, trust_remote_code=model_args.trust_remote_code) elif model_args.config_name: - config = AutoConfig.from_pretrained(model_args.config_name) + config = AutoConfig.from_pretrained(model_args.config_name, trust_remote_code=model_args.trust_remote_code) elif model_args.model_name_or_path: - config = AutoConfig.from_pretrained(model_args.model_name_or_path) + config = AutoConfig.from_pretrained( + model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code + ) else: config = CONFIG_MAPPING[model_args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") if model_args.tokenizer_name: - tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name) + tokenizer = AutoTokenizer.from_pretrained( + model_args.tokenizer_name, trust_remote_code=model_args.trust_remote_code + ) elif model_args.model_name_or_path: - tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path) + tokenizer = AutoTokenizer.from_pretrained( + model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code + ) else: raise ValueError( "You are instantiating a new tokenizer from scratch. This is not supported by this script." @@ -495,12 +511,16 @@ def main(): with training_args.strategy.scope(): # region Prepare model if checkpoint is not None: - model = TFAutoModelForMaskedLM.from_pretrained(checkpoint, config=config) + model = TFAutoModelForMaskedLM.from_pretrained( + checkpoint, config=config, trust_remote_code=model_args.trust_remote_code + ) elif model_args.model_name_or_path: - model = TFAutoModelForMaskedLM.from_pretrained(model_args.model_name_or_path, config=config) + model = TFAutoModelForMaskedLM.from_pretrained( + model_args.model_name_or_path, config=config, trust_remote_code=model_args.trust_remote_code + ) else: logger.info("Training new model from scratch") - model = TFAutoModelForMaskedLM.from_config(config) + model = TFAutoModelForMaskedLM.from_config(config, trust_remote_code=model_args.trust_remote_code) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/tensorflow/multiple-choice/run_swag.py b/examples/tensorflow/multiple-choice/run_swag.py index 170117ad9d..d78060dfa4 100644 --- a/examples/tensorflow/multiple-choice/run_swag.py +++ b/examples/tensorflow/multiple-choice/run_swag.py @@ -162,6 +162,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -349,6 +359,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -356,6 +367,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion @@ -442,6 +454,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) num_replicas = training_args.strategy.num_replicas_in_sync diff --git a/examples/tensorflow/question-answering/run_qa.py b/examples/tensorflow/question-answering/run_qa.py index 8bbc986d0e..6152263d6b 100755 --- a/examples/tensorflow/question-answering/run_qa.py +++ b/examples/tensorflow/question-answering/run_qa.py @@ -93,6 +93,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -352,6 +362,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -359,6 +370,7 @@ def main(): use_fast=True, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion @@ -639,6 +651,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) if training_args.do_train: training_dataset = model.prepare_tf_dataset( diff --git a/examples/tensorflow/summarization/run_summarization.py b/examples/tensorflow/summarization/run_summarization.py index 82359f0921..03c434330b 100644 --- a/examples/tensorflow/summarization/run_summarization.py +++ b/examples/tensorflow/summarization/run_summarization.py @@ -115,6 +115,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -402,6 +412,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -409,6 +420,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) prefix = data_args.source_prefix if data_args.source_prefix is not None else "" @@ -527,6 +539,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch diff --git a/examples/tensorflow/text-classification/run_glue.py b/examples/tensorflow/text-classification/run_glue.py index daeed0c6ea..785e88a71d 100644 --- a/examples/tensorflow/text-classification/run_glue.py +++ b/examples/tensorflow/text-classification/run_glue.py @@ -180,6 +180,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) # endregion @@ -298,6 +308,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -305,6 +316,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion @@ -388,6 +400,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion diff --git a/examples/tensorflow/text-classification/run_text_classification.py b/examples/tensorflow/text-classification/run_text_classification.py index a68aed942d..0d2ea87b96 100644 --- a/examples/tensorflow/text-classification/run_text_classification.py +++ b/examples/tensorflow/text-classification/run_text_classification.py @@ -186,6 +186,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) # endregion @@ -315,6 +325,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) else: config = AutoConfig.from_pretrained( @@ -322,12 +333,14 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion @@ -416,6 +429,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # endregion diff --git a/examples/tensorflow/token-classification/run_ner.py b/examples/tensorflow/token-classification/run_ner.py index ed8ab00e4f..bcf47eb83c 100644 --- a/examples/tensorflow/token-classification/run_ner.py +++ b/examples/tensorflow/token-classification/run_ner.py @@ -91,6 +91,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -304,9 +314,17 @@ def main(): # In distributed training, the .from_pretrained methods guarantee that only one local process can concurrently # download model & vocab. if model_args.config_name: - config = AutoConfig.from_pretrained(model_args.config_name, num_labels=num_labels) + config = AutoConfig.from_pretrained( + model_args.config_name, + num_labels=num_labels, + trust_remote_code=model_args.trust_remote_code, + ) elif model_args.model_name_or_path: - config = AutoConfig.from_pretrained(model_args.model_name_or_path, num_labels=num_labels) + config = AutoConfig.from_pretrained( + model_args.model_name_or_path, + num_labels=num_labels, + trust_remote_code=model_args.trust_remote_code, + ) else: config = CONFIG_MAPPING[model_args.model_type]() logger.warning("You are instantiating a new config instance from scratch.") @@ -319,9 +337,18 @@ def main(): ) if config.model_type in {"gpt2", "roberta"}: - tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path, use_fast=True, add_prefix_space=True) + tokenizer = AutoTokenizer.from_pretrained( + tokenizer_name_or_path, + use_fast=True, + add_prefix_space=True, + trust_remote_code=model_args.trust_remote_code, + ) else: - tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path, use_fast=True) + tokenizer = AutoTokenizer.from_pretrained( + tokenizer_name_or_path, + use_fast=True, + trust_remote_code=model_args.trust_remote_code, + ) # endregion # region Preprocessing the raw datasets @@ -392,10 +419,13 @@ def main(): model = TFAutoModelForTokenClassification.from_pretrained( model_args.model_name_or_path, config=config, + trust_remote_code=model_args.trust_remote_code, ) else: logger.info("Training new model from scratch") - model = TFAutoModelForTokenClassification.from_config(config) + model = TFAutoModelForTokenClassification.from_config( + config, trust_remote_code=model_args.trust_remote_code + ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch # on a small vocab and want a smaller embedding size, remove this test. diff --git a/examples/tensorflow/translation/run_translation.py b/examples/tensorflow/translation/run_translation.py index 68e9e9c16b..2673983ec9 100644 --- a/examples/tensorflow/translation/run_translation.py +++ b/examples/tensorflow/translation/run_translation.py @@ -109,6 +109,16 @@ class ModelArguments: "help": "The `use_auth_token` argument is deprecated and will be removed in v4.34. Please use `token`." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) @dataclass @@ -366,6 +376,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -373,6 +384,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) prefix = data_args.source_prefix if data_args.source_prefix is not None else "" @@ -480,6 +492,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=model_args.token, + trust_remote_code=model_args.trust_remote_code, ) # We resize the embeddings only when necessary to avoid index errors. If you are creating a model from scratch diff --git a/templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py b/templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py index c6b72f4fa3..12e59b8b24 100755 --- a/templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py +++ b/templates/adding_a_new_example_script/{{cookiecutter.directory_name}}/run_{{cookiecutter.example_shortcut}}.py @@ -122,6 +122,16 @@ class ModelArguments: "with private models)." }, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether or not to allow for custom models defined on the Hub in their own modeling files. This option" + "should only be set to `True` for repositories you trust and in which you have read the code, as it will" + "execute code present on the Hub on your local machine." + ) + }, + ) {% endif %} @@ -290,6 +300,7 @@ def main(): "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "token": True if model_args.token else None, + "trust_remote_code": model_args.trust_remote_code, } if model_args.config_name: config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs) @@ -304,6 +315,7 @@ def main(): "use_fast": model_args.use_fast_tokenizer, "revision": model_args.model_revision, "token": True if model_args.token else None, + "trust_remote_code": model_args.trust_remote_code, } if model_args.tokenizer_name: tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs) @@ -323,6 +335,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=True if model_args.token else None, + trust_remote_code=model_args.trust_remote_code, ) else: logger.info("Training new model from scratch") @@ -337,6 +350,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=True if model_args.token else None, + trust_remote_code=model_args.trust_remote_code, ) tokenizer = AutoTokenizer.from_pretrained( model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path, @@ -344,6 +358,7 @@ def main(): use_fast=model_args.use_fast_tokenizer, revision=model_args.model_revision, token=True if model_args.token else None, + trust_remote_code=model_args.trust_remote_code, ) model = AutoModelForSequenceClassification.from_pretrained( model_args.model_name_or_path, @@ -352,6 +367,7 @@ def main(): cache_dir=model_args.cache_dir, revision=model_args.model_revision, token=True if model_args.token else None, + trust_remote_code=model_args.trust_remote_code, ) {% endif %}