异构边缘上的快速、可移植的 Llama2 推理

Rust + Wasm 技术栈可以是 AI 推理中强大的 Python 替代方案。

与 Python 相比,Rust+ Wasm 应用程序的大小可以是 Python 的 1/100,速度可以提高 100 倍,最重要的是,可以在完全硬件加速的情况下安全地在任何地方运行,而无需对二进制代码进行任何更改。Rust 是 AGI 的语言[1]

我们创建了一个非常简单的 Rust 程序[2](40 行代码),以本机速度使用 llama2 模型进行推理。当编译为 Wasm 时,二进制应用程序[3](仅 2MB)可以在有着异构硬件加速器的设备之间完全移植。Wasm 运行时( WasmEdge[4] )还为云环境提供了安全可靠的执行环境。事实上,WasmEdge Runtime 还可以与容器工具无缝协作,[5]可以跨许多不同的设备编排和执行可移植应用程序。

用 2MB 应用运行大语言模型 Llama2

视频加载中...

这项工作基于 Georgi Gerganov[7]创建的 llama.cpp 项目[8]。我们采用了原始的 C++ 程序在 Wasm 上运行。它适用于 GGUF 格式的[9]模型文件。

在你的电脑上运行 Llama 2 模型

步骤 1. 安装 WasmEdge 以及 GGML 插件

在 Linux 或 Mac (M1/M2) 计算机上使用以下命令来安装所有内容。请参阅此处了解更多详细信息[10]

curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugin wasi_nn-ggml

步骤 2. 下载预构建的 Wasm 应用程序和模型

curl -LO https://github.com/second-state/WasmEdge-WASINN-examples/raw/master/wasmedge-ggml-llama-interactive/wasmedge-ggml-llama-interactive.wasm

你还需要下载 GGUF 格式的 llama2 模型。下面的示例下载了一个量化为 5 位权重、针对对话特别优化的 llama2 7B 模型(请在此处查看更多信息[11])。

curl -LO https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF/resolve/main/llama-2-7b-chat.Q5_K_M.gguf

步骤 3. 运行试试!

使用 WasmEdge 运行 Wasm 推理应用程序,并将 GGUF 模型传递给它。你现在可以输入问题与模型聊天。

wasmedge --dir .:. --nn-preload default:GGML:CPU:llama-2-7b-chat.Q5_K_M.gguf wasmedge-ggml-llama-interactive.wasm default

Question:

这是一个完整的对话示例。

wasmedge --dir .:. --nn-preload default:GGML:CPU:llama-2-7b-chat.Q5_K_M.gguf wasmedge-ggml-llama-interactive.wasm default

Question:
Who is the "father of the atomic bomb"?
Answer:
The "father of the atomic bomb" is a term commonly associated with physicist J. Robert Oppenheimer. Oppenheimer was a leading figure in the development of the atomic bomb during World War II, serving as the director of the Manhattan Project, which was responsible for the development and deployment of the first nuclear weapons. He is often referred to as the "father of the atomic bomb" due to his significant contributions to the field of nuclear physics and his leadership in the development of the bomb.
Question:
Was he a communist?
Answer:
J. Robert Oppenheimer was not a communist. While he was associated with the Manhattan Project, which was a secret government project, and was involved in the development of the atomic bomb, he was not a member of the Communist Party or had any known political affiliations with communism. Oppenheimer was a physicist and a scientist, and his primary focus was on the development of nuclear weapons during World War II.

可选:配置模型

你可以使用环境变量来配置模型执行。

例如,以下命令指定了上下文长度为 4千 token,这是 llama2 的标准,并且每个响应中的最大 token 数为 1k。它还告诉 WasmEdge 在 runtime 层面打印模型的日志和统计数据。

LLAMA_LOG=1 wasmedge --dir .:. --nn-preload default:GGML:CPU:llama-2-7b-chat.Q5_K_M.gguf wasmedge-ggml-llama-interactive.wasm default

llama_model_loader: loaded meta data with 19 key-value pairs and 291 tensors from llama-2-7b-chat.Q5_K_M.gguf (version GGUF V2 (latest))
llama_model_loader: - tensor    0:                token_embd.weight q5_K     [  4096, 32000,     1,     1 ]
... ...
llm_load_tensors: mem required  = 4560.96 MB (+  256.00 MB per state)
...................................................................................................
Question:
Who is the "father of the atomic bomb"?
llama_new_context_with_model: kv self size  =  256.00 MB
... ...
llama_print_timings:      sample time =     3.35 ms /   104 runs   (    0.03 ms per token, 31054.05 tokens per second)
llama_print_timings: prompt eval time =  4593.10 ms /    54 tokens (   85.06 ms per token,    11.76 tokens per second)
llama_print_timings:        eval time =  3710.33 ms /   103 runs   (   36.02 ms per token,    27.76 tokens per second)
Answer:
The "father of the atomic bomb" is a term commonly associated with physicist J. Robert Oppenheimer. Oppenheimer was a leading figure in the development of the atomic bomb during World War II, serving as the director of the Manhattan Project, which was responsible for the development and deployment of the first nuclear weapons. He is often referred to as the "father of the atomic bomb" due to his significant contributions to the field of nuclear physics and his leadership in the development of the bomb.

