|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | + |
| 18 | +import paddle |
| 19 | + |
| 20 | +from paddlenlp.peft import VeRAConfig, VeRAModel |
| 21 | +from paddlenlp.transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer |
| 22 | +from paddlenlp.utils.env import CONFIG_NAME |
| 23 | + |
| 24 | + |
| 25 | +def parse_arguments(): |
| 26 | + parser = argparse.ArgumentParser() |
| 27 | + parser.add_argument("--model_name_or_path", default=None, help="The directory of pretrained model.") |
| 28 | + parser.add_argument("--vera_path", default="", help="The directory of VeRA parameters. Default to None") |
| 29 | + parser.add_argument( |
| 30 | + "--merge_vera_model_path", |
| 31 | + default="", |
| 32 | + help="The directory of merged parameters. Default to None", |
| 33 | + ) |
| 34 | + parser.add_argument("--device", type=str, default="gpu", help="Device") |
| 35 | + parser.add_argument( |
| 36 | + "--low_gpu_mem", type=bool, default=True, help="Whether to use low gpu memory. Default to False" |
| 37 | + ) |
| 38 | + return parser.parse_args() |
| 39 | + |
| 40 | + |
| 41 | +def weight_process(name, vera_config, state_dict): |
| 42 | + weight = state_dict.pop(name + ".weight").cuda() |
| 43 | + vera_A = state_dict.pop(name + ".vera_A").cuda() |
| 44 | + vera_B = state_dict.pop(name + ".vera_B").cuda() |
| 45 | + vera_b = state_dict.pop(name + ".vera_b").cuda() |
| 46 | + vera_d = state_dict.pop(name + ".vera_d").cuda() |
| 47 | + diag_b = paddle.diag(vera_b) |
| 48 | + diag_d = paddle.diag(vera_d) |
| 49 | + |
| 50 | + scaling = vera_config.vera_alpha / vera_config.r |
| 51 | + state_dict[name + ".weight"] = (weight + vera_A @ diag_d @ vera_B @ diag_b * scaling).cpu() |
| 52 | + |
| 53 | + |
| 54 | +def merge(): |
| 55 | + args = parse_arguments() |
| 56 | + paddle.set_device(args.device) |
| 57 | + |
| 58 | + vera_config = VeRAConfig.from_pretrained(args.vera_path) |
| 59 | + if vera_config.base_model_name_or_path is None: |
| 60 | + if args.model_name_or_path is not None: |
| 61 | + raise ValueError("We can not find a valid model_name_or_path.") |
| 62 | + else: |
| 63 | + vera_config.base_model_name_or_path = args.model_name_or_path |
| 64 | + |
| 65 | + if os.path.isfile(os.path.join(args.vera_path, CONFIG_NAME)): |
| 66 | + config = AutoConfig.from_pretrained(args.vera_path) |
| 67 | + elif args.model_name_or_path is not None: |
| 68 | + config = AutoConfig.from_pretrained(args.model_name_or_path) |
| 69 | + else: |
| 70 | + raise ValueError( |
| 71 | + f"We can not find config.json in vera_path: {args.vera_path} or find a valid model_name_or_path." |
| 72 | + ) |
| 73 | + config.dtype = vera_config.dtype |
| 74 | + if ( |
| 75 | + vera_config.dtype == "bfloat16" or config.quantization_config.weight_quantize_algo in ["nf4", "fp4"] |
| 76 | + ) and args.device == "cpu": |
| 77 | + raise ValueError("We can not apply bfloat16 or nf4/fp4 vera merge on cpu.") |
| 78 | + |
| 79 | + # with device_guard() will cause SVD decomposition to fail |
| 80 | + model = AutoModelForCausalLM.from_pretrained( |
| 81 | + vera_config.base_model_name_or_path, |
| 82 | + config=config, |
| 83 | + low_cpu_mem_usage=True, |
| 84 | + ) |
| 85 | + model = VeRAModel.from_pretrained(model=model, vera_path=args.vera_path, vera_config=vera_config) |
| 86 | + |
| 87 | + model.eval() |
| 88 | + model_state_dict = model.model.state_dict() |
| 89 | + vera_name_list = [] |
| 90 | + for key in model_state_dict.keys(): |
| 91 | + if "vera_A" in key: |
| 92 | + vera_name_list.append(key[:-7]) |
| 93 | + |
| 94 | + for name in vera_name_list: |
| 95 | + weight_process(name, vera_config, model_state_dict) |
| 96 | + |
| 97 | + model.model.save_pretrained(args.merge_vera_model_path, state_dict=model_state_dict) |
| 98 | + tokenizer = AutoTokenizer.from_pretrained(vera_config.base_model_name_or_path) |
| 99 | + tokenizer.save_pretrained(args.merge_vera_model_path) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + merge() |
0 commit comments