ZHZisZZ commited on
Commit ·
304c27e
1
Parent(s): 253ee7b
sft init save
Browse files- cua_lite/train/sft.py +123 -0
cua_lite/train/sft.py
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020-2026 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
# /// script
|
| 16 |
+
# dependencies = [
|
| 17 |
+
# "trl",
|
| 18 |
+
# "Pillow>=9.4.0",
|
| 19 |
+
# "peft",
|
| 20 |
+
# "trackio",
|
| 21 |
+
# "kernels",
|
| 22 |
+
# ]
|
| 23 |
+
# ///
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
pip install pillow
|
| 27 |
+
|
| 28 |
+
# Tested on 8x H100 GPUs
|
| 29 |
+
accelerate launch \
|
| 30 |
+
--config_file examples/accelerate_configs/deepspeed_zero3.yaml \
|
| 31 |
+
examples/scripts/sft_vlm.py \
|
| 32 |
+
--dataset_name HuggingFaceH4/llava-instruct-mix-vsft \
|
| 33 |
+
--model_name_or_path llava-hf/llava-1.5-7b-hf \
|
| 34 |
+
--gradient_accumulation_steps 8 \
|
| 35 |
+
--output_dir LLaVA-1.5-7B-SFT \
|
| 36 |
+
--dtype bfloat16
|
| 37 |
+
|
| 38 |
+
For LLaVA-NeXT, use:
|
| 39 |
+
--model_name_or_path llava-hf/llava-v1.6-mistral-7b-hf
|
| 40 |
+
|
| 41 |
+
For meta-llama/Llama-3.2-11B-Vision-Instruct, use:
|
| 42 |
+
--model_name_or_path meta-llama/Llama-3.2-11B-Vision-Instruct
|
| 43 |
+
|
| 44 |
+
accelerate launch \
|
| 45 |
+
--config_file examples/accelerate_configs/deepspeed_zero3.yaml \
|
| 46 |
+
examples/scripts/sft_vlm.py \
|
| 47 |
+
--dataset_name HuggingFaceH4/llava-instruct-mix-vsft \
|
| 48 |
+
--model_name_or_path HuggingFaceTB/SmolVLM-Instruct \
|
| 49 |
+
--per_device_train_batch_size 1 \
|
| 50 |
+
--gradient_accumulation_steps 1 \
|
| 51 |
+
--output_dir SmolVLM-SFT \
|
| 52 |
+
--dtype bfloat16 \
|
| 53 |
+
--use_peft \
|
| 54 |
+
--lora_target_modules down_proj, o_proj, k_proj, q_proj, gate_proj, up_proj, v_proj
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
import os
|
| 58 |
+
|
| 59 |
+
import torch
|
| 60 |
+
from datasets import load_dataset
|
| 61 |
+
from transformers import AutoModelForImageTextToText
|
| 62 |
+
|
| 63 |
+
from trl import (
|
| 64 |
+
ModelConfig,
|
| 65 |
+
ScriptArguments,
|
| 66 |
+
SFTConfig,
|
| 67 |
+
SFTTrainer,
|
| 68 |
+
TrlParser,
|
| 69 |
+
get_kbit_device_map,
|
| 70 |
+
get_peft_config,
|
| 71 |
+
get_quantization_config,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Enable logging in a Hugging Face Space
|
| 76 |
+
os.environ.setdefault("TRACKIO_SPACE_ID", "trl-trackio")
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
parser = TrlParser((ScriptArguments, SFTConfig, ModelConfig))
|
| 80 |
+
script_args, training_args, model_args = parser.parse_args_and_config()
|
| 81 |
+
training_args.max_length = None
|
| 82 |
+
|
| 83 |
+
################
|
| 84 |
+
# Model
|
| 85 |
+
################
|
| 86 |
+
dtype = model_args.dtype if model_args.dtype in ["auto", None] else getattr(torch, model_args.dtype)
|
| 87 |
+
model_kwargs = dict(
|
| 88 |
+
revision=model_args.model_revision,
|
| 89 |
+
attn_implementation=model_args.attn_implementation,
|
| 90 |
+
dtype=dtype,
|
| 91 |
+
)
|
| 92 |
+
quantization_config = get_quantization_config(model_args)
|
| 93 |
+
if quantization_config is not None:
|
| 94 |
+
# Passing None would not be treated the same as omitting the argument, so we include it only when valid.
|
| 95 |
+
model_kwargs["device_map"] = get_kbit_device_map()
|
| 96 |
+
model_kwargs["quantization_config"] = quantization_config
|
| 97 |
+
|
| 98 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 99 |
+
model_args.model_name_or_path, trust_remote_code=model_args.trust_remote_code, **model_kwargs
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
################
|
| 103 |
+
# Dataset
|
| 104 |
+
################
|
| 105 |
+
dataset = load_dataset(script_args.dataset_name, name=script_args.dataset_config)
|
| 106 |
+
|
| 107 |
+
################
|
| 108 |
+
# Training
|
| 109 |
+
################
|
| 110 |
+
trainer = SFTTrainer(
|
| 111 |
+
model=model,
|
| 112 |
+
args=training_args,
|
| 113 |
+
train_dataset=dataset[script_args.dataset_train_split],
|
| 114 |
+
eval_dataset=dataset[script_args.dataset_test_split] if training_args.eval_strategy != "no" else None,
|
| 115 |
+
peft_config=get_peft_config(model_args),
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
trainer.train()
|
| 119 |
+
|
| 120 |
+
# Save and push to hub
|
| 121 |
+
trainer.save_model(training_args.output_dir)
|
| 122 |
+
if training_args.push_to_hub:
|
| 123 |
+
trainer.push_to_hub(dataset_name=script_args.dataset_name)
|