当前位置: 首页 > news >正文

潮州外贸网站建设医院网站建设情况说明

潮州外贸网站建设,医院网站建设情况说明,蓬安网站建设,酒店网站怎么做2023年的深度学习入门指南(22) - 百川大模型13B的运行及量化 不知道上一讲的大段代码大家看晕了没有。但是如果你仔细看了会发现#xff0c;其实代码还是不全的。比如分词器我们就没讲。 另外#xff0c;13B比7B的改进点也没有讲。 再有#xff0c;对于13B需要多少显存我们…2023年的深度学习入门指南(22) - 百川大模型13B的运行及量化 不知道上一讲的大段代码大家看晕了没有。但是如果你仔细看了会发现其实代码还是不全的。比如分词器我们就没讲。 另外13B比7B的改进点也没有讲。 再有对于13B需要多少显存我们也没说。13B光是模型加载就需要26GB的显存加上推理需要的消i耗没有个28GB以上的显存是比较悬的。恰好24GB的3090和4090单卡不够用。 我们先从应用讲起。 百川13b的命令行交互 百川官方在13b的开源代码中给我们提供了命令行交互式的应用和Web服务的基本框架。 我们先来看看命令行交互式的应用。 import os import torch import platform from colorama import Fore, Style from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation.utils import GenerationConfigdef init_model():print(init model ...)model AutoModelForCausalLM.from_pretrained(baichuan-inc/Baichuan-13B-Chat,torch_dtypetorch.float16,device_mapauto,trust_remote_codeTrue)model.generation_config GenerationConfig.from_pretrained(baichuan-inc/Baichuan-13B-Chat)tokenizer AutoTokenizer.from_pretrained(baichuan-inc/Baichuan-13B-Chat,use_fastFalse,trust_remote_codeTrue)return model, tokenizerdef clear_screen():if platform.system() Windows:os.system(cls)else:os.system(clear)print(Fore.YELLOW Style.BRIGHT 欢迎使用百川大模型输入进行对话clear 清空历史CTRLC 中断生成stream 开关流式生成exit 结束。)return []def main(streamTrue):model, tokenizer init_model()messages clear_screen()while True:prompt input(Fore.GREEN Style.BRIGHT \n用户 Style.NORMAL)if prompt.strip() exit:breakif prompt.strip() clear:messages clear_screen()continueprint(Fore.CYAN Style.BRIGHT \nBaichuan Style.NORMAL, end)if prompt.strip() stream:stream not streamprint(Fore.YELLOW ({}流式生成)\n.format(开启 if stream else 关闭), end)continuemessages.append({role: user, content: prompt})if stream:position 0try:for response in model.chat(tokenizer, messages, streamTrue):print(response[position:], end, flushTrue)position len(response)if torch.backends.mps.is_available():torch.mps.empty_cache()except KeyboardInterrupt:passprint()else:response model.chat(tokenizer, messages)print(response)if torch.backends.mps.is_available():torch.mps.empty_cache()messages.append({role: assistant, content: response})print(Style.RESET_ALL)if __name__ __main__:main()调用模型的部分大家都比较熟悉了这里唯一值得说一说的反而是显示格式相关的colorama库。 print(Fore.YELLOW Style.BRIGHT 欢迎使用百川大模型输入进行对话clear 清空历史CTRLC 中断生成stream 开关流式生成exit 结束。) ...prompt input(Fore.GREEN Style.BRIGHT \n用户 Style.NORMAL)系统提示为黄色而用户输入为绿色百川的回复为青色。 看起来百川的同学是写过前端的都用一个颜色太乱忍不了。 安装时别忘了安装colorama库。或者按下面的列表装全了吧 pip install transformers pip install sentencepiece pip install accelerate pip install transformers_stream_generator pip install colorama pip install cpm_kernels pip install streamlit百川13b的Web服务demo 百川的Web demo里关于模型的调用部分还是没啥可讲的。 但是Streamlit的前端有必要简单说一下。 Streamlit封装了很多常用的前端组件比如对话这样的高级组件就是用st.chat_message()来实现的。 我们来看个例子 import streamlit as stwith st.chat_message(assistant, avatar):st.markdown(您好我是百川大模型很高兴为您服务)我们把上面的文件存为test1.py然后在命令行运行 streamlit run test1.py运行之后会自动打开浏览器看到如下界面 with st.chat_message(assistant, avatar):这一行创建了一个聊天消息的上下文管理器消息的发送者是 “assistant”并且使用了一个机器人表情作为头像‘’。 st.markdown(您好我是百川大模型很高兴为您服务)这行代码在上述的 “assistant” 聊天消息中添加了一段 Markdown 格式的文本。 好下面我们把用户输入的功能加进来使用st.chat_input()就可以实现不需要写javascript代码 import streamlit as stwith st.chat_message(assistant, avatar):st.markdown(您好我是百川大模型很高兴为您服务)if prompt : st.chat_input(Shift Enter 换行, Enter 发送):with st.chat_message(user, avatar‍):st.markdown(prompt)运行效果如下 我们可以进一步给页面加上标题和属性 import streamlit as stst.set_page_config(page_titleBaichuan-13B-Chat) st.title(Baichuan-13B-Chat)with st.chat_message(assistant, avatar):st.markdown(您好我是百川大模型很高兴为您服务)if prompt : st.chat_input(Shift Enter 换行, Enter 发送):with st.chat_message(user, avatar‍):st.markdown(prompt)理解了上面的基础知识之后我们就直接看百川的代码吧 import json import torch import streamlit as st from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation.utils import GenerationConfigst.set_page_config(page_titleBaichuan-13B-Chat) st.title(Baichuan-13B-Chat)st.cache_resource def init_model():model AutoModelForCausalLM.from_pretrained(baichuan-inc/Baichuan-13B-Chat,torch_dtypetorch.float16,device_mapauto,trust_remote_codeTrue)model.generation_config GenerationConfig.from_pretrained(baichuan-inc/Baichuan-13B-Chat)tokenizer AutoTokenizer.from_pretrained(baichuan-inc/Baichuan-13B-Chat,use_fastFalse,trust_remote_codeTrue)return model, tokenizerdef clear_chat_history():del st.session_state.messagesdef init_chat_history():with st.chat_message(assistant, avatar):st.markdown(您好我是百川大模型很高兴为您服务)if messages in st.session_state:for message in st.session_state.messages:avatar ‍ if message[role] user else with st.chat_message(message[role], avataravatar):st.markdown(message[content])else:st.session_state.messages []return st.session_state.messagesdef main():model, tokenizer init_model()messages init_chat_history()if prompt : st.chat_input(Shift Enter 换行, Enter 发送):with st.chat_message(user, avatar‍):st.markdown(prompt)messages.append({role: user, content: prompt})print(f[user] {prompt}, flushTrue)with st.chat_message(assistant, avatar):placeholder st.empty()for response in model.chat(tokenizer, messages, streamTrue):placeholder.markdown(response)if torch.backends.mps.is_available():torch.mps.empty_cache()messages.append({role: assistant, content: response})print(json.dumps(messages, ensure_asciiFalse), flushTrue)st.button(清空对话, on_clickclear_chat_history)if __name__ __main__:main()量化 如果想要在消费级的单卡上运行百川13b的推理需要对模型进行量化。 百川13b支持8位和4位的量化。8位量化之后需要18.6G以上的显存。4位量化之后需要11.5GB以上的显存。同时CPU在实现量化的时候需要36.1G的内存32G的不太够用。 我们先看下8位量化的例子 import torch from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation.utils import GenerationConfig tokenizer AutoTokenizer.from_pretrained(baichuan-inc/Baichuan-13B-Chat, use_fastFalse, trust_remote_codeTrue) model AutoModelForCausalLM.from_pretrained(baichuan-inc/Baichuan-13B-Chat, torch_dtypetorch.float16, trust_remote_codeTrue) model.generation_config GenerationConfig.from_pretrained(baichuan-inc/Baichuan-13B-Chat) model model.quantize(8).cuda() messages [] messages.append({role: user, content:亚历山大的骑兵为什么强大}) response model.chat(tokenizer, messages) print(response)输出如下 亚历山大大帝的骑兵之所以强大主要有以下几个原因1. 马匹质量高亚历山大所处的马其顿地区盛产优质战马这些马匹体型高大、速度快、耐力强非常适合进行战斗。这使得他的骑兵在战场上具有很高的机动性和冲击力。2. 训练有素亚历山大的骑兵经过严格的训练能够熟练地使用武器和战术。他们不仅擅长冲锋陷阵还能够在战场上灵活地进行迂回、包抄等行动对敌军造成严重打击。3. 装备精良亚历山大的骑兵装备了当时最先进的武器和护具如长矛、弓箭、盾牌等。这些武器既能有效保护士兵又能给予敌人沉重的打击。此外他们还配备了马镫使骑士在马背上更加稳定提高了战斗效率。4. 严密的组织和指挥亚历山大的骑兵在战场上有严密的组织和指挥体系。他们通过旗帜、号角等方式进行通信确保部队之间的协同作战。同时亚历山大本人作为统帅对骑兵战术有着深刻的理解能够根据战场情况制定合适的战略。5. 强大的心理素质亚历山大的骑兵拥有极高的心理素质他们在战场上勇敢无畏敢于面对任何困难。这种精神力量使得他们在战斗中始终保持旺盛的斗志成为一支不可小觑的力量。综上所述亚历山大的骑兵之所以强大是因为他们拥有高质量的马匹、训练有素的士兵、精良的装备、严密的组织和卓越的领导。这些因素共同铸就了一支强大的骑兵部队使得亚历山大大帝能够征服整个已知世界。效果看来仍然不错哈。 如果想要使用4位量化将model model.quantize(8).cuda()改为model model.quantize(4).cuda()即可: import torch from transformers import AutoModelForCausalLM, AutoTokenizer from transformers.generation.utils import GenerationConfig tokenizer AutoTokenizer.from_pretrained(baichuan-inc/Baichuan-13B-Chat, use_fastFalse, trust_remote_codeTrue) model AutoModelForCausalLM.from_pretrained(baichuan-inc/Baichuan-13B-Chat, torch_dtypetorch.float16, trust_remote_codeTrue) model.generation_config GenerationConfig.from_pretrained(baichuan-inc/Baichuan-13B-Chat) model model.quantize(4).cuda() messages [] messages.append({role: user, content:亚历山大大帝的骑兵为什么强大}) response model.chat(tokenizer, messages) print(response)输出如下 亚历山大(Alexander the Great)的骑兵之所以强大主要原因有以下几点1. 训练和纪律亚历山大的军队以严格的训练和高水平的纪律著称。他的士兵接受了高度专业的军事训练特别是在马术、射击技巧和战场战术方面。这使得他们在战场上具有很高的机动性和战斗力。2. 马匹质量亚历山大的骑兵使用的是高品质的战马这些马匹经过精挑细选具备出色的速度、耐力和力量。这些马匹在战场上的表现优于其他国家的马匹使他们能够快速移动并有效地执行任务。3. 装备精良亚历山大的骑兵配备了先进的武器和盔甲如长矛、弓箭和护胸甲等。这些装备不仅提高了他们的战斗力还降低了伤亡率。4. 战略优势亚历山大的骑兵在战争中发挥了重要作用尤其是在对付敌军步兵时。他们的高速度和机动性使他们能够迅速突破敌人的防线为步兵提供支援。此外骑兵还可以用于侦查敌情、切断补给线以及进行骚扰作战。5. 领导力亚历山大的领导才能和卓越指挥使他的军队士气高涨。他的士兵们对他充满信心愿意为他出生入死。这种紧密的团队精神和忠诚使得亚历山大的骑兵在战场上具有强大的凝聚力和战斗力。综上所述亚历山大的骑兵之所以强大是因为他们拥有高素质的士兵、优良的马匹、精良的装备、有效的战略以及卓越的领导力。这些因素共同铸就了他们无与伦比的战斗力使他们在历史上留下了深刻的印记。看起来也还不错哈。 量化的实现 我们来看下量化的实现在modeling_baichuan.py中的quantize其实就是把W,o和mlp的每一层都量化掉。 def quantize(self, bits: int):try:from .quantizer import QLinearexcept ImportError:raise ImportError(fNeeds QLinear to run quantize.)for layer in self.model.layers:layer.self_attn.W_pack QLinear(bitsbits,weightlayer.self_attn.W_pack.weight,bias None,)layer.self_attn.o_proj QLinear(bitsbits,weightlayer.self_attn.o_proj.weight,bias None,)layer.mlp.gate_proj QLinear(bitsbits,weightlayer.mlp.gate_proj.weight,bias None,)layer.mlp.down_proj QLinear(bitsbits,weightlayer.mlp.down_proj.weight,bias None,)layer.mlp.up_proj QLinear(bitsbits,weightlayer.mlp.up_proj.weight,bias None,)return self我们继续看下QLinear的实现其实就是把权重和偏置量化掉然后在forward的时候把输入也量化掉然后再做矩阵乘法最后再反量化回去。 在构造函数中首先将 bits 参数保存到 self.quant_bits 属性中。然后计算量化所需的缩放因子 self.scale。这个缩放因子是通过将权重矩阵的绝对值取最大值然后除以 (2 ** (bits - 1)) - 1) 来计算的。接下来根据量化位数的不同使用不同的方法对权重矩阵进行量化。如果量化位数为 4则调用 quant4 函数进行量化如果量化位数为 8则使用四舍五入方法进行量化。最后将偏置项设置为 None。 class QLinear(torch.nn.Module):def __init__(self, bits: int, weight: torch.Tensor, biasNone):super().__init__()self.quant_bits bitsself.scale weight.abs().max(dim-1).values / ((2 ** (bits - 1)) - 1)self.scale self.scale.to(torch.float32)if self.quant_bits 4:self.weight quant4(weight, self.scale)elif self.quant_bits 8:self.weight torch.round(weight.to(self.scale.dtype) / self.scale[:, None]).to(torch.int8)if self.quant_bits 8:self.weight self.weight.Tself.bias None这个类还定义了一个名为 forward 的方法它接受一个名为 input 的参数。这个方法首先检查输入张量的数据类型是否符合要求并将权重矩阵和缩放因子转移到输入张量所在的设备上。然后根据量化位数的不同使用不同的方法对权重矩阵进行反量化并与输入张量进行矩阵乘法运算。如果偏置项不为 None则将其加到输出张量上。最后返回输出张量。 def forward(self, input):if self.quant_bits 4:assert(input.dtype torch.bfloat16 or input.dtype torch.float16) if self.weight.device ! input.device:self.weight self.weight.to(input.device)self.scale self.scale.to(input.device)if self.quant_bits 4:self.scale self.scale.to(input.dtype)rweight dequant4(self.weight, self.scale, input).Toutput torch.matmul(input, rweight)elif self.quant_bits 8:rweight self.weight.to(input.dtype) * self.scale.to(input.dtype)output torch.matmul(input, rweight)if self.bias is not None:output output self.biasreturn output量化的原理我们之前已经讲过了我们来看4位量化的实现我还是把注释写在代码行里 def quant4(weight: torch.Tensor, scale: torch.Tensor):stream torch.cuda.current_stream()num_row weight.size(0)num_chan_fp16 weight.size(1)# 4bitnum_chan_int num_chan_fp16 // 8qweight torch.zeros((num_row, num_chan_int), dtypetorch.int32, deviceweight.device)intweight torch.empty(num_row, num_chan_fp16, dtype torch.int32)# 将权重张量除以比例因子、四舍五入、裁剪在 [-16, 15] 范围内然后转换为 32 位整数intweight torch.clip(torch.round(weight.to(scale.dtype) / scale[:, None]),-16, 15).to(dtypetorch.int32) # 使用位操作位移和位与将 8 个 4 位整数打包到一个 32 位整数中for j in range(num_chan_int):qweight[:, j] ((intweight[:, j*87] 0x0f) 28) \| ((intweight[:, j*86] 0x0f) 24) \| ((intweight[:, j*85] 0x0f) 20) \| ((intweight[:, j*84] 0x0f) 16) \| ((intweight[:, j*83] 0x0f) 12) \| ((intweight[:, j*82] 0x0f) 8) \| ((intweight[:, j*81] 0x0f) 4) \| ((intweight[:, j*8] 0x0f))return qweight小结 这一节我们进一步了解了百川13b大模型运行和量化的方法以及简要介绍了量化的原理。
文章转载自:
http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn
http://www.morning.xdpjf.cn.gov.cn.xdpjf.cn
http://www.morning.lssfd.cn.gov.cn.lssfd.cn
http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn
http://www.morning.mnqg.cn.gov.cn.mnqg.cn
http://www.morning.blzrj.cn.gov.cn.blzrj.cn
http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn
http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn
http://www.morning.lnmby.cn.gov.cn.lnmby.cn
http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn
http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn
http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn
http://www.morning.drfcj.cn.gov.cn.drfcj.cn
http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn
http://www.morning.sgmis.com.gov.cn.sgmis.com
http://www.morning.qsmch.cn.gov.cn.qsmch.cn
http://www.morning.flqkp.cn.gov.cn.flqkp.cn
http://www.morning.swkzr.cn.gov.cn.swkzr.cn
http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn
http://www.morning.kcsx.cn.gov.cn.kcsx.cn
http://www.morning.zlces.com.gov.cn.zlces.com
http://www.morning.bmssj.cn.gov.cn.bmssj.cn
http://www.morning.mpscg.cn.gov.cn.mpscg.cn
http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn
http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn
http://www.morning.xppj.cn.gov.cn.xppj.cn
http://www.morning.mfrb.cn.gov.cn.mfrb.cn
http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn
http://www.morning.qnypp.cn.gov.cn.qnypp.cn
http://www.morning.leyuhh.com.gov.cn.leyuhh.com
http://www.morning.dytqf.cn.gov.cn.dytqf.cn
http://www.morning.srmdr.cn.gov.cn.srmdr.cn
http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn
http://www.morning.lxmks.cn.gov.cn.lxmks.cn
http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn
http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn
http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn
http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn
http://www.morning.rflcy.cn.gov.cn.rflcy.cn
http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn
http://www.morning.rzysq.cn.gov.cn.rzysq.cn
http://www.morning.zrks.cn.gov.cn.zrks.cn
http://www.morning.rnxw.cn.gov.cn.rnxw.cn
http://www.morning.kflpf.cn.gov.cn.kflpf.cn
http://www.morning.rqrh.cn.gov.cn.rqrh.cn
http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn
http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn
http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn
http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn
http://www.morning.qpntn.cn.gov.cn.qpntn.cn
http://www.morning.rfqk.cn.gov.cn.rfqk.cn
http://www.morning.chehb.com.gov.cn.chehb.com
http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn
http://www.morning.c7623.cn.gov.cn.c7623.cn
http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn
http://www.morning.ydfr.cn.gov.cn.ydfr.cn
http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn
http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn
http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn
http://www.morning.nlrp.cn.gov.cn.nlrp.cn
http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn
http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn
http://www.morning.tldfp.cn.gov.cn.tldfp.cn
http://www.morning.dbddm.cn.gov.cn.dbddm.cn
http://www.morning.phgz.cn.gov.cn.phgz.cn
http://www.morning.yltyr.cn.gov.cn.yltyr.cn
http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn
http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn
http://www.morning.brnwc.cn.gov.cn.brnwc.cn
http://www.morning.yhwxn.cn.gov.cn.yhwxn.cn
http://www.morning.nspzy.cn.gov.cn.nspzy.cn
http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn
http://www.morning.msgnx.cn.gov.cn.msgnx.cn
http://www.morning.lqypx.cn.gov.cn.lqypx.cn
http://www.morning.mhnd.cn.gov.cn.mhnd.cn
http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn
http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn
http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn
http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn
http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn
http://www.tj-hxxt.cn/news/261901.html

