Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Quy tắc cấu trúc mô hình","local":"model-structure-rules","sections":[{"title":"Chạy trình kiểm tra","local":"running-the-checker","sections":[],"depth":2},{"title":"Sửa một vi phạm","local":"fixing-a-violation","sections":[],"depth":2},{"title":"Tham khảo các quy tắc","local":"rules-reference","sections":[{"title":"TRF002","local":"trf002","sections":[],"depth":3},{"title":"TRF003","local":"trf003","sections":[],"depth":3},{"title":"TRF004","local":"trf004","sections":[],"depth":3},{"title":"TRF005","local":"trf005","sections":[],"depth":3},{"title":"TRF006","local":"trf006","sections":[],"depth":3},{"title":"TRF007","local":"trf007","sections":[],"depth":3},{"title":"TRF008","local":"trf008","sections":[],"depth":3},{"title":"TRF009","local":"trf009","sections":[],"depth":3},{"title":"TRF010","local":"trf010","sections":[],"depth":3},{"title":"TRF011","local":"trf011","sections":[],"depth":3},{"title":"TRF012","local":"trf012","sections":[],"depth":3},{"title":"TRF013","local":"trf013","sections":[],"depth":3},{"title":"TRF014","local":"trf014","sections":[],"depth":3},{"title":"TRF015","local":"trf015","sections":[],"depth":3},{"title":"TRF016","local":"trf016","sections":[],"depth":3},{"title":"TRF017","local":"trf017","sections":[],"depth":3},{"title":"TRF018","local":"trf018","sections":[],"depth":3},{"title":"TRF019","local":"trf019","sections":[],"depth":3},{"title":"TRF020","local":"trf020","sections":[],"depth":3},{"title":"TRF021","local":"trf021","sections":[],"depth":3},{"title":"TRF022","local":"trf022","sections":[],"depth":3},{"title":"TRF023","local":"trf023","sections":[],"depth":3},{"title":"TRF024","local":"trf024","sections":[],"depth":3},{"title":"TRF025","local":"trf025","sections":[],"depth":3},{"title":"TRF026","local":"trf026","sections":[],"depth":3},{"title":"TRF027","local":"trf027","sections":[],"depth":3},{"title":"TRF028","local":"trf028","sections":[],"depth":3},{"title":"TRF029","local":"trf029","sections":[],"depth":3},{"title":"TRF030","local":"trf030","sections":[],"depth":3},{"title":"TRF031","local":"trf031","sections":[],"depth":3},{"title":"TRF032","local":"trf032","sections":[],"depth":3},{"title":"TRF033","local":"trf033","sections":[],"depth":3},{"title":"TRF034","local":"trf034","sections":[],"depth":3},{"title":"TRF035","local":"trf035","sections":[],"depth":3},{"title":"TRF036","local":"trf036","sections":[],"depth":3},{"title":"TRF037","local":"trf037","sections":[],"depth":3},{"title":"TRF038","local":"trf038","sections":[],"depth":3},{"title":"TRF039","local":"trf039","sections":[],"depth":3},{"title":"TRF040","local":"trf040","sections":[],"depth":3},{"title":"TRF041","local":"trf041","sections":[],"depth":3},{"title":"TRF042","local":"trf042","sections":[],"depth":3},{"title":"TRF043","local":"trf043","sections":[],"depth":3},{"title":"TRF044","local":"trf044","sections":[],"depth":3},{"title":"TRF045","local":"trf045","sections":[],"depth":3},{"title":"TRF046","local":"trf046","sections":[],"depth":3},{"title":"TRF047","local":"trf047","sections":[],"depth":3},{"title":"TRF048","local":"trf048","sections":[],"depth":3},{"title":"TRF049","local":"trf049","sections":[],"depth":3},{"title":"TRF050","local":"trf050","sections":[],"depth":3},{"title":"TRF051","local":"trf051","sections":[],"depth":3},{"title":"TRF052","local":"trf052","sections":[],"depth":3},{"title":"TRF053","local":"trf053","sections":[],"depth":3},{"title":"TRF055","local":"trf055","sections":[],"depth":3},{"title":"TRF056","local":"trf056","sections":[],"depth":3},{"title":"TRF057","local":"trf057","sections":[],"depth":3},{"title":"TRF058","local":"trf058","sections":[],"depth":3},{"title":"TRF059","local":"trf059","sections":[],"depth":3},{"title":"TRF060","local":"trf060","sections":[],"depth":3},{"title":"TRF061","local":"trf061","sections":[],"depth":3},{"title":"TRF062","local":"trf062","sections":[],"depth":3},{"title":"TRF063","local":"trf063","sections":[],"depth":3},{"title":"TRF064","local":"trf064","sections":[],"depth":3},{"title":"TRF065","local":"trf065","sections":[],"depth":3},{"title":"TRF066","local":"trf066","sections":[],"depth":3},{"title":"TRF067","local":"trf067","sections":[],"depth":3},{"title":"TRF068","local":"trf068","sections":[],"depth":3}],"depth":2},{"title":"Chặn các vi phạm","local":"suppressing-violations","sections":[{"title":"Inline suppression","local":"inline-suppression","sections":[],"depth":3},{"title":"allowlist_models","local":"allowlistmodels","sections":[],"depth":3}],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.C2wSEYyY.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/B1XvRviU.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.DvYwi2oY.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.D0KV6BkP.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/595.C4u0sxDh.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BznRi4jy.js" rel="modulepreload"> | |
| <!--1ca37fs--><meta name="hf:doc:metadata" content="{"title":"Model structure rules","local":"model-structure-rules","sections":[{"title":"Running the checker","local":"running-the-checker","sections":[],"depth":2},{"title":"Fixing a violation","local":"fixing-a-violation","sections":[],"depth":2},{"title":"Rules reference","local":"rules-reference","sections":[{"title":"TRF002","local":"trf002","sections":[],"depth":3},{"title":"TRF003","local":"trf003","sections":[],"depth":3},{"title":"TRF004","local":"trf004","sections":[],"depth":3},{"title":"TRF005","local":"trf005","sections":[],"depth":3},{"title":"TRF006","local":"trf006","sections":[],"depth":3},{"title":"TRF007","local":"trf007","sections":[],"depth":3},{"title":"TRF008","local":"trf008","sections":[],"depth":3},{"title":"TRF009","local":"trf009","sections":[],"depth":3},{"title":"TRF010","local":"trf010","sections":[],"depth":3},{"title":"TRF011","local":"trf011","sections":[],"depth":3},{"title":"TRF012","local":"trf012","sections":[],"depth":3},{"title":"TRF013","local":"trf013","sections":[],"depth":3},{"title":"TRF014","local":"trf014","sections":[],"depth":3},{"title":"TRF015","local":"trf015","sections":[],"depth":3},{"title":"TRF016","local":"trf016","sections":[],"depth":3},{"title":"TRF017","local":"trf017","sections":[],"depth":3},{"title":"TRF018","local":"trf018","sections":[],"depth":3},{"title":"TRF019","local":"trf019","sections":[],"depth":3},{"title":"TRF020","local":"trf020","sections":[],"depth":3},{"title":"TRF021","local":"trf021","sections":[],"depth":3},{"title":"TRF022","local":"trf022","sections":[],"depth":3},{"title":"TRF023","local":"trf023","sections":[],"depth":3},{"title":"TRF024","local":"trf024","sections":[],"depth":3},{"title":"TRF025","local":"trf025","sections":[],"depth":3},{"title":"TRF026","local":"trf026","sections":[],"depth":3},{"title":"TRF027","local":"trf027","sections":[],"depth":3},{"title":"TRF028","local":"trf028","sections":[],"depth":3},{"title":"TRF029","local":"trf029","sections":[],"depth":3},{"title":"TRF030","local":"trf030","sections":[],"depth":3},{"title":"TRF031","local":"trf031","sections":[],"depth":3},{"title":"TRF032","local":"trf032","sections":[],"depth":3},{"title":"TRF033","local":"trf033","sections":[],"depth":3},{"title":"TRF034","local":"trf034","sections":[],"depth":3},{"title":"TRF035","local":"trf035","sections":[],"depth":3},{"title":"TRF036","local":"trf036","sections":[],"depth":3},{"title":"TRF037","local":"trf037","sections":[],"depth":3},{"title":"TRF038","local":"trf038","sections":[],"depth":3},{"title":"TRF039","local":"trf039","sections":[],"depth":3},{"title":"TRF040","local":"trf040","sections":[],"depth":3},{"title":"TRF041","local":"trf041","sections":[],"depth":3},{"title":"TRF042","local":"trf042","sections":[],"depth":3},{"title":"TRF043","local":"trf043","sections":[],"depth":3},{"title":"TRF044","local":"trf044","sections":[],"depth":3},{"title":"TRF045","local":"trf045","sections":[],"depth":3},{"title":"TRF046","local":"trf046","sections":[],"depth":3},{"title":"TRF047","local":"trf047","sections":[],"depth":3},{"title":"TRF048","local":"trf048","sections":[],"depth":3},{"title":"TRF049","local":"trf049","sections":[],"depth":3},{"title":"TRF050","local":"trf050","sections":[],"depth":3},{"title":"TRF051","local":"trf051","sections":[],"depth":3},{"title":"TRF052","local":"trf052","sections":[],"depth":3},{"title":"TRF053","local":"trf053","sections":[],"depth":3},{"title":"TRF055","local":"trf055","sections":[],"depth":3},{"title":"TRF056","local":"trf056","sections":[],"depth":3},{"title":"TRF057","local":"trf057","sections":[],"depth":3},{"title":"TRF058","local":"trf058","sections":[],"depth":3},{"title":"TRF059","local":"trf059","sections":[],"depth":3},{"title":"TRF060","local":"trf060","sections":[],"depth":3},{"title":"TRF061","local":"trf061","sections":[],"depth":3},{"title":"TRF062","local":"trf062","sections":[],"depth":3},{"title":"TRF063","local":"trf063","sections":[],"depth":3},{"title":"TRF064","local":"trf064","sections":[],"depth":3},{"title":"TRF065","local":"trf065","sections":[],"depth":3},{"title":"TRF066","local":"trf066","sections":[],"depth":3},{"title":"TRF067","local":"trf067","sections":[],"depth":3},{"title":"TRF068","local":"trf068","sections":[],"depth":3}],"depth":2},{"title":"Suppressing violations","local":"suppressing-violations","sections":[{"title":"Inline suppression","local":"inline-suppression","sections":[],"depth":3},{"title":"allowlist_models","local":"allowlistmodels","sections":[],"depth":3}],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="model-structure-rules" 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-structure-rules"><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><!---->Quy tắc cấu trúc mô hình<!----></span></h1><!--]--><!----> <p>Transformers áp dụng một tập hợp các quy tắc tĩnh lên mọi tệp <code>modeling_*.py</code>, <code>modular_*.py</code> và <code>configuration_*.py</code>. Gói <a href="https://github.com/huggingface/transformers-mlinter" rel="nofollow">mlinter</a> cung cấp bộ máy kiểm tra, và kho lưu trữ giữ tập hợp quy tắc đang hoạt động của nó trong <code>utils/rules.toml</code>. Tệp TOML cục bộ đó cho phép chúng tôi bật, tắt hoặc tinh chỉnh các quy tắc một cách nhanh chóng mà không cần chờ một bản phát hành <code>transformers-mlinter</code> mới.</p> <blockquote class="tip"><p>Xem <a href="https://huggingface.github.io/transformers-mlinter/" rel="nofollow">tài liệu mlinter</a> để biết tham chiếu đầy đủ về trình kiểm tra và chi tiết sử dụng.</p></blockquote> <p>Đây là các quy ước mô hình dự kiến khi thêm hoặc thay đổi mã mô hình hóa. Chúng giúp giữ cho mã nguồn nhất quán và đảm bảo khả năng tương thích với các tính năng như pipeline parallelism, device maps và weight tying.</p> <!--[1--><h2 class="relative group"><a id="running-the-checker" 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="#running-the-checker"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Chạy trình kiểm tra<!----></span></h2><!--]--><!----> <p><code>make typing</code> chạy <code>mlinter</code> cùng với trình kiểm tra kiểu <code>ty</code> thông qua wrapper của repo, do đó nó sẽ nhận <code>utils/rules.toml</code>. Chạy trực tiếp cùng một wrapper đó bằng các lệnh sau.</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 "><!---->python utils/check_modeling_structure.py <span class="hljs-comment"># check all modeling files</span> | |
| python utils/check_modeling_structure.py --changed-only <span class="hljs-comment"># check only files changed vs origin/main</span> | |
| python utils/check_modeling_structure.py --list-rules <span class="hljs-comment"># list all rules and their enabled status</span> | |
| python utils/check_modeling_structure.py --rule TRF001 <span class="hljs-comment"># show built-in docs for a specific rule</span><!----></pre></div><!----> <p>Cờ <code>--changed-only</code> là tùy chọn nhanh nhất trong quá trình phát triển. Nó chỉ kiểm tra các tệp bạn đã sửa đổi so với nhánh chính. Nếu bạn gọi trực tiếp <code>mlinter</code> thay vì wrapper, hãy truyền <code>--rules-toml utils/rules.toml</code> để áp dụng các ghi đè cục bộ.</p> <!--[1--><h2 class="relative group"><a id="fixing-a-violation" 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="#fixing-a-violation"><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><!---->Sửa một vi phạm<!----></span></h2><!--]--><!----> <p>Khi phát hiện vi phạm quy tắc, lỗi sẽ có dạng như sau:</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=" "><!---->src<span class="hljs-regexp">/transformers/m</span>odels<span class="hljs-regexp">/acme/m</span>odeling_acme.py:<span class="hljs-number">18</span>: TRF013: AcmeModel.__init__ does not <span class="hljs-keyword">call</span> self.post_init().<!----></pre></div><!----> <p>Sử dụng ID quy tắc để tra cứu cách khắc phục trong <a href="#rules-reference">tham chiếu quy tắc</a>. TRF013 được kích hoạt khi một lớp con của <a href="/docs/transformers/main/vi/main_classes/model#transformers.PreTrainedModel">PreTrainedModel</a> không gọi <code>self.post_init()</code>. Phương thức này thực hiện các bước hoàn tất thiết yếu, và việc bỏ qua nó sẽ gây ra lỗi thời gian chạy.</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-diff "><!----> class AcmeModel(AcmePreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.layers = nn.ModuleList( | |
| [AcmeDecoderLayer(config) for _ in range(config.num_hidden_layers)] | |
| ) | |
| <span class="hljs-addition">+ self.post_init()</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="rules-reference" 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="#rules-reference"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Tham khảo các quy tắc<!----></span></h2><!--]--><!----> <p>Mỗi quy tắc bên dưới liệt kê những gì nó thực thi và một diff hiển thị cách sửa. Chạy <code>python utils/check_modeling_structure.py --rule TRF001</code> để xem tài liệu tích hợp sẵn cho bất kỳ quy tắc nào với bộ quy tắc hiện tại của repo.</p> <!--[2--><h3 class="relative group"><a id="trf002" 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="#trf002"><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><!---->TRF002<!----></span></h3><!--]--><!----> <p>Kiểm tra xem base_model_prefix có phải là một chuỗi literal không rỗng và không chứa khoảng trắng hay không. Các tiền tố không hợp lệ có thể làm hỏng ánh xạ khóa khi nạp trọng số và việc truy cập mô hình cơ sở.</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-diff "><!---->class AcmePreTrainedModel(PreTrainedModel): | |
| <span class="hljs-deletion">- base_model_prefix = ""</span> | |
| <span class="hljs-addition">+ base_model_prefix = "model"</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf003" 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="#trf003"><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><!---->TRF003<!----></span></h3><!--]--><!----> <p>Đánh dấu mẫu <code>if not return_dict: return (x,)</code> cũ trong forward. Việc phân nhánh return_dict thủ công là dài dòng và dễ gây sai sót. Hãy để @capture_output hoặc @can_return_tuple xử lý.</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-diff "><!----><span class="hljs-deletion">-def forward(self, x, return_dict=None):</span> | |
| <span class="hljs-deletion">- if not return_dict:</span> | |
| <span class="hljs-deletion">- return (x,)</span> | |
| <span class="hljs-deletion">- return AcmeModelOutput(last_hidden_state=x)</span> | |
| <span class="hljs-addition">+@can_return_tuple</span> | |
| <span class="hljs-addition">+def forward(self, x):</span> | |
| <span class="hljs-addition">+ return AcmeModelOutput(last_hidden_state=x)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf004" 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="#trf004"><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><!---->TRF004<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng không có lớp model nào định nghĩa phương thức tie_weights. Ghi đè tie_weights sẽ làm hỏng việc tải, tính toán device_map và lưu trữ. Khai báo các trọng số liên kết trong thuộc tính lớp _tied_weights_keys.</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-diff "><!----><span class="hljs-deletion">-def tie_weights(self):</span> | |
| <span class="hljs-deletion">- self.lm_head.weight = self.emb.weight</span> | |
| <span class="hljs-addition">+class AcmeForCausalLM(AcmePreTrainedModel):</span> | |
| <span class="hljs-addition">+ _tied_weights_keys = ["lm_head.weight"]</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf005" 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="#trf005"><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><!---->TRF005<!----></span></h3><!--]--><!----> <p>Kiểm tra hình dạng của _no_split_modules khi có mặt. Các giá trị không đúng định dạng có thể làm hỏng việc phân vùng và chia nhỏ thiết bị.</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-diff "><!----><span class="hljs-deletion">-_no_split_modules = [SomeLayerClass, ""]</span> | |
| <span class="hljs-addition">+_no_split_modules = ["AcmeDecoderLayer", "AcmeAttention"]</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf006" 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="#trf006"><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><!---->TRF006<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng các đối số cache trong chữ ký forward được sử dụng trong thân hàm. Một đối số cache không được sử dụng cho thấy hỗ trợ cache chưa hoàn chỉnh và hành vi API không nhất quá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-diff "><!---->def forward(self, x, cache=None, use_cache=False): | |
| <span class="hljs-addition">+ if use_cache:</span> | |
| <span class="hljs-addition">+ ...</span> | |
| return x<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf007" 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="#trf007"><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><!---->TRF007<!----></span></h3><!--]--><!----> <p>Kiểm tra các gán thuộc tính cho self sau self.post_init() trong <strong>init</strong>. Việc thay đổi cấu trúc mô hình sau post_init sẽ bỏ qua các công việc khởi tạo và hoàn tất của 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-diff "><!---->def __init__(self, config): | |
| ... | |
| <span class="hljs-deletion">- self.post_init()</span> | |
| <span class="hljs-deletion">- self.proj = nn.Linear(...)</span> | |
| <span class="hljs-addition">+ self.proj = nn.Linear(...)</span> | |
| <span class="hljs-addition">+ self.post_init()</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf008" 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="#trf008"><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><!---->TRF008<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng add_start_docstrings trên một lớp model nhận được một đối số không rỗng. Một đối số rỗng sẽ tạo ra tài liệu API được sinh ra không rõ ràng và chất lượng thấp.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-diff "><!----><span class="hljs-deletion">-@add_start_docstrings("")</span> | |
| <span class="hljs-addition">+@add_start_docstrings("The Acme model.")</span> | |
| class AcmeModel(AcmePreTrainedModel): | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf009" 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="#trf009"><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><!---->TRF009<!----></span></h3><!--]--><!----> <p>Flags imports into another model’s package from a model’s shipped implementation files: modeling<em>*, configuration</em><em>, processing_</em>, image<em>processing</em><em>, video<em>processing</em></em>, feature<em>extraction</em><em>, tokenization_</em> and generation<em>*.py. Three forms count: package path (<code>from transformers.models.other.modeling_other import X</code>), relative path (<code>from ..other.configuration_other import X</code>) and public API (<code>from transformers import OtherModel</code>) — for the last the owning directory is inferred from the class name and confirmed against the classes it defines, so an unresolvable name is left alone. Out of scope: `modular</em><em>.py<code>(building on another model is its purpose, and the converter flattens those imports away),</code>convert_</em>.py<code>,</code><strong>init</strong>.py<code>and files under</code>auto<code>. Allowed targets: the model's own directory,</code>auto<code>and</code>timm_wrapper`. One model, one definition: model A’s behavior lives in its own files, and a change to model B must not silently change model A — for every file kind, not just modeling. Reach sub-configs through AutoConfig and CONFIG_MAPPING, and use a modular file to build on another model’s 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-diff "><!----><span class="hljs-deletion">-from transformers.models.llama.modeling_llama import LlamaAttention</span> | |
| <span class="hljs-deletion">-from transformers import CLIPTextModelWithProjection</span> | |
| <span class="hljs-addition">+# Keep implementation local to this model's own files.</span> | |
| <span class="hljs-addition">+# To build on another model, write modular_acme.py; to reuse a snippet,</span> | |
| <span class="hljs-addition">+# copy it with a # Copied from comment.</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf010" 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="#trf010"><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><!---->TRF010<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng các lớp con trực tiếp của PreTrainedConfig/PretrainedConfig trong configuration<em>*.py và modular</em>*.py có @strict(accept_kwargs=True). Nếu thiếu, một config mới sẽ bỏ qua thỏa thuận kiểm tra kiểu dữ liệu thời gian chạy của repo và lệch khỏi tiêu chuẩn config dựa trên dataclass.</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-diff "><!----><span class="hljs-addition">+@strict(accept_kwargs=True)</span> | |
| class AcmeConfig(PreTrainedConfig): | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf011" 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="#trf011"><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><!---->TRF011<!----></span></h3><!--]--><!----> <p>Trong forward() của các lớp con của PreTrainedModel, các truy cập thuộc tính của submodule mà torch.nn.Identity không có: trên các biến vòng lặp của self.layers, và self.<submodule>.<attr> trong đó <attr> không phải là thuộc tính chuẩn của nn.Module. Song song hóa pipeline có thể thay thế bất kỳ submodule nào bằng torch.nn.Identity, do đó việc đọc một thuộc tính tùy chỉnh (ví dụ: decoder_layer.attention_type) từ nó sẽ gây ra AttributeError khi chạy. Hãy đọc metadata cho từng lớp từ self.config.</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-diff "><!---->def forward(self, ...): | |
| <span class="hljs-deletion">- for decoder_layer in self.layers:</span> | |
| <span class="hljs-addition">+ for i, decoder_layer in enumerate(self.layers):</span> | |
| hidden_states = decoder_layer( | |
| hidden_states, | |
| <span class="hljs-deletion">- attention_mask=causal_mask_mapping[decoder_layer.attention_type],</span> | |
| <span class="hljs-addition">+ attention_mask=causal_mask_mapping[self.config.layer_types[i]],</span> | |
| )<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf012" 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="#trf012"><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><!---->TRF012<!----></span></h3><!--]--><!----> <p>Các thao tác in-place (ví dụ: <em>.normal(), .zero()</em>, …) trên trọng số của module bên trong _init_weights. Các tham số mang theo các cờ nội bộ để theo dõi xem chúng có cần khởi tạo lại hay không; các thao tác in-place sẽ bỏ qua các cờ này. Hãy sử dụng các nguyên thủy <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-diff "><!----><span class="hljs-addition">+from transformers import initialization as init</span> | |
| <span class="hljs-addition">+</span> | |
| def _init_weights(self, module): | |
| <span class="hljs-deletion">- module.weight.normal_(mean=0.0, std=0.02)</span> | |
| <span class="hljs-addition">+ init.normal_(module.weight, mean=0.0, std=0.02)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf013" 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="#trf013"><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><!---->TRF013<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng mọi lớp con của PreTrainedModel có định nghĩa <strong>init</strong> đều gọi self.post_init(). Trong các file modular, việc gọi super().<strong>init</strong>() được tính, vì nó truyền tiếp post_init của lớp cha. post_init thực hiện các bước hoàn thiện quan trọng (khởi tạo trọng số, thiết lập gradient checkpointing, …); bỏ qua nó sẽ gây ra các lỗi runtime tinh vi.</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-diff "><!---->class AcmeModel(AcmePreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.layers = nn.ModuleList(...) | |
| <span class="hljs-addition">+ self.post_init()</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf014" 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="#trf014"><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><!---->TRF014<!----></span></h3><!--]--><!----> <p>Các cờ <code>trust_remote_code</code> được sử dụng hoặc truyền vào (ví dụ dưới dạng kwarg) trong các tệp tích hợp model native. <code>trust_remote_code</code> tải mã tùy ý, bao gồm cả binary — đây là tính năng mạnh mẽ dành cho người dùng, không phải thứ mà một tích hợp native có thể phụ thuộc, vì mã từ xa không thể được xem xét hoặc bảo trì trong transformers.</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-diff "><!---->class AcmeModel(AcmePreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| <span class="hljs-deletion">- self.model = AutoModel.from_pretrained(..., trust_remote_code=True)</span> | |
| <span class="hljs-addition">+ self.model = AutoModel.from_pretrained(...)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf015" 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="#trf015"><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><!---->TRF015<!----></span></h3><!--]--><!----> <p>Khi một lớp con của PreTrainedModel đặt _tied_weights_keys khác rỗng, hệ thống sẽ kiểm tra trường tie_word_embeddings trong tệp cấu hình đồng hành. Nếu không có trường này, người dùng không thể kiểm soát việc ràng buộc trọng số: mô hình sẽ ràng buộc một cách vô điều kiện, làm hỏng các vòng lặp tuần tự hóa và quá trình tinh chỉnh với các đầu không bị ràng buộ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-diff "><!----># configuration_foo.py | |
| @strict(accept_kwargs=True) | |
| class FooConfig(PreTrainedConfig): | |
| hidden_size: int = 768 | |
| <span class="hljs-addition">+ tie_word_embeddings: bool = True</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf016" 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="#trf016"><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><!---->TRF016<!----></span></h3><!--]--><!----> <p>Khi một lớp image<em>processing</em><em>.py hoặc video<em>processing</em></em>.py khai báo các thuộc tính boolean do_* (do_resize, do_rescale, do_normalize, do_convert_rgb, …) và ghi đè preprocess() hoặc _preprocess(), hãy kiểm tra xem mỗi cờ có còn được sử dụng ở đó không: được tham chiếu trực tiếp, được ủy quyền qua super().preprocess/_preprocess(…, **kwargs), hoặc — chỉ với image processor — được chuyển tiếp qua _preprocess_image_like_inputs/_prepare_image_like_inputs. do_sample_frames được miễn trừ: preprocess() cơ bản sẽ sử dụng nó trước khi _preprocess() chạy. Một do_X mà bản ghi đè không bao giờ tham chiếu là vô dụng: việc đặt do_X=False sẽ không có tác dụng, thao tác vẫn được thực hiện, và các ghi đè theo từng lần gọi sẽ bị hỏng một cách im lặ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-diff "><!---->class AcmeVideoProcessor(BaseVideoProcessor): | |
| do_resize = True | |
| do_normalize = True | |
| def _preprocess( | |
| self, | |
| videos, | |
| <span class="hljs-addition">+ do_resize: bool,</span> | |
| <span class="hljs-addition">+ do_normalize: bool,</span> | |
| size, | |
| image_mean, | |
| image_std, | |
| **kwargs, | |
| ): | |
| for video in videos: | |
| <span class="hljs-deletion">- video = self.resize(video, size=size)</span> | |
| <span class="hljs-deletion">- video = self.normalize(video, image_mean, image_std)</span> | |
| <span class="hljs-addition">+ if do_resize:</span> | |
| <span class="hljs-addition">+ video = self.resize(video, size=size)</span> | |
| <span class="hljs-addition">+ if do_normalize:</span> | |
| <span class="hljs-addition">+ video = self.normalize(video, image_mean, image_std)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf017" 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="#trf017"><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><!---->TRF017<!----></span></h3><!--]--><!----> <p>Với các class mang cả @auto_docstring và @dataclass, @auto_docstring được kiểm tra trước. Các decorator được áp dụng từ dưới lên, nên @dataclass ở trên sẽ chạy @auto_docstring trước, trên một class chưa có <strong>init</strong> được tổng hợp: khi đó nó sẽ sửa <strong>init</strong> của class cha thay vì <strong>doc</strong> của class con.</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-diff "><!----><span class="hljs-deletion">-@dataclass</span> | |
| @auto_docstring( | |
| custom_intro=""" | |
| Output type of `AcmeForPreTraining`. | |
| """ | |
| ) | |
| <span class="hljs-addition">+@dataclass</span> | |
| class AcmeForPreTrainingOutput(ModelOutput): | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf018" 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="#trf018"><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><!---->TRF018<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng mọi lớp con của PreTrainedModel ghi đè <code>_init_weights(self, module, ...)</code> đều gọi lên qua <code>super()._init_weights(...)</code>. Các file modular cũng chấp nhận các sentinel <code>PreTrainedModel._init_weights(self, module)</code>, <code>PreTrainedModel._init_weights(module)</code> và <code>raise AttributeError(...)</code>. Đối với việc ghi đè hoàn toàn có chủ đích, hãy tắt cảnh báo bằng cách đặt <code># trf-ignore: TRF018</code> phía trên phương thức. Phương thức cơ sở <code>_init_weights</code> bao phủ các loại module chuẩn (Linear, Embedding, LayerNorm, RotaryEmbedding, …). Việc bỏ qua nó sẽ khiến mọi submodule mà bản ghi đè không xử lý được ở trạng thái chưa khởi tạo — điều này vẫn vượt qua các bài kiểm tra và chỉ bộc lộ về sau như một lỗi khởi tạo trọng số tinh vi (xem <a href="https://github.com/huggingface/transformers/pull/45597" rel="nofollow">https://github.com/huggingface/transformers/pull/45597</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-diff "><!---->from ... import initialization as init | |
| def _init_weights(self, module): | |
| <span class="hljs-addition">+ super()._init_weights(module)</span> | |
| if isinstance(module, AcmeCustomLayer): | |
| <span class="hljs-deletion">- module.gate.data.zero_()</span> | |
| <span class="hljs-addition">+ init.zeros_(module.gate)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf019" 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="#trf019"><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><!---->TRF019<!----></span></h3><!--]--><!----> <p>Đánh dấu các <code>_defaults</code> không rỗng trên các TypedDict <code>*ProcessorKwargs</code> trong <code>processing_*.py</code>. Các mô hình được phát hành trước ngày cắt sẽ được miễn. Việc hardcode <code>_defaults</code> khiến cấu hình processor bị phân tán trong mã nguồn Python, gây khó khăn khi ghi đè từ config và làm phình to mã nguồn. Trong <code>processor_config.json</code> trên hub, chúng đi kèm với checkpoint và có thể được cập nhật mà không cần thay đổi mã.</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-diff "><!---->class Gemma4ProcessorKwargs(ProcessingKwargs, total=False): | |
| <span class="hljs-deletion">- _defaults = {</span> | |
| <span class="hljs-deletion">- "text_kwargs": {"padding": False},</span> | |
| <span class="hljs-deletion">- "images_kwargs": {"return_tensors": "pt"},</span> | |
| <span class="hljs-deletion">- }</span> | |
| images_kwargs: Gemma4ImageProcessorKwargs<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf020" 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="#trf020"><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><!---->TRF020<!----></span></h3><!--]--><!----> <p>Trong các thư mục mô hình có cấu hình khai báo <code>kv_lora_rank</code> (Multi-head Latent Attention), kiểm tra lớp attention sở hữu phép chiếu mở rộng KV LoRA (<code>kv_b_proj</code>, hoặc bất kỳ <code>nn.Linear(config.kv_lora_rank, ...)</code> nào): phép mở rộng này phải nằm trong một phương thức chuyên dụng (ví dụ <code>expand_kv</code>) được <code>forward()</code> gọi, chứ không được viết inline. Trong các file modular, một phương thức kế thừa từ lớp cơ sở được import được tính là hợp lệ. Các backend bên ngoài (vLLM/SGLang) ghi đè phép mở rộng để sử dụng trực tiếp bộ nhớ đệm KV đã nén. Nếu viết inline trong <code>forward()</code>, sẽ không có gì để ghi đè, do đó backend phải vật chất hóa toàn bộ bộ nhớ đệm — làm mất đi lợi ích tiết kiệm bộ nhớ mà MLA tồn tại để phục vụ.</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-diff "><!----><span class="hljs-addition">+ def expand_kv(self, k_nope, k_pe):</span> | |
| <span class="hljs-addition">+ key_shape = (*k_nope.shape[:-1], -1, self.qk_nope_head_dim + self.v_head_dim)</span> | |
| <span class="hljs-addition">+ k_nope = self.kv_b_proj(k_nope).view(key_shape).transpose(1, 2)</span> | |
| <span class="hljs-addition">+ k_nope, value_states = torch.split(k_nope, [self.qk_nope_head_dim, self.v_head_dim], dim=-1)</span> | |
| <span class="hljs-addition">+ k_pe = k_pe.expand(*k_nope.shape[:-1], -1)</span> | |
| <span class="hljs-addition">+ key_states = torch.cat((k_nope, k_pe), dim=-1)</span> | |
| <span class="hljs-addition">+ return key_states, value_states</span> | |
| <span class="hljs-addition">+</span> | |
| def forward(self, hidden_states, ...): | |
| ... | |
| <span class="hljs-deletion">- k_nope = self.kv_b_proj(k_pass).view(key_shape).transpose(1, 2)</span> | |
| <span class="hljs-deletion">- k_nope, value_states = torch.split(k_nope, [self.qk_nope_head_dim, self.v_head_dim], dim=-1)</span> | |
| <span class="hljs-deletion">- k_pe = k_rot.expand(*k_nope.shape[:-1], -1)</span> | |
| <span class="hljs-deletion">- key_states = torch.cat((k_nope, k_pe), dim=-1)</span> | |
| <span class="hljs-addition">+ key_states, value_states = self.expand_kv(k_pass, k_rot)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf021" 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="#trf021"><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><!---->TRF021<!----></span></h3><!--]--><!----> <p>Trong <em>*.py và modular</em><em>.py, các cờ <code>torch.tensor(<value>, ..., device=<non-cpu>)</code> trong đó <code><value></code> được chứng minh là phân giải thành một scalar Python — từ các hằng số số và phép toán trên chúng, các trường torch.finfo/iinfo, các built-in trả về scalar và các calls của math.</em> các biến cục bộ được gán đúng một lần, self.<attr> được gán trong thân lớp, và các trường config được chú thích là int/float/bool trong tệp cấu hình đồng hành (theo attribute_map). Bất kỳ thứ gì có thể đồng thời là một sequence (<code>eos_token_id: int | list[int] | None</code>) hoặc không thể phân giải sẽ được giữ nguyên. <strong>init</strong>, _init_weights, <strong>post_init</strong> và post_init được miễn trừ: chúng không bao giờ chạy bên trong một vùng capture. torch.tensor(<python scalar>, device=<accelerator>) vật chất hóa giá trị trên host rồi sao chép nó sang thiết bị; việc capture đồ thị CUDA cấm thao tác sao chép đó, do đó mô hình không thể được capture. torch.full((), <value>, dtype=…, device=…) điền cùng một tensor 0-d trên thiết bị bằng một kernel có thể capture được và không có sự đồng bộ hó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-diff "><!---->def get_placeholder_mask(self, input_ids, inputs_embeds): | |
| special_image_mask = ( | |
| inputs_embeds | |
| == self.get_input_embeddings()( | |
| <span class="hljs-deletion">- torch.tensor(self.config.image_token_id, dtype=torch.long, device=inputs_embeds.device)</span> | |
| <span class="hljs-addition">+ torch.full((), self.config.image_token_id, dtype=torch.long, device=inputs_embeds.device)</span> | |
| ) | |
| ).all(-1)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf022" 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="#trf022"><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><!---->TRF022<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng mọi chuỗi trong danh sách <code>_no_split_modules</code> trong modeling<em>*.py hoặc modular</em>*.py đều đặt tên cho một lớp được định nghĩa trong tệp đó, được nhập vào tệp đó, hoặc được định nghĩa bởi một module anh em trong cùng thư mục model. Bổ sung cho TRF005, vốn chỉ kiểm tra hình dạng của giá trị. <code>device_map</code> khớp các chuỗi này với <code>module.__class__.__name__</code> khi chạy, nên một tên cũ hoặc sai chính tả sẽ không khớp với gì và bị bỏ qua một cách im lặng — module mà nó nên giữ nguyên vẹn vẫn có thể bị tách ra giữa các thiết bị. Xóa các mục đặt tên cho các lớp của model khác thay vì sửa chúng: <code>post_init</code> đã thu thập <code>_no_split_modules</code> từ các submodel con.</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-diff "><!---->class VideoLlavaPreTrainedModel(PreTrainedModel): | |
| <span class="hljs-deletion">- _no_split_modules = ["VideoLlavaVisionAttention"]</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf023" 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="#trf023"><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><!---->TRF023<!----></span></h3><!--]--><!----> <p>Trong cấu hình<em>*.py và modular</em><em>.py, các cờ `</em>Config<code>fields named after an upstream paper's abbreviation instead of the canonical name: d_model/n_embd -> hidden_size, d_ff/d_inner/ffn_dim/ffn_hidden_size/expansion_ratio -> intermediate_size, d_head -> head_dim, n_head/n_heads -> num_attention_heads, n_layer/n_layers/num_blocks -> num_hidden_layers. Fields are read from the class body and from __init__/__post_init__ assignments and defaults. Ambiguous but idiomatic names (num_heads, num_layers, embed_dim, mlp_ratio) are not flagged, and models added before cutoff_date keep theirs. Everything that reads a model's shape -- device_map planning, tensor/pipeline parallel plans, quantization, PEFT, attention-backend selection,</code>attribute_map<code>consumers -- looks up the canonical names, so a config spelling the same quantity</code>d_model` silently opts out of all of them. Map the checkpoint’s own spelling in the conversion script.</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-diff "><!---->@strict(accept_kwargs=True) | |
| class AcmeConfig(PreTrainedConfig): | |
| <span class="hljs-deletion">- d_model: int = 1024</span> | |
| <span class="hljs-deletion">- d_ff: int = 4096</span> | |
| <span class="hljs-deletion">- n_heads: int = 16</span> | |
| <span class="hljs-deletion">- n_layers: int = 24</span> | |
| <span class="hljs-addition">+ hidden_size: int = 1024</span> | |
| <span class="hljs-addition">+ intermediate_size: int = 4096</span> | |
| <span class="hljs-addition">+ num_attention_heads: int = 16</span> | |
| <span class="hljs-addition">+ num_hidden_layers: int = 24</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf024" 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="#trf024"><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><!---->TRF024<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em><em>.py, báo lỗi khi một hằng số nguyên lớn hơn 8 xuất hiện trong đối số kích thước của một bộ khởi tạo torch.nn (Linear, Embedding, LayerNorm, RMSNorm, GroupNorm, BatchNorm</em>, InstanceNorm<em>, Conv</em>d, ConvTranspose*d, Bilinear, MultiheadAttention), dù được truyền theo vị trí hay theo tên (in_features, out_features, in_channels, out_channels, num_embeddings, embedding_dim, embed_dim, normalized_shape, num_channels, hidden_size). Các đối số liên quan đến hình dạng toán tử (kernel_size, stride, padding, num_groups) bị bỏ qua; các hằng số không vượt quá 8 giúp giữ cho các đầu ra vô hướng, bộ phân loại nhị phân và số kênh RGB sạch sẽ. Các mô hình được thêm trước cutoff_date được miễn trừ. Việc cố định một chiều rộng bằng hằng số sẽ khóa module vào một kích thước checkpoint duy nhất: cùng một kiến trúc ở một quy mô khác sẽ bị lỗi lệch hình dạng khi nạp, và <code>from_pretrained</code> không thể chỉ ra giá trị nào là sai vì không có trường cấu hình nào trỏ đến nó. Điều này cũng làm chia rẽ nguồn sự thật, khiến việc chỉnh sửa cấu hình không còn thay đổi được mô hình được xây 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-diff "><!---->class AcmeAtomEmbedding(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| <span class="hljs-deletion">- self.proj = nn.Linear(768, 3072, bias=False)</span> | |
| <span class="hljs-deletion">- self.norm = nn.LayerNorm(3072)</span> | |
| <span class="hljs-addition">+ self.proj = nn.Linear(config.hidden_size, config.intermediate_size, bias=False)</span> | |
| <span class="hljs-addition">+ self.norm = nn.LayerNorm(config.intermediate_size)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf025" 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="#trf025"><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><!---->TRF025<!----></span></h3><!--]--><!----> <p>Trong <em>*.py và modular</em><em>.py, các cờ gọi đến nhà máy mask (các điểm vào tiện ích mask <em>create_causal_mask, create_bidirectional_mask, create_sliding_window_causal_mask, create_chunked_causal_mask, create_masks_for_generate, và bất kỳ hàm trợ giúp create</em></em>_mask nào) bên trong một lớp không kế thừa từ PreTrainedModel — các khối nn.Module thuần túy như các lớp, module chú ý và bộ mã hóa. Việc xây dựng mask là công việc có độ phức tạp O(bình phương độ dài chuỗi) và không thay đổi theo từng lớp, do đó việc xây dựng nó trong lớp sẽ trả chi phí đó một lần cho mỗi lớp, và mỗi lớp sau đó sẽ sở hữu mask riêng của mình, khiến các backend chú ý không còn có thể nhận được một mask đã chuẩn bị duy nhất. Hãy xây dựng nó một lần trong model và truyền nó xuố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-diff "><!---->class AcmeLayer(nn.Module): | |
| def forward(self, hidden_states, attention_mask=None, **kwargs): | |
| <span class="hljs-deletion">- attention_mask = create_causal_mask(</span> | |
| <span class="hljs-deletion">- config=self.config, input_embeds=hidden_states, attention_mask=attention_mask, ...</span> | |
| <span class="hljs-deletion">- )</span> | |
| return self.self_attn(hidden_states, attention_mask, **kwargs) | |
| class AcmeModel(AcmePreTrainedModel): | |
| def forward(self, input_ids=None, attention_mask=None, **kwargs): | |
| <span class="hljs-addition">+ causal_mask = create_causal_mask(</span> | |
| <span class="hljs-addition">+ config=self.config, input_embeds=inputs_embeds, attention_mask=attention_mask, ...</span> | |
| <span class="hljs-addition">+ )</span> | |
| for layer in self.layers: | |
| <span class="hljs-deletion">- hidden_states = layer(hidden_states, attention_mask, **kwargs)</span> | |
| <span class="hljs-addition">+ hidden_states = layer(hidden_states, causal_mask, **kwargs)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf026" 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="#trf026"><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><!---->TRF026<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, một lớp không phải là PreTrainedModel được xác định chỉ có <strong>init</strong> và forward, gán chính xác một self.<attr> trong <strong>init</strong>, và thân forward chính xác là <code>return self.<attr>(...)</code> cho nó (docstring ở đầu bị bỏ qua). Bất kỳ phương thức nào khác, thuộc tính bổ sung, câu lệnh trước return, hoặc lời gọi <code>super()</code> trong forward đều có nghĩa là lớp đó thực hiện công việc riêng của nó. Lớp bao (wrapper) thêm một cấp độ vào mọi tên trọng số, vào _no_split_modules, các kế hoạch song song hóa và mọi ánh xạ chuyển đổi, trong khi không tính toán gì — và người đọc phải mở thêm một lớp nữa để tìm ra điều đó. Các lớp con của PreTrainedModel được miễn trừ: chúng tồn tại cho from_pretrained và các lớp auto ngay cả khi forward chỉ ủy quyề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-diff "><!----><span class="hljs-deletion">-class AcmeAtomTransformer(nn.Module):</span> | |
| <span class="hljs-deletion">- def __init__(self, config):</span> | |
| <span class="hljs-deletion">- super().__init__()</span> | |
| <span class="hljs-deletion">- self.encoder = AcmeEncoder(config)</span> | |
| <span class="hljs-deletion">-</span> | |
| <span class="hljs-deletion">- def forward(self, hidden_states, **kwargs):</span> | |
| <span class="hljs-deletion">- return self.encoder(hidden_states, **kwargs)</span> | |
| <span class="hljs-deletion">-</span> | |
| class AcmeModel(AcmePreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| <span class="hljs-deletion">- self.atom_transformer = AcmeAtomTransformer(config)</span> | |
| <span class="hljs-addition">+ self.encoder = AcmeEncoder(config)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf027" 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="#trf027"><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><!---->TRF027<!----></span></h3><!--]--><!----> <p>Flags any <code>assert</code> statement in modeling<em>*.py, modular</em><em>.py and configuration_</em>.py. <code>python -O</code> strips asserts, so a shape or config check written as one silently disappears in optimised runs. An assert also gives a bare AssertionError, where a ValueError can name the offending value and say what to do about it.</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-diff "><!---->def forward(self, hidden_states): | |
| <span class="hljs-deletion">- assert hidden_states.dim() == 3</span> | |
| <span class="hljs-addition">+ if hidden_states.dim() != 3:</span> | |
| <span class="hljs-addition">+ raise ValueError(f"Expected a 3D tensor, got shape {tuple(hidden_states.shape)}.")</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf028" 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="#trf028"><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><!---->TRF028<!----></span></h3><!--]--><!----> <p>Kiểm tra 25 dòng đầu tiên của modeling<em>*.py, modular</em><em>.py, configuration_</em>.py, processing<em>*.py, image_processing</em><em>.py và video<em>processing</em></em>.py để tìm dòng <code>Licensed under the <name> License</code> được theo sau bởi mọi điều khoản của đoạn văn bản bảo hành tiêu chuẩn, từ <code>You may obtain a copy of the License at</code> đến <code>limitations under the License.</code>. Các dòng được làm phẳng và chuyển thành chữ thường trước, nên việc xuống dòng và phong cách chú thích không quan trọng. Tên giấy phép và dòng bản quyền không được kiểm tra: chúng thay đổi theo từng model. Một file được phát hành mà thiếu phần header sẽ khiến nguồn gốc của nó trở nên mơ hồ, và việc bổ sung sau đó đồng nghĩa với việc phải chỉnh sửa một file đã được phát hành. Việc chỉ khớp với <code>Apache License</code> sẽ bỏ qua các lỗi thực sự xảy ra: một đoạn văn bị cắt cụt giữa chừng, một header dừng lại sau URL, hoặc một header bị hỏng do thao tác tìm-đổi sai.</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-diff "><!----><span class="hljs-addition">+# Copyright 2026 The HuggingFace Team. All rights reserved.</span> | |
| <span class="hljs-addition">+#</span> | |
| <span class="hljs-addition">+# Licensed under the Apache License, Version 2.0 (the "License");</span> | |
| <span class="hljs-addition">+# ...</span> | |
| """PyTorch Acme model."""<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf029" 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="#trf029"><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><!---->TRF029<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, một cờ báo hiệu <code>__init__</code> nhận <code>config</code> cùng với một tham số có tên rõ ràng là trường của config (hidden_size, num_attention_heads, intermediate_size, head_dim, num_hidden_layers, embed_dim, dropout, eps, patch_size, rope_theta, …). Một tham số tùy chọn có giá trị mặc định là <code>None</code> được miễn trừ: đó là một sự ghi đè, không phải nguồn sự thật thứ hai, và đây là cách một lớp MLP phục vụ cả chiều rộng dense lẫn chiều rộng expert của một mô hình MoE. Một giá trị mặc định được hardcode như <code>hidden_size: int = 1024</code> thì không được — nó sẽ thắng config bất cứ khi nào người gọi không truyền gì. kosmos2 được đưa vào danh sách cho phép: trang tài liệu của nó không thể suy ra từ tên thư mục, nên điểm cắt không thể bảo trợ nó. Cùng một con số giờ đây có hai nguồn sự thật và người gọi chọn người thắng, nên việc chỉnh sửa config không còn thay đổi mô hình được xây dựng nữa. Nó cũng đẩy kiến thức về kiến trúc ra mọi vị trí gọi, nơi nó không nên có mặ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-diff "><!---->class AcmeAttention(nn.Module): | |
| <span class="hljs-deletion">- def __init__(self, config, embed_dim, num_heads, dropout):</span> | |
| <span class="hljs-addition">+ def __init__(self, config, layer_idx=None):</span> | |
| super().__init__() | |
| <span class="hljs-deletion">- self.embed_dim = embed_dim</span> | |
| <span class="hljs-deletion">- self.num_heads = num_heads</span> | |
| <span class="hljs-addition">+ self.embed_dim = config.hidden_size</span> | |
| <span class="hljs-addition">+ self.num_heads = config.num_attention_heads</span> | |
| class AcmeMLP(nn.Module): | |
| # an optional override is fine: omitting it reads the config | |
| def __init__(self, config, intermediate_size=None): | |
| super().__init__() | |
| self.intermediate_size = intermediate_size or config.intermediate_size<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf030" 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="#trf030"><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><!---->TRF030<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các chuỗi thuộc tính bắt nguồn từ <code>config</code> hoặc <code>self.config</code> có độ sâu từ ba cấp trở lên. <code>config.hidden_size</code> (một bước) và <code>config.text_config.hidden_size</code> (hai bước, truy cập sub-config thông thường) là hợp lệ. Mỗi dòng chỉ tính một vi phạm. Một module duyệt qua <code>config.diffusion_config.atom_encoder_config.hidden_size</code> sẽ bị ràng buộc với toàn bộ hệ thống phân cấp config thay vì chỉ phần riêng của nó, do đó không thể tái sử dụng, kiểm thử hoặc gán một sub-config khác. Hãy truyền sub-config liên quan xuống và chuỗi sẽ rút gọn về một bướ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-diff "><!---->class AcmeAtomEncoder(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| <span class="hljs-deletion">- self.norm = AcmeLayerNorm(config.diffusion_config.atom_encoder_config.hidden_size)</span> | |
| <span class="hljs-addition">+ self.norm = AcmeLayerNorm(config.hidden_size)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf031" 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="#trf031"><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><!---->TRF031<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, một <code>@dataclass</code> cấp cao nhất mà các lớp cơ sở của nó không có lớp nào kết thúc bằng <code>Output</code> sẽ bị đánh dấu, trừ khi nó có hai hoặc nhiều trường bắt buộc — đó là các nhóm tham số nội bộ, mà <code>ModelOutput</code> sẽ từ chối lúc chạy. Một dataclass đầu ra thông thường không thể chỉ mục như tuple, không tồn tại khi <code>return_dict=False</code>, và không hiển thị với @auto_docstring, do đó các trường của nó không bao giờ xuất hiện trong tài liệu API được tạo. ModelOutput có sẵn cả ba tính năng này.</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-diff "><!---->@auto_docstring | |
| @dataclass | |
| <span class="hljs-deletion">-class AcmeStructureOutput:</span> | |
| <span class="hljs-addition">+class AcmeStructureOutput(ModelOutput):</span> | |
| positions: torch.Tensor | |
| confidence: Optional[torch.Tensor] = None<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf032" 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="#trf032"><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><!---->TRF032<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các cờ masked<em>fill, masked_fill</em>, full, full_like và new_full được gọi với một hằng số số học bị phủ định có độ lớn từ 1e3 trở lên. Một giá trị -1e9 được hardcode sẽ tràn thành -inf trong float16 và không hề gần giá trị nhỏ nhất trong float32, do đó cùng một mask sẽ hoạt động khác nhau tùy theo dtype và có thể tạo ra NaN sau softmax. <code>torch.finfo(dtype).min</code> là giá trị nhỏ nhất có thể biểu diễn được trong dtype đang thực sự chạy.</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-diff "><!----><span class="hljs-deletion">-attention_scores = attention_scores.masked_fill(~mask, -1e9)</span> | |
| <span class="hljs-addition">+attention_scores = attention_scores.masked_fill(~mask, torch.finfo(attention_scores.dtype).min)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf033" 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="#trf033"><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><!---->TRF033<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các phương thức có tên bắt đầu bằng <code>set_</code>, ngoại trừ các phương thức theo hợp đồng của PreTrainedModel là set_input_embeddings, set_output_embeddings, set_decoder, set_encoder, set_attn_implementation và set_default_language. Một setter khiến hành vi phụ thuộc vào thứ tự gọi: giá trị không nằm trong config, do đó không được lưu, không được khôi phục bởi from_pretrained, và không hiển thị cho device-map hay kế hoạch song song hóa. Người dùng phải biết cách gọi nó, và việc quên gọi sẽ diễn ra một cách im lặ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-diff "><!---->class AcmeTriangleAttention(nn.Module): | |
| <span class="hljs-deletion">- def set_chunk_size(self, chunk_size):</span> | |
| <span class="hljs-deletion">- self.chunk_size = chunk_size</span> | |
| <span class="hljs-addition">+ def __init__(self, config):</span> | |
| <span class="hljs-addition">+ super().__init__()</span> | |
| <span class="hljs-addition">+ self.chunk_size = config.chunk_size</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf034" 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="#trf034"><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><!---->TRF034<!----></span></h3><!--]--><!----> <p>In modeling<em>*.py and modular</em><em>.py, flags a locally-defined `</em>Layer<code>/</code><em>Block<code>class instantiated in an</code>nn.ModuleList(…)<code>that does not reach</code>GradientCheckpointingLayer<code>through its base chain; modular files follow relative imports into sibling models, and unresolved chains are inconclusive. Out of scope: a model that never sets</code>supports_gradient_checkpointing = True<code>, which raises from</code>gradient_checkpointing_enable()<code>instead of skipping a layer; a layer holding</code>nn.BatchNorm</em><code>/</code>nn.InstanceNorm*<code>, whose statistics would be recomputed twice; and a stack that is not the model's token mixer, shown by an attention, modulation, mixer or SSM module assigned as</code>self.x = Y(…)<code>.</code>gradient_checkpointing_enable()` wraps a layer only if it is a GradientCheckpointingLayer. A plain nn.Module on the trunk is skipped silently, so training looks checkpointed while still allocating full activations, and the OOM surfaces far from the cause. Elsewhere — a conv backbone, a decode head — the trade is the author’s call, so the rule stays out.</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-diff "><!----><span class="hljs-deletion">-class AcmeDecoderLayer(nn.Module):</span> | |
| <span class="hljs-addition">+class AcmeDecoderLayer(GradientCheckpointingLayer):</span> | |
| def __init__(self, config, layer_idx): | |
| super().__init__()<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf035" 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="#trf035"><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><!---->TRF035<!----></span></h3><!--]--><!----> <p>Cờ <code># noqa</code> các chú thích, có hoặc không có mã, trong modeling<em>*.py, modular</em><em>.py và configuration_</em>.py. Trong một tệp modular, <code>F401</code>, <code>F821</code> và <code>F822</code> được chấp nhận: đây là nguồn sinh ra mà cố ý không định nghĩa mọi tên mà nó sử dụng, nên nhóm lỗi tên chưa xác định của ruff sẽ kích hoạt trên mã đúng — các mục <code>__all__</code> mà trình chuyển đổi điền vào, các lớp nằm trong mô hình cha, các import được giữ lại để xuất lại. Một <code># noqa</code> chỉ nêu tên những mã đó sẽ bị bỏ qua; một <code># noqa</code> nêu tên bất kỳ mã nào khác sẽ được báo cáo trên các mã còn lại. Một <code># noqa</code> trần luôn được báo cáo; trong tệp modular, thông báo sẽ yêu cầu mã cụ thể thay vì viết lại. Các tệp mô hình là mã thông thường phải tuân theo các quy tắc lint của repo, nên một lệnh tắt nghĩa là vấn đề gốc vẫn còn đó — và một <code># noqa</code> trần cũng che giấu mọi vi phạm tương lai trên dòng đó. Hãy sửa mã thay vì tắt.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-diff "><!----><span class="hljs-deletion">-from ...modeling_utils import PreTrainedModel # noqa: F401</span> | |
| <span class="hljs-addition">+from ...modeling_utils import PreTrainedModel</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf036" 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="#trf036"><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><!---->TRF036<!----></span></h3><!--]--><!----> <p>Trong <em>*.py và modular</em>*.py, mọi cấu trúc <code>nn.Sequential(...)</code> đều được đánh dấu. Sequential đặt tên cho các phần tử con theo vị trí, nên trọng số sẽ nằm tại <code>mlp.0.weight</code> và <code>mlp.2.weight</code>: do đó, ánh xạ chuyển đổi, _tied_weights_keys và mọi kế hoạch song song hóa đều tham chiếu đến các chỉ số, và việc chèn một lớp sẽ đổi tên mọi thứ phía sau nó. Nó cũng che giấu hàm forward, khiến các phép chuyển dtype và phần dư giữa các bước không thể nhìn thấy được tại nơi chúng diễn ra.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-diff "><!----><span class="hljs-deletion">- self.mlp = nn.Sequential(</span> | |
| <span class="hljs-deletion">- nn.Linear(config.hidden_size, config.intermediate_size),</span> | |
| <span class="hljs-deletion">- nn.GELU(),</span> | |
| <span class="hljs-deletion">- nn.Linear(config.intermediate_size, config.hidden_size),</span> | |
| <span class="hljs-deletion">- )</span> | |
| <span class="hljs-addition">+ self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)</span> | |
| <span class="hljs-addition">+ self.act = ACT2FN[config.hidden_act]</span> | |
| <span class="hljs-addition">+ self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf037" 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="#trf037"><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><!---->TRF037<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các lời gọi einsum được đánh dấu, đồng thời báo cáo phương trình nếu đó là một chuỗi văn bản cố định. Tính năng này bị tắt theo mặc định: einsum đôi khi là cách rõ ràng nhất để biểu diễn một phép co (contraction), nên đây là tùy chọn bật thay vì một quy ước bắt buộc. Một phương trình einsum mã hóa các hình dạng (shape) bằng một ký hiệu mà người đọc phải giải mã, và một phương trình được xây dựng động che giấu phép co nào thực sự được thực thi. Hãy mở rộng nó thành các thao tác matmul/transpose tường minh.</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-diff "><!----><span class="hljs-deletion">- pair_bias = torch.einsum("bqhc,bkhc->bhqk", query_states, key_states)</span> | |
| <span class="hljs-addition">+ pair_bias = query_states.permute(0, 2, 1, 3) @ key_states.permute(0, 2, 3, 1)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf038" 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="#trf038"><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><!---->TRF038<!----></span></h3><!--]--><!----> <p>Kiểm tra rằng mọi tệp modeling<em>*.py, processing</em><em>.py, image<em>processing</em></em>.py, video<em>processing</em><em>.py, feature<em>extraction</em></em>.py và tokenization<em>*.py đều có tệp tests/models/<model>/test</em><em>.py tương ứng (modeling<em>acme.py -> tests/models/acme/test_modeling_acme.py). Ngoại lệ: configuration</em></em>.py, được bao phủ bởi ConfigTester trong tệp test<em>modeling</em><em>.py đồng hành, và tokenization_utils</em>.py, một helper được bao phủ thông qua tokenizer của nó; tokenization<em><name>_fast.py ánh xạ đến tệp test của phiên bản slow tương ứng. Một tệp modular</em>*.py có thể định nghĩa nhiều họ cùng lúc, do đó các class của nó được định tuyến theo hậu tố tên — XxxModel/XxxPreTrainedModel/XxxFor<Task> modeling, XxxImageProcessor(Fast) image processing, XxxProcessor processing, XxxVideoProcessor video processing, XxxFeatureExtractor feature extraction, XxxTokenizer(Fast) tokenization, XxxConfig bị bỏ qua — và một vi phạm được báo cáo cho mỗi tệp test bị thiếu. <code># trf-ignore: TRF038</code> không được hỗ trợ; hãy dùng <code>allowlist_models</code> để ngoại lệ hiển thị rõ trong review. Một tệp nguồn không có tệp test sẽ không có độ bao phủ hồi quy: một forward pass bị hỏng, một ánh xạ chuyển đổi sai, hoặc một tokenizer làm mất token đặc biệt có thể được đưa vào và ở trạng thái hỏng vô thời hạn. Một config giả với trọng số ngẫu nhiên, hoặc một bảng từ vựng nhỏ viết tay, là đủ để chạy thử bất kỳ trường hợp nào trong số đó.</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-diff "><!---->src/transformers/models/acme/modeling_acme.py | |
| <span class="hljs-addition">+tests/models/acme/test_modeling_acme.py</span> | |
| src/transformers/models/acme/tokenization_acme.py | |
| <span class="hljs-addition">+tests/models/acme/test_tokenization_acme.py</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf039" 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="#trf039"><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><!---->TRF039<!----></span></h3><!--]--><!----> <p>Tìm các khối <code>if is_*_available(): import ...</code> (bao gồm các tổ hợp như <code>is_vision_available() and is_torch_available()</code>) và đánh dấu import khi tên đó không được tham chiếu ở bất kỳ đâu khác trong file, kể cả bên trong các type hint dạng chuỗi và <strong>all</strong>. Kiểm tra unused-import của ruff không dọn dẹp được những trường hợp này: import vẫn có thể truy cập được, nên khối đó trông có vẻ ổn khi xét riêng lẻ. Khi một lần refactor không còn cần đến PIL.Image, torch, v.v., import được bảo vệ sẽ tồn tại như một gánh nặng chết và là tín hiệu gây hiểu nhầm về các phụ thuộc thực sự của file.</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-diff "><!---->if is_vision_available(): | |
| <span class="hljs-deletion">- from PIL import Image</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf040" 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="#trf040"><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><!---->TRF040<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các phương thức được đánh dấu bằng cả @capture_outputs và @can_return_tuple. Bổ sung cho TRF003, vốn bao phủ việc phân nhánh return_dict thủ công trong forward(). Cả hai decorator đều pop return_dict, nên chỉ decorator ngoài cùng mới nhìn thấy giá trị thực. @capture_outputs đã xử lý chuyển đổi to_tuple, khiến @can_return_tuple trở nên thừ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-diff "><!----><span class="hljs-deletion">-@can_return_tuple</span> | |
| @merge_with_config_defaults | |
| @capture_outputs | |
| @auto_docstring | |
| def forward(self, x): | |
| return AcmeModelOutput(last_hidden_state=x)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf041" 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="#trf041"><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><!---->TRF041<!----></span></h3><!--]--><!----> <p>In modeling<em>*.py and modular</em><em>.py, flags every <code>if</code>/<code>elif</code> and conditional expression whose condition reads a `config.</em><code>or</code>self.config.<em><code>attribute without a</code># CODEPATH:<code>comment, accepted on the branch line or in the contiguous comment block directly above it. Any config attribute counts, not just boolean flags. Exempt by structure:</code>X if X is not None else fallback<code>, where the tested field is itself one of the results --</code>getattr(config, x, default)<code>spelled long, which cannot fork the graph; and a guard, an</code>if<code>with no</code>else<code>whose body only raises or only warns/logs, since one side aborts and nothing diverges past it. Merely mentioning None does not qualify:</code>config.vision<em>config is not None<code>still owes a note. Exempt by field: framework plumbing that gates no checkpoint divergence --</code>problem_type<code>selecting a loss,</code>hidden_act<code>looking up an activation,</code>num_labels<code>,</code>use_cache<code>,</code>is_decoder<code>, the special token ids, the</code>summary</em></em><code>head settings; the full list is</code>DEFAULT_EXEMPT_ATTRIBUTES<code>in</code>mlinter/trf041.py<code>, extended per project by</code>ignored_attributes = […]<code>on the rule table. A model exempts one of its own fields file-wide with a module-level</code># trf-ignore: TRF041 config.scale_embedding, config.auxiliary_loss<code>at column 0 (</code>self.config.x<code>,</code>config.x<code>and</code>x<code>are the same field). It must name at least one field -- a bare</code># trf-ignore: TRF041<code>keeps its per-line meaning -- and a condition is skipped only when every field it reads is exempt. A config-gated branch is a second architecture in the same file, and the code cannot say whether both halves are still reachable -- which is how dead experimental branches survive for releases. The rule does not forbid the branch: like Rust's</code>// SAFETY:`, it asks for the checkpoints taking each side to be written down next to it. A branch nobody can name one for is a branch to delete.</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-diff "><!----><span class="hljs-addition">+ # CODEPATH: ESMC-6B ships pre-normalised embeddings, the 300M/600M checkpoints do not.</span> | |
| if config.use_embedding_norm: | |
| hidden_states = self.embedding_norm(hidden_states) | |
| <span class="hljs-deletion">- if config.msa_encoder_enabled:</span> | |
| <span class="hljs-deletion">- hidden_states = self.msa_encoder(hidden_states)</span> | |
| <span class="hljs-addition">+ # no released checkpoint sets msa_encoder_enabled -> branch removed</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf042" 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="#trf042"><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><!---->TRF042<!----></span></h3><!--]--><!----> <p>Trong tests/models/<em>/test<em>tokenization</em></em>.py, kiểm tra tệp có định nghĩa một lớp test được thu thập kế thừa từ TokenizerTesterMixin. Chỉ các lớp mà runner thu thập mới được tính — một cơ sở <code>TestCase</code>, hoặc quy ước đặt tên <code>*Test</code> khi cơ sở là lớp test của một model khác — do đó các tệp chỉ chứa helper sẽ bị bỏ qua, và một helper trộn vào bộ suite không đáp ứng quy tắc cho một lớp test thực sự. Việc kế thừa được theo dõi qua các cơ sở trong cùng tệp và vào lớp test tokenizer của một model khác được nhập theo tên; một cơ sở không thể phân giải sẽ không bao giờ được tính. Báo cáo trên lớp test đầu tiên không chạy bộ suite. <code>auto</code> được đưa vào danh sách cho phép: test_tokenization_auto.py kiểm tra việc phân giải AutoTokenizer, không phải một tokenizer. TokenizerTesterMixin là nơi thực sự kiểm tra vòng lặp encode/decode, padding và truncation, xử lý token đặc biệt, tính bền vững của token đã thêm và sự tương đương khi lưu/tải. Một test chỉ khẳng định một vài danh sách token id viết tay sẽ vượt qua trong khi tokenizer bị hỏng ở tất cả các khía cạnh đó, và vẫn trông như đã được kiểm tra khi xem xé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-diff "><!----><span class="hljs-deletion">-class AcmeTokenizationTest(unittest.TestCase):</span> | |
| <span class="hljs-addition">+class AcmeTokenizationTest(TokenizerTesterMixin, unittest.TestCase):</span> | |
| <span class="hljs-addition">+ tokenizer_class = AcmeTokenizer</span> | |
| <span class="hljs-addition">+ test_slow_tokenizer = True</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf043" 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="#trf043"><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><!---->TRF043<!----></span></h3><!--]--><!----> <p>Đánh dấu tham số position_ids được khai báo trong chữ ký forward của các lớp có tên kết thúc bằng Attention. position_ids được tiêu thụ ở phía hạ nguồn bởi flash-attention padding-free training và phải được truyền qua <strong>kwargs. Việc đặt tên cho nó trong chữ ký sẽ nuốt mất nó trước khi giao diện attention có thể đọc được; chuẩn llama truyền position_embeddings cùng</strong>kwargs.</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-diff "><!---->class AcmeAttention(nn.Module): | |
| def forward( | |
| self, | |
| hidden_states, | |
| position_embeddings, | |
| attention_mask=None, | |
| <span class="hljs-deletion">- position_ids=None,</span> | |
| **kwargs: Unpack[TransformersKwargs], | |
| ):<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf044" 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="#trf044"><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><!---->TRF044<!----></span></h3><!--]--><!----> <p>Đánh dấu một tham số có tên cache<em>position trong bất kỳ hàm nào trong modeling</em><em>.py và modular_</em>.py. cache_position đã bị loại bỏ khỏi tất cả các mô hình trong v5. Việc đưa nó trở lại (thường được sao chép từ các nguồn trước v5) sẽ kéo theo một đối số không còn hiệu lực xuyên qua mọi lớp; việc cập nhật cache là cache.update(key_states, value_states, self.layer_idx), không có việc truyền vị trí.</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-diff "><!---->def forward( | |
| self, | |
| hidden_states, | |
| cache=None, | |
| <span class="hljs-deletion">- cache_position=None,</span> | |
| **kwargs, | |
| ): | |
| <span class="hljs-deletion">- key_states, value_states = cache.update(key_states, value_states, self.layer_idx, cache_position)</span> | |
| <span class="hljs-addition">+ key_states, value_states = cache.update(key_states, value_states, self.layer_idx)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf045" 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="#trf045"><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><!---->TRF045<!----></span></h3><!--]--><!----> <p>Đánh dấu các tham số output legacy <em>attentions, output_hidden_states và return_dict trong các chữ ký forward trong modeling</em><em>.py và modular_</em>.py. Các mô hình được đóng góp trước ngày giới hạn được miễn trừ. Bộ chồng decorator chịu trách nhiệm kiểm soát output: @capture<em>outputs giải quyết các cờ output</em>* đối với config và ghi lại các tensor thông qua _can_record_outputs, còn @can_return_tuple xử lý return_dict. Việc khai báo chúng trong chữ ký sẽ đưa lại việc truyền cờ thủ công, gây lệch khỏi các decorator.</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-diff "><!----><span class="hljs-addition">+@capture_outputs</span> | |
| def forward( | |
| self, | |
| input_ids, | |
| <span class="hljs-deletion">- output_attentions=None,</span> | |
| <span class="hljs-deletion">- output_hidden_states=None,</span> | |
| <span class="hljs-deletion">- return_dict=None,</span> | |
| **kwargs: Unpack[TransformersKwargs], | |
| ):<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf046" 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="#trf046"><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><!---->TRF046<!----></span></h3><!--]--><!----> <p>Gán cờ cho các thuộc tính của self trong các phương thức forward của modeling<em>*.py và modular</em>*.py. Trạng thái được ghi trong quá trình forward làm hỏng batching, torch.compile và việc suy luận về module. Trạng thái được mang theo phải được truyền một cách tường minh (các đối tượng cache, vòng lặp generate); các giá trị chỉ phụ thuộc vào config hoặc các hình dạng tĩnh nên được đặt trong <strong>init</strong>.</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-diff "><!---->def forward(self, hidden_states): | |
| <span class="hljs-deletion">- self.sequence_length = hidden_states.shape[1]</span> | |
| <span class="hljs-deletion">- embeddings = self.compute_embeddings(self.sequence_length)</span> | |
| <span class="hljs-addition">+ embeddings = self.compute_embeddings(hidden_states.shape[1])</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf047" 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="#trf047"><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><!---->TRF047<!----></span></h3><!--]--><!----> <p>Gán cờ cho các thuộc tính của self trong preprocess, <em>preprocess, <strong>call</strong> và post_process* methods trong image_processing</em><em>.py và video<em>processing</em></em>.py. Một processor giữ trạng thái giữa các lần gọi sẽ phá vỡ việc batch hóa preprocess-nhiều-rồi-postprocess: lần preprocess thứ hai sẽ ghi đè lên trạng thái mà lần postprocess thứ nhất cần. Trả về giá trị hoặc truyền nó qua chuỗi 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-diff "><!---->def _preprocess(self, images, **kwargs): | |
| <span class="hljs-deletion">- self.original_sizes = [image.shape[-2:] for image in images]</span> | |
| <span class="hljs-addition">+ original_sizes = [image.shape[-2:] for image in images]</span> | |
| ... | |
| <span class="hljs-addition">+ return BatchFeature(data={"pixel_values": pixel_values, "original_sizes": original_sizes})</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf048" 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="#trf048"><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><!---->TRF048<!----></span></h3><!--]--><!----> <p>Các literal dạng list/tuple/set trong khai báo _tied_weights_keys ở cấp lớp. Bản v5 đã thay đổi _tied_weights_keys thành một dict ánh xạ mỗi tham số đích bị ràng buộc (tied) đến tham số nguồn của nó. Dạng list không còn chỉ rõ tham số nào là nguồn, do đó việc ràng buộc, tính toán device_map và lưu trữ sẽ hoạt động sai một cách im lặ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-diff "><!---->class AcmeForCausalLM(AcmePreTrainedModel): | |
| <span class="hljs-deletion">- _tied_weights_keys = ["lm_head.weight"]</span> | |
| <span class="hljs-addition">+ _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf049" 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="#trf049"><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><!---->TRF049<!----></span></h3><!--]--><!----> <p>Các lệnh gọi init trong các phương thức <strong>init</strong> trong modeling<em>*.py và modular</em><em>.py: các nguyên thủy nn.init.</em> / init.* và các bộ khởi tạo tại chỗ trên các tham số của chính lớp (self.weight.data.normal_()). Các mô hình được khởi tạo trên thiết bị meta, do đó các giá trị tensor được ghi trong <strong>init</strong> sẽ bị loại bỏ trước khi tải: một tham số chỉ được khởi tạo ở đó sẽ chứa nội dung ngẫu nhiên khi tinh chỉnh từ đầu hoặc sau khi tải lại từ thiết bị meta. Phân bổ bằng torch.empty trong <strong>init</strong>, khởi tạo trong _init_weights.</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-diff "><!---->class AcmeEmbeddings(nn.Module): | |
| def __init__(self, config): | |
| super().__init__() | |
| self.position_embedding = nn.Parameter(torch.empty(config.num_positions, config.hidden_size)) | |
| <span class="hljs-deletion">- nn.init.trunc_normal_(self.position_embedding, std=config.initializer_range)</span> | |
| class AcmePreTrainedModel(PreTrainedModel): | |
| def _init_weights(self, module): | |
| super()._init_weights(module) | |
| <span class="hljs-addition">+ if isinstance(module, AcmeEmbeddings):</span> | |
| <span class="hljs-addition">+ init.trunc_normal_(module.position_embedding, std=self.config.initializer_range)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf050" 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="#trf050"><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><!---->TRF050<!----></span></h3><!--]--><!----> <p>Các cờ gọi đến một lớp *RotaryEmbedding trong <strong>init</strong> của các lớp có tên kết thúc bằng Attention. Model sở hữu một rotary_emb duy nhất, xây dựng inv_freq một lần và truyền cos/sin xuống dưới dạng position_embeddings. Mỗi module rotary cho từng lớp attention sẽ nhân đôi các buffer, tính lại tần số cho từng lớp và lệch khỏi hợp đồng rằng attention nhận position_embeddings.</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-diff "><!---->class AcmeAttention(nn.Module): | |
| def __init__(self, config, layer_idx): | |
| super().__init__() | |
| <span class="hljs-deletion">- self.rotary_emb = AcmeRotaryEmbedding(config)</span> | |
| class AcmeModel(AcmePreTrainedModel): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| <span class="hljs-addition">+ self.rotary_emb = AcmeRotaryEmbedding(config)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf051" 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="#trf051"><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><!---->TRF051<!----></span></h3><!--]--><!----> <p>So sánh các cờ với thuộc tính <em>attn_implementation trong modeling</em><em>.py và modular_</em>.py. Việc điều phối backend thuộc về ALL_ATTENTION_FUNCTIONS.get_interface, và việc xử lý tensor có điều kiện theo backend (padding, reshaping) thuộc về các wrapper dùng chung dưới integrations/. Việc phân nhánh inline khiến phần thân model nhận thức được kernel và sẽ bị lỗi khi các backend mới được đăng ký.</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-diff "><!----><span class="hljs-deletion">-if self.config._attn_implementation == "flash_attention_2":</span> | |
| <span class="hljs-deletion">- attn_output = flash_path(query_states, key_states, value_states)</span> | |
| <span class="hljs-deletion">-else:</span> | |
| <span class="hljs-deletion">- attn_output = eager_path(query_states, key_states, value_states)</span> | |
| <span class="hljs-addition">+attention_interface = ALL_ATTENTION_FUNCTIONS.get_interface(self.config._attn_implementation, eager_attention_forward)</span> | |
| <span class="hljs-addition">+attn_output, attn_weights = attention_interface(self, query_states, key_states, value_states, ...)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf052" 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="#trf052"><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><!---->TRF052<!----></span></h3><!--]--><!----> <p>Đánh dấu các gán cấp module cho các tên kết thúc bằng <em>ATTENTION_CLASSES trong modeling</em><em>.py và modular_</em>.py. Các lớp attention theo từng backend được chọn từ một dict là cách viết trước khi có interface: các lớp gần như giống hệt nhau sẽ dần khác biệt, và các kernel attention trên hub được đăng ký vào ALL_ATTENTION_FUNCTIONS sẽ không bao giờ đến được chúng. Một lớp attention duy nhất điều phối thông qua interface sẽ thay thế dict; không lan truyền nó từ một lớp cha 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-diff "><!----><span class="hljs-deletion">-ACME_ATTENTION_CLASSES = {</span> | |
| <span class="hljs-deletion">- "eager": AcmeAttention,</span> | |
| <span class="hljs-deletion">- "flash_attention_2": AcmeFlashAttention2,</span> | |
| <span class="hljs-deletion">- "sdpa": AcmeSdpaAttention,</span> | |
| <span class="hljs-deletion">-}</span> | |
| <span class="hljs-addition">+class AcmeAttention(nn.Module):</span> | |
| <span class="hljs-addition">+ def forward(self, ...):</span> | |
| <span class="hljs-addition">+ attention_interface = ALL_ATTENTION_FUNCTIONS.get_interface(self.config._attn_implementation, eager_attention_forward)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf053" 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="#trf053"><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><!---->TRF053<!----></span></h3><!--]--><!----> <p>Trong <em>*.py và modular</em>*.py, các gán cờ xây dựng <em>logits/shift_labels (và các biến thể đã dịch chuyển)</em> bằng cách cắt lát, chẳng hạn như labels[…, 1:], sẽ bị báo lỗi. Việc nhận các nhãn đã được dịch chuyển (shift_labels = kwargs.pop(“shift_labels”, labels)) là cách viết đúng và sẽ không bị báo lỗi. self.loss_function tự dịch chuyển nhãn, do đó việc dịch chuyển trước trong mã modeling sẽ huấn luyện trên các mục tiêu bị dịch chuyển hai lần hoặc buộc phải dùng một đường dẫn loss riêng. Các mô hình chỉ có decoder truyền nhãn thô và để loss tự dịch chuyển chúng. Các mô hình encoder-decoder là trường hợp đối xứng: nhãn của chúng đã được dịch chuyển bởi token bắt đầu decoder được thêm vào trước, do đó chúng truyền shift_labels=labels để ngăn loss dịch chuyển lần nữ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-diff "><!---->if labels is not None: | |
| <span class="hljs-deletion">- shift_logits = logits[..., :-1, :].contiguous()</span> | |
| <span class="hljs-deletion">- shift_labels = labels[..., 1:].contiguous()</span> | |
| <span class="hljs-deletion">- loss = nn.functional.cross_entropy(shift_logits.view(-1, self.config.vocab_size), shift_labels.view(-1))</span> | |
| <span class="hljs-addition">+ # decoder-only: labels are unshifted, the loss shifts them</span> | |
| <span class="hljs-addition">+ loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size)</span> | |
| <span class="hljs-addition">+ # encoder-decoder: labels are already shifted, hand them over as shift_labels</span> | |
| <span class="hljs-addition">+ loss = self.loss_function(logits=logits, labels=labels, shift_labels=labels, vocab_size=self.config.vocab_size)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf055" 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="#trf055"><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><!---->TRF055<!----></span></h3><!--]--><!----> <p>Các cờ <code>config_class</code>, một gán <code>config = SomeConfig</code>, một <code><Model>PreTrainedModel</code> không có chú thích <code>config</code>, và một <code>__init__</code> mà đối số <code>config</code> của nó không được chú thích hoặc trỏ đến một cấu hình khác. Một lớp được xây dựng từ một cấu hình con (<code>AcmeTextModel(config.text_config)</code>) phải khai báo cấu hình con đó. Một tệp <code>modular_*.py</code> được kiểm tra thông qua các tệp mà nó tạo ra. <code>PreTrainedModel</code> suy ra <code>config_class</code> từ chú thích <code>config</code>. Một gán hoặc một <code>config_class</code> viết tay sẽ giữ nguyên hoặc lệch khỏi giá trị của lớp cha, và một <code>__init__</code> không khớp sẽ xây dựng mô hình từ một cấu hình trong khi lại quảng bá một cấu hình khá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-diff "><!---->class Gemma4VisionModel(Gemma4PreTrainedModel): | |
| """The Gemma 4 Vision Encoder.""" | |
| <span class="hljs-deletion">- config = Gemma4VisionConfig</span> | |
| <span class="hljs-addition">+ config: Gemma4VisionConfig</span> | |
| <span class="hljs-deletion">- def __init__(self, config):</span> | |
| <span class="hljs-addition">+ def __init__(self, config: Gemma4VisionConfig):</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf056" 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="#trf056"><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><!---->TRF056<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các cờ <code>.item()</code> và <code>.tolist()</code> được gọi bên trong bất kỳ <code>forward</code> nào. Một <code>.tolist()</code> cung cấp cho đối số kích thước tách của <code>split(...)</code> được miễn trừ: torch.split cần các int của Python. Cả hai cuộc gọi đều đọc tensor trở lại host, điều mà dynamo không thể theo dõi, do đó đồ thị bị ngắ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-diff "><!----><span class="hljs-deletion">- for grid, item in zip(grid_thw.tolist(), split_items):</span> | |
| <span class="hljs-deletion">- _, height, width = grid</span> | |
| <span class="hljs-deletion">- merged.append(self.patch_merger(item, size=(height, width)))</span> | |
| <span class="hljs-addition">+ for grid, item in zip(grid_thw, split_items):</span> | |
| <span class="hljs-addition">+ merged.append(self.patch_merger(item, size=(grid[1], grid[2])))</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf057" 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="#trf057"><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><!---->TRF057<!----></span></h3><!--]--><!----> <p>Kiểm tra <code>@auto_docstring</code> trên các lớp con công khai của <code>PreTrainedModel</code> (<code><Model>PreTrainedModel</code>, <code><Model>Model</code>, <code><Model>For<Task></code>, backbones), các lớp con của <code>PreTrainedConfig</code>, các lớp con của <code>ModelOutput</code>, image processors, video processors và các lớp con của <code>ProcessorMixin</code>, cũng như trên các phương thức công khai của chúng: <code>forward</code>, <code>get_image_features</code>, <code>get_video_features</code>, <code>get_audio_features</code>, <code>get_text_features</code>, <code>preprocess</code> và <code>__call__</code>. Một <code>modular_*.py</code> được kiểm tra đối chiếu với các tệp được tạo từ nó. Nếu không có tệp này, lớp và các phương thức của nó sẽ được phát hành mà không có tài liệu về tham số, giá trị trả về và ví dụ, và sau đó phải được viết thủ công cho từng model.</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-diff "><!----><span class="hljs-addition">+@auto_docstring</span> | |
| @dataclass | |
| class AcmeModelOutputWithPast(ModelOutput): | |
| logits: torch.Tensor | None = None | |
| <span class="hljs-addition">+@auto_docstring</span> | |
| class AcmeForConditionalGeneration(AcmePreTrainedModel): | |
| <span class="hljs-addition">+ @auto_docstring</span> | |
| def forward(self, input_ids, pixel_values=None, **kwargs): | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf058" 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="#trf058"><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><!---->TRF058<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, các cờ <code>register_buffer("<name>", ...)</code> được gọi với tên buffer là chuỗi literal, trên bất kỳ receiver nào (<code>self</code>, hoặc một module khác như <code>layer.mamba</code>). Một tên được tính toán — một biến hoặc f-string, ví dụ một buffer cho mỗi layer bên trong một vòng lặp — không có tương đương gán thuộc tính và được miễn trừ. Vì torch>=2.5 <code>nn.Buffer</code> đăng ký một buffer thông qua gán thuộc tính thông thường, giống như <code>nn.Parameter</code>. Một buffer được tạo bởi một lời gọi phương thức chỉ tồn tại như một tác dụng phụ của việc chạy <code>__init__</code>, do đó một file modular muốn chỉnh sửa một buffer phải định nghĩa lại toàn bộ <code>__init__</code>. Khi được gán như một thuộc tính, nó có thể được kế thừa và ghi đè một cách độc lập.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-diff "><!----><span class="hljs-deletion">- self.register_buffer("inv_freq", inv_freq, persistent=False)</span> | |
| <span class="hljs-deletion">- self.register_buffer(</span> | |
| <span class="hljs-deletion">- "position_ids", torch.arange(config.max_position_embeddings).expand((1, -1)), persistent=False</span> | |
| <span class="hljs-deletion">- )</span> | |
| <span class="hljs-addition">+ self.inv_freq = nn.Buffer(inv_freq, persistent=False)</span> | |
| <span class="hljs-addition">+ self.position_ids = nn.Buffer(torch.arange(config.max_position_embeddings).expand((1, -1)), persistent=False)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf059" 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="#trf059"><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><!---->TRF059<!----></span></h3><!--]--><!----> <p>Đối với các thư mục mô hình mà kế hoạch song song tensor gán <code>moe_tp_experts</code>, kiểm tra xem các lớp <code>*Experts</code> được định tuyến có nhận trạng thái ẩn, chỉ số top-k và trọng số định tuyến top-k làm ba đối số vị trí đầu tiên của <code>forward</code> hay không. Các tên gọi khác như <code>selected_experts</code> và <code>routing_weights</code> được chấp nhận. <code>MoeExpertsParallel</code> áp dụng phép biến đổi gradient cho đối số vị trí thứ 3. Một chữ ký khác sẽ âm thầm áp dụng nó cho tensor sai hoặc bỏ qua 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-diff "><!---->class AcmeExperts(nn.Module): | |
| <span class="hljs-deletion">- def forward(self, hidden_states):</span> | |
| <span class="hljs-addition">+ def forward(self, hidden_states, top_k_index, top_k_weights):</span> | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf060" 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="#trf060"><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><!---->TRF060<!----></span></h3><!--]--><!----> <p>Các cờ <code>from transformers... import X</code> và <code>import transformers...</code> dưới <code>src/transformers/models/</code> và đề xuất dạng tương đối (<code>from ...modeling_utils import X</code>). Các script <code>convert_*.py</code> bị bỏ qua. Một import tuyệt đối bên trong gói sẽ đi qua <code>transformers/__init__.py</code> cấp trên theo kiểu lazy, điều này có thể gây ra import vòng tròn và có thể trỏ đến một bản cài đặt khác của transformers.</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-diff "><!----><span class="hljs-deletion">-from transformers.modeling_utils import PreTrainedModel</span> | |
| <span class="hljs-deletion">-from transformers.models.acme.configuration_acme import AcmeConfig</span> | |
| <span class="hljs-deletion">-from transformers import initialization as init</span> | |
| <span class="hljs-addition">+from ...modeling_utils import PreTrainedModel</span> | |
| <span class="hljs-addition">+from .configuration_acme import AcmeConfig</span> | |
| <span class="hljs-addition">+from ... import initialization as init</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf061" 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="#trf061"><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><!---->TRF061<!----></span></h3><!--]--><!----> <p>Trong modular_*.py, một class sẽ được đánh dấu nếu base của nó được định nghĩa trước đó trong cùng file. Các base được import từ model khác, các subclass của <code><Model>PreTrainedModel</code> và các alias tương thích ngược sẽ không bị đánh dấu. Converter sẽ inline các base được import từ các model khác nhưng giữ nguyên base trong cùng file, do đó file modeling được tạo ra sẽ giữ lại quan hệ kế thừa và các class của nó không còn có thể được đọc riêng lẻ từng cái mộ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-diff "><!---->from ..llama.modeling_llama import LlamaDecoderLayer | |
| class AcmeDecoderLayer(LlamaDecoderLayer): | |
| pass | |
| <span class="hljs-deletion">-class AcmeSparseDecoderLayer(AcmeDecoderLayer):</span> | |
| <span class="hljs-addition">+class AcmeSparseDecoderLayer(LlamaDecoderLayer):</span> | |
| def __init__(self, config, layer_idx): | |
| super().__init__(config, layer_idx) | |
| self.mlp = AcmeSparseMoeBlock(config)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf062" 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="#trf062"><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><!---->TRF062<!----></span></h3><!--]--><!----> <p>Các cờ đánh dấu các bộ khởi tạo <code>torch.nn.init</code> tại chỗ (<code>nn.init.normal_(...)</code>, bất kể cách nhập) được gọi bên trong <code>_init_weights</code>. Các hàm trợ giúp chỉ tính toán một giá trị, chẳng hạn như <code>calculate_gain</code>, sẽ không bị đánh dấu. Các hàm <code>init</code> của transformers bỏ qua các tensor đã được nạp từ checkpoint. <code>torch.nn.init</code> dựa vào một bản vá được áp dụng trong quá trình <code>from_pretrained</code>, mà một hàm được nhập theo tên sẽ thoát khỏi, do đó một trọng số đã nạp có thể bị khởi tạo lại một cách im lặ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-diff "><!---->from torch import nn | |
| <span class="hljs-addition">+</span> | |
| <span class="hljs-addition">+from ... import initialization as init</span> | |
| def _init_weights(self, module): | |
| super()._init_weights(module) | |
| if isinstance(module, AcmeLayerScale): | |
| <span class="hljs-deletion">- nn.init.constant_(module.lambda1, self.config.layer_scale_init_value)</span> | |
| <span class="hljs-addition">+ init.constant_(module.lambda1, self.config.layer_scale_init_value)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf063" 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="#trf063"><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><!---->TRF063<!----></span></h3><!--]--><!----> <p>Theo dõi mọi import tương đối mà một tệp model thực thi khi import và cảnh báo khi các tệp được import cần một backend mà tệp import không khai báo: thông qua các yêu cầu cơ sở, <code>@requires</code> yếu nhất trong số các đối tượng <code>__all__</code> của nó, hoặc một guard <code>is_<backend>_available()</code>. Tệp <code>modular_*.py</code> được kiểm tra thông qua các tệp mà nó tạo ra. Module lazy import một tệp khi các backend đã khai báo của nó được cài đặt. Nếu tệp đó import một tệp anh em cần nhiều hơn, <code>from transformers import *</code> sẽ gặp lỗi trong môi trường đó: một processor chỉ dành cho vision import một image processor của torchvision sẽ làm hỏng các cài đặt có Pillow nhưng không có torch.</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-diff "><!---->if is_vision_available(): | |
| <span class="hljs-deletion">- from .image_processing_acme import AcmeImageProcessorKwargs</span> | |
| <span class="hljs-addition">+ from .image_processing_pil_acme import AcmeImageProcessorKwargs</span> | |
| @requires(backends=("vision",)) | |
| class AcmeProcessor(ProcessorMixin):<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf064" 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="#trf064"><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><!---->TRF064<!----></span></h3><!--]--><!----> <p>Trong xử lý<em>*.py và modular</em>*.py, một lớp con của <code>ProcessingKwargs</code> sẽ gán hoặc chú thích lại <code>_defaults</code>, <code>text_kwargs</code>, <code>images_kwargs</code>, <code>videos_kwargs</code> hoặc <code>audio_kwargs</code>. <code>ProcessingKwargs._defaults</code> đã bị ngừng hỗ trợ: các giá trị mặc định là thuộc tính của lớp processor, được kế thừa và ghi đè như bất kỳ thuộc tính nào khác. Các trường modality giữ nguyên kiểu mặc định, vì các đối số khả dụng chỉ được biết đến khi các sub-processor được nạp vào lúc runtime.</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-diff "><!---->class AcmeProcessorKwargs(ProcessingKwargs, total=False): | |
| <span class="hljs-deletion">- images_kwargs: AcmeImagesKwargs</span> | |
| <span class="hljs-deletion">- _defaults = {</span> | |
| <span class="hljs-deletion">- "text_kwargs": {"padding": False},</span> | |
| <span class="hljs-deletion">- "images_kwargs": {"num_crops": 4},</span> | |
| <span class="hljs-deletion">- }</span> | |
| <span class="hljs-addition">+ num_crops: int</span> | |
| class AcmeProcessor(ProcessorMixin): | |
| valid_processor_kwargs = AcmeProcessorKwargs | |
| <span class="hljs-addition">+ text_kwargs = {"padding": False}</span> | |
| <span class="hljs-addition">+ num_crops: int = 4</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf065" 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="#trf065"><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><!---->TRF065<!----></span></h3><!--]--><!----> <p>Trong xử lý<em>*.py, đánh dấu mọi class kế thừa <code>ImagesKwargs</code>, <code>TextKwargs</code>, <code>AudioKwargs</code> hoặc <code>VideosKwargs</code>, trực tiếp hoặc thông qua subclass của một model khác. Trong modular</em><em>.py, nơi cũng chứa các sub-processor, một subclass được cho phép khi được đặt tên là <code><Model>ImageProcessorKwargs</code>, <code><Model>VideoProcessorKwargs</code>, <code><Model>VideoProcessorInitKwargs</code> hoặc <code><Model>AudioProcessorKwargs</code>. Kwargs của modality thuộc về các tệp sub-processor (`image<em>processing</em></em>.py<code>,</code>video<em>processing</em><em>.py`, …). Trong processing_</em>.py, chúng không được gán kiểu, vì các sub-processor được nạp độ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-diff "><!----><span class="hljs-deletion">-class AcmeImagesKwargs(ImagesKwargs, total=False):</span> | |
| <span class="hljs-deletion">- num_crops: int</span> | |
| <span class="hljs-deletion">-</span> | |
| <span class="hljs-deletion">-</span> | |
| class AcmeProcessorKwargs(ProcessingKwargs, total=False): | |
| <span class="hljs-deletion">- images_kwargs: AcmeImagesKwargs</span> | |
| <span class="hljs-addition">+ num_crops: int</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf066" 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="#trf066"><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><!---->TRF066<!----></span></h3><!--]--><!----> <p>Đánh dấu các backends trong <code>@requires</code> lặp lại các yêu cầu ngầm định của tệp hoặc, đối với một phương thức, các yêu cầu tường minh của lớp bao chứa. Cũng đánh dấu <code>requires_backends(...)</code> nếu nó có thể được viết dưới dạng decorator <code>@requires</code>, và một <code>@requires</code> được đặt phía trên <code>@classmethod</code> hoặc <code>@staticmethod</code>. Cutoff miễn trừ các model cũ khỏi mọi thứ ngoại trừ decorator đặt sai vị trí. Việc lặp lại các yêu cầu backend làm che mờ các phụ thuộc mà một phương thức thực sự thêm vào. Một kiểm tra không điều kiện ở đầu hàm nên thuộc về <code>@requires(backends=...)</code>, thứ này phơi bày metadata phụ thuộc và thực hiện kiểm tra khi hàm được gọi. Giữ <code>requires_backends</code> cho các yêu cầu có điều kiện. <code>@requires</code> bọc một hàm thông thường, nên khi đặt phía trên <code>@classmethod</code> hoặc <code>@staticmethod</code> nó sẽ bọc descriptor thay vào đó và mọi lời gọi đều gây ra <code>TypeError</code>: nó phải nằm bên dưới chú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-diff "><!----># image_processing_pil_acme.py already requires vision. | |
| <span class="hljs-deletion">-@requires(backends=("vision", "torch"))</span> | |
| <span class="hljs-addition">+@requires(backends=("torch",))</span> | |
| class AcmeImageProcessorPil(PilBackend): | |
| <span class="hljs-deletion">- @requires(backends=("torch",))</span> | |
| def post_process(self, outputs): | |
| return outputs | |
| <span class="hljs-addition">+ @requires(backends=("scipy",))</span> | |
| def refine(self, outputs): | |
| <span class="hljs-deletion">- requires_backends(self, ["scipy"])</span> | |
| return outputs<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf067" 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="#trf067"><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><!---->TRF067<!----></span></h3><!--]--><!----> <p>Trong modeling<em>*.py và modular</em>*.py, một cuộc gọi sẽ bị đánh dấu khi nó bật mọi trạng thái ẩn (<code>output_hidden_states=True</code>, hoặc đặt giá trị này trong <code>**kwargs</code>) trong khi hàm đó chỉ đọc <code>.hidden_states[...]</code> theo chỉ mục. Mã sử dụng toàn bộ bộ ba (duyệt, cắt, xếp chồng hoặc trả về nó) và các backbone có <code>@filter_output_hidden_states</code> được miễn trừ. Trạng thái ẩn của mỗi lớp sẽ tồn tại cho đến khi cuộc gọi trả về, do đó bộ nhớ đỉnh tăng theo độ sâu. <code>output_hidden_states</code> cũng chấp nhận một chỉ mục hoặc một danh sách các chỉ mục và chỉ bắt giữ những chỉ mục đó: trang trí một phương thức chuyển tiếp <code>**kwargs</code> bằng <code>@select_hidden_states</code>, hoặc truyền trực tiếp chỉ mụ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-diff "><!----><span class="hljs-addition">+from ...utils.output_capturing import select_hidden_states</span> | |
| ... | |
| <span class="hljs-addition">+ @select_hidden_states("vision_feature_layer")</span> | |
| def get_image_features(self, pixel_values, vision_feature_layer=None, **kwargs): | |
| <span class="hljs-deletion">- kwargs["output_hidden_states"] = True</span> | |
| image_outputs = self.vision_tower(pixel_values, return_dict=True, **kwargs) | |
| selected_image_feature = image_outputs.hidden_states[vision_feature_layer] | |
| ... | |
| <span class="hljs-deletion">- outputs = self.img_processor(img_embeds, output_hidden_states=True)</span> | |
| <span class="hljs-addition">+ outputs = self.img_processor(img_embeds, output_hidden_states=self.layer_idx)</span> | |
| return outputs.hidden_states[self.layer_idx]<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="trf068" 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="#trf068"><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><!---->TRF068<!----></span></h3><!--]--><!----> <p>Kiểm tra từng mục <code>_no_split_modules</code> với các lớp được định nghĩa hoặc nhập trong tệp modeling. Tệp <code>modular_*.py</code> được kiểm tra thông qua các tệp mà nó tạo ra trên đĩa, do đó hãy tạo lại trước khi chạy lint. accelerate khớp các tên này với tên lớp của các submodule. Một tên không phải là lớp sẽ không bao giờ khớp, vì vậy lớp đó có thể được chia tách giữa các thiết bị. Điều này thường xuất phát từ một tệp modular kế thừa <code>_no_split_modules</code>: trình chuyển đổi đổi tên tiền tố, không phải tên lớp.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-diff "><!---->class Qwen4ExpModel(Qwen3_5MoeModel): | |
| <span class="hljs-deletion">- # inherits ["Qwen3_5MoeDecoderLayer"] -> converted to "Qwen4ExpDecoderLayer", which does not exist</span> | |
| <span class="hljs-addition">+ _no_split_modules = ["Qwen4ExpTextDecoderLayer"]</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="suppressing-violations" 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="#suppressing-violations"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Chặn các vi phạm<!----></span></h2><!--]--><!----> <p>Nếu bạn cần tắt một vi phạm quy tắc, hãy sử dụng một trong hai tùy chọn bên dưới.</p> <!--[2--><h3 class="relative group"><a id="inline-suppression" 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="#inline-suppression"><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><!---->Inline suppression<!----></span></h3><!--]--><!----> <p>Thêm chú thích <code># trf-ignore: RULE_ID</code> vào dòng vi phạm. Bao gồm một lời giải thích để người xem hiểu tại sao việc bỏ qua này là hợp lý.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># trf-ignore: TRF011 — mask is derived from self.config, not the layer</span> | |
| hidden_states = layer(hidden_states, attention_mask=mask_from_config)<!----></pre></div><!----> <p>Không sử dụng <code>trf-ignore</code> để tắt các vi phạm cần được sửa trong mã.</p> <!--[2--><h3 class="relative group"><a id="allowlistmodels" 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="#allowlistmodels"><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><!---->allowlist_models<!----></span></h3><!--]--><!----> <p>Đối với các model có mã nguồn cũ không thể sửa ngay, hãy thêm tên thư mục của model vào danh sách <code>allowlist_models</code> của quy tắc tương ứng trong <a href="https://github.com/huggingface/transformers-mlinter/blob/main/mlinter/rules.toml" rel="nofollow">mlinter rules.toml</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-toml "><!----><span class="hljs-section">[rules.TRF004]</span> | |
| <span class="hljs-attr">allowlist_models</span> = [<span class="hljs-string">"existing_model"</span>, <span class="hljs-string">"your_model_name"</span>]<!----></pre></div><!----> <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/modeling_rules.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_mcl5gy = { | |
| 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.C2wSEYyY.js"), | |
| import("/docs/transformers/main/en/_app/immutable/entry/app.DvYwi2oY.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 595], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 272 kB
- Xet hash:
- 3ff1c8fc60b11584fef67bdd753b150bb02461a24838a65668b53561157010ba
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.