【fastllm】学习框架,本地运行,速度还可以,可以成功运行chatglm2模型

news2024/10/5 15:35:57

1,关于 fastllm 项目

https://www.bilibili.com/video/BV1fx421k7Mz/?vd_source=4b290247452adda4e56d84b659b0c8a2

【fastllm】学习框架,本地运行,速度还可以,可以成功运行chatglm2模型

https://github.com/ztxz16/fastllm

🚀 纯c++实现,便于跨平台移植,可以在安卓上直接编译
🚀 ARM平台支持NEON指令集加速,X86平台支持AVX指令集加速,NVIDIA平台支持CUDA加速,各个平台速度都很快就是了
🚀 支持浮点模型(FP32), 半精度模型(FP16), 量化模型(INT8, INT4) 加速
🚀 支持多卡部署,支持GPU + CPU混合部署
🚀 支持Batch速度优化
🚀 支持并发计算时动态拼Batch
🚀 支持流式输出,很方便实现打字机效果
🚀 支持python调用
🚀 前后端分离设计,便于支持新的计算设备
🚀 目前支持ChatGLM系列模型,各种LLAMA模型(ALPACA, VICUNA等),BAICHUAN模型,QWEN模型,MOSS模型,MINICPM模型等

2,本地CPU编译也非常方便

git clone https://github.com/ztxz16/fastllm.git

cd fastllm
mkdir build
cd build
cmake .. -DUSE_CUDA=OFF
make -j

3,运行webui 可以进行交互问答

文件下载:
https://hf-mirror.com/huangyuyang/chatglm2-6b-int4.flm

./webui -p /data/home/test/hf_cache/chatglm2-6b-int4.flm
Load (200 / 200)
Warmup…
finish.

please open http://127.0.0.1:8081

在这里插入图片描述

也有打字效果,不知道是咋实现的。好像不是stream 方式的。

3,速度还可以,同时也支持其他的模型

文档地址:
https://github.com/ztxz16/fastllm/blob/master/docs/llama_cookbook.md

LLaMA 类模型转换参考

这个文档提供了了转换LLaMA同结构模型的方法。

LLaMA类模型有着基本相同的结构,但权重和prompt构造有差异。在fastllm中,通过转转模型时修改部分配置,实现对这些变体模型的支持、

声明

以下配置方案根据模型的源代码整理,不保证模型推理结果与原版完全一致。

修改方式

目前,转换脚本和两行加速方式均可用于llama类模型。但无论采用哪一种方式,都需要预留足够的内存(可以用swap空间)。

在float16模式下,转换时约需要4×参数量+1GB的空闲内存。

转换脚本

这里以支持推理各类Llama结构的基座模型为例,介绍如何应用本文档。

  • 方案一:修改转换脚本

以alpaca2flm.py为模板修改。在创建model之后添加:

    model = LlamaForCausalLM.from_pretrained(model_name).float()
    # config.json中定义了自己的model_type的需要添加
    conf = model.config.__dict__
    conf["model_type"] = "llama"
    # 接下来的部分各个Chat模型有差别,Base模型有的需要添加pre_prompt。
    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "", 
                     user_role = "", bot_role = "", history_sep = "", 
                     dtype = dtype)

其中,pre_promptuser_rolebot_rolehistory_sep分别为“开始的系统提示词(第一轮对话之前)”,“用户角色标志”,“用户话语结束标志及模型回复开始标志”,“两轮对话之间的分隔符”。

  • 方案二:修改config.json
    在下载的模型目录下,修改配置文件config.json中,修改"model_type"为llama,并增加下面的键-值对:
    "pre_prompt": "",
    "user_role": "",
    "bot_role": "",
    "history_sep":  "",

如需添加Token ID而非字符串(类似baichuan-chat模型),可以使用“<FLM_FIX_TOKEN_{ID}>”的格式添加。

  • 执行脚本
python3 tools/alpaca2flm.py [输出文件名] [精度] [原始模型名称或路径]

对齐tokenizer

如果想使fastllm模型和原版transformers模型基本一致,最主要的操作是对齐tokenizer。
如果模型使用了huggingface 加速版本的Tokenizers(即模型目录中包含tokenizer.json并优先使用),目前的转换脚本仅在从本地文件转换时,能够对齐tokenizer

注意检查原始tokenizer的encode()方法返回的结果前面是否会加空格。如果原始tokenizer没有加空格,则需要设置:

    conf["tokenizer_add_dummy_prefix"] = False

Base Model

一部分模型需要制定bos_token_id,假设bos_token_id为1则可以配置如下:

    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "<FLM_FIX_TOKEN_1>", 
                     user_role = "", bot_role = "", history_sep = "", 
                     dtype = dtype)

Chat Model

对Chat Model,同样是修改转换脚本,或修改模型的config.json,以下是目前常见的chat model的配置:

