diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py index 1cf3333d88..8ada3d30f4 100644 --- a/studio/backend/core/training/trainer.py +++ b/studio/backend/core/training/trainer.py @@ -111,17 +111,21 @@ class UnslothTrainer: model_name: str, max_seq_length: int = 2048, load_in_4bit: bool = True, - hf_token: Optional[str] = None) -> bool: + hf_token: Optional[str] = None, + is_dataset_multimodal: bool = False) -> bool: """Load model for training (supports both text and vision models)""" try: print("\nClearing GPU memory before training...") clear_gpu_cache() - # Detect if this is a vision model first - self.is_vlm = is_vision_model(model_name) + # Detect if this is a vision model AND dataset is multimodal + # A vision-capable model with a text-only dataset should use FastLanguageModel + self.is_vlm = is_vision_model(model_name) and is_dataset_multimodal self.model_name = model_name - logger.info(f"Model type detected: {'Vision' if self.is_vlm else 'Text'}") + logger.info(f"Model architecture is vision: {is_vision_model(model_name)}") + logger.info(f"Dataset is multimodal: {is_dataset_multimodal}") + logger.info(f"Using VLM path: {self.is_vlm}") # Reset training state for new run self._update_progress( diff --git a/studio/backend/core/training/training.py b/studio/backend/core/training/training.py index 599f0104cd..51d411be78 100644 --- a/studio/backend/core/training/training.py +++ b/studio/backend/core/training/training.py @@ -104,7 +104,8 @@ class TrainingBackend: subset: str = None, train_split: str = "train", eval_split: str = None, - eval_steps: float = 0.01) -> bool: + eval_steps: float = 0.01, + is_dataset_multimodal: bool = False) -> bool: """ Start training. @@ -161,7 +162,8 @@ class TrainingBackend: model_name=model_name, max_seq_length=max_seq_length, load_in_4bit=load_in_4bit if use_lora_actual else False, # Only 4bit for LoRA - hf_token=hf_token if hf_token.strip() else None + hf_token=hf_token if hf_token.strip() else None, + is_dataset_multimodal=is_dataset_multimodal, ) if not success or self.trainer.should_stop: diff --git a/studio/backend/models/training.py b/studio/backend/models/training.py index 63fbbbdd0a..fd04baf3a0 100644 --- a/studio/backend/models/training.py +++ b/studio/backend/models/training.py @@ -65,6 +65,7 @@ class TrainingStartRequest(BaseModel): finetune_language_layers: bool = Field(False, description="Finetune language layers") finetune_attention_modules: bool = Field(False, description="Finetune attention modules") finetune_mlp_modules: bool = Field(False, description="Finetune MLP modules") + is_dataset_multimodal: bool = Field(False, description="Whether the dataset contains multimodal (image) data") # Logging parameters enable_wandb: bool = Field(False, description="Enable Weights & Biases logging") diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py index 2ad5988739..c09b814fa1 100644 --- a/studio/backend/routes/training.py +++ b/studio/backend/routes/training.py @@ -178,6 +178,7 @@ async def start_training( "finetune_language_layers": request.finetune_language_layers, "finetune_attention_modules": request.finetune_attention_modules, "finetune_mlp_modules": request.finetune_mlp_modules, + "is_dataset_multimodal": request.is_dataset_multimodal, "enable_wandb": request.enable_wandb, "wandb_token": request.wandb_token or "", "wandb_project": request.wandb_project or "", diff --git a/studio/backend/utils/datasets/chat_templates.py b/studio/backend/utils/datasets/chat_templates.py index a868109f5d..e3b1bef315 100644 --- a/studio/backend/utils/datasets/chat_templates.py +++ b/studio/backend/utils/datasets/chat_templates.py @@ -60,7 +60,24 @@ def get_tokenizer_chat_template(tokenizer, model_name): print(f"⚠️ Failed to apply Unsloth template '{matched_template}': {e}") print(f" Falling back to tokenizer's default chat template") else: - print(f"📝 Using tokenizer's default chat template (no Unsloth template match)") + # Check if tokenizer actually has a chat_template set + has_chat_template = ( + hasattr(tokenizer, 'chat_template') + and tokenizer.chat_template is not None + ) + if has_chat_template: + print(f"📝 Using tokenizer's own chat template (no Unsloth template match)") + else: + # Base model with no chat template — apply default ChatML + print(f"📝 No chat template found — applying default ChatML template (base model)") + try: + tokenizer = get_chat_template( + tokenizer, + chat_template="chatml", + ) + except Exception as e: + print(f"⚠️ Failed to apply default ChatML template: {e}") + print(f" Falling back to tokenizer as-is") return tokenizer @@ -227,6 +244,16 @@ def apply_chat_template_to_dataset( # ALPACA FORMAT if final_format == "alpaca": + # Set alpaca chat template on tokenizer for saving (if not already set) + # This ensures the template is saved with the model for inference + if not (hasattr(tokenizer, 'chat_template') and tokenizer.chat_template): + try: + from unsloth.chat_templates import get_chat_template + tokenizer = get_chat_template(tokenizer, chat_template="alpaca") + print(f"📝 Set alpaca chat template on tokenizer for model saving") + except Exception as e: + print(f"⚠️ Could not set alpaca template on tokenizer: {e}") + # Use custom template if provided def _format_alpaca_custom(examples): texts = [] diff --git a/studio/frontend/src/features/studio/sections/params-section.tsx b/studio/frontend/src/features/studio/sections/params-section.tsx index 0052fae5b6..141a38286d 100644 --- a/studio/frontend/src/features/studio/sections/params-section.tsx +++ b/studio/frontend/src/features/studio/sections/params-section.tsx @@ -109,7 +109,7 @@ function SliderRow({ export function ParamsSection(): ReactElement { const store = useTrainingConfigStore(); const isLora = store.trainingMethod !== "full"; - const isVision = store.modelType === "vision"; + const showVisionLora = store.isVisionModel && store.isDatasetMultimodal === true; const [loraOpen, setLoraOpen] = useState(false); const [hyperOpen, setHyperOpen] = useState(false); @@ -350,7 +350,7 @@ export function ParamsSection(): ReactElement { /> {/* Vision checkboxes */} - {isVision && ( + {showVisionLora && (
{startError}
)} + {isIncompatible && ( ++ Text model is not compatible with a multimodal dataset. Switch to a vision model or choose a text-only dataset. +
+ )} {/* Save / Clear */}