Llama-2大模型本地部署研究与应用测试

这篇具有很好参考价值的文章主要介绍了Llama-2大模型本地部署研究与应用测试。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

        最近在研究自然语言处理过程中,正好接触到大模型,特别是在年初chatgpt引来的一大波AIGC热潮以来,一直都想着如何利用大模型帮助企业的各项业务工作,比如智能检索、方案设计、智能推荐、智能客服、代码设计等等,总得感觉相比传统的搜索和智能化辅助手段,大模型提供的方式更高效、直接和精准等,而且结合chat,能够实现多轮次的迭代,更接近或了解用户需求,提供更精准的答复。目前正在开展大模型部署应用测试,目前开源大模型主要就是Llama、ChatGLM大模型等,包括Llama-1和Llama-2,在其基础上的改进大模型有Chinese-LLaMA、OpenChineseLLaMA、Moss、baichuan等等,本文主要对原始Llama大模型进行了本地部署与测试,后续再逐步扩展,结合行业数据资源进行finetune,希望在开源模型的基础上对油气行业大模型构建有所帮助,Llama-2大模型部署及应用测试如下。

一、部署环境

环境:利用anaconda管理python环境
conda:conda 4.3.30
python:Python 3.10.4
cuda version:11.0,安装低于该版本的包即可,我安装的是cu102,GPU采用Tesla V100,详见GPU监测情况
env:/root/anaconda3/envs/torch/
require包如下,主要看torch、torchaudio、torchvision、transformers、uvicorn、fastapi、accelerate。

二、目前已部署的大模型和运行比较

Chinese-Llama-2-7b,运行速度慢,加载速度快
Chinese-Llama-2-7b-4bit,运行速度相对快,加载速度最快
chinese-alpaca-2-7b-hf,运行速度更快,加载速度慢
chinese-alpaca-2-13b-hf,运行速度更快,加载速度慢
open-chinese-llama-7b-patch,运行速度中等,加载速度慢

三、目前支持的运行方式:

1.控制台运行,详见chinese-llama2Test2.py,运行命令:python chinese-llama2Test2.py Chinese-Llama-2-7b
2.Rest服务运行,restful运行,详见restApi.py,运行命令:python restApi.py Chinese-Llama-2-7b
对于Rest服务的调用,主要用postman或DHC客户端模拟POST请求,Content-Type=application/json,post参数是json格式,如 {"prompt": "北京最佳的旅游时间", "history": []}

四、应用测试

1.单次测试代码

# 一次性访问
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
model_path = "model/Chinese-Llama-2-7b"
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

            If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""

prompt = instruction.format("用中文回答,When is the best time to visit Beijing, and do you have any suggestions for me?")
generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

2.输出结果

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试

 3.循环交互模式测试代码

#循环交互模式
import torch
import sys, getopt
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
if (__name__ == '__main__') or (__name__ == 'main'):
	# 检查参数个数
	argc = len(sys.argv)
	if (argc <= 1):
		print('missingParms' % locals())
		sys.exit()
	#处理命令行参数
	modelName = sys.argv[1]
	#model_path = "model/Chinese-Llama-2-7b"
	model_path = "model/"+modelName
	tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
	if model_path.endswith("4bit"): #支持q4的轻量化模型,选择对应模型即可。
		model = AutoModelForCausalLM.from_pretrained(
				model_path,
				torch_dtype=torch.float16,
				device_map='auto'
			)
	else:
		model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
	streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
	 
	instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
				If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""
	 
	while True:
		text = input("请输入提问 prompt\n")
		if text == "q":
			break
		prompt = instruction.format(text)
		generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

4.输出结果

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试

五、监测GPU的使用情况

命令:watch -n 1 -d nvidia-smi

 1.启动时的GPU状态

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试

 2.运行过程中的GPU状态

Llama-2大模型本地部署研究与应用测试,机器学习,后端,信息资源,llama,大模型,部署测试文章来源地址https://www.toymoban.com/news/detail-681631.html

