当前位置:   article > 正文

使用 LoRA 在 vi​​ggo 数据集上微调 Microsoft phi-2 小语言模型_phi-2 微调

phi-2 微调

一、说明

        Microsoft 的基于 Transformer 的小语言模型。它可以根据 MIT 许可在HuggingFace上使用。

        它在 96 个 A100 GPU 上使用 1.4T 令牌进行了 14 天的训练。Phi-2 是一个 27 亿个参数的预训练 Transformer,不使用 RLHF 或指示微调。它进行下一个标记预测,并可用于问答、聊天格式和代码生成中的文本生成。

        事实证明,phi-2 在多个基准测试和编码和数学等任务上优于许多具有 7B 和 13B 参数的模型。

        小语言模型之所以具有优异的性能,是因为使用了经过提炼的高质量训练数据或“教科书质量”的数据。小语言模型使用知识蒸馏。也就是说,他们接受了从 LLMS 中提取的核心/基本知识的培训。然后采用剪枝和量化技术来删除模型的非必要部分。训练数据通常是综合数据集的混合物,这些数据集是专门创建的,旨在教导模型执行科学、日常活动、心理理论等领域的常识推理和一般知识。它还可能包含具有高教育意义的选择性网络数据价值和质量。小语言模型使用创新技术进行扩展。

        接下来,我们将看到有关如何使用 HuggingFace 中的 phi-2 进行提示的分步 Python 代码,然后我们将在 veggo 数据集上对其进行微调。我使用 T4 GPU 在 Google Colab 免费层上运行了此代码笔记本。

二、安装依赖库

        我的代码借鉴自 GitHub 上Harper Carrol 的这篇优秀教程。

  1. 安装所需的库
  1. #@title Install required libraries
  2. !pip install accelerate==0.25.0
  3. !pip install bitsandbytes==0.41.1
  4. !pip install datasets==2.14.6
  5. !pip install peft==0.6.2
  6. !pip install transformers==4.36.2
  7. !pip install torch==2.1.0
  8. !pip install einops==0.4.1
  9. !pip install huggingface_hub

2.所需进口

  1. import torch
  2. import transformers
  3. from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments, pipeline, logging
  4. from datasets import Dataset

3.我们将使用Google Colab Free tier(T4)上的cuda设备来运行模型

torch.set_default_device("cuda")

4.创建模型和分词器

  1. #create the model object and the corresponding tokenizer
  2. model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
  3. tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)

5. 让我们运行一些提示并查看模型响应

  1. # https://huggingface.co/microsoft/phi-2
  2. # This prompt is for code completion
  3. # here the prompt is written within the tokenizer()
  4. inputs = tokenizer('''def fibonacci(n):
  5. """
  6. This function prints the terms in Fibonacci series upto n
  7. """''', return_tensors="pt", return_attention_mask=False)
  8. outputs = model.generate(**inputs, max_length=100)
  9. text = tokenizer.batch_decode(outputs)[0]
  10. print(text)
  1. #https://huggingface.co/microsoft/phi-2
  2. # here a string containing the prompt is defined separately from the tokenizer() and then passed to it
  3. prompt = '''def fibonacci(n):
  4. """
  5. This function prints the terms in Fibonacci series upto n
  6. """'''
  7. inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
  8. outputs = model.generate(**inputs, max_length=100)
  9. text = tokenizer.batch_decode(outputs)[0]
  10. print(text)
  1. # here we see the output of phi-2 for a question-answering prompt
  2. prompt = 'What is thee relevance of mathematics for understanding physics?'
  3. inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=False)
  4. outputs = model.generate(**inputs, max_length=200)
  5. text = tokenizer.batch_decode(outputs)[0]
  6. print(text)

三、在HuggingFace的veggo微调 phi-2 模型 

现在我们将在HuggingFace 的“veggo”数据集

ViGGO是视频游戏领域的英文数据到文本生成数据集。目标响应以会话形式以意义表示形式呈现。该数据集大约有 5,000 个非常干净的数据点,因此该数据集可用于评估神经模型的迁移学习、低资源或少样本能力。

