Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from unsloth import FastVisionModel # FastLanguageModel for LLMs
- model, tokenizer = FastVisionModel.from_pretrained(
- "unsloth/Qwen2-VL-7B-Instruct-bnb-4bit",
- load_in_4bit = True, # Use 4bit to reduce memory use. False for 16bit LoRA.
- use_gradient_checkpointing = "unsloth", # True or "unsloth" for long context
- )
- model = FastVisionModel.get_peft_model(
- model,
- finetune_vision_layers = False, # False if not finetuning vision layers
- finetune_language_layers = True, # False if not finetuning language layers
- finetune_attention_modules = True, # False if not finetuning attention layers
- finetune_mlp_modules = True, # False if not finetuning MLP layers
- r = 16, # The larger, the higher the accuracy, but might overfit
- lora_alpha = 16, # Recommended alpha == r at least
- lora_dropout = 0,
- bias = "none",
- random_state = 3407,
- use_rslora = False, # We support rank stabilized LoRA
- loftq_config = None, # And LoftQ
- # target_modules = "all-linear", # Optional now! Can specify a list if needed
- )
- from unsloth import is_bf16_supported
- from unsloth.trainer import UnslothVisionDataCollator
- from trl import SFTTrainer, SFTConfig
- FastVisionModel.for_training(model)
- trainer = SFTTrainer(
- model = model,
- tokenizer = tokenizer,
- data_collator = UnslothVisionDataCollator(model, tokenizer), # Must use!
- train_dataset = finetune_data,
- args = SFTConfig(
- per_device_train_batch_size = 1,
- gradient_accumulation_steps = 3,
- warmup_steps = 5,
- #max_steps = 30,
- num_train_epochs = 1, # Set this instead of max_steps for full training runs
- learning_rate = 2e-4,
- fp16 = not is_bf16_supported(),
- bf16 = is_bf16_supported(),
- logging_steps = 1,
- optim = "adamw_8bit",
- weight_decay = 0.01,
- lr_scheduler_type = "linear",
- seed = 3407,
- output_dir = "outputs",
- report_to = "none", # For Weights and Biases
- # You MUST put the below items for vision finetuning:
- remove_unused_columns = False,
- dataset_text_field = "",
- dataset_kwargs = {"skip_prepare_dataset": True},
- dataset_num_proc = 4,
- max_seq_length = 2048,
- ),
- )
- trainer_stats = trainer.train()
- model.save_pretrained("lora_model") # Local saving
- tokenizer.save_pretrained("lora_model")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement