面向延迟敏感型搜索与推荐工作负载扩展基于 LLM 的排序系统
原题Scaling LLM-Based Ranking Systems for Latency-Critical Search & Recommendation Workloads
读前速览
本演讲介绍了 LinkedIn 如何通过优化模型、上下文和推理,扩展基于 LLM 的排序系统,以满足延迟敏感型搜索与推荐工作负载的需求。
由于存在共享前缀且没有解码阶段,LLM 排序与文本生成有着本质区别
与包含预填充和解码阶段的生成式工作负载不同,排序系统会在共享查询上下文的情况下处理多个项目,并且只需获取最终 token 的概率,因此需要采用专门的优化方法
多教师蒸馏可将模型规模从 7B 参数缩减至 400M 参数,同时保持质量
先使用针对相关性和互动指标优化的大型教师模型,再通过中间教师模型进行知识蒸馏,从而大幅减少参数量,并将质量损失控制在业务场景可接受的范围内
将上下文从 2000 个 token 压缩为单个嵌入,可实现 10x 吞吐量提升
将 LLM 用作嵌入模型,把完整职位描述压缩为单个 token 表示,可将每张 GPU 的吞吐量从每秒 290 个项目提升至 22,000 个项目,同时保持或提升排序质量
批量分词和服务端批处理可以消除推理中的 CPU 瓶颈
使用快速分词器 API 进行批处理,取代顺序执行的 CPU 分词,并实施服务端批处理,可以避免 GPU 空转,并高效处理高并发排序请求
应先优化模型和上下文,再规划硬件容量
生产工作负载的最佳表示方式,是在满足质量要求的前提下尽可能精简的表示,因此软件层面的优化比扩充硬件更能带来显著收益
完整摘要
LinkedIn 工程师 Sundara Raman Ramachandran 探讨了 LinkedIn 在大规模场景下扩展基于 LLM 的排序系统所面临的挑战及解决方案。演讲涵盖三个关键优化方向:通过多教师蒸馏将 7B 参数模型缩减至 400M 参数的模型优化;将职位描述从 2000 个 token 压缩为单个嵌入,在质量损失极小的情况下实现 10x 吞吐量提升的上下文优化;以及批量分词和服务端批处理等推理优化。演讲强调,对于生产环境中的 LLM 服务,精简模型和上下文比硬件预估更重要,并展示了 LinkedIn 如何在保持排序质量的同时,为其 1.3 billion 用户平台实现高吞吐量。
目录 · 9 节
文字记录Transcript
Sundara Raman Ramachandran:我叫 Sundara,是 LinkedIn 的技术负责人。目前,我主要负责大语言模型(LLM)推理,以及将 LLM 用于排序和推荐任务。我也是 SGLang 的开源贡献者,主要参与了评分 API 的开发,该 API 使用 LLM 进行排序。我有近 10 年的行业经验,曾在微软参与 Microsoft Office、Azure 等不同产品的工作。今天,我将介绍我们如何在 LinkedIn 扩展基于 LLM 的排序系统、遇到了哪些挑战,以及你可以从这次分享中学到什么、获得哪些启发。Sundara Raman Ramachandran: I'm Sundara. I'm a tech lead at LinkedIn. My current focus is on LLM inference and using LLMs for ranking and recommendation workloads. I'm also an open-source contributor in SGLang where I have primarily contributed to the scoring API where we use LLMs for ranking. I have close to 10 years of industry experience. I have worked with Microsoft in different products like Microsoft Office and Azure. Today I'm going to talk about how we scale the LLM based ranking systems at LinkedIn, what were the challenges we encountered, and what are some learnings and takeaways you can take from the talk.
LinkedIn 搜索LinkedIn Search
我希望大家都曾经用过 LinkedIn,可能用它找过工作,或者在社交网络中寻找某个人。我之所以这么问,是因为 LinkedIn 搜索需要在极大规模下运行。如果你还没读过相关内容,也许可以在演讲结束后打开 LinkedIn 应用,搜索一下我。如今,LinkedIn 在全球 200 个国家和地区拥有近 13 亿会员,我们的排序和推荐系统需要处理非常高的 QPS。搜索本身也在不断演进。过去,我们通常根据特定关键词进行搜索,输入搜索内容时还会使用特定的搜索筛选条件。如今,我们正逐渐转向用更自然的语言来表达意图。以 LinkedIn 的使用场景为例,你可能会搜索工作机会,输入:“帮我找娱乐行业的设计岗位。”I hope all of you would have used LinkedIn at some point, that you might have used it for searching for some job or you're looking for someone in a network. The reason I ask is LinkedIn Search operates at a massive scale. If you haven't read it already, maybe after the talk you can take up the LinkedIn app and maybe search about me. LinkedIn right now has close to 1.3 billion members worldwide across 200 countries, and we are operating a very high QPS for the ranking and recommendations. Search itself is evolving. Earlier we used to search based on specific keywords, and there were specific search filters that you put when you're entering any search query. Nowadays we are moving into more of a natural language to specify our intent. In LinkedIn's use case, you might be searching for a job where you enter, find me design roles in the entertainment industry.
或者,你正在人脉网络中寻找某个人,搜索查询可能是:“我的人脉中谁能帮我的企业申请专利?”这些都是难以理解的原始自然语言文本。仔细想想,这正是大语言模型能发挥巨大作用的地方。它们能帮助我们理解用户意图,并为用户提供更好的结果。在 LinkedIn 这样的规模下进行搜索和排序,本来就是一个足够复杂的问题;如果再引入大语言模型,难度就更高了。对我这样的工程师来说,这是一个非常有趣、值得解决的问题。Or you're searching for someone in your network where the search query could be, who in my network can help me file a patent for my business? All these are raw natural language texts which are complex to understand. If you think about this, this is where LLMs can be really powerful. They help us understand the user intent and then help us provide them better results. Search and ranking at LinkedIn scale is already a complex enough problem, and if you bring LLMs into the mix, it makes it even harder. For engineers like me, it's a really fun problem to solve.
架构Architecture
我们先来看看这些系统端到端运作的高层架构。一切都始于一个搜索查询,比如“帮我找娱乐行业的设计类职位”。搜索查询会经过第一层,也就是查询理解层。在这一层,我们会帮助判断用户的意图,弄清楚他们实际在寻找什么。接下来是检索层。LinkedIn 平台上发布了数百万个职位。检索层的任务是根据你的搜索查询,找出最相关的 top-K 个职位。然后,所有这些结果都会进入排序层,由它最终决定以什么顺序向大家展示结果。在这次演讲中,我会快速概述查询理解层和检索层,为整体内容提供更多背景。我这次演讲的重点将放在排序层。Let's look at the high-level architecture of how these things work end-to-end. It all starts with a search query where you have the find me design roles in the entertainment industry, and the search query goes through the first layer called query understanding layer. This is where we help figure out what the user intent is, what they're actually looking for. Then comes the retrieval layer. LinkedIn has millions of jobs posted in this platform. The job of the retrieval layer is to figure out the top-K most relevant jobs for your search query. Then all these results go to the ranker layer, which ultimately decides the order in which we want to show the results to you guys. In this talk, I'm going to quickly give an overview of the query understanding and the retrieval layer to add more context to the overall talk. My specific talk is going to be focused on the ranking layer.
我们先来看看查询理解层。查询理解层的目标是将用户输入的原始、含义模糊的自然语言文本,转换成更结构化、规范化的格式,以便后续系统能够理解。来看这样一个例子:一位会员是软件工程师,在湾区工作,拥有 5 年经验。他输入的搜索查询是:“查找我所在位置附近的软件工程师职位。”我们接收这个查询后,第一步是由查询规划器识别其中的搜索条件。在这个例子中,条件是职位名称和地点,规划器还会确定如何执行查询。接下来是查询标注器。这一步会为查询补充更多信息,这些信息可以从会员资料中提取,例如会员的职位名称、所在地,以及他们在 LinkedIn 上可能设置的一些搜索筛选条件。Let's look at the query understanding layer first. The goal of the query understanding layer is to turn the raw, ambiguous natural language text that the user has entered and convert that into more of a structured, normalized format, which the further systems can understand. Let's take this example where we have a member who's a software engineer in the Bay Area with 5 years of experience. The search query they have entered is, find software engineer jobs near my location. We take this query and then the first step here is the query planner, which identifies the search criteria here. In this case, it's a job title and location, and figures out a plan on how to execute it. The next comes the query tagger. This step enriches the query with more rich information, which we can pull from the member profile, like what is their job title, what is their location, and also some search filters they might have in their LinkedIn.
接下来是查询重写器,这基本上是这里最重要的部分。看看搜索查询,比如“帮我找一份我所在地点附近的软件工程师工作”,其中包含指代自身且含义模糊的文本。系统需要先弄清楚用户所在的位置,这正是查询重写器发挥作用的地方。它会把“我所在地点附近”替换成“湾区”,也就是用户所在的位置。所有这些内容都会被整理成一种规范化的格式,然后传递给检索层。Next comes the query rewriter, which is essentially the important piece here. If you look at a search query, we have something like a self-referencing, ambiguous text there, find me software engineer job near my location. Someone has to go and really understand what the user location is. That's where the query rewriter comes into play. It's going to replace the near my location with Bay Area, which is the user location. All of this gets packaged into a nice format, normalized enough, and then passed on to the retrieval layer.
现在进入检索层,我们的目标是根据用户查询找出语义上最相关的前 K 个职位。目前发布的职位有数百万个,我们不可能逐一对所有职位进行排序。流程从查询理解层的输出开始,我们将其转换为查询嵌入。然后使用该嵌入进行向量搜索,并在索引上执行 KNN 搜索。该索引是利用所有职位预先计算好的嵌入构建的。通过这一步,我们可以为搜索查询找出语义上最相关的前 K 个职位。在此步骤中,我们主要致力于优化召回率,而非精确率。具体来说,与其舍弃高度相关的职位,我们宁可让检索层多返回几个候选结果。Now when we move on to the retrieval layer, the goal here is for the user query, we have to find the top-K semantically relevant jobs. We have millions of jobs posted out there. We cannot go take every job and rank them. That's where the input starts with the query understanding layer output, and we take that and convert that into a query embedding. We then do a vector search with the embedding and do a KNN search on the index, which was pre-built using all the pre-computed embeddings from the jobs out there. From this step, we get the top-K most semantically relevant jobs for the search query. In this step, we primarily focus on optimizing for recall rather than precision. What we exactly mean by that is we would rather have a few extra candidates out of the retrieval layer than discarding something which is highly relevant.
检索层的输出接着会传递给排序层。正如前面提到的,我们会在这里确定按什么顺序向用户展示结果。我们希望把高度相关的结果放在第一位。如果你还没猜到的话,LinkedIn 目前正是用 LLM 来完成这项工作。和使用任何 LLM 一样,第一步都是提供足够的上下文,让 LLM 做出正确的判断。前两个层——查询理解层和检索层——的输出就构成了这个 LLM 的上下文。接下来是系统提示。你得编写提示词,告诉 LLM 要完成一项具体任务。你可能会惊讶于我们在生产环境中实际使用的内容。大致是这样的:我们会这样提示 LLM:“给定一个搜索查询、会员个人资料描述和一份职位信息,你能否输出‘是’或‘否’,判断这份工作是否符合该查询?”The output from the retrieval layer then is passed on to the ranking layer. As mentioned about previously, this is where we figure out what order we want to show the results to the user. We want to show the highly relevant one as the first item. If you haven't already guessed, in LinkedIn right now, we are employing LLMs to do this job for us. As with any LLM, the first step is to provide enough context for the LLM to make the right decision. That's where the output from the previous two layers, the query understanding and the retrieval layer becomes the context for this LLM. Then comes the system prompt. You have to build the prompt. You have to tell the LLM to do a specific job. You'll be surprised what we have in production. It goes by this, like we prompt the LLM, given a search query and given the member profile description and a job, can you output yes or no if the job matches the query?
实际上就是这样。这就是我们在生产环境中真正采用的方法。最后,我们会取模型预测“是”或“否”这一 token 的概率,并用它来计算最终分数。我们会对检索层提供的所有候选项都执行这一过程,然后再应用一些业务逻辑,决定优先向你展示哪个结果。It's literally that. That's what we really have in production. At the end of this, we take the probabilities of model predicting yes or no token, and use that to compute the final scores. We do this for all the candidates provided by the retrieval layer, and then finally do some business logic to show what is the first result we want to show to you.
零部分——LLM 排名是一个不同的问题Part Zero - LLM Ranking is a Different Problem
当你在如此大规模下运行系统、服务数十亿用户时,就需要优化底层的所有层次。我们主要分为三个层次。在介绍这些之前,我先说明一下为什么 LLM 排序是一个截然不同的问题。通常,我们一想到 LLM,就会想到聊天机器人文本生成。排序完全是另一类问题。 如果看生成式 LLM 的工作负载,会先有一个提供给 LLM 的提示词,随后进入一个称为预填充阶段的过程。在预填充阶段,LLM 会理解你提供给它的上下文。接下来是解码阶段;我们在这里称之为解码循环,因为我们是逐个 token 地生成文本。这个过程会经历多个步骤,例如采样和预测下一个 token。你需要获取词汇表中所有 token 的概率,并确定要向用户输出哪个 token,最终逐个 token 地生成文本。When you're operating systems at this scale and you're serving billions of members out there, you want to optimize all the layers underneath. That's where we have three main levels. Before going to that, I'm going to specify why LLM ranking is a very different problem. Usually, when we think about LLMs, we associate with chatbot text generation. Ranking is a very different problem all together. If you look at the generative LLM workloads, you have a prompt which you give to the LLM and it goes through a phase called prefill phase. Prefill phase is the one that the LLM understands the context you have given to it. Next comes the decode phase, and we call it decode loop here because we are generating text token by token. It goes through several steps like sampling, next token prediction. You want to get the probabilities of all the token and the vocabulary and figure out what is the token you want to output to the user, which ultimately gives you the generated text token by token.
这里的延迟会随着所需输出 token 的数量而变化。以排序用例为例,我们来看看职位排序场景。我们之前提到,查询是“湾区的软件工程师职位”,系统提示很简单,比如根据职位是否匹配,输出 yes 或 no。要排序的候选列表,也就是待排序的项目列表,就是职位描述。 这里的主要区别在于,假设我们要对 50 个项目进行排序,就需要针对每个项目构造一个提示,并将这 50 个提示发送给 LLM。你会有查询项 1、查询项 2,以此类推,共 50 个项目。这些内容都会经过一个统一的批量预填充阶段。另一个重要区别是,所有这些提示在查询、系统提示以及提供给 LLM 的其他上下文方面,都共享一个较长的公共前缀。The latency here scales with the number of output tokens required. If you take a ranking use case, let's take the job ranking scenario. We talked about the query here is software engineer jobs in Bay Area, and the system prompt is simple, like output yes or no based on a job match. The list of candidates or the list of items to be ranked here is the job description. The main difference here, let's say we want to rank 50 items, we are constructing 50 prompts to the LLM for each item. You'll have a query item 1, query item 2, like that, 50 items. All of this goes through a single batched prefill phase. Another important difference here is all of this share a common long shared prefix in terms of the query, in terms of the system prompt, and all other context you feed to the LLM.
这些内容会在所有你想要排序的职位之间共享。这里的第三点是,不一定会有解码阶段。严格来说,我们不会生成任何文本。我们只需要得到“是”或“否”这个 token 的最终概率。为这样的系统提供服务时,主要挑战在于我们必须满足严格的 P99 延迟 SLA。想象一下,你打开 LinkedIn 搜索某些内容,而我们要花 10 秒才显示结果。这种体验就不好了。我们还要应对高并发:只展示一个职位或一个人脉中的人是不够的,我们必须展示大量候选项,通常有数百个。这些挑战要求我们从各个方面进行优化。这就是这三点发挥作用的地方。在 LLM 系统中,我们有模型、上下文和推理。These are shared among all the jobs you want to rank. The third one here is there is not necessarily a decode phase. We are not generating any text per se. All we need is the final probabilities of the yes or no token. The major challenges when serving a system like this is we are dealing with strict P99 latency SLAs. Imagine you go to LinkedIn and search something and we take 10 seconds to show the result. That is not a good experience. We are also dealing with high concurrency where it's not enough if we just show one job or one person in a network, we have to show a bunch of candidates, usually in the hundreds. These challenges require us to optimize on all possible trends. That's where these three come into play. In an LLM system, we have the model, we have the context, and we have the inference.
每当你处理这类系统时,都要尽可能缩小模型,并尽可能缩短上下文,因为在这里,token 就是货币。推理过程必须尽可能高效。在接下来的演讲中,我会深入介绍其中的每个部分,分享我们做了什么、哪些做法对我们有效,以及你们可以从中汲取哪些经验。Whenever you're working with these kinds of systems, you want to make the model as small as possible and you want to make the context as short as possible, because token here is the currency. Inference, you have to make it as efficient as possible. In the rest of the talk, I'm going to dive deep into each of these sections where I'm going to share what we did, what worked for us, and what are some learnings that you could take away from us.
第一部分 - 模型优化Part One - Model Optimizations
我们先从模型优化说起。目标很简单:假设你有一个模型,你想尽可能缩小它的规模。我们可以采用哪些技术呢?我们首先采用了一种称为多教师蒸馏的方法。我们从一个较大的模型开始,它大约有 70 亿个参数。这个模型非常擅长理解上下文,能够理解产品政策,也能理解查询与商品之间的相关性,但它从一开始就不是为了部署到生产环境中而设计的。这个教师模型,也就是较大的模型,其全部用途是提供高质量的训练信号。随后,我们通过一种称为知识蒸馏的过程,将这些知识迁移到两个独立的较小教师模型中。这两个模型分别针对不同的指标进行了优化:一个针对相关性,另一个针对互动度。这些指标用于排序和推荐任务。这里的相关性基本上指的是搜索结果与查询的相关程度。</numerusformLet's start with the model optimizations. The goal is simple. Like you have a model, you want to make it as small as possible. What are the techniques we can employ for this? The first thing we did is something called multi-teacher distillation. We started with the larger model, around 7-billion parameter size. This model is really good in understanding the context. It can understand the product policies and it can understand the relevance between the query and item, but it was never intended to be deployed in production. The whole purpose of this teacher model or the larger model is to provide high quality training signals. We then transfer this knowledge into two separate smaller teacher models by a process called knowledge distillation. We have two models which are optimized for separate metrics, one for relevance, another for engagement. These are metrics in ranking and recommendation workloads. The relevance here basically denotes how relevant is the search result for the query.
这里的互动参与度基本上是指:如果我把这个结果展示给用户,它能在多大程度上促成互动——用户是否会点击、是否会阅读,以及诸如此类的行为。现在,我们有两个较小的教师模型,分别针对这两项指标进行了优化。然后,我们利用这两个模型训练另一个小模型,也就是我们所说的 SLM 排序器(小型语言模型),让它学习这两个教师模型的目标。实际上,我们从一个更大的、拥有 70 亿参数的模型开始,通过多步、多教师的方式对其进行知识蒸馏,再将所有上下文压缩到较小的模型中。质量会有所损失,但对于你要解决的业务场景来说,这种损失应该可以接受。我们并没有止步于此,而是进一步研究如何让这个模型变得更小。这就要用到一种叫作剪枝的技术。The engagement here basically specifies, if I show this result to the user, how likely it is to drive engagement, whether the user will click, whether the user will read, and all those things. Now we have two smaller teacher models, we call it, which is optimized on these two metrics. We then use this and then train another small model, SLM ranker is what we call it, small language model, which learns the objectives from both of these teacher models. We essentially started with a larger 7 billion model and we were able to knowledge distill all this in a multi-step, in a multi-teacher way, and then compress all the context into the smaller model. You will have some quality loss, but it should be acceptable for the business use case you're solving. We didn't stop there. We looked into how to make this model even smaller. That's where a technique called pruning comes into play.
本质上,你拿到模型后进行一些试错,看看能不能移除一些层,比如一些 MLP 层或注意力头。然后看看质量损失是否在可接受范围内,能否继续提供服务。显然,剪枝不是没有代价的。移除模型中的任何部分,都会带来一定的质量损失。因此,我们必须再次蒸馏、重新训练并微调模型,使损失控制在可接受范围内。回顾一下模型优化过程:我们从一个 70 亿参数的大模型开始,采用多教师蒸馏,将其缩小到 0.6B。接着,我们对这个 0.6B 模型移除了一些层,最终将模型缩减到 4 亿个参数。现在模型已经非常小,我们可以直接将它部署到生产环境中提供服务。It's essentially, you take the model and you do some trial and error and figure out, can I remove some layers? Can I remove some MLP layers or any attention heads? Then see if there is an acceptable quality loss that you can still serve. Obviously, pruning doesn't come for free. Whenever you're removing something from the model, there will be some quality loss. That's why we have to again re-distill, re-train and fine-tune the model so that the loss is within the acceptable range. Just to recall the model optimizations, we started with the 7 billion larger model with the multi-teacher distillation. We got it to 0.6B. We took the 0.6B and then removed some layers and finally got this in the size of 400 million parameters. Now the model is super small that we can now already serve this in production.
第二部分——上下文优化Part Two - Context Optimizations
接下来,我想谈谈上下文优化。以职位排序场景为例,一份职位描述很容易就有 2000 个 token。当你要处理一个排序请求时,假设你有一个请求,以及 100 个需要排序的职位。这样一来,你一次可能就要处理 100 × 2000,也就是 20 万个 token。如果成员数量很少,比如只有几千人,我们可以轻松地提供服务。但在 LinkedIn 这样的规模下,拥有 13 亿成员时,这就行不通了。因此,我们开始探索如何压缩这些上下文。我们想到的第一步是使用摘要器,也就是用另一个 LLM 将这份 2000 个 token 的职位描述总结成 150 个 token。所有这些工作都在线下完成,我们绝不会在在线处理流程中计算这些内容。Next, I want to talk about the context optimization. If we take the job ranking scenario and a particular job description can easily be 2000 tokens. When you want to serve a ranking request, so you have a request and you have 100 jobs you want to rank. This could easily be 100 into 2000, like 200K tokens that you want to process in one shot. If we have very few members, let's say in the order of thousands, we can serve this easily. At LinkedIn scale, when you have 1.3 billion members, this is not possible. That's where we started looking into how can we compress this context. The first step we came up with is to use a summarizer, use another LLM to summarize this job description from 2000 tokens to 150 tokens. We do all this work offline. We never compute all this in the online path.
在线服务中,我们只需从数据库中获取摘要文本,然后将其作为输入传给排序器。这帮助我们把物品提示的长度从 2000 个 token 缩短到 150 个 token,减少了 16 倍。值得注意的是,我们在完成这一切的同时,质量损失不到 2%。这就是我们认真对待提示工程的地方。提示就是一种资源,你必须尽可能缩短它们。接下来的问题是,我们并未止步于此。我们开始寻找机会,思考如何进一步缩小物品表示。能不能不用 150 个 token,而只用一个 token?严格来说,并不是一个 token。能不能只用一个稠密向量来表示?或者简单来说,能不能只用一个嵌入向量?这正是我们的研究发挥作用的地方:我们把完整的文本描述传给 LLM,由它将其压缩成一个嵌入向量。In the online, we just fetch the summarized text from the database and then pass it as input to the ranker. This helped us to bring the item prompt length all the way from 2000 tokens to 150 tokens, which is a 16x reduction in the prompt length. The interesting thing to note here is we were able to get all this thing done with a less than 2% quality loss. This is where we took prompt engineering serious. Prompt is the currency, you have to make them as small as possible. In the next question, we didn't stop there. We were looking into opportunities, how we can make the item representation even smaller. Is it possible to just have one token instead of 150 tokens? Literally not one token. Can it just be one dense vector representation? Or in simple terms, can it just be one embedding? That's where our research comes into play, where we take the full text description and then pass it to an LLM, which basically compresses into just one embedding.
LLM 做得非常出色。它也可以用作嵌入模型:输入一段完整文本,就能得到一个嵌入向量。这样一来,就计算而言,我们已将 item 的 token 长度从 2000 压缩到仅 1 个 token。然后,所有这些操作仍然都在线下完成。在线链路中不会发生任何处理。我们只需启动服务,从数据库中获取这些数据,然后传给排序器。LLM does a really good job. It can also be used as an embedding model, where you can pass in a full text and you can get one embedding out of it. Now for all compute purposes, we have compressed the item token length from 2000 to just one token. Then, again, we do all this offline. Nothing happens in the online path. We just come up, fetch this from the database and then pass it to the ranker.
现在你可以问我:“Sundara,这真的很棒。你压缩了上下文,把文本从 2000 个词元一路缩减到 1 个词元。那质量表现如何?能否给出相关性结果?”接下来我就要讨论这张图。纵轴表示排序质量,横轴表示吞吐量,单位是每块 GPU 每秒处理的项目数。最左边的数据点是基线指标:我们使用完整的职位描述来获取结果。它的排序质量最高,但吞吐量也最低,每秒只能处理 290 个项目。第二个数据点对应的是摘要文本。我们使用摘要器将词元数量从 2000 个减少到 150 个。这让吞吐量从每秒 290 个项目提高到 2200 个,接近 8 到 9 倍,同时质量损失不到 2%,在可接受范围内。Now you can ask me, "Sundara, this is really great. You have context compressed, went all the way from 2000 to one token. How does it perform quality-wise? Does it give you relevance result?" That's where I'm going to discuss about this graph. On the y-axis, we have the ranking quality. On the x-axis, I have the throughput measured in terms of items per second per GPU. The leftmost data point over there is the baseline metric where we use the full job description to get the results. This is the one which gives the highest-ranking quality and also lowest throughput. We were able to get only 290 items per second. The second data point is the data point for summarized text. When we use summarizer to bring down the token size from 2000 to 150. This helped us to almost increase the throughput from 290 to 2200, close to 8x, 9x, with a less than 2% acceptable quality loss.
右侧第三个数据点就是我上一张幻灯片提到的那个:我们将完整的职位描述进行上下文压缩,最终只得到一个嵌入。你可以看到,x 轴上的数值从 2200 一直到接近 2.2 万。这相当于吞吐量提升了约 10 倍。这里还有一点更值得注意:从质量来看,相比摘要文本,你得到的质量略有提升。这里的关键结论是,最适合你工作负载的表示形式不一定是最长的,而是能满足你的用例所需质量的最短形式。它可以是完整文本、摘要文本,甚至只是一个嵌入。我对在生产环境中提供模型服务的人最大的建议是,把模型和上下文都精简到位,尽可能缩短。在进行任何硬件估算或容量规划之前,先把模型和上下文确定好。The third data point on the right is the one I talked about in the previous slide, where we take the full job description and then context compress into just one embedding. You can see the x-axis where the numbers run from 2200 to almost 22k. This is like a 10x improvement in the throughput which you can serve. Even more interesting thing to note here is, if you see the quality, you were able to get a slightly better quality compared to the summarized text. The key takeaway here is, the best representation for your workload is not necessarily the longest one. It's the smallest one that can give you the acceptable quality your use case needs. It can be the full text, or it can be a summarized text, or it can just be even one embedding. My biggest recommendation for people serving models in production is to get the model and context really tight. Make it as short as possible. Before even you do any hardware estimates or capacity planning, get your model and context right.
第三部分 - 推理优化Part Three - Inference Optimizations
在我们的使用场景中,把这两方面都做好之后,我们开始研究推理。推理是一个非常有意思的问题。现在已经有许多优秀的开源引擎,例如 SGLang、vLLM 和 TensorRT-LLM,它们在大多数使用场景中都能提供业界领先的吞吐量。但这并不意味着它们一定能很好地适用于你的使用场景。我们就是这样着手解决这个问题的:没有把推理当作一个已经解决的问题,而是开始研究我们的工作负载有哪些不同之处,以及有哪些优化机会。在接下来的几张幻灯片中,我会深入探讨推理方面的内容。核心想法是,存在许多瓶颈,它们并非与推理本身有关,而是与系统、运行时有关;也可能与所服务的引擎有关,或者与所服务的语言有关。我会逐一指出这些问题。In our use case, once we got both of them right, we started looking into inference. Inference is a really interesting problem. We already have great open-source engines out there like SGLang, vLLM, TensorRT-LLM, and they already provide state-of-the-art throughput for most of the use cases. It doesn't necessarily mean that for your use case, it's going to work very well. That's how we approach the problem. We didn't treat inference as a solved problem. We started looking into what is different in our workload and what are all the optimization opportunities we can look out for. In the next few slides, I'm going to dive deeper into the inference side of things. The whole idea here is there are a lot of bottlenecks, not inference related, but system related, runtime related, maybe related to the engine you are serving, maybe related to the language you are serving, which I am going to call out.
这些问题也可能隐藏在你的技术栈中。第一个瓶颈出现在 CPU 端。使用 LLM 为生产系统提供服务时,第一步是一个叫作分词器的组件。在我们的代码中,我们会对一批项目进行处理。回到排序工作负载:假设你有一个请求,需要对 50 个项目进行排序。你会把大约 50 条提示词作为一个批次输入给 LLM。在我们的代码中,分词器是在 CPU 端顺序运行的。也就是说,它在 for 循环中逐条处理,导致 GPU 陷入等待,而 CPU 却处于空闲状态。为了解决这个问题,我们将其替换为批量分词器。也就是说,Hugging Face API 提供了快速分词器 API,你可以一次发送一批提示词,它会在多个核心上并行处理。These might be hiding in your stack as well. The first bottleneck was on the CPU side. When you are serving production systems using LLMs, the first step is something called a tokenizer. In our code, what we had was we were taking the batch of items. Let's go back to the ranking workload. You have a request where you want to rank 50 items, let's say. You have like 50 prompts as a batch input to the LLM. In our code, what was happening was the tokenizer was happening sequentially on the CPU side. Basically, this was happening in the for loop one by one, it was stalling the GPU and the CPU was just sitting idle. In order to fix this, we replaced this with a batch tokenizer. What it means is Hugging Face APIs provide a fast tokenizer API, where you can send a batch of prompts and then it will parallelize the work across multiple codes.
内部实现采用的是 REST,但对于我们的使用场景,开源方案已经提供了所有这些功能。你只需要调用这些 API 即可。顺理成章地,我们接下来思考的是:能否在服务端做些什么,这样对于客户端无法自然地进行批处理并发送批量请求的工作负载,我们就不必在客户端重复实现批处理逻辑。我们确实有很多生成嵌入的工作负载,其中客户端要么无法在客户端进行批处理,要么使用的是由多个客户端共享的模型。如果想在客户端进行批处理,就必须在多个客户端中重复实现这套逻辑。因此,我们引入了类似服务端批处理的机制:它会跨请求进行批处理,然后使用我之前提到的同一个快速分词器 API,一次性完成批量分词。这里需要特别强调的是,如果你有任何生产系统,请确保进行批量分词。所有这些 API 都已在开源社区中提供,只要确保你在使用它们即可。Internally, it's implemented using REST, but for our use case, all this is available in open source. You just have to leverage these APIs. A natural extension of this was, can we do something on the server side so that we don't have to repeat this client-side batching for workloads where clients cannot naturally batch and send a batch request. We did have a lot of embedding generation workloads where either the client was not able to batch on the client side, or we had a shared model where we had multiple clients. If you want to do this batching on the client side, you have to repeat this logic across multiple clients. That's where we introduced something like server-side batching, which is going to batch across the request and then do one batch tokenization using the same fast tokenizer API, which I mentioned before. The important callout here is, if you are having any production systems, make sure you are doing batch tokenization. All these APIs are available out there in open source. Just make sure you are leveraging them.
下一个瓶颈则更加隐蔽。正如我提到的,在基于 LLM 的系统中,会有一个 tokenizer 进程,它负责接收提示词并确定对应的 token ID。此外还会有一个调度器进程,也就是某种 scheduler,负责准备任务并将任务分发到 GPU。检查系统时,我们发现自己一次发送了 50 条提示词,这些内容都可以放进模型上下文中。但从日志中我们看到,这些提示词被拆分成了更小的批次。随后,我们对系统进行了性能分析,找到了根本原因:这两个进程之间的进程间通信。具体来说,我们通过 ZMQ 消息,从 tokenizer 进程向调度器进程逐条发送提示词,每条提示词都单独发送。要解决这个问题,修复方法其实也很简单。The next bottleneck was even more subtle. When you are having an LLM based system, as I mentioned, you will have the tokenizer process, which is going to take the prompt and then figure out the token IDs. There will also be a scheduler process, some sort of scheduler, which is the one which is preparing the work and then dispatching the work to the GPU. When we looked into our system, we were sending a batch of 50 prompts. All of this can fit into the model context. What we saw in the logs was, this was getting split into smaller batches. Then we profiled the system and then figured out the root cause here to be the inter-process communication between these two processes. Essentially, from the tokenizer process to the scheduler process, we were sending the prompts one by one in a separate ZMQ message. In order to fix this, again, the fixes here are really simple.
你只需要始终保持批次完整性。将所有内容放在一条 ZMQ 消息中发送,这样整个批次就能一次性从分词器传递到调度器。这张幻灯片的一个重要结论是:如果你的生产系统基于 Python,那么你肯定会出于某种原因使用多进程。我会在接下来的几张幻灯片中讨论原因。使用多进程时,务必在进程之间使用正确的通信协议。发送请求时,也要确保保持批次完整性。You just want to maintain the batch integrity throughout. You just send all of them in a single ZMQ message, so that the entire batch is passed on from the tokenizer to the scheduler in one shot. A key takeaway from this slide is, if you are having production systems based in Python, you are definitely leveraging multiprocessing for some reason. I'll discuss about why in the next few slides. Make sure when you are using multiprocessing, you are using the right set of communication protocol between them. Make sure you're maintaining the batch integrity when you're sending the request.
接下来讲下一个问题。我在幻灯片开头部分提到过,排序和文本生成完全不同。我们再来梳理一遍。文本生成会经历预填充阶段和解码阶段;在解码阶段,我们会逐个生成 token,预测词汇表中所有 token 的概率,然后选出概率最高的那个,或者根据某种启发式方法来选择。这就是采样阶段。为了实现这一过程,你需要把所有这些概率,也就是所有这些浮点数,从 GPU 复制到 CPU。这里面有大量对排序来说并不必要的工作。如果考虑排序或评分场景,你只需要进行一次大规模预填充,然后在最后复制“是”或“否”的概率即可。On to the next one, I talked about this in the initial part of the slides, where ranking is completely different from text generation. Let's walk through it again. Text generation goes through the prefill phase, and a decode phase that is where we are going to generate token by token, and we predict the probabilities of all the tokens out there in the vocabulary and we are going to pick the one which is the top or based on some heuristic. That's the sampling phase. In order for that to happen you are copying all these probabilities, all these floats from GPU to CPU. There is a lot of work that is happening which are not required for ranking. If you think about the ranking or the scoring use case, you just have to do one large prefill, and then you have to copy the yes or no probabilities at the end.
这促使我们在开源项目中引入了专用的评分 API,绕过所有这些不必要的工作,最后只获取 logits。这个评分 API 对我们来说已经相当好用了,我们解决了大部分 CPU 端的瓶颈。之后我们将系统部署到生产环境,查看 GPU 利用率指标,却发现数据并不理想。我们百思不得其解:这是怎么回事?于是我们对系统进行了性能分析,使用了 PyTorch Profiler 等工具。结果发现,GPU 内核启动之间存在大量空闲间隔。接下来我会带你看这张图。第一行是分词器,和使用任何 LLM 一样,首先需要对文本进行分词。这一步是在 CPU 端完成的。接下来是调度器进程,它同样运行在 CPU 上,负责准备要发送到 GPU 的批次。This motivated us to introduce the dedicated scoring API in open source where we bypass all this unnecessary work and then just get the logits at the end. The scoring API was already really good for us, we were able to solve most of the CPU side bottleneck. Then we deploy the system in production and we see the GPU utilization metrics, and the metrics were not good. We were scratching our heads like, what is happening? Then we go profile it, use the PyTorch profile and everything, and then what we see here is there was a lot of idle gaps between the GPU kernel launches. I am going to walk you through this graph here. On the first line we have the tokenizer, as with any LLM you will first have to tokenize it. This is happening on the CPU side. Next comes the scheduler process which is again on the CPU, and this is the process which is preparing the batches to dispatch to the GPU.
第三行是 GPU 计算部分,其中包含 kernel 启动以及所有矩阵乘法。在我们的场景中,对于排序,我们会同时为 50 个提示词打分。我们的代码会针对每个请求、每个提示词,逐个复制“是”或“否”的概率,而且采用同步阻塞的方式。这实际上会阻塞调度器,使其无法为下一个请求启动下一个 kernel。为了解决这个问题,我们把同步阻塞调用改成了异步非阻塞复制,并且不再逐个复制每个项目的“是”或“否”概率,而是改为批量复制。这样一来,在调度下一个请求的 GPU 工作时,我们就能更好地与前一个请求的 CPU 工作重叠执行。 这张幻灯片的核心要点是:如果你使用 Python 系统,并且要处理张量之类的数据,请务必使用异步非阻塞复制。例如,使用张量并将其从 GPU 复制到 CPU 时,一个简单的 Python `.tolist()` 调用就会让复制变成阻塞操作。这些细微之处都会影响整体 GPU 利用率,而我们甚至还没涉及 kernel 层面;所有这些都发生在 CPU 端。The third line is the GPU compute where you have the kernel launches, all the matrix multiplications happening. In our case, for ranking, we are scoring 50 prompts at the same time. What we were doing in our code was we are copying the yes or no probabilities one by one for each request for each prompt in a synchronized blocking way. This essentially stall the scheduler from launching the next kernel launch for the next request. In order to fix this, we replace this synchronous blocking call into an asynchronous non-blocking copy, and also instead of copying the yes or no probabilities of each and every item, we replace with a batched copy. This helped us to better overlap the CPU work of the previous request while we dispatch the GPU work of the next request. The key takeaway from this slide is if you have Python systems and if you are working with tensors and all sorts of things, just make sure you are using asynchronous non-blocking copies. For example, like when we are using some tensor and copying the tensors from GPU to CPU, a simple dot to list Python call made the copy blocking. All these are subtle things which can affect your overall GPU utilization, and we have not even gotten into kernel yet, everything is happening on the CPU side.
接下来,在解决了所有 CPU 侧的问题、处理好所有 CPU 瓶颈之后,我们开始研究哪些工作是冗余的,并尝试弄清楚能否对其进行优化。我们再来看这个排序场景:你有一个查询和若干条目。对 50 条目进行排序时,你需要构造 50 个提示词。如果看查询路径,就会发现它会在所有条目中重复。我们每次都在重复处理查询。这就是我们着手研究如何复用这部分工作的原因,最终想出了一个办法:把所有提示词拼接成一个提示词,发送给 LLM。这里的蓝色方块表示查询,绿色方块表示第一个条目,后面跟着一个分隔符,再后面是条目 2。我们把它们全部拼接起来,作为一个提示词发送给 LLM。The next thing, once we solved all the CPU side, and all the CPU bottlenecks were handled, we were looking into what work we were doing redundantly and trying to figure out if we can optimize for it. Let's revisit the ranking use case where you have the query and the items. When you are ranking 50 items you are constructing 50 prompts. If you look at the query path, the query path is repeated across all items. We are redoing the query work every time. That is where we researched a bit and trying to see how we can reuse that work, and that is where we come up with an idea to concatenate all the prompts into just one single prompt to the LLM. The blue blocks here denote the query and the green is the first item followed by a delimiter followed by item 2. We just concatenate all of them, and it is just the one single prompt to the LLM.
这里的关键挑战是,假设我们有查询任务 1 和任务 2,当 LLM 处理任务 2 并为其做出决策时,不应该带入任务 1 的上下文,否则就会得出错误的结果。为此,我们稍微深入到了内核层,并在 Flash 内核中引入了自定义的 Item-Aware Attention Mask,这样 LLM 在处理任务 2 时就会忘记任务 1。通过这项优化,我们能够在单次处理中为 n 个项目打分,并自然地复用查询 KV,因为它在提示中只出现一次。The key challenge here is, let's say we have a query job 1 and job 2, when the LLM is processing the job 2 and making a decision for it, it should not bring the context of job 1, or else it is going to give you wrong results. That is where we went a little bit into the kernel and then we introduced the custom Item-Aware Attention Mask, inside the flash into the kernel, so that when the LLM sees job 2 it is going to forget about job 1. By doing this optimization we were able to score n items in single pass, and we were able to naturally reuse the query KVs because we only have it once in the prompt.
我知道我刚才稍微深入讲了些内核方面的内容,不过接下来的几张幻灯片会非常有意思,因为这些都是我们在生产环境部署时没料到的情况,而它们都和 Python 有关。我们的 Python 代码可能不少。我们在生产环境中遇到的第一个有意思的问题是:我们部署了系统,把它优化到足够快,然后将其部署到模型服务器上。查看生产环境指标时,我们发现 P99 延迟每隔几秒就会出现一次尖峰。再强调一下,对于搜索和推荐,我们对延迟的要求非常严格。通常,P99 必须低于 500 毫秒。我们查看图表,发现 P99 每隔几秒就会飙升约 300 毫秒。后来发现,原因是 Python 垃圾回收器。在找到这个原因之前,我们花了一些时间排查。通常首先会怀疑的是 QPS 突然升高,或者某个请求的提示词特别长。I know I went a little bit deeper into the kernel and all, but the next few slides will be really interesting because these are something we didn't expect when we deployed in production, and these are brought by Python. We might have a lot of Python code. The first interesting thing that we encountered in production was, we deploy the system, we make it fast enough, and we deploy in the model server. We go see the production metrics, we were seeing spikes every few seconds in P99 latency. Just to reiterate, for search and recommendation, we are operating at a very strict latency. Usually, the P99 has to be less than 500 milliseconds. We go see the graph and we see every few seconds the P99 spikes by 300 milliseconds or so. The reason here turned out to be the Python garbage collector. Before we even got to that, we spent some time, the usual suspects would be like, there was a surge in QPS, or there was one request that had a longer prompt.
这些就像是用户方面的罪魁祸首。你先逐一排除它们,然后还得花更多时间调试到底发生了什么,最后发现根本原因是垃圾回收器。在模型服务器中,加载模型时会创建很多生命周期很长的 Python 对象,它们会一直存活到整个进程结束。实际情况是,Python 垃圾回收器每隔几秒就会启动一次,扫描这些对象,判断应该释放哪些对象。这导致偶尔会出现近 300 毫秒的延迟峰值。为了解决这个问题,我们启动了模型服务器,并用一些预热请求对它进行预热。然后,Python 提供了一个很棒的 API,叫作 gc.freeze,它会告诉垃圾回收器:到目前为止创建的所有对象都会在模型服务器的整个生命周期内存活,所以可以忽略它们。These are like user culprits. You go rule them out, and then you have to spend more time debugging what's happening, and the root cause turned out to be the garbage collector. In model server, when you load the model and there are a lot of long-lived Python objects that get created that lives through the lifecycle of the overall process. What was happening is this Python garbage collector was kicking in every few seconds and was scanning each of these objects to figure out which one it should free. This contributed to almost 300-millisecond spike every now and then. In order to fix this what we did was we started the model server. We warmed it up using some warm-up request. Then Python gives you a beautiful API called gc.freeze, which tells the garbage collector, up until this point, all the objects that got created are going to live throughout the life cycle of the model server, just to ignore it.
此后,请求路径中创建的任何临时对象,都会成为垃圾回收器关注的对象。通过这个修复,我们成功让 P99 稳定了下来。接下来的瓶颈是 Python 臭名昭著的全局解释器锁(Global Interpreter Lock,GIL),它实际上使得 Python 字节码在同一时刻只能由一个线程执行一次。这和 Java、C++ 等语言不同,后者在线程并行工作时可以实现真正的并行。Python 完全是另一回事。在 LinkedIn 的系统中,我们使用现有的开源引擎,包括 SGLang 引擎,然后将它放在一个 gRPC 服务进程后面。请求以 gRPC protobuf 的形式到达,由服务进程负责反序列化请求。它还会处理一些可观测性、日志记录、指标等工作,然后将请求分发给 SGLang 工作进程。After this, whatever temporary object that gets created during the request path is the one the garbage collector will be interested in. With this fix we were able to get the P99 in a stable way. The next bottleneck was Python's infamous Global Interpreter Lock, which effectively makes Python bytecode executable by single thread only once at a time. It's different from how Java and C++ and all those things can give you true parallelism when you are working with threads. Python is a different thing all together. In LinkedIn's system, we take the open-source engines we have, and we take the SGLang engine, and then put it behind a gRPC servicer process. The request comes as a gRPC protobuf and the servicer process is the one which is going to deserialize the request. It's also going to do some observability, logging, metrics, and other stuff, and then dispatch the request to the SGLang worker process.
由于全局解释器锁(Global Interpreter Lock),当我们发送大量 QPS 时,单个服务进程就成了瓶颈。为了解决这个问题,我们将单进程替换为多个进程,让它们能够在不同的 CPU 核心上并行处理这项工作。通过多进程解决服务进程的瓶颈后,SGLang 工作进程又成了瓶颈;它实际上负责准备工作并将工作分发到 GPU。这里的解决办法也一样:不再使用单个进程,而是使用多个进程。 需要注意的是,每个进程都有自己的模型副本,也会有自己的 CUDA 流,并负责启动内核、执行 MemSync 等操作,实际上是以分时方式使用 GPU。在我们的场景中,这样做可行,是因为模型比较小。如今我认为 GPU 上有足够的 HBM,因此你们也应该能够采用这种部署方式。再次提醒,在基于 Python 的生产系统中,要留意全局解释器锁;解决它可能就是提升吞吐量的一个简单办法。Because of the Global Interpreter Lock, and when we send large QPS the single servicer process became the bottleneck. In order to fix this, we replaced a single process with multiple processes, so that all of them can do this work in parallel in separate CPU cores. Once we solve the bottleneck on a servicer with multiprocessing, the SGLang worker process which is effectively preparing the work and dispatching the work to the GPU became the bottleneck. The fix here is again the same, instead of one process you will have multiple processes. The callout here is, each process has its own model copy and it's going to have its own CUDA stream and do the kernel launches and MemSync and whatnot, and it's effectively going to time slice the GPU. In our case we were able to do it because the model was smaller. Nowadays I think we get enough HBM in the GPU, so you should be able to deploy this as well. The callout again here, in production systems based on Python, is, worry about this Global Interpreter Lock and this can be your single fix which can help improve the throughput.
第四部分——LinkedIn 规模下的经验教训Part Four - Learnings at LinkedIn Scale
我知道我在推理方面讲得有点深入了。接下来的四张幻灯片,我会尽量讲得非常简单。其实很简单。以下是你可以从这次演讲和我们的工作中汲取的主要经验。第一条是我所说的“对你实际提供的服务进行基准测试”。你可能会想:“Sundara,这不是显而易见吗?不然你还能怎么做?”我来讲个我们做过的小故事。我们在模型、上下文和推理方面做了各种出色的优化,然后进行离线基准测试,算出一块 GPU 能达到的吞吐量,再确定我们要提供多少 QPS。我们做完估算后,就去告诉管理层我们需要多少块 GPU。他们很满意,我们也很高兴他们满意。I know I went a little deeper into the inference side of things. I'm going to keep it really simple in the last four slides. It's pretty simple. These are the major learnings that you could take away from this talk and from our work. The first one is what I call benchmark what you serve. You will be like, "Sundara, that's so obvious. What are you doing otherwise?" I will tell a small story of what we did. We do all these beautiful optimizations in model, context, inference, and then we go and do an offline benchmark. We figure out the throughput we were able to get out of a GPU and then we know how much QPS we want to serve. We do the estimates, and we go inform our leadership, this is the amount of GPUs we need. They were happy and we were happy they were happy.
我们把它部署到生产环境并发送一些暗流量后,看到的结果却完全不同。我们没能达到离线基准测试中取得的成绩。我们百思不得其解:到底哪里出了问题?是硬件有问题吗?是特定的 GPU 有问题吗?还是客户端与服务器之间存在通信瓶颈?是不是网络出了什么状况?后来发现,是我们的基准测试方法不正确。模拟合成流量时,我们实际上是以均匀分布的方式发送请求。也就是说,在一个秒级时间间隔内,你会根据 QPS 来估算。你会说,我的吞吐量是每秒 1000 个作业、每秒 1000 个项目,或者类似的数值。模拟合成流量时,请求会以等间隔的方式发送。We go deploy this in production and we send some dark traffic, that's where we see an entirely different result. We were not able to realize the offline benchmark numbers we were able to get. We are scratching our head like, what is wrong here? Is there anything wrong with the hardware? Is there anything wrong with the specific GPU, or is there any communication bottleneck between the client and server? Something happening over the network. It turned out, we were doing our benchmarks incorrectly. When we are simulating synthetic traffic, we were effectively using even distribution to send the request. What it means is within a second interval, you are estimating based on QPS. You're going to say that my throughput is 1000 jobs per second, 1000 items per second, or something of that sort. When you're simulating the synthetic traffic, the requests are getting sent in an equally spaced manner.
它们都是以均匀分布到达的,间隔相同。在生产环境中为真实用户流量提供服务时,你不能指望用户遵循相同的模式。在一秒的时间间隔内,请求量可能会激增,导致队列深度增加。之后也可能会有一段较为平静的时期。这里最重要的一点是,如果你在生产环境中运行服务,最接近真实情况的做法,就是记录并回放生产流量。如果无法做到这一点,次佳方案是使用泊松分布来模拟合成流量,而不是采用均匀分布。一定要确保采用了正确的方法。第二个经验是我所说的“离线必须与在线一致”。这是什么意思?当你在生产环境中使用基于模型的系统时,会有一位工程师负责对模型进行微调。All of them arrive in a uniform distribution with equal spaces. In production, when you're serving real time user traffic, you cannot expect the users to follow the same patterns. Within a second interval, you might have a surge in requests so the queue depth increases. After that, there could be some quieter periods. The major takeaway from this here is if you are serving systems in production, the closest possible result you can get is if you can record and replay your production traffic. If you don't have that option, the second closest thing you could get is if you can simulate the synthetic traffic using a Poisson distribution instead of an even distribution. Just make sure you're doing the right thing there. The second learning is what I call offline must match online. What do we mean by this? When you're working with a model-based system in production, there is an engineer who's going to work on fine-tuning the model.
会有提示词工程师负责设计提示词,他们也有自己的一套代码。而部署到生产环境中的系统则完全不同。你可能会使用开源系统,也可能会使用自己的模型服务器。此外,还有一套与这两者都不同的默认配置。例如,一个简单的分词配置,比如添加序列结束符或序列开始符的配置,就可能彻底改变提示词本身。你必须确保离线系统与在线系统完全一致。不要等到生产环境中才发现这些差异或不一致之处。必须把差异检查设为一道门槛,未通过就绝不能上线。There'll be prompt engineers who are designing the prompts, and they have their own set of code. The system that gets deployed in production is entirely different. You might be leveraging an open-source system, or you might be leveraging your own model server. There are a set of defaults which is different from both these. Like for example, a simple tokenization config like add end of sequence or beginning of sequence config, can change the entire prompt in itself. You have to make sure there is absolute parity between the offline and the online system. Don't discover all these disparities or discrepancies in production. Disparity has to be a gate without which you cannot launch in production at all.
第三点体会是这场演讲的核心。当你在生产环境中运行系统、模型、大语言模型或类似事物时,并没有什么单一的解决方案。你必须从所有可能的环节提升吞吐量。你要尽可能缩小模型、缩短上下文,并尽可能提高推理效率。 最后一点体会是说给技术负责人、经理和领导们听的。当你们处理生产系统时,会涉及多个角色:推理工程师、模型工程师、提示词工程师,以及负责制定产品政策的产品经理。把所有这些角色聚在一起,形成一种固定的协作机制,安排定期会议,这一点非常重要。你们可以每天站会,也可以每周开会,让每个人都能分享想法,并相互核对、交叉验证所有内容。The third learning is the crux of the talk. There is no single fix here when you are serving systems in production, models, LLMs, anything of that sort. You have to squeeze in throughput from all the possible places. You have to make the model as small as possible, and you have to make the context as short as possible and make the inference as efficient as possible. The final learning, if there are any tech leads, managers, and leaders, this is for you guys. When you are dealing with production systems, there are multiple actors here. You have the inference engineers. You have the model engineers. You have the prompt engineers. You have the product managers who are defining the product policies. It's very important that we bring all of these actors into one setup, one meeting which is recurring. You can have a standup or a meeting every week so that each of them can share their idea, cross check, cross validate everything.
你会惊讶于在进入生产环境之前,光是通过这些会议就能发现多少问题。我们常常认为,优化就是要优化代码、CPU、GPU、Python、Java、C++、Rust,以及所有这些东西。但我们首先要做、也是最重要的一项优化,是把合适的人聚到一起,消除沟通障碍,确保他们能够分享想法,从而有效地改善当前整个系统。You will be surprised how many issues you can catch in these meetings before even you get to production. We often think about optimizations as, if we have to optimize the code, we have to optimize the CPU, GPU, Python, Java, C++, Rust, and all those things. The first and foremost optimization we had to do is with respect to bringing the right people together, you remove the communication barrier and make sure you allow them to share their ideas, and then effectively improve the overall system in play.
问题与解答Questions and Answers
参与者 1:你能再详细介绍一下那个单个 token 的嵌入吗?Participant 1: Can you tell me more about that single token embedding?
Sundara Raman Ramachandran:这里的想法是,你可以将 LLM 用作交叉编码器。本质上,这意味着你可以输入完整文本,然后得到一个单一的嵌入表示。获取这种嵌入有多种技术。你可以使用所谓的最后一个 token 池化:让模型完成一次完整的前向传播,然后只提取最后一个 token 的嵌入。我们认为,最后一个 token 包含了整段文本的完整上下文。这是一种方法。另一种方法是提取每个 token 的嵌入,然后对所有这些嵌入取平均,得到平均表示。还有多种其他技术。对我们来说,效果最好的是最后一个 token 的嵌入。我们将完整文本输入 LLM,将其用作编码模型,然后得到单一的职位嵌入表示。Sundara Raman Ramachandran: The idea here is you can use LLMs as a cross encoder. What it essentially means is you can give full text and then get one single embedding representation out of it. There are multiple techniques how you get this embedding. You can do some last token pooling is what they call. You make the model to do a complete forward pass and then just take the embedding from the last token. We expect the last token to have the complete context of the entire full text. That is one way. The other way is you can take the embedding for each token and then do an average of all these embeddings to get the average template. There are multiple techniques out there. What worked for us is the last token embedding. We take the full text, give it to an LLM, use it as an encoding model, and then get the one single job embedding representation.
实际上,如果你仔细想想,就像是从 2000 个 token 变成了一个 token。在大语言模型系统内部,运作方式就是这样。你输入一个 token 列表,比如 100 个 token。首先,这 100 个 token 都会分别转换成一个嵌入向量。这些嵌入向量会作为输入,参与模型中的整体矩阵乘法。然后,模型最终会输出另一个嵌入向量,接着你再把这个嵌入向量转换回来,将其映射为另一个 token,这就成了输出。嵌入向量就是矩阵运算的输入,所有运算都是在这些向量之间进行的。Effectively, if you think about it, like from 2000 tokens, this has become one token. In LLM systems internally, this is how it works. You give a list of tokens, let's say 100 tokens. All this first gets converted into one embedding for each of these 100 tokens. The embedding is the input to the overall matrix multiplication that goes into the model. Then it's finally going to spit out another embedding, and then you convert this embedding back, map it to another token, and that becomes your output. Embedding is the one which is the input to the matrix and everything is multiplied between.
参与者 2:你提到过对模型本身进行剪枝,去掉那些不需要的可见层。你是如何找出不需要的部分并将它们剪掉的?Participant 2: You talked about pruning the models themselves, the visible layers that weren't needed. How did you go about identifying the parts that weren't needed and pruning them?
Sundara Raman Ramachandran:这个问题更多是关于结构化剪枝:我们如何确定要剪枝的层和 MLP 层,以及采用了哪些技术。Sundara Raman Ramachandran: The question was more about the structured pruning, how we identified the layers and the MLP layers we have to prune, and what are the techniques we employed.
这部分就有点像研究中的反复试验。我们会拿到这个模型,也就是 0.6B 的学生排序器,然后采用一些基于 SFT 的技术,将某些层屏蔽掉。首先移除特定的 MLP 层,比如倒数第二层,然后再进行质量分析,判断当前质量是否足以满足使用场景的需求。我们会预测这种质量,然后再次采用后训练或预训练的方法来提升质量。还可以进一步进行大量基于 RL 的微调,以提升质量。这些就是我们采用的技术。This is where it comes in a bit of a trial and error, in a research way. Where we take the model, the 0.6B student ranker which we got, and then we do some SFT based techniques where we mask out certain layers. You start by removing certain MLP layers, one before the last layer, before we prune it, and then do some quality analysis to figure out if the current quality is good enough for the use case. You predict this quality and then you do again a post-training or a pre-training approach to improve the quality again. There is a lot of RL-based fine-tuning you can do further to improve the quality. Those are the techniques we employed.
问题是,使用该系统后我们能够提高多少效率。The question is how much efficiency we were able to save with using the system.
我们在这里主要优化两件事。第一,我们希望提升用户参与度。想想所有社交媒体网站,我们要确保两点:一是相关性,我们希望展示尽可能相关的结果。如果你在搜索某个人,第一条结果就必须是你要找的那个人。这是第一点。第二,如果你考虑的是职位排名之类的问题,我们真正关注的是职位参与度。最重要的是改善这里的产品指标。我们刚才把之前采用的技术和这项技术做了比较,看看我们能提升多少参与度、能提供多相关的结果。对于 LinkedIn 这样的大公司来说,其他方面的吞吐量要排在后面。等我们确认产品策略有了显著改善之后,再回过头来尝试优化吞吐量,看看还能提升多少。We are optimizing for two things here. One is we want to drive the user engagement. When you think about all the social media websites, we want to make sure two things here, one is the relevance. We want to show the most relevant possible result. If you are searching for someone, the first result has to be the exact person you are searching for. That is number one. The second thing, if you're thinking about job ranking and all, it's really the job engagement which we are looking for. The most important thing is to improve the product metrics here. We were just comparing with the earlier techniques, what we employed versus this, and how much engagement we are able to improve, how relevant results we are able to give. The throughput on the other things come next for big companies like LinkedIn. Once we make sure we are able to see significant product policy improvement, then we go back and try to optimize on how much throughput we can squeeze.
这是一个迭代过程。就目前而言,我们已经把优化做到足够好了。但如果你问的是之前的检索模型或排序模型,那么之前的排序模型只是一些小型 MLP 模型,只需做几次乘法运算,就能得到余弦相似度之类的结果。显然,这样会更快,但它能给出正确的答案吗?不能。如果这样做会给终端用户提供错误答案,那就毫无帮助。因此,我们会先比较产品质量,然后再考虑吞吐量、成本节省等因素。This is an iterative process. Right now, we are here, we have already optimized good enough, but if you are asking the question about previous retrieval models or previous ranker models, the previous ranker models were just small MLP models where you can just do a couple of multiplication and get the cosine similarity or something. Obviously, that's going to be faster, but is this the thing which is giving you the right answer? No. If as an end user experience, if I give you the wrong answer by doing that, that's not helpful. That's why we compare the product quality and then comes the throughput and cost savings and everything.
参与者 3:你观察到,使用这种基于 LLM 的重新排序方法与之前的重新排序方法相比,产品质量有何差异?Participant 3: What was the product quality difference that you saw between using this LLM based re-ranking versus the previous re-ranking methods?
Sundara Raman Ramachandran:我在博客里讨论过这些内容,也有几篇相关论文。我记得我们看到拍卖指标(也就是我们所说的相关性指标等)提升了大约 5% 到 10%。Sundara Raman Ramachandran: I've discussed all this in the blogs, and there are a couple of associated papers also. I think we were able to see, if I'm not wrong, close to like a 5% to 10% increase in the auction metrics, is what we call it, the relevance metrics and all.
参与者 4:这与排名无关,不过在检索信息时,你们如何处理安全问题,比如数据隔离?Participant 4: It's not related to the ranking, but for the retrieval, how do you handle the security part when you retrieve the information, the compartmentalization of the data?
Sundara Raman Ramachandran:你的意思是,当我们检索有关作业等信息时,如何处理其中的安全问题?Sundara Raman Ramachandran: You're asking, when we retrieve the information about the jobs and all, how do we handle the security part of it?
参与者 4:当用户提出问题时,你们如何确保检索到的结果仅限于用户授权访问的内容?面对各种剪枝和其他模型,你们如何处理这一部分?Participant 4: When the user asks a question, how do you make sure to retrieve the results only that the user allowed it to? With all the pruning and all other models, how do you handle this part?
Sundara Raman Ramachandran:我们来看一下职位排名的场景。职位描述或职位文本是公开发布的,其中不包含任何个人身份信息(PII)。如果存在这类信息,都会预先过滤掉。例如,在处理人员搜索或会员个人资料时,我们会屏蔽所有公开信息数据,或未在其 LinkedIn 个人资料中公开的私人数据。如果有出生日期之类的信息,也会被屏蔽。它根本不会输入模型。只有公开可获取的信息才会作为模型的输入。Sundara Raman Ramachandran: Let's look at the job ranking scenario. The job description or job text is posted in public. There is no PII data which is available in the job. All those things get pre-filtered if there is something. For example, when we are working with people search or member profile, we're going to mask all the public information data or the private data, which is not exposed in their LinkedIn profile already. If there is something, for example, the date of birth or something, that's going to get masked out there. It's not even fed into the model at all. Only the publicly available information goes as input to the model.
参与者 5:既然你说这已经投入生产环境了,而且我相信这种请求搜索每秒会发生数百万次请求,我想知道,这套大语言模型基础设施是如何与现有服务或服务器集成,以处理如此庞大的请求量的?Participant 5: Since you're saying this is in production, and I believe there's millions of requests every second happening for this kind of request search, I was wondering how this LLM infrastructure integrated with the existing service or server to handle this huge amount of requests?
Sundara Raman Ramachandran:我认为,人员搜索和职位搜索目前已经向所有高级用户推出了。我想,到今年年底,这些功能会逐步向所有用户开放。说到实际部署在生产环境中的服务器,我会在最后展示一张框图。这就是我们实际上部署在生产环境中的系统,也就是整个模型服务器的高层架构。我们使用的是基于 Python 的 gRPC 服务器,并采用了开源引擎 SGLang。它位于 Python gRPC 服务端之后。严格来说,它们都属于同一个应用程序,并没有拆分成多个部分。我认为,它并不是由多个服务端组成的,只是一个 Python 应用程序。归根结底,这些全都是 Python 进程。这就是模型服务器。基本上,它会对应我们在架构图中看到的排序器模块。它负责接收查询和候选项列表,并为每个候选项给出分数。Sundara Raman Ramachandran: I believe the people search and job search is right now rolled out to all premium users. It, I think, gradually will get rolled out to all users by end of this year. In terms of the actual server in production, I show a block diagram in the end. This is what we actually have in production. This is the entire model server high-level architecture. We are using a Python-based gRPC server, and we are leveraging an open-source engine, SGLang. It sits behind the Python gRPC servicer. It's all part of one application per se. It's not split into multiple, I believe. It's not like a multi-servicer and all, it's just one Python application. All these are just Python processes in the end. This is the model server. Essentially, this model server is the one which will fit into the ranker block, which we saw in the architecture diagram. This is the piece which is taking the query and the list of candidates, and it's going to give the scores for each of the candidates.
查看更多带有文字稿的演示文稿See more presentations with transcripts