Buckets:

hf-doc-build/doc / transformers /main /vi /auto_docstring.html
mishig's picture
download
raw
72.6 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Tự động tạo docstring&quot;,&quot;local&quot;:&quot;auto-generating-docstrings&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;@auto_docstring&quot;,&quot;local&quot;:&quot;autodocstring&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cách sử dụng&quot;,&quot;local&quot;:&quot;usage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Các lớp mô hình&quot;,&quot;local&quot;:&quot;model-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Các lớp cấu hình&quot;,&quot;local&quot;:&quot;config-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Các lớp Processor&quot;,&quot;local&quot;:&quot;processor-classes&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Bộ xử lý hình ảnh và video&quot;,&quot;local&quot;:&quot;image-and-video-processors&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4}],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Hàm&quot;,&quot;local&quot;:&quot;functions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Tài liệu hóa các đối số&quot;,&quot;local&quot;:&quot;documenting-arguments&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Kiểm tra các docstring&quot;,&quot;local&quot;:&quot;checking-the-docstrings&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Danh sách kiểm tra tham khảo nhanh&quot;,&quot;local&quot;:&quot;quick-reference-checklist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cách hoạt động&quot;,&quot;local&quot;:&quot;how-it-works&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/11.CgcRyIiP.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload">
<!--1wlpssc--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Auto-generating docstrings&quot;,&quot;local&quot;:&quot;auto-generating-docstrings&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;@auto_docstring&quot;,&quot;local&quot;:&quot;autodocstring&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Usage&quot;,&quot;local&quot;:&quot;usage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model classes&quot;,&quot;local&quot;:&quot;model-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Config classes&quot;,&quot;local&quot;:&quot;config-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Processor classes&quot;,&quot;local&quot;:&quot;processor-classes&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image and video processors&quot;,&quot;local&quot;:&quot;image-and-video-processors&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4}],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Functions&quot;,&quot;local&quot;:&quot;functions&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Documenting arguments&quot;,&quot;local&quot;:&quot;documenting-arguments&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Checking the docstrings&quot;,&quot;local&quot;:&quot;checking-the-docstrings&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Quick-reference checklist&quot;,&quot;local&quot;:&quot;quick-reference-checklist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;How it works&quot;,&quot;local&quot;:&quot;how-it-works&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="auto-generating-docstrings" 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="#auto-generating-docstrings"><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ự động tạo docstring<!----></span></h1><!--]--><!----> <p>Trình trang trí <code>@auto_docstring</code> tạo ra các docstring nhất quán cho các lớp và phương thức của model. Nó tự động lấy các mô tả tham số chuẩn, vì vậy bạn chỉ cần viết tài liệu cho các tham số mới hoặc tùy chỉnh. Khi <a href="./modular_transformers">thêm một model mới</a>, hãy bỏ qua các phần lặp lại và tập trung vào những gì mới.</p> <!--[1--><h2 class="relative group"><a id="autodocstring" 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="#autodocstring"><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><!---->@auto_docstring<!----></span></h2><!--]--><!----> <p>Nhập decorator trong tệp <code>modular_model.py</code> của bạn (hoặc <code>modeling_model.py</code> đối với các model cũ 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-python "><!----><span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring<!----></pre></div><!----> <p>Nếu model của bạn kế thừa từ một model của thư viện khác trong một file modular, <code>@auto_docstring</code> đã được áp dụng trong class cha. <code>make fix-repo</code> sẽ sao chép nó vào file <code>modeling_model.py</code> được tạo ra cho bạn. Chỉ cần áp dụng decorator một cách tường minh để tùy chỉnh hành vi của nó (các model độc lập, custom intros, hoặc các argument bị ghi đè).</p> <blockquote class="warning"><p>Khi ghi đè bất kỳ decorator nào trong một file modular, hãy bao gồm <strong>tất cả</strong> các decorator từ hàm hoặc lớp cha. Nếu bạn chỉ ghi đè một số, phần còn lại sẽ không xuất hiện trong file modeling được tạo ra.</p></blockquote> <p>Trình trang trí chấp nhận các đối số tùy chọn sau:</p> <table><thead><tr><th>tham số</th><th>mô tả</th></tr></thead><tbody><tr><td><code>custom_intro</code></td><td>Mô tả lớp hoặc phương thức, được chèn vào trước phần Args. Bắt buộc đối với các lớp không kết thúc bằng <a href="#how-it-works">hậu tố được công nhận</a> như <code>ForCausalLM</code> hoặc <code>ForTokenClassification</code>.</td></tr><tr><td><code>custom_args</code></td><td>Văn bản docstring cho các tham số cụ thể. Hữu ích khi cùng một tập hợp các đối số tùy chỉnh xuất hiện ở nhiều vị trí khác nhau trong tệp modeling.</td></tr><tr><td><code>checkpoint</code></td><td>Một định danh checkpoint mô hình (<code>"org/my-model"</code>) được sử dụng để tạo các ví dụ sử dụng. Ghi đè checkpoint được suy luận tự động từ lớp cấu hình. Thường được đặt trên các lớp cấu hình.</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="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="#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><!---->Cách sử dụng<!----></span></h2><!--]--><!----> <p>Cách <code>@auto_docstring</code> hoạt động phụ thuộc vào đối tượng bạn đang trang trí. Các lớp Model lấy tài liệu tham số từ <code>__init__</code>, các lớp config lấy từ các chú thích ở cấp lớp, các lớp processor tự động tạo mô tả từ các thành phần của chúng, và các phương thức như <code>forward</code> sẽ nhận được kiểu trả về cùng các ví dụ sử dụng.</p> <!--[2--><h3 class="relative group"><a id="model-classes" 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="#model-classes"><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 lớp mô hình<!----></span></h3><!--]--><!----> <p>Đặt <code>@auto_docstring</code> ngay phía trên định nghĩa lớp. Bộ trang trí này suy ra mô tả tham số từ chữ ký và docstring của phương thức <code>__init__</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-python "><!----><span class="hljs-keyword">from</span> transformers.modeling_utils <span class="hljs-keyword">import</span> PreTrainedModel
<span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring
<span class="hljs-meta">@auto_docstring</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyAwesomeModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config, custom_parameter: <span class="hljs-built_in">int</span> = <span class="hljs-number">10</span>, another_custom_arg: <span class="hljs-built_in">str</span> = <span class="hljs-string">&quot;default&quot;</span></span>):
<span class="hljs-string">r&quot;&quot;&quot;
custom_parameter (`int`, *optional*, defaults to 10):
Description of the custom_parameter for MyAwesomeModel.
another_custom_arg (`str`, *optional*, defaults to &quot;default&quot;):
Documentation for another unique argument.
&quot;&quot;&quot;</span>
<span class="hljs-built_in">super</span>().__init__(config)
<span class="hljs-variable language_">self</span>.custom_parameter = custom_parameter
<span class="hljs-variable language_">self</span>.another_custom_arg = another_custom_arg
<span class="hljs-comment"># ... rest of your init</span>
<span class="hljs-comment"># ... other methods</span><!----></pre></div><!----> <p>Truyền <code>custom_intro</code> và <code>custom_args</code> để có thêm quyền kiểm soát. Các đối số tùy chỉnh có thể đặt trong <code>custom_args</code> hoặc trong docstring của <code>__init__</code>. Sử dụng <code>custom_args</code> khi cùng một tập hợp đối số lặp lại ở nhiều phương thức khác 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-python "><!----><span class="hljs-meta">@auto_docstring(<span class="hljs-params">
custom_intro=<span class="hljs-string">&quot;&quot;&quot;This model performs specific synergistic operations.
It builds upon the standard Transformer architecture with unique modifications.&quot;&quot;&quot;</span>,
custom_args=<span class="hljs-string">&quot;&quot;&quot;
custom_parameter (`type`, *optional*, defaults to `default_value`):
A concise description for custom_parameter if not defined or overriding the description in `auto_docstring.py`.
internal_helper_arg (`type`, *optional*, defaults to `default_value`):
A concise description for internal_helper_arg if not defined or overriding the description in `auto_docstring.py`.
&quot;&quot;&quot;</span>
</span>)</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MySpecialModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config: ConfigType, custom_parameter: <span class="hljs-string">&quot;type&quot;</span> = <span class="hljs-string">&quot;default_value&quot;</span>, internal_helper_arg=<span class="hljs-literal">None</span></span>):
<span class="hljs-comment"># ...</span><!----></pre></div><!----> <p>Cũng áp dụng <code>@auto_docstring</code> cho các lớp kế thừa từ <a href="/docs/transformers/main/vi/main_classes/output#transformers.utils.ModelOutput">ModelOutput</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-python "><!----><span class="hljs-meta">@auto_docstring(<span class="hljs-params">
custom_intro=<span class="hljs-string">&quot;&quot;&quot;
Custom model outputs with additional fields.
&quot;&quot;&quot;</span>
</span>)</span>
<span class="hljs-meta">@dataclass</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelOutput</span>(<span class="hljs-title class_ inherited__">ImageClassifierOutput</span>):
<span class="hljs-string">r&quot;&quot;&quot;
loss (`torch.Tensor`, *optional*):
The loss of the model.
custom_field (`torch.Tensor` of shape `(batch_size, hidden_size)`, *optional*):
A custom output field specific to this model.
&quot;&quot;&quot;</span>
<span class="hljs-comment"># Standard fields (hidden_states, logits, attentions, etc.) are documented automatically when</span>
<span class="hljs-comment"># the description matches the standard text. Loss typically varies per model, so document it above.</span>
loss: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span>
logits: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span>
hidden_states: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">tuple</span>[torch.Tensor, ...]] = <span class="hljs-literal">None</span>
attentions: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">tuple</span>[torch.Tensor, ...]] = <span class="hljs-literal">None</span>
<span class="hljs-comment"># Custom fields need to be documented in the docstring above</span>
custom_field: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="config-classes" 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="#config-classes"><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 lớp cấu hình<!----></span></h3><!--]--><!----> <p>Đặt <code>@auto_docstring</code> ngay phía trên một lớp con của <a href="/docs/transformers/main/vi/main_classes/configuration#transformers.PreTrainedConfig">PreTrainedConfig</a>, cùng với decorator <code>@strict</code>. <code>@strict</code> thêm vào việc kiểm tra kiểu dữ liệu lúc runtime và biến lớp thành một dataclass đã được kiểm chứng. Các tham số cấu hình là <em>ghi chú kiểu ở cấp lớp</em> (không phải các đối số của <code>__init__</code>), và <code>@auto_docstring</code> đọc chúng từ thân lớp để tạo tài liệu.</p> <p><code>ConfigArgs</code> cung cấp các tham số chuẩn như <code>vocab_size</code>, <code>hidden_size</code> và <code>num_hidden_layers</code>, do đó chúng không cần mô tả trừ khi hành vi khác đi. Các tham số cơ sở của <a href="/docs/transformers/main/vi/main_classes/configuration#transformers.PreTrainedConfig">PreTrainedConfig</a> được loại bỏ tự động. Đối số <code>checkpoint</code> tạo ra ví dụ 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-python "><!----><span class="hljs-keyword">from</span> huggingface_hub.dataclasses <span class="hljs-keyword">import</span> strict
<span class="hljs-keyword">from</span> ...configuration_utils <span class="hljs-keyword">import</span> PreTrainedConfig
<span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring
<span class="hljs-meta">@strict</span>
<span class="hljs-meta">@auto_docstring(<span class="hljs-params">checkpoint=<span class="hljs-string">&quot;org/my-model-checkpoint&quot;</span></span>)</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelConfig</span>(<span class="hljs-title class_ inherited__">PreTrainedConfig</span>):
<span class="hljs-string">r&quot;&quot;&quot;
custom_param (`int`, *optional*, defaults to 64):
Description of a parameter specific to this model.
another_param (`str`, *optional*, defaults to `&quot;gelu&quot;`):
Description of another model-specific parameter.
```python
from transformers import MyModelConfig, MyModel
configuration = MyModelConfig()
model = MyModel(configuration)
configuration = model.config
```
&quot;&quot;&quot;</span>
model_type = <span class="hljs-string">&quot;my_model&quot;</span>
<span class="hljs-comment"># Standard params (vocab_size, hidden_size, etc.) are auto-documented from ConfigArgs.</span>
vocab_size: <span class="hljs-built_in">int</span> = <span class="hljs-number">32000</span>
hidden_size: <span class="hljs-built_in">int</span> = <span class="hljs-number">768</span>
num_hidden_layers: <span class="hljs-built_in">int</span> = <span class="hljs-number">12</span>
<span class="hljs-comment"># Model-specific params must be documented in the class docstring above.</span>
custom_param: <span class="hljs-built_in">int</span> = <span class="hljs-number">64</span>
another_param: <span class="hljs-built_in">str</span> = <span class="hljs-string">&quot;gelu&quot;</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="processor-classes" 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="#processor-classes"><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 lớp Processor<!----></span></h3><!--]--><!----> <p>Bộ xử lý đa phương thức (các lớp con của <a href="/docs/transformers/main/vi/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a>, <code>processing_*.py</code>) luôn sử dụng <code>@auto_docstring</code> thuần. Phần giới thiệu lớp được tạo tự động. Chỉ tài liệu hóa các tham số của <code>__init__</code> chưa được bao phủ bởi <code>ProcessorArgs</code> (<code>image_processor</code>, <code>tokenizer</code>, <code>chat_template</code>, và các tham số khác).</p> <p>Nếu mọi tham số đều là chuẩn, hãy bỏ qua docstring. Trang trí <code>__call__</code> bằng <code>@auto_docstring</code> cũng vậy. Docstring thân của nó chỉ chứa một phần <code>Returns:</code> cùng bất kỳ đối số gọi cụ thể nào của mô hình. <code>return_tensors</code> được thêm vào 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-python "><!----><span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> ProcessorMixin, ProcessingKwargs, Unpack
<span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelProcessorKwargs</span>(ProcessingKwargs, total=<span class="hljs-literal">False</span>):
<span class="hljs-string">&quot;&quot;&quot;
custom_call_kwarg (`bool`, *optional*, defaults to `False`):
A parameter with a default that can passed to __call__ which is not
covered by the standard processor arguments (including each modality).
&quot;&quot;&quot;</span>
custom_call_kwarg: <span class="hljs-built_in">bool</span>
<span class="hljs-meta">@auto_docstring</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelProcessor</span>(<span class="hljs-title class_ inherited__">ProcessorMixin</span>):
valid_processor_kwargs = MyModelProcessorKwargs
custom_call_kwarg = <span class="hljs-literal">False</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, image_processor=<span class="hljs-literal">None</span>, tokenizer=<span class="hljs-literal">None</span>, custom_param: <span class="hljs-built_in">int</span> = <span class="hljs-number">4</span>, **kwargs</span>):
<span class="hljs-string">r&quot;&quot;&quot;
custom_param (`int`, *optional*, defaults to 4):
A parameter specific to this processor not covered by the standard ProcessorArgs.
&quot;&quot;&quot;</span>
<span class="hljs-built_in">super</span>().__init__(image_processor, tokenizer)
<span class="hljs-variable language_">self</span>.custom_param = custom_param
<span class="hljs-meta"> @auto_docstring</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__call__</span>(<span class="hljs-params">self, images=<span class="hljs-literal">None</span>, text=<span class="hljs-literal">None</span>, **kwargs: Unpack[MyModelProcessorKwargs]</span>):
<span class="hljs-string">r&quot;&quot;&quot;
Returns:
[BatchFeature](/docs/transformers/main/en/main_classes/feature_extractor#transformers.BatchFeature): A [BatchFeature](/docs/transformers/main/en/main_classes/feature_extractor#transformers.BatchFeature) with the following fields:
- **input_ids** -- Token ids to be fed to the model.
- **pixel_values** -- Pixel values to be fed to the model.
&quot;&quot;&quot;</span>
<span class="hljs-comment"># ...</span><!----></pre></div><!----> <!--[3--><h4 class="relative group"><a id="image-and-video-processors" 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="#image-and-video-processors"><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ộ xử lý hình ảnh và video<!----></span></h4><!--]--><!----> <p>Bộ xử lý hình ảnh và video (các lớp con của <code>BaseImageProcessor</code>, <code>image_processing_*.py</code>) tuân theo một trong hai mẫu.</p> <p>Nếu bộ xử lý có các tham số cụ thể cho mô hình, hãy định nghĩa một TypedDict <code>XxxImageProcessorKwargs(ImagesKwargs, total=False)</code> với docstring cho các tham số đó, đặt <code>valid_kwargs</code> trên lớp và sử dụng <code>@auto_docstring</code> trần. <code>__init__</code> không có docstring.</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-python "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorKwargs</span>(ImagesKwargs, total=<span class="hljs-literal">False</span>):
<span class="hljs-string">r&quot;&quot;&quot;
custom_threshold (`float`, *optional*, defaults to `self.custom_threshold`):
A parameter specific to this image processor.
&quot;&quot;&quot;</span>
custom_threshold: <span class="hljs-built_in">float</span> | <span class="hljs-literal">None</span>
<span class="hljs-meta">@auto_docstring</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessor</span>(<span class="hljs-title class_ inherited__">TorchvisionBackend</span>):
valid_kwargs = MyModelImageProcessorKwargs
custom_threshold: <span class="hljs-built_in">float</span> = <span class="hljs-number">0.5</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelImageProcessorKwargs]</span>):
<span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <p>Nếu lớp chỉ đặt các thuộc tính cấp lớp chuẩn (<code>size</code>, <code>resample</code>, <code>image_mean</code>, v.v.) mà không có kwargs tùy chỉnh nào, hãy dùng <code>@auto_docstring(custom_intro="Constructs a MyModel image processor.")</code> thay thế.</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-python "><!----><span class="hljs-meta">@auto_docstring(<span class="hljs-params">custom_intro=<span class="hljs-string">&quot;Constructs a MyModel image processor.&quot;</span></span>)</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessor</span>(<span class="hljs-title class_ inherited__">TorchvisionBackend</span>):
resample = PILImageResampling.BICUBIC
image_mean = IMAGENET_STANDARD_MEAN
image_std = IMAGENET_STANDARD_STD
size = {<span class="hljs-string">&quot;height&quot;</span>: <span class="hljs-number">224</span>, <span class="hljs-string">&quot;width&quot;</span>: <span class="hljs-number">224</span>}<!----></pre></div><!----> <p>Khi ghi đè <code>preprocess</code>, hãy trang trí nó bằng <code>@auto_docstring</code> và chỉ tài liệu hóa các đối số không có trong <code>ImageProcessorArgs</code>. Các đối số chuẩn và <code>return_tensors</code> sẽ được bao gồm tự động.</p> <!--[2--><h3 class="relative group"><a id="functions" 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="#functions"><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><!---->Hàm<!----></span></h3><!--]--><!----> <p>Đặt <code>@auto_docstring</code> ngay phía trên định nghĩa hàm. Bộ trang trí này suy ra mô tả tham số từ chữ ký của hàm.</p> <p>Trình trang trí tạo ra văn bản giá trị trả về từ docstring của lớp <code>ModelOutput</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-python "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>):
<span class="hljs-comment"># ...</span>
<span class="hljs-meta"> @auto_docstring</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">
self,
input_ids: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span>,
attention_mask: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span>,
new_custom_argument: <span class="hljs-type">Optional</span>[torch.Tensor] = <span class="hljs-literal">None</span>,
<span class="hljs-comment"># ... other arguments</span>
</span>) -&gt; <span class="hljs-type">Union</span>[<span class="hljs-type">Tuple</span>, ModelOutput]:
<span class="hljs-string">r&quot;&quot;&quot;
new_custom_argument (`torch.Tensor`, *optional*):
Description of this new custom argument and its expected shape or type.
&quot;&quot;&quot;</span>
<span class="hljs-comment"># ...</span><!----></pre></div><!----> <p>Truyền <code>custom_intro</code> và <code>custom_args</code> để có thêm quyền kiểm soát. Sử dụng <code>custom_args</code> để định nghĩa tài liệu cho các tham số dùng chung một lần khi cùng một tập tham số xuất hiện trong nhiều phương thứ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-python "><!---->MODEL_COMMON_CUSTOM_ARGS = <span class="hljs-string">r&quot;&quot;&quot;
common_arg_1 (`torch.Tensor`, *optional*, defaults to `default_value`):
Description of common_arg_1
common_arg_2 (`torch.Tensor`, *optional*, defaults to `default_value`):
Description of common_arg_2
&quot;&quot;&quot;</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>):
<span class="hljs-comment"># ...</span>
<span class="hljs-meta"> @auto_docstring(<span class="hljs-params">
custom_intro=<span class="hljs-string">&quot;&quot;&quot;This is a custom introduction for the function.&quot;&quot;&quot;</span>,
custom_args=MODEL_COMMON_CUSTOM_ARGS
</span>)</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, input_ids=<span class="hljs-literal">None</span>, common_arg_1=<span class="hljs-literal">None</span>, common_arg_2=<span class="hljs-literal">None</span></span>) -&gt; ModelOutput:
<span class="hljs-string">r&quot;&quot;&quot;method-specific args go here&quot;&quot;&quot;</span>
<span class="hljs-comment"># ...</span><!----></pre></div><!----> <p>Viết các phần <code>Returns</code> và <code>Examples</code> thủ công trong docstring để ghi đè các phiên bản được tạo 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-python "><!----> <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, input_ids=<span class="hljs-literal">None</span></span>) -&gt; torch.Tensor:
<span class="hljs-string">r&quot;&quot;&quot;
Returns:
`torch.Tensor`: A custom Returns section for non-ModelOutput return types.
Example:
```python
model = MyModel.from_pretrained(&quot;org/my-model&quot;)
output = model(input_ids)
```
&quot;&quot;&quot;</span>
<span class="hljs-comment"># ...</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="documenting-arguments" 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="#documenting-arguments"><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ài liệu hóa các đối số<!----></span></h3><!--]--><!----> <p>Tuân theo các quy tắc sau khi tài liệu hóa các kiểu đối số khác nhau.</p> <ul><li><p><code>auto_docstring.py</code> định nghĩa các đối số chuẩn (<code>input_ids</code>, <code>attention_mask</code>, <code>pixel_values</code>, v.v.) và tự động bao gồm chúng. Không định nghĩa lại chúng cục bộ trừ khi đối số hoạt động khác trong mô hình của bạn.</p> <p>Nếu một đối số chuẩn hoạt động khác đi trong model của bạn, hãy ghi đè cục bộ trong khối <code>r""" """</code>. Định nghĩa cục bộ có độ ưu tiên cao hơn. Ví dụ, đối số <code>labels</code> thường được tùy chỉnh riêng cho từng model và thường cần được ghi đè.</p></li> <li><p>Các đối số cấu hình chuẩn (<code>vocab_size</code>, <code>hidden_size</code>, <code>num_hidden_layers</code>, v.v.) tuân theo cùng một nguyên tắc nhưng được lấy từ <code>ConfigArgs</code>. Các đối số bộ xử lý chuẩn (<code>image_processor</code>, <code>tokenizer</code>, <code>do_resize</code>, <code>return_tensors</code>, v.v.) được lấy từ <code>ProcessorArgs</code> và <code>ImageProcessorArgs</code>. Chỉ tài liệu hóa một tham số nếu nó đặc thù cho mô hình hoặc có hành vi khác so với mô tả chuẩn.</p></li> <li><p>Tài liệu hóa các đối số mới hoặc tùy chỉnh trong khối <code>r""" """</code>. Đặt chúng sau chữ ký đối với các hàm, trong docstring của <code>__init__</code> đối với các lớp model hoặc processor, trong docstring thân lớp đối với các lớp config, hoặc trong thân TypedDict <code>XxxImageProcessorKwargs</code> đối với các image processor.</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 "><!---->argument_name (`<span class="hljs-built_in">type</span>`, *optional*, defaults to `X`):
Description of the argument.
Explain its purpose, expected shape/<span class="hljs-built_in">type</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">complex</span>, <span class="hljs-keyword">and</span> default behavior.
This can span multiple lines.<!----></pre></div><!----> <ul><li>Bao gồm <code>type</code> trong dấu backtick.</li> <li>Thêm <em>optional</em> nếu đối số không bắt buộc hoặc có giá trị mặc định.</li> <li>Thêm “mặc định là X” nếu nó có giá trị mặc định. Bạn không cần thêm “mặc định là <code>None</code>” nếu giá trị mặc định là <code>None</code>.</li> <li>Truyền cùng một khối vào <code>custom_args</code> khi cùng các đối số lặp lại qua nhiều phương thức (xem <a href="#functions">ví dụ về Functions ở trên</a>).</li></ul></li> <li><p>Trình trang trí (decorator) tự động trích xuất các kiểu dữ liệu từ chữ ký của hàm. Nếu một tham số có chú thích kiểu dữ liệu, bạn không cần lặp lại kiểu đó trong chuỗi định dạng của docstring. Khi cả hai đều có mặt, kiểu dữ liệu trong chữ ký sẽ được ưu tiên. Kiểu dữ liệu trong docstring đóng vai trò là phương án dự phòng cho các tham số không có chú thích.</p></li></ul> <!--[1--><h2 class="relative group"><a id="checking-the-docstrings" 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="#checking-the-docstrings"><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><!---->Kiểm tra các docstring<!----></span></h2><!--]--><!----> <p>Một script tiện ích xác thực docstring khi bạn mở một pull request. CI chạy script và kiểm tra các mục sau.</p> <blockquote class="tip"><p>Nếu bạn thấy <code>[ERROR]</code> trong đầu ra, hãy thêm mô tả của tham số vào docstring hoặc lớp Args phù hợp trong <code>auto_docstring.py</code>.</p></blockquote> <ul><li>Kiểm tra xem <code>@auto_docstring</code> có được áp dụng cho các lớp model và phương thức công khai liên quan hay không.</li> <li>Kiểm tra tính đầy đủ và nhất quán của các đối số: các đối số được tài liệu hóa phải tồn tại trong chữ ký, và kiểu dữ liệu cùng giá trị mặc định phải khớp. Các đối số không xác định mà không có mô tả cục bộ sẽ được đánh dấu.</li> <li>Đánh dấu các placeholder chưa hoàn chỉnh như <code>&lt;fill_type></code> và <code>&lt;fill_docstring></code>.</li> <li>Kiểm tra xem các docstring có tuân theo phong cách định dạng mong đợi hay không.</li></ul> <p>Chạy kiểm tra cục bộ trước khi cam kế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-bash "><!---->make fix-repo<!----></pre></div><!----> <p><code>make fix-repo</code> cũng chạy một số kiểm tra khác. Để chỉ chạy các kiểm tra docstring và auto-docstring, hãy sử dụng 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 "><!----><span class="hljs-comment"># to only check files included in the diff without fixing them</span>
python utils/check_docstrings.py
<span class="hljs-comment"># to fix and overwrite the files in the diff</span>
<span class="hljs-comment"># python utils/check_docstrings.py --fix_and_overwrite</span>
<span class="hljs-comment"># to fix and overwrite all files</span>
<span class="hljs-comment"># python utils/check_docstrings.py --fix_and_overwrite --check_all</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="quick-reference-checklist" 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="#quick-reference-checklist"><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><!---->Danh sách kiểm tra tham khảo nhanh<!----></span></h2><!--]--><!----> <table><thead><tr><th>Làm</th><th>Đừng</th></tr></thead><tbody><tr><td>Áp dụng <code>@auto_docstring</code> cho các lớp model, config và processor cùng các phương thức chính của chúng (<code>forward</code>, <code>__call__</code>, <code>preprocess</code>).</td><td>Thêm <code>@auto_docstring</code> vào các model kế thừa trong các file modular vì nó được kế thừa tự động.</td></tr><tr><td>Chỉ tài liệu hóa các đối số mới hoặc các đối số cụ thể cho mô hình.</td><td>Định nghĩa lại các đối số chuẩn (<code>input_ids</code>, <code>attention_mask</code>, <code>vocab_size</code>, v.v.) có hành vi giống như mô tả mặc định của chúng.</td></tr><tr><td>Đặt các tham số cấu hình trong docstring của thân lớp dưới dạng chú thích cấp lớp.</td><td>Đặt các tham số cấu hình trong <code>__init__</code>.</td></tr><tr><td>Đặt các tham số bộ xử lý ảnh vào một TypedDict <code>XxxImageProcessorKwargs</code>.</td><td>Đặt các tham số bộ xử lý ảnh trong <code>__init__</code>.</td></tr><tr><td>Chạy <code>python utils/check_docstrings.py --fix_and_overwrite</code> trước khi commit.</td><td>Bỏ qua đầu ra <code>[ERROR]</code> vì nó có nghĩa là một tham số chưa được tài liệu hóa.</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="how-it-works" 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="#how-it-works"><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ách hoạt động<!----></span></h2><!--]--><!----> <p>Trình trang trí <code>@auto_docstring</code> tạo ra các docstring thông qua các bước sau.</p> <ol><li><p>Trình trang trí kiểm tra chữ ký để đọc các đối số, kiểu dữ liệu và giá trị mặc định từ <code>__init__</code> của lớp được trang trí hoặc từ hàm được trang trí. Đối với các lớp cấu hình, nó duyệt qua các chú thích ở cấp độ lớp dọc theo chuỗi kế thừa và dừng lại trước <a href="/docs/transformers/main/vi/main_classes/configuration#transformers.PreTrainedConfig">PreTrainedConfig</a>, loại trừ các trường của lớp cơ sở.</p> <p>Nó tự động lọc ra các tham số như <code>self</code>, <code>kwargs</code>, <code>args</code>, <code>deprecated_arguments</code> và các tên có tiền tố <code>_</code>. Một số tham số riêng tư được đổi tên thành tương đương công khai của chúng (<code>_out_features</code> → <code>out_features</code> đối với các mô hình backbone).</p></li> <li><p>Mô tả các đối số phổ biến được lấy từ <code>auto_docstring.py</code>: <code>ModelArgs</code> (đầu vào của model), <code>ModelOutputArgs</code> (các trường đầu ra như <code>hidden_states</code>), <code>ImageProcessorArgs</code> (tiền xử lý ảnh), <code>ProcessorArgs</code> (các thành phần của processor đa phương tiện) và <code>ConfigArgs</code> (các siêu tham số cấu hình).</p></li> <li><p>Mô tả của từng tham số tuân theo chuỗi ưu tiên sau:</p> <ul><li>Docstring thủ công (<code>r""" """</code> block hoặc <code>custom_args</code>) có độ ưu tiên cao hơn.</li> <li>Từ điển nguồn được định nghĩa trước (<code>ModelArgs</code>, <code>ModelForArgs</code>, <code>ConfigArgs</code>, <code>ImageProcessorArgs</code>, <code>ProcessorArgs</code>, <code>ModelOutputArgs</code>) là phương án dự phòng.</li> <li>Nếu cả hai nguồn đều không có mô tả, tham số sẽ được đánh dấu bằng <code>[ERROR]</code> trong kết quả build.</li></ul></li> <li><p>Đối với các lớp mô hình có tên chuẩn như <code>ModelForCausalLM</code>, hoặc các lớp được ánh xạ tới một pipeline, <code>@auto_docstring</code> sẽ tạo phần giới thiệu. Đối với các bộ xử lý đa phương thức, phần giới thiệu sẽ liệt kê các thành phần (tokenizer, image processor, v.v.) mà lớp đó bao bọc. Xem <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/utils/auto_docstring.py#L2437" rel="nofollow">ClassDocstring</a> để biết danh sách đầy đủ.</p> <p>Nếu tên lớp không có trong <code>ClassDocstring</code>, hãy đặt <code>custom_intro</code>.</p></li> <li><p>Docstring được định nghĩa sẵn có thể tham chiếu các giá trị động từ <a href="https://github.com/huggingface/transformers/tree/main/src/transformers/models/auto" rel="nofollow">auto_modules</a> của Transformers, chẳng hạn như <code>{processor_class}</code>, <code>{image_processor_class}</code> và <code>{config_class}</code>. Các placeholder sẽ được tự động phân giải.</p></li> <li><p>Trình trang trí (decorator) chọn các ví dụ sử dụng dựa trên tác vụ hoặc khả năng tương thích pipeline của mô hình. Nó đọc metadata checkpoint từ lớp cấu hình để các ví dụ sử dụng các ID mô hình thực. Đối số <code>checkpoint</code> ghi đè lên checkpoint được suy ra từ docstring của lớp cấu hình. Đặt <code>checkpoint</code> trên các lớp cấu hình, hoặc khi việc suy luận checkpoint thất bại. Nếu bạn thấy lỗi như <code>"Config not found for &lt;model_name>"</code>, hãy thêm một mục vào <code>HARDCODED_CONFIG_FOR_MODELS</code> trong <code>auto_docstring.py</code>.</p></li> <li><p>Đối với các phương thức như <code>forward</code>, decorator sẽ viết phần <code>Returns</code> từ kiểu trả về của phương thức. Khi kiểu trả về là một lớp con của <a href="/docs/transformers/main/vi/main_classes/output#transformers.utils.ModelOutput">ModelOutput</a>, <code>@auto_docstring</code> sẽ lấy mô tả các trường từ docstring của lớp đó. Khối <code>Returns</code> tùy chỉnh trong docstring của hàm sẽ có quyền ưu tiên cao hơn.</p></li> <li><p>Đối với các phương thức trong <code>UNROLL_KWARGS_METHODS</code> và các lớp trong <code>UNROLL_KWARGS_CLASSES</code>, bộ trang trí sẽ mở rộng <code>**kwargs</code> được đánh kiểu bằng <code>Unpack[KwargsTypedDict]</code>. Mỗi khóa từ <code>TypedDict</code> sẽ trở thành một tham số được tài liệu hóa.</p> <p>Cùng sự mở rộng này được áp dụng cho các phương thức <code>__call__</code> và <code>preprocess</code> trên các lớp con của <a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.BaseImageProcessor">BaseImageProcessor</a> và <a href="/docs/transformers/main/vi/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a>. Các kiểu cơ sở chung (<code>TextKwargs</code>, <code>ImagesKwargs</code>, <code>VideosKwargs</code>, <code>AudioKwargs</code>) bị bỏ qua. Chỉ các lớp con cụ thể cho từng model mới được mở rộng.</p></li></ol> <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/auto_docstring.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, 11],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
72.6 kB
·
Xet hash:
01fa6f4b04ae3c9813c7c11fb3e64d165dc3ff9514841623c44fa77f17f17553

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