实战精选 | 如何用 OpenVINO™ 在本地快速部署 Llama 3.2

点击蓝字

关注我们,让开发变得更有趣

作者 | 武卓  英特尔 AI 软件布道师

排版 | 吴紫琴

OpenVINO™

随着 Llama 3.2 的刚刚发布,最新的AI模型进展比以往更加易于获取。借助 OpenVINO™ 和 Optimum Intel 的无缝集成,你可以在本地 Intel 硬件上压缩、优化并运行这个强大的模型。在本指南中,我们将带你完成整个流程,从环境搭建到最终执行,帮助你以最少的努力充分发挥 Llama 3.2 的潜力。

OpenVINO™

在 AI PC 的集成 GPU 上运行 Llama 3.2

下载 OpenVINO™ GenAI 示例代码

安装最新版本及依赖项

使用 NNCF 下载并导出 Llama 3.2

运行模型

总结

第0步:为开发准备你的机器!

对于第一次使用的用户,建议你按照Wiki中的基本设置步骤(1、2和3)进行。

机器设置并准备就绪 =)

第1步:下载 OpenVINO™ GenAI 示例代码

使用 OpenVINO™ GenAI API 在 Windows 上运行 Llama 3.1 的最简单方法是使用提供的示例代码进行设置。

首先,克隆仓库:

git clone https://github.com/openvinotoolkit/openvino.genai.git

在仓库中,你会找到一个名为 chat_sample 的 Python 示例。这个简洁的示例可以让你用不到40行代码执行 Llama 3.2,并与用户进行对话。它是开始探索模型功能的简便途径。

OpenVINO™ GenAI 的 Python 示例中的聊天示例

下面是示例代码的预览:

#!/usr/bin/env python3
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


import argparse
import openvino_genai
def streamer(subword):
    print(subword, end='', flush=True)
    # Return flag corresponds whether generation should be stopped.
    # False means continue generation.
    return False




def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('model_dir')
    args = parser.parse_args()


    device = 'CPU'  # GPU can be used as well
    pipe = openvino_genai.LLMPipeline(args.model_dir, device)


    config = openvino_genai.GenerationConfig()
    config.max_new_tokens = 100


    pipe.start_chat()
    while True:
        prompt = input('question:\n')
        if 'Stop!' == prompt:
            break
        pipe.generate(prompt, config, streamer)


        print('\n----------')
    pipe.finish_chat()




if '__main__' == __name__:
    main()

接下来,让我们设置环境以处理模型的下载、转换和执行。

第2步:安装最新版本和依赖项

为了避免依赖冲突,建议创建一个单独的虚拟环境:

python -m venv openvino_venv

激活环境,

openvino_venv\Script\activate

现在,安装必要的依赖项:

python -m pip install --upgrade pip
pip install -U --pre openvino-genai openvino openvino-tokenizers[transformers] --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly 
pip install --extra-index-url https://download.pytorch.org/whl/cpu "git+https://github.com/huggingface/optimum-intel.git" "git+https://github.com/openvinotoolkit/nncf.git" "onnx<=1.16.1"

第3步:使用 NNCF 下载并导出 Llama 3.2

在 从Hugging Face 导出模型之前,确保你已在此处接受使用协议。

然后,使用以下命令下载并导出模型:

optimum-cli export openvino --model meta-llama/Meta-Llama-3.2-3B-Instruct --task text-generation-with-past --weight-format int4 --group-size 64 --ratio 1.0 --sym --awq --scale-estimation --dataset “wikitext2” --all-layers llama-3.2-3b-instruct-INT4

注:对于从 HuggingFace 网站上下载 Llama-3.2 模型的开发者,如果使用 Windows 系统,可以在 powershell 窗口中首先进行如下设置,再使用上述命令进行模型的下载、转换及压缩:

pip install -U huggingface_hub
set HF_ENDPOINT=https://hf-mirror.com

如果是 Linux 系统,在终端窗口中使用如下命令首先进行设置:

pip install -U huggingface_hub
export HF_ENDPOINT=https://hf-mirror.com

也可以直接在魔搭社区

