手把手教你在树莓派4B上运行LLaMA 7B模子

打印 上一主题 下一主题

主题 833|帖子 833|积分 2499

弁言

LLaMA全称是Large Language Model Meta AI,是由Meta AI研究职员发布的一个预训练语言模子。与最近爆火的ChatGPT相比,LLaMA架构更小,但训练过程和单GPU推理速度更快,成本更低。本日在刷推特的时间偶然中看到了如许一条消息,@ggerganov在GitHub上发布了llama.cpp,使用了4-bit量化将模子尽可能缩小,并能在多种移动装备上运行。这我就不淡定了,正妙手里有个闲置的Raspberry Pi 4B 4GB版本,赶紧搞起。
   Added a new section to my post highlighting the advances we’ve already seen in the last two days: LLaMA runs on a 4GB RaspberryPi and a Pixel 6 phone now! https://t.co/pWOv6PP85b
— Simon Willison (@simonw) March 13, 2023
  配置树莓派

   假如你已经配置好了树莓派,且gcc版本为10以上的就可以跳过这一部分了
  以下是我配置树莓派的步骤
起首通过Raspberry Pi Imager刷入系统至SD卡 - 系统版本为Ubuntu Server 22.10 (64 bit) - 注意这里一定要使用64 bit的系统,不然后续可能无法编译
配置系统
直接用命令行升级软件包并安装相干依赖,这里给出参考命令
  1. sudo su
  2. apt-get update
  3. apt-get upgrade
  4. # 一些会用到的工具
  5. apt-get install gcc g++ python3 python3-pip
  6. # 安装python依赖
  7. python3 -m pip install torch numpy sentencepiece
复制代码
llama.cpp

起首可以先下载LLaMa模子,7B的模子大概是28GB左右,网速欠好的同砚可以提前开始下载,以免到时间还需要苦等。可以在网上搜索到走漏的下载磁力链接,用你喜好的任意P2P / Torrent软件下载即可。
接下来就可以进入llama.cpp按照里面的教程进行操作了。固然看似比力简单,但里面照旧有一些小坑的地方的。我在这里简单论述下我遇到的题目以及我是怎么办理的。
构建二进制

  1. # build this repo
  2. git clone https://github.com/ggerganov/llama.cpp
  3. cd llama.cpp
  4. make
复制代码
起首需要克隆仓库到本地,并进行make。我在这一步发现两个题目

  • Rasbian Buster自带的gcc版本较老(gcc 8)且无法升级,因此才需要在前面重装系统并安装最新版本的。现在我使用的是gcc 12。
  • make的时间提示不支持的选项。
makefile里面处理一些平台特定的flag的时间是读取系统平台并储存到UNAME_S,UNAME_P和UNAME_M里面,之后通过这三个参数判定编译器相干的选项。假如这里通过不了的话可以考虑看看makefile里相近平台的参数并指定。make UNAME_P=armv7 UNAME_M=armv7就可以指定为armv7的编译选项。

  • make的时间提示內联错误‘always_inline’ ``‘vdotq_s32’
这个题目比力坑爹。我一开始以为是编译器的题目,换了好几种选项都不可。后来发现是作者在03.14针对Apple Sillicon做的一个优化 Use vdotq_s32 to improve performance #67 - Merged。现在暂不清楚为什么树莓派无法处理这个,已经向作者反馈了。既然无法针对性的做处理,我实验了直接revert对应的commit。之后就没有编译错误可以继续了。在我报出这个题目之后两小时,另一位贡献者@Ronsor已经提出了一个PR去修复这个题目了 Don't use vdotq_s32 if it's not available #139,点赞。
预处理模子

  1. # obtain the original LLaMA model weights and place them in ./models
  2. ls ./models
  3. 65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
  4. # install Python dependencies
  5. python3 -m pip install torch numpy sentencepiece
  6. # convert the 7B model to ggml FP16 format
  7. python3 convert-pth-to-ggml.py models/7B/ 1
  8. # quantize the model to 4-bits
  9. ./quantize.sh 7B
