Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Tambahkan model dengan modular transformers","local":"add-a-model-with-modular-transformers","sections":[{"title":"Menerapkan file modular","local":"implementing-a-modular-file","sections":[{"title":"Konfigurasi","local":"config","sections":[],"depth":3},{"title":"Norm","local":"norm","sections":[],"depth":3},{"title":"Perhatian","local":"attention","sections":[],"depth":3},{"title":"DecoderLayer","local":"decoderlayer","sections":[],"depth":3},{"title":"Model","local":"model","sections":[],"depth":3},{"title":"Kepala model","local":"model-head","sections":[],"depth":3},{"title":"Kelas lainnya","local":"other-classes","sections":[],"depth":3},{"title":"Menyelesaikan file","local":"finishing-the-file","sections":[],"depth":3}],"depth":2},{"title":"Generate file pemodelan","local":"generate-the-modeling-files","sections":[],"depth":2},{"title":"Pola untuk file modular","local":"patterns-for-modular-files","sections":[{"title":"Menghapus atribut","local":"removing-attributes","sections":[],"depth":3},{"title":"Bekerja dengan super()","local":"working-with-super","sections":[{"title":"Panggil kelas kakek-nenek secara langsung","local":"call-a-grandparent-class-directly","sections":[],"depth":4},{"title":"super_kwargs","local":"superkwargs","sections":[],"depth":4}],"depth":3},{"title":"Menghapus metode yang tidak digunakan","local":"deleting-unused-methods","sections":[],"depth":3},{"title":"Menimpa metode yang dihias","local":"overriding-decorated-methods","sections":[],"depth":3},{"title":"Penamaan khusus","local":"special-naming","sections":[],"depth":3},{"title":"Docstring konfigurasi","local":"config-docstrings","sections":[],"depth":3}],"depth":2},{"title":"Konversi checkpoint","local":"checkpoint-conversion","sections":[{"title":"Tulis skrip konversi","local":"write-a-conversion-script","sections":[],"depth":3},{"title":"Pemetaan konversi runtime","local":"runtime-conversion-mapping","sections":[],"depth":3}],"depth":2},{"title":"Langkah selanjutnya","local":"next-steps","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/675.C9W3viMf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--1rp9gg8--><meta name="hf:doc:metadata" content="{"title":"Add a model with modular transformers","local":"add-a-model-with-modular-transformers","sections":[{"title":"Implementing a modular file","local":"implementing-a-modular-file","sections":[{"title":"Config","local":"config","sections":[],"depth":3},{"title":"Norm","local":"norm","sections":[],"depth":3},{"title":"Attention","local":"attention","sections":[],"depth":3},{"title":"DecoderLayer","local":"decoderlayer","sections":[],"depth":3},{"title":"Model","local":"model","sections":[],"depth":3},{"title":"Model head","local":"model-head","sections":[],"depth":3},{"title":"Other classes","local":"other-classes","sections":[],"depth":3},{"title":"Finishing the file","local":"finishing-the-file","sections":[],"depth":3}],"depth":2},{"title":"Generate the modeling files","local":"generate-the-modeling-files","sections":[],"depth":2},{"title":"Patterns for modular files","local":"patterns-for-modular-files","sections":[{"title":"Removing attributes","local":"removing-attributes","sections":[],"depth":3},{"title":"Working with super()","local":"working-with-super","sections":[{"title":"Call a grandparent class directly","local":"call-a-grandparent-class-directly","sections":[],"depth":4},{"title":"super_kwargs","local":"superkwargs","sections":[],"depth":4}],"depth":3},{"title":"Deleting unused methods","local":"deleting-unused-methods","sections":[],"depth":3},{"title":"Overriding decorated methods","local":"overriding-decorated-methods","sections":[],"depth":3},{"title":"Special naming","local":"special-naming","sections":[],"depth":3},{"title":"Config docstrings","local":"config-docstrings","sections":[],"depth":3}],"depth":2},{"title":"Checkpoint conversion","local":"checkpoint-conversion","sections":[{"title":"Write a conversion script","local":"write-a-conversion-script","sections":[],"depth":3},{"title":"Runtime conversion mapping","local":"runtime-conversion-mapping","sections":[],"depth":3}],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="add-a-model-with-modular-transformers" 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="#add-a-model-with-modular-transformers"><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><!---->Tambahkan model dengan modular transformers<!----></span></h1><!--]--><!----> <p>Modular transformers mengurangi kode yang diperlukan untuk menambahkan model dengan memungkinkan impor dan pewarisan, berbanding terbalik dengan kebijakan <a href="https://huggingface.co/blog/transformers-design-philosophy" rel="nofollow">satu model, satu file</a>. Alih-alih mengulang komponen model di berbagai file, tambahkan file <em>modular</em> ke folder model Anda dan warisi dari kelas yang sudah ada.</p> <p>Konverter menghasilkan file mandiri dari file modular. Pengguna mendapatkan antarmuka file tunggal yang sama yang sudah mereka kenal.</p> <blockquote class="note"><p>Modular transformers tidak dimaksudkan untuk menggantikan <a href="./add_new_model">kode pemodelan lama</a>. Jika model Anda tidak didasarkan pada model yang sudah ada, tambahkan file <code>modeling.py</code> secara manual. Hal yang sama berlaku untuk file konfigurasi, tokenisasi, atau pemrosesan yang tidak dapat mewarisi dengan bersih dari file serupa.</p> <p>Tidak ada satu urutan yang benar. Beberapa kontributor menulis file modular terlebih dahulu lalu menghasilkan kode darinya. Yang lain memulai dengan <code>modeling.py</code> yang ditulis manual dan kemudian merefaktornya menjadi file modular. Kedua pendekatan tersebut berhasil.</p></blockquote> <!--[1--><h2 class="relative group"><a id="implementing-a-modular-file" 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="#implementing-a-modular-file"><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><!---->Menerapkan file modular<!----></span></h2><!--]--><!----> <p>Mulai dengan mencari model di Transformers yang mirip dengan model Anda. Titik awal yang baik adalah <a href="./model_doc/mistral">Mistral</a>, <a href="./model_doc/qwen2">Qwen2</a>, <a href="./model_doc/cohere">Cohere</a> dan <a href="./model_doc/cohere2">Cohere2</a>, serta <a href="./model_doc/llama">Llama</a>. Tabel di bawah ini memetakan komponen umum ke model yang dapat Anda warisi.</p> <table><thead><tr><th>Komponen</th><th>Model</th></tr></thead><tbody><tr><td>Campuran pakar</td><td>Mixtral atau Qwen2-MoE</td></tr><tr><td>Rotary embedding yang diselang-seling (dan/atau parsial)</td><td>GLM, Phi</td></tr><tr><td>Model ruang keadaan</td><td>Jamba, Bamba, Zamba, Mamba2</td></tr><tr><td>State tersembunyi berulang</td><td>Gemma2</td></tr><tr><td>Pola perhatian jendela geser/perhatian penuh per lapisan</td><td>Gemma2, Cohere2</td></tr><tr><td>Pemotongan QKV</td><td>Olmo</td></tr><tr><td>Normalisasi QK</td><td>Olmo2, Cohere</td></tr><tr><td>QKV Terpadu (tidak disarankan)</td><td>Phi3</td></tr></tbody></table> <blockquote class="tip"><p>Gunakan alat <a href="https://huggingface.co/spaces/Molbap/modular-detector-v2" rel="nofollow">modular-detector-v2</a> untuk menemukan implementasi yang sudah ada agar dapat diwarisi. Tempelkan potongan kode, dan alat ini akan mengembalikan metode yang paling mirip yang sudah ada di Transformers, sehingga Anda dapat mengidentifikasi kelas induk terbaik sebelum mulai menulis.</p></blockquote> <p>Jangan memodifikasi model yang sudah ada hanya agar pewarisan (inheritance) berfungsi untuk model baru Anda. Jika mengubah nama atau membuat subkelas dari kelas induk terasa terlalu rumit, salin kode yang relevan secara langsung sebagai gantinya.</p> <p>Buat <code>src/transformers/models/<name>/modular_<name>.py</code>, di mana <code><name></code> sesuai dengan nama direktori model dalam format snake_case. Bagian ini memandu Anda dalam mengimplementasikan <a href="./model_doc/olmo2">Olmo2</a> dari <a href="./model_doc/olmo">Olmo</a> dengan pendekatan modular (lihat file <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/models/olmo2/modular_olmo2.py" rel="nofollow">modular_olmo2.py</a> asli).</p> <!--[2--><h3 class="relative group"><a id="config" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#config"><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><!---->Konfigurasi<!----></span></h3><!--]--><!----> <p>Ada dua poin di mana <a href="/docs/transformers/main/id/model_doc/olmo2#transformers.Olmo2Config">Olmo2Config</a> berbeda dari <a href="/docs/transformers/main/id/model_doc/olmo#transformers.OlmoConfig">OlmoConfig</a>.</p> <ol><li>Ada argumen baru, <code>rms_norm_eps</code>.</li> <li>Argumen <code>clip_qkv</code> tidak lagi digunakan.</li></ol> <p>Deklarasikan argumen baru sebagai anotasi tipe level kelas dengan nilai default. Untuk argumen yang dihapus, tetapkan <code>AttributeError()</code> untuk menekan atribut yang diwarisi di file yang dihasilkan (lihat <a href="#removing-attributes">Menghapus atribut</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> Disalin</div><!----></button><!----></div> <pre class="language-diff "><!----><span class="hljs-deletion">- @auto_docstring(checkpoint="allenai/OLMo-7B-hf")</span> | |
| <span class="hljs-addition">+ @auto_docstring(checkpoint="allenai/Olmo2-7B-1124-hf")</span> | |
| <span class="hljs-addition">+ @strict</span> | |
| <span class="hljs-deletion">- class OlmoConfig(PreTrainedConfig):</span> | |
| <span class="hljs-addition">+ class Olmo2Config(OlmoConfig):</span> | |
| ... | |
| <span class="hljs-deletion">- model_type = "olmo"</span> | |
| <span class="hljs-addition">+ model_type = "olmo2"</span> | |
| ... | |
| <span class="hljs-addition">+ rms_norm_eps: float = 1e-5</span> | |
| <span class="hljs-deletion">- clip_qkv: float | None = None</span> | |
| <span class="hljs-addition">+ clip_qkv = AttributeError()</span><!----></pre></div><!----> <p><code>@auto_docstring</code> menghasilkan dokumentasi argumen standar secara otomatis (lihat panduan <a href="./auto_docstring">@auto_docstring</a>). <code>@strict</code> menolak kwargs yang tidak dikenal saat instansiasi, sehingga dapat mendeteksi kesalahan ketik dan argumen yang sudah usang lebih awal. Tambahkan keduanya ke setiap kelas config karena dekorator tidak diwarisi dari kelas induk. Deklarasikan secara eksplisit meskipun config induk sudah memilikinya.</p> <p>Untuk mengatur atribut turunan atau menangani logika kompatibilitas mundur, gunakan <code>__post_init__</code> alih-alih <code>__init__</code>. Sebagai contoh, Cohere2 menghitung <code>head_dim</code> dan menurunkan <code>layer_types</code> pada saat inisialisasi.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">__post_init__</span>(<span class="hljs-params">self, **kwargs</span>): | |
| <span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.num_key_value_heads <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>: | |
| <span class="hljs-variable language_">self</span>.num_key_value_heads = <span class="hljs-variable language_">self</span>.num_attention_heads | |
| <span class="hljs-variable language_">self</span>.head_dim = <span class="hljs-variable language_">self</span>.hidden_size // <span class="hljs-variable language_">self</span>.num_attention_heads | |
| <span class="hljs-built_in">super</span>().__post_init__(**kwargs)<!----></pre></div><!----> <p>Untuk model yang mendukung paralelisme tensor atau pipeline, definisikan <code>base_model_tp_plan</code> dan <code>base_model_pp_plan</code> sebagai kamus tingkat kelas pada config. Kedua kamus ini mendefinisikan cara memecah model ke berbagai perangkat. Lihat config yang sudah ada seperti <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/models/olmo2/modular_olmo2.py" rel="nofollow">Olmo2</a> atau <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/models/cohere2/modular_cohere2.py" rel="nofollow">Cohere2</a> untuk contoh.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewModelConfig</span>(<span class="hljs-title class_ inherited__">PreTrainedConfig</span>): | |
| model_type = <span class="hljs-string">"my_new_model"</span> | |
| <span class="hljs-comment"># Tensor parallelism: maps layer name patterns to sharding strategies.</span> | |
| <span class="hljs-comment"># Use "colwise" / "rowwise" for standard sharding, or the "gather_output" /</span> | |
| <span class="hljs-comment"># "split_input" variants when an extra op (e.g. a QK norm) prevents fusing.</span> | |
| base_model_tp_plan = { | |
| <span class="hljs-string">"layers.*.self_attn.q_proj"</span>: <span class="hljs-string">"colwise"</span>, | |
| <span class="hljs-string">"layers.*.self_attn.k_proj"</span>: <span class="hljs-string">"colwise"</span>, | |
| <span class="hljs-string">"layers.*.self_attn.v_proj"</span>: <span class="hljs-string">"colwise"</span>, | |
| <span class="hljs-string">"layers.*.self_attn.o_proj"</span>: <span class="hljs-string">"rowwise"</span>, | |
| <span class="hljs-string">"layers.*.mlp.gate_proj"</span>: <span class="hljs-string">"colwise"</span>, | |
| <span class="hljs-string">"layers.*.mlp.up_proj"</span>: <span class="hljs-string">"colwise"</span>, | |
| <span class="hljs-string">"layers.*.mlp.down_proj"</span>: <span class="hljs-string">"rowwise"</span>, | |
| } | |
| <span class="hljs-comment"># Pipeline parallelism: maps submodule names to their (input, output) tensor names.</span> | |
| base_model_pp_plan = { | |
| <span class="hljs-string">"embed_tokens"</span>: ([<span class="hljs-string">"input_ids"</span>], [<span class="hljs-string">"inputs_embeds"</span>]), | |
| <span class="hljs-string">"layers"</span>: ([<span class="hljs-string">"hidden_states"</span>, <span class="hljs-string">"attention_mask"</span>], [<span class="hljs-string">"hidden_states"</span>]), | |
| <span class="hljs-string">"norm"</span>: ([<span class="hljs-string">"hidden_states"</span>], [<span class="hljs-string">"hidden_states"</span>]), | |
| }<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="norm" 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="#norm"><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><!---->Norm<!----></span></h3><!--]--><!----> <p>Untuk menyalin kelas induk tanpa perubahan, warisi dengan <code>pass</code>. Linter akan menyalin konten induk dan mengganti nama semua referensi agar sesuai dengan model baru.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ..olmo.modeling_olmo <span class="hljs-keyword">import</span> OlmoRotaryEmbedding | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">Olmo2RotaryEmbedding</span>(<span class="hljs-title class_ inherited__">OlmoRotaryEmbedding</span>): | |
| <span class="hljs-keyword">pass</span><!----></pre></div><!----> <p>Untuk mengubah perilaku tertentu, warisi dan timpa hanya bagian yang berbeda. <code>Olmo2RMSNorm</code> berbeda dari <code>LlamaRMSNorm</code> pada satu baris. Perkalian terjadi <em>sebelum</em> pengembalian ke dtype input, bukan setelahnya.</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> Disalin</div><!----></button><!----></div> <pre class="language-diff "><!----> from ..llama.modeling_llama import LlamaRMSNorm | |
| class Olmo2RMSNorm(LlamaRMSNorm): | |
| def forward(self, hidden_states): | |
| input_dtype = hidden_states.dtype | |
| hidden_states = hidden_states.to(torch.float32) | |
| variance = hidden_states.pow(2).mean(-1, keepdim=True) | |
| hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) | |
| <span class="hljs-deletion">- return self.weight * hidden_states.to(input_dtype)</span> | |
| <span class="hljs-addition">+ return (self.weight * hidden_states).to(input_dtype)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="attention" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#attention"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Perhatian<!----></span></h3><!--]--><!----> <p>Atensi Olmo2 identik dengan Olmo, kecuali ia menerapkan <code>RMSNorm</code> pada query dan key, serta menghapus qkv clipping. <code>super().__init__(...)</code> menyalin body induk dan menambahkan dua baris norm baru. <code>forward</code> didefinisikan ulang sepenuhnya karena query dan key kini melewati norm sebelum proyeksi. Linter juga menarik semua fungsi yang diimpor ke dalam file yang dihasilkan, termasuk <code>apply_rotary_pos_emb</code>, <code>eager_attention_forward</code>, dan dependensinya.</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> Disalin</div><!----></button><!----></div> <pre class="language-diff "><!----> class Olmo2Attention(OlmoAttention): | |
| def __init__(self, config: Olmo2Config, layer_idx: int | None = None): | |
| super().__init__(config, layer_idx=layer_idx) | |
| <span class="hljs-addition">+ self.q_norm = Olmo2RMSNorm(config.num_attention_heads * self.head_dim, config.rms_norm_eps)</span> | |
| <span class="hljs-addition">+ self.k_norm = Olmo2RMSNorm(config.num_key_value_heads * self.head_dim, config.rms_norm_eps)</span> | |
| def forward(self, ...): | |
| ... | |
| <span class="hljs-deletion">- query_states = self.q_proj(hidden_states)</span> | |
| <span class="hljs-deletion">- key_states = self.k_proj(hidden_states)</span> | |
| <span class="hljs-addition">+ query_states = self.q_norm(self.q_proj(hidden_states))</span> | |
| <span class="hljs-addition">+ key_states = self.k_norm(self.k_proj(hidden_states))</span> | |
| value_states = self.v_proj(hidden_states) | |
| <span class="hljs-deletion">- if self.config.clip_qkv is not None:</span> | |
| <span class="hljs-deletion">- query_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)</span> | |
| <span class="hljs-deletion">- key_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)</span> | |
| <span class="hljs-deletion">- value_states.clamp_(min=-self.config.clip_qkv, max=self.config.clip_qkv)</span> | |
| <span class="hljs-deletion">-</span> | |
| ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="decoderlayer" 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="#decoderlayer"><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><!---->DecoderLayer<!----></span></h3><!--]--><!----> <p>Setelah <code>super().__init__(...)</code>, timpa atribut norm dengan instance <code>Olmo2RMSNorm</code> dan tetapkan ulang <code>self.self_attn</code> ke kelas <code>Olmo2Attention</code> yang baru. Perintah <code>del self.input_layernorm</code> menghapus penugasan <code>input_layernorm</code> dari kelas induk karena Olmo2 menerapkan norm <em>setelah</em> attention, bukan sebelumnya. Lihat <a href="#removing-attributes">Menghapus atribut</a> untuk detail tentang apa yang dihapus dan tidak dihapus oleh <code>del</code>.</p> <p><code>forward</code> ditulis ulang untuk mencerminkan penempatan norm pasca-attention. Penulisan ulang <code>forward</code> hanya diperlukan ketika sebuah atribut diganti namanya, bukan ketika hanya tipenya yang berubah.</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> Disalin</div><!----></button><!----></div> <pre class="language-diff "><!----> class Olmo2DecoderLayer(OlmoDecoderLayer): | |
| def __init__(self, config: Olmo2Config, layer_idx: int): | |
| super().__init__(config, layer_idx=layer_idx) | |
| <span class="hljs-deletion">- self.self_attn = OlmoAttention(config=config, layer_idx=layer_idx)</span> | |
| <span class="hljs-deletion">- self.input_layernorm = OlmoLayerNorm(config.hidden_size)</span> | |
| <span class="hljs-deletion">- self.post_attention_layernorm = OlmoLayerNorm(config.hidden_size)</span> | |
| <span class="hljs-addition">+ self.self_attn = Olmo2Attention(config=config, layer_idx=layer_idx)</span> | |
| <span class="hljs-addition">+ del self.input_layernorm</span> | |
| <span class="hljs-addition">+ self.post_attention_layernorm = Olmo2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)</span> | |
| <span class="hljs-addition">+ self.post_feedforward_layernorm = Olmo2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)</span> | |
| def forward(self, ...): | |
| residual = hidden_states | |
| <span class="hljs-deletion">- hidden_states = self.input_layernorm(hidden_states)</span> | |
| # Self Attention | |
| hidden_states, _ = self.self_attn(...) | |
| <span class="hljs-deletion">- hidden_states = residual + hidden_states</span> | |
| <span class="hljs-addition">+ hidden_states = self.post_attention_layernorm(hidden_states)</span> | |
| <span class="hljs-addition">+ hidden_states = residual + hidden_states</span> | |
| # Fully Connected | |
| residual = hidden_states | |
| <span class="hljs-deletion">- hidden_states = self.post_attention_layernorm(hidden_states)</span> | |
| hidden_states = self.mlp(hidden_states) | |
| <span class="hljs-deletion">- hidden_states = residual + hidden_states</span> | |
| <span class="hljs-addition">+ hidden_states = self.post_feedforward_layernorm(hidden_states)</span> | |
| <span class="hljs-addition">+ hidden_states = residual + hidden_states</span> | |
| return hidden_states<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="model" 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"><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><!---->Model<!----></span></h3><!--]--><!----> <p>Hanya tipe dari <code>self.norm</code> yang berubah di sini. Metode <code>forward</code> identik dengan milik kelas induk, sehingga linter akan membawanya secara otomatis.</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> Disalin</div><!----></button><!----></div> <pre class="language-diff "><!----> class Olmo2Model(OlmoModel): | |
| def __init__(self, config: Olmo2Config): | |
| super().__init__(config) | |
| <span class="hljs-deletion">- self.layers = nn.ModuleList(</span> | |
| <span class="hljs-deletion">- [OlmoDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]</span> | |
| <span class="hljs-deletion">- )</span> | |
| <span class="hljs-deletion">- self.norm = OlmoLayerNorm(config.hidden_size)</span> | |
| <span class="hljs-addition">+ self.norm = Olmo2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)</span> | |
| <span class="hljs-addition">+ self.layers = nn.ModuleList(</span> | |
| <span class="hljs-addition">+ [Olmo2DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]</span> | |
| <span class="hljs-addition">+ )</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="model-head" 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-head"><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><!---->Kepala model<!----></span></h3><!--]--><!----> <p>Logikanya identik dengan <a href="/docs/transformers/main/id/model_doc/olmo#transformers.OlmoForCausalLM">OlmoForCausalLM</a>, sehingga tidak diperlukan perubahan.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ..olmo.modeling_olmo <span class="hljs-keyword">import</span> OlmoForCausalLM | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">Olmo2ForCausalLM</span>(<span class="hljs-title class_ inherited__">OlmoForCausalLM</span>): | |
| <span class="hljs-keyword">pass</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="other-classes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#other-classes"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Kelas lainnya<!----></span></h3><!--]--><!----> <p><a href="https://github.com/huggingface/transformers/blob/main/src/transformers/models/olmo2/modeling_olmo2.py" rel="nofollow">modeling_olmo2.py</a> yang dihasilkan oleh linter juga berisi kelas (<code>Olmo2MLP</code>, <code>Olmo2RotaryEmbedding</code>, <code>Olmo2PreTrainedModel</code>) yang tidak didefinisikan secara eksplisit di <code>modular_olmo2.py</code>.</p> <p>Linter menarik kelas mana pun yang bergantung pada kelas yang diwarisi, kecuali Anda mendefinisikannya ulang secara eksplisit. Fungsi yang diimpor seperti <code>apply_rotary_pos_emb</code> mengikuti aturan yang sama.</p> <p>Sebagai contoh, <code>OlmoDecoderLayer</code> memiliki <code>self.mlp = OlmoMLP(config)</code>. <code>Olmo2MLP</code> tidak pernah didefinisikan dalam file modular, sehingga linter membuatnya secara otomatis, setara dengan menggunakan <code>pass</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ..olmo.modeling_olmo <span class="hljs-keyword">import</span> OlmoMLP | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">Olmo2MLP</span>(<span class="hljs-title class_ inherited__">OlmoMLP</span>): | |
| <span class="hljs-keyword">pass</span><!----></pre></div><!----> <p>Jika Anda ingin <code>Olmo2MLP</code> mewarisi dari model lain, nyatakan secara eksplisit.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># switch to Mistral definition</span> | |
| <span class="hljs-keyword">from</span> ..mistral.modeling_mistral <span class="hljs-keyword">import</span> MistralMLP | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">Olmo2MLP</span>(<span class="hljs-title class_ inherited__">MistralMLP</span>): | |
| <span class="hljs-keyword">pass</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="finishing-the-file" 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="#finishing-the-file"><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><!---->Menyelesaikan file<!----></span></h3><!--]--><!----> <p>Setiap file modular harus mendeklarasikan <code>logger</code> dan daftar <code>__all__</code> pada level modul.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->logger = logging.get_logger(__name__) | |
| __all__ = [ | |
| <span class="hljs-string">"Olmo2Config"</span>, | |
| <span class="hljs-string">"Olmo2ForCausalLM"</span>, | |
| <span class="hljs-string">"Olmo2Model"</span>, | |
| <span class="hljs-string">"Olmo2PreTrainedModel"</span>, | |
| ]<!----></pre></div><!----> <p><code>__all__</code> harus mencantumkan setiap kelas publik dalam file tersebut. Konverter dan impor hilir bergantung padanya. Kelas yang tidak ada di <code>__all__</code> tidak akan diekspor dengan benar.</p> <!--[1--><h2 class="relative group"><a id="generate-the-modeling-files" 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="#generate-the-modeling-files"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Generate file pemodelan<!----></span></h2><!--]--><!----> <p>Skrip <code>modular_model_converter.py</code> menghasilkan file <code>modeling.py</code>, <code>configuration.py</code>, dan file lainnya secara mandiri dari file modular Anda. Untuk setiap class yang diwarisi, skrip ini menyalin isi parent ke child, mengubah nama semua referensi agar sesuai dengan model baru, serta menarik fungsi atau class helper yang dibutuhkan oleh parent tersebut.</p> <p>File output tidak mengandung impor lintas-model maupun pewarisan dari direktori model lain. Linter meratakan pewarisan menjadi satu tingkat. Jika <code>Olmo2Attention</code> mewarisi dari <code>OlmoAttention</code>, maka <code>Olmo2Attention</code> yang dihasilkan sepenuhnya mandiri. Namun, jika <code>OlmoAttention</code> sendiri mewarisi dari sesuatu yang lain, linter tidak akan meng-inline kakek kelas tersebut.</p> <p>Jalankan perintah di bawah untuk menghasilkan file dari file modular.</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> Disalin</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/modular_model_converter.py your_model<!----></pre></div><!----> <p>Jangan pernah mengedit file yang dihasilkan secara langsung karena semua perubahan akan ditimpa pada eksekusi berikutnya.</p> <!--[1--><h2 class="relative group"><a id="patterns-for-modular-files" 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="#patterns-for-modular-files"><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><!---->Pola untuk file modular<!----></span></h2><!--]--><!----> <p>Bagian-bagian di bawah ini mendokumentasikan pola penggunaan umum, seperti menghapus atribut atau menimpa metode yang didekorasi, saat bekerja dengan file modular.</p> <!--[2--><h3 class="relative group"><a id="removing-attributes" 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="#removing-attributes"><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><!---->Menghapus atribut<!----></span></h3><!--]--><!----> <p>Menghapus atribut yang diwarisi bergantung pada apakah Anda bekerja dengan kelas config atau subclass <code>nn.Module</code>.</p> <p>Untuk kelas konfigurasi, tetapkan <code>AttributeError()</code> ke atribut pada level kelas.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewConfig</span>(<span class="hljs-title class_ inherited__">ParentConfig</span>): | |
| removed_attr = AttributeError()<!----></pre></div><!----> <p>Linter menghapus deklarasi atribut secara keseluruhan dari file config yang dihasilkan. Kelas config menggunakan tata letak bergaya dataclass tanpa <code>__init__</code>, sehingga penugasan <code>AttributeError()</code> pada level kelas adalah pendekatan yang benar.</p> <p>Untuk subclass <code>nn.Module</code>, gunakan <code>del self.attribute</code> setelah <code>super().__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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewModel</span>(<span class="hljs-title class_ inherited__">ParentModel</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config: MyNewConfig</span>): | |
| <span class="hljs-built_in">super</span>().__init__(config) | |
| <span class="hljs-keyword">del</span> <span class="hljs-variable language_">self</span>.attribute<!----></pre></div><!----> <p><code>del self.attribute</code> hanya menghapus baris penugasan <code>self.attribute = ...</code> dari tubuh induk yang disalin. Baris lain yang merujuk ke <code>self.attribute</code> tidak dihapus. Jika metode <code>forward</code> atau metode lain pada induk juga merujuk ke atribut tersebut, override metode-metode itu juga.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">DummyModel</span>(nn.Module): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config: DummyConfig</span>): | |
| <span class="hljs-built_in">super</span>().__init__() | |
| <span class="hljs-variable language_">self</span>.attribute = config.attribute | |
| <span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.attribute: | |
| <span class="hljs-comment"># do more stuff with `self.attribute` here</span> | |
| ... | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewDummyModel</span>(<span class="hljs-title class_ inherited__">DummyModel</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config: MyNewDummyConfig</span>): | |
| <span class="hljs-built_in">super</span>().__init__(config) | |
| <span class="hljs-keyword">del</span> <span class="hljs-variable language_">self</span>.attribute | |
| <span class="hljs-comment"># 'self.attribute = config.attribute' is removed, but the 'if self.attribute:' block remains.</span> | |
| <span class="hljs-comment"># Override forward() or any other method that references self.attribute.</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="working-with-super" 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="#working-with-super"><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><!---->Bekerja dengan super()<!----></span></h3><!--]--><!----> <p><code>super().__init__(config)</code> memberi tahu konverter untuk menyalin isi kelas induk ke kelas turunan. Ada dua pola yang memungkinkan Anda menimpa perilaku ini.</p> <ul><li>Panggil kelas induk tertentu secara langsung ketika Anda membutuhkan output yang dihasilkan untuk memanggil kelas kakek (<code>nn.Module.__init__</code>) alih-alih kelas induk modular.</li> <li>Gunakan <code>**super_kwargs</code> untuk mewarisi tanda tangan penuh metode induk sambil menambahkan docstring kustom atau mengganti dekorator.</li></ul> <!--[3--><h4 class="relative group"><a id="call-a-grandparent-class-directly" 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="#call-a-grandparent-class-directly"><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><!---->Panggil kelas kakek-nenek secara langsung<!----></span></h4><!--]--><!----> <p>Jelaskan secara eksplisit kelas mana yang Anda panggil ketika Anda membutuhkan <code>super()</code> untuk menargetkan kelas induk yang dihasilkan, bukan kelas induk modular. Contoh di bawah ini memanggil <code>nn.Module.__init__(self)</code> secara langsung. <code>DummyModule</code> itu sendiri adalah <code>nn.Module</code>, sehingga konverter menuliskannya sebagai <code>super().__init__()</code> di <code>MyNewDummyModule</code> yang dihasilkan.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewDummyModule</span>(<span class="hljs-title class_ inherited__">DummyModule</span>): | <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyNewDummyModule</span>(nn.Module): | |
| | | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>): | <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>): | |
| nn.Module.__init__(<span class="hljs-variable language_">self</span>) | <span class="hljs-built_in">super</span>().__init__() | |
| <span class="hljs-variable language_">self</span>.foo = config.foo | <span class="hljs-variable language_">self</span>.foo = config.foo | |
| ... | ...<!----></pre></div><!----> <!--[3--><h4 class="relative group"><a id="superkwargs" 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="#superkwargs"><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><!---->super_kwargs<!----></span></h4><!--]--><!----> <p>Gunakan <code>**super_kwargs</code> untuk mewarisi tanda tangan penuh metode induk sambil menambahkan docstring kustom atau mengganti dekorator. Pada tanda tangan yang dioverride, ini memberi tahu linter untuk memperluas semua argumen induk dalam output yang dihasilkan.</p> <p>Penggunaan yang paling umum adalah menambahkan docstring yang spesifik untuk model, seperti mendokumentasikan argumen <code>labels</code>, tanpa menulis ulang seluruh signature.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># modular_gemma.py</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">GemmaForCausalLM</span>(<span class="hljs-title class_ inherited__">LlamaForCausalLM</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">**super_kwargs</span>): | |
| <span class="hljs-string">r""" | |
| Example: | |
| ```python | |
| >>> from transformers import AutoTokenizer, GemmaForCausalLM | |
| >>> model = GemmaForCausalLM.from_pretrained("google/gemma-7b") | |
| ... | |
| ```"""</span> | |
| <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>().forward(**super_kwargs)<!----></pre></div><!----> <p><code>GemmaForCausalLM.forward</code> yang dihasilkan memiliki tanda tangan <code>LlamaForCausalLM</code> lengkap tanpa perlu menyalin secara manual.</p> <p><code>**super_kwargs</code> adalah jalan pintas untuk kasus-kasus khusus. Jika Anda mengubah perilaku, tulislah tanda tangan lengkapnya sebagai gantinya.</p> <!--[2--><h3 class="relative group"><a id="deleting-unused-methods" 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="#deleting-unused-methods"><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><!---->Menghapus metode yang tidak digunakan<!----></span></h3><!--]--><!----> <p>Hapus metode induk dengan menimpanya menggunakan pernyataan <code>raise AttributeError("")</code>. Linter akan menghapus metode tersebut dari file yang dihasilkan.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">GemmaTokenizer</span>(<span class="hljs-title class_ inherited__">LlamaTokenizer</span>): | |
| ... | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">get_spm_processor</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">raise</span> AttributeError(<span class="hljs-string">"Not needed for Gemma"</span>) | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">unk_token_length</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">raise</span> AttributeError(<span class="hljs-string">"Not needed for Gemma"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="overriding-decorated-methods" 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="#overriding-decorated-methods"><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><!---->Menimpa metode yang dihias<!----></span></h3><!--]--><!----> <p>Saat Anda menimpa metode induk yang sudah diberi dekorator, dekorator dari metode induk tersebut akan diterapkan secara otomatis. Jika Anda menambahkan dekorator Anda sendiri, dekorator tersebut akan menggantikan dekorator dari metode induk.</p> <p>Dua dekorator muncul di seluruh pustaka, satu untuk <a href="./model_output_tracing">mengambil output antara model</a> dan satu lagi untuk <a href="./auto_docstring">menghasilkan docstring secara otomatis</a>.</p> <p>Pada contoh di bawah, sebuah subclass menimpa (override) metode parent yang sudah diberi dekorator tanpa menambahkan dekorator miliknya sendiri. Dekorator dari parent tersebut tetap berlaku.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">NewModel</span>(<span class="hljs-title class_ inherited__">DummyModel</span>): | <span class="hljs-keyword">class</span> <span class="hljs-title class_">NewModel</span>(nn.Module): | |
| ... | ... | |
| | | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">...</span>): | @decorator(...) | |
| ... | <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">...</span>): | |
| | ...<!----></pre></div><!----> <p>Jika Anda menambahkan dekorator baru, dekorator Anda akan menggantikan dekorator induknya.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">NewModel</span>(<span class="hljs-title class_ inherited__">DummyModel</span>): | <span class="hljs-keyword">class</span> <span class="hljs-title class_">NewModel</span>(nn.Module): | |
| ... | ... | |
| | | |
| <span class="hljs-meta"> @my_new_decorator(<span class="hljs-params">...</span>) | @my_new_decorator(<span class="hljs-params">...</span>)</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">...</span>): | <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">...</span>): | |
| ... | ...<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="special-naming" 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="#special-naming"><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><!---->Penamaan khusus<!----></span></h3><!--]--><!----> <p>Linter secara otomatis mengganti nama semua elemen saat mewarisi dari sebuah kelas. Gunakan prefiks nama kelas yang sama di semua kelas dalam file yang sama.</p> <p>Hindari mencampurkan awalan seperti pada contoh di bawah. <code>MyModelIncredibleMLP</code> melanggar konvensi penamaan, dan linter tidak akan tahu apakah harus menggunakan <code>MyModelIncredible</code> atau <code>MyModel</code> saat mengubah nama dependensi tingkat lebih tinggi.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelIncredibleMLP</span>(<span class="hljs-title class_ inherited__">LlamaMLP</span>): | |
| ... | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelDecoderLayer</span>(<span class="hljs-title class_ inherited__">LlamaDecoderLayer</span>): | |
| ...<!----></pre></div><!----> <p>Tanpa <a href="#other-classes">dependensi implisit</a>, Anda dapat mengganti nama satu kelas secara lokal. Definisikan ulang secara eksplisit setiap penyebutan lain dari kelas tersebut dengan pola nama baru. Jika tidak, linter akan menambahkan kelas <code>MyModelMLP</code> yang tidak diinginkan di samping <code>MyModelIncredibleMLP</code>.</p> <p>Linter mengeluarkan peringatan ketika mendeteksi awalan yang ambigu.</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> Disalin</div><!----></button><!----></div> <pre class="language-text "><!---->We detected multiple prefix names when inheriting from transformers.models.llama.modeling_llama: ('Emu3Text', 'Emu3'). We will only use the most used 'Emu3' prefix when grabbing args and dependencies. Make sure to subclass the intermediate classes with the prefix you want (if different from 'Emu3') or use a single prefix in all the modular (best).<!----></pre></div><!----> <p>Prefiks ambigu paling umum terjadi pada model multimodal di mana nama kelas mencakup kualifikasi modalitas seperti <code>Text</code>. Untuk memberikan prefiks tertentu pada sebuah dependensi, ubah namanya secara eksplisit dengan <code>pass</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">Emu3TextMLP</span>(<span class="hljs-title class_ inherited__">LlamaMLP</span>): | |
| <span class="hljs-keyword">pass</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="config-docstrings" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#config-docstrings"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Docstring konfigurasi<!----></span></h3><!--]--><!----> <p>Linter belum mendukung pewarisan docstring parsial. Saat menambahkan atau menghapus atribut konfigurasi, tambahkan docstring lengkap secara langsung di file modular di bawah definisi kelas.</p> <!--[1--><h2 class="relative group"><a id="checkpoint-conversion" 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="#checkpoint-conversion"><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><!---->Konversi checkpoint<!----></span></h2><!--]--><!----> <p>Setelah Anda menghasilkan file pemodelan, pastikan bahwa bobot asli dimuat dengan benar. Tulis skrip konversi untuk menerjemahkan format checkpoint hulu menjadi format yang kompatibel dengan Transformers, lalu simpan ke Hub.</p> <!--[2--><h3 class="relative group"><a id="write-a-conversion-script" 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="#write-a-conversion-script"><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><!---->Tulis skrip konversi<!----></span></h3><!--]--><!----> <p>Tambahkan file <code>convert_<model>_to_hf.py</code> ke <code>src/transformers/models/<model>/</code>. Skrip ini memuat bobot upstream, mengubah nama dan bentuk kunci agar sesuai dengan nama parameter modul Anda, lalu menyimpan hasilnya dengan <a href="/docs/transformers/main/id/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a>.</p> <blockquote class="tip"><p>Cari skrip yang sudah ada untuk disalin dan diadaptasi. Model di bawah <code>src/transformers/models/</code> menyertakan <code>convert_*_to_hf.py</code> yang dapat Anda gunakan sebagai titik awal.</p></blockquote> <p>Setelah menjalankan skrip, muat checkpoint yang disimpan dengan <a href="/docs/transformers/main/id/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a> dan pastikan setiap bobot yang diharapkan dimuat dengan benar. Kunci checkpoint yang tidak digunakan menunjukkan nama yang tidak cocok, jadi cetak kunci tersebut untuk mendeteksi masalah sejak dini.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->model = YourModelForTask.from_pretrained(<span class="hljs-string">"path/to/output/"</span>)<!----></pre></div><!----> <p>Periksa kecocokan bentuk dan nama saat mengiterasi kunci. Ketidakcocokan bentuk biasanya berarti ada parameter di konfigurasi Anda yang salah, arsitektur berbeda dari aslinya, atau bobot perlu ditransposisikan.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">for</span> key, tensor <span class="hljs-keyword">in</span> original_state_dict.items(): | |
| hf_tensor = hf_model.state_dict().get(mapped_key) | |
| <span class="hljs-keyword">assert</span> hf_tensor.shape == tensor.shape, ( | |
| <span class="hljs-string">f"Shape mismatch for <span class="hljs-subst">{key}</span>: expected <span class="hljs-subst">{tensor.shape}</span>, got <span class="hljs-subst">{hf_tensor.shape}</span>"</span> | |
| )<!----></pre></div><!----> <p>Perbaiki semua masalah dengan mengulang proses antara file modular Anda, file modeling yang dihasilkan, dan skrip konversi hingga semua bobot dimuat dengan bersih.</p> <p>Setelah checkpoint dimuat dengan baik, unggah ke Hub menggunakan <a href="/docs/transformers/main/id/main_classes/model#transformers.utils.PushToHubMixin.push_to_hub">push_to_hub()</a>. Lihat panduan <a href="./model_sharing">model sharing</a> untuk detail lebih lanjut.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->model.push_to_hub(<span class="hljs-string">"username/your-model-name"</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="runtime-conversion-mapping" 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="#runtime-conversion-mapping"><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><!---->Pemetaan konversi runtime<!----></span></h3><!--]--><!----> <p>Tambahkan pemetaan runtime ke <code>src/transformers/conversion_mapping.py</code> ketika bobot yang dipublikasikan tidak sesuai dengan tata letak parameter modul Anda. Kasus umum meliputi bobot terfusi yang disimpan secara terpisah dan tensor ahli MoE yang perlu ditumpuk. Pemetaan ini memungkinkan <a href="/docs/transformers/main/id/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a> memuat checkpoint Hub tanpa langkah ekspor terpisah.</p> <p>Lihat panduan <a href="./weightconverter">dynamic weight loading</a> untuk cara menulis aturan <code>WeightRenaming</code> dan <a href="/docs/transformers/main/id/internal/modeling_utils#transformers.WeightConverter">WeightConverter</a> serta mendaftarkannya untuk <code>model_type</code> Anda.</p> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Langkah selanjutnya<!----></span></h2><!--]--><!----> <ul><li><a href="./modeling_rules">Aturan struktur model</a> adalah aturan statis yang diterapkan pada semua file <code>modeling_*.py</code>, <code>modular_*.py</code>, dan <code>configuration_*.py</code>. Jalankan <code>make typing</code> untuk memeriksanya sebelum membuka PR.</li> <li><a href="./add_vision_processing_components">Menambahkan komponen pemrosesan visual</a> membahas cara menambahkan image processor, video processor, dan processor untuk model multimodal.</li> <li><a href="./auto_docstring">Auto-generating docstrings</a> menunjukkan cara menggunakan <code>@auto_docstring</code> agar Anda tidak perlu menulis dokumentasi argumen secara manual untuk API model yang dibagikan.</li> <li><a href="./testing">Menulis pengujian model</a> membahas cara menulis pengujian integrasi untuk model baru Anda dan menjalankannya secara lokal.</li> <li><a href="./pr_checks">Periksa pull request</a> menjelaskan pemeriksaan CI yang harus dilewati PR Anda sebelum dapat digabungkan, serta cara mereproduksi dan memperbaikinya secara lokal.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/modular_transformers.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_1m6yd5j = { | |
| base: "/docs/transformers/main/en", | |
| assets: "/docs/transformers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"), | |
| import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 675], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 109 kB
- Xet hash:
- 66e802057b1ad101789614330c571affbdc38c7321564dfd102e6447dc679704
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.