InternLM(书生)

  • internlm/internlm-chat-7b
  • internlm/internlm-chat-7b v1.1
  • internlm/internlm-chat-20b
    conf = model.config.__dict__
    conf["model_type"] = "llama"
    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "<s><s>", 
                     user_role = "<|User|>:", bot_role = "<eoh>\n<|Bot|>:", 
                     history_sep = "<eoa>\n<s>", dtype = dtype)

可以直接使用llamalike2flm.py脚本转换:

cd build
python3 tools/llamalike2flm.py internlm-7b-fp16.flm float16 internlm/internlm-chat-20b #导出float16模型
python3 tools/llamalike2flm.py internlm-7b-int8.flm int8 internlm/internlm-chat-20b #导出int8模型
python3 tools/llamalike2flm.py internlm-7b-int4.flm int4 internlm/internlm-chat-20b #导出int4模型
python3 tools/llamalike2flm.py internlm-7b-int4.flm float16 internlm/internlm-chat-7b #导出internlm-chat-7b float16模型

XVERSE

  • xverse/XVERSE-13B-Chat
  • xverse/XVERSE-7B-Chat
    conf = model.config.__dict__
    conf["model_type"] = "llama"
    conf["tokenizer_add_dummy_prefix"] = False
    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "", 
                     user_role = "Human: ", bot_role = "\n\nAssistant: ", 
                     history_sep = "<FLM_FIX_TOKEN_3>", dtype = dtype)

XVERSE-13B-Chat V1 版本需要对输入做NFKC规范化,fastllm暂不支持,因此需要使用原始tokenizer.

  • xverse/XVERSE-13B-256K

该模型没有将RoPE外推参数放到config中,因此需要手工指定:

    conf = model.config.__dict__
    conf["model_type"] = "llama"
    conf["rope_theta"] = 500000
    conf["rope_scaling.type"] = "dynamic"
    conf["rope_scaling.factor"] = 2.0
    conf["tokenizer_add_dummy_prefix"] = False
    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "", 
                     user_role = "Human: ", bot_role = "\n\nAssistant: ", 
                     history_sep = "<FLM_FIX_TOKEN_3>", dtype = dtype)

其他 llama1 系列

  • Vicuna v1.1 v1.3
    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt="A chat between a curious user and an artificial intelligence assistant. "
                                "The assistant gives helpful, detailed, and polite answers to the user's questions. "
                     user_role="USER: ", bot_role=" ASSISTANT:",  history_sep="<s>", dtype=dtype)
  • BiLLa
    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "\n", 
                     user_role = "Human: ", bot_role = "\nAssistant: ", 
                     history_sep = "\n", dtype = dtype)

llama2-chat

  • meta-llama/Llama-2-chat
ModelLlama2-chatLlama2-chat-hf
7Bmeta-llama/Llama-2-7b-chatmeta-llama/Llama-2-7b-chat-hf
13Bmeta-llama/Llama-2-13b-chatmeta-llama/Llama-2-13b-chat-hf
ModelCodeLlama-Instruct
7Bcodellama/CodeLlama-7b-Instruct-hf
13Bcodellama/CodeLlama-13b-Instruct-hf

官方示例代码中,可以不用系统提示语:

    torch2flm.tofile(exportPath, model, tokenizer, pre_prompt = "<FLM_FIX_TOKEN_1>", 
                     user_role = "[INST] ", bot_role = " [/INST]", 
                     history_sep = " <FLM_FIX_TOKEN_2><FLM_FIX_TOKEN_1>", dtype = dtype)

Llama-2系列支持系统提示语需要修改代码,单轮可以使用以下带有系统提示语的版本:

    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt = "<FLM_FIX_TOKEN_1>[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, " \
        "while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. " \
        "Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, " \
        "or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, " \
        "please don't share false information.\n<</SYS>>\n\n", 
                     user_role = " ", bot_role = " [/INST]", 
                     history_sep = " <FLM_FIX_TOKEN_2><FLM_FIX_TOKEN_1>", dtype = dtype)
  • ymcui/Chinese-Alpaca-2
ModelChinese-Alpaca-2Chinese-Alpaca-2-16K
7Bziqingyang/chinese-alpaca-2-7bziqingyang/chinese-alpaca-2-7b-16k
13Bziqingyang/chinese-alpaca-2-13bziqingyang/chinese-alpaca-2-13b-16k
    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt = "<FLM_FIX_TOKEN_1>[INST] <<SYS>>\nYou are a helpful assistant. 你是一个乐于助人的助手。\n<</SYS>>\n\n"
                     user_role = " ", bot_role = " [/INST]", 
                     history_sep = " <FLM_FIX_TOKEN_2><FLM_FIX_TOKEN_1>", dtype = dtype)

