Buckets:

download
raw
26.4 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Writing kernels&quot;,&quot;local&quot;:&quot;writing-kernels&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Two-class pattern&quot;,&quot;local&quot;:&quot;two-class-pattern&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Parameter transformation&quot;,&quot;local&quot;:&quot;parameter-transformation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Module fusion&quot;,&quot;local&quot;:&quot;module-fusion&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/transformers/pr_43838/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/entry/start.1bcc8411.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/scheduler.31fdf58d.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/singletons.49522dd1.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/index.252883d5.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/paths.91497448.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/entry/app.9d434a07.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/preload-helper.f02a9c49.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/index.2f76fdf0.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/nodes/0.b44e4c87.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/nodes/77.158d677d.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/CopyLLMTxtMenu.f658c69c.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.0d9e7a9c.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/IconCopy.ac192424.js">
<link rel="modulepreload" href="/docs/transformers/pr_43838/en/_app/immutable/chunks/CodeBlock.e52df5d6.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Writing kernels&quot;,&quot;local&quot;:&quot;writing-kernels&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Two-class pattern&quot;,&quot;local&quot;:&quot;two-class-pattern&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Parameter transformation&quot;,&quot;local&quot;:&quot;parameter-transformation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Module fusion&quot;,&quot;local&quot;:&quot;module-fusion&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div 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>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> </div> <h1 class="relative group"><a id="writing-kernels" 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="#writing-kernels"><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>Writing kernels</span></h1> <p data-svelte-h="svelte-1rztd32">This guide explains how to write kernels that go beyond a stateless <code>forward</code> replacement. It covers two capabilities the extended <code>KernelConfig</code> API supports:</p> <ol data-svelte-h="svelte-1xidunp"><li>Parameter transformation: the kernel expects weights in a different layout than the original model (for example, renamed or merged parameters).</li> <li>Module fusion: the kernel replaces multiple adjacent modules with a single fused implementation.</li></ol> <p data-svelte-h="svelte-1pfrnox">For basic kernels (stateless <code>forward</code> replacements with no parameter changes), see the <a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> library documentation.</p> <h2 class="relative group"><a id="two-class-pattern" 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="#two-class-pattern"><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>Two-class pattern</span></h2> <p data-svelte-h="svelte-n2hdw3">Any kernel that carries its own parameters follows a two-class pattern.</p> <ul data-svelte-h="svelte-286423"><li><code>KernelName</code>: contains only the <code>forward</code> pass. The <code>kernels</code> library uses this class to kernelize the model because it does not allow stateful kernel classes.</li> <li><code>KernelNameLayout</code>: an <code>nn.Module</code> that holds the parameters and monkey-patches the original module before the checkpoint is loaded. At runtime, <code>kernelize</code> replaces its <code>forward</code> with the <code>forward</code> from <code>KernelName</code>’. You do not need to define <code>forward</code>. Transformers injects one automatically with the same signature as <code>KernelName.forward</code>.</li></ul> <blockquote class="important" data-svelte-h="svelte-1m1u2ep"></blockquote> <p data-svelte-h="svelte-1xfk0tj">The naming convention is strict. The layout class must be named <code>{KernelName}Layout</code> and defined in the same module as <code>KernelName</code>.</p> <h2 class="relative group"><a id="parameter-transformation" 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="#parameter-transformation"><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>Parameter transformation</span></h2> <p data-svelte-h="svelte-1kb2uky">Use this pattern when the kernel expects weights under different names or in a different shape than the original model checkpoint.</p> <p data-svelte-h="svelte-w0rea7">The <code>KernelNameLayout</code> class has the same <code>__init__</code> signature as the module it replaces and declares a <code>conversion_mapping</code> class attribute that tells Transformers how to remap checkpoint keys to the new parameter names (see <a href="../weightconverter">Dynamic weight loading</a> for more details).</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="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.nn <span class="hljs-keyword">as</span> nn
<span class="hljs-keyword">class</span> <span class="hljs-title class_">CustomRMSNormLayout</span>(nn.Module):
conversion_mapping = [...] <span class="hljs-comment"># rules that remap checkpoint keys to the new parameter names</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, hidden_size: <span class="hljs-built_in">int</span>, eps: <span class="hljs-built_in">float</span> = <span class="hljs-number">1e-6</span></span>):
<span class="hljs-built_in">super</span>().__init__()
self.scale = nn.Parameter(torch.ones(hidden_size))
self.variance_epsilon = eps
<span class="hljs-keyword">class</span> <span class="hljs-title class_">CustomRMSNorm</span>(nn.Module):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, hidden_states: torch.Tensor</span>) -&gt; torch.Tensor:
input_dtype = hidden_states.dtype
hidden_states = hidden_states.to(torch.float32)
variance = hidden_states.<span class="hljs-built_in">pow</span>(<span class="hljs-number">2</span>).mean(-<span class="hljs-number">1</span>, keepdim=<span class="hljs-literal">True</span>)
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
<span class="hljs-keyword">return</span> self.scale * hidden_states.to(input_dtype)
<span class="hljs-keyword">class</span> <span class="hljs-title class_">layers</span>:
CustomRMSNorm = CustomRMSNorm<!-- HTML_TAG_END --></pre></div> <blockquote class="note" data-svelte-h="svelte-15jb5km"><p>The <code>layers</code> class is required by the <code>kernels</code> library to expose the kernel entry point.</p></blockquote> <p data-svelte-h="svelte-c62esv">Load this kernel by passing the repo and class name to <a href="/docs/transformers/pr_43838/en/main_classes/kernels#transformers.KernelConfig">KernelConfig</a>. The key is the original module class name from the model. The value points to the <code>KernelName</code> class (not the <code>Layout</code>) in the repo.</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="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, KernelConfig
kernel_config = KernelConfig({<span class="hljs-string">&quot;RMSNorm&quot;</span>: <span class="hljs-string">&quot;owner/my-kernel:CustomRMSNorm&quot;</span>})
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen3-0.6B&quot;</span>,
use_kernels=<span class="hljs-literal">True</span>,
kernel_config=kernel_config,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-sqg2cv">When the model loads, Transformers:</p> <ol data-svelte-h="svelte-13q7q27"><li>Loads <code>CustomRMSNorm</code> from the repo and looks for <code>CustomRMSNormLayout</code> in the same module.</li> <li>Monkey-patches every <code>RMSNorm</code> in the model with <code>CustomRMSNormLayout</code>.</li> <li>Remaps checkpoint weights using <code>conversion_mapping</code> so they load into the new parameter names.</li> <li>Calls <code>kernelize</code>, which replaces <code>CustomRMSNormLayout.forward</code> with <code>CustomRMSNorm.forward</code>.</li></ol> <h2 class="relative group"><a id="module-fusion" 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="#module-fusion"><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>Module fusion</span></h2> <p data-svelte-h="svelte-1fezffe">Use this pattern when a kernel replaces multiple adjacent modules with a single fused implementation. Because the fused module combines parameters from several original modules, the <code>KernelNameLayout.__init__</code> receives the instantiated child modules rather than their constructor arguments.</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="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.nn <span class="hljs-keyword">as</span> nn
<span class="hljs-keyword">class</span> <span class="hljs-title class_">RMSNormMLPLayout</span>(nn.Module):
conversion_mapping = [...] <span class="hljs-comment"># rules that remap checkpoint keys to the fused parameter names</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, norm, mlp</span>):
<span class="hljs-built_in">super</span>().__init__()
self.variance_epsilon = norm.variance_epsilon
self.scale = nn.Parameter(torch.empty_like(norm.weight))
self.gate_up_proj = nn.Linear(
mlp.gate_proj.in_features,
mlp.gate_proj.out_features + mlp.up_proj.out_features,
bias=mlp.gate_proj.bias <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>,
device=mlp.gate_proj.weight.device,
dtype=mlp.gate_proj.weight.dtype,
)
self.down_proj = nn.Linear(
mlp.down_proj.in_features,
mlp.down_proj.out_features,
bias=mlp.down_proj.bias <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>,
device=mlp.down_proj.weight.device,
dtype=mlp.down_proj.weight.dtype,
)
self.act_fn = mlp.act_fn
<span class="hljs-keyword">class</span> <span class="hljs-title class_">RMSNormMLP</span>(nn.Module):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, hidden_states: torch.Tensor</span>) -&gt; torch.Tensor:
input_dtype = hidden_states.dtype
hidden_states = hidden_states.to(torch.float32)
variance = hidden_states.<span class="hljs-built_in">pow</span>(<span class="hljs-number">2</span>).mean(-<span class="hljs-number">1</span>, keepdim=<span class="hljs-literal">True</span>)
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
hidden_states = self.scale * hidden_states.to(input_dtype)
gate, up = self.gate_up_proj(hidden_states).chunk(<span class="hljs-number">2</span>, dim=-<span class="hljs-number">1</span>)
<span class="hljs-keyword">return</span> self.down_proj(self.act_fn(gate) * up)
<span class="hljs-keyword">class</span> <span class="hljs-title class_">layers</span>:
RMSNormMLP = RMSNormMLP<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1gecwes">To fuse modules, pass a tuple of <code>(class_name, path_pattern)</code> pairs as the key in <code>KernelConfig</code> instead of a plain string. All patterns must share the same parent module (Transformers fuses the children in that parent). The <code>*</code> wildcard matches any single path segment.</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="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, KernelConfig
kernel_config = KernelConfig(
{
(
(<span class="hljs-string">&quot;RMSNorm&quot;</span>, <span class="hljs-string">&quot;model.layers.*.post_attention_layernorm&quot;</span>),
(<span class="hljs-string">&quot;MLP&quot;</span>, <span class="hljs-string">&quot;model.layers.*.mlp&quot;</span>),
): <span class="hljs-string">&quot;owner/my-kernel:RMSNormMLP&quot;</span>,
}
)
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen3-0.6B&quot;</span>,
use_kernels=<span class="hljs-literal">True</span>,
kernel_config=kernel_config,
device_map=<span class="hljs-string">&quot;cuda&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-sqg2cv">When the model loads, Transformers:</p> <ol data-svelte-h="svelte-5pb171"><li>Loads <code>RMSNormMLP</code> from the repo and finds <code>RMSNormMLPLayout</code> in the same module.</li> <li>Matches every decoder layer at <code>model.layers.*</code> and builds a fused parent class whose <code>__init__</code> calls <code>RMSNormMLPLayout(post_attention_layernorm, mlp)</code>.</li> <li>Replaces the remaining child (<code>mlp</code>) with <code>nn.Identity()</code> to preserve the parent module’s interface.</li> <li>Remaps checkpoint weights using <code>conversion_mapping</code>.</li> <li>Calls <code>kernelize</code>, which replaces <code>RMSNormMLPLayout.forward</code> with <code>RMSNormMLP.forward</code>.</li></ol> <blockquote class="tip" data-svelte-h="svelte-t3mlbk"><p>The order of pairs in the fusion tuple determines the argument order passed to <code>KernelNameLayout.__init__</code>.</p></blockquote> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/kernel_doc/writing_kernels.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 data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_1o1ef6h = {
assets: "/docs/transformers/pr_43838/en",
base: "/docs/transformers/pr_43838/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/transformers/pr_43838/en/_app/immutable/entry/start.1bcc8411.js"),
import("/docs/transformers/pr_43838/en/_app/immutable/entry/app.9d434a07.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 77],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
26.4 kB
·
Xet hash:
d06d77c89bdc7ec0ad9f1b41573cf0aeaa41ec96099213043c18bcfe0a7b15a9

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