边缘上的 llama。图片由 Midjourney 生成。

为什么不选Python?

像 llama2 这样的大语言模型通常使用 Python 进行训练(例如PyTorch 、 Tensorflow和 JAX)。但使用 Python 进行推理应用(AI 中约 95% 的计算)将是一个严重的错误。

LLM 工具链中常用的 Python 包直接相互冲突。

因 LLVM、 Tensorflow和 Swift 语言而闻名的 Chris Lattner[18] 在最近一期创业播客中接受了精彩的采访[19]他讨论了为什么 Python 非常适合模型训练,但对于推理应用来说却是错误的选择。

Rust+ Wasm 的优点

Rust + Wasm 堆栈提供了统一的云计算基础设施,涵盖设备到边缘云、本地服务器和公共云。对于 AI 推理应用程序来说,它是 Python 堆栈的强大替代方案。难怪埃隆·马斯克说 Rust 是 AGI 的语言。

Rust 推理程序

我们的演示推理程序是用 Rust 编写的并且编译成了 Wasm。Rust 源代码[20]非常简单。它只有 40 行代码。Rust 程序管理用户输入,跟踪对话历史记录,将文本转换为 llama2 的聊天模板,并使用 WASI NN 标准 API 运行推理操作[21]


fn main() {
    let args: Vec = env::args().collect();
    let model_name: &str = &args[1];

    let graph =
        wasi_nn::GraphBuilder::new(wasi_nn::GraphEncoding::Ggml, wasi_nn::ExecutionTarget::AUTO)
            .build_from_cache(model_name)
            .unwrap();
    let mut context = graph.init_execution_context().unwrap();

    let system_prompt = String::from("<>You are a helpful, respectful and honest assistant. Always answer as short as possible, while being safe. <>");
    let mut saved_prompt = String::new();

    loop {
        println!("Question:");
        let input = read_input();
        if saved_prompt == "" {
            saved_prompt = format!("[INST] {} {} [/INST]", system_prompt, input.trim());
        } else {
            saved_prompt = format!("{} [INST] {} [/INST]", saved_prompt, input.trim());
        }

        // Set prompt to the input tensor.
        let tensor_data = saved_prompt.as_bytes().to_vec();
        context
            .set_input(0, wasi_nn::TensorType::U8, &[1], &tensor_data)
            .unwrap();

        // Execute the inference.
        context.compute().unwrap();

        // Retrieve the output.
        let mut output_buffer = vec![0u8; 1000];
        let output_size = context.get_output(0, &mut output_buffer).unwrap();
        let output = String::from_utf8_lossy(&output_buffer[..output_size]).to_string();
        println!("Answer:
{}", output.trim());

        saved_prompt = format!("{} {} ", saved_prompt, output.trim());
    }
}

要自行构建应用程序,只需安装 Rust 编译器及添加 wasm32-wasi 编译器目标。

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup target add wasm32-wasi

然后,查看源项目,并运行 Cargo 命令以从 Rust 源项目构建 Wasm 文件。

# Clone 源代码
git clone https://github.com/second-state/WasmEdge-WASINN-examples/
cd WasmEdge-WASINN-examples/wasmedge-ggml-llama-interactive/

# 构建 Rust 程序
cargo build --target wasm32-wasi --release

# 输出的 Wasm 结果文件
cp target/wasm32-wasi/release/wasmedge-ggml-llama-interactive.wasm .

在云端或边缘运行

一旦获得 Wasm 字节码文件,你可以将其部署在任何支持 WasmEdge Runtime 的设备上。你的设备上只需安装带有 GGML 插件的 WasmEdge[22]。我们目前有适用于通用 Linux、Ubuntu Linux 和 Mac M1/M2 的 GGML 插件。

基于 Llama.cpp[23],WasmEdge GGML 插件将自动利用设备上的一切硬件加速来运行你的 Llama2 模型。例如,GGML 插件的 Mac OS 版本使用 Metal API 在 M1/M2 的内置神经处理引擎上运行推理工作负载。GGML 插件的 Linux CPU 版本使用 OpenBLAS 库来自动检测和利用现代 CPU(如 AVX 和 SIMD)上的高级计算特性。

这就是我们在不牺牲性能的情况下实现跨异构 AI 硬件和平台的可移植性的方法。

下一步是什么

虽然 WasmEdge GGML 工具目前可用(并且确实得到我们的云原生客户使用),但它仍处于早期阶段。如果你有兴趣为开源项目做出贡献并塑造未来大语言模型推理基础设施的方向,那么不妨为以下一些唾手可得的成果进行贡献[24]

其它 AI 模型

作为轻量级、快速、可移植且安全的 Python 替代品,WasmEdge 和 WASI NN 能够围绕大语言模型以外的流行 AI 模型构建推理应用程序。例如,

边缘轻量级 AI 推理才刚刚开始!

关于 WasmEdge

WasmEdge 是轻量级、安全、高性能、可扩展、兼容OCI的软件容器与运行环境。目前是 CNCF 沙箱项目。WasmEdge 被应用在 SaaS、云原生,service mesh、边缘计算、边缘云、微服务、流数据处理等领域。

✨ GitHub:https://github.com/WasmEdge/WasmEdge

官网:https://wasmedge.org/

‍‍ Discord 群:https://discord.gg/U4B5sFTkFc

文档:https://wasmedge.org/docs

参考资料

[1]

Rust 是 AGI 的语言: https://blog.stackademic.com/why-did-elon-musk-say-that-rust-is-the-language-of-agi-eb36303ce341

[2]

Rust 程序: https://github.com/second-state/WasmEdge-WASINN-examples/tree/master/wasmedge-ggml-llama-interactive

[3]

二进制应用程序: https://github.com/second-state/WasmEdge-WASINN-examples/blob/master/wasmedge-ggml-llama-interactive/wasmedge-ggml-llama-interactive.wasm

[4]

WasmEdge: https://github.com/WasmEdge/WasmEdge

[5]

WasmEdge Runtime 还可以与容器工具无缝协作,: https://wasmedge.org/docs/start/build-and-run/docker_wasm

[7]

Georgi Gerganov: https://github.com/ggerganov

[8]

llama.cpp 项目: https://github.com/ggerganov/llama.cpp

[9]

GGUF 格式的: https://github.com/philpax/ggml/blob/gguf-spec/docs/gguf.md

[10]

请参阅此处了解更多详细信息: https://github.com/second-state/WasmEdge-WASINN-examples/blob/master/wasmedge-ggml-llama-interactive/README.md#requirement

[11]

请在此处查看更多信息: https://www.secondstate.io/articles/convert-pytorch-to-gguf/

[12]

Python 包具有复杂的依赖关系: https://x.com/santiviquez/status/1676677829751177219

[13]

数 GB: https://hub.docker.com/r/pytorch/pytorch/tags

[14]

数十 GB: https://github.com/pytorch/serve/issues/1420

[15]

慢达 35,000 倍。: https://www.modular.com/blog/how-mojo-gets-a-35-000x-speedup-over-python-part-1

[16]

委托给: https://x.com/gdb/status/1676726449934331904

[17]

非常适合演示,但很难: https://podcasts.apple.com/ph/podcast/expanding-ai-chip-capabilities-beyond-nvidia-with/id315114957?i=1000627798935

[18]

Chris Lattner: https://en.wikipedia.org/wiki/Chris_Lattner

[19]

精彩的采访: https://www.youtube.com/watch?v=ap0VLOPyGqM

[20]

Rust 源代码: https://github.com/second-state/WasmEdge-WASINN-examples/tree/master/wasmedge-ggml-llama-interactive

[21]

NN 标准 API 运行推理操作: https://github.com/WebAssembly/wasi-nn

[22]

带有 GGML 插件的 WasmEdge: https://github.com/second-state/WasmEdge-WASINN-examples/tree/master/wasmedge-ggml-llama-interactive#requirement

[23]

Llama.cpp: https://github.com/ggerganov/llama.cpp

[24]

进行贡献: https://wasmedge.org/docs/contribute/overview

[25]

llama.cpp: https://github.com/ggerganov/llama.cpp

[26]

mediapipe-rs 项目: https://github.com/WasmEdge/mediapipe-rs

[27]

Tensorflow 模型 mediapipe: https://developers.google.com/mediapipe

[28]

WasmEdge YOLO: https://github.com/WasmEdge/WasmEdge/issues/2768

[29]

YOLOv8: https://ultralytics.com/yolov8

[30]

WasmEdge ADAS 演示: https://github.com/second-state/WasmEdge-WASINN-examples/tree/master/openvino-road-segmentation-adas

[31]

WasmEdge Document AI 项目将为: https://github.com/WasmEdge/WasmEdge/issues/2356

展开阅读全文

页面更新:2024-02-24

标签:边缘   容器   应用程序   插件   模型   语言   快速   工具   程序   项目   设备

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top