Buckets:

hf-doc-build/doc / transformers /main /vi /continuous_batching.html
mishig's picture
download
raw
118 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Lô liên tục&quot;,&quot;local&quot;:&quot;continuous-batching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;generate_batch&quot;,&quot;local&quot;:&quot;generatebatch&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ContinuousBatchingManager&quot;,&quot;local&quot;:&quot;continuousbatchingmanager&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Tắt trình quản lý&quot;,&quot;local&quot;:&quot;shutting-down-the-manager&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Thêm các yêu cầu&quot;,&quot;local&quot;:&quot;adding-requests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tham số lấy mẫu cho từng yêu cầu&quot;,&quot;local&quot;:&quot;per-request-sampling-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Lấy kết quả&quot;,&quot;local&quot;:&quot;retrieving-results&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Streaming&quot;,&quot;local&quot;:&quot;streaming&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ContinuousBatchingConfig&quot;,&quot;local&quot;:&quot;continuousbatchingconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Kích thước trang bộ đệm KV&quot;,&quot;local&quot;:&quot;kv-cache-page-size&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Kích thước lô prefill&quot;,&quot;local&quot;:&quot;prefill-batch-size&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Giới hạn về lô và lịch trình&quot;,&quot;local&quot;:&quot;batch-and-scheduling-limits&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Xác suất log&quot;,&quot;local&quot;:&quot;log-probabilities&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Biểu đồ CUDA&quot;,&quot;local&quot;:&quot;cuda-graphs&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Đóng gói đồng bộ&quot;,&quot;local&quot;:&quot;async-batching&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Biên dịch&quot;,&quot;local&quot;:&quot;compilation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Đường dẫn giải mã nhanh&quot;,&quot;local&quot;:&quot;decode-fast-path&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tách bộ nhớ ra CPU&quot;,&quot;local&quot;:&quot;cpu-offloading&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Timeout song song tensor&quot;,&quot;local&quot;:&quot;tensor-parallel-timeout&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Bộ nhớ đệm tiền tố&quot;,&quot;local&quot;:&quot;prefix-caching&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Chọn một backend chú ý&quot;,&quot;local&quot;:&quot;choose-an-attention-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Song song tensor&quot;,&quot;local&quot;:&quot;tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Chú ý cửa sổ trượt&quot;,&quot;local&quot;:&quot;sliding-window-attention&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Các bước tiếp theo&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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="{&quot;title&quot;:&quot;Continuous batching&quot;,&quot;local&quot;:&quot;continuous-batching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;generate_batch&quot;,&quot;local&quot;:&quot;generatebatch&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ContinuousBatchingManager&quot;,&quot;local&quot;:&quot;continuousbatchingmanager&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Shutting down the manager&quot;,&quot;local&quot;:&quot;shutting-down-the-manager&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Adding requests&quot;,&quot;local&quot;:&quot;adding-requests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Per-request sampling parameters&quot;,&quot;local&quot;:&quot;per-request-sampling-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Retrieving results&quot;,&quot;local&quot;:&quot;retrieving-results&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Streaming&quot;,&quot;local&quot;:&quot;streaming&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ContinuousBatchingConfig&quot;,&quot;local&quot;:&quot;continuousbatchingconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;KV cache page size&quot;,&quot;local&quot;:&quot;kv-cache-page-size&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Prefill batch size&quot;,&quot;local&quot;:&quot;prefill-batch-size&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Batch and scheduling limits&quot;,&quot;local&quot;:&quot;batch-and-scheduling-limits&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Log probabilities&quot;,&quot;local&quot;:&quot;log-probabilities&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;CUDA graphs&quot;,&quot;local&quot;:&quot;cuda-graphs&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Async batching&quot;,&quot;local&quot;:&quot;async-batching&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Compilation&quot;,&quot;local&quot;:&quot;compilation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Decode fast path&quot;,&quot;local&quot;:&quot;decode-fast-path&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;CPU offloading&quot;,&quot;local&quot;:&quot;cpu-offloading&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tensor parallel timeout&quot;,&quot;local&quot;:&quot;tensor-parallel-timeout&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Prefix caching&quot;,&quot;local&quot;:&quot;prefix-caching&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Choose an attention backend&quot;,&quot;local&quot;:&quot;choose-an-attention-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tensor parallelism&quot;,&quot;local&quot;:&quot;tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Sliding window attention&quot;,&quot;local&quot;:&quot;sliding-window-attention&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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><!---->Lô liên tục<!----></span></h1><!--]--><!----> <p>Continuous batching tối đa hóa việc sử dụng GPU bằng cách lên lịch lại động cho batch ở mỗi bước sinh. Khi các yêu cầu hoàn tất, các yêu cầu mới sẽ tham gia ngay lập tức thay vì chờ đợi toàn bộ batch hoàn thành. GPU luôn được lấp đầy và thông lượng duy trì ở mức cao.</p> <blockquote class="tip"><p>Đối với triển khai trong môi trường sản xuất, hãy sử dụng <a href="./serve-cli/serving_optims#continuous-batching">transformers serve</a>. Công cụ này được xây dựng dựa trên <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager">ContinuousBatchingManager</a> và cung cấp một điểm cuối HTTP tương thích với OpenAI.</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>Continuous batching được hỗ trợ thông qua <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousMixin.generate_batch">generate_batch()</a>. Truyền vào một danh sách các prompt đã được token hóa và nhận lại kết quả cho tất cả chúng khi hoàn tất. <code>generate_batch</code> xử lý lịch trình bên trong và chặn cho đến khi tất cả các yêu cầu hoàn thành.</p> <p>Đối với các trường hợp sử dụng phục vụ và phát trực tiếp, hãy sử dụng <a href="#continuousbatchingmanager">ContinuousBatchingManager</a> trực tiếp để quản lý các yêu cầu.</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> Đã sao chép</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">&quot;Qwen/Qwen3-4B&quot;</span>,
attn_implementation=<span class="hljs-string">&quot;flash_attention_2&quot;</span>,
device_map=<span class="hljs-string">&quot;auto&quot;</span>,
dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-4B&quot;</span>)
prompts = [
<span class="hljs-string">&quot;What&#x27;s up?&quot;</span>,
<span class="hljs-string">&quot;Name a cat breed.&quot;</span>,
<span class="hljs-string">&quot;Write a detailed history of quantum mechanics.&quot;</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&quot;[<span class="hljs-subst">{request_id}</span>] <span class="hljs-subst">{text}</span>&quot;</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/vi/main_classes/text_generation#transformers.ContinuousBatchingManager">ContinuousBatchingManager</a> chạy một luồng nền và cho phép bạn gửi yêu cầu cũng như lấy kết quả một cách độc lập. Ở mỗi bước sinh, nó kiểm tra các yêu cầu đã hoàn thành và lên lịch cho các yêu cầu mới tham gia vào batch. Điều này hữu ích cho streaming, phục vụ thời gian thực, hoặc gửi yêu cầu khi chúng đến.</p> <p>Sử dụng <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousMixin.continuous_batching_context_manager">continuous_batching_context_manager()</a> để khởi động và dừng trình quản lý một cách an toàn. Ví dụ bên dưới chứa các đầu vào có độ dài khác nhau. Ngay khi prompt ngắn nhất hoàn tất, nó sẽ rời khỏi batch trong khi các prompt dài hơn tiếp tục tạo ra. Với static batching, bạn sẽ phải đệm tất cả chúng để có cùng độ dài. Continuous batching giải phóng prompt đã hoàn tất để bạn có thể bắt đầu xử lý prompt tiếp theo ngay lập tức.</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> Đã sao chép</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">&quot;Write a detailed history of quantum mechanics.&quot;</span>),
request_id=<span class="hljs-string">&quot;long&quot;</span>,
max_new_tokens=<span class="hljs-number">512</span>,
)
manager.add_request(
input_ids=tokenizer.encode(<span class="hljs-string">&quot;What&#x27;s up?&quot;</span>),
request_id=<span class="hljs-string">&quot;short_0&quot;</span>,
max_new_tokens=<span class="hljs-number">32</span>,
)
manager.add_request(
input_ids=tokenizer.encode(<span class="hljs-string">&quot;Name a cat breed.&quot;</span>),
request_id=<span class="hljs-string">&quot;short_1&quot;</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&quot;[<span class="hljs-subst">{result.request_id}</span>] <span class="hljs-subst">{text}</span>&quot;</span>)<!----></pre></div><!----> <p>Bạn cũng có thể gọi <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousMixin.init_continuous_batching">init_continuous_batching()</a> để tự quản lý vòng đời.</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> Đã sao chép</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><!---->Tắt trình quản lý<!----></span></h3><!--]--><!----> <p>Quản lý chạy một luồng nền và giữ các tài nguyên phân tán. Việc tắt diễn ra theo hai giai đoạn để bạn có thể chọn cách xử lý công việc đang chạy.</p> <p>Gọi <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.stop">stop()</a> để dừng luồng nền. Mặc định, trình quản lý sẽ ngừng nhận các yêu cầu mới và chờ cho đến khi các yêu cầu đang xếp hàng và đang hoạt động hoàn tất trước khi luồng thoát.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.stop()<!----></pre></div><!----> <p>Truyền <code>hard_stop=True</code> để hủy bỏ công việc đang chờ ngay lập tức. Các yêu cầu trong hàng đợi và đang hoạt động sẽ bị lỗi với <code>RuntimeError</code> thay vì hoàn tất.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.stop(hard_stop=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>Khi <code>stop</code> được gọi, <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.add_request">add_request()</a> và <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.add_requests">add_requests()</a> sẽ từ chối các yêu cầu mới và ghi lại cảnh báo. Bạn vẫn có thể gọi <code>start</code> một lần nữa để chạy phiên sinh mới với cùng một trình quản lý.</p> <p>Gọi <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.destroy">destroy()</a> để giải phóng các tài nguyên phân tán. <code>destroy</code> sẽ dừng manager trước nếu nó vẫn đang chạy, và manager không thể được khởi động lại sau đó. Hãy sử dụng nó khi bạn đã hoàn tất việc sử dụng continuous batching trong suốt vòng đời của tiến trình.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.destroy()<!----></pre></div><!----> <p><a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousMixin.continuous_batching_context_manager">continuous_batching_context_manager()</a> xử lý quy trình này. Nó gọi <code>stop</code> khi thoát và <code>destroy</code> trừ khi bạn truyền <code>persistent_manager=True</code> để lưu bộ nhớ đệm trình quản lý trên mô hình cho phiên tiếp theo.</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><!---->Thêm các yêu cầu<!----></span></h3><!--]--><!----> <p><a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.add_request">add_request()</a> gửi một yêu cầu đơn lẻ. Hãy cung cấp <code>request_id</code> hoặc để trình quản lý tự động tạo một cái.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.add_request(input_ids=input_ids, request_id=<span class="hljs-string">&quot;my_request&quot;</span>)<!----></pre></div><!----> <p><a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.add_requests">add_requests()</a> gửi một lô yêu cầu cùng lúc. Hàm này tự động sắp xếp các đầu vào để tối đa hóa số lần truy cập bộ nhớ đệm tiền tố khi chia sẻ khối được bật.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.add_requests(inputs=inputs)<!----></pre></div><!----> <p>Hủy một yêu cầu bằng <a href="/docs/transformers/main/vi/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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->manager.cancel_request(request_id=<span class="hljs-string">&quot;my_request&quot;</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><!---->Tham số lấy mẫu cho từng yêu cầu<!----></span></h3><!--]--><!----> <p>Bật <code>per_request_processors</code> để áp dụng <code>temperature</code>, <code>top_k</code> và <code>top_p</code> độc lập cho từng yêu cầu trong cùng một bước forward, cho phép sử dụng các tham số lấy mẫu khác nhau cho các yêu cầu khác nhau (ví dụ: đầu ra sáng tạo, nhiệt độ cao so với đầu ra chính xác, nhiệt độ thấ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> Đã sao chép</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>Mỗi tham số trong <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> phải là một giá trị không mặc định để tạo ra logits processor tương ứng khi chạy. Ví dụ, đặt <code>temperature</code> thành một giá trị khác <code>None</code> hoặc <code>1</code> để hỗ trợ kiểm soát nhiệt độ theo từng yêu cầu. Các yêu cầu có nhiệt độ là <code>1</code> vẫn có thể được tạo ra sau đó.</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><!---->Lấy kết quả<!----></span></h3><!--]--><!----> <p>Lặp qua trình quản lý để nhận kết quả khi chúng đến.</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> Đã sao chép</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/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.get_result">get_result()</a> lấy kết quả tiếp theo từ hàng đợi đầu ra. Truyền <code>request_id</code> để lọc cho một yêu cầu cụ thể. Nếu kết quả tiếp theo trong hàng đợi không khớp, nó sẽ được đưa lại vào hàng đợi và phương thức trả về <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> Đã sao chép</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">&quot;my_request&quot;</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><!---->Streaming<!----></span></h3><!--]--><!----> <p>Đặt <code>streaming=True</code> cho một yêu cầu, sau đó sử dụng <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousBatchingManager.request_id_iter">request_id_iter()</a> để lặp qua các đầu ra một phần khi các token được tạo ra.</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> Đã sao chép</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">&quot;streamed&quot;</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">&quot;streamed&quot;</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">&quot;&quot;</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/vi/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> kiểm soát bộ đệm KV, lịch trình, đồ thị CUDA, mức sử dụng bộ nhớ và nhiều khía cạnh khác. Truyền nó vào <code>generate_batch</code> hoặc <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.ContinuousMixin.init_continuous_batching">init_continuous_batching()</a> bằng đối số <code>continuous_batching_config</code>, tách biệt với <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a>. Hai cấu hình này mô tả những thứ khác nhau. <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> lấy mô hình làm trung tâm và chứa các tham số lấy mẫu và dừng, trong khi <a href="/docs/transformers/main/vi/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> lấy phần cứng làm trung tâm và chứa các tham số bộ nhớ và lịch trình.</p> <blockquote class="warning"><p>Việc đặt <code>continuous_batching_config</code> trên một <a href="/docs/transformers/main/vi/main_classes/text_generation#transformers.GenerationConfig">GenerationConfig</a> đã bị ngừng hỗ trợ, sẽ phát ra <code>FutureWarning</code> và sẽ bị loại bỏ trong v5.19.</p></blockquote> <p>Mặc định, <code>max_batch_tokens</code> là <code>8192</code>, bị giới hạn bởi bộ nhớ GPU khả dụng và không bao giờ thấp hơn <code>256</code>, trong khi <code>num_blocks</code> lấp đầy phần bộ nhớ còn lại. Hãy sử dụng bảng bên dưới để giúp bạn chọn các tính năng phù hợp.</p> <table><thead><tr><th>Tính năng</th><th>Bộ nhớ</th><th>Thông lượng</th><th>Độ trễ</th></tr></thead><tbody><tr><td><code>max_memory_percent</code> / <code>page_size</code></td><td>✓ kiểm soát ngân sách KV</td><td></td><td></td></tr><tr><td><code>max_batch_tokens</code></td><td>↑ bộ đệm đầu vào lớn hơn</td><td>✓ các lô prefill lớn hơn</td><td>✓ TTFT khi bị giới hạn bởi prefill</td></tr><tr><td><code>scheduler</code></td><td></td><td>✓ chính sách lập lịch</td><td>✓ TTFT</td></tr><tr><td>Biểu đồ CUDA</td><td>↑ lưu trữ đồ thị</td><td>✓ ít chi phí điều phối hơn</td><td>✓</td></tr><tr><td>Đóng gói đồng bộ</td><td>↑ ~2× bộ đệm I/O</td><td>✓ chồng chéo CPU/GPU</td><td></td></tr><tr><td>Biên dịch</td><td>↑ chỉ warmup-time</td><td>✓ các bước truyền tiến nhanh hơn</td><td>✓</td></tr><tr><td>Đường dẫn giải mã nhanh</td><td>↑ bảng khối cho mỗi yêu cầu</td><td>✓ các bước chỉ giải mã nhanh hơn</td><td>✓</td></tr><tr><td>Tách bộ nhớ ra CPU</td><td>↑ bộ nhớ CPU cố định</td><td>✓ bỏ qua một số lần nạp lại tiền tố</td><td></td></tr><tr><td>Bộ nhớ đệm tiền tố</td><td>↓ các khối KV dùng chung</td><td>✓ bỏ qua các bước prefill trùng lặp</td><td>✓ TTFT</td></tr><tr><td>Chú ý phân trang</td><td>↓ không có phân mảnh</td><td>✓ thành viên lô động</td><td></td></tr><tr><td>Cửa sổ trượt</td><td>↓ KV có giới hạn cho mỗi lớp</td><td></td><td></td></tr><tr><td>Bộ xử lý theo từng yêu cầu</td><td></td><td>✓ các tham số lấy mẫu hỗn hợp cho mỗi lô</td><td></td></tr><tr><td><code>max_requests_per_batch</code></td><td>↓ giới hạn bộ đệm logits</td><td>✓ giới hạn kích thước lô</td><td></td></tr><tr><td><code>safety_margin</code></td><td></td><td></td><td>✓ bảo vệ độ trễ giải mã</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> Đã sao chép</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">&quot;fifo&quot;</span>, <span class="hljs-comment"># &quot;fifo&quot; or &quot;prefill_first&quot;</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><!---->Kích thước trang bộ đệm KV<!----></span></h3><!--]--><!----> <p><code>page_size</code> xác định số lượng token KV mà một trang attention đầy đủ chứa cho một lớp. Giá trị này phải ít nhất là <code>4</code>, nếu không quá trình khởi tạo sẽ gây ra <code>ValueError</code>. Giá trị mặc định <code>256</code> hoạt động tốt cho hầu hết các tác vụ.</p> <blockquote class="note"><p>Tham số <code>block_size</code> đã bị ngừng hỗ trợ và được thay thế bằng <code>page_size</code>.</p></blockquote> <p>Kích thước trang lớn hơn giúp giảm chi phí quản lý nhưng lại lãng phí không gian khi một chuỗi kết thúc giữa trang. Trang nhỏ hơn lãng phí ít không gian hơn nhưng đòi hỏi nhiều công việc quản lý hơn. Thông thường bạn không cần phải tinh chỉnh <code>page_size</code>. Nếu khối lượng công việc của bạn có nhiều yêu cầu ngắn, hãy thử một giá trị nhỏ hơn. Nếu có một vài yêu cầu rất dài, hãy thử một giá trị lớn hơn. Xem <a href="./continuous_batching_architecture#memory-management">Quản lý bộ nhớ</a> để biết cách các trang, khối và đoạn kết hợp với nhau.</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> Đã sao chép</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><!---->Kích thước lô prefill<!----></span></h3><!--]--><!----> <p><code>max_batch_tokens</code> đặt ngân sách token cho một lần forward pass, tức là số lượng token query tối đa mà mô hình xử lý cùng lúc. Ngân sách lớn hơn sẽ nhồi nhiều token prompt hơn vào mỗi lần prefill, từ đó tăng thông lượng prefill và giảm thời gian đến token đầu tiên trên các tác vụ nặng về prompt. Bộ lập lịch sẽ chia các prompt vượt quá ngân sách thành nhiều bước. Xem <a href="./continuous_batching_architecture#chunked-prefill">chunked prefill</a> để biết cách xử lý các prompt quá lớn.</p> <p>Mặc định, <code>max_batch_tokens</code> là <code>8192</code>. Giá trị này bị giới hạn bởi bộ nhớ GPU khả dụng để các tensor đầu vào theo lô không chiếm hết bộ nhớ KV cache, và không bao giờ thấp hơn <code>256</code>. Trên GPU có ít bộ nhớ trống, giới hạn này sẽ hạ giá trị xuống dưới <code>8192</code>.</p> <p><code>max_batch_tokens</code> và <code>num_blocks</code> cùng lấy từ một ngân sách bộ nhớ, do đó chúng đánh đổi lẫn nhau. Ngân sách token lớn hơn sẽ phân bổ các bộ đệm đầu vào lớn hơn và để lại ít khối KV cache hơn cho các yêu cầu đồng thời hoặc dài. Tăng <code>max_batch_tokens</code> để đẩy cao thông lượng prefill khi bạn có thêm bộ nhớ khả dụng, và giảm nó để giải phóng bộ nhớ cho nhiều khối KV hơn hoặc để tránh lỗi hết bộ nhớ.</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> Đã sao chép</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><!---->Giới hạn về lô và lịch trình<!----></span></h3><!--]--><!----> <p><code>max_requests_per_batch</code> giới hạn số lượng yêu cầu được thực thi trong một lần forward pass duy nhất. Mô hình tạo ra một tensor logits có kích thước bằng số token trong batch nhân với kích thước từ vựng, và tensor này được chuyển sang <code>fp32</code> để lấy mẫu, khiến bộ nhớ sử dụng tăng gấp đôi. Mỗi yêu cầu chỉ cần một dự đoán token tiếp theo, do đó việc đặt giới hạn nhỏ hơn sẽ giữ tensor này ở kích thước nhỏ hơn và tránh lỗi hết bộ nhớ trên các batch nặng prefill với từ vựng lớn. Mặc định, giá trị này được đặt bằng số prompt được gửi, với giá trị dự phòng là 1024, sau đó được giới hạn để phù hợp với <code>max_batch_tokens</code> và <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> Đã sao chép</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> dành ra một phần dung lượng cache trống cho các yêu cầu đang hoạt động. Khi dung lượng trống giảm xuống dưới <code>safety_margin</code> (một phần của tổng kích thước cache), trình điều độ sẽ ngừng chấp nhận các prefill mới và chỉ tiếp tục giải mã các yêu cầu đang trong quá trình xử lý. Điều này ưu tiên hoàn thành công việc đang hoạt động hơn là bắt đầu các yêu cầu mới, giúp bảo vệ độ trễ giải mã và trì hoãn <a href="./continuous_batching_architecture#offloading">offloading</a>. Giá trị phải nằm trong khoảng từ <code>0</code> đến <code>1</code>, trong đó <code>0</code> vô hiệu hóa biên an toàn.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(safety_margin=<span class="hljs-number">0.15</span>)<!----></pre></div><!----> <p>Giá trị mặc định phụ thuộc vào trình lập lịch. Với trình lập lịch FIFO, giá trị là <code>0.15</code> và với <code>prefill_first</code>, giá trị là <code>0.0</code>. Xem <a href="./continuous_batching_architecture#admission">Admission</a> để biết cách biên độ tương tác với việc lập lịch.</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><!---->Xác suất log<!----></span></h3><!--]--><!----> <p><a href="/docs/transformers/main/vi/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a> trả về log probability của từng token được sinh ra khi <code>return_logprobs=True</code>. Điều này hữu ích cho RL, nơi logprobs là đầu vào của một số vòng lặp huấn luyện.</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> Đã sao chép</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&quot;<span class="hljs-subst">{token}</span> | logprob: <span class="hljs-subst">{log_prob}</span>&quot;</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><!---->Biểu đồ CUDA<!----></span></h3><!--]--><!----> <p>CUDA graphs loại bỏ chi phí điều phối CPU bằng cách ghi lại đồ thị thực thi GPU một lần và phát lại nó cho các lô có hình dạng khớp. Bật chúng một cách rõ ràng bằng <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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(use_cuda_graph=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>Khi đang hoạt động, trình quản lý sẽ đệm (pad) độ dài query và KV lên các khoảng cố định để các shape lặp lại và đồ thị được tái sử dụng. Các giá trị nhỏ hơn của <code>q_padding_interval_size</code> và <code>kv_padding_interval_size</code> sẽ giảm thiểu tính toán lãng phí do đệm, nhưng điều này đồng nghĩa với việc có nhiều shape duy nhất hơn mà đồ thị phải ghi nhận và lưu trữ, dẫn đến tốn nhiều bộ nhớ hơn.</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> Đã sao chép</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><!---->Đóng gói đồng bộ<!----></span></h3><!--]--><!----> <p>Async batching cho phép xếp chồng việc lên lịch CPU cho batch tiếp theo với việc tính toán GPU cho batch hiện tại. Tính năng này yêu cầu CUDA graphs và làm tăng gấp đôi lượng VRAM sử dụng cho các tensor đầu vào.</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> Đã sao chép</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><!---->Biên dịch<!----></span></h3><!--]--><!----> <p><code>default_compile_level</code> áp dụng <code>torch.compile</code> cho các bước forward của model. Biên dịch đánh đổi một chi phí khởi động ban đầu để đổi lấy các bước forward nhanh hơn trong quá trình sinh. Các mức cao hơn thực hiện tối ưu hóa mạnh mẽ hơn, giúp cải thiện thông lượng nhưng kéo dài thời gian khởi động. Giữ mức thấp cho các bài kiểm tra và lặp lại benchmark, và tăng mức lên cho các tác vụ phục vụ chạy dài, nơi chi phí khởi động được bù đắp qua nhiều yêu cầu.</p> <p>Mức độ dao động từ <code>0</code> đến <code>3</code>. Mức <code>0</code> là mặc định và bỏ qua hoàn toàn việc biên dịch.</p> <table><thead><tr><th>Mức</th><th><code>mode</code></th><th><code>dynamic</code></th><th>Sự đánh đổi</th></tr></thead><tbody><tr><td>0</td><td>—</td><td>—</td><td>Không biên dịch (mặc định), khởi động nhanh nhất</td></tr><tr><td>1</td><td><code>default</code></td><td><code>True</code></td><td>Tăng tốc độ khiêm tốn, khởi động ngắn</td></tr><tr><td>2</td><td><code>max-autotune-no-cudagraphs</code></td><td><code>True</code></td><td>Tăng tốc độ hơn, thời gian khởi động dài hơn</td></tr><tr><td>3</td><td><code>max-autotune-no-cudagraphs</code></td><td><code>False</code></td><td>Thông lượng tốt nhất, thời gian khởi động dài nhất</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(default_compile_level=<span class="hljs-number">1</span>)<!----></pre></div><!----> <p>Mức này cung cấp <a href="/docs/transformers/main/vi/internal/generation_utils#transformers.CompileConfig">CompileConfig</a> mặc định cho các đường dẫn thực thi varlen và decode. Nó chỉ áp dụng cho đường dẫn không có cấu hình rõ ràng, do đó <code>varlen_compile_config</code> và <code>decode_compile_config</code> có độ ưu tiên cao hơn khi được đặt. Dưới FlashAttention, đường dẫn varlen bỏ qua việc biên dịch vì <code>max_length_k</code> gây ra việc biên dịch lại thường xuyên, nên trong trường hợp đó, mức này chỉ ảnh hưởng đến đường dẫn 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><!---->Đường dẫn giải mã nhanh<!----></span></h3><!--]--><!----> <p>Khi một batch chỉ chứa các yêu cầu decode (một token truy vấn cho mỗi chuỗi), trình quản lý có thể điều phối đến kernel <code>flash_attn_with_kvcache</code> thay vì kernel biến độ dài. Cách này nhanh hơn đường dẫn varlen vì kernel đọc và ghi bộ nhớ đệm KV phân trang tại chỗ thông qua bảng khối, thay vì phải thực hiện cập nhật thủ công. Xem <a href="./paged_attention">Paged attention</a> để biết chi tiết ở cấp độ kernel.</p> <p>Fast path được xác định kích thước bởi <code>max_blocks_per_request</code>, tham số này quyết định kích thước bảng khối (block table) cho mỗi yêu cầu. Mặc định, giá trị này được suy luận tự động. Nếu <code>max_prompt_length</code> và <code>max_generated_length</code> được đặt trên manager, bảng khối sẽ được xác định kích thước để chứa độ dài chuỗi tối đa. Ngược lại, một giá trị mặc định dự phòng (32 khối mỗi yêu cầu) sẽ được sử dụng.</p> <p>Đặt <code>max_blocks_per_request</code> thành một giá trị cụ thể để xác định kích thước bảng khối một cách rõ ràng. Điều này hữu ích khi bạn biết độ dài tối đa của chuỗi cho mỗi yêu cầu và muốn giới hạn chi phí bộ nhớ của bảng khối.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_blocks_per_request=<span class="hljs-number">64</span>)<!----></pre></div><!----> <p>Đặt <code>max_blocks_per_request=0</code> để tắt fast path và buộc mọi batch đi qua varlen kernel. Thao tác này khôi phục hành vi mặc định trước đó và hữu ích khi fast path không khả dụng cho implementation attention của bạn (manager cũng tự động tắt nó khi kernel nền tảng không thể được sử dụng).</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(max_blocks_per_request=<span class="hljs-number">0</span>)<!----></pre></div><!----> <p>Đường dẫn nhanh dựa trên kernel <code>flash_attn_with_kvcache</code>, có sẵn cho hai tổ hợp thiết bị và triển khai attention.</p> <table><thead><tr><th>Thiết bị</th><th><code>attn_implementation</code></th></tr></thead><tbody><tr><td>CUDA</td><td><code>flash_attention_2</code> hoặc <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>Với bất kỳ tổ hợp nào khác, hoặc khi không thể import kernel, manager sẽ quay lại đường dẫn varlen. Nó chỉ ghi log cảnh báo khi bạn đặt <code>max_blocks_per_request</code> một cách tường minh.</p> <p>Full attention hỗ trợ bảng khối kernel, nhưng sliding attention thì không. Nếu mô hình có bất kỳ lớp sliding attention nào, bao gồm cả các mô hình hybrid cũng sử dụng full attention, quá trình khởi tạo sẽ ép <code>max_blocks_per_request</code> thành <code>0</code>, và giá trị khác 0 mà bạn đặt sẽ bị ghi đè. Sau đó, trình quản lý sẽ ghi log rằng đường nhanh decode không khả dụng vì một số loại attention của mô hình không hỗ trợ bảng khối kernel.</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><!---->Tách bộ nhớ ra CPU<!----></span></h3><!--]--><!----> <p>CPU offloading sao chép các khối KV cache bị loại bỏ vào bộ đệm CPU đã được cố định và phân bổ trước khi bộ nhớ KV cache trên GPU đầy. Sau khi có không gian cache khả dụng, trình quản lý sẽ sao chép các khối này trở lại GPU và tiếp tục yêu cầu mà không cần tính toán lại prompt và các token đã tạo.</p> <p>Đặt <code>cpu_offload_space</code> thành dung lượng bộ nhớ đệm CPU tính bằng GiB. Giá trị mặc định, <code>0.0</code>, sẽ tắt tính năng chuyển bộ nhớ ra 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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->cb_config = ContinuousBatchingConfig(cpu_offload_space=<span class="hljs-number">8.0</span>)<!----></pre></div><!----> <p>Mặc định, <code>cpu_offload_space_safety_threshold=0.8</code> giới hạn dung lượng được yêu cầu ở mức 80% RAM hệ thống khả dụng khi <code>psutil</code> được cài đặt. Đặt <code>cpu_offload_space=None</code> để xác định kích thước pool swap từ ngưỡng an toàn.</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><!---->Timeout song song tensor<!----></span></h3><!--]--><!----> <p>Dưới chế độ song song tensor, trình quản lý tạo một nhóm giao tiếp CPU để điều phối việc gửi yêu cầu, hủy yêu cầu và tắt hệ thống giữa các rank. <code>cpu_group_timeout</code> giới hạn thời gian tối đa mà một thao tác tập thể trên nhóm này có thể bị chặn trước khi tiến trình bị sập. Nếu một rank bị treo, thời gian chờ quá hạn sẽ ngăn các rank khác chờ đợi vô hạn.</p> <p>Đặt timeout dài hơn cho các tác vụ phát sinh các thao tác tập thể không thường xuyên, hoặc truyền <code>None</code> để tắt nó.</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> Đã sao chép</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><!---->Bộ nhớ đệm tiền tố<!----></span></h3><!--]--><!----> <p>Khi nhiều yêu cầu chia sẻ một tiền tố chung, chẳng hạn như một prompt hệ thống, trình quản lý sẽ tái sử dụng các khối KV cache của chúng thay vì tính toán lại. <code>allow_block_sharing</code> mặc định là <code>True</code>. Chia sẻ tiền tố chỉ được kích hoạt khi mọi bộ cấp phát kiểu chú ý đều hỗ trợ chia sẻ, do đó chỉ các mô hình chỉ dùng chú ý đầy đủ mới sử dụng tính năng này. Các mô hình trượt thuần túy và mô hình lai sẽ tắt tính năng này ngay cả khi <code>allow_block_sharing</code> là <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> Đã sao chép</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><!---->Chọn một backend chú ý<!----></span></h2><!--]--><!----> <p>Continuous batching hỗ trợ FlashAttention, SDPA và <code>"paged|eager"</code>. Đặt <code>attn_implementation</code> khi bạn tải model.</p> <table><thead><tr><th>Backend</th><th><code>attn_implementation</code></th><th>Yêu cầu</th></tr></thead><tbody><tr><td>FlashAttention 2</td><td><code>“flash_attention_2”</code></td><td>gói <code>flash-attn</code> hoặc <code>kernels</code></td></tr><tr><td>FlashAttention 3</td><td><code>“flash_attention_3”</code></td><td>gói <code>flash-attn-3</code> hoặc <code>kernels</code></td></tr><tr><td>SDPA (gốc PyTorch)</td><td><code>“sdpa”</code></td><td>Không</td></tr><tr><td>Eager</td><td><code>“paged|eager”</code></td><td>Không</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen3-4B&quot;</span>,
attn_implementation=<span class="hljs-string">&quot;flash_attention_2&quot;</span>,
device_map=<span class="hljs-string">&quot;auto&quot;</span>,
dtype=torch.bfloat16,
)<!----></pre></div><!----> <p>Continuous batching chạy nhanh hơn với FlashAttention so với eager hoặc SDPA, chủ yếu vì FlashAttention không cần attention mask. Nếu model hỗ trợ FlashAttention, một model được nạp với <code>"eager"</code> hoặc <code>"sdpa"</code> sẽ chuyển sang FlashAttention 3, hoặc sang FlashAttention 2 khi FlashAttention 3 chưa được cài đặt, và ghi lại một cảnh báo. Nếu cả hai phiên bản đều chưa được cài đặt, backend sẽ không thay đổi.</p> <p>Để giữ nguyên eager hoặc SDPA, chẳng hạn khi kiểm thử, hãy đặt <code>auto_switch_to_flash=False</code> trong <a href="/docs/transformers/main/vi/main_classes/continuous_batching#transformers.ContinuousBatchingConfig">ContinuousBatchingConfig</a>. Mỗi khi backend vẫn là <code>"eager"</code>, continuous batching sẽ chạy <code>"paged|eager"</code> thay thế.</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><!---->Song song tensor<!----></span></h2><!--]--><!----> <p>Đối với các mô hình quá lớn để chạy trên một GPU duy nhất, hãy chia nhỏ trọng số giữa các thiết bị bằng tensor parallelism. Đặt số lượng thiết bị bằng <code>DistributedConfig(tp_size=N)</code>. Continuous batching đọc kích thước tensor parallel từ mô hình để xác định kích thước paged KV cache cho từng shard. Xem <a href="./tensor_parallelism">Tensor parallelism</a> để biết danh sách các kiến trúc được hỗ trợ và cách thức chia nhỏ hoạt động.</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> Đã sao chép</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">&quot;Qwen/Qwen3-32B&quot;</span>,
attn_implementation=<span class="hljs-string">&quot;flash_attention_2&quot;</span>,
distributed_config=distributed_config,
)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-32B&quot;</span>)
inputs = [tokenizer.encode(p) <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> [<span class="hljs-string">&quot;What&#x27;s up?&quot;</span>, <span class="hljs-string">&quot;Name a cat breed.&quot;</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>Chạy script bằng <code>torchrun</code>, đặt <code>--nproc-per-node</code> bằng số lượng GPU mà bạn muốn chia nhỏ (shard) dữ liệu.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-shell "><!---->torchrun --nproc-per-node 4 cb_tp.py<!----></pre></div><!----> <p>Kích thước song song tensor phải chia hết cho <code>num_key_value_heads</code> của mô hình (kiểm tra cấu hình mô hình). Nếu không, bộ đệm trang sẽ gây ra lỗi khi khởi động, vì vậy hãy chọn <code>--nproc-per-node</code> phù hợp.</p> <blockquote class="warning"><p>Không đặt <code>device_map</code> cùng với <code>distributed_config</code>. Hai tham số này xung đột vì <code>device_map</code> đặt các module hoàn chỉnh lên các GPU cụ thể, trong khi tensor parallelism chia nhỏ chính các tham số đó trên tất cả các 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><!---->Chú ý cửa sổ trượt<!----></span></h2><!--]--><!----> <p>Các mô hình có chú ý cửa sổ trượt (Mistral, Gemma 2) hoạt động với continuous batching. Các lớp trượt sử dụng <code>SlidingAttentionCacheAllocator</code>, chỉ giữ một cửa sổ cố định các token gần nhất bằng cách tái sử dụng một tập hợp nhỏ các trang. Loại chú ý được lấy từ <code>layer_types</code> hoặc <code>sliding_window</code> của mô hình, không phải từ <a href="/docs/transformers/main/vi/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> Đã sao chép</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">&quot;google/gemma-2-2b&quot;</span>)
config.sliding_window = <span class="hljs-number">4096</span>
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;google/gemma-2-2b&quot;</span>,
config=config,
attn_implementation=<span class="hljs-string">&quot;sdpa&quot;</span>,
device_map=<span class="hljs-string">&quot;auto&quot;</span>,
dtype=torch.bfloat16,
)<!----></pre></div><!----> <p><a href="#prefix-caching">Prefix caching</a> và <a href="#decode-fast-path">decode fast path</a> sẽ bị vô hiệu hóa khi có sliding attention, bao gồm cả các mô hình hybrid có thêm các lớp full attention.</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><!---->Các bước tiếp theo<!----></span></h2><!--]--><!----> <ul><li>Bài đăng trên blog <a href="https://huggingface.co/blog/continuous_batching" rel="nofollow">Continuous batching</a> đề cập đến KV caching, chunked prefill và dynamic scheduling kèm theo các con số benchmark hiệu năng.</li> <li>Để tìm hiểu sâu hơn về cách hệ thống liên tục hoạt động, hãy xem tài liệu <a href="./continuous_batching_architecture">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:
118 kB
·
Xet hash:
57cc3a0bf3837bbc0d70be06e4cf2d4b38d97eaecea5c860a3177633e35fb9e3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.