RUC-GSAI/YuLan-Chat

  • Full
    • YuLan-Chat-2-13B
  • Delta (需要原始LLaMA)
    • YuLan-Chat-1-65B-v2
    • YuLan-Chat-1-65B-v1
    • YuLan-Chat-1-13B-v1
    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt="The following is a conversation between a human and an AI assistant namely YuLan, developed by GSAI, Renmin University of China. " \
                                "The AI assistant gives helpful, detailed, and polite answers to the user's questions.\n",
                     user_role="[|Human|]:", bot_role="\n[|AI|]:", history_sep="\n", dtype=dtype)

WizardCoder

  • WizardCoder-Python-7B-V1.0
  • WizardCoder-Python-13B-V1.0
    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt="Below is an instruction that describes a task. " \
                                "Write a response that appropriately completes the request.\n\n",
                     user_role="### Instruction:\n", bot_role="\n\n### Response:", history_sep="\n", dtype=dtype)

Deepseek Coder

  • Deepseek-Coder-1.3B-Instruct
  • Deepseek-Coder-6.7B-Instruct
  • Deepseek-Coder-7B-Instruct v1.5
    torch2flm.tofile(exportPath, model, tokenizer, 
                     pre_prompt="<FLM_FIX_TOKEN_32013>	You are an AI programming assistant, utilizing the Deepseek Coder model, developed by Deepseek Company, " \
                                "and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, " \
                                "and other non-computer science questions, you will refuse to answer.\n",
                     user_role="### Instruction:\n", bot_role="\n### Response:\n", history_sep="\n<|EOT|>\n", dtype=dtype)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1508404.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【你也能从零基础学会网站开发】Web建站之javascript入门篇 Array数组

&#x1f680; 个人主页 极客小俊 ✍&#x1f3fb; 作者简介&#xff1a;web开发者、设计师、技术分享 &#x1f40b; 希望大家多多支持, 我们一起学习和进步&#xff01; &#x1f3c5; 欢迎评论 ❤️点赞&#x1f4ac;评论 &#x1f4c2;收藏 &#x1f4c2;加关注 数组概述 …

Vue+SpringBoot打造农家乐订餐系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 用户2.2 管理员 三、系统展示四、核心代码4.1 查询菜品类型4.2 查询菜品4.3 加购菜品4.4 新增菜品收藏4.5 新增菜品留言 五、免责说明 一、摘要 1.1 项目介绍 基于JAVAVueSpringBootMySQL的农家乐订餐系统&#xff0c…

RabbitMQ 安装登陆 提示:User can only log in via localhost

RabbitMQ默认提供了一个guest/guest用户&#xff0c;但是从3.3.0 版本以后该账号只能localhost登陆&#xff08;User can only log in via localhost&#xff09; 解决办法&#xff1a;通过命令行创建一个管理员账号 通过以下命令创建一个用户user&#xff0c;密码为user12345…

LVGL在VScode中安装模拟器运行配置笔记教程

1、LVGL模拟器工程搭建 LVGL(Light and Versatile Graphics Library,轻巧而多功能的图形库)是一个免费的开放源代码图形库,它提供创建具有易于使用的图形元素,精美的视觉效果和低内存占用的嵌入式GUI所需的一切。本文主要讲述如何实现在VScode中实现LVGL模拟器环境的搭建运行。…

开源是什么?——跟老吕学Python编程

开源是什么&#xff1f;——跟老吕学Python编程 开源是什么&#xff1f;开放源代码软件是什么&#xff1f;开源软件许可证是什么&#xff1f;开放源代码软件是什么&#xff1f;开放源代码的软件代表有什么&#xff1f;开放源代码软件与自由软件的概念 开源的定义是什么&#xf…

【Linux】第四十二站:线程局部存储与线程分离

一、线程的局部存储 1.实现多线程 如果我们想创建多线程&#xff0c;我们可以用下面的代码类似去实现 #include <iostream> #include <pthread.h> #include <string> #include <cstdlib> #include <unistd.h> #include <thread> #inclu…

AI智能答题系统是什么?

AI智能答题系统是一种基于人工智能技术的智能问答系统&#xff0c;旨在提供精准、高效的答题解答服务。该系统利用自然语言处理&#xff08;NLP&#xff09;、机器学习、知识图谱等多种技术&#xff0c;可以理解用户提出的问题&#xff0c;并在庞大的知识库或数据集中查找相关信…

鸿蒙报错:Hhvigor Update the SDKs by going to Tools > SDK Manager....

鸿蒙报错&#xff1a;Hhvigor Update the SDKs by going to Tools > SDK Manager… 打开setting里面的sdk&#xff0c;将API9工程下的全部勾上&#xff0c;应用下载 刚打开 js 和 Native 是没勾上的

LeetCode-102.题: 二叉树的层序遍历(原创)

