[{"data":1,"prerenderedAt":348},["ShallowReactive",2],{"content-query-IvEC86EAby":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":342,"_id":343,"_source":344,"_file":345,"_stem":346,"_extension":347},"/technology-blogs/zh/3583","zh",false,"","基于MindSpore NLP实现BGE-M3模型论文解读","作者：未平 来源：知乎","2025-01-16","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/ade6081155d74e5ca658360da55308c6.png","technology-blogs","实践",{"type":15,"children":16,"toc":337},"root",[17,25,44,49,57,64,69,74,79,84,89,94,99,104,112,117,122,129,134,139,159,167,172,177,184,194,203,210,217,225,230,235,240,245,250,255,263,268,273,278,285,290,298,303,308,319,327,332],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于mindspore-nlp实现bge-m3模型论文解读",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29,31,37,39],{"type":24,"value":30},"**作者：**",{"type":18,"tag":32,"props":33,"children":34},"strong",{},[35],{"type":24,"value":36},"未平",{"type":24,"value":38}," ",{"type":18,"tag":32,"props":40,"children":41},{},[42],{"type":24,"value":43},"来源：知乎",{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":24,"value":48},"BGE M3模型，全称为M3-Embedding，主要通过自我知识蒸馏（Self-Knowledge Distillation）实现了在多语言、多功能和多粒度文本嵌入的显著进步。此模型支持超过100种工作语言的语义检索，能同时完成密集检索（Dense Retrieval）、多向量检索（Multi-Vector Retrieval）和稀疏检索（Sparse Retrieval）三种常见的检索功能，并能处理从短句到长文档（最多8192个令牌）不同粒度的输入。",{"type":18,"tag":26,"props":50,"children":51},{},[52],{"type":18,"tag":32,"props":53,"children":54},{},[55],{"type":24,"value":56},"模型创新点",{"type":18,"tag":58,"props":59,"children":61},"h2",{"id":60},"bge-m3-embedding模型的核心创新体现在以下几个方面",[62],{"type":24,"value":63},"BGE M3-Embedding模型的核心创新体现在以下几个方面：",{"type":18,"tag":26,"props":65,"children":66},{},[67],{"type":24,"value":68},"**1、多语言支持：**该模型支持超过100种语言的检索，不仅能够在同一语言内部进行检索，还能实现跨语言的检索功能。这种多语言支持对于多语言应用场景中的信息检索具有极大的实际价值，能够在全球化的语境中处理复杂的语言需求。",{"type":18,"tag":26,"props":70,"children":71},{},[72],{"type":24,"value":73},"**2、多功能检索：**M3-Embedding模型具有三种主要检索功能：",{"type":18,"tag":26,"props":75,"children":76},{},[77],{"type":24,"value":78},"**密集检索（Dense Retrieval）：**通过计算查询和文档的嵌入向量相似度来进行检索。",{"type":18,"tag":26,"props":80,"children":81},{},[82],{"type":24,"value":83},"**多向量检索（Multi-Vector Retrieval）：**基于多个嵌入向量的细粒度交互计算查询和文档的相关性。",{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"**稀疏检索（Sparse Retrieval）：**将输出嵌入用于估计每个词的重要性，从而实现稀疏检索。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":24,"value":93},"M3-Embedding模型不仅能够单独执行这些检索功能，还能将这些功能组合起来，通过加权求和的方式整合不同检索方式的相关性分数，实现更高精度的检索结果。",{"type":18,"tag":26,"props":95,"children":96},{},[97],{"type":24,"value":98},"**1、多粒度处理：**该模型能够处理从短句到长达8192个令牌的长文档输入，具备极强的适应性。相较于大多数只能处理短文本的模型，M3-Embedding显著提升了对长文档的处理能力，适用于长篇文档的语义匹配和信息检索。",{"type":18,"tag":26,"props":100,"children":101},{},[102],{"type":24,"value":103},"**2、自我知识蒸馏：**该模型的另一项关键创新是引入了自我知识蒸馏技术。自我知识蒸馏的原理是将多种检索功能生成的相关性分数作为教师信号，通过知识蒸馏来增强模型的学习效果。这种方法利用不同检索功能的异质性进行集成，类似于集成学习的思想，从而提升了模型的整体性能。",{"type":18,"tag":26,"props":105,"children":106},{},[107],{"type":18,"tag":108,"props":109,"children":111},"img",{"alt":7,"src":110},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/a6f0042c88d346eb9549593accd9c299.png",[],{"type":18,"tag":26,"props":113,"children":114},{},[115],{"type":24,"value":116},"M3-Embedding 多阶段自知识蒸馏训练过程",{"type":18,"tag":26,"props":118,"children":119},{},[120],{"type":24,"value":121},"**3、优化批处理策略：**为了支持大规模的多语言数据训练，M3-Embedding模型优化了批处理策略，采用了大批量处理和高训练吞吐量，极大地提高了训练效率。通过将训练数据按序列长度进行分组，减少了填充操作，从而更有效地利用GPU资源。这一策略使得模型在处理长文档时能够显著扩大批量大小，进一步提高嵌入的区分度。",{"type":18,"tag":26,"props":123,"children":124},{},[125],{"type":18,"tag":108,"props":126,"children":128},{"alt":7,"src":127},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/2b48117fe8d74deeb9d55d35bf11a65f.png",[],{"type":18,"tag":26,"props":130,"children":131},{},[132],{"type":24,"value":133},"数据按长度分组并采样。启用梯度检查点和跨GPU广播以节省内存。",{"type":18,"tag":26,"props":135,"children":136},{},[137],{"type":24,"value":138},"**4、高质量数据策划：**M3-Embedding模型的训练数据来源广泛，包含了三类数据：",{"type":18,"tag":140,"props":141,"children":142},"ul",{},[143,149,154],{"type":18,"tag":144,"props":145,"children":146},"li",{},[147],{"type":24,"value":148},"大规模的无监督数据，从多语言语料库中提取具有丰富语义结构的文本对；",{"type":18,"tag":144,"props":150,"children":151},{},[152],{"type":24,"value":153},"高质量的监督数据，覆盖了多种语言和任务；",{"type":18,"tag":144,"props":155,"children":156},{},[157],{"type":24,"value":158},"为长文档检索任务生成的合成数据，通过生成式预训练模型（如GPT-3.5）自动生成问题和对应的文档对，以增强模型对长文档的理解能力。",{"type":18,"tag":26,"props":160,"children":161},{},[162],{"type":18,"tag":32,"props":163,"children":164},{},[165],{"type":24,"value":166},"数据集上的评价指标得分",{"type":18,"tag":26,"props":168,"children":169},{},[170],{"type":24,"value":171},"在多项权威的多语言和跨语言信息检索基准测试中，BGE M3-Embedding模型的表现超越了多个现有模型，达到了新的性能标准。以下是该模型在MIRACL和MKQA等基准数据集上的表现：",{"type":18,"tag":26,"props":173,"children":174},{},[175],{"type":24,"value":176},"**1、MIRACL数据集：**该数据集包含18种语言的检索任务。M3-Embedding在密集检索（Dense Retrieval）、稀疏检索（Sparse Retrieval）和多向量检索（Multi-Vector Retrieval）三种检索任务中均取得了优异的成绩。例如，在nDCG@10的评估指标下，M3-Embedding的平均得分为71.5，显著优于其他方法。",{"type":18,"tag":26,"props":178,"children":179},{},[180],{"type":18,"tag":108,"props":181,"children":183},{"alt":7,"src":182},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/e61f4729be37479ba2845c92e581c8a5.png",[],{"type":18,"tag":26,"props":185,"children":186},{},[187,192],{"type":18,"tag":32,"props":188,"children":189},{},[190],{"type":24,"value":191},"2",{"type":24,"value":193},"、**MKQA数据集：**在25种语言的跨语言检索任务中，M3-Embedding同样表现出色。在Recall@100评估指标下，M3-Embedding的得分为75.5，远超大多数现有模型，尤其在低资源语言（如阿拉伯语、希伯来语等）中表现尤为突出。",{"type":18,"tag":26,"props":195,"children":196},{},[197,201],{"type":18,"tag":108,"props":198,"children":200},{"alt":7,"src":199},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/9c7c5973d9a64c0a9a9880235c3994d9.png",[],{"type":24,"value":202}," **3、MLDR和NarrativeQA：**M3-Embedding还在长文档检索任务中表现出色。在MLDR和NarrativeQA等长文档数据集上，M3-Embedding通过多功能检索的结合，实现了对长达8192个令牌文档的高效处理，进一步验证了其在长文档场景下的优越性。",{"type":18,"tag":26,"props":204,"children":205},{},[206],{"type":18,"tag":108,"props":207,"children":209},{"alt":7,"src":208},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/06eb2686f1c04742933a1484996f4fc5.png",[],{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":18,"tag":108,"props":214,"children":216},{"alt":7,"src":215},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/9406638952ba43e79a2a672539cb9f04.png",[],{"type":18,"tag":26,"props":218,"children":219},{},[220],{"type":18,"tag":32,"props":221,"children":222},{},[223],{"type":24,"value":224},"相比其他工作的优势",{"type":18,"tag":26,"props":226,"children":227},{},[228],{"type":24,"value":229},"与其他嵌入模型相比，BGE M3-Embedding具有以下显著优势：",{"type":18,"tag":26,"props":231,"children":232},{},[233],{"type":24,"value":234},"**1、多语言和跨语言能力：**M3-Embedding支持超过100种语言，并且能够进行高效的跨语言检索。相比于只支持少数几种语言的传统模型，M3-Embedding在全球化应用中具有更强的通用性和适应性。",{"type":18,"tag":26,"props":236,"children":237},{},[238],{"type":24,"value":239},"**2、多功能性：**M3-Embedding不仅能进行密集检索，还能同时支持稀疏检索和多向量检索。这种多功能的嵌入模型为信息检索系统提供了极大的灵活性，能够根据不同场景需求选择最合适的检索策略，并且支持多种检索功能的组合使用，从而在复杂任务中获得更高的检索精度。",{"type":18,"tag":26,"props":241,"children":242},{},[243],{"type":24,"value":244},"**3、自我知识蒸馏技术：**自我知识蒸馏技术使得M3-Embedding能够有效整合多种检索功能的优势，提升模型的整体性能。相较于传统的知识蒸馏方法，自我知识蒸馏无需依赖外部教师模型，能够更高效地训练多功能检索模型，降低了训练复杂度并提高了效果。",{"type":18,"tag":26,"props":246,"children":247},{},[248],{"type":24,"value":249},"**4、长文档处理能力：**M3-Embedding显著提升了对长文档的处理能力，支持最多8192个令牌的输入。这使得该模型在长文档检索和语义匹配任务中具有显著优势，尤其在法律、医学等需要处理大篇幅文档的领域，M3-Embedding的应用潜力巨大。",{"type":18,"tag":26,"props":251,"children":252},{},[253],{"type":24,"value":254},"**5、训练效率：**通过优化批处理策略，M3-Embedding能够在大规模多语言数据集上高效训练，同时保持高吞吐量和大批量处理能力。这不仅提高了模型的训练速度，也增强了模型对不同语言和长文档的处理能力。",{"type":18,"tag":26,"props":256,"children":257},{},[258],{"type":18,"tag":32,"props":259,"children":260},{},[261],{"type":24,"value":262},"使用 MindSpore NLP 进行模型评估",{"type":18,"tag":26,"props":264,"children":265},{},[266],{"type":24,"value":267},"对于MindSpore NLP的bge_m3实现，我们使用了官方相同的MKQA基准来评估bge_m3模型的跨语言检索性能。MKQA基准包含了25种非英语语言的查询，每个查询任务都需要从英文维基百科语料库中检索包含答案的段落。为了进行这一实验，我们利用了BEIR提供的精心处理过的语料库BeIR/nq，并采用了密集检索（Dense Retrieval）方式。主要的评价指标包括Recall@100和Recall@20。整个测试过程均与官方相同。",{"type":18,"tag":26,"props":269,"children":270},{},[271],{"type":24,"value":272},"由于资源限制，此次测试仅覆盖150000条数据，导致评估结果低于全量测试。根据测试结果，MindSpore NLP实现与官方实现的误差在1%以内。",{"type":18,"tag":26,"props":274,"children":275},{},[276],{"type":24,"value":277},"测试结果如下：",{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":18,"tag":108,"props":282,"children":284},{"alt":7,"src":283},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/17/c0498f55348047dfab88c71c537a9255.png",[],{"type":18,"tag":26,"props":286,"children":287},{},[288],{"type":24,"value":289},"如结果所示，MindNLP的实现在精度上与官方实现非常接近，误差均低于1%。",{"type":18,"tag":26,"props":291,"children":292},{},[293],{"type":18,"tag":32,"props":294,"children":295},{},[296],{"type":24,"value":297},"完整代码",{"type":18,"tag":26,"props":299,"children":300},{},[301],{"type":24,"value":302},"推理代码的实现参考FlagEmbedding/C_MTEB/MKQA/dense_retrieval at master · FlagOpen/FlagEmbedding (github.com)",{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":24,"value":307},"完整推理代码",{"type":18,"tag":26,"props":309,"children":310},{},[311],{"type":18,"tag":312,"props":313,"children":317},"a",{"href":314,"rel":315},"https://link.zhihu.com/?target=https%3A//github.com/fanxing-6/BGE-M3-MindNLP-TEST",[316],"nofollow",[318],{"type":24,"value":314},{"type":18,"tag":26,"props":320,"children":321},{},[322],{"type":18,"tag":32,"props":323,"children":324},{},[325],{"type":24,"value":326},"总结",{"type":18,"tag":26,"props":328,"children":329},{},[330],{"type":24,"value":331},"BGE-M3模型凭借M3嵌入模型凭借其在多语言支持、多功能性和处理不同数据粒度的能力上的优异表现，为文本嵌入领域带来了新的突破。",{"type":18,"tag":26,"props":333,"children":334},{},[335],{"type":24,"value":336},"建议各位开发者利用MindNLP等工具来加载并复现该模型的实验成果。MindNLP提供了一套与PyTorch风格一致的简洁接口，加载和评估预训练模型非常直接和高效。",{"title":7,"searchDepth":338,"depth":338,"links":339},4,[340],{"id":60,"depth":341,"text":63},2,"markdown","content:technology-blogs:zh:3583.md","content","technology-blogs/zh/3583.md","technology-blogs/zh/3583","md",1776506131601]