BugFix: AttributeError: 'InternVLChatConfig' object has no attribute 'llm_config'
Browse filesWhen loading InternVideo2_5_Chat_8B model or InternVL2_5 series models:
It raises AttributeError: 'InternVLChatConfig' object has no attribute 'llm_config'
Please refer: https://huggingface.co/OpenGVLab/InternVideo2_5_Chat_8B/discussions/14
### Simple Solution for InternVL Configuration Issue
*(Tested with transformers v4.52.4)*
#### Required Modifications
1. **Add Initialization** (configuration_internvl_chat.py:49)
```python
self.vision_config = InternVisionConfig(**vision_config)
self.llm_config = None # Initialize llm_config to prevent AttributeError
```
2. **Add Null Check** (configuration_internvl_chat.py:85)
```python
output['llm_config'] = self.llm_config.to_dict() if self.llm_config is not None else {}
```
#### Root Cause Analysis
When executing:
```python
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda().to(torch.bfloat16)
```
The following occurs:
1. The Hugging Face framework downloads and parses `configuration_internvl_chat.py`
2. During config initialization (`transformers/configuration_utils.py:816-822`):
```python
config_dict = self.to_dict()
# Get the default config dict (from a fresh PreTrainedConfig instance)
default_config_dict = PretrainedConfig().to_dict()
# get class specific config dict
class_config_dict = self.__class__().to_dict() if not self.has_no_defaults_at_init else {}
```
3. **Key Issue**:
- `self.llm_config` is `None` during `class_config_dict` generation because `llm_config` is None
- Without explicit initialization, this triggers an `AttributeError` when `.to_dict()` is called
#### Why the Fix Works
1. The initialization ensures `self.llm_config` always exists (even as `None`)
2. The null check prevents method calls on `None` while maintaining expected dictionary structure
---
|
@@ -47,6 +47,8 @@ class InternVLChatConfig(PretrainedConfig):
|
|
| 47 |
logger.info('llm_config is None. Initializing the LlamaConfig config with default values (`LlamaConfig`).')
|
| 48 |
|
| 49 |
self.vision_config = InternVisionConfig(**vision_config)
|
|
|
|
|
|
|
| 50 |
if llm_config.get('architectures')[0] == 'LlamaForCausalLM':
|
| 51 |
self.llm_config = LlamaConfig(**llm_config)
|
| 52 |
elif llm_config.get('architectures')[0] == 'InternLM2ForCausalLM':
|
|
@@ -81,7 +83,7 @@ class InternVLChatConfig(PretrainedConfig):
|
|
| 81 |
"""
|
| 82 |
output = copy.deepcopy(self.__dict__)
|
| 83 |
output['vision_config'] = self.vision_config.to_dict()
|
| 84 |
-
output['llm_config'] = self.llm_config.to_dict()
|
| 85 |
output['model_type'] = self.__class__.model_type
|
| 86 |
output['use_backbone_lora'] = self.use_backbone_lora
|
| 87 |
output['use_llm_lora'] = self.use_llm_lora
|
|
|
|
| 47 |
logger.info('llm_config is None. Initializing the LlamaConfig config with default values (`LlamaConfig`).')
|
| 48 |
|
| 49 |
self.vision_config = InternVisionConfig(**vision_config)
|
| 50 |
+
# Initialize llm_config to prevent AttributeError
|
| 51 |
+
self.llm_config = None
|
| 52 |
if llm_config.get('architectures')[0] == 'LlamaForCausalLM':
|
| 53 |
self.llm_config = LlamaConfig(**llm_config)
|
| 54 |
elif llm_config.get('architectures')[0] == 'InternLM2ForCausalLM':
|
|
|
|
| 83 |
"""
|
| 84 |
output = copy.deepcopy(self.__dict__)
|
| 85 |
output['vision_config'] = self.vision_config.to_dict()
|
| 86 |
+
output['llm_config'] = self.llm_config.to_dict() if self.llm_config is not None else {}
|
| 87 |
output['model_type'] = self.__class__.model_type
|
| 88 |
output['use_backbone_lora'] = self.use_backbone_lora
|
| 89 |
output['use_llm_lora'] = self.use_llm_lora
|