草庐IT

FastChat开放,媲美ChatGPT的90%能力——从下载到安装、部署

蒋含竹 2023-06-27 原文

FastChat开放,媲美ChatGPT的90%能力——从下载到安装、部署

前言

  • 最近ChatGPT非常火,引爆了整个商业市场以及NLP学术界,但是ChatGPT很多东西都不开放,你也没法个人部署、研究
  • 于是很多大语言模型横空出世,在开放的大语言模型中,最近我认为效果很不错的是FastChat,基于LLaMA做了二次调参训练,据官方称能达到ChatGPT的90%的能力。(具体能否达到这个值,可以直接去他们的Demo上试试)
  • 相关链接
  • 注意
    • 安装教程时间为2023年4月8日,FastChat项目频繁更新,后续可能会有不一致的地方

两个前置软件

  • Git: https://git-scm.com/
    • 代码管理工具,用于等会从GitHub安装huggingface的transformers
    • 直接安装即可,不用改配置
  • Anaconda:https://www.anaconda.com/
    • Python依赖管理工具,用于做Python依赖环境管理,你也可以用其他的管理工具
    • 直接安装即可,不用改配置

创建FastChat虚拟环境

  • 首先,利用Anaconda为FastChat创建一个虚拟环境,执行命令$ conda create -n fastchat python=3.9
    • 如果是Windows系统,你需要先打开Anaconda Prompt
    • 官网要求Python版本要>= 3.8,注意不要安装错版本了
    • 如果Anaconda下载新环境的依赖较慢的话,你可以切换下清华源,命令如下(按顺序)
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
    conda config --set show_channel_urls yes
    
  • 切换到刚才创建好的fastchat虚拟环境,执行 $ conda activate fastchat
  • 这个时候你就切换到了我们为FastChat准备的Python虚拟环境了,接下来可以开始安装我们需要的一些依赖

安装PyTorch

  • 由于FastChat使用的是PyTorch深度学习框架,建议提前安装好PyTorch的依赖
    • 因为官方的源文件有申明PyTorch的依赖,虽然等下安装FastChat时会自动安装PyTorch,但是默认安装的不是带CUDA版本的,到时候重新弄麻烦
    • 而且还能不能指定自己需要的PyTorch版本
  • 具体怎么安装,请参考官方链接 https://pytorch.org/get-started/locally/
  • 安装命令样例
    • conda $ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
    • conda $ conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia
    • Pip $ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
    • Pip $ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu116
  • 安装完成后,建议进入Python命令行测试一下,下面是个样例
    (base) PS C:\Users\alion> conda activate fastchat
    (fastchat) PS C:\Users\alion> python
    Python 3.9.16 (main, Mar  8 2023, 10:39:24) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import torch
    >>> print(torch.__version__)
    1.13.1+cu116
    >>> print(torch.version.cuda)
    11.6
    >>>
    >>> exit()
    (fastchat) PS C:\Users\alion>
    

