Skip to content

Commit

Permalink
add workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
emattia committed Aug 30, 2024
1 parent 8914bcc commit 25a5204
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
36 changes: 19 additions & 17 deletions finetune_hf_peft.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import os
import json
from metaflow import FlowSpec, step, IncludeFile, Parameter, secrets, resources, retry, pypi, huggingface_card, nvidia, S3
from metaflow import FlowSpec, step, IncludeFile, Parameter, secrets, resources, secrets, retry, pypi_base, huggingface_card, kubernetes, S3
from metaflow.profilers import gpu_profile
from exceptions import GatedRepoError, GATED_HF_ORGS

@pypi_base(packages={
'datasets': '',
'torch': '',
'transformers': '',
'peft': '',
'trl': '',
'accelerate': '',
'bitsandbytes': '',
'sentencepiece': '',
'safetensors': '',
'requests': ''
})
class FinetuneLlama3LoRA(FlowSpec):

script_args_file = IncludeFile(
Expand All @@ -19,7 +31,6 @@ class FinetuneLlama3LoRA(FlowSpec):
help="Flag for a smoke test"
)

@pypi(disabled=True)
@secrets(sources=["huggingface-token"])
@step
def start(self):
Expand All @@ -33,25 +44,17 @@ def start(self):
raise GatedRepoError(self.script_args.dataset_name)
self.next(self.sft)

@pypi(packages={
'datasets': '',
'torch': '',
'transformers': '',
'peft': '',
'trl': '',
'accelerate': '',
'bitsandbytes': '',
'sentencepiece': '',
'safetensors': ''
})
@gpu_profile(interval=1)
@huggingface_card
@nvidia
@secrets(sources=["huggingface-token"])
@kubernetes(gpu=1)
@step
def sft(self):
import os
from my_peft_tools import create_model, create_trainer, save_model, get_tar_bytes
import huggingface_hub
huggingface_hub.login('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')

huggingface_hub.login(os.environ['HF_TOKEN']) # contained in hugginface-token secret
model, tokenizer = create_model(self.script_args)
trainer = create_trainer(self.script_args, tokenizer, model, smoke=self.smoke, card=True)
trainer.train()
Expand All @@ -62,11 +65,10 @@ def sft(self):
s3.put('lora_merged.tar.gz', get_tar_bytes(merge_output_dirname))
self.next(self.end)

@pypi(disabled=True)
@step
def end(self):
print("Training completed successfully!")


if __name__ == '__main__':
FinetuneLlama3LoRA()
FinetuneLlama3LoRA()
6 changes: 3 additions & 3 deletions hf_peft_args.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"local_rank": -1,
"per_device_train_batch_size": 1,
"per_device_eval_batch_size": 4,
"per_device_train_batch_size": 16,
"per_device_eval_batch_size": 16,
"gradient_accumulation_steps": 17,
"learning_rate": 3e-4,
"max_grad_norm": 0.3,
Expand Down Expand Up @@ -30,4 +30,4 @@
"logging_steps": 5,
"merge": false,
"output_dir": "./lora_checkpoints"
}
}
1 change: 0 additions & 1 deletion huggingface_model_card.json

This file was deleted.

4 changes: 2 additions & 2 deletions my_peft_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ def save_model(args, trainer, dirname="final", merge_dirname="final_merged_check
if args.merge:
"""
This conditional block merges the LoRA adapter with the original model weights.
NOTE: For use with NIM, we do not need to do the merge, the adapter_config.json
NOTE: For use with NIM, we do not need to do the merge.
"""
model = AutoPeftModelForCausalLM.from_pretrained(output_dir, device_map="auto", torch_dtype=torch.bfloat16)
model = model.merge_and_unload()
output_merged_dir = os.path.join(args.output_dir, merge_dirname)
model.save_pretrained(output_merged_dir, safe_serialization=True)
return output_dir, merge_dirname
return output_dir, output_merged_dir
else:
return output_dir, None

Expand Down

0 comments on commit 25a5204

Please sign in to comment.