【课程总结】day21(下):大模型的三大架构及T5体验

打印 上一主题 下一主题

主题 1019|帖子 1019|积分 3057

前言

在前两篇学习中【课程总结】day19(中):Transformer架构及留意力机制了解,我们初步了解大模型的公共底层架构Transformer的构成,同时借助对 Transformer 的代码深入了解(【课程总结】day20:Transformer源码深入明白之训练过程),初步掌握了 Transformer 的训练过程。本篇我们将对大模型的训练阶段进行初步了解,同时部署一个T5模型进行试用体验。
大模型的三大架构

大模型(如大型语言模型)的架构通常有多种类型,以下是三种主要的架构:
Encoder-Decoder 架构

架构
由两个主要部门组成:编码器(Encoder)息争码器(Decoder),即Transformer 架构。它先明白输入的信息(Encoder部门),然后基于这个明白天生新的、相干的内容(Decoder部门)。
特点


  • 这种架构就像是翻译家。他先听你说一段话(比如英文),明白它,然后把它翻译成另一种语言(比如中文)。
  • 擅优点理需要明白输入然后天生相干输出的任务,比如翻译或问答系统。
代表公司及产品


  • Google:Transformer、T5(Text-to-Text Transfer Transformer)
  • Facebook:BART(Bidirectional and Auto-Regressive Transformers)
Encoder-Only 架构

架构
仅包罗编码器部门,即只是使用 Transformer 的 Encoder ,它专注于明白和分析输入的信息,而不是创造新的内容。
特点


  • 这种架构就像是一个专业的书评家。他阅读和明白一本书(输入的信息),然后告诉你这本书是关于什么的,比如它的主题是爱情、冒险还是悬疑。
  • 擅长明白和分类信息,比如判定一段文本的情绪倾向(积极还是消极)大概主题分类。
代表公司及产品


  • Google:BERT(Bidirectional Encoder Representations from Transformers)
  • Facebook:RoBERTa、DistilBERT
Decoder-Only 架构

架构
仅包罗解码器部门,即只是使用 Transformer 的 Decoder ,它接收一些信息(开头),然后天生接下来的内容(故事)。
特点


  • 这种架构就像一个讲故事的人。你给他一个开头,比如“有一次,一只小猫走失了”,然后他会继续这个故事,讲述下去,一直到故事结束。
  • 擅长创造性的写作,比如写小说或自动天生文章。它更多关注于从已有的信息(开头)扩展出新的内容。
代表公司及产品


  • OpenAI:GPT-3、GPT-4
三大架构演进图


大模型T5的体验

为了对大模型有个初步感受,本次我们拉取代码在当地部署一个T5模型并体验它。
情况搭建

体验大模型的方法有两种方案:当地情况 和 长途情况。
当地情况

当地情况的设置和使用方法,可以检察往期文章:Windows下安装ubuntu方法。
长途情况

第一步:访问Modelscope平台,注册账号。

第二步:启动魔搭平台的PAI-DSW实例


第三步:在新开的页面中登录阿里云账号
第四步:在PAI-DSW实例中启动终端命令行

选择模型

在魔搭平台中搜索ChatLM模型,检察中文对话0.2B小模型,选择 模型文件,点击 下载模型。
代码拉取

在终端中输入以下命令,拉取模型代码
  1. git clone https://www.modelscope.cn/charent/ChatLM-mini-Chinese.git
复制代码
安装依赖

  1. pip install transformers
复制代码
模型使用

  1. from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
  2. import torch
  3. # 因为已经下载了模型,所以model_id改为本地路径
  4. model_id = 'ChatLM-mini-Chinese'
  5. # 判断GPU是否可用
  6. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  7. # 加载分词器
  8. tokenizer = AutoTokenizer.from_pretrained(model_id)
  9. # 加载模型
  10. model = AutoModelForSeq2SeqLM.from_pretrained(model_id, trust_remote_code=True).to(device)
  11. txt = '如何评价Apple这家公司?'
  12. # 对输入内容编码
  13. encode_ids = tokenizer([txt])
  14. input_ids, attention_mask = torch.LongTensor(encode_ids['input_ids']), torch.LongTensor(encode_ids['attention_mask'])
  15. # 调用模型预测结果
  16. outs = model.my_generate(
  17.     input_ids=input_ids.to(device),
  18.     attention_mask=attention_mask.to(device),
  19.     max_seq_len=256,
  20.     search_type='beam',
  21. )
  22. # 对输出内容解码
  23. outs_txt = tokenizer.batch_decode(outs.cpu().numpy(), skip_special_tokens=True, clean_up_tokenization_spaces=True)
  24. # 打印输出
  25. print(outs_txt[0])
