mirror of
https://github.com/modelscope/modelscope.git
synced 2026-09-01 19:49:03 +02:00
fix trust_remote_code=true
This commit is contained in:
@@ -170,7 +170,7 @@ class MsDataset:
|
||||
custom_cfg: Optional[Config] = Config(),
|
||||
token: Optional[str] = None,
|
||||
dataset_info_only: Optional[bool] = False,
|
||||
trust_remote_code: Optional[bool] = True,
|
||||
trust_remote_code: Optional[bool] = False,
|
||||
**config_kwargs,
|
||||
) -> Union[dict, 'MsDataset', NativeIterableDataset]:
|
||||
"""Load a MsDataset from the ModelScope Hub, Hugging Face Hub, urls, or a local dataset.
|
||||
|
||||
@@ -894,7 +894,7 @@ class DatasetsWrapperHF:
|
||||
streaming: bool = False,
|
||||
num_proc: Optional[int] = None,
|
||||
storage_options: Optional[Dict] = None,
|
||||
trust_remote_code: bool = True,
|
||||
trust_remote_code: bool = False,
|
||||
dataset_info_only: Optional[bool] = False,
|
||||
**config_kwargs,
|
||||
) -> Union[DatasetDict, Dataset, IterableDatasetDict, IterableDataset,
|
||||
|
||||
@@ -13,7 +13,8 @@ class Vllm(InferFramework):
|
||||
model_id_or_dir: str,
|
||||
dtype: str = 'auto',
|
||||
quantization: str = None,
|
||||
tensor_parallel_size: int = 1):
|
||||
tensor_parallel_size: int = 1,
|
||||
trust_remote_code: bool = False):
|
||||
"""
|
||||
Args:
|
||||
dtype: The dtype to use, support `auto`, `float16`, `bfloat16`, `float32`
|
||||
@@ -30,14 +31,11 @@ class Vllm(InferFramework):
|
||||
if not Vllm.check_gpu_compatibility(8) and (dtype
|
||||
in ('bfloat16', 'auto')):
|
||||
dtype = 'float16'
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {self.model_dir}. Please make '
|
||||
'sure that you can trust the external codes.')
|
||||
self.model = LLM(
|
||||
self.model_dir,
|
||||
dtype=dtype,
|
||||
quantization=quantization,
|
||||
trust_remote_code=True,
|
||||
trust_remote_code=trust_remote_code,
|
||||
tensor_parallel_size=tensor_parallel_size)
|
||||
|
||||
def __call__(self, prompts: Union[List[str], List[List[int]]],
|
||||
|
||||
@@ -25,7 +25,8 @@ class VisionChatPipeline(VisualQuestionAnsweringPipeline):
|
||||
preprocessor: Preprocessor = None,
|
||||
config_file: str = None,
|
||||
device: str = 'gpu',
|
||||
auto_collate=True,
|
||||
auto_collate: bool = True,
|
||||
trust_remote_code: bool = False,
|
||||
**kwargs):
|
||||
# super().__init__
|
||||
self.device_name = device
|
||||
@@ -37,14 +38,11 @@ class VisionChatPipeline(VisualQuestionAnsweringPipeline):
|
||||
torch_dtype = kwargs.get('torch_dtype', torch.float16)
|
||||
multimodal_max_length = kwargs.get('multimodal_max_length', 8192)
|
||||
self.device = 'cuda' if device == 'gpu' else device
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model}. Please make '
|
||||
'sure that you can trust the external codes.')
|
||||
self.model = AutoModelForCausalLM.from_pretrained(
|
||||
model,
|
||||
torch_dtype=torch_dtype,
|
||||
multimodal_max_length=multimodal_max_length,
|
||||
trust_remote_code=True).to(self.device)
|
||||
trust_remote_code=trust_remote_code).to(self.device)
|
||||
self.text_tokenizer = self.model.get_text_tokenizer()
|
||||
self.visual_tokenizer = self.model.get_visual_tokenizer()
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from threading import Lock
|
||||
from typing import Any, Callable, Dict, Generator, Iterator, List, Tuple, Union
|
||||
from typing import Any, Callable, Dict, Generator, Iterator, List, Tuple, Union, Optional
|
||||
|
||||
import json
|
||||
import numpy as np
|
||||
@@ -97,16 +97,13 @@ class LLMPipeline(Pipeline, PipelineStreamingOutputMixin):
|
||||
assert base_model is not None, 'Cannot get adapter_cfg.model_id_or_path from configuration.json file.'
|
||||
revision = self.cfg.safe_get('adapter_cfg.model_revision',
|
||||
'master')
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {base_model}. Please make sure that you can '
|
||||
'trust the external codes.')
|
||||
base_model = Model.from_pretrained(
|
||||
base_model,
|
||||
revision,
|
||||
invoked_by=Invoke.PIPELINE,
|
||||
device_map=self.device_map,
|
||||
torch_dtype=self.torch_dtype,
|
||||
trust_remote_code=True)
|
||||
trust_remote_code=self.trust_remote_code)
|
||||
swift_model = Swift.from_pretrained(base_model, model_id=model)
|
||||
return swift_model
|
||||
|
||||
@@ -137,13 +134,10 @@ class LLMPipeline(Pipeline, PipelineStreamingOutputMixin):
|
||||
model) else snapshot_download(model)
|
||||
# TODO: Temporary use of AutoModelForCausalLM
|
||||
# Need to be updated into a universal solution
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model_dir}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
model_dir,
|
||||
device_map=self.device_map,
|
||||
trust_remote_code=True)
|
||||
trust_remote_code=self.trust_remote_code)
|
||||
model.model_dir = model_dir
|
||||
return model
|
||||
else:
|
||||
@@ -173,17 +167,16 @@ class LLMPipeline(Pipeline, PipelineStreamingOutputMixin):
|
||||
format_output: Callable = None,
|
||||
tokenizer: PreTrainedTokenizer = None,
|
||||
llm_framework: str = None,
|
||||
trust_remote_code: Optional[bool] = None,
|
||||
*args,
|
||||
**kwargs):
|
||||
self.device_map = kwargs.pop('device_map', None)
|
||||
self.trust_remote_code = trust_remote_code
|
||||
self.llm_framework = llm_framework
|
||||
|
||||
if os.path.exists(kwargs['model']):
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {kwargs["model"]}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
config = AutoConfig.from_pretrained(
|
||||
kwargs['model'], trust_remote_code=True)
|
||||
kwargs['model'], trust_remote_code=self.trust_remote_code)
|
||||
q_config = config.__dict__.get('quantization_config', None)
|
||||
if q_config:
|
||||
if q_config.get(
|
||||
@@ -432,11 +425,8 @@ class LLMPipeline(Pipeline, PipelineStreamingOutputMixin):
|
||||
model_dir = self.model.model_dir
|
||||
if tokenizer_class is None:
|
||||
tokenizer_class = AutoTokenizer
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model_dir}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
return tokenizer_class.from_pretrained(
|
||||
model_dir, trust_remote_code=True)
|
||||
model_dir, trust_remote_code=self.trust_remote_code)
|
||||
|
||||
@staticmethod
|
||||
def format_messages(messages: Dict[str, List[Dict[str, str]]],
|
||||
|
||||
@@ -251,8 +251,9 @@ class ChatGLM6bV2TextGenerationPipeline(Pipeline):
|
||||
|
||||
def __init__(self,
|
||||
model: Union[Model, str],
|
||||
quantization_bit=None,
|
||||
use_bf16=False,
|
||||
quantization_bit = None,
|
||||
use_bf16 = False,
|
||||
trust_remote_code: Optional[bool] = None,
|
||||
**kwargs):
|
||||
from modelscope import AutoTokenizer
|
||||
device: str = kwargs.get('device', 'gpu')
|
||||
@@ -269,12 +270,9 @@ class ChatGLM6bV2TextGenerationPipeline(Pipeline):
|
||||
if use_bf16:
|
||||
default_torch_dtype = torch.bfloat16
|
||||
torch_dtype = kwargs.get('torch_dtype', default_torch_dtype)
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model_dir}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
model = Model.from_pretrained(
|
||||
model_dir,
|
||||
trust_remote_code=True,
|
||||
trust_remote_code=trust_remote_code,
|
||||
device_map=device_map,
|
||||
torch_dtype=torch_dtype)
|
||||
else:
|
||||
@@ -288,11 +286,8 @@ class ChatGLM6bV2TextGenerationPipeline(Pipeline):
|
||||
|
||||
self.model = model
|
||||
self.model.eval()
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {self.model.model_dir}. Please '
|
||||
'make sure that you can trust the external codes.')
|
||||
self.tokenizer = AutoTokenizer.from_pretrained(
|
||||
self.model.model_dir, trust_remote_code=True)
|
||||
self.model.model_dir, trust_remote_code=trust_remote_code)
|
||||
|
||||
super().__init__(model=model, **kwargs)
|
||||
|
||||
@@ -321,6 +316,7 @@ class QWenChatPipeline(Pipeline):
|
||||
device_map = kwargs.get('device_map', 'auto')
|
||||
use_max_memory = kwargs.get('use_max_memory', False)
|
||||
revision = kwargs.get('model_revision', 'v.1.0.5')
|
||||
trust_remote_code = kwargs.pop('trust_remote_code', None)
|
||||
|
||||
if use_max_memory:
|
||||
max_memory = f'{int(torch.cuda.mem_get_info()[0] / 1024 ** 3) - 2}GB'
|
||||
@@ -334,19 +330,16 @@ class QWenChatPipeline(Pipeline):
|
||||
bf16 = False
|
||||
|
||||
if isinstance(model, str):
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
self.tokenizer = AutoTokenizer.from_pretrained(
|
||||
model, revision=revision, trust_remote_code=True)
|
||||
model, revision=revision, trust_remote_code=trust_remote_code)
|
||||
self.model = AutoModelForCausalLM.from_pretrained(
|
||||
model,
|
||||
device_map=device_map,
|
||||
revision=revision,
|
||||
trust_remote_code=True,
|
||||
trust_remote_code=trust_remote_code,
|
||||
fp16=bf16).eval()
|
||||
self.model.generation_config = GenerationConfig.from_pretrained(
|
||||
model, trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
|
||||
model, trust_remote_code=trust_remote_code) # 可指定不同的生成长度、top_p等相关超参
|
||||
|
||||
super().__init__(model=self.model, **kwargs)
|
||||
# skip pipeline model placement
|
||||
@@ -388,6 +381,7 @@ class QWenTextGenerationPipeline(Pipeline):
|
||||
device_map = kwargs.get('device_map', 'auto')
|
||||
use_max_memory = kwargs.get('use_max_memory', False)
|
||||
revision = kwargs.get('model_revision', 'v.1.0.4')
|
||||
trust_remote_code = kwargs.pop('trust_remote_code', None)
|
||||
|
||||
if use_max_memory:
|
||||
max_memory = f'{int(torch.cuda.mem_get_info()[0] / 1024 ** 3) - 2}GB'
|
||||
@@ -401,17 +395,14 @@ class QWenTextGenerationPipeline(Pipeline):
|
||||
bf16 = False
|
||||
|
||||
if isinstance(model, str):
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
self.model = AutoModelForCausalLM.from_pretrained(
|
||||
model,
|
||||
device_map=device_map,
|
||||
revision=revision,
|
||||
trust_remote_code=True,
|
||||
trust_remote_code=trust_remote_code,
|
||||
bf16=bf16).eval()
|
||||
self.tokenizer = AutoTokenizer.from_pretrained(
|
||||
model, revision=revision, trust_remote_code=True)
|
||||
model, revision=revision, trust_remote_code=trust_remote_code)
|
||||
self.model.generation_config = GenerationConfig.from_pretrained(
|
||||
model)
|
||||
else:
|
||||
|
||||
@@ -909,6 +909,7 @@ class TemplateLoader:
|
||||
ignore_file_pattern = [r'.+\.bin$', r'.+\.safetensors$', r'.+\.gguf$']
|
||||
tokenizer = kwargs.get('tokenizer')
|
||||
config = kwargs.get('config')
|
||||
trust_remote_code = kwargs.pop('trust_remote_code', None)
|
||||
for _info in template_info:
|
||||
if re.fullmatch(_info.template_regex, model_id):
|
||||
if _info.template:
|
||||
@@ -922,8 +923,8 @@ class TemplateLoader:
|
||||
' Please make sure that you can trust the external codes.'
|
||||
)
|
||||
tokenizer = AutoTokenizer.from_pretrained(
|
||||
model_dir, trust_remote_code=True)
|
||||
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
|
||||
model_dir, trust_remote_code=trust_remote_code)
|
||||
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=trust_remote_code)
|
||||
except Exception:
|
||||
pass
|
||||
return TemplateLoader.load_by_template_name(
|
||||
|
||||
@@ -94,10 +94,7 @@ def get_hf_automodel_class(model_dir: str,
|
||||
if not os.path.exists(config_path):
|
||||
return None
|
||||
try:
|
||||
logger.warning(
|
||||
f'Use trust_remote_code=True. Will invoke codes from {model_dir}. Please make sure '
|
||||
'that you can trust the external codes.')
|
||||
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
|
||||
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=False)
|
||||
if task_name is None:
|
||||
automodel_class = get_default_automodel(config)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user