-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TRL SFTTrainer Examples #2211
Merged
Merged
TRL SFTTrainer Examples #2211
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f013c2c
WIP sft mixin
9986f34
its running at least
51dd109
clean up
72e3685
Merge branch 'main' into sa/sft_trainer_mixin
086c2ff
revert debugging changes
64096c7
example script
e243b46
POC SFT sparse trainer
c84cd60
use sft data functionality
4f938cb
update unit tests
3821606
move examples folder
4f619bd
clarity comments
e425523
barest bones trainer
3c9a2e3
style
2ed1bb2
tweaks to work with distillation and FSDP
118c7c1
Merge branch 'main' into sa/sft_trainer_mixin
1f61081
tests and readme
44cb166
naming
db5060c
Merge branch 'main' into sa/sft_trainer_mixin
e8021e6
Merge branch 'main' into sa/sft_trainer_mixin
7648a7f
Merge branch 'main' into sa/sft_trainer_mixin
02aec71
Merge branch 'main' into sa/sft_trainer_mixin
0051eaf
move examples
7630dd7
quality
7b289d2
Merge branch 'main' into sa/sft_trainer_mixin
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
91 changes: 91 additions & 0 deletions
91
src/sparseml/transformers/finetune/examples/test_trl_distillation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from transformers import DefaultDataCollator | ||
|
||
from sparseml.transformers import ( | ||
DataTrainingArguments, | ||
SFTTrainer, | ||
SparseAutoModelForCausalLM, | ||
SparseAutoTokenizer, | ||
TextGenerationDataset, | ||
TrainingArguments, | ||
) | ||
|
||
|
||
model_path = "neuralmagic/Llama-2-7b-pruned50-retrained" | ||
teacher_path = "zoo:llama2-7b-gsm8k_llama2_pretrain-base" | ||
output_dir = "./output_trl_sft_test_7b_gsm8k" | ||
|
||
model = SparseAutoModelForCausalLM.from_pretrained( | ||
model_path, torch_dtype="auto", device_map="auto" | ||
) | ||
teacher = SparseAutoModelForCausalLM.from_pretrained( | ||
teacher_path, torch_dtype="auto", device_map="auto" | ||
) | ||
|
||
tokenizer = SparseAutoTokenizer.from_pretrained(model_path) | ||
|
||
# Load gsm8k using SparseML dataset tools | ||
data_args = DataTrainingArguments( | ||
dataset="gsm8k", dataset_config_name="main", max_seq_length=512 | ||
) | ||
dataset_manager = TextGenerationDataset.load_from_registry( | ||
data_args.dataset, | ||
data_args=data_args, | ||
split="train", | ||
tokenizer=tokenizer, | ||
) | ||
train_dataset = dataset_manager.tokenize_and_process() | ||
print(f"--> Training Set Length = {len(train_dataset)}") | ||
|
||
# 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 | ||
OutputDistillationModifier: | ||
targets: ['re:model.layers.\\d+$'] | ||
comparison: "square_head" | ||
start: 0 | ||
orig_scale: 1.0 | ||
distill_scale: 1.0 | ||
""" | ||
|
||
data_collator = DefaultDataCollator() | ||
training_args = TrainingArguments( | ||
output_dir=output_dir, | ||
num_train_epochs=0.6, | ||
logging_steps=50, | ||
gradient_checkpointing=True, | ||
bf16=True, | ||
) | ||
trainer = SFTTrainer( | ||
model=model, | ||
teacher=teacher, | ||
tokenizer=tokenizer, | ||
recipe=recipe, | ||
train_dataset=train_dataset, | ||
data_collator=data_collator, | ||
args=training_args, | ||
data_args=data_args, | ||
max_seq_length=data_args.max_seq_length, | ||
packing=True, | ||
) | ||
trainer.train() | ||
trainer.save_model(output_dir=trainer.args.output_dir) |
78 changes: 78 additions & 0 deletions
78
src/sparseml/transformers/finetune/examples/test_trl_sft_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from datasets import load_dataset | ||
|
||
from sparseml.transformers import ( | ||
SFTTrainer, | ||
SparseAutoModelForCausalLM, | ||
SparseAutoTokenizer, | ||
TrainingArguments, | ||
) | ||
from trl import DataCollatorForCompletionOnlyLM | ||
|
||
|
||
model_path = "neuralmagic/Llama-2-7b-pruned50-retrained" | ||
output_dir = "./output_trl_sft_test_7b_gsm8k_sft_data" | ||
model = SparseAutoModelForCausalLM.from_pretrained( | ||
model_path, torch_dtype="auto", device_map="auto" | ||
) | ||
tokenizer = SparseAutoTokenizer.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() | ||
trainer.save_model(output_dir=trainer.args.output_dir) | ||
Satrat marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it important at all that the TrainingArguments comes from SparseML?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few things won't work if the native transformers TrainingArguments is used: no support for recipe overrides, no compressed save, no multistage training runs. The mix-in uses these params, so if we wanted to support the non-sparseml TrainingArguments we would have to check each time we reference them. I don't think its worth the extra lines personally