Spaces:
Sleeping
Sleeping
Commit ·
abe8bbe
1
Parent(s): e7168ce
update
Browse files
examples/tutorials/lora_unsloth/requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
datasets
|
|
|
|
| 2 |
|
|
|
|
| 1 |
datasets
|
| 2 |
+
unsloth
|
| 3 |
|
examples/tutorials/lora_unsloth/step_2_train_model.py
CHANGED
|
@@ -5,6 +5,9 @@ import os
|
|
| 5 |
from pathlib import Path
|
| 6 |
import platform
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
if platform.system() in ("Windows", "Darwin"):
|
| 9 |
from project_settings import project_path
|
| 10 |
else:
|
|
@@ -49,20 +52,15 @@ def get_args():
|
|
| 49 |
|
| 50 |
def convert_to_qwen_format(example):
|
| 51 |
"""
|
| 52 |
-
|
| 53 |
:param example: {"conversation_id": 612, "category": "", "conversation": [{"human": "", "assistant": ""}], "dataset": ""}
|
| 54 |
-
:return:
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
for conversation in example["conversation"]:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
result = {"conversations": conversations}
|
| 64 |
-
print(result)
|
| 65 |
-
exit(0)
|
| 66 |
return result
|
| 67 |
|
| 68 |
|
|
@@ -79,34 +77,29 @@ def main():
|
|
| 79 |
full_finetuning=False
|
| 80 |
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
def format_func(example):
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
add_generation_prompt=False, # 训练期间要关闭,如果是推理则设为True
|
| 106 |
-
)
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
return {"text": formatted_texts}
|
| 110 |
|
| 111 |
dataset_dict = load_dataset(
|
| 112 |
path=args.dataset_path,
|
|
@@ -123,15 +116,15 @@ def main():
|
|
| 123 |
|
| 124 |
train_dataset = train_dataset.map(
|
| 125 |
convert_to_qwen_format,
|
| 126 |
-
batched=
|
| 127 |
-
remove_columns=
|
| 128 |
)
|
| 129 |
print(train_dataset)
|
| 130 |
|
| 131 |
train_dataset = train_dataset.map(
|
| 132 |
format_func,
|
| 133 |
-
batched=
|
| 134 |
-
remove_columns=
|
| 135 |
)
|
| 136 |
print(train_dataset)
|
| 137 |
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
import platform
|
| 7 |
|
| 8 |
+
# os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
| 9 |
+
os.environ["UNSLOTH_USE_MODELSCOPE"] = "1"
|
| 10 |
+
|
| 11 |
if platform.system() in ("Windows", "Darwin"):
|
| 12 |
from project_settings import project_path
|
| 13 |
else:
|
|
|
|
| 52 |
|
| 53 |
def convert_to_qwen_format(example):
|
| 54 |
"""
|
|
|
|
| 55 |
:param example: {"conversation_id": 612, "category": "", "conversation": [{"human": "", "assistant": ""}], "dataset": ""}
|
|
|
|
| 56 |
"""
|
| 57 |
+
conversation_ = []
|
| 58 |
for conversation in example["conversation"]:
|
| 59 |
+
conversation_.append([
|
| 60 |
+
{"role": "user", "content": conversation["human"].strip()},
|
| 61 |
+
{"role": "assistant", "content": conversation["assistant"].strip()},
|
| 62 |
+
])
|
| 63 |
+
result = {"conversation": conversation_}
|
|
|
|
|
|
|
|
|
|
| 64 |
return result
|
| 65 |
|
| 66 |
|
|
|
|
| 77 |
full_finetuning=False
|
| 78 |
)
|
| 79 |
|
| 80 |
+
model = FastLanguageModel.get_peft_model(
|
| 81 |
+
model,
|
| 82 |
+
r=32, # Choose any number > 0! Suggested 8, 16, 32, 64, 128
|
| 83 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
| 84 |
+
"gate_proj", "up_proj", "down_proj", ],
|
| 85 |
+
lora_alpha=32, # Best to choose alpha = rank or rank*2
|
| 86 |
+
lora_dropout=0, # Supports any, but = 0 is optimized
|
| 87 |
+
bias="none", # Supports any, but = "none" is optimized
|
| 88 |
+
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
|
| 89 |
+
use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
|
| 90 |
+
random_state=3407,
|
| 91 |
+
use_rslora=False, # rank stabilized LoRA
|
| 92 |
+
loftq_config=None, # LoftQ
|
| 93 |
+
)
|
| 94 |
+
print(model)
|
| 95 |
|
| 96 |
def format_func(example):
|
| 97 |
+
formated_text = tokenizer.apply_chat_template(
|
| 98 |
+
example["conversation"],
|
| 99 |
+
tokenize=False, # 训练时部分词,true返回的是张量
|
| 100 |
+
add_generation_prompt=False, # 训练期间要关闭,如果是推理则设为True
|
| 101 |
+
)
|
| 102 |
+
return {"text": formated_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
dataset_dict = load_dataset(
|
| 105 |
path=args.dataset_path,
|
|
|
|
| 116 |
|
| 117 |
train_dataset = train_dataset.map(
|
| 118 |
convert_to_qwen_format,
|
| 119 |
+
batched=False,
|
| 120 |
+
remove_columns=["conversation_id", "category", "dataset"]
|
| 121 |
)
|
| 122 |
print(train_dataset)
|
| 123 |
|
| 124 |
train_dataset = train_dataset.map(
|
| 125 |
format_func,
|
| 126 |
+
batched=False,
|
| 127 |
+
remove_columns=["conversation_id", "category", "dataset"]
|
| 128 |
)
|
| 129 |
print(train_dataset)
|
| 130 |
|