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

青海网站建设湖北省建设交易协会网站

青海网站建设,湖北省建设交易协会网站,四川成都现在可以去吗,jsp网站开发遇到问题目录 GLM-130B和ChatGLM-6BChatGLM-6B直接部署基于PEFT的LoRA微调ChatGLM-6B GLM-130B和ChatGLM-6B 对于三类主要预训练框架: autoregressive(无条件生成),GPT的训练目标是从左到右的文本生成。autoencoding(语言理解…

目录

  • GLM-130B和ChatGLM-6B
  • ChatGLM-6B直接部署
  • 基于PEFT的LoRA微调ChatGLM-6B

GLM-130B和ChatGLM-6B

对于三类主要预训练框架:

  • autoregressive(无条件生成),GPT的训练目标是从左到右的文本生成
  • autoencoding(语言理解,比如BERT、ALBERT、RoBERTa、DeBERTa),encoder-decoder(有条件生成)。其训练目标是对文本进行随机掩码,然后预测被掩码的词
  • 基于encoder-decoder的T5,编码器中的注意力是双向的,解码器中的注意力是单向的,可同时应⽤于⾃然语⾔理解任务和⽣成任务,但T5为了达到和RoBERTa和DeBERTa相似的性能,往往需要更多的参数量。T5的训练目标是接受一段文本,从左到右生成另一段文本

为了统一,GLM在结构和训练目标上兼容三种预训练模型。在结构上,通过attention mask实现同时存在单向注意力和双向注意力:
fig1
当attention mask是全1矩阵的时候,这时的注意力是双向的,当attention mask是三角矩阵时,比如上图,注意力就变成单向。因此,GLM可以在只使⽤Transformer编码器的情况下,⾃定义attention mask来兼容三种模型结构。具体回顾 LLM中的微调演变与LLM架构类型-LLM的架构分类

训练时,GLM采用自回归空格填充任务,用于兼容三种模型的训练目标,先采样输入文本中部分片段,将其替换为[MASK] token,然后预测[MASK]所对应的文本片段,与掩码语⾔模型不同的是,预测的过程是自回归方式:
fig2

  • 当被mask的片段长度为1,等价于BERT(掩码语言建模),当全部文本都被mask,等价于GPT(无条件语言生成),当将文本1和文本2拼接在一起,然后将文本2整体mask后,等价于T5(条件语言生成)。

GLM有两个交替优化的训练目标:

  • 文档级别的生成:从文档中随机采样一个文本片段进行掩码,片段的长度为文档长度的50%-100%;
  • 句子级别的生成:从文档中随机掩码若干文本片段,每个文本片段必须为完整的句子,被掩码的词数量为整个文档长度的15%;

GLM-130B是拥有1300亿参数的中英双语模型,在96块A100上训练了60天。ChatGLM-6B基于GLM架构,具有62亿参数,无量化的情况下占用显存13G,INT8量化后支持在单张11G显存的2080Ti上推理,INT4量化后只需6G显存进行推理,7G显存做P-Tuning v2微调。ChatGLM-6B以GLM-130B为基座,加入code预训练,并进行SFT和RLHF,支持中文问答。


关于量化
INT8量化是一种将深度学习模型中的权重和激活值从32位浮点数(FP32)减少到8位整数(INT8)的技术,这可以减少计算资源需求,降低能耗,量化通常包括以下步骤:

  • 选择量化范围:确定权重和激活的最小值和最大值;
  • 量化映射:根据范围将32位浮点数映射为8位整数;
  • 反量化:将8位整数转回浮点数用于计算。

ChatGLM-6B直接部署

首先获取项目:

$ git clone https://github.com/THUDM/ChatGLM-6B
$ cd ChatGLM-6B

注意环境配置:torch版本不低于1.10,transformers为4.27.1,下载模型文件:

$ git clone https://huggingface.co/THUDM/chatglm-6b

直接新建my_demo.py:

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print("response: ", response)
print("history: ", history)
response, history = model.chat(tokenizer, "如何提高弹跳", history=history)
print("response: ", response)
print("history: ", history)

生成的答案为(同时打印了历史信息):
fig3
也可以交互式问答,注意修改cli_demo.py中的模型路径:

$ python cli_demo.py

程序会在命令行中进行交互式的对话,在命令行中输入指示并回车即可生成回复,输入 clear 可以清空对话历史(history),输入 stop 终止程序。

也可以利用gradio可视化界面,注意修改web_demo.py中的模型路径:

from transformers import AutoModel, AutoTokenizer
import gradio as gr
import mdtex2htmltokenizer = AutoTokenizer.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()

运行web_demo.py即可:
fig4

默认情况下,模型以 FP16 精度加载,运行模型需要大概 13GB 显存。

基于PEFT的LoRA微调ChatGLM-6B

官方基于P-Tuning v2微调,此处我们使用非官方项目ChatGLM-Tuning基于LoRA微调:

$ git clone https://github.com/mymusise/ChatGLM-Tuning
$ cd ChatGLM-Tuning

首先新建data_process.sh进行数据处理:

python cover_alpaca2jsonl.py \--data_path data/alpaca_data.json \--save_path data/alpaca_data.jsonl \

alpaca_data.json包含用于微调Alpaca模型的52k指令数据(回顾 LLaMA-7B微调记录)。data_process.sh用于将这52k数据处理为ChatGLM-6B的格式。

对于alpaca_data.json,包含 instruction,input,output,格式为:

[{"instruction": "Give three tips for staying healthy.","input": "","output": "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables. \n2. Exercise regularly to keep your body active and strong. \n3. Get enough sleep and maintain a consistent sleep schedule."},...{"instruction": "Edit the following sentence (highlight changes in bold)","input": "We use computers for gaming and entertainment","output": "We use computers for gaming, entertainment, and work."}
]

处理后的alpaca_data.jsonl包含context,target,格式为:

{"context": "Instruction: Give three tips for staying healthy.\nAnswer: ", "target": "1.Eat a balanced diet and make sure to include plenty of fruits and vegetables. \n2. Exercise regularly to keep your body active and strong. \n3. Get enough sleep and maintain a consistent sleep schedule."}
{"context": "Instruction: Edit the following sentence (highlight changes in bold)\nInput: We use computers for gaming and entertainment\nAnswer: ", "target": "We use computers for gaming, entertainment, and work."}

可以看到这个格式正好符合前面所提到的GLM的训练目标。

下一步,新建token.sh将数据token化,首先修改tokenize_dataset_rows.py中, 函数read_jsonl内的模型路径:

model_name = "/data/temp/my-alpaca-lora/chatglm-6b"

token.sh为:

python tokenize_dataset_rows.py \--jsonl_path data/alpaca_data.jsonl \--save_path data/alpaca \--max_seq_length 200 \--skip_overlength False \

然后新建finetune.sh执行微调,注意修改finetune.py中的模型路径:

tokenizer = AutoTokenizer.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True)def main():...# init modelmodel = AutoModel.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", load_in_8bit=True, trust_remote_code=True, device_map="auto")

