Get a model's architecture and configuration. Args: model_path(str): the model path
(model_path: str, trust_remote_code: bool = False)
| 149 | |
| 150 | |
| 151 | def get_model_arch(model_path: str, trust_remote_code: bool = False): |
| 152 | """Get a model's architecture and configuration. |
| 153 | |
| 154 | Args: |
| 155 | model_path(str): the model path |
| 156 | """ |
| 157 | try: |
| 158 | cfg = AutoConfig.from_pretrained(model_path, trust_remote_code=trust_remote_code) |
| 159 | except Exception as e: # noqa |
| 160 | from transformers import PretrainedConfig |
| 161 | cfg = PretrainedConfig.from_pretrained(model_path, trust_remote_code=trust_remote_code) |
| 162 | |
| 163 | _cfg = cfg.to_dict() |
| 164 | if _cfg.get('architectures', None): |
| 165 | arch = _cfg['architectures'][0] |
| 166 | if _cfg.get('auto_map'): |
| 167 | for _, v in _cfg['auto_map'].items(): |
| 168 | if 'InternLMXComposer2ForCausalLM' in v: |
| 169 | arch = 'InternLMXComposer2ForCausalLM' |
| 170 | elif _cfg.get('auto_map', None) and 'AutoModelForCausalLM' in _cfg['auto_map']: |
| 171 | arch = _cfg['auto_map']['AutoModelForCausalLM'].split('.')[-1] |
| 172 | elif _cfg.get('language_config', None) and _cfg['language_config'].get( |
| 173 | 'auto_map', None) and 'AutoModelForCausalLM' in _cfg['language_config']['auto_map']: |
| 174 | arch = _cfg['language_config']['auto_map']['AutoModelForCausalLM'].split('.')[-1] |
| 175 | else: |
| 176 | raise RuntimeError(f'Could not find model architecture from config: {_cfg}') |
| 177 | return arch, cfg |
| 178 | |
| 179 | |
| 180 | def search_nested_config(config, key): |
no test coverage detected