6. 让我们设置加速器来加速训练/微调

  1. #@title Set up accelerator to speed up the training/finetuning
  2. from accelerate import FullyShardedDataParallelPlugin, Accelerator
  3. from torch.distributed.fsdp.fully_sharded_data_parallel import FullOptimStateDictConfig, FullStateDictConfig
  4. fsdp_plugin = FullyShardedDataParallelPlugin(
  5. state_dict_config=FullStateDictConfig(offload_to_cpu=True, rank0_only=False),
  6. optim_state_dict_config=FullOptimStateDictConfig(offload_to_cpu=True, rank0_only=False),
  7. )
  8. accelerator = Accelerator(fsdp_plugin=fsdp_plugin)

7. 使用有效的 HuggingFace 访问令牌登录您的 Huggingface 帐户。

        您应该在 HuggingFace 上有一个帐户,然后您可以创建一个免费的访问令牌。

  1. #@title login to your huggingface account using your access token
  2. # you can find your access token at https://huggingface.co/settings/tokens
  3. from huggingface_hub import notebook_login
  4. notebook_login()

8.加载viggo数据集

  1. #@title load viggo dataset
  2. from datasets import load_dataset
  3. train_dataset = load_dataset('gem/viggo', split='train')
  4. eval_dataset = load_dataset('gem/viggo', split='validation')
  5. test_dataset = load_dataset('gem/viggo', split='test')

9. 加载基础模型phi-2

  1. #@title load base model microsoft/phi-2
  2. import torch
  3. from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForLanguageModeling
  4. base_model_id = "microsoft/phi-2"
  5. model = AutoModelForCausalLM.from_pretrained(base_model_id,
  6. load_in_8bit=True,
  7. torch_dtype=torch.float16,
  8. trust_remote_code=True)

10. 在下面的代码单元中,我们设置 tokenizer 对象, tokenize() 函数将 tokenizer 应用于每个提示,并创建一个“labels”列,其值与数据中的“input_ids”列相同。

        generate_and_tokenize_prompt() 函数将每个数据点转换为适合传递给 phi-2 模型的提示格式。它从数据点中提取“目标”和“含义表示”。最后,我们使用 map() 函数将此函数应用于 train 和 val 数据集中的每个数据点。

  1. #@title set up the tokenizer for base model
  2. tokenizer = AutoTokenizer.from_pretrained(
  3. base_model_id,
  4. add_eos_token=True,
  5. add_bos_token=True,
  6. use_fast=False, # needed for now, should be fixed soon
  7. )
  8. #@title setup tokenize function to make labels and input_ids the same for the self-supervised fine-tuning.
  9. def tokenize(prompt):
  10. result = tokenizer(prompt)
  11. result["labels"] = result["input_ids"].copy()
  12. return result
  13. #@title convert each sample into a prompt
  14. def generate_and_tokenize_prompt(data_point):
  15. full_prompt =f"""Given a target sentence construct the underlying meaning representation of the input sentence as a single function with attributes and attribute values.
  16. This function should describe the target string accurately and the function must be one of the following ['inform', 'request', 'give_opinion', 'confirm', 'verify_attribute', 'suggest', 'request_explanation', 'recommend', 'request_attribute'].
  17. The attributes must be one of the following: ['name', 'exp_release_date', 'release_year', 'developer', 'esrb', 'rating', 'genres', 'player_perspective', 'has_multiplayer', 'platforms', 'available_on_steam', 'has_linux_release', 'has_mac_release', 'specifier']
  18. ### Target sentence:
  19. {data_point["target"]}
  20. ### Meaning representation:
  21. {data_point["meaning_representation"]}
  22. """
  23. return tokenize(full_prompt)
  24. #@title Reformat the prompt and tokenize each sample:
  25. tokenized_train_dataset = train_dataset.map(generate_and_tokenize_prompt)
  26. tokenized_val_dataset = eval_dataset.map(generate_and_tokenize_prompt)

11. 模型的输入张量通常使用 max_length 参数将每个输入填充到统一长度。

        为了确定该参数的值,我们可以绘制每个 input_id 的长度分布,并将 max_length 设置为等于最长 input_id 的长度。在本例中,选择的 max_length 为 320。

