-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathex_trl_constant.py
More file actions
59 lines (49 loc) · 1.72 KB
/
ex_trl_constant.py
File metadata and controls
59 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from datasets import load_dataset
from sft_trainer import SFTTrainer
from transformers import AutoModelForCausalLM, AutoTokenizer
from trl import DataCollatorForCompletionOnlyLM
from llmcompressor.transformers.utils.arg_parser import TrainingArguments
model_path = "neuralmagic/Llama-2-7b-pruned50-retrained"
output_dir = "./output_trl_sft_test_7b_gsm8k_sft_data"
model = AutoModelForCausalLM.from_pretrained(
model_path, torch_dtype="auto", device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer.pad_token = tokenizer.eos_token
# recipe for maintaining model sparsity during finetuning
recipe = """
test_stage:
pruning_modifiers:
ConstantPruningModifier:
targets: ['re:.*q_proj.weight', 're:.*k_proj.weight', 're:.*v_proj.weight',
're:.*o_proj.weight','re:.*gate_proj.weight', 're:.*up_proj.weight',
're:.*down_proj.weight']
start: 0
"""
# Load gsm8k using TRL dataset tools
dataset = load_dataset("gsm8k", "main", split="train")
def formatting_prompts_func(example):
output_texts = []
for i in range(len(example["question"])):
text = f"Question: {example['question'][i]}\n Answer: {example['answer'][i]}"
output_texts.append(text)
return output_texts
response_template = "Answer:"
collator = DataCollatorForCompletionOnlyLM(response_template, tokenizer=tokenizer)
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=0.6,
logging_steps=50,
gradient_checkpointing=True,
)
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
recipe=recipe,
train_dataset=dataset,
formatting_func=formatting_prompts_func,
data_collator=collator,
args=training_args,
max_seq_length=512,
)
trainer.train()