LLMs之Llama3:基于Colab平台(免费T4-GPU)利用LLaMA-Factory的GUI界面(底层采用unsloth优化框架【加速训练5~30倍+减少50%的内存占用】)对llama-3-

LLMs之Llama3:基于Colab平台(免费T4-GPU)利用LLaMA-Factory的GUI界面(底层采用unsloth优化框架【加速训练5~30倍+减少50%的内存占用】)对llama-3-

    正在检查是否收录...

LLMs之Llama3:基于Colab平台(免费T4-GPU)利用LLaMA-Factory的GUI界面(底层采用unsloth优化框架【加速训练5~30倍+减少50%的内存占用】)对llama-3-8b-Instruct-bnb-4bit模型采用alpaca数据集实现CLI方式/GUI傻瓜可视化方式进行LoRA指令微调→模型推理测试→CLI方式合并权重

目录

基于colab平台(免费T4-GPU)利用LLaMA-Factory的GUI界面(底层采用unsloth优化框架【加速训练5倍~30+减少50%的内存占用】)对llama-3-8b-Instruct-bnb-4bit模型采用alpaca数据集实现CLI方式/GUI傻瓜可视化方式进行LoRA指令微调→模型推理测试→CLI方式合并权重

# 1、安装依赖

# 1.1、克隆 LLaMA-Factory 仓库并安装必要的 Python 包,包括 unsloth、xformers 和 bitsandbytes。

# 1.2、检查 GPU 环境,确保可以使用 Colab 的 Tesla T4 GPU。

# 2、更新身份数据集:

# 3、模型微调

# T1、通过 LLaMA Board 微调模型:

# T2、通过命令行微调模型:It takes ~30min for training.

# 4、模型推理

# 5、合并 LoRA 适配器并可选地上传模型:

实现代码

基于colab平台(免费T4-GPU)利用LLaMA-Factory的GUI界面(底层采用unsloth优化框架【加速训练5倍~30+减少50%的内存占用】)对llama-3-8b-Instruct-bnb-4bit模型采用alpaca数据集实现CLI方式/GUI傻瓜可视化方式进行LoRA指令微调→模型推理测试→CLI方式合并权重

# 1、安装依赖

# 1.1、克隆 LLaMA-Factory 仓库并安装必要的 Python 包,包括 unsloth、xformers 和 bitsandbytes。

# 1.2、检查 GPU 环境,确保可以使用 Colab 的 Tesla T4 GPU。

# 2、更新身份数据集:

# 读取并修改 identity.json 文件,替换其中的占位符为 “Llama-3” 和 “LLaMA Factory”。

# 这一步骤是为了个性化训练数据,确保模型能够生成与特定身份相关的回复。

# 3、模型微调

# T1、通过 LLaMA Board 微调模型:

# 使用 llamafactory-cli 命令行工具启动一个 Web UI 界面,可能用于监控和调整微调过程。

# T2、通过命令行微调模型:It takes ~30min for training.

# 定义微调参数,包括使用的模型、数据集、模板、微调类型(此处使用 LoRA 适配器)、输出目录、批处理大小、学习率调度器、日志步骤等。

# 使用 llamafactory-cli 命令行工具开始微调过程。

# 这一步使用的技术包括 LoRA 适配器(用于节省内存)、4位量化、LoRA+ 算法以及浮点16混合精度训练。

# 4、模型推理

# 推断微调后的模型:用于测试和验证微调后的模型性能。

# 设置模型参数,加载微调时使用的 LoRA 适配器,并初始化一个 ChatModel 实例。

# 通过 CLI 应用程序与模型交互,输入查询并接收模型的生成文本。

f

# 5、合并 LoRA 适配器并可选地上传模型:

# 定义参数以合并 LoRA 适配器到原始模型,并指定输出目录。

# 使用 llamafactory-cli 命令行工具执行导出操作。

# 注意,由于 Colab 免费版本内存限制,无法在此环境中合并 8B 的模型。

实现代码

