学做沪江网站要多久,简单网站开发实例,做响应式网站公司,wordpress 返回顶部功能本文截取自20万字的《PyTorch实用教程》#xff08;第二版#xff09;#xff0c;敬请关注#xff1a;《Pytorch实用教程》#xff08;第二版#xff09;《Pytorch实用教程》#xff08;第二版#xff09;无论是零基础入门#xff0c;还是CV、NLP、LLM项目应用#x…本文截取自20万字的《PyTorch实用教程》第二版敬请关注《Pytorch实用教程》第二版《Pytorch实用教程》第二版无论是零基础入门还是CV、NLP、LLM项目应用或是进阶工程化部署落地在这里都有。相信在本书的帮助下读者将能够轻松掌握 PyTorch 的使用成为一名优秀的深度学习工程师。 - TingsongYu/PyTorch-Tutorial-2ndhttps://github.com/TingsongYu/PyTorch-Tutorial-2nd
前言
baichuan作为首批开源的国产大语言模型具备7B和13B两个尺寸在多个子任务上有出色表现本节就来了解baichuan系列大模型。
Baichuan 简介
Baichuan开源大模型由百川智能研发目前最新开源版本为Baichuan2闭源版本为Baichuan3。
百川智能成立于2023年4月10日由前搜狗公司CEO王小川创立。公司以帮助大众轻松、吾普惠地获取世界知识和专业服务为使命致力于通过语言AI的突破构建中国最优秀的大模型底座。
Baichuan2提供7B13B两个尺寸具体如下
基座模型对齐模型对齐模型 4bits 量化7BBaichuan2-7B-BaseBaichuan2-7B-ChatBaichuan2-7B-Chat-4bits13BBaichuan2-13B-BaseBaichuan2-13B-ChatBaichuan2-13B-Chat-4bits
更多关于Baichuan的信息可查阅 公司首页百川大模型-汇聚世界知识 创作妙笔生花-百川智能 github: https://github.com/baichuan-inc 技术报告https://arxiv.org/abs/2309.10305
本地部署安装
第一步下载下载baichuan2代码
git clone https://github.com/baichuan-inc/Baichuan2
第二步下载7B-in4模型权重
git clone https://huggingface.co/baichuan-inc/Baichuan2-7B-Chat-4bits
也可以通过github desktop下载
第三步环境配置
根据Baichuan2中的 requirements.txt进行安装其中pytorch环境自行配置要求 pytorch ≥ 2.x
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
第四步报错处理
根据官方的教程安装了requirements.txt后报错通常会报错
init model ...
A matching Triton is not available, some optimizations will not be enabled
Traceback (most recent call last):File C:\Users\yts32\anaconda3\envs\pt220\lib\site-packages\xformers\__init__.py, line 55, in _is_triton_availablefrom xformers.triton.softmax import softmax as triton_softmax # noqaFile C:\Users\yts32\anaconda3\envs\pt220\lib\site-packages\xformers\triton\softmax.py, line 11, in moduleimport triton
ModuleNotFoundError: No module named triton
C:\Users\yts32\anaconda3\envs\pt220\lib\site-packages\bitsandbytes\cuda_setup\main.py:166: UserWarning: Welcome to bitsandbytes. For bug reports, please run
python -m bitsandbytes
如果是linux可以尝试
pip install bitsandbytes0.41.1 -q
pip install accelerate0.25.0 -q
参考自https://github.com/baichuan-inc/Baichuan2/issues/52
如果是windows可以尝试先下载bitsandbytes-windows版的0.41.1的安装包再手动安装。原因是通过pip install bitsandbytes只能获得linux的而windows的目前最高版本时0.37.x因此需要手动下载安装。
https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.41.1-py3-none-win_amd64.whl
pip install bitsandbytes-0.41.1-py3-none-win_amd64.whl
参考自https://github.com/baichuan-inc/Baichuan2/issues/35
如果报错TypeError: NoneType object is not subscriptable
pip install accelerate0.25.0 -q
如果报错auto-gptq 0.7.1 requires accelerate0.26.0, but you have accelerate 0.25.0 which is incompatible.
GitHub - AutoGPTQ/AutoGPTQ: An easy-to-use LLMs quantization package with user-friendly apis, based on GPTQ algorithm.
pip install auto-gptq0.6
第五步配置路径
model model.generation_config tokenizer三个的路径需要配置
def init_model():print(init model ...)model AutoModelForCausalLM.from_pretrained(rG:\04-model-weights\Baichuan2-7B-Chat-4bits,torch_dtypetorch.float16,device_mapauto,trust_remote_codeTrue)model.generation_config GenerationConfig.from_pretrained(rG:\04-model-weights\Baichuan2-7B-Chat-4bits)tokenizer AutoTokenizer.from_pretrained(rG:\04-model-weights\Baichuan2-7B-Chat-4bits,use_fastFalse,trust_remote_codeTrue)return model, tokenizer
第六步运行cli_demo.py
C:\Users\yts32\anaconda3\envs\chatglm\python.exe D:\github_desktop\Baichuan2\cli_demo.py
init model ...bin C:\Users\yts32\anaconda3\envs\chatglm\lib\site-packages\bitsandbytes\libbitsandbytes_cuda121.dll
欢迎使用百川大模型输入进行对话vim 多行输入clear 清空历史CTRLC 中断生成stream 开关流式生成exit 结束。用户你好Baichuan 2
你好今天我能为您提供什么帮助用户你是谁Baichuan 2我是百川大模型是由百川智能的工程师们创造的大语言模型我可以和人类进行自然交流、解答问题、协助创作帮助大众轻松、普惠的获得世界知识和专业服务。如果你有任何问题可以随时向我提问模型结构分析
Baichuan2的模型结构可通过如下UML类图了解其他更多模型结构可以参考前两节Qwen和ChatGLM的结构分析。
Prompt 结构分析
baichuan2的Prompt结构是经典的三角色设计包括system, user, assistant。
在示例代码中并没有给出system的预设需要分析源代码后才看到system可通过messages来维护。bichuan2中的messages等同于history的作用用于记录历史对话信息。
一个真实的messages如下
[{content: 你好, role: user},
{content: 你好今天我能为您提供什么帮助, role: assistant},
{content: 今天天气如何, role: user}]
特殊token处理
不同的角色之间通常用特殊token标记在baichun2代码中可通过generation_config中看到特殊token的index但对应的text没有显示给出。
\.cache\huggingface\modules\transformers_modules\Baichuan2-7B-Chat-4bits\generation_utils.py
# 以下代码是组装历史对话的代码段首先判断当前角色然后获取角色分隔token
for message in round:if message[role] user:round_tokens.append(model.generation_config.user_token_id)else:round_tokens.append(model.generation_config.assistant_token_id)
单轮推理长度限制
模型支持的上下文是4K这里包括输入输出4K在单轮对话时会对输入长度做限制。
首先预留2K是用于本轮对话的输出因此输入的最大长度为4K-2K2K。详细代码如下
max_input_tokens model.config.model_max_length - max_new_tokens
input_tokens input_tokens[-max_input_tokens:] # truncate left其中
model.config.model_max_lengt 4096
max_new_tokens 2048
参考自C:\Users\yts32\.cache\huggingface\modules\transformers_modules\Baichuan2-7B-Chat-4bits\generation_utils.py
对于输入超过2K的情况是会被向左截断。
显存与上下文长度分析
百川官方给出了字符串长度与token之间的换算的比例一般情况下Baichuan2大模型1个token约等于1.5个中文汉字。详见产品定价Docs
通过分析发现 在未进行对话时显存仅占用5.3GB 第一次对话时显存立即飙升到6.4GB 前2000字符显存消耗不高2000之后显存消耗激增 超过3500字符后同样出现了截断参考Qwen、ChatGLM的分析 统计代码如下完整代码cli_demo.py位于github
conversation_length sum([len(content[content]) for content in messages])
import subprocess
import json
result subprocess.run([gpustat, --json], stdoutsubprocess.PIPE)
output result.stdout.decode()
data json.loads(output)
used_memory data[gpus][0][memory.used]
f.writelines({}, {}\n.format(conversation_length, used_memory))
f.flush()
小结
本节对Baichuan2模型进行了本地部署安装并分析模型结构、prompt结构、推理上限机制、显存分析等内容有助于进一步理解LLM原理。
下一小节分析Yi。