12. 接下来,我们将再次应用 tokenize(),并将 max_length 参数设置为 320。

  1. max_length = 320 # appropriate max length for this dataset
  2. # redefine the tokenize function and tokenizer
  3. tokenizer = AutoTokenizer.from_pretrained(
  4. base_model_id,
  5. padding_side="left",
  6. add_eos_token=True,
  7. add_bos_token=True,
  8. trust_remote_code=True,
  9. use_fast=False, # needed for now, should be fixed soon
  10. )
  11. tokenizer.pad_token = tokenizer.eos_token
  12. def tokenize(prompt):
  13. result = tokenizer(
  14. prompt,
  15. truncation=True,
  16. max_length=max_length,
  17. padding="max_length",
  18. )
  19. result["labels"] = result["input_ids"].copy()
  20. return result
  21. #@title tokenize train and validation datasets using generate_and_tokenize_prompt function
  22. tokenized_train_dataset = train_dataset.map(generate_and_tokenize_prompt)
  23. tokenized_val_dataset = eval_dataset.map(generate_and_tokenize_prompt)

四、使用LoRA来微调phi-2

        13.让我们使用LoRA(低阶适应)来微调phi-2

        低秩适应是一种快速微调大型语言模型的技术。它冻结预训练的模型权重,并将可训练的秩分解矩阵注入到 Transformer 架构的每一层中,从而减少下游任务的可训练参数的数量。它可以将可训练参数的数量减少10000倍,将GPU内存需求减少3倍。

        要使用 LoRA 微调模型,您需要:

  1. 实例化基本模型。
  2. 创建一个配置 ( LoraConfig),在其中定义 LoRA 特定参数。
  3. 用 包裹基本模型get_peft_model()以获得可训练的PeftModel.
  4. PeftModel像平常训练基本模型一样训练。

   LoraConfig允许您通过以下参数控制 LoRA 如何应用于基础模型:

  • r:更新矩阵的秩,以 表示int。较低的秩会导致较小的更新矩阵和较少的可训练参数。
  • target_modules:应用 LoRA 更新矩阵的模块(例如,注意力块)。
  • alpha:LoRA 比例因子。
  • bias:指定是否bias应训练参数。可以是'none''all'或者'lora_only'
  • modules_to_save:除了 LoRA 层之外的模块列表,要设置为可训练并保存在最终检查点中。这些通常包括模型的自定义头,该头是为微调任务随机初始化的。
  • layers_to_transform:LoRA 转换的层列表。如果未指定,target_modules则变换中的所有图层。
  • layers_patterntarget_modules:如果layers_to_transform指定,则匹配 中图层名称的模式。默认情况下,PeftModel将查看公共层模式(layershblocks等),将其用于奇异和自定义模型。
  • rank_pattern:从图层名称或正则表达式到与 指定的默认排名不同的排名的映射r
  • alpha_pattern:从图层名称或正则表达式到 alpha 的映射,与 指定的默认 alpha 不同lora_alpha

        我们将把 LoRA 应用到模型的 Wqkv、fc1、fc2 层。

  1. from peft import LoraConfig, get_peft_model
  2. config = LoraConfig(
  3. r=8,
  4. lora_alpha=16,
  5. target_modules=[
  6. "Wqkv",
  7. "fc1",
  8. "fc2",
  9. ],
  10. bias="none",
  11. lora_dropout=0.05, # Conventional
  12. task_type="CAUSAL_LM",
  13. )
  14. model = get_peft_model(model, config)
  15. # Apply the acceleratort to the model for faster traning.
  16. model = accelerator.prepare_model(model)

五、 使用 LoRA 微调/训练模型

        您将需要设置训练参数或配置参数,例如保存模型的输出目录。我正在将微调后的模型保存/推送到我的 HuggingFace 帐户,您也可以将微调后的模型保存在本地目录或 Colab 目录中。

        其他训练参数包括warmup_steps、per_device_train_batch_size、gradient_accumulation_steps、max_steps、learning_rate、logging_steps、optim、logging_dir、save_strategy、save_steps、evaluation_strategy、eval_steps、do_eval、push_to_hub、report_to、run_name等。

        maz_steps 确定要执行的最大训练步骤,越长,您的模型就越精细,完成训练所需的时间也越长。当 max_steps = 1000 时,我花了 90 分钟在免费的 Google Colab 上进行训练。学习率也会影响训练时间。

  1. #Train the model and push each check point to Huggingface
  2. import transformers
  3. tokenizer.pad_token = tokenizer.eos_token
  4. trainer = transformers.Trainer(
  5. model=model,
  6. train_dataset=tokenized_train_dataset,
  7. eval_dataset=tokenized_val_dataset,
  8. args=transformers.TrainingArguments(
  9. output_dir="./phi2-finetunedonviggodataset",
  10. warmup_steps=5,
  11. per_device_train_batch_size=1,
  12. gradient_accumulation_steps=4,
  13. max_steps=500,
  14. learning_rate=2.5e-5,
  15. logging_steps=50,
  16. optim="paged_adamw_8bit",
  17. logging_dir="./logs", # Directory for storing logs
  18. save_strategy="steps", # Save the model checkpoint every logging step
  19. save_steps=50, # Save checkpoints every 50 steps
  20. evaluation_strategy="steps", # Evaluate the model every logging step
  21. eval_steps=50, # Evaluate and save checkpoints every 50 steps
  22. do_eval=True, # Perform evaluation at the end of training
  23. push_to_hub=True,
  24. ),
  25. data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False),
  26. )
  27. model.config.use_cache = False
  28. trainer.train()

        现在您已经在 viggo 数据集上微调了 phi-2,并将其保存在 output_dir 或您的 Huggingface 帐户中。

