PEFT 文件

BOFT

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

BOFT

正交蝶式 (BOFT) 是一種為微調基礎模型而設計的通用方法。它從 Cooley-Tukey 快速傅立葉變換中汲取靈感,提高了正交微調 (OFT) 正規化的引數效率,在微調不同基礎模型(包括大型視覺 Transformer、大型語言模型和文字到影像擴散模型)方面顯示出良好的效果。

論文摘要如下:

大型基礎模型正變得無處不在,但從零開始訓練它們的成本高得令人望而卻步。因此,如何高效地將這些強大的模型應用於下游任務變得越來越重要。在本文中,我們研究了一種有原則的微調正規化——正交微調 (OFT)——用於下游任務的適配。儘管 OFT 表現出良好的泛化能力,但由於正交矩陣的高維度,它仍然使用相當多的可訓練引數。為了解決這個問題,我們首先從資訊傳輸的角度審視 OFT,然後確定了一些能夠提高參數效率的關鍵期望特性。受 Cooley-Tukey 快速傅立葉變換演算法如何實現高效資訊傳輸的啟發,我們提出了一種使用蝶式結構的高效正交引數化方法。我們將這種引數化應用於 OFT,創造了一種新穎的引數高效微調方法,稱為正交蝶式 (BOFT)。透過將 OFT 作為一個特例,BOFT 引入了一個廣義的正交微調框架。最後,我們對將大型視覺 Transformer、大型語言模型和文字到影像擴散模型適配到視覺和語言領域的各種下游任務進行了廣泛的實證研究。.

BOFTConfig

class peft.BOFTConfig

< >

( task_type: typing.Union[str, peft.utils.peft_types.TaskType, NoneType] = None peft_type: typing.Union[str, peft.utils.peft_types.PeftType, NoneType] = None auto_mapping: typing.Optional[dict] = None base_model_name_or_path: typing.Optional[str] = None revision: typing.Optional[str] = None inference_mode: bool = False boft_block_size: int = 4 boft_block_num: int = 0 boft_n_butterfly_factor: int = 1 target_modules: Optional[Union[list[str], str]] = None exclude_modules: Optional[Union[list[str], str]] = None boft_dropout: float = 0.0 fan_in_fan_out: bool = False bias: str = 'none' modules_to_save: Optional[list[str]] = None init_weights: bool = True layers_to_transform: Optional[Union[list[int], int]] = None layers_pattern: Optional[Union[list[str], str]] = None )

引數

  • boft_block_size (int) — 不同層之間的 BOFT 塊大小。
  • boft_block_num (int) — 每個注入層的 BOFT 塊數量。
  • boft_n_butterfly_factor (int) — 不同層之間的蝶式因子數量。
  • target_modules (Union[List[str],str]) — 應用介面卡的模組名稱。
  • exclude_modules (Optional[Union[List[str], str]]) — 不應用介面卡的模組名稱。當傳遞字串時,將執行正則表示式匹配。當傳遞字串列表時,將執行精確匹配,或者檢查模組名稱是否以任何傳遞的字串結尾。
  • boft_dropout (float) — 乘性丟棄機率,透過在訓練期間將 OFT 塊設定為單位矩陣,類似於 LoRA 中的丟棄層。
  • fan_in_fan_out (bool) — 如果要替換的層儲存權重的方式是 (fan_in, fan_out),則設定為 True。例如,gpt-2 使用 Conv1D,它儲存權重的方式是 (fan_in, fan_out),因此應將此設定為 True
  • bias (str) — BOFT 的偏置型別。可以是 'none'、'all' 或 'boft_only'。如果為 'all' 或 'boft_only',相應的偏置將在訓練期間更新。請注意,這意味著即使停用了介面卡,模型的輸出也不會與未經適配的基礎模型相同。
  • modules_to_save (List[str]) — 除了 BOFT 層之外,要設定為可訓練並儲存在最終檢查點中的模組列表。
  • layers_to_transform (Union[List[int],int]) — 要轉換的層索引,如果指定了此引數,它將在該列表中指定的層索引上應用 BOFT 轉換。如果傳遞單個整數,它將在該索引的層上應用 BOFT 轉換。
  • layers_pattern (Optional[Union[List[str], str]]) — 層模式名稱,僅當 layers_to_transform 不為 None 且層模式不在常見層模式中時使用。這應針對模型的 nn.ModuleList,通常稱為 'layers''h'

這是用於儲存 BOFTModel 配置的配置類。

BOFTModel

class peft.BOFTModel

< >

( model peft_config: Union[PeftConfig, dict[str, PeftConfig]] adapter_name: str low_cpu_mem_usage: bool = False state_dict: Optional[dict[str, torch.Tensor]] = None ) torch.nn.Module

引數

  • model ([transformers.PreTrainedModel]) — 待適配的模型。
  • config ([BOFTConfig]) — BOFT 模型的配置。
  • adapter_name (str) — 介面卡名稱,預設為 “default”
  • low_cpu_mem_usage (bool, optional, 預設為 False) — 在元裝置上建立空的介面卡權重。有助於加快載入過程。

返回

torch.nn.Module

BOFT 模型。

從預訓練的 Transformer 模型建立 BOFT 和 OFT 模型。論文:https://huggingface.co/papers/2311.06243 https://huggingface.co/papers/2306.07280

示例

>>> import transformers >>> from transformers import AutoModelForSeq2SeqLM, BOFTConfig >>> from peft import
BOFTConfig, get_peft_model

>>> config = BOFTConfig( ... boft_block_size=8, ... boft_n_butterfly_factor=1, ... target_modules=["query",
"value", "key", "output.dense", "mlp.fc1", "mlp.fc2"], ... boft_dropout=0.1, ... bias="boft_only", ...
modules_to_save=["classifier"], ... )

>>> model = transformers.Dinov2ForImageClassification.from_pretrained( ... "facebook/dinov2-large", ...
num_labels=100, ... ) >>> boft_model = get_peft_model(model, config)

屬性:

  • model ([transformers.PreTrainedModel]) — 待適配的模型。
  • peft_config ([BOFTConfig]): BOFT 模型的配置。

delete_adapter

< >

( adapter_name: str )

引數

  • adapter_name (str) — 待刪除的介面卡名稱。

刪除一個現有的介面卡。

merge_and_unload

< >

( progressbar: bool = False safe_merge: bool = False adapter_names: typing.Optional[list[str]] = None )

引數

  • progressbar (bool) — 是否顯示錶示解除安裝和合並過程的進度條
  • safe_merge (bool) — 是否啟用安全合併檢查,以檢查介面卡權重中是否存在任何潛在的 Nan 值
  • adapter_names (List[str], optional) — 應合併的介面卡名稱列表。如果為 None,則將合併所有活動的介面卡。預設為 None

此方法將 BOFT 層合併到基礎模型中。如果有人想將基礎模型用作獨立模型,則需要此方法。

unload

< >

( )

透過移除所有 boft 模組而不進行合併,來恢復基礎模型。這將返回原始的基礎模型。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.