首页 > 其他分享 >Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(五)基于已训练好的模型进行推理

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(五)基于已训练好的模型进行推理

时间:2024-05-26 19:00:12浏览次数:25  
标签:INFO 14 训练 05 模型 30 Llama

LlaMA 3 系列博客

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (一)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (二)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (三)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (四)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (五)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (六)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (七)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (八)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (九)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (十)

构建安全的GenAI/LLMs核心技术解密之大模型对抗攻击(一)

构建安全的GenAI/LLMs核心技术解密之大模型对抗攻击(二)

构建安全的GenAI/LLMs核心技术解密之大模型对抗攻击(三)

构建安全的GenAI/LLMs核心技术解密之大模型对抗攻击(四)

构建安全的GenAI/LLMs核心技术解密之大模型对抗攻击(五)

你好 GPT-4o!

大模型标记器之Tokenizer可视化(GPT-4o)

大模型标记器 Tokenizer之Byte Pair Encoding (BPE) 算法详解与示例

大模型标记器 Tokenizer之Byte Pair Encoding (BPE)源码分析

大模型之自注意力机制Self-Attention(一)

大模型之自注意力机制Self-Attention(二)

大模型之自注意力机制Self-Attention(三)

基于 LlaMA 3 + LangGraph 在windows本地部署大模型 (十一)

Llama 3 模型家族构建安全可信赖企业级AI应用之 Code Llama (一)

Llama 3 模型家族构建安全可信赖企业级AI应用之 Code Llama (二)

Llama 3 模型家族构建安全可信赖企业级AI应用之 Code Llama (三)

Llama 3 模型家族构建安全可信赖企业级AI应用之 Code Llama (四)

Llama 3 模型家族构建安全可信赖企业级AI应用之 Code Llama (五)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话(一)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话(二)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话(三)

大模型之深入理解Transformer位置编码(Positional Embedding)

大模型之深入理解Transformer Layer Normalization(一)

大模型之深入理解Transformer Layer Normalization(二)

大模型之深入理解Transformer Layer Normalization(三)

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(一)初学者的起点

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(二)矩阵操作的演练

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(三)初始化一个嵌入层

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(四)预先计算 RoPE 频率

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(五)预先计算因果掩码

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(六)首次归一化:均方根归一化(RMSNorm)

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(七) 初始化多查询注意力

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(八)旋转位置嵌入

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(九) 计算自注意力

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(十) 残差连接及SwiGLU FFN

大模型之一步一步使用PyTorch编写Meta的Llama 3代码(十一)输出概率分布 及损失函数计算

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(一)加载简化分词器及设置参数

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(二)RoPE 及注意力机制

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(三) FeedForward 及 Residual Layers

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(四) 构建 Llama3 类模型本身

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(五)训练并测试你自己的 minLlama3

大模型之使用PyTorch编写Meta的Llama 3实际功能代码(六)加载已经训练好的miniLlama3模型

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话 (四)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话 (五)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话 (六)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话 (七)

Llama 3 模型家族构建安全可信赖企业级AI应用之使用 Llama Guard 保护大模型对话 (八)

Llama 3 模型家族构建安全可信赖企业级AI应用之 CyberSecEval 2:量化 LLM 安全和能力的基准(一)

Llama 3 模型家族构建安全可信赖企业级AI应用之 CyberSecEval 2:量化 LLM 安全和能力的基准(二)

Llama 3 模型家族构建安全可信赖企业级AI应用之 CyberSecEval 2:量化 LLM 安全和能力的基准(三)

Llama 3 模型家族构建安全可信赖企业级AI应用之 CyberSecEval 2:量化 LLM 安全和能力的基准(四)

Llama 3 模型家族构建安全可信赖企业级AI应用之code shield(一)Code Shield简介

Llama 3 模型家族构建安全可信赖企业级AI应用之code shield(二)防止 LLM 生成不安全代码

Llama 3 模型家族构建安全可信赖企业级AI应用之code shield(三)Code Shield代码示例

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(一) LLaMA-Factory简介

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(二) LLaMA-Factory训练方法及数据集

大模型之Ollama:在本地机器上释放大型语言模型的强大功能

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(三)通过Web UI微调

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(四)通过命令方式微调

Llama模型家族之使用 Supervised Fine-Tuning(SFT)微调预训练Llama 3 语言模型(五) 基于已训练好的模型进行推理

基于已训练好的模型进行推理