复制代码
我们将前面下载下来的模子放到llama.cpp/models文件夹,主要包罗7B模子文件夹和tokenizer.model分词器模子。然后使用convert-pth-to-ggml.py进行预处理转换成FP16精度,末了使用./quantize.sh脚本进行4 bit量化以进一步缩小。
这一步主要遇到的这么两个题目

  • 使用scp从Mac上传文件到pi上,稍微配置了一会儿,主要是等候时间较长。
  • 在pi上运行convert-pth-to-ggml.py这一步的时间,消耗内存太大OOM进程直接被系统kill掉了。
简单看了下convert-pth-to-ggml.py,似乎都是做一些浮点精度的转换,末了生成的也是通用的模子格式ggml。于是我决定实验先用Mac做ggml转换,然后拷贝到Pi上作进一步的处理ggml-model-f16.bin。实操发现如许是可行的,Pi也可以成功的运行quantize.sh量化脚本。
使用llama.cpp

到这里我们的安装就已经竣事了,紧张又兴奋的使用时间开始了。先来跑一个简单的Hello World。
  1. ./main -m ./models/7B/ggml-model-q4_0.bin -p "Hello world!" -t 8 -n 512
复制代码
  1. main: seed = 167882008main: seed = 167882008main: seed = 1678820083
  2. llama_model_load: loading model from './models/7B/ggml-model-q4_0.bin' - please wait ...
  3. llama_model_load: n_vocab = 32000
  4. llama_model_load: n_ctx   = 512
  5. llama_model_load: n_embd  = 4096
  6. llama_model_load: n_mult  = 256
  7. llama_model_load: n_head  = 32
  8. llama_model_load: n_layer = 32
  9. llama_model_load: n_rot   = 128
  10. llama_model_load: f16     = 2
  11. llama_model_load: n_ff    = 11008
  12. llama_model_load: n_parts = 1
  13. llama_model_load: ggml ctx size = 4529.34 MB
  14. llama_model_load: memory_size =   512.00 MB, n_mem = 16384
  15. llama_model_load: loading model part 1/1 from './models/7B/ggml-model-q4_0.bin'
  16. llama_model_load: .................................... done
  17. llama_model_load: model size =  4017.27 MB / num tensors = 291
  18. system_info: n_threads = 4 / 4 | AVX = 0 | AVX2 = 0 | AVX512 = 0 | FMA = 0 | NEON = 1 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 0 | VSX = 0 |
  19. main: prompt: 'Hello world'
  20. main: number of tokens in prompt = 3
  21.      1 -> ''
  22. 10994 -> 'Hello'
  23.   3186 -> ' world'
  24. sampling parameters: temp = 0.800000, top_k = 40, top_p = 0.950000, repeat_last_n = 64, repeat_penalty = 1.300000
  25. Hello world! | Welcome to the new site.
  26. We've been working hard on a few updates around here and it feels really great to finally share this
  27. main: mem per token = 14368644 bytes
  28. main:     load time = 113492.53 ms
  29. main:   sample time =   987.31 ms
  30. main:  predict time = 3881120.50 ms / 121285.02 ms per token
  31. main:    total time = 4215260.00 ms
复制代码
我们可以看到,生成了这句30词的复兴,Pi用了1小时10分钟,生成速度约为121 s/Token。有可能是我编译出了题目,这个后续再修复一下吧。当然由于时间太慢了,我后面的几个用例都直接在Mac上运行了。
再跑一个官方例子
  1. ./main -m ./models/7B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -t 8 -n 512