复制代码
运行结果:

增补知识

tokenizer 分词器

在Jupyter Notebook中检察tokenizer,可以看到分词器中包罗常见的Token。
  1. PreTrainedTokenizerFast(name_or_path='ChatLM-mini-Chinese', vocab_size=29298, model_max_length=1000000000000000019884624838656, is_fast=True, padding_side='right', truncation_side='right', special_tokens={'eos_token': '[EOS]', 'unk_token': '[UNK]', 'pad_token': '[PAD]'}, clean_up_tokenization_spaces=True),  added_tokens_decoder={
  2.         0: AddedToken("[PAD]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  3.         1: AddedToken("[EOS]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  4.         2: AddedToken("[SEP]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  5.         3: AddedToken("[BOS]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  6.         4: AddedToken("[CLS]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  7.         5: AddedToken("[MASK]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  8.         6: AddedToken("[UNK]", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
  9. }
复制代码
model 模型

在Jupyter Notebook中检察model,可以看到T5模型的结构。
  1. TextToTextModel(
  2.   (shared): Embedding(29298, 768)
  3.   (encoder): T5Stack(
  4.     (embed_tokens): Embedding(29298, 768)
  5.     (block): ModuleList(
  6.       (0): T5Block(
  7.         (layer): ModuleList(
  8.           (0): T5LayerSelfAttention(
  9.             (SelfAttention): T5Attention(
  10.               (q): Linear(in_features=768, out_features=768, bias=False)
  11.               (k): Linear(in_features=768, out_features=768, bias=False)
  12.               (v): Linear(in_features=768, out_features=768, bias=False)
  13.               (o): Linear(in_features=768, out_features=768, bias=False)
  14.               (relative_attention_bias): Embedding(32, 12)
  15.             )
  16.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  17.             (dropout): Dropout(p=0.1, inplace=False)
  18.           )
  19.           (1): T5LayerFF(
  20.             (DenseReluDense): T5DenseActDense(
  21.               (wi): Linear(in_features=768, out_features=3072, bias=False)
  22.               (wo): Linear(in_features=3072, out_features=768, bias=False)
  23.               (dropout): Dropout(p=0.1, inplace=False)
  24.               (act): ReLU()
  25.             )
  26.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  27.             (dropout): Dropout(p=0.1, inplace=False)
  28.           )
  29.         )
  30.       )
  31.       (1-9): 9 x T5Block(
  32.         (layer): ModuleList(
  33.           (0): T5LayerSelfAttention(
  34.             (SelfAttention): T5Attention(
  35.               (q): Linear(in_features=768, out_features=768, bias=False)
  36.               (k): Linear(in_features=768, out_features=768, bias=False)
  37.               (v): Linear(in_features=768, out_features=768, bias=False)
  38.               (o): Linear(in_features=768, out_features=768, bias=False)
  39.             )
  40.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  41.             (dropout): Dropout(p=0.1, inplace=False)
  42.           )
  43.           (1): T5LayerFF(
  44.             (DenseReluDense): T5DenseActDense(
  45.               (wi): Linear(in_features=768, out_features=3072, bias=False)
  46.               (wo): Linear(in_features=3072, out_features=768, bias=False)
  47.               (dropout): Dropout(p=0.1, inplace=False)
  48.               (act): ReLU()
  49.             )
  50.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  51.             (dropout): Dropout(p=0.1, inplace=False)
  52.           )
  53.         )
  54.       )
  55.     )
  56.     (final_layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  57.     (dropout): Dropout(p=0.1, inplace=False)
  58.   )
  59.   (decoder): T5Stack(
  60.     (embed_tokens): Embedding(29298, 768)
  61.     (block): ModuleList(
  62.       (0): T5Block(
  63.         (layer): ModuleList(
  64.           (0): T5LayerSelfAttention(
  65.             (SelfAttention): T5Attention(
  66.               (q): Linear(in_features=768, out_features=768, bias=False)
  67.               (k): Linear(in_features=768, out_features=768, bias=False)
  68.               (v): Linear(in_features=768, out_features=768, bias=False)
  69.               (o): Linear(in_features=768, out_features=768, bias=False)
  70.               (relative_attention_bias): Embedding(32, 12)
  71.             )
  72.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  73.             (dropout): Dropout(p=0.1, inplace=False)
  74.           )
  75.           (1): T5LayerCrossAttention(
  76.             (EncDecAttention): T5Attention(
  77.               (q): Linear(in_features=768, out_features=768, bias=False)
  78.               (k): Linear(in_features=768, out_features=768, bias=False)
  79.               (v): Linear(in_features=768, out_features=768, bias=False)
  80.               (o): Linear(in_features=768, out_features=768, bias=False)
  81.             )
  82.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  83.             (dropout): Dropout(p=0.1, inplace=False)
  84.           )
  85.           (2): T5LayerFF(
  86.             (DenseReluDense): T5DenseActDense(
  87.               (wi): Linear(in_features=768, out_features=3072, bias=False)
  88.               (wo): Linear(in_features=3072, out_features=768, bias=False)
  89.               (dropout): Dropout(p=0.1, inplace=False)
  90.               (act): ReLU()
  91.             )
  92.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  93.             (dropout): Dropout(p=0.1, inplace=False)
  94.           )
  95.         )
  96.       )
  97.       (1-9): 9 x T5Block(
  98.         (layer): ModuleList(
  99.           (0): T5LayerSelfAttention(
  100.             (SelfAttention): T5Attention(
  101.               (q): Linear(in_features=768, out_features=768, bias=False)
  102.               (k): Linear(in_features=768, out_features=768, bias=False)
  103.               (v): Linear(in_features=768, out_features=768, bias=False)
  104.               (o): Linear(in_features=768, out_features=768, bias=False)
  105.             )
  106.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  107.             (dropout): Dropout(p=0.1, inplace=False)
  108.           )
  109.           (1): T5LayerCrossAttention(
  110.             (EncDecAttention): T5Attention(
  111.               (q): Linear(in_features=768, out_features=768, bias=False)
  112.               (k): Linear(in_features=768, out_features=768, bias=False)
  113.               (v): Linear(in_features=768, out_features=768, bias=False)
  114.               (o): Linear(in_features=768, out_features=768, bias=False)
  115.             )
  116.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  117.             (dropout): Dropout(p=0.1, inplace=False)
  118.           )
  119.           (2): T5LayerFF(
  120.             (DenseReluDense): T5DenseActDense(
  121.               (wi): Linear(in_features=768, out_features=3072, bias=False)
  122.               (wo): Linear(in_features=3072, out_features=768, bias=False)
  123.               (dropout): Dropout(p=0.1, inplace=False)
  124.               (act): ReLU()
  125.             )
  126.             (layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  127.             (dropout): Dropout(p=0.1, inplace=False)
  128.           )
  129.         )
  130.       )
  131.     )
  132.     (final_layer_norm): FusedRMSNorm(torch.Size([768]), eps=1e-06, elementwise_affine=True)
  133.     (dropout): Dropout(p=0.1, inplace=False)
  134.   )
  135.   (lm_head): Linear(in_features=768, out_features=29298, bias=False)
  136. )
复制代码


  • 检察该模型的结构,其结构是一个典范的Transformer模型结构。
  • (encoder): T5Stack 是编码器,其内部是由10个T5Block组成,(decoder): T5Stack 是解码器,其内部也是由10个T5Block组成。
  • T5LayerSelfAttention 是自留意力处理模块,T5LayerCrossAttention 是融合留意力处理模块,T5LayerFF 是前馈模块。
  • (lm_head): Linear 是对应Transformer的输出层。
内容小结



  • 大模型有三大架构:Encoderdecoder、Encoder-Only、Decoder-Only。
  • Encoderdecoder架构就像是翻译家,代表模型是T5模型。
  • Encoder-Only架构就像是书评家,代表模型是BERT模型。
  • Decoder-Only架构就像是数学家,代表模型是GPT-4模型。
  • 大模型训练阶段由三个阶段组成:预训练(PT) 、监督微调(SFT) 和 基于人类反馈的强化学习(RLHF) 。
参考资料

CSDN:大语言模型的三种主要架构
CSDN:解析大语言模型训练三阶段
接待关注公众号以得到最新的文章和新闻



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

本帖子中包含更多资源

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

x
回复

举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

钜形不锈钢水箱

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