Navigation

    Gpushare.com

    • Register
    • Login
    • Search
    • Popular
    • Categories
    • Recent
    • Tags

    paddle版本reformer尝试复现。

    技术交流
    1
    1
    64
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 183****0229
      183****0229 last edited by

      介绍

      Reformer: The Efficient Transformer 论文复现 paddle2.x

      依赖

      • transformers
      • paddlenlp
      • easydict
      • torch
      • paddle

      地址

      https://github.com/JunnYu/paddle_reformer

      目标

      ReformerModel,ReformerForSequenceClassification和ReformerForQuestionAnswering网络前向推理输出对齐参考代码。
      注: 由于ReformerForSequenceClassification和ReformerForQuestionAnswering都使用ReformerModel作为主干部分,因此只需要对齐ReformerModel部分前向传播的权重即可。

      准备

      • 从 https://huggingface.co/google/ 下载reformer权重pytorch_model.bin放入google下对应的文件夹
      • 从 https://huggingface.co/junnyu/reformer_paddle 下载转化后的paddle权重放入paddle下对应的文件夹

      eval模式对齐

      (加载预训练权重模型无法对齐,而随机初始化模型权重能对齐)
      由于paddle框架与pytorch框架的nn.Linear和matmul的方法在输入的变量方差非常大的情况不能完全对齐,导致对齐最大误差非常大。

      对于12层的模型。
      使用预训练权重误差会达到(12_layer.txt文件最后一行)
      mean dif: tensor(2.6582e-05) 
      max dif: tensor(0.0009)
      而使用随机初始化的权重,误差正常!(random_12_layer.txt文件最后一行)
      mean dif: tensor(8.7896e-07) 
      max dif: tensor(4.7684e-06)
      

      train模式对齐(进行了train模式下,前向传播结果对齐和反向传播的梯度对齐)

      该模式下同样也存在上面的问题,因此都在随机初始化权重的情况下进行精度对齐。

      # 进入train文件夹
      cd train
      # 在GPU和CPU模式下,进行loss和grad的比较
      python compare_train.py
      # test on cpu!
      # compare loss
      # mean difference: tensor(8.5831e-06)
      # max difference: tensor(8.5831e-06)
      # ==================================================
      # compare grad
      # mean difference: tensor(1.6347e-11)
      # max difference: tensor(1.4472e-08)
      # ==================================================
      # test on gpu!
      # compare loss
      # mean difference: tensor(4.7684e-07)
      # max difference: tensor(4.7684e-07)
      # ==================================================
      # compare grad
      # mean difference: tensor(8.7412e-11)
      # max difference: tensor(4.4449e-08)
      

      吐槽

      • 改这个模型头都大了,其中api转换是大头!个人感觉paddle2.x的一些API好不人性化,比如gather,scatter,必须要用别的方法才能与pytorch的api对齐,之后有空再详细说下我是如何“曲线救国”的。
      • 模型搭建没有出错!!!!!!误差大的原因是框架导致的!!!!!!
      1 Reply Last reply Reply Quote 1
      • First post
        Last post