复制代码
  1. Building a website can be done in 10 simple steps:
  2. Start with the end goal. Think about what your audience wants to achieve before they ever visit you site – and once there, how do these users interact on it? This is an important step as it will help formulate all of those great ideas into something that makes sense for their target marketplace!
  3. 2) Get Inspired & Develop a Mood Board: It's always good to have some kinda inspiration board. If you’re thinking about starting from scratch, use Pinterest or other sources (like Behance and Dribbble), as they will give your ideas a direction when it comes time for the visual design phase!
  4. 3) Strategize Your Content: Here's where things start to get tricky. You need some kinda content strategy in place here – do you have all of that information ready at hand? Now is also a good time as any to think about wireframes and how users will interact with your site once its built!
  5. 4) Do It With A Purpose: Are there particular goals for the user’s journey on this website, or simply trying to build an online presence? If so you may want to consider using marketing automation software like Hubspot that allows marketers and salespeople to work together better. This will help them organize your content into a more logical hierarchy – which is one of those things we love doing!
  6. 5) Visual Design: It's time for the fun part, where you think about colors (orange!) layout & typography, etcetera - and then start to design it using Illustrator or Photoshop. At this point in your project its important that all parties involved have a clear understanding of what needs to be done!
  7. 6) User Interface Design: This is when the UI designer comes into play – they should know how things interact with one another, understand user goals and create an experience so seamless it feels like second nature. The better this step goes down, you will end up having a much more engaging website at hand!
  8. 7) Coding Starts: After all of that planning is done here’s where the coding process begins – usually with HTML & CSS first and then followed by Javascript to make sure your site works flawlessly across multiple browsers.
  9. 8 ) Test, Test Again And Then Test Some More…: This phase can go on for a while (we know this from experience
  10. main: mem per token = 14434244 bytes
  11. main:     load time =  4570.61 ms
  12. main:   sample time =   448.29 ms
  13. main:  predict time = 138987.47 ms / 271.99 ms per token
  14. main:    total time = 146495.30 ms
复制代码
在Mac上运行的速度照旧非常快的(废话),生成512个词的复兴用了2分26秒,平均0.27 s/Token。
别的,仓库里还包罗了一个对于android装备的编译选项和实际运行的视频,有爱好的读者可以自己研究下。也有人实验在三星Galaxy S22 Ultra上运行这个模子,速度竟然可以到达1.2s/Token。
小结

现在整个项目照旧处于一个比力低级的形态,在RPi上生成速度相称慢,几乎不可用。别的,作者也在README中提到了一些限制

  • 现在很难判定量化是否影响了模子生成的质量,需要一些更严谨的基准测试。
  • 现在模子并没有使用MacOS提供的Accelerate框架,由于对于整个解码器中大部分张量外形来说,使用ARM_NEON的内部实现和使用Accelerate框架并没有什么性能差别。
除开性能之外,项目的指引还需要更加完善一些才能够更好的帮到各人。
但无论如何,在训练和运行语言模子渐渐要求海量算力的本日,能有如许一个模子让个人装备也能跑起来一个以致还挺好用的模子,照旧挺让人感动的。不知道是巧合照旧什么,第一个跑通的老兄在Raspberry Pi 4 4GB #58里面展示的例句是:“The first man on the moon was Neil Armstrong…" 总要有人实验,去发现,去做第一个登上月球的人,不是吗?
参考文献

假如你想进一步相识相干内容的话,可以阅读下面的文章


  • ChatGPT论文阅读系列-LLaMA: Open and Efficient Foundation Language Models
  • LLaMA快速上手指南: 便捷构建“本地版ChatGPT”
   作者:Mestrace
文章来源:github
  推荐阅读


  • 编译入门那些事儿(6):LLVM Metadata System
  • 聊聊 DiT 和 GenTron
  • 阿里提出Mamba in Mamba | 比现有SOTA提速10倍,GPU使用减少73.4%
  • 编译入门那些事儿(5):Jump Table 优化先容
更多嵌入式AI干货请关注嵌入式AI专栏。欢迎添加极术小姐姐微信(id:aijishu20)加入技能交流群,请备注研究方向。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

涛声依旧在

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表