【题目描述】 给你二叉树的根节点 root &#xff0c;返回其节点值的 层序遍历 。 &#xff08;即逐层地&#xff0c;从左到右访问所有节点&#xff09;。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;[[3],[9,20],[15,7]] 【题目链接…

Linux进程概念僵尸进程孤儿进程

文章目录 一、什么是进程二、进程的状态三、Linux是如何做的&#xff1f;3.1 R状态3.2 S状态3.3 D状态3.4 T状态3.5 t状态3.6 X状态3.7 Z状态 四、僵尸进程4.1 僵尸进程危害 五、孤儿进程 一、什么是进程 对于进程理解来说&#xff0c;在Windows上是也可以观察到的&#xff0c…

数据结构与算法第三套试卷小题

1.删除链表节点 **分析&#xff1a;**首先用指针变量q指向结点A的后继结点B&#xff0c;然后将结点B的值复制到结点A中&#xff0c;最后删除结点B。 2.时间复杂度的计算 **分析&#xff1a;**当涉及嵌套循环的时候&#xff0c;我们可以直接分析内层循环即可&#xff0c;看内…

sql注入基础学习

1.常用SQL语句 01、显示数据库 show databases&#xff1b; 02、打开数据库 use db name&#xff1b; 03、显示数据表 show tables&#xff1b; 04、显示表结构 describe table_name&#xff1b; 05、显示表中各字段信息&#xff0c;即表结构 show columns from table_nam…

【框架学习 | 第五篇】SpringMVC(常用注解、获取请求参数、域对象共享数据、拦截器、异常处理、上传/下载文件)

文章目录 1.SpringMVC简介1.1定义1.2主要组件1.3工作流程1.3.1简要流程1.3.2详细流程 1.4优缺点 2.常用注解3.获取请求参数3.1通过 HttpServletRequest 获取请求参数3.2通过控制器方法的形参获取请求参数3.2.1请求路径参数与方法形参一致3.2.2请求路径参数与方法形参不一致3.2.…

笔记本电脑使用时需要一直插电吗?笔记本正确的充电方式

随着科技的不断发展&#xff0c;笔记本电脑已经成为人们日常生活和工作中不可或缺的电子设备。而在使用笔记本电脑时&#xff0c;很多人会有一个疑问&#xff0c;那就是笔记本电脑使用时需要一直插电吗&#xff1f;本文将就此问题展开讨论。 不一定需要一直插电&#xff0c;如果…

C++:2024/3/11

作业1&#xff1a;编程 要求&#xff1a;提示并输入一个字符串&#xff0c;统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数 代码&#xff1a; #include <iostream>using namespace std;int main() {string str;cout << "请输入一个字…

sql-mysql可视化工具Workbench导入sql文件

mysql可视化工具Workbench导入sql文件 1、打开workbench2、导入sql文件3、第一行加上库名4、开始运行 1、打开workbench 2、导入sql文件 3、第一行加上库名 4、开始运行

参与Penpad launch任务,实现Penpad与Scroll的双空投

在比特币 ETF 、BTC 减半等利好消息的持续推动下&#xff0c;加密市场逐渐进入到新一轮牛市周期中。除了以太坊 Layer1 生态 TVL 不断飙升外&#xff0c;Layer2 赛道 TVL 也在不断飙升并且屡创新高。 而在牛市背景下&#xff0c;Layer2 空投所带来的财富效应预期正在被进一步拉…

IAB视频广告标准《数字视频和有线电视广告格式指南》之 目录和概述及视频配套广告 - 我为什么要翻译介绍美国人工智能科技公司IAB系列(2)

写在前面 谈及到中国企业走入国际市场&#xff0c;拓展海外营销渠道的时候&#xff0c;如果单纯依靠一个小公司去国外做广告&#xff0c;拉渠道&#xff0c;找代理公司&#xff0c;从售前到售后&#xff0c;都是非常不现实的。我们可以回想一下40年前&#xff0c;30年前&#x…

Facebook商城号为什么被封?如何防封?

由于Facebook商城的高利润空间&#xff0c;越来越多的跨境电商商家注意到它的存在。Facebook作为全球最大、用户量最大的社媒平台&#xff0c;同时也孕育了一个巨大的商业生态&#xff0c;包括广告投放、商城交易等。依托背后的大流量&#xff0c;Facebook商城起号较快&#xf…

接口测试\接口测试脚本之Jsoup解析HTML

第一次接触jsoup还是在处理收货地址的时候&#xff0c;当时在写一个下单流程&#xff0c;需要省市区id以及详细门牌号等等&#xff0c;因此同事介绍了jsoup,闲来无事&#xff0c;在此闲扯一番&#xff01; 1.我们来看下&#xff0c;什么是jsoup,先来看看官方文档是怎么说的&am…