在不同型号上应用PEFT/LoRA的目标模块

Yog*_*sch 16 nlp huggingface-transformers huggingface peft fine-tuning

我正在研究在不同模型上使用 PEFT 的几个不同 示例。LoraConfig对象包含一个target_modules数组。在一些示例中,目标模块是["query_key_value"],有时是["q", "v"],有时是其他。

我不太明白目标模块的值来自哪里。我应该在模型页面的哪个位置查看 LoRA 适配模块是什么?

一个示例(针对型号 Falcon 7B):

peft_config = LoraConfig(
    lora_alpha=lora_alpha,
    lora_dropout=lora_dropout,
    r=lora_r,
    bias="none",
    task_type="CAUSAL_LM",
    target_modules=[
        "query_key_value",
        "dense",
        "dense_h_to_4h",
        "dense_4h_to_h",
    ]
Run Code Online (Sandbox Code Playgroud)

另一个例子(对于型号 Opt-6.7B):

config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)
Run Code Online (Sandbox Code Playgroud)

还有另一个(对于型号 Flan-T5-xxl):

lora_config = LoraConfig(
 r=16,
 lora_alpha=32,
 target_modules=["q", "v"],
 lora_dropout=0.05,
 bias="none",
 task_type=TaskType.SEQ_2_SEQ_LM
)
Run Code Online (Sandbox Code Playgroud)

dea*_*u5p 24

假设您加载了您选择的某个模型:

model = AutoModelForCausalLM.from_pretrained("some-model-checkpoint")

然后你可以通过打印这个模型来查看可用的模块:

print(model)

你会得到类似这样的东西(SalesForce/CodeGen25):

LlamaForCausalLM(
  (model): LlamaModel(
    (embed_tokens): Embedding(51200, 4096, padding_idx=0)
    (layers): ModuleList(
      (0-31): 32 x LlamaDecoderLayer(
        (self_attn): LlamaAttention(
          (q_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (k_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (v_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (o_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (rotary_emb): LlamaRotaryEmbedding()
        )
        (mlp): LlamaMLP(
          (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)
          (down_proj): Linear(in_features=11008, out_features=4096, bias=False)
          (up_proj): Linear(in_features=4096, out_features=11008, bias=False)
          (act_fn): SiLUActivation()
        )
        (input_layernorm): LlamaRMSNorm()
        (post_attention_layernorm): LlamaRMSNorm()
      )
    )
    (norm): LlamaRMSNorm()
  )
  (lm_head): Linear(in_features=4096, out_features=51200, bias=False)
)
Run Code Online (Sandbox Code Playgroud)

就我而言,您可以找到包含 q_proj、k_proj、v_proj 和 o_proj 的 LLamaAttention 模块。这是 LoRA 可用的一些模块。

我建议您阅读LoRA 论文中有关使用哪些模块的更多信息。