|
33 | 33 | from datasets import Dataset, IterableDataset |
34 | 34 | from torch.distributed._tensor import DTensor |
35 | 35 | from torch.utils.data import DataLoader |
36 | | -from transformers import AutoTokenizer, PreTrainedTokenizerBase, TrainerCallback |
| 36 | +from transformers import AutoTokenizer, PreTrainedModel, PreTrainedTokenizerBase, TrainerCallback |
37 | 37 | from transformers.data.data_collator import DataCollatorMixin |
38 | 38 | from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR |
39 | 39 |
|
@@ -826,8 +826,16 @@ def __init__( |
826 | 826 |
|
827 | 827 | self._is_vlm = text_config is not model.config |
828 | 828 | if self._is_vlm: |
829 | | - model.model.requires_grad_(False) |
830 | | - model.model.language_model.requires_grad_(True) |
| 829 | + # Train the text model only. It is located through the text config, since module names differ across |
| 830 | + # architectures (`model.language_model` for Qwen-VL and Gemma 3, `model.text_model` for SmolVLM). |
| 831 | + text_model = next( |
| 832 | + module |
| 833 | + for module in model.modules() |
| 834 | + if isinstance(module, PreTrainedModel) and module is not model and module.config is text_config |
| 835 | + ) |
| 836 | + model.requires_grad_(False) |
| 837 | + text_model.requires_grad_(True) |
| 838 | + model.get_output_embeddings().requires_grad_(True) |
831 | 839 |
|
832 | 840 | patch_chunked_lm_head( |
833 | 841 | model, chunk_size=8192, temperature=self.temperature, output_router_logits=self.aux_loss_enabled |
|
0 commit comments