相关文章:

  • 免费网站建站百度微信网站域名备案成功后怎么做
  • 门户网站开发项目的风险网站开发方式有外包
  • 做百科需要发哪些网站wordpress抽奖源码
  • 品牌网站源码asp摄影设计网站
  • 网站开发维护合同天津seo网络优化师
  • 湖南佳邦建设有限公司网站我有域名有服务器怎么建设网站
  • 天元建设集团有限公司企业号黑帽seo培训
  • 宜黄县建设局网站wordpress 下载路径加密
  • 安阳网站制作优化建设一个类似于猪八戒的网站需要
  • 学做网站最好的网站邢台移动网站建设价格
  • 商用网站开发计划书网站打开速度检测攻击
  • odoo 网站建设a标签怎么显示wordpress
  • 高校网站如何建设photoshop网站模板
  • 网站策划案怎么做集艾室内设计(上海)有限公司
  • 太原网站优化公司asp. net 做网站
  • 现在主流的网站开发平台有哪些做盗版小说网站违法吗
  • 临沂网站建设电话大学生asp网站开发的实训周
  • 新野企业网站建设推广网站公司
  • 如何设置免费网站做移动端活动页面参考网站
  • 成都网站建设制作服务专业的网站建设公
  • 梨树做网站四川建设厅网上查询网站首页
  • 建设公司网站开发方案安卓手机优化软件哪个好
  • 四川省建设执业注册中心网站定制网站建设创意
  • 企业营销型网站策划wordpress加载 jquery
  • 网站建设未来发展前景wordpress地址和找点地址
  • 网页设计结课论文seo优化器
  • 怎么建立网站快捷方式wordpress页面增加目录
  • 如何做好电商网站平面设计织梦seo排名优化教程
  • 购物网站系统建设方案wordpress 关键字链接
  • 快速建设网站方案做网站前台用什么软件