16.接下来,我们将比较基本模型(没有微调)和微调模型(上面训练过的)上示例提示的性能

  1. #Load the base model
  2. import torch
  3. from transformers import AutoTokenizer, AutoModelForCausalLM
  4. base_model_id = "microsoft/phi-2"
  5. base_model = AutoModelForCausalLM.from_pretrained(
  6. base_model_id,
  7. load_in_8bit=True,
  8. device_map="auto",
  9. trust_remote_code=True,
  10. torch_dtype=torch.float16,
  11. )
  12. eval_tokenizer = AutoTokenizer.from_pretrained(
  13. base_model_id,
  14. add_bos_token=True,
  15. trust_remote_code=True,
  16. use_fast=False,
  17. )
  18. #create a sample prompt for evaluation on base model
  19. eval_prompt = """Given a target sentence construct the underlying meaning representation of the input sentence as a single function with attributes and attribute values.
  20. This function should describe the target string accurately and the function must be one of the following ['inform', 'request', 'give_opinion', 'confirm', 'verify_attribute', 'suggest', 'request_explanation', 'recommend', 'request_attribute'].
  21. The attributes must be one of the following: ['name', 'exp_release_date', 'release_year', 'developer', 'esrb', 'rating', 'genres', 'player_perspective', 'has_multiplayer', 'platforms', 'available_on_steam', 'has_linux_release', 'has_mac_release', 'specifier']
  22. ### Target sentence:
  23. Earlier, you stated that you didn't have strong feelings about PlayStation's Little Big Adventure. Is your opinion true for all games which don't have multiplayer?
  24. ### Meaning representation:
  25. """
  26. # tokenize the above prompt and generate the response from base model
  27. model_input = eval_tokenizer(eval_prompt, return_tensors="pt").to('cuda')
  28. base_model.eval()
  29. with torch.no_grad():
  30. print(eval_tokenizer.decode(base_model.generate(**model_input, max_new_tokens=100)[0], skip_special_tokens=True))

17. 现在让我们从我的 HuggingFace 帐户加载经过微调的模型,并在其上测试相同的提示。

  1. from peft import PeftModel
  2. ft_model = PeftModel.from_pretrained(base_model, "nimrita/phi2-finetunedonviggodataset", force_download=True)
  3. eval_prompt = """Given a target sentence construct the underlying meaning representation of the input sentence as a single function with attributes and attribute values.
  4. This function should describe the target string accurately and the function must be one of the following ['inform', 'request', 'give_opinion', 'confirm', 'verify_attribute', 'suggest', 'request_explanation', 'recommend', 'request_attribute'].
  5. The attributes must be one of the following: ['name', 'exp_release_date', 'release_year', 'developer', 'esrb', 'rating', 'genres', 'player_perspective', 'has_multiplayer', 'platforms', 'available_on_steam', 'has_linux_release', 'has_mac_release', 'specifier']
  6. ### Target sentence:
  7. Earlier, you stated that you didn't have strong feelings about PlayStation's Little Big Adventure. Is your opinion true for all games which don't have multiplayer?
  8. ### Meaning representation:
  9. """
  10. model_input = eval_tokenizer(eval_prompt, return_tensors="pt").to('cuda')
  11. ft_model = ft_model.to('cuda')
  12. ft_model.eval()
  13. with torch.no_grad():
  14. print(eval_tokenizer.decode(ft_model.generate(**model_input, max_new_tokens=100)[0], skip_special_tokens=True))

        您刚刚微调了 phi-2。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号