Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"连续批处理","local":"continuous-batching","sections":[{"title":"generate_batch","local":"generatebatch","sections":[],"depth":2},{"title":"ContinuousBatchingManager","local":"continuousbatchingmanager","sections":[{"title":"关闭管理器","local":"shutting-down-the-manager","sections":[],"depth":3},{"title":"添加请求","local":"adding-requests","sections":[],"depth":3},{"title":"按请求的采样参数","local":"per-request-sampling-parameters","sections":[],"depth":3},{"title":"获取结果","local":"retrieving-results","sections":[],"depth":3},{"title":"流式","local":"streaming","sections":[],"depth":3}],"depth":2},{"title":"ContinuousBatchingConfig","local":"continuousbatchingconfig","sections":[{"title":"KV 缓存页大小","local":"kv-cache-page-size","sections":[],"depth":3},{"title":"预填充批处理大小","local":"prefill-batch-size","sections":[],"depth":3},{"title":"批处理和调度限制","local":"batch-and-scheduling-limits","sections":[],"depth":3},{"title":"对数概率","local":"log-probabilities","sections":[],"depth":3},{"title":"CUDA 图","local":"cuda-graphs","sections":[],"depth":3},{"title":"异步批处理","local":"async-batching","sections":[],"depth":3},{"title":"编译","local":"compilation","sections":[],"depth":3},{"title":"快速解码路径","local":"decode-fast-path","sections":[],"depth":3},{"title":"CPU 卸载","local":"cpu-offloading","sections":[],"depth":3},{"title":"张量并行超时","local":"tensor-parallel-timeout","sections":[],"depth":3},{"title":"前缀缓存","local":"prefix-caching","sections":[],"depth":3}],"depth":2},{"title":"选择注意力后端","local":"choose-an-attention-backend","sections":[],"depth":2},{"title":"张量并行","local":"tensor-parallelism","sections":[],"depth":2},{"title":"滑动窗口注意力","local":"sliding-window-attention","sections":[],"depth":2},{"title":"后续步骤","local":"next-steps","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/38.Dfv3MLej.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--12vztif--><meta name="hf:doc:metadata" content="{"title":"Continuous batching","local":"continuous-batching","sections":[{"title":"generate_batch","local":"generatebatch","sections":[],"depth":2},{"title":"ContinuousBatchingManager","local":"continuousbatchingmanager","sections":[{"title":"Shutting down the manager","local":"shutting-down-the-manager","sections":[],"depth":3},{"title":"Adding requests","local":"adding-requests","sections":[],"depth":3},{"title":"Per-request sampling parameters","local":"per-request-sampling-parameters","sections":[],"depth":3},{"title":"Retrieving results","local":"retrieving-results","sections":[],"depth":3},{"title":"Streaming","local":"streaming","sections":[],"depth":3}],"depth":2},{"title":"ContinuousBatchingConfig","local":"continuousbatchingconfig","sections":[{"title":"KV cache page size","local":"kv-cache-page-size","sections":[],"depth":3},{"title":"Prefill batch size","local":"prefill-batch-size","sections":[],"depth":3},{"title":"Batch and scheduling limits","local":"batch-and-scheduling-limits","sections":[],"depth":3},{"title":"Log probabilities","local":"log-probabilities","sections":[],"depth":3},{"title":"CUDA graphs","local":"cuda-graphs","sections":[],"depth":3},{"title":"Async batching","local":"async-batching","sections":[],"depth":3},{"title":"Compilation","local":"compilation","sections":[],"depth":3},{"title":"Decode fast path","local":"decode-fast-path","sections":[],"depth":3},{"title":"CPU offloading","local":"cpu-offloading","sections":[],"depth":3},{"title":"Tensor parallel timeout","local":"tensor-parallel-timeout","sections":[],"depth":3},{"title":"Prefix caching","local":"prefix-caching","sections":[],"depth":3}],"depth":2},{"title":"Choose an attention backend","local":"choose-an-attention-backend","sections":[],"depth":2},{"title":"Tensor parallelism","local":"tensor-parallelism","sections":[],"depth":2},{"title":"Sliding window attention","local":"sliding-window-attention","sections":[],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="continuous-batching" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#continuous-batching"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->连续批处理<!----></span></h1><!--]--><!----> <p>连续批处理通过在每次生成步骤中动态重新调度批次,从而最大化 GPU 利用率。当某些请求完成时,新请求会立即加入,而无需等待整个批次完成。GPU 始终保持满载,吞吐量也维持在较高水平。</p> <blockquote class="tip"><p>在生产部署中,请使用 <a href="./serve-cli/serving_optims#continuous-batching">transformers serve</a>。它基于 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager">ContinuousBatchingManager</a> 构建,并暴露了一个兼容 OpenAI 的 HTTP 端点。</p></blockquote> <!--[1--><h2 class="relative group"><a id="generatebatch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#generatebatch"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->generate_batch<!----></span></h2><!--]--><!----> <p>连续批处理通过 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousMixin.generate_batch">generate_batch()</a> 提供支持。传入一个已分词的提示列表,待所有请求完成后即可获取全部结果。<code>generate_batch</code> 在内部处理调度,并阻塞直到所有请求完成。</p> <p>对于服务提供和流式传输用例,请直接使用 <a href="#continuousbatchingmanager">ContinuousBatchingManager</a> 来管理请求。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer | |
| <span class="hljs-keyword">from</span> transformers.generation <span class="hljs-keyword">import</span> ContinuousBatchingConfig, GenerationConfig | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen3-4B"</span>, | |
| attn_implementation=<span class="hljs-string">"flash_attention_2"</span>, | |
| device_map=<span class="hljs-string">"auto"</span>, | |
| dtype=torch.bfloat16, | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"Qwen/Qwen3-4B"</span>) | |
| prompts = [ | |
| <span class="hljs-string">"What's up?"</span>, | |
| <span class="hljs-string">"Name a cat breed."</span>, | |
| <span class="hljs-string">"Write a detailed history of quantum mechanics."</span>, | |
| ] | |
| inputs = [tokenizer.encode(p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> prompts] | |
| generation_config = GenerationConfig( | |
| max_new_tokens=<span class="hljs-number">64</span>, | |
| eos_token_id=tokenizer.eos_token_id, | |
| ) | |
| outputs = model.generate_batch(inputs=inputs, generation_config=generation_config) | |
| <span class="hljs-keyword">for</span> request_id, output <span class="hljs-keyword">in</span> outputs.items(): | |
| text = tokenizer.decode(output.generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"[<span class="hljs-subst">{request_id}</span>] <span class="hljs-subst">{text}</span>"</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="continuousbatchingmanager" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#continuousbatchingmanager"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ContinuousBatchingManager<!----></span></h2><!--]--><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager">ContinuousBatchingManager</a> 会运行一个后台线程,允许您独立地提交请求并获取结果。在每次生成步骤中,它会检查已完成的请求,并调度新请求加入批次。这对于流式处理、实时服务或按到达顺序提交请求的场景非常有用。</p> <p>使用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousMixin.continuous_batching_context_manager">continuous_batching_context_manager()</a> 来安全地启动和停止管理器。下面的示例包含变长输入。一旦最短的提示词生成完毕,它就会离开批次,而较长的提示词则继续生成。如果使用静态批处理,你必须将它们全部填充到相同长度。连续批处理会释放已完成的提示词,从而可以立即开始处理下一个提示词。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">with</span> model.continuous_batching_context_manager(generation_config=generation_config) <span class="hljs-keyword">as</span> manager: | |
| manager.add_request( | |
| input_ids=tokenizer.encode(<span class="hljs-string">"Write a detailed history of quantum mechanics."</span>), | |
| request_id=<span class="hljs-string">"long"</span>, | |
| max_new_tokens=<span class="hljs-number">512</span>, | |
| ) | |
| manager.add_request( | |
| input_ids=tokenizer.encode(<span class="hljs-string">"What's up?"</span>), | |
| request_id=<span class="hljs-string">"short_0"</span>, | |
| max_new_tokens=<span class="hljs-number">32</span>, | |
| ) | |
| manager.add_request( | |
| input_ids=tokenizer.encode(<span class="hljs-string">"Name a cat breed."</span>), | |
| request_id=<span class="hljs-string">"short_1"</span>, | |
| max_new_tokens=<span class="hljs-number">32</span>, | |
| ) | |
| <span class="hljs-keyword">for</span> result <span class="hljs-keyword">in</span> manager: | |
| text = tokenizer.decode(result.generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span>) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"[<span class="hljs-subst">{result.request_id}</span>] <span class="hljs-subst">{text}</span>"</span>)<!----></pre></div><!----> <p>您也可以调用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousMixin.init_continuous_batching">init_continuous_batching()</a> 来自行管理生命周期。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager = model.init_continuous_batching(generation_config=generation_config) | |
| manager.start() | |
| <span class="hljs-comment"># submit and retrieve requests...</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="shutting-down-the-manager" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#shutting-down-the-manager"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->关闭管理器<!----></span></h3><!--]--><!----> <p>Manager 会运行一个后台线程并持有分布式资源。关闭过程分为两个阶段,以便您可以选择如何处理正在进行的任务。</p> <p>调用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.stop">stop()</a> 以停止后台线程。默认情况下,管理器会停止接受新的提交,并等待队列中及正在处理的请求完成后再退出线程。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.stop()<!----></pre></div><!----> <p>传入 <code>hard_stop=True</code> 可立即放弃待处理的工作。已排队和正在进行的请求将抛出 <code>RuntimeError</code> 而不会完成。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.stop(hard_stop=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>调用 <code>stop</code> 后,<a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.add_request">add_request()</a> 和 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.add_requests">add_requests()</a> 会丢弃新的提交并记录一条警告。您可以再次调用 <code>start</code>,使用同一个管理器运行另一个生成会话。</p> <p>调用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.destroy">destroy()</a> 以释放分布式资源。如果 <code>destroy</code> 仍在运行,它会先停止管理器,且之后无法重启管理器。请在进程生命周期内完成连续批处理(continuous batching)后使用此方法。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.destroy()<!----></pre></div><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousMixin.continuous_batching_context_manager">continuous_batching_context_manager()</a> 负责处理此流程。退出时它会调用 <code>stop</code>,并调用 <code>destroy</code>,除非你传入 <code>persistent_manager=True</code> 以将管理器缓存在模型上供下一个会话使用。</p> <!--[2--><h3 class="relative group"><a id="adding-requests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#adding-requests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->添加请求<!----></span></h3><!--]--><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.add_request">add_request()</a> 用于提交单个请求。您可以提供 <code>request_id</code>,也可以让管理器自动生成一个。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.add_request(input_ids=input_ids, request_id=<span class="hljs-string">"my_request"</span>)<!----></pre></div><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.add_requests">add_requests()</a> 一次性提交一个批次。当启用块共享时,它会自动对输入进行排序,以最大化前缀缓存命中率。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.add_requests(inputs=inputs)<!----></pre></div><!----> <p>使用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.cancel_request">cancel_request()</a> 取消请求。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->manager.cancel_request(request_id=<span class="hljs-string">"my_request"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="per-request-sampling-parameters" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#per-request-sampling-parameters"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->按请求的采样参数<!----></span></h3><!--]--><!----> <p>启用 <code>per_request_processors</code>,以便在同一次前向传播中针对每个请求独立应用 <code>temperature</code>、<code>top_k</code> 和 <code>top_p</code>,从而允许不同请求使用不同的采样参数(例如,一个请求生成富有创意、高温度的输出,而另一个请求生成精确、低温度的输出)。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(per_request_processors=<span class="hljs-literal">True</span>) | |
| <span class="hljs-comment"># each request gets its own sampling parameters</span> | |
| manager.add_request(input_ids=inputs_a, temperature=<span class="hljs-number">0.9</span>, top_p=<span class="hljs-number">0.95</span>) | |
| manager.add_request(input_ids=inputs_b, temperature=<span class="hljs-number">0.1</span>, top_k=<span class="hljs-number">10</span>)<!----></pre></div><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> 中的每个参数都必须设置为非默认值,才能在运行时创建对应的 logits 处理器。例如,将 <code>temperature</code> 设置为 <code>None</code> 或 <code>1</code> 以外的值,以支持按请求控制温度。之后仍然可以创建温度为 <code>1</code> 的请求。</p> <!--[2--><h3 class="relative group"><a id="retrieving-results" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#retrieving-results"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->获取结果<!----></span></h3><!--]--><!----> <p>遍历管理器以在结果到达时接收它们。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">for</span> result <span class="hljs-keyword">in</span> manager: | |
| <span class="hljs-built_in">print</span>(tokenizer.decode(result.generated_tokens, skip_special_tokens=<span class="hljs-literal">True</span>))<!----></pre></div><!----> <p><a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.get_result">get_result()</a> 从输出队列中获取下一个结果。传入 <code>request_id</code> 可筛选特定请求。如果队列中的下一个结果不匹配,该结果会被重新入队,方法返回 <code>None</code>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># next available result</span> | |
| result = manager.get_result() | |
| <span class="hljs-comment"># filter for a specific request</span> | |
| result = manager.get_result(request_id=<span class="hljs-string">"my_request"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="streaming" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#streaming"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->流式<!----></span></h3><!--]--><!----> <p>在请求中设置 <code>streaming=True</code>,然后使用 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousBatchingManager.request_id_iter">request_id_iter()</a> 在生成 token 时迭代获取部分输出。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.generation.continuous_batching <span class="hljs-keyword">import</span> RequestStatus | |
| manager.add_request(input_ids=input_ids, request_id=<span class="hljs-string">"streamed"</span>, streaming=<span class="hljs-literal">True</span>) | |
| <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> manager.request_id_iter(request_id=<span class="hljs-string">"streamed"</span>): | |
| token = tokenizer.decode(chunk.generated_tokens[-<span class="hljs-number">1</span>:], skip_special_tokens=<span class="hljs-literal">True</span>) | |
| <span class="hljs-built_in">print</span>(token, end=<span class="hljs-string">""</span>, flush=<span class="hljs-literal">True</span>) | |
| <span class="hljs-keyword">if</span> chunk.status == RequestStatus.FINISHED: | |
| <span class="hljs-keyword">break</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="continuousbatchingconfig" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#continuousbatchingconfig"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ContinuousBatchingConfig<!----></span></h2><!--]--><!----> <p><a href="/docs/transformers/main/zh/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> 用于控制 KV 缓存、调度、CUDA 图、内存使用等。请通过 <code>continuous_batching_config</code> 参数将其传递给 <code>generate_batch</code> 或 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.ContinuousMixin.init_continuous_batching">init_continuous_batching()</a>,并与 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> 分开传递。这两个配置描述的内容不同:<a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> 以模型为中心,包含采样和停止参数;而 <a href="/docs/transformers/main/zh/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> 以硬件为中心,包含内存和调度参数。</p> <blockquote class="warning"><p>在 <a href="/docs/transformers/main/zh/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> 上设置 <code>continuous_batching_config</code> 已被弃用,会发出 <code>FutureWarning</code>,并将在 v5.19 中移除。</p></blockquote> <p>默认情况下,<code>max_batch_tokens</code> 为 <code>8192</code>,受可用 GPU 内存限制,且不会低于 <code>256</code>,而 <code>num_blocks</code> 会填满剩余的内存。请使用下表来帮助您选择合适的功能。</p> <table><thead><tr><th>功能</th><th>内存</th><th>吞吐量</th><th>延迟</th></tr></thead><tbody><tr><td><code>max_memory_percent</code> / <code>page_size</code></td><td>✓ 控制 KV 预算</td><td></td><td></td></tr><tr><td><code>max_batch_tokens</code></td><td>↑ 更大的输入缓冲区</td><td>✓ 更大的预填充批次</td><td>✓ 预填充受限时的 TTFT</td></tr><tr><td><code>scheduler</code></td><td></td><td>✓ 调度策略</td><td>✓ TTFT</td></tr><tr><td>CUDA 图</td><td>↑ 图存储</td><td>✓ 更低的调度开销</td><td>✓</td></tr><tr><td>异步批处理</td><td>↑ ~2× I/O 缓冲区</td><td>✓ 重叠 CPU/GPU</td><td></td></tr><tr><td>编译</td><td>↑ 仅预热时间</td><td>✓ 更快的正向传播</td><td>✓</td></tr><tr><td>快速解码路径</td><td>↑ 每个请求的块表</td><td>✓ 更快的仅解码步骤</td><td>✓</td></tr><tr><td>CPU 卸载</td><td>↑ 固定 CPU 内存</td><td>✓ 跳过部分重新预填充</td><td></td></tr><tr><td>前缀缓存</td><td>↓ 共享 KV 块</td><td>✓ 跳过冗余的预填充</td><td>✓ TTFT</td></tr><tr><td>分页注意力</td><td>↓ 无碎片</td><td>✓ 动态批次成员</td><td></td></tr><tr><td>滑动窗口</td><td>↓ 每层有界的 KV</td><td></td><td></td></tr><tr><td>按请求处理的处理器</td><td></td><td>✓ 每个批次使用不同的采样参数</td><td></td></tr><tr><td><code>max_requests_per_batch</code></td><td>↓ 限制 logits 缓冲区</td><td>✓ 限制批处理大小</td><td></td></tr><tr><td><code>safety_margin</code></td><td></td><td></td><td>✓ 保护解码延迟</td></tr></tbody></table> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.generation <span class="hljs-keyword">import</span> ContinuousBatchingConfig | |
| cb_config = ContinuousBatchingConfig( | |
| max_memory_percent=<span class="hljs-number">0.8</span>, | |
| page_size=<span class="hljs-number">256</span>, <span class="hljs-comment"># KV cache page size in tokens</span> | |
| scheduler_type=<span class="hljs-string">"fifo"</span>, <span class="hljs-comment"># "fifo" or "prefill_first"</span> | |
| ) | |
| outputs = model.generate_batch( | |
| inputs=inputs, | |
| generation_config=generation_config, | |
| continuous_batching_config=cb_config, | |
| )<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="kv-cache-page-size" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#kv-cache-page-size"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->KV 缓存页大小<!----></span></h3><!--]--><!----> <p><code>page_size</code> 设置单个注意力层中一个完整 KV 页面所容纳的 token 数量。该值必须至少为 <code>4</code>,否则初始化时会抛出 <code>ValueError</code>。默认值 <code>256</code> 适用于大多数工作负载。</p> <blockquote class="note"><p><code>block_size</code> 参数已弃用,并由 <code>page_size</code> 替代。</p></blockquote> <p>更大的页面大小可以减少簿记开销,但当序列在页面中间停止时会浪费空间。较小的页面浪费的空间更少,但需要更多的簿记。通常无需调整 <code>page_size</code>。如果您的工作负载包含许多短请求,可以尝试较小的值。如果包含少数非常长的请求,可以尝试较大的值。有关页面、块和块如何协同工作,请参阅 <a href="./continuous_batching_architecture#memory-management">内存管理</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(page_size=<span class="hljs-number">128</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="prefill-batch-size" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prefill-batch-size"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->预填充批处理大小<!----></span></h3><!--]--><!----> <p><code>max_batch_tokens</code> 用于设置单次前向传播的 token 预算,即模型一次处理的最大查询 token 数量。更大的预算可以将更多的 prompt token 打包到每次 prefill 中,从而提升 prefill 吞吐量,并降低以 prompt 为主的工作负载的首 token 延迟。调度器会将超出预算的 prompt 拆分到多个步骤中处理。有关如何处理超大 prompt,请参阅 <a href="./continuous_batching_architecture#chunked-prefill">分块 prefill</a>。</p> <p>默认情况下,<code>max_batch_tokens</code> 为 <code>8192</code>。该值受可用 GPU 显存的限制,以确保每个批次的输入张量不会挤占 KV 缓存的空间,并且其值不会低于 <code>256</code>。在空闲显存较少的 GPU 上,该限制会将其值降低到 <code>8192</code> 以下。</p> <p><code>max_batch_tokens</code> 和 <code>num_blocks</code> 共享同一内存预算,因此二者相互制约。较大的 token 预算会分配更大的输入缓冲区,从而为并发或长请求保留更少的 KV cache 块。当可用内存更充足时,可提高 <code>max_batch_tokens</code> 以提升 prefill 吞吐量;反之,可降低该值以释放内存用于更多 KV 块,或避免内存溢出错误。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_batch_tokens=<span class="hljs-number">16384</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="batch-and-scheduling-limits" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#batch-and-scheduling-limits"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->批处理和调度限制<!----></span></h3><!--]--><!----> <p><code>max_requests_per_batch</code> 限制了单次前向传播中运行的请求数量。模型会生成一个 logits 张量,其大小由批处理中的 token 数量和词表大小决定,并且为了进行采样,该张量会被转换为 <code>fp32</code>,从而使内存占用翻倍。由于每个请求只需要一个下一个 token 的预测,因此设置较小的上限可以保持该张量更小,并避免在包含大量预填充(prefill)且词表较大的批处理中发生内存溢出错误。默认情况下,该值设置为提交的提示(prompt)数量,若未指定则回退为 1024,随后会根据 <code>max_batch_tokens</code> 和 <code>num_blocks</code> 进行上限约束以适配资源。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_requests_per_batch=<span class="hljs-number">256</span>)<!----></pre></div><!----> <p><code>safety_margin</code> 为正在处理的请求预留一部分空闲缓存容量。当空闲容量低于 <code>safety_margin</code>(即总缓存容量的一定比例)时,调度器将停止接纳新的 prefill 请求,仅继续处理已在进行中的 decode 请求。这一机制优先保证完成当前活跃的工作,而非启动新请求,从而保护 decode 延迟并推迟 <a href="./continuous_batching_architecture#offloading">offloading</a>。该值必须介于 <code>0</code> 和 <code>1</code> 之间,其中 <code>0</code> 表示禁用该预留机制。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(safety_margin=<span class="hljs-number">0.15</span>)<!----></pre></div><!----> <p>默认值取决于调度器。对于 FIFO 调度器,该值为 <code>0.15</code>;对于 <code>prefill_first</code>,该值为 <code>0.0</code>。有关 margin 如何与调度交互,请参阅 <a href="./continuous_batching_architecture#admission">Admission</a>。</p> <!--[2--><h3 class="relative group"><a id="log-probabilities" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#log-probabilities"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->对数概率<!----></span></h3><!--]--><!----> <p>当 <code>return_logprobs=True</code> 时,<a href="/docs/transformers/main/zh/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> 会返回每个生成 token 的对数概率。这在强化学习中非常有用,因为 logprobs 是某些训练循环的输入。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(return_logprobs=<span class="hljs-literal">True</span>) | |
| <span class="hljs-comment"># generate_batch()</span> | |
| <span class="hljs-keyword">for</span> request_id, output <span class="hljs-keyword">in</span> outputs.items(): | |
| <span class="hljs-keyword">for</span> token_id, log_prob <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(output.generated_tokens, output.logprobs): | |
| token = tokenizer.decode([token_id]) | |
| <span class="hljs-built_in">print</span>(<span class="hljs-string">f"<span class="hljs-subst">{token}</span> | logprob: <span class="hljs-subst">{log_prob}</span>"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="cuda-graphs" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#cuda-graphs"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->CUDA 图<!----></span></h3><!--]--><!----> <p>CUDA 图通过一次性记录 GPU 执行图,并在形状匹配的批次上重放该图,从而消除 CPU 调度开销。请显式使用 <code>use_cuda_graph=True</code> 来启用它们。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(use_cuda_graph=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>启用后,管理器会将查询和 KV 的长度填充到固定的间隔,从而使形状重复出现并实现图复用。<code>q_padding_interval_size</code> 和 <code>kv_padding_interval_size</code> 的值越小,填充造成的计算浪费越少,但这也意味着图需要记录和存储更多独特的形状,从而占用更多内存。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig( | |
| use_cuda_graph=<span class="hljs-literal">True</span>, | |
| q_padding_interval_size=<span class="hljs-number">64</span>, | |
| kv_padding_interval_size=<span class="hljs-number">16384</span>, | |
| )<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="async-batching" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#async-batching"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->异步批处理<!----></span></h3><!--]--><!----> <p>异步批处理(Async batching)将下一批次的 CPU 调度与当前批次的 GPU 计算重叠执行。它需要 CUDA graphs,并且会使输入张量占用的显存大约翻倍。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig( | |
| use_cuda_graph=<span class="hljs-literal">True</span>, | |
| use_async_batching=<span class="hljs-literal">True</span>, | |
| )<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="compilation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#compilation"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->编译<!----></span></h3><!--]--><!----> <p><code>default_compile_level</code> 会对模型的前向传播应用 <code>torch.compile</code>。编译以一次性的预热开销换取生成阶段更快的前向传播速度。更高的级别会执行更激进的优化,从而提升吞吐量,但也会延长预热时间。在测试和基准测试迭代期间,请保持较低的级别;对于长时间运行的服务负载,由于预热成本会在大量请求中分摊,因此可以提高该级别。</p> <p>级别范围从 <code>0</code> 到 <code>3</code>。级别 <code>0</code> 为默认值,将完全跳过编译。</p> <table><thead><tr><th>级别</th><th><code>mode</code></th><th><code>dynamic</code></th><th>权衡</th></tr></thead><tbody><tr><td>0</td><td>—</td><td>—</td><td>无编译(默认),启动速度最快</td></tr><tr><td>1</td><td><code>default</code></td><td><code>True</code></td><td>速度提升有限,预热时间短</td></tr><tr><td>2</td><td><code>max-autotune-no-cudagraphs</code></td><td><code>True</code></td><td>更快的加速效果,更长的预热时间</td></tr><tr><td>3</td><td><code>max-autotune-no-cudagraphs</code></td><td><code>False</code></td><td>最佳吞吐量,最长的预热时间</td></tr></tbody></table> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(default_compile_level=<span class="hljs-number">1</span>)<!----></pre></div><!----> <p>该级别为 varlen 和 decode 执行路径提供默认的 <a href="/docs/transformers/main/zh/internal/generation_utils#transformers.CompileConfig">CompileConfig</a>。它仅适用于未显式配置的路径,因此当 <code>varlen_compile_config</code> 和 <code>decode_compile_config</code> 被设置时,它们具有更高的优先级。在 FlashAttention 下,由于 <code>max_length_k</code> 会触发频繁的重新编译,varlen 路径会跳过编译,因此在这种情况下,该级别仅影响 decode 路径。</p> <!--[2--><h3 class="relative group"><a id="decode-fast-path" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#decode-fast-path"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->快速解码路径<!----></span></h3><!--]--><!----> <p>当批次中仅包含解码请求(每个序列一个查询 token)时,管理器可以调度到 <code>flash_attn_with_kvcache</code> 内核,而不是变长内核。这比 varlen 路径更快,因为该内核通过块表直接就地读写分页 KV 缓存,而无需经过手动更新。有关内核级别的详细信息,请参阅 <a href="./paged_attention">Paged attention</a>。</p> <p>快速路径的大小由 <code>max_blocks_per_request</code> 决定,该参数用于确定每个请求的块表维度。默认情况下,此值会自动推断。如果管理器上设置了 <code>max_prompt_length</code> 和 <code>max_generated_length</code>,则块表的大小将调整为可容纳最大序列长度。否则,将使用回退默认值(每个请求 32 个块)。</p> <p>将 <code>max_blocks_per_request</code> 设置为特定值,以显式指定块表的大小。当您已知每个请求的最大序列长度并希望限制块表的内存开销时,此设置非常有用。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_blocks_per_request=<span class="hljs-number">64</span>)<!----></pre></div><!----> <p>将 <code>max_blocks_per_request=0</code> 设置为 0 可禁用快速路径,强制所有批次通过 varlen 内核处理。这将恢复默认行为之前的状态,在快速路径不适用于您的注意力实现时非常有用(当底层内核无法使用时,管理器也会自动禁用它)。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_blocks_per_request=<span class="hljs-number">0</span>)<!----></pre></div><!----> <p>快速路径依赖于 <code>flash_attn_with_kvcache</code> 内核,该内核支持两种设备和注意力实现组合。</p> <table><thead><tr><th>设备</th><th><code>attn_implementation</code></th></tr></thead><tbody><tr><td>CUDA</td><td><code>flash_attention_2</code> 或 <code>flash_attention_3</code></td></tr><tr><td>XPU</td><td><a href="https://huggingface.co/kernels-community/flash-attn2" rel="nofollow">flash_attention_2</a></td></tr></tbody></table> <p>对于其他任何组合,或者当无法导入内核时,管理器会回退到 varlen 路径。仅当显式设置了 <code>max_blocks_per_request</code> 时,才会记录警告。</p> <p>Full attention 支持 kernel block tables,但 sliding attention 不支持。如果模型包含任何 sliding attention 层(包括同时使用 full attention 的混合模型),初始化会将 <code>max_blocks_per_request</code> 强制设为 <code>0</code>,您设置的任何非零值都会被覆盖。随后,管理器会记录日志,指出由于模型的部分 attention 类型不支持 kernel block tables,因此 decode 快速路径不可用。</p> <!--[2--><h3 class="relative group"><a id="cpu-offloading" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#cpu-offloading"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->CPU 卸载<!----></span></h3><!--]--><!----> <p>CPU offloading 在 GPU KV 缓存已满时,将已驱逐的 KV 缓存块复制到预先分配的固定 CPU 缓冲区中。当缓存空间可用后,管理器会将这些块复制回 GPU,并恢复该请求,而无需重新计算其提示词和已生成的 token。</p> <p>将 <code>cpu_offload_space</code> 设置为以 GiB 为单位的 CPU 交换空间。默认值 <code>0.0</code> 表示禁用 CPU 卸载。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(cpu_offload_space=<span class="hljs-number">8.0</span>)<!----></pre></div><!----> <p>默认情况下,当安装了 <code>psutil</code> 时,<code>cpu_offload_space_safety_threshold=0.8</code> 会将请求的空间限制为可用系统内存的 80%。将 <code>cpu_offload_space=None</code> 设置为根据安全阈值来确定交换池的大小。</p> <!--[2--><h3 class="relative group"><a id="tensor-parallel-timeout" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensor-parallel-timeout"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->张量并行超时<!----></span></h3><!--]--><!----> <p>在张量并行下,管理器会创建一个 CPU 通信组,用于跨 rank 协调请求的提交、取消和关闭。<code>cpu_group_timeout</code> 限制了该组上的集合通信在导致进程崩溃前可以阻塞的最长时间。如果某个 rank 停滞,该超时机制可防止其他 rank 无限期等待。</p> <p>对于发出较少集合通信操作的工作负载,请设置更长的超时时间,或传入 <code>None</code> 以禁用该功能。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(cpu_group_timeout=<span class="hljs-number">600.0</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="prefix-caching" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prefix-caching"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->前缀缓存<!----></span></h3><!--]--><!----> <p>当多个请求共享相同的前缀(例如系统提示词)时,管理器会复用它们的 KV 缓存块,而不是重新计算。<code>allow_block_sharing</code> 的默认值为 <code>True</code>。仅当所有注意力类型的分配器都支持共享时,前缀共享才会生效,因此只有纯全注意力模型会使用它。纯滑动窗口模型和混合模型即使 <code>allow_block_sharing</code> 为 <code>True</code>,也会将其关闭。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig( | |
| allow_block_sharing=<span class="hljs-literal">True</span>, <span class="hljs-comment"># default</span> | |
| )<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="choose-an-attention-backend" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#choose-an-attention-backend"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->选择注意力后端<!----></span></h2><!--]--><!----> <p>Continuous batching 支持 FlashAttention、SDPA 以及 <code>"paged|eager"</code>。加载模型时,请设置 <code>attn_implementation</code>。</p> <table><thead><tr><th>后端</th><th><code>attn_implementation</code></th><th>要求</th></tr></thead><tbody><tr><td>FlashAttention 2</td><td><code>“flash_attention_2”</code></td><td><code>flash-attn</code> 或 <code>kernels</code> 包</td></tr><tr><td>FlashAttention 3</td><td><code>“flash_attention_3”</code></td><td><code>flash-attn-3</code> 或 <code>kernels</code> 软件包</td></tr><tr><td>SDPA(PyTorch 原生)</td><td><code>“sdpa”</code></td><td>无</td></tr><tr><td>Eager</td><td><code>“paged|eager”</code></td><td>无</td></tr></tbody></table> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!---->model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen3-4B"</span>, | |
| attn_implementation=<span class="hljs-string">"flash_attention_2"</span>, | |
| device_map=<span class="hljs-string">"auto"</span>, | |
| dtype=torch.bfloat16, | |
| )<!----></pre></div><!----> <p>在启用连续批处理(Continuous batching)时,使用 FlashAttention 比使用 eager 或 SDPA 更快,这主要是因为 FlashAttention 不需要注意力掩码(attention mask)。如果模型支持 FlashAttention,那么以 <code>"eager"</code> 或 <code>"sdpa"</code> 方式加载的模型会切换到 FlashAttention 3;如果未安装 FlashAttention 3,则会切换到 FlashAttention 2,并记录一条警告。如果两个版本均未安装,则后端保持不变。</p> <p>例如,在测试时若要保留 eager 或 SDPA,请在 <a href="/docs/transformers/main/zh/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> 中设置 <code>auto_switch_to_flash=False</code>。只要后端保持为 <code>"eager"</code>,连续批处理就会运行 <code>"paged|eager"</code>。</p> <!--[1--><h2 class="relative group"><a id="tensor-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#tensor-parallelism"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->张量并行<!----></span></h2><!--]--><!----> <p>对于无法在单个 GPU 上容纳的模型,请使用张量并行将权重分片到多个设备上。通过 <code>DistributedConfig(tp_size=N)</code> 设置设备数量。连续批处理会从模型中读取张量并行大小,以按分片确定分页 KV 缓存的大小。有关支持的架构列表及分片机制,请参阅 <a href="./tensor_parallelism">张量并行</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer, DistributedConfig | |
| <span class="hljs-keyword">from</span> transformers.generation <span class="hljs-keyword">import</span> ContinuousBatchingConfig, GenerationConfig | |
| distributed_config = DistributedConfig(tp_size=<span class="hljs-number">4</span>) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen3-32B"</span>, | |
| attn_implementation=<span class="hljs-string">"flash_attention_2"</span>, | |
| distributed_config=distributed_config, | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"Qwen/Qwen3-32B"</span>) | |
| inputs = [tokenizer.encode(p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> [<span class="hljs-string">"What's up?"</span>, <span class="hljs-string">"Name a cat breed."</span>]] | |
| generation_config = GenerationConfig(max_new_tokens=<span class="hljs-number">64</span>, eos_token_id=tokenizer.eos_token_id) | |
| outputs = model.generate_batch(inputs=inputs, generation_config=generation_config)<!----></pre></div><!----> <p>使用 <code>torchrun</code> 启动脚本,并将 <code>--nproc-per-node</code> 设置为您希望进行分片的 GPU 数量。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-shell "><!---->torchrun --nproc-per-node 4 cb_tp.py<!----></pre></div><!----> <p>张量并行大小必须能整除模型的 <code>num_key_value_heads</code>(请检查模型配置)。否则,分页缓存会在启动时抛出错误,因此请选择合适的 <code>--nproc-per-node</code>。</p> <blockquote class="warning"><p>不要同时设置 <code>device_map</code> 和 <code>distributed_config</code>。这两者会相互冲突,因为 <code>device_map</code> 会将整个模块放置在特定的 GPU 上,而张量并行则会将这些相同的参数分片到所有 GPU 上。</p></blockquote> <!--[1--><h2 class="relative group"><a id="sliding-window-attention" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sliding-window-attention"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->滑动窗口注意力<!----></span></h2><!--]--><!----> <p>采用滑动窗口注意力机制的模型(Mistral、Gemma 2)支持连续批处理。滑动层使用 <code>SlidingAttentionCacheAllocator</code>,通过复用少量页面来仅保留最近 token 的固定窗口。注意力类型取自模型的 <code>layer_types</code> 或 <code>sliding_window</code>,而非 <a href="/docs/transformers/main/zh/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoConfig, AutoModelForCausalLM | |
| config = AutoConfig.from_pretrained(<span class="hljs-string">"google/gemma-2-2b"</span>) | |
| config.sliding_window = <span class="hljs-number">4096</span> | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"google/gemma-2-2b"</span>, | |
| config=config, | |
| attn_implementation=<span class="hljs-string">"sdpa"</span>, | |
| device_map=<span class="hljs-string">"auto"</span>, | |
| dtype=torch.bfloat16, | |
| )<!----></pre></div><!----> <p>当存在滑动注意力时,<a href="#prefix-caching">前缀缓存</a>和<a href="#decode-fast-path">解码快速路径</a>均会被禁用,包括同时包含完整注意力层的混合模型。</p> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->后续步骤<!----></span></h2><!--]--><!----> <ul><li><a href="https://huggingface.co/blog/continuous_batching" rel="nofollow">连续批处理博客文章</a>介绍了 KV 缓存、分块预填充以及动态调度,并附有性能基准数据。</li> <li>如需深入了解连续批处理系统的工作原理,请参阅 <a href="./continuous_batching_architecture">连续批处理架构</a> 文档。</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/continuous_batching.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_1m6yd5j = { | |
| base: "/docs/transformers/main/en", | |
| assets: "/docs/transformers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"), | |
| import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 38], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 112 kB
- Xet hash:
- 81cabdb83f78a8f70d733ed51b0bb8d64711524c2dc014f0905580659eea0e6f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.