From d88898e4772acc2fb45f72b0183612bc4829eb5a Mon Sep 17 00:00:00 2001 From: tastelikefeet <58414341+tastelikefeet@users.noreply.github.com> Date: Sun, 1 Oct 2023 23:09:09 +0800 Subject: [PATCH 1/4] fix work_dir not set in trainer(#573) * fix work_dir * fix running --- modelscope/trainers/trainer.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modelscope/trainers/trainer.py b/modelscope/trainers/trainer.py index a3707918..25f948bc 100644 --- a/modelscope/trainers/trainer.py +++ b/modelscope/trainers/trainer.py @@ -181,8 +181,20 @@ class EpochBasedTrainer(BaseTrainer): compile_options = {} self.model = compile_model(self.model, **compile_options) - if 'work_dir' in kwargs: + if kwargs.get('work_dir', None) is not None: self.work_dir = kwargs['work_dir'] + if 'train' not in self.cfg: + self.cfg['train'] = ConfigDict() + self.cfg['train']['work_dir'] = self.work_dir + if 'checkpoint' in self.cfg['train']: + if 'period' in self.cfg['train']['checkpoint']: + self.cfg['train']['checkpoint']['period'][ + 'save_dir'] = self.work_dir + if 'best' in self.cfg['train']['checkpoint']: + self.cfg['train']['checkpoint']['best'][ + 'save_dir'] = self.work_dir + if 'logging' in self.cfg['train']: + self.cfg['train']['logging']['out_dir'] = self.work_dir else: self.work_dir = self.cfg.train.get('work_dir', './work_dir') From 43046a719bf676837b05fd7cf80106f41fc49425 Mon Sep 17 00:00:00 2001 From: Zhicheng Zhang Date: Sun, 8 Oct 2023 13:36:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?move=20venv=20import=20from=20file=20level?= =?UTF-8?q?=20to=20class=20level=20to=20avoid=20import=20error=E2=80=A6=20?= =?UTF-8?q?(#575)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move venv import from file level to class level to avoid import error on windows * pass lint check --------- Co-authored-by: Zhicheng Zhang --- modelscope/utils/plugins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modelscope/utils/plugins.py b/modelscope/utils/plugins.py index 3d39514a..b4485830 100644 --- a/modelscope/utils/plugins.py +++ b/modelscope/utils/plugins.py @@ -9,7 +9,6 @@ import os import pkgutil import shutil import sys -import venv from contextlib import contextmanager from fnmatch import fnmatch from pathlib import Path @@ -1144,6 +1143,7 @@ class EnvsManager(object): cfg = read_config(model_dir) self.plugins = cfg.get('plugins', []) self.allow_remote = cfg.get('allow_remote', False) + import venv self.env_builder = venv.EnvBuilder( system_site_packages=True, clear=False, From 57e2647a4a61b39d4fdffbceb854565bbfaba6b2 Mon Sep 17 00:00:00 2001 From: Jintao Date: Tue, 10 Oct 2023 10:43:16 +0800 Subject: [PATCH 3/4] remove configuration.json dependency (#579) --- modelscope/models/base/base_model.py | 18 ++++--- modelscope/utils/automodel_utils.py | 59 +++++++++++++---------- modelscope/utils/hf_util.py | 70 +--------------------------- 3 files changed, 47 insertions(+), 100 deletions(-) diff --git a/modelscope/models/base/base_model.py b/modelscope/models/base/base_model.py index 788d5c43..a63859cc 100644 --- a/modelscope/models/base/base_model.py +++ b/modelscope/models/base/base_model.py @@ -126,7 +126,7 @@ class Model(ABC): ) invoked_by = '%s/%s' % (Invoke.KEY, invoked_by) - ignore_file_pattern = kwargs.get('ignore_file_pattern', None) + ignore_file_pattern = kwargs.pop('ignore_file_pattern', None) local_model_dir = snapshot_download( model_name_or_path, revision, @@ -134,18 +134,19 @@ class Model(ABC): ignore_file_pattern=ignore_file_pattern) logger.info(f'initialize model from {local_model_dir}') + configuration_path = osp.join(local_model_dir, ModelFile.CONFIGURATION) + cfg = None if cfg_dict is not None: cfg = cfg_dict - else: - cfg = Config.from_file( - osp.join(local_model_dir, ModelFile.CONFIGURATION)) - task_name = cfg.task + elif os.path.exists(configuration_path): + cfg = Config.from_file(configuration_path) + task_name = getattr(cfg, 'task', None) if 'task' in kwargs: task_name = kwargs.pop('task') - model_cfg = cfg.model + model_cfg = getattr(cfg, 'model', None) if hasattr(model_cfg, 'model_type') and not hasattr(model_cfg, 'type'): model_cfg.type = model_cfg.model_type - model_type = model_cfg.type + model_type = getattr(model_cfg, 'type', None) if isinstance(device, str) and device.startswith('gpu'): device = 'cuda' + device[3:] use_hf = kwargs.pop('use_hf', None) @@ -162,6 +163,9 @@ class Model(ABC): model = model.to(device) return model # use ms + if cfg is None: + raise FileNotFoundError( + f'`{ModelFile.CONFIGURATION}` file not found.') model_cfg.model_dir = local_model_dir # install and import remote repos before build diff --git a/modelscope/utils/automodel_utils.py b/modelscope/utils/automodel_utils.py index afd83817..1f5de3b6 100644 --- a/modelscope/utils/automodel_utils.py +++ b/modelscope/utils/automodel_utils.py @@ -6,8 +6,11 @@ from modelscope.utils.ast_utils import INDEX_KEY from modelscope.utils.import_utils import LazyImportModule -def can_load_by_ms(model_dir: str, tast_name: str, model_type: str) -> bool: - if ('MODELS', tast_name, +def can_load_by_ms(model_dir: str, task_name: Optional[str], + model_type: Optional[str]) -> bool: + if model_type is None or task_name is None: + return False + if ('MODELS', task_name, model_type) in LazyImportModule.AST_INDEX[INDEX_KEY]: return True ms_wrapper_path = os.path.join(model_dir, 'ms_wrapper.py') @@ -25,11 +28,27 @@ def _can_load_by_hf_automodel(automodel_class: type, config) -> bool: return False -def get_hf_automodel_class(model_dir: str, task_name: str) -> Optional[type]: - from modelscope import (AutoConfig, AutoModel, AutoModelForCausalLM, - AutoModelForSeq2SeqLM, - AutoModelForTokenClassification, - AutoModelForSequenceClassification) +def get_default_automodel(config) -> Optional[type]: + import modelscope.utils.hf_util as hf_util + if not hasattr(config, 'auto_map'): + return None + auto_map = config.auto_map + automodel_list = [k for k in auto_map.keys() if k.startswith('AutoModel')] + if len(automodel_list) == 1: + return getattr(hf_util, automodel_list[0]) + if len(automodel_list) > 1 and len( + set([auto_map[k] for k in automodel_list])) == 1: + return getattr(hf_util, automodel_list[0]) + return None + + +def get_hf_automodel_class(model_dir: str, + task_name: Optional[str]) -> Optional[type]: + from modelscope.utils.hf_util import (AutoConfig, AutoModel, + AutoModelForCausalLM, + AutoModelForSeq2SeqLM, + AutoModelForTokenClassification, + AutoModelForSequenceClassification) automodel_mapping = { Tasks.backbone: AutoModel, Tasks.chat: AutoModelForCausalLM, @@ -37,19 +56,18 @@ def get_hf_automodel_class(model_dir: str, task_name: str) -> Optional[type]: Tasks.text_classification: AutoModelForSequenceClassification, Tasks.token_classification: AutoModelForTokenClassification, } - automodel_class = automodel_mapping.get(task_name, None) - if automodel_class is None: - return None config_path = os.path.join(model_dir, 'config.json') if not os.path.exists(config_path): return None try: - try: - config = AutoConfig.from_pretrained( - model_dir, trust_remote_code=True) - except (FileNotFoundError, ValueError): - return None + config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True) + if task_name is None: + automodel_class = get_default_automodel(config) + else: + automodel_class = automodel_mapping.get(task_name, None) + if automodel_class is None: + return None if _can_load_by_hf_automodel(automodel_class, config): return automodel_class if (automodel_class is AutoModelForCausalLM @@ -71,14 +89,5 @@ def try_to_load_hf_model(model_dir: str, task_name: str, model = None if automodel_class is not None: # use hf - device_map = kwargs.get('device_map', None) - torch_dtype = kwargs.get('torch_dtype', None) - config = kwargs.get('config', None) - - model = automodel_class.from_pretrained( - model_dir, - device_map=device_map, - torch_dtype=torch_dtype, - config=config, - trust_remote_code=True) + model = automodel_class.from_pretrained(model_dir, **kwargs) return model diff --git a/modelscope/utils/hf_util.py b/modelscope/utils/hf_util.py index 6ef98ccf..97b4ea3d 100644 --- a/modelscope/utils/hf_util.py +++ b/modelscope/utils/hf_util.py @@ -21,7 +21,7 @@ from transformers.models.auto.tokenization_auto import ( TOKENIZER_MAPPING_NAMES, get_tokenizer_config) from modelscope import snapshot_download -from modelscope.utils.constant import Invoke +from modelscope.utils.constant import DEFAULT_MODEL_REVISION, Invoke try: from transformers import GPTQConfig as GPTQConfigHF @@ -84,69 +84,6 @@ patch_tokenizer_base() patch_model_base() -def check_hf_code(model_dir: str, auto_class: type, - trust_remote_code: bool) -> None: - config_path = os.path.join(model_dir, 'config.json') - if not os.path.exists(config_path): - raise FileNotFoundError(f'{config_path} is not found') - config_dict = PretrainedConfig.get_config_dict(config_path)[0] - auto_class_name = auto_class.__name__ - if auto_class is AutoTokenizerHF: - tokenizer_config = get_tokenizer_config(model_dir) - # load from repo - if trust_remote_code: - has_remote_code = False - if auto_class is AutoTokenizerHF: - auto_map = tokenizer_config.get('auto_map', None) - if auto_map is not None: - module_name = auto_map.get(auto_class_name, None) - if module_name is not None: - module_name = module_name[0] - has_remote_code = True - else: - auto_map = config_dict.get('auto_map', None) - if auto_map is not None: - module_name = auto_map.get(auto_class_name, None) - has_remote_code = module_name is not None - - if has_remote_code: - module_path = os.path.join(model_dir, - module_name.split('.')[0] + '.py') - if not os.path.exists(module_path): - raise FileNotFoundError(f'{module_path} is not found') - return - - # trust_remote_code is False or has_remote_code is False - model_type = config_dict.get('model_type', None) - if model_type is None: - raise ValueError(f'`model_type` key is not found in {config_path}.') - - trust_remote_code_info = '.' - if not trust_remote_code: - trust_remote_code_info = ', You can try passing `trust_remote_code=True`.' - if auto_class is AutoConfigHF: - if model_type not in CONFIG_MAPPING: - raise ValueError( - f'{model_type} not found in HF `CONFIG_MAPPING`{trust_remote_code_info}' - ) - elif auto_class is AutoTokenizerHF: - tokenizer_class = tokenizer_config.get('tokenizer_class') - if tokenizer_class is not None: - return - if model_type not in TOKENIZER_MAPPING_NAMES: - raise ValueError( - f'{model_type} not found in HF `TOKENIZER_MAPPING_NAMES`{trust_remote_code_info}' - ) - else: - mapping_names = [ - m.model_type for m in auto_class._model_mapping.keys() - ] - if model_type not in mapping_names: - raise ValueError( - f'{model_type} not found in HF `auto_class._model_mapping`{trust_remote_code_info}' - ) - - def get_wrapped_class(module_class, ignore_file_pattern=[], **kwargs): """Get a custom wrapper class for auto classes to download the models from the ModelScope hub Args: @@ -166,7 +103,7 @@ def get_wrapped_class(module_class, ignore_file_pattern=[], **kwargs): ignore_file_pattern = kwargs.pop('ignore_file_pattern', default_ignore_file_pattern) if not os.path.exists(pretrained_model_name_or_path): - revision = kwargs.pop('revision', None) + revision = kwargs.pop('revision', DEFAULT_MODEL_REVISION) model_dir = snapshot_download( pretrained_model_name_or_path, revision=revision, @@ -175,9 +112,6 @@ def get_wrapped_class(module_class, ignore_file_pattern=[], **kwargs): else: model_dir = pretrained_model_name_or_path - if module_class is not GenerationConfigHF: - trust_remote_code = kwargs.get('trust_remote_code', False) - check_hf_code(model_dir, module_class, trust_remote_code) module_obj = module_class.from_pretrained(model_dir, *model_args, **kwargs) From 362872d6bb7e8beb8c02d8a27ec386a5fb023973 Mon Sep 17 00:00:00 2001 From: tastelikefeet <58414341+tastelikefeet@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:43:30 +0800 Subject: [PATCH 4/4] fix onnx backend (#580) --- modelscope/exporters/torch_model_exporter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modelscope/exporters/torch_model_exporter.py b/modelscope/exporters/torch_model_exporter.py index 473b9705..1dd1b459 100644 --- a/modelscope/exporters/torch_model_exporter.py +++ b/modelscope/exporters/torch_model_exporter.py @@ -233,7 +233,9 @@ class TorchModelExporter(Exporter): return onnx_model = onnx.load(output) onnx.checker.check_model(onnx_model) - ort_session = ort.InferenceSession(output) + ort_session = ort.InferenceSession( + output, + providers=['CUDAExecutionProvider', 'CPUExecutionProvider']) with torch.no_grad(): model.eval() outputs_origin = model.forward(