在 “小小容器” WasmEdge 里运行小小羊驼 llama 2

昨天,特斯拉前 AI 总监、OpenAI 联合创始人 Andrej Karpathy 开源了 llama2.c 。 只用 500 行纯 C 语言就能训练和推理 llama 2 模型的框架,没有任何繁杂的 python 依赖。这个项目一推出就受到大家的追捧,24 小时内 GitHub 收获 4000 颗星!

可是,C 编译的原生机器码不能跨平台,不安全,也不可被调度。这些问题使得它的应用场景非常有限。这时,一个大胆的想法油然而生!把 llama2.c 编译成 Wasm 在 WasmEdge 里运行!

​图片来自 https://github.com/karpathy/llama2.c

这么做的好处是:

  • 轻量级:一个 Wasm 文件只有几十 KB 大小,相比于 Python 镜像动辄几百上千 MB,差了一万倍。
  • 安全:沙箱机制,提供隔离性,适合多租户的云部署。
  • 可移植:Wasm 文件无需任何改变,可以在 x86, ARM, Apple, RISC-V 机器上运行
  • 性能:没有冷启动,且运行速度接近本机速度
  • 能够被 Docker 和 kuberbetes 等容器工具进行管理

下面,我们来具体看看是如何实现的。

先决条件

请参考 WasmEdge 的官方文档安装 WasmEdge runtime

复制代码
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s — -p /usr/local

准备 wasi-sdk

复制代码
export WASI_VERSION=20
export WASI_VERSION_FULL=${WASI_VERSION}.0
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz
tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz
export WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}
CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"

把 llama2.c 编译成 Wasm

复制代码
git clone https://github.com/karpathy/llama2.c.git
cd llama2.c
$CC run.c -D_WASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-process-clocks -o run.wasm

优化 wasm file 并且运行

这里我们将使用 WasmEdge 的 AOT 编译器对编译好的 Wasm 文件进行优化,以提升 Wasm 的性能。

复制代码
$ wget https://karpathy.ai/llama2c/model.bin -P out
$ wasmedgec run.wasm run-aot.wasm
[2023-07-24 16:39:52.851] [info] compile start
[2023-07-24 16:39:52.858] [info] verify start
[2023-07-24 16:39:52.862] [info] optimize start
[2023-07-24 16:39:53.251] [info] codegen start
[2023-07-24 16:39:53.608] [info] output start
[2023-07-24 16:39:53.611] [info] compile done
[2023-07-24 16:39:53.611] [info] output start

运行这个 wasm 文件

复制代码
$ wasmedge --dir .:. run-aot.wasm out/model.bin

输出如下:

复制代码
Once upon a time, there was a wealthy man. He lived in a big house with many things. The wealthy man liked to play in the fog.
One day, the wealthy man saw that the fog was increasing. The fog was getting stronger and the weight on the man's body made it hard to walk. The man said, "Oh no, I need to find a place to stop."
The wealthy man walked and walked, looking for a safe place. Soon, he found a small house. To his surprise, the house was full of toys and candy! The man said, "I found this house of good value. I can keep all the toys and candy in it." And from that day on, the wealthy man never played in the fog again.
<s>
 Once upon a time, there was a little girl named Lily. She loved to play with her toys and sing songs. One day, Lily's friend Timmy came over to play.
"Hi Lily, do you want to play with my new toy car?" asked Timmy.
"Yay, thank you!" replied Lily.
But after a while, Lily started to feel sleep
achieved tok/s: 30.738912

就是这样啦。 WasmEdge 也将逐步支持 Llama2 7B 及更大的 model。

最后。 如果你有兴趣使用 Wasm 作为 Python 的高性能替代品在生产环境中进行 AI 推理,请查看我们基于Rust 的库 mediapipe-rs。 这是 Google 的 mediapipe 模型。同时支持 TF Lite 和 Pytorch!

https://github.com/WasmEdge/mediapipe-rshttps://github.com/WasmEdge/mediapipe-rs

相关推荐
千宇宙航34 分钟前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
onceco1 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
森焱森2 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
jndingxin4 小时前
OpenCV CUDA模块设备层-----高效地计算两个 uint 类型值的带权重平均值
人工智能·opencv·计算机视觉
Sweet锦4 小时前
零基础保姆级本地化部署文心大模型4.5开源系列
人工智能·语言模型·文心一言
hie988945 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
晨同学03275 小时前
opencv的颜色通道问题 & rgb & bgr
人工智能·opencv·计算机视觉
蓝婷儿5 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
大千AI助手5 小时前
PageRank:互联网的马尔可夫链平衡态
人工智能·机器学习·贝叶斯·mc·pagerank·条件概率·马尔科夫链
小和尚同志6 小时前
Cline | Cline + Grok3 免费 AI 编程新体验
人工智能·aigc