from llamafactory.chat import ChatModel
from llamafactory.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()
  1. 导入必要的模块:

    • ChatModelllamafactory.chat 导入,用于创建聊天模型。
    • torch_gcllamafactory.extras.misc 导入,用于调用PyTorch的垃圾收集器,以释放不再使用的内存。
  2. 切换到LLaMA-Factory的目录:

    • 使用 %cd /content/LLaMA-Factory/ 切换到包含LLaMA模型和相关文件的目录。
  3. 设置模型参数:

    • args 是一个字典,包含了模型配置的参数。
    • model_name_or_path 指定了模型的名称或路径,这里使用的是经过bnb-4bit量化的Llama-3-8B-Instruct模型。
    • adapter_name_or_path 指定了保存的LoRA适配器的名称或路径。
    • template 指定了模型训练时使用的模板。
    • finetuning_type 指定了微调类型,这里使用的是LoRA。
    • quantization_bit 设置模型量化的位数,这里为4位。
    • use_unsloth 表示是否使用UnslothAI的LoRA优化,以加快生成速度。
  4. 创建聊天模型实例:

    • chat_model = ChatModel(args) 使用指定的参数创建 ChatModel 实例。
  5. 初始化消息列表:

    • messages 是一个空列表,用于存储用户和助手的消息。
  6. 打印欢迎信息并进入循环:

    • 循环中,程序会提示用户输入消息。
    • 如果用户输入 exit,则退出循环并结束程序。
    • 如果用户输入 clear,则清空消息列表,调用 torch_gc() 释放内存,并打印已清除历史记录的信息。
  7. 处理用户输入:

    • 用户输入的消息被添加到 messages 列表中。
    • 然后,程序调用 chat_model.stream_chat(messages) 来生成助手的响应。
    • 助手的响应被打印出来,并添加到 messages 列表中。
  8. 在循环结束后调用 torch_gc()

    • 程序再次调用 torch_gc() 以释放任何不再使用的内存。

这段代码演示了如何使用LLaMA模型创建一个简单的聊天应用程序,并通过量化和LoRA优化来提高性能。

官网提供的日志为:

