Buckets:

HuggingFaceDocBuilder's picture
download
raw
31.8 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Orthogonal Finetuning (OFT and BOFT)&quot;,&quot;local&quot;:&quot;orthogonal-finetuning-oft-and-boft&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Merge OFT/BOFT weights into the base model&quot;,&quot;local&quot;:&quot;merge-oftboft-weights-into-the-base-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Utils for OFT / BOFT&quot;,&quot;local&quot;:&quot;utils-for-oft--boft&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Common OFT / BOFT parameters in PEFT&quot;,&quot;local&quot;:&quot;common-oft--boft-parameters-in-peft&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;OFT-specific parameters&quot;,&quot;local&quot;:&quot;oft-specific-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;BOFT-specific parameters&quot;,&quot;local&quot;:&quot;boft-specific-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;OFT Example Usage&quot;,&quot;local&quot;:&quot;oft-example-usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;BOFT Example Usage&quot;,&quot;local&quot;:&quot;boft-example-usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/peft/pr_3206/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/entry/start.06e1cdbd.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/scheduler.78382b47.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/singletons.25ed789f.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/index.fadd215c.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/paths.d98268af.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/entry/app.cb2a6689.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/preload-helper.1df7c689.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/index.6dd35eb6.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/nodes/0.fa4b1245.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/nodes/6.878bf7e8.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.db10b59f.js">
<link rel="modulepreload" href="/docs/peft/pr_3206/en/_app/immutable/chunks/CodeBlock.e9241c92.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Orthogonal Finetuning (OFT and BOFT)&quot;,&quot;local&quot;:&quot;orthogonal-finetuning-oft-and-boft&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Merge OFT/BOFT weights into the base model&quot;,&quot;local&quot;:&quot;merge-oftboft-weights-into-the-base-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Utils for OFT / BOFT&quot;,&quot;local&quot;:&quot;utils-for-oft--boft&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Common OFT / BOFT parameters in PEFT&quot;,&quot;local&quot;:&quot;common-oft--boft-parameters-in-peft&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;OFT-specific parameters&quot;,&quot;local&quot;:&quot;oft-specific-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;BOFT-specific parameters&quot;,&quot;local&quot;:&quot;boft-specific-parameters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;OFT Example Usage&quot;,&quot;local&quot;:&quot;oft-example-usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;BOFT Example Usage&quot;,&quot;local&quot;:&quot;boft-example-usage&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="orthogonal-finetuning-oft-and-boft" 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="#orthogonal-finetuning-oft-and-boft"><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>Orthogonal Finetuning (OFT and BOFT)</span></h1> <p data-svelte-h="svelte-d6gmtl">This conceptual guide gives a brief overview of <a href="https://huggingface.co/papers/2306.07280" rel="nofollow">OFT</a>, <a href="https://huggingface.co/papers/2506.19847" rel="nofollow">OFTv2</a> and <a href="https://huggingface.co/papers/2311.06243" rel="nofollow">BOFT</a>, a parameter-efficient fine-tuning technique that utilizes orthogonal matrix to multiplicatively transform the pretrained weight matrices.</p> <p data-svelte-h="svelte-91fr84">To achieve efficient fine-tuning, OFT represents the weight updates with an orthogonal transformation. The orthogonal transformation is parameterized by an orthogonal matrix multiplied to the pretrained weight matrix. These new matrices can be trained to adapt to the new data while keeping the overall number of changes low. The original weight matrix remains frozen and doesn’t receive any further adjustments. To produce the final results, both the original and the adapted weights are multiplied togethor.</p> <p data-svelte-h="svelte-lal94n">Orthogonal Butterfly (BOFT) generalizes OFT with Butterfly factorization and further improves its parameter efficiency and finetuning flexibility. In short, OFT can be viewed as a special case of BOFT. Different from LoRA that uses additive low-rank weight updates, BOFT uses multiplicative orthogonal weight updates. The comparison is shown below.</p> <div class="flex justify-center" data-svelte-h="svelte-1veppqu"><img src="https://raw.githubusercontent.com/wy1iu/butterfly-oft/main/assets/BOFT_comparison.png"></div> <p data-svelte-h="svelte-1dyux9">BOFT has some advantages compared to LoRA:</p> <ul data-svelte-h="svelte-1tn8i92"><li>BOFT proposes a simple yet generic way to finetune pretrained models to downstream tasks, yielding a better preservation of pretraining knowledge and a better parameter efficiency.</li> <li>Through the orthogonality, BOFT introduces a structural constraint, i.e., keeping the <a href="https://huggingface.co/papers/1805.09298" rel="nofollow">hyperspherical energy</a> unchanged during finetuning. This can effectively reduce the forgetting of pretraining knowledge.</li> <li>BOFT uses the butterfly factorization to efficiently parameterize the orthogonal matrix, which yields a compact yet expressive learning space (i.e., hypothesis class).</li> <li>The sparse matrix decomposition in BOFT brings in additional inductive biases that are beneficial to generalization.</li></ul> <p data-svelte-h="svelte-18dbsba">In principle, BOFT can be applied to any subset of weight matrices in a neural network to reduce the number of trainable parameters. Given the target layers for injecting BOFT parameters, the number of trainable parameters can be determined based on the size of the weight matrices.</p> <h2 class="relative group"><a id="merge-oftboft-weights-into-the-base-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="#merge-oftboft-weights-into-the-base-model"><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>Merge OFT/BOFT weights into the base model</span></h2> <p data-svelte-h="svelte-10xuh5x">Similar to LoRA, the weights learned by OFT/BOFT can be integrated into the pretrained weight matrices using the merge_and_unload() function. This function merges the adapter weights with the base model which allows you to effectively use the newly merged model as a standalone model.</p> <div class="flex justify-center" data-svelte-h="svelte-1a4qpj5"><img src="https://raw.githubusercontent.com/wy1iu/butterfly-oft/main/assets/boft_merge.png"></div> <p data-svelte-h="svelte-6kbdta">This works because during training, the orthogonal weight matrix (R in the diagram above) and the pretrained weight matrices are separate. But once training is complete, these weights can actually be merged (multiplied) into a new weight matrix that is equivalent.</p> <h2 class="relative group"><a id="utils-for-oft--boft" 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="#utils-for-oft--boft"><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>Utils for OFT / BOFT</span></h2> <h3 class="relative group"><a id="common-oft--boft-parameters-in-peft" 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="#common-oft--boft-parameters-in-peft"><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>Common OFT / BOFT parameters in PEFT</span></h3> <p data-svelte-h="svelte-qq3gtj">As with other methods supported by PEFT, to fine-tune a model using OFT or BOFT, you need to:</p> <ol data-svelte-h="svelte-psegni"><li>Instantiate a base model.</li> <li>Create a configuration (<code>OFTConfig</code> or <code>BOFTConfig</code>) where you define OFT/BOFT-specific parameters.</li> <li>Wrap the base model with <code>get_peft_model()</code> to get a trainable <code>PeftModel</code>.</li> <li>Train the <code>PeftModel</code> as you normally would train the base model.</li></ol> <h3 class="relative group"><a id="oft-specific-parameters" 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="#oft-specific-parameters"><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>OFT-specific parameters</span></h3> <p data-svelte-h="svelte-19hbjqq"><code>OFTConfig</code> allows you to control how OFT is applied to the base model through the following parameters:</p> <ul data-svelte-h="svelte-zsxfpf"><li><code>r</code>: OFT rank, number of OFT blocks per injected layer. <strong>Bigger</strong> <code>r</code> results in more sparse update matrices with <strong>fewer</strong> trainable paramters. <strong>Note</strong>: You can only specify either <code>r</code> or <code>oft_block_size</code>, but not both simultaneously, because <code>r</code> × <code>oft_block_size</code> = layer dimension. For simplicity, we let the user speficy either <code>r</code> or <code>oft_block_size</code> and infer the other one. Default set to <code>r = 0</code>, the user is advised to set the <code>oft_block_size</code> instead for better clarity.</li> <li><code>oft_block_size</code>: OFT block size across different layers. <strong>Bigger</strong> <code>oft_block_size</code> results in more dense update matrices with <strong>more</strong> trainable parameters. <strong>Note</strong>: Please choose <code>oft_block_size</code> to be divisible by layer’s input dimension (<code>in_features</code>), e.g., 4, 8, 16. You can only specify either <code>r</code> or <code>oft_block_size</code>, but not both simultaneously, because <code>r</code> × <code>oft_block_size</code> = layer dimension. For simplicity, we let the user speficy either <code>r</code> or <code>oft_block_size</code> and infer the other one. Default set to <code>oft_block_size = 32</code>.</li> <li><code>use_cayley_neumann</code>: Specifies whether to use the Cayley-Neumann parameterization (efficient but approximate) or the vanilla Cayley parameterization (exact but computationally expensive because of matrix inverse). We recommend to set it to <code>True</code> for better efficiency, but performance may be slightly worse because of the approximation error. Please test both settings (<code>True</code> and <code>False</code>) depending on your needs. Default is <code>False</code>.</li> <li><code>module_dropout</code>: The multiplicative dropout probability, by setting OFT blocks to identity during training, similar to the dropout layer in LoRA.</li> <li><code>bias</code>: specify if the <code>bias</code> parameters should be trained. Can be <code>&quot;none&quot;</code>, <code>&quot;all&quot;</code> or <code>&quot;oft_only&quot;</code>.</li> <li><code>target_modules</code>: The modules (for example, attention blocks) to inject the OFT matrices.</li> <li><code>modules_to_save</code>: List of modules apart from OFT matrices to be set as trainable and saved in the final checkpoint. These typically include model’s custom head that is randomly initialized for the fine-tuning task.</li></ul> <h3 class="relative group"><a id="boft-specific-parameters" 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="#boft-specific-parameters"><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>BOFT-specific parameters</span></h3> <p data-svelte-h="svelte-1wxeuea"><code>BOFTConfig</code> allows you to control how BOFT is applied to the base model through the following parameters:</p> <ul data-svelte-h="svelte-1u1opeo"><li><code>boft_block_size</code>: the BOFT matrix block size across different layers, expressed in <code>int</code>. <strong>Bigger</strong> <code>boft_block_size</code> results in more dense update matrices with <strong>more</strong> trainable parameters. <strong>Note</strong>, please choose <code>boft_block_size</code> to be divisible by most layer’s input dimension (<code>in_features</code>), e.g., 4, 8, 16. Also, please only
specify either <code>boft_block_size</code> or <code>boft_block_num</code>, but not both simultaneously or leaving both to 0, because <code>boft_block_size</code> x <code>boft_block_num</code> must equal the layer’s input dimension.</li> <li><code>boft_block_num</code>: the number of BOFT matrix blocks across different layers, expressed in <code>int</code>. <strong>Bigger</strong> <code>boft_block_num</code> result in sparser update matrices with <strong>fewer</strong> trainable parameters. <strong>Note</strong>, please choose <code>boft_block_num</code> to be divisible by most layer’s input dimension (<code>in_features</code>), e.g., 4, 8, 16. Also, please only
specify either <code>boft_block_size</code> or <code>boft_block_num</code>, but not both simultaneously or leaving both to 0, because <code>boft_block_size</code> x <code>boft_block_num</code> must equal the layer’s input dimension.</li> <li><code>boft_n_butterfly_factor</code>: the number of butterfly factors. <strong>Note</strong>, for <code>boft_n_butterfly_factor=1</code>, BOFT is the same as vanilla OFT, for <code>boft_n_butterfly_factor=2</code>, the effective block size of OFT becomes twice as big and the number of blocks become half.</li> <li><code>bias</code>: specify if the <code>bias</code> parameters should be trained. Can be <code>&quot;none&quot;</code>, <code>&quot;all&quot;</code> or <code>&quot;boft_only&quot;</code>.</li> <li><code>boft_dropout</code>: specify the probability of multiplicative dropout.</li> <li><code>target_modules</code>: The modules (for example, attention blocks) to inject the OFT/BOFT matrices.</li> <li><code>modules_to_save</code>: List of modules apart from OFT/BOFT matrices to be set as trainable and saved in the final checkpoint. These typically include model’s custom head that is randomly initialized for the fine-tuning task.</li></ul> <h2 class="relative group"><a id="oft-example-usage" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#oft-example-usage"><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>OFT Example Usage</span></h2> <p data-svelte-h="svelte-1or7048">For using OFT for quantized finetuning with <a href="https://github.com/huggingface/trl" rel="nofollow">TRL</a> for <code>SFT</code>, <code>PPO</code>, or <code>DPO</code> fine-tuning, follow the following outline:</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> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
<span class="hljs-keyword">from</span> trl <span class="hljs-keyword">import</span> SFTTrainer
<span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> OFTConfig
<span class="hljs-keyword">if</span> use_quantization:
bnb_config = BitsAndBytesConfig(
load_in_4bit=<span class="hljs-literal">True</span>,
bnb_4bit_quant_type=<span class="hljs-string">&quot;nf4&quot;</span>,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=<span class="hljs-literal">True</span>,
bnb_4bit_quant_storage=torch.bfloat16,
)
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;model_name&quot;</span>,
quantization_config=bnb_config
)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;model_name&quot;</span>)
<span class="hljs-comment"># Configure OFT</span>
peft_config = OFTConfig(
oft_block_size=<span class="hljs-number">32</span>,
use_cayley_neumann=<span class="hljs-literal">True</span>,
target_modules=<span class="hljs-string">&quot;all-linear&quot;</span>,
bias=<span class="hljs-string">&quot;none&quot;</span>,
task_type=<span class="hljs-string">&quot;CAUSAL_LM&quot;</span>
)
trainer = SFTTrainer(
model=model,
train_dataset=ds[<span class="hljs-string">&#x27;train&#x27;</span>],
peft_config=peft_config,
processing_class=tokenizer,
args=training_arguments,
data_collator=collator,
)
trainer.train()<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="boft-example-usage" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#boft-example-usage"><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>BOFT Example Usage</span></h2> <p data-svelte-h="svelte-1py8k9k">For an example of the BOFT method application to various downstream tasks, please refer to the following guides:</p> <p data-svelte-h="svelte-5zw2dt">Take a look at the following step-by-step guides on how to finetune a model with BOFT:</p> <ul data-svelte-h="svelte-yv73mp"><li><a href="https://github.com/huggingface/peft/blob/main/examples/boft_dreambooth/boft_dreambooth.md" rel="nofollow">Dreambooth finetuning with BOFT</a></li> <li><a href="https://github.com/huggingface/peft/blob/main/examples/boft_controlnet/boft_controlnet.md" rel="nofollow">Controllable generation finetuning with BOFT (ControlNet)</a></li></ul> <p data-svelte-h="svelte-9y5ksw">For the task of image classification, one can initialize the BOFT config for a DinoV2 model as follows:</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">import</span> transformers
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSeq2SeqLM, BOFTConfig
<span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> BOFTConfig, get_peft_model
config = BOFTConfig(
boft_block_size=<span class="hljs-number">4</span>,
boft_n_butterfly_factor=<span class="hljs-number">2</span>,
target_modules=[<span class="hljs-string">&quot;query&quot;</span>, <span class="hljs-string">&quot;value&quot;</span>, <span class="hljs-string">&quot;key&quot;</span>, <span class="hljs-string">&quot;output.dense&quot;</span>, <span class="hljs-string">&quot;mlp.fc1&quot;</span>, <span class="hljs-string">&quot;mlp.fc2&quot;</span>],
boft_dropout=<span class="hljs-number">0.1</span>,
bias=<span class="hljs-string">&quot;boft_only&quot;</span>,
modules_to_save=[<span class="hljs-string">&quot;classifier&quot;</span>],
)
model = transformers.Dinov2ForImageClassification.from_pretrained(
<span class="hljs-string">&quot;facebook/dinov2-large&quot;</span>,
num_labels=<span class="hljs-number">100</span>,
)
boft_model = get_peft_model(model, config)<!-- HTML_TAG_END --></pre></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/peft/blob/main/docs/source/conceptual_guides/oft.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_1pyekj7 = {
assets: "/docs/peft/pr_3206/en",
base: "/docs/peft/pr_3206/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/peft/pr_3206/en/_app/immutable/entry/start.06e1cdbd.js"),
import("/docs/peft/pr_3206/en/_app/immutable/entry/app.cb2a6689.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 6],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
31.8 kB
·
Xet hash:
5c03ee701d474910a3c5a3af0cf51fabc0e0344c5dae488edcf6ce7ee970d755

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