安装 FastChat

  • 官方有两种安装方式,
    1. 直接利用Pip安装
    2. 下载官方源码后安装
  • 第二种方式可能会出错,建议先直接用第一种(不过你都可以试试,哈哈)
    • 第二种的话,可以有更多的控制性,例如在pyproject.toml文件中移除掉你认为不需要的依赖,或是源码中有错误需要修改
    • 如果用源码装可能会出现错误
      • 信息大概是这样git clone --filter=blob:none --quiet https://github.com/huggingface/transformers.git ... exit code: 128
      • 你先去fastchat项目下,打开pyproject.toml文件,移除掉dependencies中的transformers,等下手动安装pip3 install git+https://github.com/huggingface/transformers
      • 当然这个也可能会卡住,多试几次吧😁
  • 直接执行Pip命令
    • 慢的话可以在Pip命令后面加个清华源 -i https://pypi.tuna.tsinghua.edu.cn/simple
    • 如果github没法访问,请记得开启你的专业工具
      # 安装FastChat
      pip3 install fschat
      
      # 安装 huggingface/transformers
      pip3 install git+https://github.com/huggingface/transformers
      
  • 请确保你已经安装好Git,并在命令行中能使用,否则安装huggingface/transformers时会报错
  • 我已经安装好了,可以看看输出样例
    (fastchat) PS C:\Users\alion> pip3 install fschat
    Requirement already satisfied: fschat in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (0.1.8)
    Requirement already satisfied: tokenizers>=0.12.1 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.13.3)
    Requirement already satisfied: requests in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (2.28.1)
    Requirement already satisfied: torch in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (1.13.1+cu116)
    Requirement already satisfied: wandb in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.14.2)
    Requirement already satisfied: uvicorn in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.21.1)
    Requirement already satisfied: sentencepiece in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.1.97)
    Requirement already satisfied: accelerate in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.18.0)
    Requirement already satisfied: numpy in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (1.24.1)
    Requirement already satisfied: fastapi in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (0.95.0)
    Requirement already satisfied: gradio==3.23 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (3.23.0)
    Requirement already satisfied: markdown2[all] in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from fschat) (2.4.8)
    # 太多了,中间的我就省略了
    Requirement already satisfied: fonttools>=4.22.0 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from matplotlib->gradio==3.23->fschat) (4.39.3)
    Requirement already satisfied: kiwisolver>=1.0.1 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from matplotlib->gradio==3.23->fschat) (1.4.4)
    Requirement already satisfied: svgwrite in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from wavedrom->markdown2[all]->fschat) (1.4.3)
    Requirement already satisfied: smmap<6,>=3.0.1 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb->fschat) (5.0.0)
    Requirement already satisfied: zipp>=3.1.0 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from importlib-resources>=3.2.0->matplotlib->gradio==3.23->fschat) (3.15.0)
    Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from jsonschema>=3.0->altair>=4.2.0->gradio==3.23->fschat) (0.19.3)
    Requirement already satisfied: uc-micro-py in c:\programdata\anaconda3\envs\fastchat\lib\site-packages (from linkify-it-py<3,>=1->markdown-it-py[linkify]>=2.0.0->gradio==3.23->fschat) (1.0.1)
    (fastchat) PS C:\Users\alion>
    

下载 LLaMA,并转换

  • 由于FastChat基于LLaMA做的二次调参训练,所以我们需要先拿到LLaMA模型文件
  • 官方的获取方式比较麻烦,需要你先填个表单,然后等他们回复,认为可以了才会给你文件
  • 不过幸运的是,国内有人共享了 LLaMA模型文件
  • 下载后,关于解压或是保存文件的路径中 ,请记得不要有中文、特殊符号等
  • 接下来,我们还需要转换一下LLaMA的文件,以便于构建FastChat对应的模型Vicuna(我资源不够,用的7B模型)
    1. 下载 huggingface/transformers 的源码 $ git clone https://github.com/huggingface/transformers.git
    2. 进入到项目下 $ cd transformers
    3. 利用huggingface/transformers中的代码,完成对于LLaMA的转换,示例$ python src/transformers/models/llama/convert_llama_weights_to_hf.py --input_dir D:/code/model/LLaMA --model_size 7B --output_dir D:/code/model/transformer_model_7b
      • 参数:--input_dir指定的是刚才你下载好的LLaMA文件地址,这个路径下有个tokenizer.model文件,请仔细核对一下
      • 参数:--model_size指定用哪个参数数量级的模型,7B代表的是70亿个参数的那个模型(如果你用的种子链接下载的话,还有13B/30B/65B的模型)
      • 参数:--output_dir 是转换后输出的路径,等下要用

生成FastChat对应的模型Vicuna

  • 接下来我们需要生成Vicuna模型,将原始的LLaMA weights转为Vicuna weights
  • 这个过程需要消耗大量的内存,CPU也是拉满😁,官方给出的参考值如下
    • Vicuna-13B 需要大约60GB内存
    • Vicuna-7B 需要大约30GB内存
  • 确实需要这么多,请准备好足够的内存空间
  • 命令样例如 python -m fastchat.model.apply_delta --base-model-path D:/code/model/transformer_model_7b --target-model-path D:/code/model/vicuna-7b --delta-path lmsys/vicuna-7b-delta-v0
    • 参数:--base-model-path指定的是上一步,我们转换好的LLaMA 文件路径
    • 参数:--target-model-path是接下来生成的Vicuna文件要存放的位置,稍后启动FastChat要用
    • 参数:--delta-path不用改
  • 上面的命令官方已更新(2023-04-13记录),最新安装的同学请换成下面这个:
    • python -m fastchat.model.apply_delta --base-model-path D:/code/model/transformer_model_7b --target-model-path D:/code/model/vicuna-7b --delta-path lmsys/vicuna-7b-delta-v1.1
  • 这个过程较长,需要等下,下面是我的控制台打印信息样例