https://www.modelscope.cn/models/LLM-Research/Llama-3.2-3B-Instruct

进行模型下载。

下载完成后,在上述模型转换压缩命令中将模型名替换为下载后的模型保存的路径,即将命令修改如下:

optimum-cli export openvino --model <your_model_path>/Llama-3.2-3B-Instruct --task text-generation-with-past --weight-format int4 --group-size 64 --ratio 1.0 --sym --awq --scale-estimation --dataset “wikitext2” --all-layers llama-3.2-3b-instruct-INT4

第四步:运行模型

你现在可以使用 OpenVINO™ 运行模型推理。运行以下命令:

python chat_sample.py ./llama-3.2-8b-instruct-INT4

默认情况下,示例在 CPU 上运行。要切换到 GPU,只需在 chat_sample.py 中更新 device 参数:

pipe = ov_genai.LLMPipeline(model_path, "GPU")

最后,在我的AI PC集成显卡以及英特尔ARC A770独立显卡上运行推理的情况!

结论

使用 OpenVINO™ 在本地运行 Llama 3.2 为开发人员提供了一种强大且高效的解决方案,能够最大限度地提升 Intel 硬件上的 AI 性能。通过这种设置,你可以享受更快的推理时间、更低的延迟和更少的资源消耗——所有这些只需最少的设置和编码工作。希望本指南能帮助你快速有效地开始。祝编码愉快!

Notices & Disclaimers

Performance varies by use, configuration, and other factors. Learn more on the Performance Index site.

(https://edc.intel.com/content/www/us/en/products/performance/benchmarks/overview/).

Performance results are based on testing as of dates shown in configurations and may not reflect all publicly available updates. See backup for configuration details. No product or component can be absolutely secure. Your costs and results may vary. Intel technologies may require enabled hardware, software or service activation.

© Intel Corporation. Intel, the Intel logo, and other Intel marks are trademarks of Intel Corporation or its subsidiaries.

OpenVINO™

---------------------------------------

*OpenVINO and the OpenVINO logo are trademarks of Intel Corporation or its subsidiaries.

-----------------------------

 OpenVINO 中文社区 

微信号 : openvinodev

B站:OpenVINO中文社区

“开放、开源、共创”

致力于通过定期举办线上与线下的沙龙、动手实践及开发者交流大会等活动,促进人工智能开发者之间的交流学习。

○ 点击 “ 在看 ”,让更多人看见

Llama2是一种基于Hadoop的分布式计算框架,可用于大规模数据处理和分析。本地部署Llama2可以提供更好的性能和灵活性,同时可以充分利用本地计算资源。 本地部署Llama2的步骤如下: 1. 确保已经安装和配置好Hadoop和YARN,因为Llama2需要依赖它们进行资源管理和任务调度。 2. 下载Llama2的安装包,并解压到本地的一个目录。 3. 修改Llama2的配置文件,指定正确的Hadoop和YARN的相关参数,例如ResourceManager的地址和端口号。 4. 启动Llama2的服务,可以通过命令行或者脚本来执行。启动成功后,可以通过Web界面查看Llama2的运行状态和各项指标。 5. 配置和提交任务。在Llama2中,可以使用YARN的API或者命令行工具来配置和提交任务。任务可以是MapReduce作业、Spark作业或者其他的计算任务。 Llama2的项目实战主要包括以下几个方面: 1. 数据处理和分析:可以使用Llama2来处理和分析大规模的数据集。通过配置和提交适当的任务,可以利用集群中的多个节点并行执行任务,从而加速数据处理过程。 2. 机器学习和深度学习:Llama2提供了丰富的机器学习和深度学习算法库,可以在分布式环境下运行。通过配置和提交相应的任务,可以训练和部署复杂的机器学习模型。 3. 实时流处理:Llama2还支持实时流处理,可以通过配置和提交流处理任务来处理实时数据流。借助于Hadoop和YARN的优势,可以实现高吞吐量和低延迟的实时数据处理。 总之,Llama2的本地部署和项目实战可以极大地提升大规模数据处理和分析的效率和灵活性。通过合理配置和提交任务,可以充分利用集群中的计算资源,加速任务执行并获得更好的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值