/content/LLaMA-Factory
/usr/local/lib/python3.10/dist-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: 
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
  warnings.warn(
[INFO|tokenization_utils_base.py:2087] 2024-05-18 14:30:42,715 >> loading file tokenizer.json from cache at /root/.cache/huggingface/hub/models--unsloth--llama-3-8b-Instruct-bnb-4bit/snapshots/2950abc9d0b34ddd43fd52bbf0d7dca82807ce96/tokenizer.json
[INFO|tokenization_utils_base.py:2087] 2024-05-18 14:30:42,716 >> loading file added_tokens.json from cache at None
[INFO|tokenization_utils_base.py:2087] 2024-05-18 14:30:42,718 >> loading file special_tokens_map.json from cache at /root/.cache/huggingface/hub/models--unsloth--llama-3-8b-Instruct-bnb-4bit/snapshots/2950abc9d0b34ddd43fd52bbf0d7dca82807ce96/special_tokens_map.json
[INFO|tokenization_utils_base.py:2087] 2024-05-18 14:30:42,719 >> loading file tokenizer_config.json from cache at /root/.cache/huggingface/hub/models--unsloth--llama-3-8b-Instruct-bnb-4bit/snapshots/2950abc9d0b34ddd43fd52bbf0d7dca82807ce96/tokenizer_config.json
[WARNING|logging.py:314] 2024-05-18 14:30:43,133 >> Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
05/18/2024 14:30:43 - INFO - llamafactory.data.template - Replace eos token: <|eot_id|>
INFO:llamafactory.data.template:Replace eos token: <|eot_id|>
[INFO|configuration_utils.py:726] 2024-05-18 14:30:43,246 >> loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--unsloth--llama-3-8b-Instruct-bnb-4bit/snapshots/2950abc9d0b34ddd43fd52bbf0d7dca82807ce96/config.json
[INFO|configuration_utils.py:789] 2024-05-18 14:30:43,249 >> Model config LlamaConfig {
  "_name_or_path": "unsloth/llama-3-8b-Instruct-bnb-4bit",
  "architectures": [
    "LlamaForCausalLM"
  ],
  "attention_bias": false,
  "attention_dropout": 0.0,
  "bos_token_id": 128000,
  "eos_token_id": 128009,
  "hidden_act": "silu",
  "hidden_size": 4096,
  "initializer_range": 0.02,
  "intermediate_size": 14336,
  "max_position_embeddings": 8192,
  "model_type": "llama",
  "num_attention_heads": 32,
  "num_hidden_layers": 32,
  "num_key_value_heads": 8,
  "pretraining_tp": 1,
  "quantization_config": {
    "_load_in_4bit": true,
    "_load_in_8bit": false,
    "bnb_4bit_compute_dtype": "bfloat16",
    "bnb_4bit_quant_type": "nf4",
    "bnb_4bit_use_double_quant": true,
    "llm_int8_enable_fp32_cpu_offload": false,
    "llm_int8_has_fp16_weight": false,
    "llm_int8_skip_modules": null,
    "llm_int8_threshold": 6.0,
    "load_in_4bit": true,
    "load_in_8bit": false,
    "quant_method": "bitsandbytes"
  },
  "rms_norm_eps": 1e-05,
  "rope_scaling": null,
  "rope_theta": 500000.0,
  "tie_word_embeddings": false,
  "torch_dtype": "bfloat16",
  "transformers_version": "4.40.2",
  "use_cache": true,
  "vocab_size": 128256
}

05/18/2024 14:30:43 - INFO - llamafactory.model.utils.quantization - Loading ?-bit BITSANDBYTES-quantized model.
INFO:llamafactory.model.utils.quantization:Loading ?-bit BITSANDBYTES-quantized model.
05/18/2024 14:30:43 - INFO - llamafactory.model.patcher - Using KV cache for faster generation.
INFO:llamafactory.model.patcher:Using KV cache for faster generation.
05/18/2024 14:30:43 - INFO - llamafactory.model.adapter - Upcasting trainable params to float32.
INFO:llamafactory.model.adapter:Upcasting trainable params to float32.
05/18/2024 14:30:43 - INFO - llamafactory.model.adapter - Fine-tuning method: LoRA
INFO:llamafactory.model.adapter:Fine-tuning method: LoRA

标签:INFO,14,训练,05,模型,30,Llama
From: https://blog.csdn.net/duan_zhihua/article/details/139173930

相关文章

  • CV再放大招 | YOLOv10:毫秒级实时端到端目标检测开源模型
    〔探索AI的无限可能,微信关注“AIGCmagic”公众号,让智能科技点亮你的生活〕本文作者:AIGCmagic社区  猫先生5月25日凌晨,由清华多媒体智能组发布YOLOv10:毫秒级实时端到端目标检测开源模型。项目地址:https://github.com/THU-MIG/yolov10论文地址:https://arxiv.org/pdf/24......
  • 【元胞自动机】基于元胞自动机模拟社会力模型解决人员疏散问题附Matlab代码
    【元胞自动机】基于元胞自动机模拟社会力模型解决人员疏散问题附Matlab代码首先,元胞自动机(CellularAutomata,简称CA)是一种离散动力系统,由一个规则化的网络组成,每个元胞根据自身状态和周围邻居元胞的状态更新自身状态。CA模型已被广泛应用于模拟各种复杂系统,包括人群......
  • 代码随想录算法训练营第第18天 | 513.找树左下角的值 、112. 路径总和 、106.从中
    找树左下角的值本地递归偏难,反而迭代简单属于模板题,两种方法掌握一下题目链接/文章讲解/视频讲解:https://programmercarl.com/0513.找树左下角的值.html/***Definitionforabinarytreenode.*functionTreeNode(val,left,right){*this.val=(val===undef......
  • CSS盒模型
    盒子模型(boxmodel)无论是div,span,还是a都是盒子但是,图片,表单元素疑虑看作文本,它们并不是盒子。这个很好理解,比如说,一张图片里并不能放东西,它自己就是自己的内容盒子中的区域:一个盒子中主要的属性就5个:width,height,padding,border,margin。width和height:内容的宽度和高度pad......
  • 一起来聊聊大模型的token
    文章目录前言一、token是什么二、常用分词方法三、GPT-3的分词方式1.代码示例2.`Ġworld`和`world`的区别1)分词中的空格前缀2)后续计算中的区别3.为什么使用子词分词总结前言学习大模型的朋友肯定听说过大模型接口按token,自己编写代码的时候也经常看到tok......
  • [学习分享]基于matlab的新安江模型_01_模型介绍与蓄满产流
    写在前面的  最近笔者刚完成水文预报这门课的课程设计,课程设计要求根据课本自行实现新安江模型,完成径流模拟。现在课程设计已经基本全部做完,自己感觉做的也还不错,同时也因为蛮喜欢水文预报这门课的,所以想再对课程设计的整个过程做个整理分享出来,也希望能够帮助到一些困惑于......
  • [书生·浦语大模型实战营]——第三节:茴香豆:搭建你的 RAG 智能助理
    0.RAG概述定义:RAG(RetrievalAugmentedGeneration)技术,通过检索与用户输入相关的信息片段,并结合外部知识库来生成更准确、更丰富的回答。解决LLMs在处理知识密集型任务时可能遇到的挑战,如幻觉、知识过时和缺乏透明、可追溯的推理过程等。提供更准确的回答、降低推理成......
  • YOLOv8_pose训练流程-原理解析[关键点检测理论篇]
            本篇将介绍一下YOLOv8关键点检测网络的训练流程,同样在看此篇文章之前先去看一下预测流程[YOLOv8_pose预测流程-原理解析[关键点检测理论篇],还有目标检测任务的训练流程YOLOv8训练流程-原理解析[目标检测理论篇],这两篇都是前置课程,下图是YOLOv8实例分割的网络......
  • 重排模型DLCM
    论文名:LearningaDeepListwiseContextModelforRankingRefinement背景在搜索场景下,给定一个查询q,q和d特征的向量表示x(q,d),rank阶段的loss可以表示为:其中:Q是query的集合,D是doc集合,f是rank模型函数可以看到,传统的rank模型是一种point-wise的建模方法,没有考虑不同doc之间......
  • 2024年5月大语言模型论文推荐:模型优化、缩放到推理、基准测试和增强性能
    前一篇文章总结了关于计算机视觉方面的论文,这篇文章将要总结了2024年5月发表的一些最重要的大语言模型的论文。这些论文涵盖了塑造下一代语言模型的各种主题,从模型优化和缩放到推理、基准测试和增强性能。大型语言模型(llm)发展迅速,跟上这些领域新颖的研究将有助于引导模型的持续......