Buckets:

hf-doc-build/doc / transformers /main /vi /conversations.html
mishig's picture
download
raw
29.6 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Cơ bản về Chat&quot;,&quot;local&quot;:&quot;chat-basics&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;chat CLI&quot;,&quot;local&quot;:&quot;chat-cli&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TextGenerationPipeline&quot;,&quot;local&quot;:&quot;textgenerationpipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Hiệu suất và mức sử dụng bộ nhớ&quot;,&quot;local&quot;:&quot;performance-and-memory-usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.CDsgEKDZ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CjAZ_-zP.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DYxD-VOr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.DxuVi3jG.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/rBgV2CO2.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bqqc259Y.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.D3Lf5I--.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CpYHmYpM.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/6.C_TOdj3A.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DNJDeoze.js" rel="modulepreload">
<!--15426f3--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Chat basics&quot;,&quot;local&quot;:&quot;chat-basics&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;chat CLI&quot;,&quot;local&quot;:&quot;chat-cli&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TextGenerationPipeline&quot;,&quot;local&quot;:&quot;textgenerationpipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Performance and memory usage&quot;,&quot;local&quot;:&quot;performance-and-memory-usage&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="chat-basics" 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="#chat-basics"><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ơ bản về Chat<!----></span></h1><!--]--><!----> <p>Mô hình chat là các mô hình hội thoại mà bạn có thể gửi tin nhắn và nhận phản hồi. Hầu hết các mô hình ngôn ngữ từ giữa năm 2023 trở đi đều là mô hình chat và có thể được gọi là mô hình “instruct” hoặc “instruction-tuned”. Các mô hình không hỗ trợ chat thường được gọi là mô hình “base” hoặc “pretrained”.</p> <p>Các mô hình lớn hơn và mới hơn thường có khả năng mạnh mẽ hơn, nhưng các mô hình chuyên biệt cho một số lĩnh vực nhất định (y khoa, văn bản pháp lý, ngôn ngữ không phải tiếng Anh, v.v.) thường có thể vượt trội hơn so với các mô hình lớn hơn này. Hãy thử các bảng xếp hạng như <a href="https://hf.co/spaces/HuggingFaceH4/open_llm_leaderboard" rel="nofollow">OpenLLM</a> và <a href="https://lmarena.ai/leaderboard" rel="nofollow">LMArena</a> để giúp bạn xác định mô hình tốt nhất cho trường hợp sử dụng của mình.</p> <p>Hướng dẫn này cho bạn cách nhanh chóng nạp các mô hình chat trong Transformers từ dòng lệnh, cách xây dựng và định dạng một cuộc hội thoại, và cách trò chuyện bằng <a href="/docs/transformers/main/vi/main_classes/pipelines#transformers.TextGenerationPipeline">TextGenerationPipeline</a>.</p> <!--[1--><h2 class="relative group"><a id="chat-cli" 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="#chat-cli"><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><!---->chat CLI<!----></span></h2><!--]--><!----> <p>Sau khi bạn đã <a href="./installation">cài đặt Transformers</a>, bạn có thể trò chuyện trực tiếp với một model ngay từ dòng lệnh. Lệnh bên dưới khởi động một phiên tương tác với model, kèm theo một số lệnh cơ bản được liệt kê ở đầu phiên.</p> <blockquote><p>Đối với các lệnh sau, hãy đảm bảo rằng <a href="./serve-cli/serving"><code>transformers serve</code> đang chạy</a>.</p></blockquote> <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-bash "><!---->transformers chat Qwen/Qwen2.5-0.5B-Instruct<!----></pre></div><!----> <div class="flex justify-center"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/transformers-chat-cli.png"/></div> <p>Bạn có thể khởi chạy CLI với các cờ <code>generate</code> tùy ý, theo định dạng <code>arg_1=value_1 arg_2=value_2 ...</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-bash "><!---->transformers chat Qwen/Qwen2.5-0.5B-Instruct do_sample=False max_new_tokens=10<!----></pre></div><!----> <p>Để xem danh sách đầy đủ các tùy chọn, hãy chạy lệnh bên dướ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-bash "><!---->transformers chat -h<!----></pre></div><!----> <p>Trò chuyện được triển khai trên nền <a href="./model_doc/auto">AutoClass</a>, sử dụng công cụ từ <a href="./llm_tutorial">text generation</a> và <a href="./chat_templating">chat</a>. Nó sử dụng CLI <code>transformers serve</code> bên trong (<a href="./serve-cli/serving">tài liệu</a>).</p> <!--[1--><h2 class="relative group"><a id="textgenerationpipeline" 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="#textgenerationpipeline"><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><!---->TextGenerationPipeline<!----></span></h2><!--]--><!----> <p><a href="/docs/transformers/main/vi/main_classes/pipelines#transformers.TextGenerationPipeline">TextGenerationPipeline</a> là một lớp sinh văn bản cấp cao có “chế độ chat”. Chế độ chat được kích hoạt khi phát hiện mô hình hội thoại và prompt chat được <a href="./llm_tutorial#prompt-format">định dạng đúng</a>.</p> <p>Các mô hình chat nhận vào một danh sách các tin nhắn (lịch sử chat) làm đầu vào. Mỗi tin nhắn là một từ điển có các khóa <code>role</code> và <code>content</code>.
Để bắt đầu cuộc trò chuyện, hãy thêm một tin nhắn <code>user</code> duy nhất. Bạn cũng có thể tùy chọn bao gồm một tin nhắn <code>system</code> để đưa ra các chỉ dẫn cho mô hình về cách ứng xử.</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 "><!---->chat = [
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;You are a helpful science assistant.&quot;</span>},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Hey, can you explain gravity to me?&quot;</span>}
]<!----></pre></div><!----> <p>Tạo <a href="/docs/transformers/main/vi/main_classes/pipelines#transformers.TextGenerationPipeline">TextGenerationPipeline</a> và truyền <code>chat</code> vào nó. Đối với các mô hình lớn, việc đặt <a href="./models#big-model-inference">device_map=“auto”</a> giúp tải mô hình nhanh hơn và tự động đặt nó trên thiết bị nhanh nhất có sẵ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">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline
pipeline = pipeline(task=<span class="hljs-string">&quot;text-generation&quot;</span>, model=<span class="hljs-string">&quot;HuggingFaceTB/SmolLM2-1.7B-Instruct&quot;</span>, dtype=<span class="hljs-string">&quot;auto&quot;</span>, device_map=<span class="hljs-string">&quot;auto&quot;</span>)
response = pipeline(chat, max_new_tokens=<span class="hljs-number">512</span>)
<span class="hljs-built_in">print</span>(response[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>][-<span class="hljs-number">1</span>][<span class="hljs-string">&quot;content&quot;</span>])<!----></pre></div><!----> <p>Nếu quá trình này thành công, bạn sẽ thấy phản hồi từ mô hình! Nếu bạn muốn tiếp tục cuộc trò chuyện,
bạn cần cập nhật lịch sử trò chuyện với phản hồi của mô hình. Bạn có thể làm điều này bằng cách thêm văn bản
vào <code>chat</code> (sử dụng vai trò <code>assistant</code>), hoặc bằng cách đọc <code>response[0]["generated_text"]</code>, bao gồm
lịch sử trò chuyện đầy đủ, kể cả phản hồi mới nhất.</p> <p>Khi đã có phản hồi của mô hình, bạn có thể tiếp tục cuộc trò chuyện bằng cách thêm một thông điệp <code>user</code> mới vào lịch sử chat.</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 "><!---->chat = response[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>]
chat.append(
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Woah! But can it be reconciled with quantum mechanics?&quot;</span>}
)
response = pipeline(chat, max_new_tokens=<span class="hljs-number">512</span>)
<span class="hljs-built_in">print</span>(response[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>][-<span class="hljs-number">1</span>][<span class="hljs-string">&quot;content&quot;</span>])<!----></pre></div><!----> <p>Bằng cách lặp lại quy trình này, bạn có thể tiếp tục cuộc trò chuyện tùy ý, ít nhất là cho đến khi mô hình hết cửa sổ ngữ cảnh
hoặc bạn hết bộ nhớ.</p> <!--[1--><h2 class="relative group"><a id="performance-and-memory-usage" 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="#performance-and-memory-usage"><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><!---->Hiệu suất và mức sử dụng bộ nhớ<!----></span></h2><!--]--><!----> <p>Transformers nạp mô hình với độ chính xác <code>float32</code> đầy đủ theo mặc định, và đối với một mô hình 8B, điều này đòi hỏi ~32GB bộ nhớ! Hãy sử dụng đối số <code>torch_dtype="auto"</code>, thường dùng <code>bfloat16</code> cho các mô hình được huấn luyện với định dạng này, để giảm mức sử dụng bộ nhớ của bạn.</p> <blockquote class="tip"><p>Tham khảo tài liệu <a href="./quantization/overview">Quantization</a> để biết thêm thông tin về các backend lượng tử hóa khác nhau có sẵn.</p></blockquote> <p>Để giảm mức sử dụng bộ nhớ xuống thấp hơn nữa, bạn có thể lượng tử hóa mô hình xuống 8-bit hoặc 4-bit bằng <a href="https://hf.co/docs/bitsandbytes/index" rel="nofollow">bitsandbytes</a>. Tạo một <a href="/docs/transformers/main/vi/main_classes/quantization#transformers.BitsAndBytesConfig">BitsAndBytesConfig</a> với các cài đặt lượng tử hóa mong muốn và truyền nó vào tham số <code>model_kwargs</code> của pipelines. Ví dụ bên dưới lượng tử hóa một mô hình xuống 8-bit.</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> pipeline, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=<span class="hljs-literal">True</span>)
pipeline = pipeline(task=<span class="hljs-string">&quot;text-generation&quot;</span>, model=<span class="hljs-string">&quot;meta-llama/Meta-Llama-3-8B-Instruct&quot;</span>, device_map=<span class="hljs-string">&quot;auto&quot;</span>, model_kwargs={<span class="hljs-string">&quot;quantization_config&quot;</span>: quantization_config})<!----></pre></div><!----> <p>Nhìn chung, kích thước và hiệu suất của mô hình có mối tương quan trực tiếp. Các mô hình lớn hơn sẽ chạy chậm hơn, đồng thời đòi hỏi nhiều bộ nhớ hơn, vì mỗi tham số hoạt động phải được đọc từ bộ nhớ cho từng token được tạo ra.
Đây là nút thắt cổ chai trong việc tạo văn bản của LLM và các lựa chọn chính để cải thiện tốc độ tạo là hoặc lượng tử hóa mô hình, hoặc sử dụng phần cứng có băng thông bộ nhớ cao hơn. Việc tăng thêm sức mạnh tính toán không mang lại lợi ích đáng kể.</p> <p>Bạn cũng có thể thử các kỹ thuật như <a href="./assisted_decoding#speculative-decoding">speculative decoding</a>, trong đó một mô hình nhỏ hơn tạo ra các token ứng viên để mô hình lớn hơn xác minh. Nếu các token ứng viên là chính xác, mô hình lớn hơn có thể tạo ra nhiều hơn một token mỗi lần. Điều này giảm đáng kể nút thắt cổ chai về băng thông và cải thiện tốc độ tạo.</p> <blockquote class="tip"><p>Các mô hình Mixture-of-Expert (MoE) như <a href="./model_doc/mixtral">Mixtral</a>, <a href="./model_doc/qwen2_moe">Qwen2MoE</a> và <a href="./model_doc/gpt_oss">GPT-OSS</a> có rất nhiều tham số, nhưng chỉ “kích hoạt” một phần nhỏ trong số đó để tạo ra mỗi token. Do đó, các mô hình MoE thường có yêu cầu về băng thông bộ nhớ thấp hơn nhiều và có thể nhanh hơn so với một LLM thông thường cùng kích thước. Tuy nhiên, các kỹ thuật như speculative decoding không hiệu quả với các mô hình MoE vì số tham số được kích hoạt sẽ tăng lên với mỗi token dự đoán mới.</p></blockquote> <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/conversations.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_dqakyn = {
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.CDsgEKDZ.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.DxuVi3jG.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 6],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
29.6 kB
·
Xet hash:
d832fb2ff28cb614436296ad575c968ea91e2eba60183202f8f14eaeb02d3400

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