如何用 OpenVINO™ 在本地快速部署 Llama 3.2

种地  论坛元老 | 2024-10-13 06:22:55 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1837|帖子 1837|积分 5511

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
作者 | 武卓  英特尔 AI 软件布道师
随着 Llama 3.2 的刚刚发布,最新的AI模子进展比以往更加易于获取。借助 OpenVINO™ 和 Optimum Intel 的无缝集成,你可以在本地 Intel 硬件上压缩、优化并运行这个强大的模子。在本指南中,我们将带你完成整个流程,从环境搭建到最终执行,帮助你以最少的积极充实发挥 Llama 3.2 的潜力。
第0步:为开辟准备你的呆板!

对于第一次使用的用户,建议你按照Wiki中的基本设置步调(1、2和3)进行。
呆板设置并准备停当 =)
第1步:下载 OpenVINO™ GenAI 示例代码

使用 OpenVINO™ GenAI API 在 Windows 上运行 Llama 3.1 的最简单方法是使用提供的示例代码进行设置。
首先,克隆仓库:
  1. git clone https://github.com/openvinotoolkit/openvino.genai.git
复制代码
在仓库中,你会找到一个名为 chat_sample 的 Python 示例。这个简洁的示例可以让你用不到40行代码执行 Llama 3.2,并与用户进行对话。它是开始探索模子功能的轻便途径。
OpenVINO™ GenAI 的 Python 示例中的聊天示例
下面是示例代码的预览:
  1. #!/usr/bin/env python3# Copyright (C) 2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0import argparseimport openvino_genaidef streamer(subword):    print(subword, end='', flush=True)    # Return flag corresponds whether generation should be stopped.    # False means continue generation.    return Falsedef 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步:安装最新版本和依赖项

为了制止依赖辩说,建议创建一个单独的虚拟环境:
  1. python -m venv openvino_venv
复制代码
激活环境,
  1. openvino_venv\Script\activate
复制代码
现在,安装必要的依赖项:
  1. python -m pip install --upgrade pippip 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 导出模子之前,确保你已在此处担当使用协议。
然后,使用以下命令下载并导出模子:
  1. 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 窗口中首先进行如下设置,再使用上述命令进行模子的下载、转换及压缩:
  1. pip install -U huggingface_hubset HF_ENDPOINT=https://hf-mirror.com
复制代码
如果是 Linux 体系,在终端窗口中使用如下命令首先进行设置:
  1. pip install -U huggingface_hubexport HF_ENDPOINT=https://hf-mirror.com
复制代码
也可以直接在魔搭社区
https://www.modelscope.cn/models/LLM-Research/Llama-3.2-3B-Instruct
进行模子下载。
下载完成后,在上述模子转换压缩命令中将模子名更换为下载后的模子生存的路径,即将命令修改如下:
  1. 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™ 运行模子推理。运行以下命令:
  1. python chat_sample.py ./llama-3.2-8b-instruct-INT4
复制代码
默认情况下,示例在 CPU 上运行。要切换到 GPU,只需在 chat_sample.py 中更新 device 参数:
  1. 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.

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

种地

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表