build trainer given a trainer name Args: name (str, optional): Trainer name, if None, default trainer will be used. default_args (dict, optional): Default initialization arguments. If ``trust_remote_code`` key is set to True in default_args,
(name: str = Trainers.default, default_args: dict = None)
| 14 | |
| 15 | |
| 16 | def build_trainer(name: str = Trainers.default, default_args: dict = None): |
| 17 | """ build trainer given a trainer name |
| 18 | |
| 19 | Args: |
| 20 | name (str, optional): Trainer name, if None, default trainer |
| 21 | will be used. |
| 22 | default_args (dict, optional): Default initialization arguments. |
| 23 | If ``trust_remote_code`` key is set to True in default_args, |
| 24 | remote code and plugins declared in the model configuration |
| 25 | will be allowed to execute. |
| 26 | """ |
| 27 | cfg = dict(type=name) |
| 28 | default_args = default_args or {} |
| 29 | model = default_args.get('model', None) |
| 30 | model_revision = default_args.get('model_revision', DEFAULT_MODEL_REVISION) |
| 31 | model_id = model[0] if isinstance(model, |
| 32 | list) and len(model) > 0 else model |
| 33 | trust_remote_code = default_args.get( |
| 34 | 'trust_remote_code', False) or check_model_from_owner_group(model_id) |
| 35 | |
| 36 | if isinstance(model, str) \ |
| 37 | or (isinstance(model, list) and isinstance(model[0], str)): |
| 38 | if is_official_hub_path(model, revision=model_revision): |
| 39 | # read config file from hub and parse |
| 40 | configuration = read_config( |
| 41 | model, revision=model_revision) if isinstance( |
| 42 | model, str) else read_config( |
| 43 | model[0], revision=model_revision) |
| 44 | model_dir = normalize_model_input(model, model_revision) |
| 45 | if configuration: |
| 46 | plugins = configuration.safe_get('plugins') |
| 47 | allow_remote = configuration.get('allow_remote', False) |
| 48 | if (filter_plugin_in_whitelist(plugins) |
| 49 | or allow_remote) and not trust_remote_code: |
| 50 | raise RuntimeError( |
| 51 | 'Detected plugins or allow_remote field in the model ' |
| 52 | 'configuration file, but trust_remote_code=True was ' |
| 53 | 'not explicitly set.\n' |
| 54 | 'To prevent potential execution of malicious code, ' |
| 55 | 'loading has been refused.\n' |
| 56 | 'If you trust this model repository, please pass ' |
| 57 | 'trust_remote_code=True in default_args to ' |
| 58 | 'build_trainer().') |
| 59 | register_plugins_repo(plugins) |
| 60 | model_dir_str = model_dir if isinstance(model_dir, |
| 61 | str) else model_dir[0] |
| 62 | register_modelhub_repo(model_dir_str, trust_remote_code |
| 63 | and allow_remote) |
| 64 | return build_from_cfg(cfg, TRAINERS, default_args=default_args) |
searching dependent graphs…