e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
146 lines
5.4 KiB
Python
146 lines
5.4 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
from pathlib import Path
|
|
from typing import Literal, Optional, List
|
|
|
|
import yaml
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class DataConfig(BaseModel):
|
|
dataset: Optional[str] = None
|
|
local_dataset: Optional[List[str]] = None
|
|
format_type: Literal["auto", "alpaca", "chatml", "sharegpt"] = "auto"
|
|
|
|
|
|
class TrainingConfig(BaseModel):
|
|
training_type: Literal["lora", "full"] = "lora"
|
|
max_seq_length: int = 2048
|
|
load_in_4bit: bool = True
|
|
output_dir: Path = Path("./outputs")
|
|
num_epochs: int = 3
|
|
learning_rate: float = 2e-4
|
|
batch_size: int = 2
|
|
gradient_accumulation_steps: int = 4
|
|
warmup_steps: int = 5
|
|
max_steps: int = 0
|
|
save_steps: int = 0
|
|
weight_decay: float = 0.01
|
|
random_seed: int = 3407
|
|
packing: bool = False
|
|
train_on_completions: bool = False
|
|
gradient_checkpointing: Literal["unsloth", "true", "none"] = "unsloth"
|
|
|
|
|
|
class LoraConfig(BaseModel):
|
|
lora_r: int = 64
|
|
lora_alpha: int = 16
|
|
lora_dropout: float = 0.0
|
|
target_modules: str = "q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj,down_proj"
|
|
vision_all_linear: bool = False
|
|
use_rslora: bool = False
|
|
use_loftq: bool = False
|
|
finetune_vision_layers: bool = True
|
|
finetune_language_layers: bool = True
|
|
finetune_attention_modules: bool = True
|
|
finetune_mlp_modules: bool = True
|
|
|
|
|
|
class LoggingConfig(BaseModel):
|
|
enable_wandb: bool = False
|
|
wandb_project: str = "unsloth-training"
|
|
wandb_token: Optional[str] = None
|
|
enable_tensorboard: bool = False
|
|
tensorboard_dir: str = "runs"
|
|
hf_token: Optional[str] = None
|
|
|
|
|
|
class Config(BaseModel):
|
|
model: Optional[str] = None
|
|
data: DataConfig = Field(default_factory = DataConfig)
|
|
training: TrainingConfig = Field(default_factory = TrainingConfig)
|
|
lora: LoraConfig = Field(default_factory = LoraConfig)
|
|
logging: LoggingConfig = Field(default_factory = LoggingConfig)
|
|
|
|
def apply_overrides(self, **kwargs):
|
|
"""Apply CLI overrides by matching arg names to config fields."""
|
|
for key, value in kwargs.items():
|
|
if value is None:
|
|
continue
|
|
if hasattr(self, key):
|
|
setattr(self, key, value)
|
|
else:
|
|
for section in (self.data, self.training, self.lora, self.logging):
|
|
if hasattr(section, key):
|
|
setattr(section, key, value)
|
|
break
|
|
|
|
def model_kwargs(self, use_lora: bool, is_vision: bool) -> dict:
|
|
"""Return kwargs for trainer.prepare_model_for_training()."""
|
|
if use_lora and is_vision:
|
|
# Vision models expect a string (e.g. "all-linear"); None uses trainer defaults
|
|
target_modules = "all-linear" if self.lora.vision_all_linear else None
|
|
else:
|
|
parsed = [
|
|
m.strip() for m in str(self.lora.target_modules).split(",") if m and m.strip()
|
|
]
|
|
target_modules = parsed or None
|
|
|
|
return {
|
|
"use_lora": use_lora,
|
|
"finetune_vision_layers": self.lora.finetune_vision_layers,
|
|
"finetune_language_layers": self.lora.finetune_language_layers,
|
|
"finetune_attention_modules": self.lora.finetune_attention_modules,
|
|
"finetune_mlp_modules": self.lora.finetune_mlp_modules,
|
|
"target_modules": target_modules,
|
|
"lora_r": self.lora.lora_r,
|
|
"lora_alpha": self.lora.lora_alpha,
|
|
"lora_dropout": self.lora.lora_dropout,
|
|
"use_gradient_checkpointing": self.training.gradient_checkpointing,
|
|
"use_rslora": self.lora.use_rslora,
|
|
"use_loftq": self.lora.use_loftq,
|
|
}
|
|
|
|
def training_kwargs(self) -> dict:
|
|
"""Return kwargs for trainer.start_training()."""
|
|
return {
|
|
"output_dir": str(self.training.output_dir),
|
|
"num_epochs": self.training.num_epochs,
|
|
"learning_rate": self.training.learning_rate,
|
|
"batch_size": self.training.batch_size,
|
|
"gradient_accumulation_steps": self.training.gradient_accumulation_steps,
|
|
"warmup_steps": self.training.warmup_steps,
|
|
"max_steps": self.training.max_steps,
|
|
"save_steps": self.training.save_steps,
|
|
"weight_decay": self.training.weight_decay,
|
|
"random_seed": self.training.random_seed,
|
|
"packing": self.training.packing,
|
|
"train_on_completions": self.training.train_on_completions,
|
|
"max_seq_length": self.training.max_seq_length,
|
|
"enable_wandb": self.logging.enable_wandb,
|
|
"wandb_project": self.logging.wandb_project,
|
|
"wandb_token": self.logging.wandb_token,
|
|
"enable_tensorboard": self.logging.enable_tensorboard,
|
|
"tensorboard_dir": self.logging.tensorboard_dir,
|
|
}
|
|
|
|
|
|
def load_config(path: Optional[Path]) -> Config:
|
|
"""Load config from YAML/JSON file, or return defaults if no path given."""
|
|
if not path:
|
|
return Config()
|
|
|
|
path = Path(path)
|
|
if not path.exists():
|
|
raise FileNotFoundError(f"Config file not found: {path}")
|
|
|
|
text = path.read_text(encoding = "utf-8")
|
|
if path.suffix.lower() in {".yaml", ".yml"}:
|
|
data = yaml.safe_load(text) or {}
|
|
else:
|
|
import json
|
|
data = json.loads(text or "{}")
|
|
|
|
return Config(**data)
|