[{"data":1,"prerenderedAt":497},["ShallowReactive",2],{"content-query-L1Bc3gv8W1":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":491,"_id":492,"_source":493,"_file":494,"_stem":495,"_extension":496},"/technology-blogs/zh/769","zh",false,"","技术干货｜昇思MindSpore可变序列长度的动态Transformer已发布！","针对每个样本自适应地使用最合适的token数目进行表征","2021-11-01","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/ddfb81086bbb4cb9ab9e014ff6cc0207.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":485},"root",[17,25,34,39,55,71,83,90,110,115,122,127,132,144,151,156,166,173,178,196,206,211,216,223,228,233,240,248,253,260,265,273,278,285,290,298,303,310,315,322,327,332,339,344,362,370,375,380,385,404,409,414,419,433,447,452,459,464,475,480],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"技术干货昇思mindspore可变序列长度的动态transformer已发布",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/3de8c92da18245bb96559a1b875f6546.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37],{"type":24,"value":38},"从事AI算法研究的朋友们都知道，以Transformer为代表的基于自注意力机制的神经网络已经在大规模文本建模、文本情感理解、机器翻译等领域取得了显著的性能提升和广泛的实际应用。近年来，Transformer系列模型以有效增强CNN所欠缺的长距离依赖关系建模的能力，在视觉领域同样大获成功，应用特别广泛。实际应用中，随着数据集大小的增加，由于Transformer通常将所有输入样本表征为固定数目的tokens，开销也急剧增加。研究团队发现采用定长的token序列表征数据集中所有的图像是一种低效且次优的做法！",{"type":18,"tag":26,"props":40,"children":41},{},[42,44,53],{"type":24,"value":43},"为此，清华大学黄高老师研究团队和华为中央媒体技术院联合研究的关于动态Transformer的最新工作Not All Images are Worth 16x16 Words: Dynamic Transformers for Efficient Image Recognition (**",{"type":18,"tag":45,"props":46,"children":50},"a",{"href":47,"rel":48},"https://arxiv.org/pdf/2105.15075.pdf**%EF%BC%8C%E5%B7%B2%E8%A2%ABNeurIPS",[49],"nofollow",[51],{"type":24,"value":52},"https://arxiv.org/pdf/2105.15075.pdf**，已被NeurIPS",{"type":24,"value":54}," 2021接收)，可针对每个样本自适应地使用最合适的token数目进行表征，并在Github上发布MindSpore实现的训练推理代码：",{"type":18,"tag":26,"props":56,"children":57},{},[58],{"type":18,"tag":59,"props":60,"children":61},"strong",{},[62,69],{"type":18,"tag":45,"props":63,"children":66},{"href":64,"rel":65},"https://github.com/blackfeather-wang/Dynamic-Vision-Transformer-MindSpore**%EF%BC%8C%E5%8F%AF%E4%BD%BF%E6%A8%A1%E5%9E%8B%E8%AE%AD%E7%BB%83%E7%B2%BE%E5%BA%A6%E6%8C%81%E5%B9%B3%EF%BC%8C%E5%B9%B3%E5%9D%87%E6%8E%A8%E7%90%86%E5%8A%A0%E9%80%9F1.6-3.6%E5%80%8D%EF%BC%81%E5%8A%A8%E6%80%81Transformer%E6%A8%A1%E5%9E%8B%E6%9C%AA%E6%9D%A5%E4%BC%9A%E5%90%8C%E6%AD%A5%E4%B8%8A%E7%BA%BF%E8%87%B3**MindSpore",[49],[67],{"type":24,"value":68},"https://github.com/blackfeather-wang/Dynamic-Vision-Transformer-MindSpore**，可使模型训练精度持平，平均推理加速1.6-3.6倍！动态Transformer模型未来会同步上线至**MindSpore",{"type":24,"value":70}," ModelZoo",{"type":18,"tag":26,"props":72,"children":73},{},[74,76],{"type":24,"value":75},"（**",{"type":18,"tag":45,"props":77,"children":80},{"href":78,"rel":79},"https://gitee.com/mindspore/models/tree/master**%EF%BC%89%E4%B8%AD%EF%BC%8C%E5%8F%AF%E7%9B%B4%E6%8E%A5%E8%8E%B7%E5%8F%96%E4%BD%BF%E7%94%A8%E3%80%82",[49],[81],{"type":24,"value":82},"https://gitee.com/mindspore/models/tree/master**）中，可直接获取使用。",{"type":18,"tag":26,"props":84,"children":85},{},[86],{"type":18,"tag":30,"props":87,"children":89},{"alt":7,"src":88},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/931443e15ccc436ab1bc5002fd64c095.jpg",[],{"type":18,"tag":26,"props":91,"children":92},{},[93,98,100,105,106],{"type":18,"tag":59,"props":94,"children":95},{},[96],{"type":24,"value":97},"●",{"type":24,"value":99}," ",{"type":18,"tag":59,"props":101,"children":102},{},[103],{"type":24,"value":104},"背景技术",{"type":24,"value":99},{"type":18,"tag":59,"props":107,"children":108},{},[109],{"type":24,"value":97},{"type":18,"tag":26,"props":111,"children":112},{},[113],{"type":24,"value":114},"近年来，Google的工作Vision Transformer（ViT）系列视觉模型受到了广泛关注，这些模型通常将图像数据划分为固定数目的patch，并将每个patch对应的像素值采用线性映射等方式嵌入为一维的token，作为Transformer模型的输入，示意图如图2所示。",{"type":18,"tag":26,"props":116,"children":117},{},[118],{"type":18,"tag":30,"props":119,"children":121},{"alt":7,"src":120},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/d72b3303d4944e0f84e8871c9316c1dd.jpg",[],{"type":18,"tag":26,"props":123,"children":124},{},[125],{"type":24,"value":126},"图2 Vision Transformer（ViT）对输入的表征方式",{"type":18,"tag":26,"props":128,"children":129},{},[130],{"type":24,"value":131},"假设模型结构固定，即每个token的维度大小固定，将输入表征为更多的token可以实现对图片更为细粒度的建模，往往可以有效提升模型的测试准确率；然而与此相对，由于Transformer的计算开销随token数目成二次方增长，增多token将导致大量增长的计算开销。为了在精度和效率之间取得一个合适的平衡，现有的ViT模型一般将token数设置为14x14或16x16。",{"type":18,"tag":26,"props":133,"children":134},{},[135,137,142],{"type":24,"value":136},"论文提出，一个更合适的方法应当是，",{"type":18,"tag":59,"props":138,"children":139},{},[140],{"type":24,"value":141},"根据每个输入的具体特征，对每张图片设置对其最合适的token数目",{"type":24,"value":143},"。以图3为例，左侧的苹果图片构图简单且物体尺寸较大，右侧图片则包含复杂的人群、建筑、草坪等内容且物体尺寸均较小。显然，前者只需要少量token就可以有效表征其内容，后者则需要更多的token描述不同构图要素之间的复杂关系。",{"type":18,"tag":26,"props":145,"children":146},{},[147],{"type":18,"tag":30,"props":148,"children":150},{"alt":7,"src":149},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/35d5f7e4f7ff42cbb01dc3d91c387b93.jpg",[],{"type":18,"tag":26,"props":152,"children":153},{},[154],{"type":24,"value":155},"图3 根据具体输入确定token数目",{"type":18,"tag":26,"props":157,"children":158},{},[159,161],{"type":24,"value":160},"这一问题对于网络的推理效率是非常关键的。从表1中可以看到，若将token数目设置为4x4，准确率仅仅下降了15.9%（76.7% v.s. 60.8%），但计算开销下降了8.5倍（1.78G v.s. 0.21G）。",{"type":18,"tag":59,"props":162,"children":163},{},[164],{"type":24,"value":165},"这一结果表明，正确识别占数据大多数的较“简单”的样本只需4x4或更少的token，相当多的计算浪费在了使用存在大量冗余的14x14 token表征他们！",{"type":18,"tag":26,"props":167,"children":168},{},[169],{"type":18,"tag":30,"props":170,"children":172},{"alt":7,"src":171},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/cca6b8b950414c8b91dcad80e752bc24.jpg",[],{"type":18,"tag":26,"props":174,"children":175},{},[176],{"type":24,"value":177},"表1 T2T-ViT-12使用更少token时的测试精度和计算开销",{"type":18,"tag":26,"props":179,"children":180},{},[181,185,186,191,192],{"type":18,"tag":59,"props":182,"children":183},{},[184],{"type":24,"value":97},{"type":24,"value":99},{"type":18,"tag":59,"props":187,"children":188},{},[189],{"type":24,"value":190},"算法原理",{"type":24,"value":99},{"type":18,"tag":59,"props":193,"children":194},{},[195],{"type":24,"value":97},{"type":18,"tag":197,"props":198,"children":200},"h3",{"id":199},"动态vit推理训练过程",[201],{"type":18,"tag":59,"props":202,"children":203},{},[204],{"type":24,"value":205},"动态ViT推理训练过程",{"type":18,"tag":26,"props":207,"children":208},{},[209],{"type":24,"value":210},"受到上述现象的启发，论文提出了一个动态视觉Transformer框架（Dynamic Vision Transformer，DVT），意在针对每个样本选择一个合适数目的token来进行表征。",{"type":18,"tag":26,"props":212,"children":213},{},[214],{"type":24,"value":215},"首先介绍DVT的推理过程，如图4所示，对于任意测试样本，首先将其粗略表征为最小数目的token，判断预测结果是否可信，若可信，则直接输出当前结果并终止推理；若不可信，则依次激活下一个Transformer将输入图片表征为更多的token，进行更细粒度、但计算开销更大的推理，得到结果之后再次判断是否可信，以此类推。值得注意的是，处于下游的Transformer可以复用上游模型已产生的深度特征（特征复用机制）和注意力图（关系复用机制），以尽可能避免进行冗余和重复的计算。",{"type":18,"tag":26,"props":217,"children":218},{},[219],{"type":18,"tag":30,"props":220,"children":222},{"alt":7,"src":221},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/84a2af0dbd6945219162242e757c0fce.jpg",[],{"type":18,"tag":26,"props":224,"children":225},{},[226],{"type":24,"value":227},"图4 Dynamic Vision Transformer（DVT）",{"type":18,"tag":26,"props":229,"children":230},{},[231],{"type":24,"value":232},"训练网络在所有出口都取得正确的预测结果，训练目标如下式所示。其中和分别代表数据和标签，代表第个出口的softmax预测概率，代表交叉熵损失。",{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":18,"tag":30,"props":237,"children":239},{"alt":7,"src":238},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/880e743c91044b39933af721e9af8287.jpg",[],{"type":18,"tag":26,"props":241,"children":242},{},[243],{"type":18,"tag":59,"props":244,"children":245},{},[246],{"type":24,"value":247},"特征复用机制",{"type":18,"tag":26,"props":249,"children":250},{},[251],{"type":24,"value":252},"当一个处于下游位置的Transformer被激活时，应当训练其在先前Transformer的特征的基础上进行进一步提升，而非完全从0开始重新提取特征。基于此，论文提出特征复用机制使训练更为高效，如下图5所示。将上游Transformer最后一层输出的特征取出，经多层感知器变换和上采样后，作为上下文嵌入整合入下游模型每一层的多层感知器模块中。",{"type":18,"tag":26,"props":254,"children":255},{},[256],{"type":18,"tag":30,"props":257,"children":259},{"alt":7,"src":258},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/4aa09ab73a244f7fa47b71cc0f7833d0.jpg",[],{"type":18,"tag":26,"props":261,"children":262},{},[263],{"type":24,"value":264},"图5 特征复用（Feature Reuse）机制",{"type":18,"tag":197,"props":266,"children":268},{"id":267},"关系复用机制",[269],{"type":18,"tag":59,"props":270,"children":271},{},[272],{"type":24,"value":267},{"type":18,"tag":26,"props":274,"children":275},{},[276],{"type":24,"value":277},"除特征复用机制外，位于下游的模型同样可以借助上游Transformer已经得到的注意力图来进行更准确的全局注意力关系建模，基于此，提出关系复用机制，示意图如下图6所示。将上游模型的全部注意力图以logits的形式进行整合，经MLP变换和上采样后，加入到下层每个注意力图的logits中。这样，下游模型每一层的attention模块都可灵活复用上游模型不同深度的全部attention信息，且这一复用信息的“强度”可以通过改变MLP的参数自动地调整。",{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":18,"tag":30,"props":282,"children":284},{"alt":7,"src":283},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/099d48bc1c6040e590121a8dba536ebc.jpg",[],{"type":18,"tag":26,"props":286,"children":287},{},[288],{"type":24,"value":289},"图6 关系复用（Relationship Reuse）机制",{"type":18,"tag":26,"props":291,"children":292},{},[293],{"type":18,"tag":59,"props":294,"children":295},{},[296],{"type":24,"value":297},"MindSpore实验效果",{"type":18,"tag":26,"props":299,"children":300},{},[301],{"type":24,"value":302},"MindSpore上仅需两行代码调用通用动态ViT（DVT）框架，如图7所示，输入两类token的ViT网络，并设置是否开启特征复用和关系复用，完成训练后，如图8所示，即可在ImageNet上获得1.6-3.6倍的推理加速！",{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":18,"tag":30,"props":307,"children":309},{"alt":7,"src":308},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/2c3c1c28b4bb4e59a74c6b045688491d.jpg",[],{"type":18,"tag":26,"props":311,"children":312},{},[313],{"type":24,"value":314},"图7 MindSpore DVT调用方法",{"type":18,"tag":26,"props":316,"children":317},{},[318],{"type":18,"tag":30,"props":319,"children":321},{"alt":7,"src":320},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/a050b9681acd417c92d2548eb370cda3.jpg",[],{"type":18,"tag":26,"props":323,"children":324},{},[325],{"type":24,"value":326},"图8 MindSpore+DVT（DeiT）在ImageNet上的计算效率",{"type":18,"tag":26,"props":328,"children":329},{},[330],{"type":24,"value":331},"图9给出了简单和困难的可视化样本结果。",{"type":18,"tag":26,"props":333,"children":334},{},[335],{"type":18,"tag":30,"props":336,"children":338},{"alt":7,"src":337},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/c7116895489b45c08b9ea9f2ebf80eaa.jpg",[],{"type":18,"tag":26,"props":340,"children":341},{},[342],{"type":24,"value":343},"图9 可视化结果",{"type":18,"tag":26,"props":345,"children":346},{},[347,351,352,357,358],{"type":18,"tag":59,"props":348,"children":349},{},[350],{"type":24,"value":97},{"type":24,"value":99},{"type":18,"tag":59,"props":353,"children":354},{},[355],{"type":24,"value":356},"总结",{"type":24,"value":99},{"type":18,"tag":59,"props":359,"children":360},{},[361],{"type":24,"value":97},{"type":18,"tag":26,"props":363,"children":364},{},[365],{"type":18,"tag":59,"props":366,"children":367},{},[368],{"type":24,"value":369},"下面总结该研究的价值：",{"type":18,"tag":26,"props":371,"children":372},{},[373],{"type":24,"value":374},"（1）针对视觉Transformer提出了一个自然、通用、有效的自适应推理框架，理论和实际效果都比较显著。",{"type":18,"tag":26,"props":376,"children":377},{},[378],{"type":24,"value":379},"（2）提出了一个颇具启发性的思路，对全部图片以固定方式划分patch的表征方式，是不够灵活和次优的，一个更合理的策略是，应当根据输入数据动态调整表征方式。这或为开发高效、可解释性强、迁移性好的通用视觉Transformer提供了新方向。",{"type":18,"tag":26,"props":381,"children":382},{},[383],{"type":24,"value":384},"（3）这种按输入数据动态调整表征方式的动态性推理也为应用至动态性训练提供了一种很好的方向。",{"type":18,"tag":26,"props":386,"children":387},{},[388,390,402],{"type":24,"value":389},"最后感谢清华大学黄高老师研究团队博士生王雨霖和中央媒体技术院专家的供稿，",{"type":18,"tag":59,"props":391,"children":392},{},[393,395],{"type":24,"value":394},"作者的主页链接：",{"type":18,"tag":45,"props":396,"children":399},{"href":397,"rel":398},"http://www.rainforest-wang.cool",[49],[400],{"type":24,"value":401},"www.rainforest-wang.cool",{"type":24,"value":403},"。",{"type":18,"tag":26,"props":405,"children":406},{},[407],{"type":24,"value":408},"参考资料：",{"type":18,"tag":26,"props":410,"children":411},{},[412],{"type":24,"value":413},"[1] Wang Y, Huang R, Song S, et al. Not All Images are Worth 16x16 Words: Dynamic Vision Transformers with Adaptive Sequence Length[J]. arXiv preprint arXiv:2105.15075, 2021.",{"type":18,"tag":26,"props":415,"children":416},{},[417],{"type":24,"value":418},"[2] Dosovitskiy A, Beyer L, Kolesnikov A, et al. An image is worth 16x16 words: Transformers for image recognition at scale[J]. arXiv preprint arXiv:2010.11929, 2020.",{"type":18,"tag":26,"props":420,"children":421},{},[422,424],{"type":24,"value":423},"[3] ",{"type":18,"tag":59,"props":425,"children":426},{},[427],{"type":18,"tag":45,"props":428,"children":431},{"href":429,"rel":430},"https://www.mindspore.cn/",[49],[432],{"type":24,"value":429},{"type":18,"tag":26,"props":434,"children":435},{},[436,438],{"type":24,"value":437},"[4] ",{"type":18,"tag":59,"props":439,"children":440},{},[441],{"type":18,"tag":45,"props":442,"children":445},{"href":443,"rel":444},"https://github.com/blackfeather-wang/Dynamic-Vision-Transformer-MindSpore",[49],[446],{"type":24,"value":443},{"type":18,"tag":26,"props":448,"children":449},{},[450],{"type":24,"value":451},"扫描下方二维码加入MindSpore项目↓",{"type":18,"tag":26,"props":453,"children":454},{},[455],{"type":18,"tag":30,"props":456,"children":458},{"alt":7,"src":457},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/11/01/276b9fad37fe42b0a9378e0894c3c2e6.jpg",[],{"type":18,"tag":26,"props":460,"children":461},{},[462],{"type":24,"value":463},"MindSpore官方资料",{"type":18,"tag":26,"props":465,"children":466},{},[467,469],{"type":24,"value":468},"GitHub : ",{"type":18,"tag":45,"props":470,"children":473},{"href":471,"rel":472},"https://github.com/mindspore-ai/mindspore",[49],[474],{"type":24,"value":471},{"type":18,"tag":26,"props":476,"children":477},{},[478],{"type":24,"value":479},"Gitee : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":24,"value":484},"官方QQ群 : 871543426",{"title":7,"searchDepth":486,"depth":486,"links":487},4,[488,490],{"id":199,"depth":489,"text":205},3,{"id":267,"depth":489,"text":267},"markdown","content:technology-blogs:zh:769.md","content","technology-blogs/zh/769.md","technology-blogs/zh/769","md",1776506140896]