-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
vera-pissa method added #8722
Merged
Merged
vera-pissa method added #8722
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1d51f1f
vera-pissa method added
TranscenderNing aa95de7
add vera-pissa
TranscenderNing d0f9689
Add vera-pissa and test
TranscenderNing 92f8773
Add vera-pissa and tests and correct the lint format
TranscenderNing 277e388
Revise according to the review comments and add tests
TranscenderNing 1baf39a
Revise according to the review comments and pass tests
TranscenderNing 0aef75a
Revise according to the review comments and pass tests 1
TranscenderNing 6c6c708
Revise according to the review comments and pass tests 2
TranscenderNing dd86a6c
Revise according to the review comments and pass tests 3
TranscenderNing ddc939a
Revise according to the review comments and pass tests 31
TranscenderNing d4810c1
Revise according to the review comments and pass tests 32
TranscenderNing 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"model_name_or_path": "facebook/llama-7b", | ||
"dataset_name_or_path": "./data", | ||
"output_dir": "./checkpoints/vera_ckpts", | ||
"per_device_train_batch_size": 4, | ||
"gradient_accumulation_steps": 4, | ||
"per_device_eval_batch_size": 8, | ||
"eval_accumulation_steps":16, | ||
"num_train_epochs": 1, | ||
"learning_rate": 3e-04, | ||
"warmup_steps": 30, | ||
"logging_steps": 1, | ||
"evaluation_strategy": "epoch", | ||
"save_strategy": "epoch", | ||
"src_length": 1024, | ||
"max_length": 2048, | ||
"fp16": true, | ||
"fp16_opt_level": "O2", | ||
"do_train": true, | ||
"do_eval": true, | ||
"disable_tqdm": true, | ||
"load_best_model_at_end": true, | ||
"eval_with_do_generation": false, | ||
"metric_for_best_model": "accuracy", | ||
"recompute": true, | ||
"save_total_limit": 10, | ||
"tensor_parallel_degree": 1, | ||
"pipeline_parallel_degree": 1, | ||
"vera": true, | ||
"zero_padding": false, | ||
"use_flash_attention": false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Copyright (c) 2024 PaddlePaddle Authors. 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 argparse | ||
import os | ||
|
||
import paddle | ||
|
||
from paddlenlp.peft import VeRAConfig, VeRAModel | ||
from paddlenlp.transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer | ||
from paddlenlp.utils.env import CONFIG_NAME | ||
|
||
|
||
def parse_arguments(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--model_name_or_path", default=None, help="The directory of pretrained model.") | ||
parser.add_argument("--vera_path", default="", help="The directory of VeRA parameters. Default to None") | ||
parser.add_argument( | ||
"--merge_vera_model_path", | ||
default="", | ||
help="The directory of merged parameters. Default to None", | ||
) | ||
parser.add_argument("--device", type=str, default="gpu", help="Device") | ||
parser.add_argument( | ||
"--low_gpu_mem", type=bool, default=True, help="Whether to use low gpu memory. Default to False" | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def weight_process(name, vera_config, state_dict): | ||
weight = state_dict.pop(name + ".weight").cuda() | ||
vera_A = state_dict.pop(name + ".vera_A").cuda() | ||
vera_B = state_dict.pop(name + ".vera_B").cuda() | ||
vera_b = state_dict.pop(name + ".vera_b").cuda() | ||
vera_d = state_dict.pop(name + ".vera_d").cuda() | ||
diag_b = paddle.diag(vera_b) | ||
diag_d = paddle.diag(vera_d) | ||
|
||
scaling = vera_config.vera_alpha / vera_config.r | ||
state_dict[name + ".weight"] = (weight + vera_A @ diag_d @ vera_B @ diag_b * scaling).cpu() | ||
|
||
|
||
def merge(): | ||
args = parse_arguments() | ||
paddle.set_device(args.device) | ||
|
||
vera_config = VeRAConfig.from_pretrained(args.vera_path) | ||
if vera_config.base_model_name_or_path is None: | ||
if args.model_name_or_path is not None: | ||
raise ValueError("We can not find a valid model_name_or_path.") | ||
else: | ||
vera_config.base_model_name_or_path = args.model_name_or_path | ||
|
||
if os.path.isfile(os.path.join(args.vera_path, CONFIG_NAME)): | ||
config = AutoConfig.from_pretrained(args.vera_path) | ||
elif args.model_name_or_path is not None: | ||
config = AutoConfig.from_pretrained(args.model_name_or_path) | ||
else: | ||
raise ValueError( | ||
f"We can not find config.json in vera_path: {args.vera_path} or find a valid model_name_or_path." | ||
) | ||
config.dtype = vera_config.dtype | ||
if ( | ||
vera_config.dtype == "bfloat16" or config.quantization_config.weight_quantize_algo in ["nf4", "fp4"] | ||
) and args.device == "cpu": | ||
raise ValueError("We can not apply bfloat16 or nf4/fp4 vera merge on cpu.") | ||
|
||
vera_config.merge_weights = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vera_config.merge_weights没有merge weight了,记得去掉,否则会报错 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
# with device_guard(): 会导致svd无法进行分解 | ||
model = AutoModelForCausalLM.from_pretrained( | ||
vera_config.base_model_name_or_path, | ||
config=config, | ||
low_cpu_mem_usage=True, | ||
) | ||
model = VeRAModel.from_pretrained(model=model, vera_path=args.vera_path, vera_config=vera_config) | ||
|
||
model.eval() | ||
model_state_dict = model.model.state_dict() | ||
vera_name_list = [] | ||
for key in model_state_dict.keys(): | ||
if "vera_A" in key: | ||
vera_name_list.append(key[:-7]) | ||
|
||
for name in vera_name_list: | ||
weight_process(name, vera_config, model_state_dict) | ||
|
||
model.model.save_pretrained(args.merge_vera_model_path, state_dict=model_state_dict) | ||
tokenizer = AutoTokenizer.from_pretrained(vera_config.base_model_name_or_path) | ||
tokenizer.save_pretrained(args.merge_vera_model_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
merge() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright (c) 2024 PaddlePaddle Authors. 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 .vera_config import VeRAConfig | ||
from .vera_layers import VeRALinear | ||
from .vera_model import VeRAModel |
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.
是否验证过merge后的模型正确性?
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.
验证过,用merge后的模型可以正确预测。 done