(fastchat) PS D:\code\transformers> python -m fastchat.model.apply_delta --base-model-path D:/code/model/transformer_model_7b --target-model-path D:/code/model/vicuna-7b --delta-path lmsys/vicuna-7b-delta-v0
Loading base model
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████| 2/2 [00:03<00:00,  1.63s/it]
Loading delta
Downloading ()lve/main/config.json: 100%|█████████████████████████████████████████████| 619/619 [00:00<00:00, 103kB/s]
Downloading ()model.bin.index.json: 100%|████████████████████████████████████████| 26.8k/26.8k [00:00<00:00, 1.30MB/s]
Downloading ()l-00001-of-00002.bin: 100%|████████████████████████████████████████| 9.98G/9.98G [06:47<00:00, 24.5MB/s]
Downloading ()l-00002-of-00002.bin: 100%|████████████████████████████████████████| 3.50G/3.50G [02:29<00:00, 23.4MB/s]
Downloading shards: 100%|███████████████████████████████████████████████████████████████| 2/2 [09:19<00:00, 279.81s/it]
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████| 2/2 [00:03<00:00,  1.68s/it]
Downloading ()neration_config.json: 100%|████████████████████████████████████████████| 137/137 [00:00<00:00, 27.3kB/s]
Downloading ()okenizer_config.json: 100%|█████████████████████████████████████████████| 727/727 [00:00<00:00, 121kB/s]
Downloading tokenizer.model: 100%|███████████████████████████████████████████████████| 500k/500k [00:01<00:00, 411kB/s]
Downloading ()cial_tokens_map.json: 100%|█████████████████████████████████████████████| 411/411 [00:00<00:00, 103kB/s]
Applying delta
Applying delta: 100%|████████████████████████████████████████████████████████████████| 323/323 [00:22<00:00, 14.57it/s]
Saving target model
(fastchat) PS D:\code\transformers-main>

启动FastChat的命令行交互

  • 前序工作我们已经准备好了,接下来可以和Vicuna进行命令行式的交流了
  • 执行命令 $ pyhon -m fastchat.serve.cli --model-name D:\code\model\vicuna-7b
    • 如果显卡内存不够,你可以使用CPU $ python -m fastchat.serve.cli --model-name D:\code\model\vicuna-7b --device cpu
    • 如果有多张显卡,可通过--num-gpus 2来指定多张显卡
    • 官方给出的参考值如下
      • 使用显卡的情况下:Vicuna-13B大概需要28GB显存,Vicuna-7B大概需要14GB显存
      • 使用CPU的情况下:Vicuna-13B大概需要60GB内存,Vicuna-7B大概需要30GB内存
  • 上面的命令官方已更新(2023-04-13记录),最新安装的同学请换成下面这个:
    • $ pyhon -m fastchat.serve.cli --model-path D:\code\model\vicuna-7b
  • 稍等一会,就可以正式和Vicuna交流了
  • 下面是一个样例


  • 可以看出来7B模型还是有较大问题的,你可以到官方Demo地址中,拿这个问题问问13B模型

将模型部署为一个服务,提供Web GUI

  • 启动 controller
    • 新打开命令行,进入到fastchat环境 $ conda activate fastchat
    • 执行命令 $ python -m fastchat.serve.controller
  • 启动 model worker
    • 新打开命令行,进入到fastchat环境 $ conda activate fastchat
    • 执行命令 $ python -m fastchat.serve.model_worker --model-path D:\code\model\vicuna-7b
    • 同样,如果你显卡内存不够,需要使用CPU,在后面加上参数--device cpu
  • 关于controller和model worker的说明
    • controller用来控制协调Webserver的请求,将具体的任务转给model worker
    • model worker负责执行,可以部署多个,会自动链接到controller
  • 启动前可以测试一下
    • 新打开命令行,进入到fastchat环境 $ conda activate fastchat
    • 执行 $ python -m fastchat.serve.test_message --model-name vicuna-7b
  • 最后,启动 web server,执行 python -m fastchat.serve.gradio_web_server
  • 打开浏览器,访问地址 http://127.0.0.1:7860/

有关FastChat开放,媲美ChatGPT的90%能力——从下载到安装、部署的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  8. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  9. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  10. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

随机推荐