源码地址:https://colab.research.google.com/drive/1eRTPn37ltBbYsISy9Aw2NuI2Aq5CQrD9?usp=sharing#scrollTo=kTESHaFvbNTr

 ''' Finetune Llama-3 with LLaMA Factory Please use a free Tesla T4 Colab GPU to run this! 4月22日 源码地址:https://colab.research.google.com/drive/1eRTPn37ltBbYsISy9Aw2NuI2Aq5CQrD9?usp=sharing#scrollTo=kTESHaFvbNTr ''' # 1、安装依赖 # 1.1、克隆 LLaMA-Factory 仓库并安装必要的 Python 包,包括 unsloth、xformers 和 bitsandbytes。 %cd /content/ %rm -rf LLaMA-Factory !git clone https://github.com/hiyouga/LLaMA-Factory.git %cd LLaMA-Factory %ls !pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" !pip install --no-deps xformers==0.0.25 !pip install .[bitsandbytes] # 1.2、检查 GPU 环境,确保可以使用 Colab 的 Tesla T4 GPU。 import torch try: assert torch.cuda.is_available() is True except AssertionError: print("Please set up a GPU before using LLaMA Factory: https://medium.com/mlearning-ai/training-yolov4-on-google-colab-316f8fff99c6") # 2、更新身份数据集: # 读取并修改 identity.json 文件,替换其中的占位符为 “Llama-3” 和 “LLaMA Factory”。 # 这一步骤是为了个性化训练数据,确保模型能够生成与特定身份相关的回复。 import json NAME = "Llama-3" AUTHOR = "LLaMA Factory" with open("data/identity.json", "r", encoding="utf-8") as f: dataset = json.load(f) for sample in dataset: sample["output"] = sample["output"].replace("{{"+ "name" + "}}", NAME).replace("{{"+ "author" + "}}", AUTHOR) with open("data/identity.json", "w", encoding="utf-8") as f: json.dump(dataset, f, indent=2, ensure_ascii=False) # 3、模型微调 # T1、通过 LLaMA Board 微调模型: # 使用 llamafactory-cli 命令行工具启动一个 Web UI 界面,可能用于监控和调整微调过程。 %cd /content/LLaMA-Factory/ !GRADIO_SHARE=1 llamafactory-cli webui # T2、通过命令行微调模型:It takes ~30min for training. # 定义微调参数,包括使用的模型、数据集、模板、微调类型(此处使用 LoRA 适配器)、输出目录、批处理大小、学习率调度器、日志步骤等。 # 使用 llamafactory-cli 命令行工具开始微调过程。 # 这一步使用的技术包括 LoRA 适配器(用于节省内存)、4位量化、LoRA+ 算法以及浮点16混合精度训练。 import json args = dict( stage="sft", # do supervised fine-tuning do_train=True, model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # use bnb-4bit-quantized Llama-3-8B-Instruct model dataset="identity,alpaca_gpt4_en", # use alpaca and identity datasets template="llama3", # use llama3 prompt template finetuning_type="lora", # use LoRA adapters to save memory lora_target="all", # attach LoRA adapters to all linear layers output_dir="llama3_lora", # the path to save LoRA adapters per_device_train_batch_size=2, # the batch size gradient_accumulation_steps=4, # the gradient accumulation steps lr_scheduler_type="cosine", # use cosine learning rate scheduler logging_steps=10, # log every 10 steps warmup_ratio=0.1, # use warmup scheduler save_steps=1000, # save checkpoint every 1000 steps learning_rate=5e-5, # the learning rate num_train_epochs=3.0, # the epochs of training max_samples=500, # use 500 examples in each dataset max_grad_norm=1.0, # clip gradient norm to 1.0 quantization_bit=4, # use 4-bit QLoRA loraplus_lr_ratio=16.0, # use LoRA+ algorithm with lambda=16.0 use_unsloth=True, # use UnslothAI's LoRA optimization for 2x faster training fp16=True, # use float16 mixed precision training ) json.dump(args, open("train_llama3.json", "w", encoding="utf-8"), indent=2) %cd /content/LLaMA-Factory/ !llamafactory-cli train train_llama3.json # 4、模型推理 # 推断微调后的模型:用于测试和验证微调后的模型性能。 # 设置模型参数,加载微调时使用的 LoRA 适配器,并初始化一个 ChatModel 实例。 # 通过 CLI 应用程序与模型交互,输入查询并接收模型的生成文本。 from llmtuner.chat import ChatModel from llmtuner.extras.misc import torch_gc %cd /content/LLaMA-Factory/ args = dict( model_name_or_path="unsloth/llama-3-8b-Instruct-bnb-4bit", # use bnb-4bit-quantized Llama-3-8B-Instruct model adapter_name_or_path="llama3_lora", # load the saved LoRA adapters template="llama3", # same to the one in training finetuning_type="lora", # same to the one in training quantization_bit=4, # load 4-bit quantized model use_unsloth=True, # use UnslothAI's LoRA optimization for 2x faster generation ) chat_model = ChatModel(args) messages = [] print("Welcome to the CLI application, use `clear` to remove the history, use `exit` to exit the application.") while True: query = input("\nUser: ") if query.strip() == "exit": break if query.strip() == "clear": messages = [] torch_gc() print("History has been removed.") continue messages.append({"role": "user", "content": query}) print("Assistant: ", end="", flush=True) response = "" for new_text in chat_model.stream_chat(messages): print(new_text, end="", flush=True) response += new_text print() messages.append({"role": "assistant", "content": response}) torch_gc() # 5、合并 LoRA 适配器并可选地上传模型: # 定义参数以合并 LoRA 适配器到原始模型,并指定输出目录。 # 使用 llamafactory-cli 命令行工具执行导出操作。 # 注意,由于 Colab 免费版本内存限制,无法在此环境中合并 8B 的模型。 # NOTE: the Colab free version has merely 12GB RAM, where merging LoRA of a 8B model needs at least 18GB RAM, thus you cannot perform it in the free version. # !huggingface-cli login import json args = dict( model_name_or_path="meta-llama/Meta-Llama-3-8B-Instruct", # use official non-quantized Llama-3-8B-Instruct model adapter_name_or_path="llama3_lora", # load the saved LoRA adapters template="llama3", # same to the one in training finetuning_type="lora", # same to the one in training export_dir="llama3_lora_merged", # the path to save the merged model export_size=2, # the file shard size (in GB) of the merged model export_device="cpu", # the device used in export, can be chosen from `cpu` and `cuda` #export_hub_model_id="your_id/your_model", # the Hugging Face hub ID to upload model ) json.dump(args, open("merge_llama3.json", "w", encoding="utf-8"), indent=2) %cd /content/LLaMA-Factory/ !llamafactory-cli export merge_llama3.json 

llamactoclijson适配器gpu命令行数据集chatguigit模型推理命令行工具idealpacacodingapptpuweb可视化
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-16396.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

ChatGPT登陆苹果全家桶: iOS 18、iPadOS 18和macOS Sequoia将全面支持

上一篇

AppleIntelligence发布!苹果发布大模型Siri

下一篇
  • 复制图片
按住ctrl可打开默认菜单