[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
1bf2af5165
commit
727da805d9
42 changed files with 2394 additions and 2394 deletions
|
|
@ -78,17 +78,17 @@ def formatting_prompts_func(examples):
|
|||
}
|
||||
|
||||
|
||||
def load_and_compute_8bit_ppl(result_queue, load_in_4bit = False, load_in_8bit = False):
|
||||
def load_and_compute_8bit_ppl(result_queue, load_in_4bit=False, load_in_8bit=False):
|
||||
"""Load model and compute perplexity in subprocess"""
|
||||
from unsloth import FastLanguageModel
|
||||
from tests.utils.perplexity_eval import ppl_model
|
||||
|
||||
# Load model
|
||||
merged_model, merged_tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length = 2048,
|
||||
load_in_4bit = load_in_4bit,
|
||||
load_in_8bit = load_in_8bit,
|
||||
model_name="./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length=2048,
|
||||
load_in_4bit=load_in_4bit,
|
||||
load_in_8bit=load_in_8bit,
|
||||
)
|
||||
# Set up tokenizer
|
||||
# merged_tokenizer = get_chat_template(
|
||||
|
|
@ -98,7 +98,7 @@ def load_and_compute_8bit_ppl(result_queue, load_in_4bit = False, load_in_8bit =
|
|||
|
||||
# Load dataset fresh in subprocess
|
||||
dataset_ppl = load_dataset(
|
||||
"allenai/openassistant-guanaco-reformatted", split = "eval"
|
||||
"allenai/openassistant-guanaco-reformatted", split="eval"
|
||||
)
|
||||
|
||||
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
||||
|
|
@ -146,7 +146,7 @@ def load_and_compute_8bit_ppl(result_queue, load_in_4bit = False, load_in_8bit =
|
|||
"text": texts,
|
||||
}
|
||||
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched = True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched=True)
|
||||
|
||||
# Compute perplexity using the passed dataset
|
||||
ppl_value = ppl_model(merged_model, merged_tokenizer, dataset_ppl)
|
||||
|
|
@ -172,7 +172,7 @@ def load_and_compute_8bit_ppl(result_queue, load_in_4bit = False, load_in_8bit =
|
|||
|
||||
# Main execution code should be wrapped in this guard
|
||||
if __name__ == "__main__":
|
||||
mp.set_start_method("spawn", force = True)
|
||||
mp.set_start_method("spawn", force=True)
|
||||
|
||||
if torch.cuda.is_bf16_supported():
|
||||
compute_dtype = torch.bfloat16
|
||||
|
|
@ -182,31 +182,31 @@ if __name__ == "__main__":
|
|||
attn_implementation = "sdpa"
|
||||
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "unsloth/Qwen2.5-7B-Instruct",
|
||||
max_seq_length = 2048,
|
||||
dtype = compute_dtype,
|
||||
load_in_4bit = True,
|
||||
load_in_8bit = False,
|
||||
full_finetuning = False,
|
||||
attn_implementation = attn_implementation,
|
||||
model_name="unsloth/Qwen2.5-7B-Instruct",
|
||||
max_seq_length=2048,
|
||||
dtype=compute_dtype,
|
||||
load_in_4bit=True,
|
||||
load_in_8bit=False,
|
||||
full_finetuning=False,
|
||||
attn_implementation=attn_implementation,
|
||||
)
|
||||
|
||||
dataset_train = load_dataset(
|
||||
"allenai/openassistant-guanaco-reformatted", split = "train"
|
||||
"allenai/openassistant-guanaco-reformatted", split="train"
|
||||
)
|
||||
dataset_ppl = load_dataset(
|
||||
"allenai/openassistant-guanaco-reformatted", split = "eval"
|
||||
"allenai/openassistant-guanaco-reformatted", split="eval"
|
||||
)
|
||||
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched = True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched = True)
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched=True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched=True)
|
||||
|
||||
add_to_comparison("Base model 4 bits", ppl_model(model, tokenizer, dataset_ppl))
|
||||
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
r = 16,
|
||||
target_modules = [
|
||||
r=16,
|
||||
target_modules=[
|
||||
"k_proj",
|
||||
"q_proj",
|
||||
"v_proj",
|
||||
|
|
@ -215,40 +215,40 @@ if __name__ == "__main__":
|
|||
"down_proj",
|
||||
"up_proj",
|
||||
],
|
||||
lora_alpha = 16,
|
||||
lora_dropout = 0,
|
||||
bias = "none",
|
||||
use_gradient_checkpointing = "unsloth",
|
||||
random_state = 3407,
|
||||
use_rslora = False,
|
||||
loftq_config = None,
|
||||
lora_alpha=16,
|
||||
lora_dropout=0,
|
||||
bias="none",
|
||||
use_gradient_checkpointing="unsloth",
|
||||
random_state=3407,
|
||||
use_rslora=False,
|
||||
loftq_config=None,
|
||||
)
|
||||
|
||||
from unsloth import is_bfloat16_supported
|
||||
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
train_dataset = dataset_train,
|
||||
dataset_text_field = "text",
|
||||
max_seq_length = 2048,
|
||||
data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
|
||||
dataset_num_proc = 2,
|
||||
packing = False,
|
||||
args = TrainingArguments(
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
warmup_ratio = 0.1,
|
||||
max_steps = 200,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bfloat16_supported(),
|
||||
bf16 = is_bfloat16_supported(),
|
||||
logging_steps = 50,
|
||||
optim = "adamw_8bit",
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "outputs",
|
||||
report_to = "none",
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
train_dataset=dataset_train,
|
||||
dataset_text_field="text",
|
||||
max_seq_length=2048,
|
||||
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
|
||||
dataset_num_proc=2,
|
||||
packing=False,
|
||||
args=TrainingArguments(
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
warmup_ratio=0.1,
|
||||
max_steps=200,
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bfloat16_supported(),
|
||||
bf16=is_bfloat16_supported(),
|
||||
logging_steps=50,
|
||||
optim="adamw_8bit",
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="outputs",
|
||||
report_to="none",
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -260,7 +260,7 @@ if __name__ == "__main__":
|
|||
# saving and merging the model to local disk
|
||||
print("merge and save to local disk")
|
||||
model.save_pretrained_merged(
|
||||
save_directory = "./unsloth_out/merged_qwen_text_model", tokenizer = tokenizer
|
||||
save_directory="./unsloth_out/merged_qwen_text_model", tokenizer=tokenizer
|
||||
)
|
||||
|
||||
# print("cleaning")
|
||||
|
|
@ -272,10 +272,10 @@ if __name__ == "__main__":
|
|||
# load model from local disk and test
|
||||
print("Loading merged model in 4 bit for perplexity test")
|
||||
merged_model, merged_tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length = 2048,
|
||||
load_in_4bit = True,
|
||||
load_in_8bit = False,
|
||||
model_name="./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length=2048,
|
||||
load_in_4bit=True,
|
||||
load_in_8bit=False,
|
||||
)
|
||||
|
||||
add_to_comparison(
|
||||
|
|
@ -284,7 +284,7 @@ if __name__ == "__main__":
|
|||
|
||||
print("Computing 8-bit model perplexity in subprocess...")
|
||||
result_queue = mp.Queue()
|
||||
p = mp.Process(target = load_and_compute_8bit_ppl, args = (result_queue, False, True))
|
||||
p = mp.Process(target=load_and_compute_8bit_ppl, args=(result_queue, False, True))
|
||||
p.start()
|
||||
p.join()
|
||||
|
||||
|
|
@ -293,10 +293,10 @@ if __name__ == "__main__":
|
|||
|
||||
print("Loading merged model in 16 bit for perplexity test")
|
||||
merged_model, merged_tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length = 2048,
|
||||
load_in_4bit = False,
|
||||
load_in_8bit = False,
|
||||
model_name="./unsloth_out/merged_qwen_text_model",
|
||||
max_seq_length=2048,
|
||||
load_in_4bit=False,
|
||||
load_in_8bit=False,
|
||||
)
|
||||
|
||||
add_to_comparison(
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def formatting_prompts_func(examples):
|
|||
convos = examples["messages"]
|
||||
texts = [
|
||||
tokenizer.apply_chat_template(
|
||||
convo, tokenize = False, add_generation_prompt = False
|
||||
convo, tokenize=False, add_generation_prompt=False
|
||||
)
|
||||
for convo in convos
|
||||
]
|
||||
|
|
@ -52,34 +52,34 @@ else:
|
|||
attn_implementation = "sdpa"
|
||||
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "unsloth/Llama-3.2-1B-Instruct",
|
||||
max_seq_length = 2048,
|
||||
dtype = compute_dtype,
|
||||
load_in_4bit = True,
|
||||
load_in_8bit = False,
|
||||
full_finetuning = False,
|
||||
attn_implementation = attn_implementation,
|
||||
model_name="unsloth/Llama-3.2-1B-Instruct",
|
||||
max_seq_length=2048,
|
||||
dtype=compute_dtype,
|
||||
load_in_4bit=True,
|
||||
load_in_8bit=False,
|
||||
full_finetuning=False,
|
||||
attn_implementation=attn_implementation,
|
||||
)
|
||||
|
||||
tokenizer = get_chat_template(
|
||||
tokenizer,
|
||||
chat_template = "llama-3.1",
|
||||
chat_template="llama-3.1",
|
||||
)
|
||||
|
||||
from unsloth.chat_templates import standardize_sharegpt
|
||||
|
||||
dataset_train = load_dataset("allenai/openassistant-guanaco-reformatted", split = "train")
|
||||
dataset_ppl = load_dataset("allenai/openassistant-guanaco-reformatted", split = "eval")
|
||||
dataset_train = load_dataset("allenai/openassistant-guanaco-reformatted", split="train")
|
||||
dataset_ppl = load_dataset("allenai/openassistant-guanaco-reformatted", split="eval")
|
||||
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched = True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched = True)
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched=True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched=True)
|
||||
|
||||
add_to_comparison("Base model 4 bits", ppl_model(model, tokenizer, dataset_ppl))
|
||||
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
r = 16,
|
||||
target_modules = [
|
||||
r=16,
|
||||
target_modules=[
|
||||
"k_proj",
|
||||
"q_proj",
|
||||
"v_proj",
|
||||
|
|
@ -88,40 +88,40 @@ model = FastLanguageModel.get_peft_model(
|
|||
"down_proj",
|
||||
"up_proj",
|
||||
],
|
||||
lora_alpha = 16,
|
||||
lora_dropout = 0,
|
||||
bias = "none",
|
||||
use_gradient_checkpointing = "unsloth",
|
||||
random_state = 3407,
|
||||
use_rslora = False,
|
||||
loftq_config = None,
|
||||
lora_alpha=16,
|
||||
lora_dropout=0,
|
||||
bias="none",
|
||||
use_gradient_checkpointing="unsloth",
|
||||
random_state=3407,
|
||||
use_rslora=False,
|
||||
loftq_config=None,
|
||||
)
|
||||
|
||||
from unsloth import is_bfloat16_supported
|
||||
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
train_dataset = dataset_train,
|
||||
dataset_text_field = "text",
|
||||
max_seq_length = 2048,
|
||||
data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
|
||||
dataset_num_proc = 2,
|
||||
packing = False,
|
||||
args = TrainingArguments(
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
warmup_ratio = 0.1,
|
||||
max_steps = 30,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bfloat16_supported(),
|
||||
bf16 = is_bfloat16_supported(),
|
||||
logging_steps = 50,
|
||||
optim = "adamw_8bit",
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "outputs",
|
||||
report_to = "none",
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
train_dataset=dataset_train,
|
||||
dataset_text_field="text",
|
||||
max_seq_length=2048,
|
||||
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
|
||||
dataset_num_proc=2,
|
||||
packing=False,
|
||||
args=TrainingArguments(
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
warmup_ratio=0.1,
|
||||
max_steps=30,
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bfloat16_supported(),
|
||||
bf16=is_bfloat16_supported(),
|
||||
logging_steps=50,
|
||||
optim="adamw_8bit",
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="outputs",
|
||||
report_to="none",
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -129,8 +129,8 @@ from unsloth.chat_templates import train_on_responses_only
|
|||
|
||||
trainer = train_on_responses_only(
|
||||
trainer,
|
||||
instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
instruction_part="<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part="<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
)
|
||||
|
||||
# run training
|
||||
|
|
@ -160,7 +160,7 @@ try:
|
|||
print("\n" + "=" * 80)
|
||||
print("=== UPLOADING MODEL TO HUB ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
model.push_to_hub_merged(repo_name, tokenizer = tokenizer, token = hf_token)
|
||||
model.push_to_hub_merged(repo_name, tokenizer=tokenizer, token=hf_token)
|
||||
success["upload"] = True
|
||||
print("✅ Model uploaded successfully!")
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def formatting_prompts_func(examples):
|
|||
convos = examples["messages"]
|
||||
texts = [
|
||||
tokenizer.apply_chat_template(
|
||||
convo, tokenize = False, add_generation_prompt = False
|
||||
convo, tokenize=False, add_generation_prompt=False
|
||||
)
|
||||
for convo in convos
|
||||
]
|
||||
|
|
@ -52,34 +52,34 @@ else:
|
|||
attn_implementation = "sdpa"
|
||||
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "unsloth/Llama-3.1-8B-Instruct",
|
||||
max_seq_length = 2048,
|
||||
dtype = compute_dtype,
|
||||
load_in_4bit = True,
|
||||
load_in_8bit = False,
|
||||
full_finetuning = False,
|
||||
attn_implementation = attn_implementation,
|
||||
model_name="unsloth/Llama-3.1-8B-Instruct",
|
||||
max_seq_length=2048,
|
||||
dtype=compute_dtype,
|
||||
load_in_4bit=True,
|
||||
load_in_8bit=False,
|
||||
full_finetuning=False,
|
||||
attn_implementation=attn_implementation,
|
||||
)
|
||||
|
||||
tokenizer = get_chat_template(
|
||||
tokenizer,
|
||||
chat_template = "llama-3.1",
|
||||
chat_template="llama-3.1",
|
||||
)
|
||||
|
||||
from unsloth.chat_templates import standardize_sharegpt
|
||||
|
||||
dataset_train = load_dataset("allenai/openassistant-guanaco-reformatted", split = "train")
|
||||
dataset_ppl = load_dataset("allenai/openassistant-guanaco-reformatted", split = "eval")
|
||||
dataset_train = load_dataset("allenai/openassistant-guanaco-reformatted", split="train")
|
||||
dataset_ppl = load_dataset("allenai/openassistant-guanaco-reformatted", split="eval")
|
||||
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched = True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched = True)
|
||||
dataset_train = dataset_train.map(formatting_prompts_func, batched=True)
|
||||
dataset_ppl = dataset_ppl.map(formatting_prompts_func, batched=True)
|
||||
|
||||
add_to_comparison("Base model 4 bits", ppl_model(model, tokenizer, dataset_ppl))
|
||||
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
r = 16,
|
||||
target_modules = [
|
||||
r=16,
|
||||
target_modules=[
|
||||
"k_proj",
|
||||
"q_proj",
|
||||
"v_proj",
|
||||
|
|
@ -88,40 +88,40 @@ model = FastLanguageModel.get_peft_model(
|
|||
"down_proj",
|
||||
"up_proj",
|
||||
],
|
||||
lora_alpha = 16,
|
||||
lora_dropout = 0,
|
||||
bias = "none",
|
||||
use_gradient_checkpointing = "unsloth",
|
||||
random_state = 3407,
|
||||
use_rslora = False,
|
||||
loftq_config = None,
|
||||
lora_alpha=16,
|
||||
lora_dropout=0,
|
||||
bias="none",
|
||||
use_gradient_checkpointing="unsloth",
|
||||
random_state=3407,
|
||||
use_rslora=False,
|
||||
loftq_config=None,
|
||||
)
|
||||
|
||||
from unsloth import is_bfloat16_supported
|
||||
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
train_dataset = dataset_train,
|
||||
dataset_text_field = "text",
|
||||
max_seq_length = 2048,
|
||||
data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
|
||||
dataset_num_proc = 2,
|
||||
packing = False,
|
||||
args = TrainingArguments(
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
warmup_ratio = 0.1,
|
||||
max_steps = 30,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bfloat16_supported(),
|
||||
bf16 = is_bfloat16_supported(),
|
||||
logging_steps = 50,
|
||||
optim = "adamw_8bit",
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "outputs",
|
||||
report_to = "none",
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
train_dataset=dataset_train,
|
||||
dataset_text_field="text",
|
||||
max_seq_length=2048,
|
||||
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
|
||||
dataset_num_proc=2,
|
||||
packing=False,
|
||||
args=TrainingArguments(
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
warmup_ratio=0.1,
|
||||
max_steps=30,
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bfloat16_supported(),
|
||||
bf16=is_bfloat16_supported(),
|
||||
logging_steps=50,
|
||||
optim="adamw_8bit",
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="outputs",
|
||||
report_to="none",
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -129,8 +129,8 @@ from unsloth.chat_templates import train_on_responses_only
|
|||
|
||||
trainer = train_on_responses_only(
|
||||
trainer,
|
||||
instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
instruction_part="<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part="<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
)
|
||||
|
||||
# run training
|
||||
|
|
@ -161,7 +161,7 @@ try:
|
|||
print("\n" + "=" * 80)
|
||||
print("=== UPLOADING MODEL TO HUB ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
model.push_to_hub_merged(repo_name, tokenizer = tokenizer, token = hf_token)
|
||||
model.push_to_hub_merged(repo_name, tokenizer=tokenizer, token=hf_token)
|
||||
success["upload"] = True
|
||||
print("✅ Model uploaded successfully!")
|
||||
except Exception as e:
|
||||
|
|
@ -173,8 +173,8 @@ try:
|
|||
print("\n" + "=" * 80)
|
||||
print("=== VERIFYING REPO CONTENTS ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
fs = HfFileSystem(token = hf_token)
|
||||
file_list = fs.ls(repo_name, detail = True)
|
||||
fs = HfFileSystem(token=hf_token)
|
||||
file_list = fs.ls(repo_name, detail=True)
|
||||
safetensors_found = any(
|
||||
file["name"].endswith("model.safetensors.index.json") for file in file_list
|
||||
)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ max_seq_length = 2048 # Can increase for longer reasoning traces
|
|||
lora_rank = 64 # Larger rank = smarter, but slower
|
||||
|
||||
|
||||
def evaluate_merged_model(result_queue, load_in_4bit = False, load_in_8bit = False):
|
||||
def evaluate_merged_model(result_queue, load_in_4bit=False, load_in_8bit=False):
|
||||
from unsloth import FastLanguageModel
|
||||
from tests.utils.aime_eval import evaluate_model_aime
|
||||
|
||||
|
|
@ -32,12 +32,12 @@ def evaluate_merged_model(result_queue, load_in_4bit = False, load_in_8bit = Fal
|
|||
lora_rank = 64 # Larger rank = smarter, but slower
|
||||
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "./final_merged_model",
|
||||
max_seq_length = max_seq_length,
|
||||
load_in_4bit = True, # False for LoRA 16bit
|
||||
fast_inference = True, # Enable vLLM fast inference
|
||||
max_lora_rank = lora_rank,
|
||||
gpu_memory_utilization = 0.8, # Reduce if out of memory
|
||||
model_name="./final_merged_model",
|
||||
max_seq_length=max_seq_length,
|
||||
load_in_4bit=True, # False for LoRA 16bit
|
||||
fast_inference=True, # Enable vLLM fast inference
|
||||
max_lora_rank=lora_rank,
|
||||
gpu_memory_utilization=0.8, # Reduce if out of memory
|
||||
)
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
|
|
@ -53,14 +53,14 @@ def evaluate_merged_model(result_queue, load_in_4bit = False, load_in_8bit = Fal
|
|||
print(f"{'='*60}")
|
||||
|
||||
evaluate_model_aime(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
model_type = model_type,
|
||||
temperature = 0.3,
|
||||
n_sampling = 8,
|
||||
max_tokens = 32768,
|
||||
top_p = 0.95,
|
||||
seed = 0,
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
model_type=model_type,
|
||||
temperature=0.3,
|
||||
n_sampling=8,
|
||||
max_tokens=32768,
|
||||
top_p=0.95,
|
||||
seed=0,
|
||||
)
|
||||
|
||||
result_queue.put(results)
|
||||
|
|
@ -74,12 +74,12 @@ def evaluate_merged_model(result_queue, load_in_4bit = False, load_in_8bit = Fal
|
|||
# Main execution code should be wrapped in this guard
|
||||
def training_run(result_queue):
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "meta-llama/Llama-3.2-3B-Instruct",
|
||||
max_seq_length = max_seq_length,
|
||||
load_in_4bit = False, # False for LoRA 16bit
|
||||
fast_inference = True, # Enable vLLM fast inference
|
||||
max_lora_rank = lora_rank,
|
||||
gpu_memory_utilization = 0.8, # Reduce if out of memory
|
||||
model_name="meta-llama/Llama-3.2-3B-Instruct",
|
||||
max_seq_length=max_seq_length,
|
||||
load_in_4bit=False, # False for LoRA 16bit
|
||||
fast_inference=True, # Enable vLLM fast inference
|
||||
max_lora_rank=lora_rank,
|
||||
gpu_memory_utilization=0.8, # Reduce if out of memory
|
||||
)
|
||||
|
||||
"""### Helper Functions
|
||||
|
|
@ -166,10 +166,10 @@ def training_run(result_queue):
|
|||
lengths = dataset.map(
|
||||
lambda x: {
|
||||
"tokens": tokenizer.apply_chat_template(
|
||||
x["prompt"], add_generation_prompt = True, tokenize = True
|
||||
x["prompt"], add_generation_prompt=True, tokenize=True
|
||||
)
|
||||
},
|
||||
batched = True,
|
||||
batched=True,
|
||||
).map(lambda x: {"length": len(x["tokens"])})["length"]
|
||||
|
||||
max_length = max(lengths)
|
||||
|
|
@ -181,7 +181,7 @@ def training_run(result_queue):
|
|||
)
|
||||
return max_length, avg_length
|
||||
|
||||
def extract_unsloth_answer(text, start_tag = "<SOLUTION>", end_tag = "</SOLUTION>"):
|
||||
def extract_unsloth_answer(text, start_tag="<SOLUTION>", end_tag="</SOLUTION>"):
|
||||
"""Extract answer from Unsloth SOLUTION tags"""
|
||||
pattern = re.escape(start_tag) + r"(.*?)" + re.escape(end_tag)
|
||||
matches = re.findall(pattern, text, re.DOTALL)
|
||||
|
|
@ -213,10 +213,10 @@ def training_run(result_queue):
|
|||
"""Count tokens in text"""
|
||||
if not text:
|
||||
return 0
|
||||
encoding = tokenizer_instance(text, return_tensors = "pt")
|
||||
encoding = tokenizer_instance(text, return_tensors="pt")
|
||||
return len(encoding["input_ids"][0])
|
||||
|
||||
def check_format_compliance(text, format_type = "unsloth"):
|
||||
def check_format_compliance(text, format_type="unsloth"):
|
||||
"""Check if response follows expected format"""
|
||||
if format_type == "unsloth":
|
||||
reasoning_start = "<start_reasoning>"
|
||||
|
|
@ -419,11 +419,11 @@ def training_run(result_queue):
|
|||
# Save comparison
|
||||
comparison_data = {
|
||||
"summary": all_results,
|
||||
"best_model": max(all_results, key = lambda x: x["exact_match_pct"]),
|
||||
"best_model": max(all_results, key=lambda x: x["exact_match_pct"]),
|
||||
}
|
||||
|
||||
with open("model_comparison_comprehensive.json", "w") as f:
|
||||
json.dump(comparison_data, f, indent = 4)
|
||||
json.dump(comparison_data, f, indent=4)
|
||||
|
||||
print(
|
||||
f"\nBest performing model: {comparison_data['best_model']['model_type']} "
|
||||
|
|
@ -449,10 +449,10 @@ def training_run(result_queue):
|
|||
from datasets import load_dataset
|
||||
|
||||
# Load GSM8K
|
||||
gsm8k_dataset = load_dataset("openai/gsm8k", "main", split = "train")
|
||||
gsm8k_dataset = load_dataset("openai/gsm8k", "main", split="train")
|
||||
|
||||
# Load LIMO (adjust this based on your access method)
|
||||
limo_train = load_dataset("GAIR/LIMO", split = "train")
|
||||
limo_train = load_dataset("GAIR/LIMO", split="train")
|
||||
|
||||
# Prepare datasets
|
||||
gsm8k_train = prepare_gsm8k_dataset(gsm8k_dataset)
|
||||
|
|
@ -466,28 +466,28 @@ def training_run(result_queue):
|
|||
|
||||
# Single temperature evaluation on combined dataset
|
||||
results = evaluate_model_aime(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
model_type = "base",
|
||||
temperature = 0.3,
|
||||
n_sampling = 8,
|
||||
max_tokens = 32768,
|
||||
top_p = 0.95,
|
||||
seed = 0,
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
model_type="base",
|
||||
temperature=0.3,
|
||||
n_sampling=8,
|
||||
max_tokens=32768,
|
||||
top_p=0.95,
|
||||
seed=0,
|
||||
)
|
||||
|
||||
from unsloth.chat_templates import get_chat_template
|
||||
|
||||
tokenizer = get_chat_template(
|
||||
tokenizer,
|
||||
chat_template = "llama-3.1",
|
||||
chat_template="llama-3.1",
|
||||
)
|
||||
|
||||
def formatting_prompts_func(examples):
|
||||
convos = examples["prompt"]
|
||||
texts = [
|
||||
tokenizer.apply_chat_template(
|
||||
convo, tokenize = False, add_generation_prompt = False
|
||||
convo, tokenize=False, add_generation_prompt=False
|
||||
)
|
||||
for convo in convos
|
||||
]
|
||||
|
|
@ -497,7 +497,7 @@ def training_run(result_queue):
|
|||
|
||||
limo_train = limo_train.map(
|
||||
formatting_prompts_func,
|
||||
batched = True,
|
||||
batched=True,
|
||||
)
|
||||
|
||||
from trl import SFTTrainer
|
||||
|
|
@ -510,8 +510,8 @@ def training_run(result_queue):
|
|||
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
r = lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules = [
|
||||
r=lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules=[
|
||||
"q_proj",
|
||||
"k_proj",
|
||||
"v_proj",
|
||||
|
|
@ -520,37 +520,37 @@ def training_run(result_queue):
|
|||
"up_proj",
|
||||
"down_proj",
|
||||
], # Remove QKVO if out of memory
|
||||
lora_alpha = lora_rank,
|
||||
use_gradient_checkpointing = "unsloth", # Enable long context finetuning
|
||||
random_state = 3407,
|
||||
lora_alpha=lora_rank,
|
||||
use_gradient_checkpointing="unsloth", # Enable long context finetuning
|
||||
random_state=3407,
|
||||
)
|
||||
|
||||
if limo_train is not None:
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
train_dataset = limo_train,
|
||||
dataset_text_field = "text",
|
||||
max_seq_length = max_seq_length,
|
||||
data_collator = DataCollatorForSeq2Seq(tokenizer = tokenizer),
|
||||
dataset_num_proc = 2,
|
||||
packing = False, # Can make training 5x faster for short sequences.
|
||||
args = TrainingArguments(
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
warmup_steps = 5,
|
||||
num_train_epochs = 1, # Set this for 1 full training run.
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
train_dataset=limo_train,
|
||||
dataset_text_field="text",
|
||||
max_seq_length=max_seq_length,
|
||||
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer),
|
||||
dataset_num_proc=2,
|
||||
packing=False, # Can make training 5x faster for short sequences.
|
||||
args=TrainingArguments(
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
warmup_steps=5,
|
||||
num_train_epochs=1, # Set this for 1 full training run.
|
||||
# max_steps = 60,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bfloat16_supported(),
|
||||
bf16 = is_bfloat16_supported(),
|
||||
logging_steps = 1,
|
||||
optim = "adamw_8bit",
|
||||
weight_decay = 0.01,
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "outputs",
|
||||
report_to = "none", # Use this for WandB etc
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bfloat16_supported(),
|
||||
bf16=is_bfloat16_supported(),
|
||||
logging_steps=1,
|
||||
optim="adamw_8bit",
|
||||
weight_decay=0.01,
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="outputs",
|
||||
report_to="none", # Use this for WandB etc
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -558,8 +558,8 @@ def training_run(result_queue):
|
|||
|
||||
trainer = train_on_responses_only(
|
||||
trainer,
|
||||
instruction_part = "<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part = "<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
instruction_part="<|start_header_id|>user<|end_header_id|>\n\n",
|
||||
response_part="<|start_header_id|>assistant<|end_header_id|>\n\n",
|
||||
)
|
||||
|
||||
# Train
|
||||
|
|
@ -588,7 +588,7 @@ def training_run(result_queue):
|
|||
PRINT_EVERY_STEPS = 5
|
||||
|
||||
match_numbers = re.compile(
|
||||
solution_start + r".*?([\d\.\,]{1,})", flags = re.MULTILINE | re.DOTALL
|
||||
solution_start + r".*?([\d\.\,]{1,})", flags=re.MULTILINE | re.DOTALL
|
||||
)
|
||||
|
||||
def check_numbers(prompts, completions, answer, **kwargs):
|
||||
|
|
@ -642,37 +642,37 @@ def training_run(result_queue):
|
|||
from trl import GRPOConfig, GRPOTrainer
|
||||
|
||||
training_args = GRPOConfig(
|
||||
learning_rate = 5e-6,
|
||||
weight_decay = 0.1,
|
||||
warmup_ratio = 0.1,
|
||||
lr_scheduler_type = "cosine",
|
||||
optim = "adamw_torch_fused",
|
||||
logging_steps = 1,
|
||||
per_device_train_batch_size = 1,
|
||||
gradient_accumulation_steps = 4, # Increase to 4 for smoother training
|
||||
num_generations = 8, # Decrease if out of memory
|
||||
max_prompt_length = max_prompt_length,
|
||||
max_completion_length = max_seq_length - max_prompt_length,
|
||||
learning_rate=5e-6,
|
||||
weight_decay=0.1,
|
||||
warmup_ratio=0.1,
|
||||
lr_scheduler_type="cosine",
|
||||
optim="adamw_torch_fused",
|
||||
logging_steps=1,
|
||||
per_device_train_batch_size=1,
|
||||
gradient_accumulation_steps=4, # Increase to 4 for smoother training
|
||||
num_generations=8, # Decrease if out of memory
|
||||
max_prompt_length=max_prompt_length,
|
||||
max_completion_length=max_seq_length - max_prompt_length,
|
||||
# num_train_epochs = 1, # Set to 1 for a full training run
|
||||
# max_steps = 250,
|
||||
max_steps = 1000,
|
||||
save_steps = 250,
|
||||
max_grad_norm = 0.1,
|
||||
report_to = "none", # Can use Weights & Biases
|
||||
output_dir = "outputs",
|
||||
max_steps=1000,
|
||||
save_steps=250,
|
||||
max_grad_norm=0.1,
|
||||
report_to="none", # Can use Weights & Biases
|
||||
output_dir="outputs",
|
||||
)
|
||||
|
||||
trainer = GRPOTrainer(
|
||||
model = model,
|
||||
processing_class = tokenizer,
|
||||
reward_funcs = [
|
||||
model=model,
|
||||
processing_class=tokenizer,
|
||||
reward_funcs=[
|
||||
match_format_exactly,
|
||||
match_format_approximately,
|
||||
check_answer_correctness,
|
||||
check_numbers,
|
||||
],
|
||||
args = training_args,
|
||||
train_dataset = gsm8k_train,
|
||||
args=training_args,
|
||||
train_dataset=gsm8k_train,
|
||||
)
|
||||
|
||||
# Train
|
||||
|
|
@ -696,14 +696,14 @@ def training_run(result_queue):
|
|||
print(f"{'='*60}")
|
||||
|
||||
grpo_results = evaluate_model_aime(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
model_type = "grpo",
|
||||
temperature = 0.3,
|
||||
n_sampling = 8,
|
||||
max_tokens = 32768,
|
||||
top_p = 0.95,
|
||||
seed = 0,
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
model_type="grpo",
|
||||
temperature=0.3,
|
||||
n_sampling=8,
|
||||
max_tokens=32768,
|
||||
top_p=0.95,
|
||||
seed=0,
|
||||
)
|
||||
|
||||
all_results.append(grpo_results)
|
||||
|
|
@ -716,7 +716,7 @@ def training_run(result_queue):
|
|||
# Save as merged model
|
||||
try:
|
||||
model.save_pretrained_merged(
|
||||
"final_merged_model", tokenizer, save_method = "merged_16bit"
|
||||
"final_merged_model", tokenizer, save_method="merged_16bit"
|
||||
)
|
||||
print("✅ Merged model saved to: final_merged_model/")
|
||||
except Exception as e:
|
||||
|
|
@ -774,12 +774,12 @@ def training_run(result_queue):
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mp.set_start_method("spawn", force = True)
|
||||
mp.set_start_method("spawn", force=True)
|
||||
result_queue = mp.Queue()
|
||||
all_results = []
|
||||
|
||||
# run main finetuning and grpo loop
|
||||
p = mp.Process(target = training_run, args = (result_queue,))
|
||||
p = mp.Process(target=training_run, args=(result_queue,))
|
||||
p.start()
|
||||
p.join()
|
||||
|
||||
|
|
@ -787,7 +787,7 @@ if __name__ == "__main__":
|
|||
all_results = results
|
||||
|
||||
# evaluate merged model loaded 16bits
|
||||
p = mp.Process(target = evaluate_merged_model, args = (result_queue, False, False))
|
||||
p = mp.Process(target=evaluate_merged_model, args=(result_queue, False, False))
|
||||
p.start()
|
||||
p.join()
|
||||
|
||||
|
|
@ -796,7 +796,7 @@ if __name__ == "__main__":
|
|||
safe_remove_directory("./unsloth_compiled_cache")
|
||||
|
||||
# Merged model load 8 bits model AIME eval
|
||||
p = mp.Process(target = evaluate_merged_model, args = (result_queue, False, True))
|
||||
p = mp.Process(target=evaluate_merged_model, args=(result_queue, False, True))
|
||||
p.start()
|
||||
p.join()
|
||||
|
||||
|
|
@ -806,7 +806,7 @@ if __name__ == "__main__":
|
|||
safe_remove_directory("./unsloth_compiled_cache")
|
||||
|
||||
# Merged model load 4 bits model AIME eval
|
||||
p = mp.Process(target = evaluate_merged_model, args = (result_queue, True, False))
|
||||
p = mp.Process(target=evaluate_merged_model, args=(result_queue, True, False))
|
||||
p.start()
|
||||
p.join()
|
||||
|
||||
|
|
|
|||
|
|
@ -43,31 +43,31 @@ tokenizer_files = [
|
|||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope = "session", params = model_to_test)
|
||||
@pytest.fixture(scope="session", params=model_to_test)
|
||||
def loaded_model_tokenizer(request):
|
||||
model_name = request.param
|
||||
print("Loading model and tokenizer...")
|
||||
|
||||
model, tokenizer = FastModel.from_pretrained(
|
||||
model_name, # use small model
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = True,
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=True,
|
||||
)
|
||||
|
||||
# Apply LoRA
|
||||
model = FastModel.get_peft_model(
|
||||
model,
|
||||
r = 16,
|
||||
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"],
|
||||
lora_alpha = 16,
|
||||
use_gradient_checkpointing = "unsloth",
|
||||
r=16,
|
||||
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
|
||||
lora_alpha=16,
|
||||
use_gradient_checkpointing="unsloth",
|
||||
)
|
||||
|
||||
return model, tokenizer
|
||||
|
||||
|
||||
@pytest.fixture(scope = "session", params = torchao_models)
|
||||
@pytest.fixture(scope="session", params=torchao_models)
|
||||
def fp16_model_tokenizer(request):
|
||||
"""Load model in FP16 for TorchAO quantization"""
|
||||
model_name = request.param
|
||||
|
|
@ -75,29 +75,29 @@ def fp16_model_tokenizer(request):
|
|||
|
||||
model, tokenizer = FastModel.from_pretrained(
|
||||
model_name,
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = False, # No BnB quantization
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=False, # No BnB quantization
|
||||
)
|
||||
|
||||
# Apply LoRA
|
||||
model = FastModel.get_peft_model(
|
||||
model,
|
||||
r = 16,
|
||||
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"],
|
||||
lora_alpha = 16,
|
||||
use_gradient_checkpointing = "unsloth",
|
||||
r=16,
|
||||
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
|
||||
lora_alpha=16,
|
||||
use_gradient_checkpointing="unsloth",
|
||||
)
|
||||
|
||||
return model, tokenizer
|
||||
|
||||
|
||||
@pytest.fixture(scope = "session")
|
||||
@pytest.fixture(scope="session")
|
||||
def model(loaded_model_tokenizer):
|
||||
return loaded_model_tokenizer[0]
|
||||
|
||||
|
||||
@pytest.fixture(scope = "session")
|
||||
@pytest.fixture(scope="session")
|
||||
def tokenizer(loaded_model_tokenizer):
|
||||
return loaded_model_tokenizer[1]
|
||||
|
||||
|
|
@ -133,7 +133,7 @@ def test_save_merged_16bit(model, tokenizer, temp_save_dir: str):
|
|||
)
|
||||
|
||||
model.save_pretrained_merged(
|
||||
save_path, tokenizer = tokenizer, save_method = "merged_16bit"
|
||||
save_path, tokenizer=tokenizer, save_method="merged_16bit"
|
||||
)
|
||||
|
||||
# Check model files
|
||||
|
|
@ -172,9 +172,9 @@ def test_save_merged_16bit(model, tokenizer, temp_save_dir: str):
|
|||
# Test loading the model from the saved path
|
||||
loaded_model, loaded_tokenizer = FastLanguageModel.from_pretrained(
|
||||
save_path,
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = True,
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=True,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -186,7 +186,7 @@ def test_save_merged_4bit(model, tokenizer, temp_save_dir: str):
|
|||
)
|
||||
|
||||
model.save_pretrained_merged(
|
||||
save_path, tokenizer = tokenizer, save_method = "merged_4bit_forced"
|
||||
save_path, tokenizer=tokenizer, save_method="merged_4bit_forced"
|
||||
)
|
||||
|
||||
# Check model files
|
||||
|
|
@ -230,15 +230,15 @@ def test_save_merged_4bit(model, tokenizer, temp_save_dir: str):
|
|||
# Test loading the model from the saved path
|
||||
loaded_model, loaded_tokenizer = FastModel.from_pretrained(
|
||||
save_path,
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = True,
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
importlib.util.find_spec("torchao") is None,
|
||||
reason = "require torchao to be installed",
|
||||
reason="require torchao to be installed",
|
||||
)
|
||||
def test_save_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
||||
model, tokenizer = fp16_model_tokenizer
|
||||
|
|
@ -251,9 +251,9 @@ def test_save_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
|||
torchao_config = Int8DynamicActivationInt8WeightConfig()
|
||||
model.save_pretrained_torchao(
|
||||
save_path,
|
||||
tokenizer = tokenizer,
|
||||
torchao_config = torchao_config,
|
||||
push_to_hub = False,
|
||||
tokenizer=tokenizer,
|
||||
torchao_config=torchao_config,
|
||||
push_to_hub=False,
|
||||
)
|
||||
|
||||
weight_files_16bit = [
|
||||
|
|
@ -316,15 +316,15 @@ def test_save_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
|||
with torch.serialization.safe_globals([getattr]):
|
||||
loaded_model, loaded_tokenizer = FastModel.from_pretrained(
|
||||
torchao_save_path,
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = False,
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=False,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
importlib.util.find_spec("torchao") is None,
|
||||
reason = "require torchao to be installed",
|
||||
reason="require torchao to be installed",
|
||||
)
|
||||
def test_save_and_inference_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
||||
model, tokenizer = fp16_model_tokenizer
|
||||
|
|
@ -343,9 +343,9 @@ def test_save_and_inference_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
|||
# Save with TorchAO
|
||||
model.save_pretrained_torchao(
|
||||
save_path,
|
||||
tokenizer = tokenizer,
|
||||
torchao_config = torchao_config,
|
||||
push_to_hub = False,
|
||||
tokenizer=tokenizer,
|
||||
torchao_config=torchao_config,
|
||||
push_to_hub=False,
|
||||
)
|
||||
|
||||
torchao_save_path = save_path + "-torchao"
|
||||
|
|
@ -361,9 +361,9 @@ def test_save_and_inference_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
|||
with torch.serialization.safe_globals([getattr]):
|
||||
loaded_model, loaded_tokenizer = FastModel.from_pretrained(
|
||||
torchao_save_path,
|
||||
max_seq_length = 128,
|
||||
dtype = None,
|
||||
load_in_4bit = False,
|
||||
max_seq_length=128,
|
||||
dtype=None,
|
||||
load_in_4bit=False,
|
||||
)
|
||||
|
||||
FastModel.for_inference(loaded_model) # Enable native 2x faster inference
|
||||
|
|
@ -376,24 +376,24 @@ def test_save_and_inference_torchao(fp16_model_tokenizer, temp_save_dir: str):
|
|||
]
|
||||
inputs = loaded_tokenizer.apply_chat_template(
|
||||
messages,
|
||||
tokenize = True,
|
||||
add_generation_prompt = True, # Must add for generation
|
||||
return_tensors = "pt",
|
||||
tokenize=True,
|
||||
add_generation_prompt=True, # Must add for generation
|
||||
return_tensors="pt",
|
||||
).to("cuda")
|
||||
|
||||
outputs = loaded_model.generate( # ← Use loaded_model, not model
|
||||
input_ids = inputs,
|
||||
max_new_tokens = 64,
|
||||
use_cache = False, # Avoid cache issues
|
||||
temperature = 1.5,
|
||||
min_p = 0.1,
|
||||
do_sample = True,
|
||||
pad_token_id = loaded_tokenizer.pad_token_id or loaded_tokenizer.eos_token_id,
|
||||
input_ids=inputs,
|
||||
max_new_tokens=64,
|
||||
use_cache=False, # Avoid cache issues
|
||||
temperature=1.5,
|
||||
min_p=0.1,
|
||||
do_sample=True,
|
||||
pad_token_id=loaded_tokenizer.pad_token_id or loaded_tokenizer.eos_token_id,
|
||||
)
|
||||
|
||||
# Decode with the LOADED tokenizer
|
||||
generated_text = loaded_tokenizer.decode(outputs[0], skip_special_tokens = True)
|
||||
input_text = loaded_tokenizer.decode(inputs[0], skip_special_tokens = True)
|
||||
generated_text = loaded_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
||||
input_text = loaded_tokenizer.decode(inputs[0], skip_special_tokens=True)
|
||||
response_part = generated_text[len(input_text) :].strip()
|
||||
|
||||
print(f"Input: {input_text}")
|
||||
|
|
|
|||
|
|
@ -26,11 +26,11 @@ print(f"{'='*80}")
|
|||
|
||||
|
||||
model, tokenizer = FastModel.from_pretrained(
|
||||
model_name = "unsloth/csm-1b",
|
||||
max_seq_length = 2048, # Choose any for long context!
|
||||
dtype = None, # Leave as None for auto-detection
|
||||
auto_model = CsmForConditionalGeneration,
|
||||
load_in_4bit = False, # Select True for 4bit - reduces memory usage
|
||||
model_name="unsloth/csm-1b",
|
||||
max_seq_length=2048, # Choose any for long context!
|
||||
dtype=None, # Leave as None for auto-detection
|
||||
auto_model=CsmForConditionalGeneration,
|
||||
load_in_4bit=False, # Select True for 4bit - reduces memory usage
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -39,8 +39,8 @@ base_model_class = model.__class__.__name__
|
|||
|
||||
model = FastModel.get_peft_model(
|
||||
model,
|
||||
r = 32, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules = [
|
||||
r=32, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules=[
|
||||
"q_proj",
|
||||
"k_proj",
|
||||
"v_proj",
|
||||
|
|
@ -49,14 +49,14 @@ model = FastModel.get_peft_model(
|
|||
"up_proj",
|
||||
"down_proj",
|
||||
],
|
||||
lora_alpha = 32,
|
||||
lora_dropout = 0, # Supports any, but = 0 is optimized
|
||||
bias = "none", # Supports any, but = "none" is optimized
|
||||
lora_alpha=32,
|
||||
lora_dropout=0, # Supports any, but = 0 is optimized
|
||||
bias="none", # Supports any, but = "none" is optimized
|
||||
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
|
||||
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
||||
random_state = 3407,
|
||||
use_rslora = False, # We support rank stabilized LoRA
|
||||
loftq_config = None, # And LoftQ
|
||||
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
||||
random_state=3407,
|
||||
use_rslora=False, # We support rank stabilized LoRA
|
||||
loftq_config=None, # And LoftQ
|
||||
)
|
||||
|
||||
print("✅ Model and LoRA adapters loaded successfully!")
|
||||
|
|
@ -110,11 +110,11 @@ print(f"{'='*80}")
|
|||
|
||||
|
||||
model, processor = FastModel.from_pretrained(
|
||||
model_name = "./csm",
|
||||
max_seq_length = 2048, # Choose any for long context!
|
||||
dtype = None, # Leave as None for auto-detection
|
||||
auto_model = CsmForConditionalGeneration,
|
||||
load_in_4bit = False, # Select True for 4bit - reduces memory usage
|
||||
model_name="./csm",
|
||||
max_seq_length=2048, # Choose any for long context!
|
||||
dtype=None, # Leave as None for auto-detection
|
||||
auto_model=CsmForConditionalGeneration,
|
||||
load_in_4bit=False, # Select True for 4bit - reduces memory usage
|
||||
)
|
||||
|
||||
from transformers import AutoProcessor
|
||||
|
|
@ -138,19 +138,19 @@ try:
|
|||
"We just finished fine tuning a text to speech model... and it's pretty good!"
|
||||
)
|
||||
speaker_id = 0
|
||||
inputs = processor(f"[{speaker_id}]{text}", add_special_tokens = True).to("cuda")
|
||||
inputs = processor(f"[{speaker_id}]{text}", add_special_tokens=True).to("cuda")
|
||||
audio_values = model.generate(
|
||||
**inputs,
|
||||
max_new_tokens = 125, # 125 tokens is 10 seconds of audio, for longer speech increase this
|
||||
max_new_tokens=125, # 125 tokens is 10 seconds of audio, for longer speech increase this
|
||||
# play with these parameters to get the best results
|
||||
depth_decoder_temperature = 0.6,
|
||||
depth_decoder_top_k = 0,
|
||||
depth_decoder_top_p = 0.9,
|
||||
temperature = 0.8,
|
||||
top_k = 50,
|
||||
top_p = 1.0,
|
||||
depth_decoder_temperature=0.6,
|
||||
depth_decoder_top_k=0,
|
||||
depth_decoder_top_p=0.9,
|
||||
temperature=0.8,
|
||||
top_k=50,
|
||||
top_p=1.0,
|
||||
#########################################################
|
||||
output_audio = True,
|
||||
output_audio=True,
|
||||
)
|
||||
audio = audio_values[0].to(torch.float32).cpu().numpy()
|
||||
sf.write("example_without_context.wav", audio, 24000)
|
||||
|
|
|
|||
|
|
@ -42,10 +42,10 @@ print(f"{'='*80}")
|
|||
|
||||
max_seq_length = 2048
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "unsloth/Llasa-1B",
|
||||
max_seq_length = max_seq_length,
|
||||
dtype = None, # Select None for auto detection
|
||||
load_in_4bit = False, # Choose True for 4bit which reduces memory
|
||||
model_name="unsloth/Llasa-1B",
|
||||
max_seq_length=max_seq_length,
|
||||
dtype=None, # Select None for auto detection
|
||||
load_in_4bit=False, # Choose True for 4bit which reduces memory
|
||||
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
|
||||
)
|
||||
|
||||
|
|
@ -54,16 +54,16 @@ base_model_class = model.__class__.__name__
|
|||
|
||||
model = FastLanguageModel.get_peft_model(
|
||||
model,
|
||||
r = 128, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules = ["q_proj", "v_proj"],
|
||||
lora_alpha = 128,
|
||||
lora_dropout = 0, # Supports any, but = 0 is optimized
|
||||
bias = "none", # Supports any, but = "none" is optimized
|
||||
r=128, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules=["q_proj", "v_proj"],
|
||||
lora_alpha=128,
|
||||
lora_dropout=0, # Supports any, but = 0 is optimized
|
||||
bias="none", # Supports any, but = "none" is optimized
|
||||
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
|
||||
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
||||
random_state = 3407,
|
||||
use_rslora = False, # We support rank stabilized LoRA
|
||||
loftq_config = None, # And LoftQ
|
||||
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
||||
random_state=3407,
|
||||
use_rslora=False, # We support rank stabilized LoRA
|
||||
loftq_config=None, # And LoftQ
|
||||
)
|
||||
|
||||
print("✅ Model and LoRA adapters loaded successfully!")
|
||||
|
|
@ -117,10 +117,10 @@ print(f"{'='*80}")
|
|||
|
||||
|
||||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||||
model_name = "./lasa",
|
||||
max_seq_length = max_seq_length,
|
||||
dtype = None, # Select None for auto detection
|
||||
load_in_4bit = False, # Choose True for 4bit which reduces memory
|
||||
model_name="./lasa",
|
||||
max_seq_length=max_seq_length,
|
||||
dtype=None, # Select None for auto detection
|
||||
load_in_4bit=False, # Choose True for 4bit which reduces memory
|
||||
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
|
||||
)
|
||||
|
||||
|
|
@ -166,7 +166,7 @@ def extract_speech_ids(speech_tokens_str):
|
|||
|
||||
# TTS start!
|
||||
with torch.inference_mode():
|
||||
with torch.amp.autocast("cuda", dtype = model.dtype):
|
||||
with torch.amp.autocast("cuda", dtype=model.dtype):
|
||||
formatted_text = (
|
||||
f"<|TEXT_UNDERSTANDING_START|>{input_text}<|TEXT_UNDERSTANDING_END|>"
|
||||
)
|
||||
|
|
@ -178,7 +178,7 @@ with torch.inference_mode():
|
|||
]
|
||||
|
||||
input_ids = tokenizer.apply_chat_template(
|
||||
chat, tokenize = True, return_tensors = "pt", continue_final_message = True
|
||||
chat, tokenize=True, return_tensors="pt", continue_final_message=True
|
||||
)
|
||||
input_ids = input_ids.to("cuda")
|
||||
|
||||
|
|
@ -187,16 +187,16 @@ with torch.inference_mode():
|
|||
# Generate the speech autoregressively
|
||||
outputs = model.generate(
|
||||
input_ids,
|
||||
max_length = 2048, # We trained our model with a max length of 2048
|
||||
eos_token_id = speech_end_id,
|
||||
do_sample = True,
|
||||
top_p = 1.2, # Adjusts the diversity of generated content
|
||||
temperature = 1.2, # Controls randomness in output
|
||||
max_length=2048, # We trained our model with a max length of 2048
|
||||
eos_token_id=speech_end_id,
|
||||
do_sample=True,
|
||||
top_p=1.2, # Adjusts the diversity of generated content
|
||||
temperature=1.2, # Controls randomness in output
|
||||
)
|
||||
# Extract the speech tokens
|
||||
generated_ids = outputs[0][input_ids.shape[1] : -1]
|
||||
|
||||
speech_tokens = tokenizer.batch_decode(generated_ids, skip_special_tokens = True)
|
||||
speech_tokens = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
||||
|
||||
# Convert token <|s_23456|> to int 23456
|
||||
speech_tokens = extract_speech_ids(speech_tokens)
|
||||
|
|
|
|||
|
|
@ -28,12 +28,12 @@ print(f"{'='*80}")
|
|||
|
||||
|
||||
model, tokenizer = FastModel.from_pretrained(
|
||||
model_name = "unsloth/whisper-large-v3",
|
||||
dtype = None, # Leave as None for auto detection
|
||||
load_in_4bit = False, # Set to True to do 4bit quantization which reduces memory
|
||||
auto_model = WhisperForConditionalGeneration,
|
||||
whisper_language = "English",
|
||||
whisper_task = "transcribe",
|
||||
model_name="unsloth/whisper-large-v3",
|
||||
dtype=None, # Leave as None for auto detection
|
||||
load_in_4bit=False, # Set to True to do 4bit quantization which reduces memory
|
||||
auto_model=WhisperForConditionalGeneration,
|
||||
whisper_language="English",
|
||||
whisper_task="transcribe",
|
||||
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
|
||||
)
|
||||
|
||||
|
|
@ -46,17 +46,17 @@ model.generation_config.forced_decoder_ids = None
|
|||
|
||||
model = FastModel.get_peft_model(
|
||||
model,
|
||||
r = 64, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules = ["q_proj", "v_proj"],
|
||||
lora_alpha = 64,
|
||||
lora_dropout = 0, # Supports any, but = 0 is optimized
|
||||
bias = "none", # Supports any, but = "none" is optimized
|
||||
r=64, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
target_modules=["q_proj", "v_proj"],
|
||||
lora_alpha=64,
|
||||
lora_dropout=0, # Supports any, but = 0 is optimized
|
||||
bias="none", # Supports any, but = "none" is optimized
|
||||
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
|
||||
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
||||
random_state = 3407,
|
||||
use_rslora = False, # We support rank stabilized LoRA
|
||||
loftq_config = None, # And LoftQ
|
||||
task_type = None, # ** MUST set this for Whisper **
|
||||
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
||||
random_state=3407,
|
||||
use_rslora=False, # We support rank stabilized LoRA
|
||||
loftq_config=None, # And LoftQ
|
||||
task_type=None, # ** MUST set this for Whisper **
|
||||
)
|
||||
|
||||
print("✅ Model and LoRA adapters loaded successfully!")
|
||||
|
|
@ -110,12 +110,12 @@ print(f"{'='*80}")
|
|||
|
||||
|
||||
model, tokenizer = FastModel.from_pretrained(
|
||||
model_name = "./whisper",
|
||||
dtype = None, # Leave as None for auto detection
|
||||
load_in_4bit = False, # Set to True to do 4bit quantization which reduces memory
|
||||
auto_model = WhisperForConditionalGeneration,
|
||||
whisper_language = "English",
|
||||
whisper_task = "transcribe",
|
||||
model_name="./whisper",
|
||||
dtype=None, # Leave as None for auto detection
|
||||
load_in_4bit=False, # Set to True to do 4bit quantization which reduces memory
|
||||
auto_model=WhisperForConditionalGeneration,
|
||||
whisper_language="English",
|
||||
whisper_task="transcribe",
|
||||
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
|
||||
)
|
||||
|
||||
|
|
@ -135,7 +135,7 @@ try:
|
|||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
}
|
||||
response = requests.get(audio_url, headers = headers)
|
||||
response = requests.get(audio_url, headers=headers)
|
||||
response.raise_for_status()
|
||||
with open(audio_file, "wb") as f:
|
||||
f.write(response.content)
|
||||
|
|
@ -156,12 +156,12 @@ model.eval()
|
|||
# Create pipeline without specifying the device
|
||||
whisper = pipeline(
|
||||
"automatic-speech-recognition",
|
||||
model = model,
|
||||
tokenizer = tokenizer.tokenizer,
|
||||
feature_extractor = tokenizer.feature_extractor,
|
||||
processor = tokenizer,
|
||||
return_language = True,
|
||||
torch_dtype = torch.float16, # Remove the device parameter
|
||||
model=model,
|
||||
tokenizer=tokenizer.tokenizer,
|
||||
feature_extractor=tokenizer.feature_extractor,
|
||||
processor=tokenizer,
|
||||
return_language=True,
|
||||
torch_dtype=torch.float16, # Remove the device parameter
|
||||
)
|
||||
# Example usage
|
||||
audio_file = "Speech_12dB_s16.flac"
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ from tests.utils.cleanup_utils import safe_remove_directory
|
|||
## Dataset Preparation"""
|
||||
|
||||
print("\n📊 Loading and preparing dataset...")
|
||||
dataset = load_dataset("lbourdois/OCR-liboaccn-OPUS-MIT-5M-clean", "en", split = "train")
|
||||
dataset = load_dataset("lbourdois/OCR-liboaccn-OPUS-MIT-5M-clean", "en", split="train")
|
||||
# To select the first 2000 examples
|
||||
train_dataset = dataset.select(range(2000))
|
||||
|
||||
|
|
@ -81,11 +81,11 @@ print("🤖 Loading base vision model...")
|
|||
try:
|
||||
model, tokenizer = FastVisionModel.from_pretrained(
|
||||
# model_name = "unsloth/Qwen2-VL-7B-Instruct",
|
||||
model_name = "unsloth/Qwen2-VL-7B-Instruct",
|
||||
max_seq_length = 2048, # Choose any for long context!
|
||||
load_in_4bit = True, # 4 bit quantization to reduce memory
|
||||
load_in_8bit = False, # [NEW!] A bit more accurate, uses 2x memory
|
||||
full_finetuning = False, # [NEW!] We have full finetuning now!
|
||||
model_name="unsloth/Qwen2-VL-7B-Instruct",
|
||||
max_seq_length=2048, # Choose any for long context!
|
||||
load_in_4bit=True, # 4 bit quantization to reduce memory
|
||||
load_in_8bit=False, # [NEW!] A bit more accurate, uses 2x memory
|
||||
full_finetuning=False, # [NEW!] We have full finetuning now!
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load base model: {e}")
|
||||
|
|
@ -96,18 +96,18 @@ print("\n🔧 Setting up LoRA configuration...")
|
|||
try:
|
||||
model = FastVisionModel.get_peft_model(
|
||||
model,
|
||||
finetune_vision_layers = True, # Turn off for just text!
|
||||
finetune_language_layers = True, # Should leave on!
|
||||
finetune_attention_modules = True, # Attention good for GRPO
|
||||
finetune_mlp_modules = True, # SHould leave on always!
|
||||
r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
lora_alpha = 32,
|
||||
lora_dropout = 0, # Supports any, but = 0 is optimized
|
||||
bias = "none", # Supports any, but = "none" is optimized
|
||||
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
||||
random_state = 3407,
|
||||
use_rslora = False, # We support rank stabilized LoRA
|
||||
loftq_config = None, # And LoftQ
|
||||
finetune_vision_layers=True, # Turn off for just text!
|
||||
finetune_language_layers=True, # Should leave on!
|
||||
finetune_attention_modules=True, # Attention good for GRPO
|
||||
finetune_mlp_modules=True, # SHould leave on always!
|
||||
r=16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
lora_alpha=32,
|
||||
lora_dropout=0, # Supports any, but = 0 is optimized
|
||||
bias="none", # Supports any, but = "none" is optimized
|
||||
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
||||
random_state=3407,
|
||||
use_rslora=False, # We support rank stabilized LoRA
|
||||
loftq_config=None, # And LoftQ
|
||||
)
|
||||
print("✅ LoRA configuration applied successfully!")
|
||||
print(f" 🎯 LoRA rank (r): 16")
|
||||
|
|
@ -128,40 +128,40 @@ FastVisionModel.for_training(model) # Enable for training!
|
|||
|
||||
try:
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
data_collator = UnslothVisionDataCollator(model, tokenizer),
|
||||
train_dataset = train_dataset,
|
||||
args = SFTConfig(
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
data_collator=UnslothVisionDataCollator(model, tokenizer),
|
||||
train_dataset=train_dataset,
|
||||
args=SFTConfig(
|
||||
# per_device_train_batch_size = 4,
|
||||
# gradient_accumulation_steps = 8,
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
gradient_checkpointing = True,
|
||||
gradient_checkpointing_kwargs = {
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
gradient_checkpointing=True,
|
||||
gradient_checkpointing_kwargs={
|
||||
"use_reentrant": False
|
||||
}, # use reentrant checkpointing
|
||||
max_grad_norm = 0.3, # max gradient norm based on QLoRA paper
|
||||
warmup_ratio = 0.03,
|
||||
max_grad_norm=0.3, # max gradient norm based on QLoRA paper
|
||||
warmup_ratio=0.03,
|
||||
# num_train_epochs = 2, # Set this instead of max_steps for full training runs
|
||||
max_steps = 10,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bf16_supported(),
|
||||
bf16 = is_bf16_supported(),
|
||||
logging_steps = 5,
|
||||
save_strategy = "epoch",
|
||||
optim = "adamw_torch_fused",
|
||||
weight_decay = 0.01,
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "checkpoints",
|
||||
report_to = "none", # For Weights and Biases
|
||||
max_steps=10,
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bf16_supported(),
|
||||
bf16=is_bf16_supported(),
|
||||
logging_steps=5,
|
||||
save_strategy="epoch",
|
||||
optim="adamw_torch_fused",
|
||||
weight_decay=0.01,
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="checkpoints",
|
||||
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,
|
||||
remove_unused_columns=False,
|
||||
dataset_text_field="",
|
||||
dataset_kwargs={"skip_prepare_dataset": True},
|
||||
dataset_num_proc=4,
|
||||
max_seq_length=2048,
|
||||
),
|
||||
)
|
||||
print("✅ Trainer setup completed!")
|
||||
|
|
@ -221,7 +221,7 @@ try:
|
|||
print("=== UPLOADING MODEL TO HUB ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
print(f"🚀 Uploading to repository: {repo_name}")
|
||||
model.push_to_hub_merged(repo_name, tokenizer = tokenizer, token = hf_token)
|
||||
model.push_to_hub_merged(repo_name, tokenizer=tokenizer, token=hf_token)
|
||||
success["upload"] = True
|
||||
print("✅ Model uploaded successfully!")
|
||||
except Exception as e:
|
||||
|
|
@ -233,8 +233,8 @@ try:
|
|||
print("\n" + "=" * 80)
|
||||
print("=== VERIFYING REPO CONTENTS ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
fs = HfFileSystem(token = hf_token)
|
||||
file_list = fs.ls(repo_name, detail = True)
|
||||
fs = HfFileSystem(token=hf_token)
|
||||
file_list = fs.ls(repo_name, detail=True)
|
||||
safetensors_found = any(
|
||||
file["name"].endswith("model.safetensors.index.json") for file in file_list
|
||||
)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ from tests.utils.cleanup_utils import safe_remove_directory
|
|||
## Dataset Preparation"""
|
||||
|
||||
print("\n📊 Loading and preparing dataset...")
|
||||
dataset = load_dataset("lbourdois/OCR-liboaccn-OPUS-MIT-5M-clean", "en", split = "train")
|
||||
dataset = load_dataset("lbourdois/OCR-liboaccn-OPUS-MIT-5M-clean", "en", split="train")
|
||||
# To select the first 2000 examples
|
||||
train_dataset = dataset.select(range(2000))
|
||||
|
||||
|
|
@ -82,11 +82,11 @@ print("🤖 Loading base vision model...")
|
|||
try:
|
||||
model, tokenizer = FastVisionModel.from_pretrained(
|
||||
# model_name = "unsloth/Qwen2-VL-7B-Instruct",
|
||||
model_name = "unsloth/Qwen2-VL-2B-Instruct",
|
||||
max_seq_length = 2048, # Choose any for long context!
|
||||
load_in_4bit = True, # 4 bit quantization to reduce memory
|
||||
load_in_8bit = False, # [NEW!] A bit more accurate, uses 2x memory
|
||||
full_finetuning = False, # [NEW!] We have full finetuning now!
|
||||
model_name="unsloth/Qwen2-VL-2B-Instruct",
|
||||
max_seq_length=2048, # Choose any for long context!
|
||||
load_in_4bit=True, # 4 bit quantization to reduce memory
|
||||
load_in_8bit=False, # [NEW!] A bit more accurate, uses 2x memory
|
||||
full_finetuning=False, # [NEW!] We have full finetuning now!
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load base model: {e}")
|
||||
|
|
@ -97,18 +97,18 @@ print("\n🔧 Setting up LoRA configuration...")
|
|||
try:
|
||||
model = FastVisionModel.get_peft_model(
|
||||
model,
|
||||
finetune_vision_layers = True, # Turn off for just text!
|
||||
finetune_language_layers = True, # Should leave on!
|
||||
finetune_attention_modules = True, # Attention good for GRPO
|
||||
finetune_mlp_modules = True, # SHould leave on always!
|
||||
r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
lora_alpha = 32,
|
||||
lora_dropout = 0, # Supports any, but = 0 is optimized
|
||||
bias = "none", # Supports any, but = "none" is optimized
|
||||
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
|
||||
random_state = 3407,
|
||||
use_rslora = False, # We support rank stabilized LoRA
|
||||
loftq_config = None, # And LoftQ
|
||||
finetune_vision_layers=True, # Turn off for just text!
|
||||
finetune_language_layers=True, # Should leave on!
|
||||
finetune_attention_modules=True, # Attention good for GRPO
|
||||
finetune_mlp_modules=True, # SHould leave on always!
|
||||
r=16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
|
||||
lora_alpha=32,
|
||||
lora_dropout=0, # Supports any, but = 0 is optimized
|
||||
bias="none", # Supports any, but = "none" is optimized
|
||||
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
||||
random_state=3407,
|
||||
use_rslora=False, # We support rank stabilized LoRA
|
||||
loftq_config=None, # And LoftQ
|
||||
)
|
||||
print("✅ LoRA configuration applied successfully!")
|
||||
print(f" 🎯 LoRA rank (r): 16")
|
||||
|
|
@ -129,40 +129,40 @@ FastVisionModel.for_training(model) # Enable for training!
|
|||
|
||||
try:
|
||||
trainer = SFTTrainer(
|
||||
model = model,
|
||||
tokenizer = tokenizer,
|
||||
data_collator = UnslothVisionDataCollator(model, tokenizer),
|
||||
train_dataset = train_dataset,
|
||||
args = SFTConfig(
|
||||
model=model,
|
||||
tokenizer=tokenizer,
|
||||
data_collator=UnslothVisionDataCollator(model, tokenizer),
|
||||
train_dataset=train_dataset,
|
||||
args=SFTConfig(
|
||||
# per_device_train_batch_size = 4,
|
||||
# gradient_accumulation_steps = 8,
|
||||
per_device_train_batch_size = 2,
|
||||
gradient_accumulation_steps = 4,
|
||||
gradient_checkpointing = True,
|
||||
gradient_checkpointing_kwargs = {
|
||||
per_device_train_batch_size=2,
|
||||
gradient_accumulation_steps=4,
|
||||
gradient_checkpointing=True,
|
||||
gradient_checkpointing_kwargs={
|
||||
"use_reentrant": False
|
||||
}, # use reentrant checkpointing
|
||||
max_grad_norm = 0.3, # max gradient norm based on QLoRA paper
|
||||
warmup_ratio = 0.03,
|
||||
max_grad_norm=0.3, # max gradient norm based on QLoRA paper
|
||||
warmup_ratio=0.03,
|
||||
# num_train_epochs = 2, # Set this instead of max_steps for full training runs
|
||||
max_steps = 10,
|
||||
learning_rate = 2e-4,
|
||||
fp16 = not is_bf16_supported(),
|
||||
bf16 = is_bf16_supported(),
|
||||
logging_steps = 5,
|
||||
save_strategy = "epoch",
|
||||
optim = "adamw_torch_fused",
|
||||
weight_decay = 0.01,
|
||||
lr_scheduler_type = "linear",
|
||||
seed = 3407,
|
||||
output_dir = "checkpoints",
|
||||
report_to = "none", # For Weights and Biases
|
||||
max_steps=10,
|
||||
learning_rate=2e-4,
|
||||
fp16=not is_bf16_supported(),
|
||||
bf16=is_bf16_supported(),
|
||||
logging_steps=5,
|
||||
save_strategy="epoch",
|
||||
optim="adamw_torch_fused",
|
||||
weight_decay=0.01,
|
||||
lr_scheduler_type="linear",
|
||||
seed=3407,
|
||||
output_dir="checkpoints",
|
||||
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,
|
||||
remove_unused_columns=False,
|
||||
dataset_text_field="",
|
||||
dataset_kwargs={"skip_prepare_dataset": True},
|
||||
dataset_num_proc=4,
|
||||
max_seq_length=2048,
|
||||
),
|
||||
)
|
||||
print("✅ Trainer setup completed!")
|
||||
|
|
@ -221,7 +221,7 @@ try:
|
|||
print("=== UPLOADING MODEL TO HUB ===".center(80))
|
||||
print("=" * 80 + "\n")
|
||||
print(f"🚀 Uploading to repository: {repo_name}")
|
||||
model.push_to_hub_merged(repo_name, tokenizer = tokenizer, token = hf_token)
|
||||
model.push_to_hub_merged(repo_name, tokenizer=tokenizer, token=hf_token)
|
||||
success["upload"] = True
|
||||
print("✅ Model uploaded successfully!")
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue