Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Modular transformers","local":"modular-transformers","sections":[{"title":"What is it?","local":"what-is-it","sections":[{"title":"Details","local":"details","sections":[],"depth":3},{"title":"Enforcement","local":"enforcement","sections":[],"depth":3},{"title":"Examples","local":"examples","sections":[],"depth":3}],"depth":2},{"title":"What it is not","local":"what-it-is-not","sections":[],"depth":2}],"depth":1}"> | |
| <link href="/docs/transformers/pr_33686/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/entry/start.353d2327.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/scheduler.25b97de1.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/singletons.7c4973e1.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/index.e188933d.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/paths.0de49938.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/entry/app.045023f1.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/index.d9030fc9.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/nodes/0.f5f21eaa.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/each.e59479a4.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/nodes/363.a00e33b6.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/CodeBlock.e6cd0d95.js"> | |
| <link rel="modulepreload" href="/docs/transformers/pr_33686/en/_app/immutable/chunks/EditOnGithub.91d95064.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{"title":"Modular transformers","local":"modular-transformers","sections":[{"title":"What is it?","local":"what-is-it","sections":[{"title":"Details","local":"details","sections":[],"depth":3},{"title":"Enforcement","local":"enforcement","sections":[],"depth":3},{"title":"Examples","local":"examples","sections":[],"depth":3}],"depth":2},{"title":"What it is not","local":"what-it-is-not","sections":[],"depth":2}],"depth":1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <h1 class="relative group"><a id="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="#modular-transformers"><span><svg class="" 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>Modular transformers</span></h1> <p data-svelte-h="svelte-1qsl2az"><code>transformers</code> is an opinionated framework; our philosophy is defined in the following <a href="./philosophy">conceptual guide</a>.</p> <p data-svelte-h="svelte-1qcwnc7">The core of that philosophy is exemplified by the <a href="https://huggingface.co/blog/transformers-design-philosophy" rel="nofollow">single model, single file</a> | |
| aspect of the library. This component’s downside is that it limits the inheritance and importability of components from | |
| files to others in the toolkit.</p> <p data-svelte-h="svelte-hcbv0p">As a result, model components tend to be repeated across many files. There are as many attention layers defined | |
| in <code>transformers</code> as there are models, and a significant number of those are identical to each other. | |
| The unfortunate consequence is that independent implementations tend to diverge as fixes and changes get applied | |
| to specific parts of the code.</p> <p data-svelte-h="svelte-14lpvvv">In order to balance this issue, we introduced the concept of “copies” across the library. By adding a comment indicating | |
| that code is a copy of another, we can enforce through CI and local commands that copies do not diverge. However, | |
| while the complexity is low, this is often quite tedious to do.</p> <p data-svelte-h="svelte-1tehds2">And, finally, this contributes to adding a significant overhead to contributing models which we would like to remove. | |
| This approach often requires model contributions to add modeling code (~1k lines), processor (~500 lines), tests, docs, | |
| etc. Model contribution PRs rarely add less than 3-5k lines of code, with much of this code being boilerplate.</p> <p data-svelte-h="svelte-5u9x31">This raises the bar for contributions, and with Modular Transformers, we’re aiming to lower the bar to a much more | |
| acceptable point.</p> <h2 class="relative group"><a id="what-is-it" 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="#what-is-it"><span><svg class="" 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>What is it?</span></h2> <p data-svelte-h="svelte-1qqnnub">Modular Transformers introduces the concept of a “modular” file to a model folder. This modular file accepts code | |
| that isn’t typically accepted in modeling/processing files, as it allows importing from neighbouring models as well | |
| as inheritance from classes to others.</p> <p data-svelte-h="svelte-huwsbv">This modular file defines models, processors, and the configuration class that would otherwise be defined in their | |
| respective modules.</p> <p data-svelte-h="svelte-dk7wye">Finally, this feature introduces a new <code>linter</code> which will “unravel” the modular file into the “single model, single | |
| file” directory structure. These files will get auto-generated every time the script is run; reducing the required | |
| contributions to the modular file, and therefore only to the changes between the contributed model and others.</p> <p data-svelte-h="svelte-12pm85e">Model users will end up importing and using the single-file interface, so no change is expected here. Doing this, we | |
| hope to combine the best of both worlds: enabling simple contributions while sticking to our philosophy.</p> <p data-svelte-h="svelte-ah1dr9">This is therefore a replacement for the <code># Copied from</code> markers, and previously contributed models can be expected to | |
| be moved to the new Modular Transformers format in the coming months.</p> <h3 class="relative group"><a id="details" 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="#details"><span><svg class="" 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>Details</span></h3> <p data-svelte-h="svelte-xkvkhs">The “linter”, which unravels the inheritance and creates all single-files from the modular file, will flatten the | |
| inheritance while trying to be invisible to Python users. At this time, the linter flattens a <strong>single</strong> level of | |
| inheritance.</p> <p data-svelte-h="svelte-1gkqha7">For example:</p> <ul data-svelte-h="svelte-1ajvdmd"><li>If a configuration class inherits from another and adds/deletes an argument, the generated file will either directly | |
| reference it (in case of addition) or completely remove it (in case of deletion).</li> <li>If a class inherits from another, for example: class GemmaModel(LlamaModel):, dependencies are automatically | |
| inferred. All submodules will be automatically inferred from the superclass.</li></ul> <p data-svelte-h="svelte-12b6859">You should be able to write everything (the tokenizer, the image processor, the model, the config) in this <code>modular</code> | |
| file, and the corresponding files will be created for you.</p> <h3 class="relative group"><a id="enforcement" 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="#enforcement"><span><svg class="" 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>Enforcement</span></h3> <p data-svelte-h="svelte-drmr0e">[TODO] We are introducing a new test, that makes sure the generated content matches what is present in the <code>modular_xxxx.py</code></p> <h3 class="relative group"><a id="examples" 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="#examples"><span><svg class="" 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>Examples</span></h3> <p data-svelte-h="svelte-1o8u693">Here is a quick example with BERT and RoBERTa. The two models are intimately related: their modeling implementation | |
| differs solely by a change in the embedding layer.</p> <p data-svelte-h="svelte-si4m91">Instead of redefining the model entirely, here is what the <code>modular_roberta.py</code> file looks like for the modeling & | |
| configuration classes (for the sake of the example, the tokenizer is ignored at this time as very different).</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 class="" 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> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> torch <span class="hljs-keyword">import</span> nn | |
| <span class="hljs-keyword">from</span> ..bert.configuration_bert <span class="hljs-keyword">import</span> BertConfig | |
| <span class="hljs-keyword">from</span> ..bert.modeling_bert <span class="hljs-keyword">import</span> ( | |
| BertModel, | |
| BertEmbeddings, | |
| BertForMaskedLM | |
| ) | |
| <span class="hljs-comment"># The RoBERTa config is identical to BERT's config</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">RobertaConfig</span>(<span class="hljs-title class_ inherited__">BertConfig</span>): | |
| model_type = <span class="hljs-string">'roberta'</span> | |
| <span class="hljs-comment"># We redefine the embeddings here to highlight the padding ID difference, and we redefine the position embeddings</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">RobertaEmbeddings</span>(<span class="hljs-title class_ inherited__">BertEmbeddings</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>): | |
| <span class="hljs-built_in">super</span>().__init__(config()) | |
| self.padding_idx = config.pad_token_id | |
| self.position_embeddings = nn.Embedding( | |
| config.max_position_embeddings, config.hidden_size, padding_idx=self.padding_idx | |
| ) | |
| <span class="hljs-comment"># The RoBERTa model is identical to the BERT model, except for the embedding layer. </span> | |
| <span class="hljs-comment"># We redefine the embeddings above, so here there is no need to do additional work</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">RobertaModel</span>(<span class="hljs-title class_ inherited__">BertModel</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>): | |
| <span class="hljs-built_in">super</span>().__init__(config) | |
| self.embeddings = RobertaEmbeddings(config) | |
| <span class="hljs-comment"># The heads now only need to redefine the model inside to the correct `RobertaModel`</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">RobertaForMaskedLM</span>(<span class="hljs-title class_ inherited__">BertForMaskedLM</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>): | |
| <span class="hljs-built_in">super</span>().__init__(config) | |
| self.model = RobertaModel(config)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-tpa7s6">Note that if you do not use the dependency that you defined, you will have the following error:</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 class="" 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> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->ValueError: You defined `RobertaEmbeddings` <span class="hljs-keyword">in</span> the modular_roberta.py, it should be used | |
| when you define `BertModel`, as it is one of it<span class="hljs-string">'s direct dependencies. Make sure | |
| you use it in the `__init__` function.</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-834mi0">Additionally, you may find a list of examples here:</p> <h2 class="relative group"><a id="what-it-is-not" 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="#what-it-is-not"><span><svg class="" 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>What it is not</span></h2> <p data-svelte-h="svelte-1cj74u8">It is not a replacement for the modeling code (yet?), and if your model is not based on anything else that ever existed, then you can add a <code>modeling</code> file as usual.</p> <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"><span data-svelte-h="svelte-1kd6by1"><</span> <span data-svelte-h="svelte-x0xyl0">></span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p> | |
| <script> | |
| { | |
| __sveltekit_gh72n0 = { | |
| assets: "/docs/transformers/pr_33686/en", | |
| base: "/docs/transformers/pr_33686/en", | |
| env: {} | |
| }; | |
| const element = document.currentScript.parentElement; | |
| const data = [null,null]; | |
| Promise.all([ | |
| import("/docs/transformers/pr_33686/en/_app/immutable/entry/start.353d2327.js"), | |
| import("/docs/transformers/pr_33686/en/_app/immutable/entry/app.045023f1.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 363], | |
| data, | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 21.2 kB
- Xet hash:
- 880aae0b9c7c1f9a0e26cd85e7971490a2b21c8eb84945ccc1ce3bd73ea4461b
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.