到了这里,关于Llama-2大模型本地部署研究与应用测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 本地部署开源大模型的完整教程:LangChain + Streamlit+ Llama

    在过去的几个月里,大型语言模型(llm)获得了极大的关注,这些模型创造了令人兴奋的前景,特别是对于从事聊天机器人、个人助理和内容创作的开发人员。 大型语言模型(llm)是指能够生成与人类语言非常相似的文本并以自然方式理解提示的机器学习模型。这些模型使用广泛

    2024年02月11日
    浏览(47)
  • llama.cpp一种在本地CPU上部署的量化模型(超低配推理llama)

    前不久,Meta前脚发布完开源大语言模型LLaMA, 随后就被网友“泄漏”,直接放了一个磁力链接下载链接。 然而那些手头没有顶级显卡的朋友们,就只能看看而已了 但是 Georgi Gerganov 开源了一个项目llama.cpp ggerganov/llama.cpp: Port of Facebook’s LLaMA model in C/C++ (github.com) 次项目的牛逼

    2023年04月23日
    浏览(40)
  • Llama 及 中文Alpaca模型部署测试

    环境: Xeon  E5-2680v4 16C 40G RAM WinServer 2019 Standard Edition Python 3.10 依赖库: accelerate==0.18.0 anyio==3.5.0 argon2-cffi==21.3.0 argon2-cffi-bindings==21.2.0 asttokens==2.0.5 attrs==22.1.0 Babel==2.11.0 backcall==0.2.0 beautifulsoup4==4.12.2 bleach==4.1.0 brotlipy==0.7.0 certifi==2022.12.7 cffi==1.15.1 chardet==5.1.0 charset-normalizer==3.1.

    2024年02月09日
    浏览(49)
  • 【个人笔记本】本地化部署详细流程 LLaMA中文模型:Chinese-LLaMA-Alpaca-2

    不推荐小白,环境配置比较复杂 下载原始模型:Chinese-LLaMA-Alpaca-2 linux部署llamacpp环境 使用llamacpp将Chinese-LLaMA-Alpaca-2模型转换为gguf模型 windows部署Text generation web UI 环境 使用Text generation web UI 加载模型并进行对话 笔记本环境: 操作系统:win11 CPU:AMD R7535HS GPU:笔记本4060显卡

    2024年02月08日
    浏览(51)
  • llama.cpp LLM模型 windows cpu安装部署;运行LLaMA-7B模型测试

    参考: https://www.listera.top/ji-xu-zhe-teng-xia-chinese-llama-alpaca/ https://blog.csdn.net/qq_38238956/article/details/130113599 cmake windows安装参考:https://blog.csdn.net/weixin_42357472/article/details/131314105 1、下载: 2、编译 3、测试运行 参考: https://zhuanlan.zhihu.com/p/638427280 模型下载: https://huggingface.co/nya

    2024年02月15日
    浏览(46)
  • 如何拥有自己的专属GPT-本地部署目前最强大模型llama3

    你是不是苦于没法使用ChatGPT?或者访问了ChatGPT却没法使用GPT4?现在一切问题都可以解决了! 4月18日,Meta发布两款开源Llama 3 8B与Llama 3 70B模型,供外部开发者免费使用。这个消息轰动了全球开发者。按照Meta的说法,Llama 3 8B和Llama 3 70B是目前同体量下,性能最好的开源模型。

    2024年04月26日
    浏览(43)
  • AI-windows下使用llama.cpp部署本地Chinese-LLaMA-Alpaca-2模型

    生成的文件在 .buildbin ,我们要用的是 main.exe , binmain.exe -h 查看使用帮助 本项目基于Meta发布的可商用大模型Llama-2开发,是中文LLaMAAlpaca大模型的第二期项目,开源了中文LLaMA-2基座模型和Alpaca-2指令精调大模型。这些模型在原版Llama-2的基础上扩充并优化了中文词表,使用

    2024年04月25日
    浏览(42)
  • 基于llama.cpp学习开源LLM本地部署

    目录 前言 一、llama.cpp是什么? 二、使用步骤 1.下载编译llama.cpp 2. 普通编译 3. BLAS编译 3.1、OpenBLAS 编译 CPU版 3.2 cuBLAS 编译GPU版本 4. 模型量化 4.1、模型文件下载:

    2024年01月21日
    浏览(37)
  • llama.cpp LLM模型 windows cpu安装部署;运行LLaMA2模型测试

    参考: https://www.listera.top/ji-xu-zhe-teng-xia-chinese-llama-alpaca/ https://blog.csdn.net/qq_38238956/article/details/130113599 cmake windows安装参考:https://blog.csdn.net/weixin_42357472/article/details/131314105 1、下载: 2、编译 3、测试运行 参考: https://zhuanlan.zhihu.com/p/638427280 模型下载: https://huggingface.co/nya

    2024年02月16日
    浏览(38)
  • 【类ChatGPT】本地CPU部署中文羊驼大模型LLaMA和Alpaca

    昨天在github上看到一个在本地部署中文大模型的项目,和大家分享一下。先把地址po出来。 项目名称:中文LLaMAAlpaca大语言模型+本地部署 (Chinese LLaMA Alpaca LLMs) 项目地址:https://github.com/ymcui/Chinese-LLaMA-Alpaca 以下是原github中给出的体验GIF,可以看到这个模型还是具备一定的指令

    2023年04月23日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包