Spaces:
Paused
Paused
Commit ·
56afdcd
1
Parent(s): 14c64f6
update
Browse files- examples/tutorials/dpo/ultrachat/requirements.txt +1 -1
- examples/tutorials/dpo/ultrachat/step_2_train_sft_model_ddp.py +12 -4
- examples/tutorials/dpo/ultrachat/step_2_train_sft_model_deepspeed.py +14 -35
- examples/tutorials/dpo/ultrachat/{step_2_train_sft_model_stages_0.py → step_2_train_sft_model_single_gpu.py} +14 -5
- examples/tutorials/rlhf/gpt2_sst2/step_5_ppo_rlhf.py +3 -0
examples/tutorials/dpo/ultrachat/requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
datasets
|
| 2 |
transformers==4.50.0
|
| 3 |
trl==0.16.1
|
| 4 |
-
deepspeed==0.
|
|
|
|
| 1 |
datasets
|
| 2 |
transformers==4.50.0
|
| 3 |
trl==0.16.1
|
| 4 |
+
deepspeed==0.15.2
|
examples/tutorials/dpo/ultrachat/step_2_train_sft_model_ddp.py
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
https://huggingface.co/docs/trl/v0.16.1/en/sft_trainer
|
| 5 |
|
| 6 |
双卡 V00 32G 全参微调
|
| 7 |
-
torchrun --nproc_per_node=2
|
| 8 |
|
| 9 |
"""
|
| 10 |
import argparse
|
|
@@ -59,7 +59,7 @@ def get_args():
|
|
| 59 |
),
|
| 60 |
parser.add_argument(
|
| 61 |
"--output_model_dir",
|
| 62 |
-
default=(temp_directory / "trained_models/qwen2_5-0_5B-ultrachat-sft").as_posix(),
|
| 63 |
type=str
|
| 64 |
),
|
| 65 |
parser.add_argument(
|
|
@@ -92,8 +92,16 @@ def format_func(examples, tokenizer):
|
|
| 92 |
def main():
|
| 93 |
args = get_args()
|
| 94 |
|
| 95 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
if tokenizer.pad_token is None:
|
| 98 |
tokenizer.pad_token = tokenizer.eos_token
|
| 99 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
|
|
|
| 4 |
https://huggingface.co/docs/trl/v0.16.1/en/sft_trainer
|
| 5 |
|
| 6 |
双卡 V00 32G 全参微调
|
| 7 |
+
torchrun --nproc_per_node=2 step_2_train_sft_model_ddp.py
|
| 8 |
|
| 9 |
"""
|
| 10 |
import argparse
|
|
|
|
| 59 |
),
|
| 60 |
parser.add_argument(
|
| 61 |
"--output_model_dir",
|
| 62 |
+
default=(temp_directory / "trained_models/qwen2_5-0_5B-ultrachat-sft-ddp").as_posix(),
|
| 63 |
type=str
|
| 64 |
),
|
| 65 |
parser.add_argument(
|
|
|
|
| 92 |
def main():
|
| 93 |
args = get_args()
|
| 94 |
|
| 95 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 96 |
+
args.model_name,
|
| 97 |
+
cache_dir=args.model_cache_dir,
|
| 98 |
+
trust_remote_code=True,
|
| 99 |
+
)
|
| 100 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 101 |
+
args.model_name,
|
| 102 |
+
cache_dir=args.model_cache_dir,
|
| 103 |
+
trust_remote_code=True,
|
| 104 |
+
)
|
| 105 |
if tokenizer.pad_token is None:
|
| 106 |
tokenizer.pad_token = tokenizer.eos_token
|
| 107 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
examples/tutorials/dpo/ultrachat/step_2_train_sft_model_deepspeed.py
CHANGED
|
@@ -99,14 +99,14 @@ def main():
|
|
| 99 |
|
| 100 |
model = AutoModelForCausalLM.from_pretrained(
|
| 101 |
args.model_name,
|
| 102 |
-
|
| 103 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 104 |
low_cpu_mem_usage=True,
|
| 105 |
trust_remote_code=True,
|
| 106 |
)
|
| 107 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 108 |
args.model_name,
|
| 109 |
-
|
| 110 |
trust_remote_code=True,
|
| 111 |
)
|
| 112 |
if tokenizer.pad_token is None:
|
|
@@ -132,49 +132,30 @@ def main():
|
|
| 132 |
|
| 133 |
response_template = "<|im_end|>\n<|im_start|>assistant\n"
|
| 134 |
instruction_template = "<|im_end|>\n<|im_start|>user\n"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# tokenizer=tokenizer,
|
| 139 |
-
# mlm=False,
|
| 140 |
-
# )
|
| 141 |
-
data_collator = DataCollatorForLanguageModeling(
|
| 142 |
tokenizer=tokenizer,
|
| 143 |
mlm=False,
|
| 144 |
)
|
| 145 |
|
| 146 |
sft_config = SFTConfig(
|
| 147 |
output_dir=args.output_model_dir,
|
| 148 |
-
|
|
|
|
| 149 |
num_train_epochs=1,
|
| 150 |
per_device_train_batch_size=1 if debug_mode else 2,
|
| 151 |
gradient_accumulation_steps=1 if debug_mode else 8,
|
| 152 |
-
save_strategy="steps",
|
| 153 |
-
save_steps=100,
|
| 154 |
-
save_total_limit=2,
|
| 155 |
-
logging_steps=100,
|
| 156 |
-
learning_rate=2e-5,
|
| 157 |
warmup_steps=100,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
lr_scheduler_type="cosine",
|
| 159 |
-
|
| 160 |
-
bf16=False,
|
| 161 |
-
gradient_checkpointing=True,
|
| 162 |
-
optim="adamw_torch",
|
| 163 |
report_to="none",
|
| 164 |
-
max_seq_length=1024 if debug_mode else 2048,
|
| 165 |
-
packing=False,
|
| 166 |
-
dataloader_num_workers=args.num_workers if args.num_workers else 2,
|
| 167 |
-
dataloader_pin_memory=False,
|
| 168 |
-
remove_unused_columns=True,
|
| 169 |
-
ddp_find_unused_parameters=False, # 重要
|
| 170 |
-
group_by_length=False,
|
| 171 |
-
no_cuda=False,
|
| 172 |
-
seed=42,
|
| 173 |
-
data_seed=42,
|
| 174 |
-
local_rank=args.local_rank, # 使用local_rank
|
| 175 |
-
ddp_backend="nccl" if torch.cuda.is_available() else "gloo",
|
| 176 |
-
# 关键修改7:添加deepspeed特定配置
|
| 177 |
-
torch_compile=False, # 禁用torch.compile避免冲突
|
| 178 |
)
|
| 179 |
|
| 180 |
trainer = SFTTrainer(
|
|
@@ -184,11 +165,9 @@ def main():
|
|
| 184 |
train_dataset=train_dataset,
|
| 185 |
)
|
| 186 |
|
| 187 |
-
# 开始训练
|
| 188 |
print("开始训练...")
|
| 189 |
trainer.train()
|
| 190 |
|
| 191 |
-
# 保存模型
|
| 192 |
if args.local_rank in (-1, 0):
|
| 193 |
print(f"保存模型到: {args.output_model_dir}")
|
| 194 |
trainer.save_model()
|
|
|
|
| 99 |
|
| 100 |
model = AutoModelForCausalLM.from_pretrained(
|
| 101 |
args.model_name,
|
| 102 |
+
cache_dir=args.model_cache_dir,
|
| 103 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 104 |
low_cpu_mem_usage=True,
|
| 105 |
trust_remote_code=True,
|
| 106 |
)
|
| 107 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 108 |
args.model_name,
|
| 109 |
+
cache_dir=args.model_cache_dir,
|
| 110 |
trust_remote_code=True,
|
| 111 |
)
|
| 112 |
if tokenizer.pad_token is None:
|
|
|
|
| 132 |
|
| 133 |
response_template = "<|im_end|>\n<|im_start|>assistant\n"
|
| 134 |
instruction_template = "<|im_end|>\n<|im_start|>user\n"
|
| 135 |
+
data_collator = DataCollatorForCompletionOnlyLM(
|
| 136 |
+
response_template=response_template,
|
| 137 |
+
instruction_template=instruction_template,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
tokenizer=tokenizer,
|
| 139 |
mlm=False,
|
| 140 |
)
|
| 141 |
|
| 142 |
sft_config = SFTConfig(
|
| 143 |
output_dir=args.output_model_dir,
|
| 144 |
+
dataset_text_field="formated_text",
|
| 145 |
+
deepspeed="./ds_config/deepspeed_stage_3_config.json",
|
| 146 |
num_train_epochs=1,
|
| 147 |
per_device_train_batch_size=1 if debug_mode else 2,
|
| 148 |
gradient_accumulation_steps=1 if debug_mode else 8,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
warmup_steps=100,
|
| 150 |
+
learning_rate=3e-5,
|
| 151 |
+
logging_steps=100,
|
| 152 |
+
save_steps=10,
|
| 153 |
+
save_total_limit=2,
|
| 154 |
+
optim="adamw_8bit",
|
| 155 |
+
weight_decay=0,
|
| 156 |
lr_scheduler_type="cosine",
|
| 157 |
+
seed=3407,
|
|
|
|
|
|
|
|
|
|
| 158 |
report_to="none",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
trainer = SFTTrainer(
|
|
|
|
| 165 |
train_dataset=train_dataset,
|
| 166 |
)
|
| 167 |
|
|
|
|
| 168 |
print("开始训练...")
|
| 169 |
trainer.train()
|
| 170 |
|
|
|
|
| 171 |
if args.local_rank in (-1, 0):
|
| 172 |
print(f"保存模型到: {args.output_model_dir}")
|
| 173 |
trainer.save_model()
|
examples/tutorials/dpo/ultrachat/{step_2_train_sft_model_stages_0.py → step_2_train_sft_model_single_gpu.py}
RENAMED
|
@@ -4,6 +4,8 @@
|
|
| 4 |
https://huggingface.co/docs/trl/v0.16.1/en/sft_trainer
|
| 5 |
|
| 6 |
单卡 V00 32G 全参微调
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
import argparse
|
| 9 |
import os
|
|
@@ -11,8 +13,6 @@ from pathlib import Path
|
|
| 11 |
import platform
|
| 12 |
|
| 13 |
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
| 14 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1" # 指定使用2张卡
|
| 15 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 16 |
|
| 17 |
debug_mode = True if platform.system() in ("Windows", "Darwin") else False
|
| 18 |
print(f"debug_mode: {debug_mode}")
|
|
@@ -55,7 +55,7 @@ def get_args():
|
|
| 55 |
),
|
| 56 |
parser.add_argument(
|
| 57 |
"--output_model_dir",
|
| 58 |
-
default=(temp_directory / "trained_models/qwen2_5-0_5B-ultrachat-sft").as_posix(),
|
| 59 |
type=str
|
| 60 |
),
|
| 61 |
parser.add_argument(
|
|
@@ -90,9 +90,17 @@ def main():
|
|
| 90 |
|
| 91 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 92 |
|
| 93 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
model = model.to(device)
|
| 95 |
-
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
|
| 96 |
if tokenizer.pad_token is None:
|
| 97 |
tokenizer.pad_token = tokenizer.eos_token
|
| 98 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
|
@@ -136,6 +144,7 @@ def main():
|
|
| 136 |
optim="adamw_torch",
|
| 137 |
report_to="none",
|
| 138 |
max_length=1024 if debug_mode else 2048,
|
|
|
|
| 139 |
)
|
| 140 |
|
| 141 |
trainer = SFTTrainer(
|
|
|
|
| 4 |
https://huggingface.co/docs/trl/v0.16.1/en/sft_trainer
|
| 5 |
|
| 6 |
单卡 V00 32G 全参微调
|
| 7 |
+
python3 step_2_train_sft_model_single_gpu.py
|
| 8 |
+
|
| 9 |
"""
|
| 10 |
import argparse
|
| 11 |
import os
|
|
|
|
| 13 |
import platform
|
| 14 |
|
| 15 |
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
|
|
|
|
|
|
| 16 |
|
| 17 |
debug_mode = True if platform.system() in ("Windows", "Darwin") else False
|
| 18 |
print(f"debug_mode: {debug_mode}")
|
|
|
|
| 55 |
),
|
| 56 |
parser.add_argument(
|
| 57 |
"--output_model_dir",
|
| 58 |
+
default=(temp_directory / "trained_models/qwen2_5-0_5B-ultrachat-sft-single-gpu").as_posix(),
|
| 59 |
type=str
|
| 60 |
),
|
| 61 |
parser.add_argument(
|
|
|
|
| 90 |
|
| 91 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 92 |
|
| 93 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 94 |
+
args.model_name,
|
| 95 |
+
cache_dir=args.model_cache_dir,
|
| 96 |
+
trust_remote_code=True,
|
| 97 |
+
)
|
| 98 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 99 |
+
args.model_name,
|
| 100 |
+
cache_dir=args.model_cache_dir,
|
| 101 |
+
trust_remote_code=True,
|
| 102 |
+
)
|
| 103 |
model = model.to(device)
|
|
|
|
| 104 |
if tokenizer.pad_token is None:
|
| 105 |
tokenizer.pad_token = tokenizer.eos_token
|
| 106 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
|
|
|
| 144 |
optim="adamw_torch",
|
| 145 |
report_to="none",
|
| 146 |
max_length=1024 if debug_mode else 2048,
|
| 147 |
+
dataloader_pin_memory=False # 为 True时,内存中的数据不会被降级到磁盘,只有当数据真的在内存中才可被GPU高效访问。
|
| 148 |
)
|
| 149 |
|
| 150 |
trainer = SFTTrainer(
|
examples/tutorials/rlhf/gpt2_sst2/step_5_ppo_rlhf.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
#!/usr/bin/python3
|
| 2 |
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
| 3 |
import argparse
|
| 4 |
import copy
|
| 5 |
import os
|
|
|
|
| 1 |
#!/usr/bin/python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
https://huggingface.co/docs/trl/v0.16.1/en/ppo_trainer
|
| 5 |
+
"""
|
| 6 |
import argparse
|
| 7 |
import copy
|
| 8 |
import os
|