From fe4546a7bb3f8bd0a8e87ddd244fa1c2e1ed0022 Mon Sep 17 00:00:00 2001 From: Yaser Afshar Date: Thu, 24 Oct 2024 15:57:27 -0700 Subject: [PATCH] Add trust_remote_code parameter and remove True - Introduced a new model parameter `trust_remote_code` - Set the default value of `trust_remote_code` to `False` to enhance security Former-commit-id: 09437763267bc7081159a6878cee9652a2b1ddac --- examples/extras/nlg_eval/llama3_lora_predict.yaml | 1 + examples/inference/llama3.yaml | 1 + examples/inference/llama3_lora_sft.yaml | 1 + examples/inference/llama3_vllm.yaml | 1 + examples/inference/llava1_5.yaml | 1 + examples/inference/qwen2_vl.yaml | 1 + examples/merge_lora/llama3_gptq.yaml | 1 + examples/merge_lora/llama3_lora_sft.yaml | 1 + examples/merge_lora/qwen2vl_lora_sft.yaml | 1 + examples/train_full/llama3_full_sft.yaml | 1 + examples/train_full/qwen2vl_full_sft.yaml | 1 + examples/train_lora/llama3_lora_dpo.yaml | 1 + examples/train_lora/llama3_lora_eval.yaml | 1 + examples/train_lora/llama3_lora_kto.yaml | 1 + examples/train_lora/llama3_lora_ppo.yaml | 1 + examples/train_lora/llama3_lora_pretrain.yaml | 1 + examples/train_lora/llama3_lora_reward.yaml | 1 + examples/train_lora/llama3_lora_sft.yaml | 1 + examples/train_lora/llama3_lora_sft_ds3.yaml | 1 + examples/train_lora/llama3_preprocess.yaml | 1 + examples/train_lora/llava1_5_lora_sft.yaml | 1 + examples/train_lora/qwen2vl_lora_dpo.yaml | 1 + examples/train_lora/qwen2vl_lora_sft.yaml | 1 + examples/train_qlora/llama3_lora_sft_aqlm.yaml | 1 + examples/train_qlora/llama3_lora_sft_awq.yaml | 1 + examples/train_qlora/llama3_lora_sft_gptq.yaml | 1 + examples/train_qlora/llama3_lora_sft_otfq.yaml | 1 + src/llamafactory/chat/vllm_engine.py | 2 +- src/llamafactory/data/loader.py | 2 +- src/llamafactory/eval/evaluator.py | 2 +- src/llamafactory/hparams/model_args.py | 10 ++++++++++ src/llamafactory/model/loader.py | 4 ++-- src/llamafactory/model/model_utils/unsloth.py | 2 +- 33 files changed, 43 insertions(+), 6 deletions(-) diff --git a/examples/extras/nlg_eval/llama3_lora_predict.yaml b/examples/extras/nlg_eval/llama3_lora_predict.yaml index 3cdca843..2d0dfad3 100644 --- a/examples/extras/nlg_eval/llama3_lora_predict.yaml +++ b/examples/extras/nlg_eval/llama3_lora_predict.yaml @@ -4,6 +4,7 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct adapter_name_or_path: saves/llama3-8b/lora/sft +trust_remote_code: true ### method stage: sft diff --git a/examples/inference/llama3.yaml b/examples/inference/llama3.yaml index a2a8c6fd..2851e9a3 100644 --- a/examples/inference/llama3.yaml +++ b/examples/inference/llama3.yaml @@ -1,3 +1,4 @@ model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct template: llama3 infer_backend: huggingface # choices: [huggingface, vllm] +trust_remote_code: true diff --git a/examples/inference/llama3_lora_sft.yaml b/examples/inference/llama3_lora_sft.yaml index 25d5457f..7796c526 100644 --- a/examples/inference/llama3_lora_sft.yaml +++ b/examples/inference/llama3_lora_sft.yaml @@ -2,3 +2,4 @@ model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct adapter_name_or_path: saves/llama3-8b/lora/sft template: llama3 infer_backend: huggingface # choices: [huggingface, vllm] +trust_remote_code: true diff --git a/examples/inference/llama3_vllm.yaml b/examples/inference/llama3_vllm.yaml index 8dd3b61a..4379956c 100644 --- a/examples/inference/llama3_vllm.yaml +++ b/examples/inference/llama3_vllm.yaml @@ -2,3 +2,4 @@ model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct template: llama3 infer_backend: vllm vllm_enforce_eager: true +trust_remote_code: true diff --git a/examples/inference/llava1_5.yaml b/examples/inference/llava1_5.yaml index 163d6e1e..2e934ddc 100644 --- a/examples/inference/llava1_5.yaml +++ b/examples/inference/llava1_5.yaml @@ -1,3 +1,4 @@ model_name_or_path: llava-hf/llava-1.5-7b-hf template: llava infer_backend: huggingface # choices: [huggingface, vllm] +trust_remote_code: true diff --git a/examples/inference/qwen2_vl.yaml b/examples/inference/qwen2_vl.yaml index f6dc5539..b5eabc66 100644 --- a/examples/inference/qwen2_vl.yaml +++ b/examples/inference/qwen2_vl.yaml @@ -1,3 +1,4 @@ model_name_or_path: Qwen/Qwen2-VL-7B-Instruct template: qwen2_vl infer_backend: huggingface # choices: [huggingface, vllm] +trust_remote_code: true diff --git a/examples/merge_lora/llama3_gptq.yaml b/examples/merge_lora/llama3_gptq.yaml index 70c96a6b..21bd05dd 100644 --- a/examples/merge_lora/llama3_gptq.yaml +++ b/examples/merge_lora/llama3_gptq.yaml @@ -1,6 +1,7 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct template: llama3 +trust_remote_code: true ### export export_dir: models/llama3_gptq diff --git a/examples/merge_lora/llama3_lora_sft.yaml b/examples/merge_lora/llama3_lora_sft.yaml index 1e017f69..24fc0c18 100644 --- a/examples/merge_lora/llama3_lora_sft.yaml +++ b/examples/merge_lora/llama3_lora_sft.yaml @@ -5,6 +5,7 @@ model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct adapter_name_or_path: saves/llama3-8b/lora/sft template: llama3 finetuning_type: lora +trust_remote_code: true ### export export_dir: models/llama3_lora_sft diff --git a/examples/merge_lora/qwen2vl_lora_sft.yaml b/examples/merge_lora/qwen2vl_lora_sft.yaml index f97e2766..ebbb4c71 100644 --- a/examples/merge_lora/qwen2vl_lora_sft.yaml +++ b/examples/merge_lora/qwen2vl_lora_sft.yaml @@ -5,6 +5,7 @@ model_name_or_path: Qwen/Qwen2-VL-7B-Instruct adapter_name_or_path: saves/qwen2_vl-7b/lora/sft template: qwen2_vl finetuning_type: lora +trust_remote_code: true ### export export_dir: models/qwen2_vl_lora_sft diff --git a/examples/train_full/llama3_full_sft.yaml b/examples/train_full/llama3_full_sft.yaml index d1afe101..fd52f26d 100644 --- a/examples/train_full/llama3_full_sft.yaml +++ b/examples/train_full/llama3_full_sft.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_full/qwen2vl_full_sft.yaml b/examples/train_full/qwen2vl_full_sft.yaml index 3cf299bd..855c98ac 100644 --- a/examples/train_full/qwen2vl_full_sft.yaml +++ b/examples/train_full/qwen2vl_full_sft.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: Qwen/Qwen2-VL-7B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_lora/llama3_lora_dpo.yaml b/examples/train_lora/llama3_lora_dpo.yaml index db026608..56914866 100644 --- a/examples/train_lora/llama3_lora_dpo.yaml +++ b/examples/train_lora/llama3_lora_dpo.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: dpo diff --git a/examples/train_lora/llama3_lora_eval.yaml b/examples/train_lora/llama3_lora_eval.yaml index 5dac08a8..60d7c2f3 100644 --- a/examples/train_lora/llama3_lora_eval.yaml +++ b/examples/train_lora/llama3_lora_eval.yaml @@ -1,6 +1,7 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct adapter_name_or_path: saves/llama3-8b/lora/sft +trust_remote_code: true ### method finetuning_type: lora diff --git a/examples/train_lora/llama3_lora_kto.yaml b/examples/train_lora/llama3_lora_kto.yaml index 2c21f752..d52ce79d 100644 --- a/examples/train_lora/llama3_lora_kto.yaml +++ b/examples/train_lora/llama3_lora_kto.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: kto diff --git a/examples/train_lora/llama3_lora_ppo.yaml b/examples/train_lora/llama3_lora_ppo.yaml index fc2dfb24..ec0231e7 100644 --- a/examples/train_lora/llama3_lora_ppo.yaml +++ b/examples/train_lora/llama3_lora_ppo.yaml @@ -1,6 +1,7 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct reward_model: saves/llama3-8b/lora/reward +trust_remote_code: true ### method stage: ppo diff --git a/examples/train_lora/llama3_lora_pretrain.yaml b/examples/train_lora/llama3_lora_pretrain.yaml index 71f8603c..a6378671 100644 --- a/examples/train_lora/llama3_lora_pretrain.yaml +++ b/examples/train_lora/llama3_lora_pretrain.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: pt diff --git a/examples/train_lora/llama3_lora_reward.yaml b/examples/train_lora/llama3_lora_reward.yaml index b8450ef0..c42350aa 100644 --- a/examples/train_lora/llama3_lora_reward.yaml +++ b/examples/train_lora/llama3_lora_reward.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: rm diff --git a/examples/train_lora/llama3_lora_sft.yaml b/examples/train_lora/llama3_lora_sft.yaml index 8f998a69..243f2445 100644 --- a/examples/train_lora/llama3_lora_sft.yaml +++ b/examples/train_lora/llama3_lora_sft.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_lora/llama3_lora_sft_ds3.yaml b/examples/train_lora/llama3_lora_sft_ds3.yaml index 89a686d7..41752418 100644 --- a/examples/train_lora/llama3_lora_sft_ds3.yaml +++ b/examples/train_lora/llama3_lora_sft_ds3.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_lora/llama3_preprocess.yaml b/examples/train_lora/llama3_preprocess.yaml index c7344797..78df8cdb 100644 --- a/examples/train_lora/llama3_preprocess.yaml +++ b/examples/train_lora/llama3_preprocess.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_lora/llava1_5_lora_sft.yaml b/examples/train_lora/llava1_5_lora_sft.yaml index 1831153b..2f60f927 100644 --- a/examples/train_lora/llava1_5_lora_sft.yaml +++ b/examples/train_lora/llava1_5_lora_sft.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: llava-hf/llava-1.5-7b-hf +trust_remote_code: true ### method stage: sft diff --git a/examples/train_lora/qwen2vl_lora_dpo.yaml b/examples/train_lora/qwen2vl_lora_dpo.yaml index 08d9d8bd..e11fbd0f 100644 --- a/examples/train_lora/qwen2vl_lora_dpo.yaml +++ b/examples/train_lora/qwen2vl_lora_dpo.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: Qwen/Qwen2-VL-7B-Instruct +trust_remote_code: true ### method stage: dpo diff --git a/examples/train_lora/qwen2vl_lora_sft.yaml b/examples/train_lora/qwen2vl_lora_sft.yaml index 9c7e6742..14eea19e 100644 --- a/examples/train_lora/qwen2vl_lora_sft.yaml +++ b/examples/train_lora/qwen2vl_lora_sft.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: Qwen/Qwen2-VL-7B-Instruct +trust_remote_code: true ### method stage: sft diff --git a/examples/train_qlora/llama3_lora_sft_aqlm.yaml b/examples/train_qlora/llama3_lora_sft_aqlm.yaml index c426e310..89b77e5a 100644 --- a/examples/train_qlora/llama3_lora_sft_aqlm.yaml +++ b/examples/train_qlora/llama3_lora_sft_aqlm.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: ISTA-DASLab/Meta-Llama-3-8B-Instruct-AQLM-2Bit-1x16 +trust_remote_code: true ### method stage: sft diff --git a/examples/train_qlora/llama3_lora_sft_awq.yaml b/examples/train_qlora/llama3_lora_sft_awq.yaml index 09ab19a4..61c03e50 100644 --- a/examples/train_qlora/llama3_lora_sft_awq.yaml +++ b/examples/train_qlora/llama3_lora_sft_awq.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: TechxGenus/Meta-Llama-3-8B-Instruct-AWQ +trust_remote_code: true ### method stage: sft diff --git a/examples/train_qlora/llama3_lora_sft_gptq.yaml b/examples/train_qlora/llama3_lora_sft_gptq.yaml index 9b282a76..47626b82 100644 --- a/examples/train_qlora/llama3_lora_sft_gptq.yaml +++ b/examples/train_qlora/llama3_lora_sft_gptq.yaml @@ -1,5 +1,6 @@ ### model model_name_or_path: TechxGenus/Meta-Llama-3-8B-Instruct-GPTQ +trust_remote_code: true ### method stage: sft diff --git a/examples/train_qlora/llama3_lora_sft_otfq.yaml b/examples/train_qlora/llama3_lora_sft_otfq.yaml index ed6b4748..76d7259e 100644 --- a/examples/train_qlora/llama3_lora_sft_otfq.yaml +++ b/examples/train_qlora/llama3_lora_sft_otfq.yaml @@ -2,6 +2,7 @@ model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct quantization_bit: 4 quantization_method: bitsandbytes # choices: [bitsandbytes (4/8), hqq (2/3/4/5/6/8), eetq (8)] +trust_remote_code: true ### method stage: sft diff --git a/src/llamafactory/chat/vllm_engine.py b/src/llamafactory/chat/vllm_engine.py index 527fde07..fd54b5a9 100644 --- a/src/llamafactory/chat/vllm_engine.py +++ b/src/llamafactory/chat/vllm_engine.py @@ -72,7 +72,7 @@ class VllmEngine(BaseEngine): engine_args = { "model": model_args.model_name_or_path, - "trust_remote_code": True, + "trust_remote_code": model_args.trust_remote_code, "download_dir": model_args.cache_dir, "dtype": model_args.infer_dtype, "max_model_len": model_args.vllm_maxlen, diff --git a/src/llamafactory/data/loader.py b/src/llamafactory/data/loader.py index 4924f98e..863b6492 100644 --- a/src/llamafactory/data/loader.py +++ b/src/llamafactory/data/loader.py @@ -129,7 +129,7 @@ def _load_single_dataset( token=model_args.hf_hub_token, streaming=data_args.streaming, num_proc=data_args.preprocessing_num_workers, - trust_remote_code=True, + trust_remote_code=model_args.trust_remote_code, ) if dataset_attr.num_samples is not None and not data_args.streaming: diff --git a/src/llamafactory/eval/evaluator.py b/src/llamafactory/eval/evaluator.py index 46bc2666..82e37f41 100644 --- a/src/llamafactory/eval/evaluator.py +++ b/src/llamafactory/eval/evaluator.py @@ -100,7 +100,7 @@ class Evaluator: cache_dir=self.model_args.cache_dir, download_mode=self.eval_args.download_mode, token=self.model_args.hf_hub_token, - trust_remote_code=True, + trust_remote_code=self.model_args.trust_remote_code, ) pbar.set_postfix_str(categorys[subject]["name"]) inputs, outputs, labels = [], [], [] diff --git a/src/llamafactory/hparams/model_args.py b/src/llamafactory/hparams/model_args.py index e823e6ec..ba4c9725 100644 --- a/src/llamafactory/hparams/model_args.py +++ b/src/llamafactory/hparams/model_args.py @@ -305,6 +305,16 @@ class ModelArguments(QuantizationArguments, ProcessorArguments, ExportArguments, init=False, metadata={"help": "Whether use block diag attention or not, derived from `neat_packing`. Do not specify it."}, ) + trust_remote_code: bool = field( + default=False, + metadata={ + "help": ( + "Whether to trust the execution of code from datasets/models defined on the Hub. " + "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.model_name_or_path is None: diff --git a/src/llamafactory/model/loader.py b/src/llamafactory/model/loader.py index 0b696720..022cce06 100644 --- a/src/llamafactory/model/loader.py +++ b/src/llamafactory/model/loader.py @@ -52,7 +52,7 @@ def _get_init_kwargs(model_args: "ModelArguments") -> Dict[str, Any]: skip_check_imports() model_args.model_name_or_path = try_download_model_from_other_hub(model_args) return { - "trust_remote_code": True, + "trust_remote_code": model_args.trust_remote_code, "cache_dir": model_args.cache_dir, "revision": model_args.model_revision, "token": model_args.hf_hub_token, @@ -155,7 +155,7 @@ def load_model( load_class = AutoModelForCausalLM if model_args.train_from_scratch: - model = load_class.from_config(config, trust_remote_code=True) + model = load_class.from_config(config, trust_remote_code=model_args.trust_remote_code) else: model = load_class.from_pretrained(**init_kwargs) diff --git a/src/llamafactory/model/model_utils/unsloth.py b/src/llamafactory/model/model_utils/unsloth.py index e87f4fd0..4f4fd499 100644 --- a/src/llamafactory/model/model_utils/unsloth.py +++ b/src/llamafactory/model/model_utils/unsloth.py @@ -39,7 +39,7 @@ def _get_unsloth_kwargs( "device_map": {"": get_current_device()}, "rope_scaling": getattr(config, "rope_scaling", None), "fix_tokenizer": False, - "trust_remote_code": True, + "trust_remote_code": model_args.trust_remote_code, "use_gradient_checkpointing": "unsloth", }