#!/usr/bin/env python3 """ QLoRA fine-tune for a VISION-LANGUAGE base (e.g. Qwopus3.5-9B-Coder), TEXT-ONLY. The base is multimodal, but our data is text and this base's chat template expects plain-string content. Unsloth's vision collator always wraps content into typed-list parts, which that template rejects -- so we DON'T use the vision collator. Instead we load with FastModel (handles the VL arch), freeze the vision tower + LoRA only the language layers, render each chat to a text string ourselves, and train via the standard text-SFT path (SFTTrainer tokenizes the text; the model forwards text-only). Env: TRAIN_MODEL, TRAIN_MAXLEN, TRAIN_RANK, TRAIN_DATA, TRAIN_OUT, TRAIN_EPOCHS, TRAIN_LR """ import os import unsloth # noqa: F401 (import first to patch transformers/trl) from unsloth import FastModel from datasets import load_dataset from trl import SFTTrainer, SFTConfig MODEL = os.environ.get("TRAIN_MODEL", "Jackrong/Qwopus3.5-9B-Coder") MAX_SEQ_LEN = int(os.environ.get("TRAIN_MAXLEN", "2048")) LORA_RANK = int(os.environ.get("TRAIN_RANK", "16")) DATA = os.environ.get("TRAIN_DATA", "train.jsonl") OUT = os.environ.get("TRAIN_OUT", "qlora_out") EPOCHS = float(os.environ.get("TRAIN_EPOCHS", "2")) LR = float(os.environ.get("TRAIN_LR", "2e-4")) model, tokenizer = FastModel.from_pretrained( MODEL, max_seq_length=MAX_SEQ_LEN, load_in_4bit=True, use_gradient_checkpointing="unsloth", ) model = FastModel.get_peft_model( model, r=LORA_RANK, lora_alpha=LORA_RANK, lora_dropout=0.0, bias="none", finetune_vision_layers=False, finetune_language_layers=True, finetune_attention_modules=True, finetune_mlp_modules=True, use_gradient_checkpointing="unsloth", random_state=3407, ) # Render chats to text ourselves (string content -> this template is happy). # SFTTrainer then tokenizes "text" with the underlying *text* tokenizer. text_tok = getattr(tokenizer, "tokenizer", tokenizer) def fmt(ex): return {"text": tokenizer.apply_chat_template( ex["messages"], tokenize=False, add_generation_prompt=False)} ds = load_dataset("json", data_files=DATA, split="train") ds = ds.map(fmt, remove_columns=[c for c in ds.column_names if c != "text"]) trainer = SFTTrainer( model=model, tokenizer=text_tok, train_dataset=ds, args=SFTConfig( dataset_text_field="text", max_seq_length=MAX_SEQ_LEN, per_device_train_batch_size=1, gradient_accumulation_steps=8, warmup_ratio=0.05, num_train_epochs=EPOCHS, learning_rate=LR, logging_steps=5, optim="adamw_8bit", weight_decay=0.01, lr_scheduler_type="linear", seed=3407, output_dir=OUT, report_to="none", ), ) trainer.train() model.save_pretrained(OUT) text_tok.save_pretrained(OUT) print("LoRA adapter saved to", OUT)