forked from NVIDIA/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LoRA support for HF::AutoModelForCausalLM (NVIDIA#10982)
* add LinearAdapter Signed-off-by: Alexandros Koumparoulis <[email protected]> * add hf lora example Signed-off-by: Alexandros Koumparoulis <[email protected]> * remove unused imports Signed-off-by: Alexandros Koumparoulis <[email protected]> * fix Signed-off-by: Alexandros Koumparoulis <[email protected]> * fix Signed-off-by: Alexandros Koumparoulis <[email protected]> * subclass mixin Signed-off-by: Alexandros Koumparoulis <[email protected]> * remove stale imports Signed-off-by: Alexandros Koumparoulis <[email protected]> * undo Signed-off-by: Alexandros Koumparoulis <[email protected]> * fix scale Signed-off-by: Alexandros Koumparoulis <[email protected]> * regex selector for peft Signed-off-by: Alexandros Koumparoulis <[email protected]> * move lora Signed-off-by: Alexandros Koumparoulis <[email protected]> * fmt Signed-off-by: Alexandros Koumparoulis <[email protected]> * hf_auto_model_for_causal_lm finetune recipe Signed-off-by: Alexandros Koumparoulis <[email protected]> * Apply isort and black reformatting Signed-off-by: akoumpa <[email protected]> --------- Signed-off-by: Alexandros Koumparoulis <[email protected]> Signed-off-by: akoumpa <[email protected]> Co-authored-by: akoumpa <[email protected]> Signed-off-by: Hainan Xu <[email protected]>
- Loading branch information
1 parent
cf6c6a7
commit 91f4b5f
Showing
4 changed files
with
222 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. 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. | ||
|
||
import fiddle as fdl | ||
from pytorch_lightning.loggers import WandbLogger | ||
from nemo import lightning as nl | ||
from nemo.collections import llm | ||
|
||
|
||
def mk_hf_dataset(tokenizer): | ||
EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN | ||
|
||
def formatting_prompts_func(examples): | ||
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. | ||
### Instruction: | ||
{} | ||
### Input: | ||
{} | ||
### Response: | ||
{}""" | ||
instruction = examples["context"] | ||
input = examples["question"] | ||
output = examples["answers"]['text'] | ||
if isinstance(output, list): | ||
output = output[0] | ||
text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN | ||
ans = tokenizer(text) | ||
tokens = ans['input_ids'] | ||
return { | ||
'tokens': tokens, | ||
'labels': tokens[1:] + [tokens[-1]], | ||
} | ||
|
||
from datasets import load_dataset | ||
|
||
dataset = load_dataset("rajpurkar/squad", split="train") | ||
dataset = dataset.map(formatting_prompts_func, batched=False, batch_size=2) | ||
return dataset | ||
|
||
|
||
if __name__ == '__main__': | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--model', default='meta-llama/Llama-3.2-1B') | ||
parser.add_argument('--strategy', type=str, default='auto', choices=['auto', 'ddp', 'fsdp']) | ||
parser.add_argument('--devices', default=1) | ||
parser.add_argument('--accelerator', default='gpu', choices=['gpu']) | ||
parser.add_argument('--max-steps', type=int, default=100) | ||
parser.add_argument('--wandb-project', type=str, default=None) | ||
args = parser.parse_args() | ||
|
||
wandb = None | ||
if args.wandb_project is not None: | ||
model = '_'.join(args.model.split('/')[-2:]) | ||
wandb = WandbLogger( | ||
project=args.wandb_project, | ||
name=f'{model}_dev{args.devices}_strat_{args.strategy}', | ||
) | ||
grad_clip = 0.5 | ||
if args.strategy == 'fsdp': | ||
# See: https://github.com/Lightning-AI/pytorch-lightning/blob/8ad3e29816a63d8ce5c00ac104b14729a4176f4f/src/lightning/pytorch/plugins/precision/fsdp.py#L81 | ||
grad_clip = None | ||
use_dist_samp = False | ||
tokenizer = llm.HfAutoModelForCausalLM.configure_tokenizer(args.model) | ||
|
||
llm.api.finetune( | ||
model=llm.HfAutoModelForCausalLM(args.model), | ||
data=llm.HfDatasetDataModule( | ||
mk_hf_dataset(tokenizer.tokenizer), pad_token_id=tokenizer.tokenizer.eos_token_id | ||
), | ||
trainer=nl.Trainer( | ||
devices=args.devices, | ||
max_steps=args.max_steps, | ||
accelerator=args.accelerator, | ||
strategy=args.strategy, | ||
log_every_n_steps=1, | ||
limit_val_batches=0.0, | ||
num_sanity_val_steps=0, | ||
accumulate_grad_batches=10, | ||
gradient_clip_val=grad_clip, | ||
use_distributed_sampler=use_dist_samp, | ||
logger=wandb, | ||
), | ||
optim=fdl.build(llm.adam.pytorch_adam_with_flat_lr(max_lr=1e-5, clip_grad=0.5)), | ||
log=None, | ||
peft=llm.peft.LoRA( | ||
target_modules=['*_proj'], | ||
dim=32, | ||
), | ||
) |
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