finetune.sh为:

python finetune.py \--dataset_path data/alpaca \--lora_rank 8 \--per_device_train_batch_size 6 \--gradient_accumulation_steps 1 \--max_steps 52000 \--save_steps 1000 \--save_total_limit 2 \--learning_rate 1e-4 \--fp16 \--remove_unused_columns false \--logging_steps 50 \--output_dir output \

额外说明,在finetune.py,通过get_peft_model将模型封装为带有LoRA分支的模型:

model = AutoModel.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", load_in_8bit=True, trust_remote_code=True, device_map="auto")
...
# setup peft
peft_config = LoraConfig(task_type=TaskType.CAUSAL_LM,inference_mode=False,r=finetune_args.lora_rank,lora_alpha=32,lora_dropout=0.1)
model = get_peft_model(model, peft_config)

其余训练内容都可以不变,这样就能进行LoRA优化。

微调后,通过以下方式加载模型:

from peft import PeftModelmodel = AutoModel.from_pretrained("/data/temp/my-alpaca-lora/chatglm-6b", trust_remote_code=True, load_in_8bit=True, device_map='auto')model = PeftModel.from_pretrained(model, "./output/")
http://www.yayakq.cn/news/642963/

相关文章:

  • 质感设计网站企业网站源码哪个最好
  • 怎么看一个网站是由哪个网络公司做的电商设计网站素材
  • 网站开发与管理心得体会科技公司网站欣赏
  • 没有做老千的斗牛网站6电子商务这个专业好吗
  • 深圳学校网站建设lnmp wordpress php7
  • 设置网站文件夹的安全项广州企业网站模板购买
  • 网站最下面版权模板网站刷收益是怎么做的
  • 临沂手机网站建设网站建设课程职业教育机构
  • 汕头seo网站优化网页设计介绍北京网站
  • 东莞网站建设dgjwz有项目没有钱怎么找投资人
  • 怎样做网站表白小程序搭建多少钱
  • 做seo用哪种建站程序最好网站的ftp怎么查
  • dede我的网站如何使用seo进行综合查询
  • 昆山网站建设多少钱php网站开发自学
  • 网站建设公司兴田德润优惠微信的网站怎么做
  • 腾讯云建站流程wordpress加链接地址
  • 深圳微信网站运营seo是什么地方
  • 网站降权怎么救wordpress标签小工具栏
  • 做旅游攻略比较好的网站php调用wordpress函数
  • WordPress创建页面左侧导航网络优化的目的
  • 网站设计的能力要求wrix 网站开发
  • 商城网站wordpress学院网站建设项目范围变更申请表
  • 宁波住房与城乡建设部网站深圳建设集团有限公司有分公司吗
  • 前端网站做中 英文wordpress 获取子菜单
  • 海外网站建设教程ui培训机构排名前十
  • 网站pc和手机端做商城网站的公司
  • 枣阳网站开发智慧团建官方网址
  • php网站底部文件百度上怎么做推广
  • 长沙做网站的包吃包住4000企业网站建设策划书案例
  • 做期货网站违法的吗用pageadmin做的网站用什么虚拟主机号