🎯

LoRAによるSLMのファインチューニングのハイパーパラメータチューニング - target_modules

2024/10/26に公開

はじめに

LoRAによるPhi-3-mini-4k-instructのFineTuningをしていて、なかなか思うような回答が得られないなと言うときにtarget_modulesを変更したら精度が上がった気がするのでメモします。

LoRAによる効率的なファインチューニング:大規模言語モデルにおける最適パラメータ選択のガイド
というDatabricksの記事があり、下記の日本語訳を参考にしました。
https://qiita.com/taka_yayoi/items/7f50d3ebbe4bebd6877c

LoRAのハイパーパラメータ

こちらのPhi-3のFineTuningのCookbookの中に、下記のようなパラメータが記載されています。

args = TrainingArguments(
        output_dir="./phi-3-mini-LoRA",
        evaluation_strategy="steps",
        do_eval=True,
        optim="adamw_torch",
        per_device_train_batch_size=8,
        gradient_accumulation_steps=4,
        per_device_eval_batch_size=8,
        log_level="debug",
        save_strategy="epoch",
        logging_steps=100,
        learning_rate=1e-4,
        fp16 = not torch.cuda.is_bf16_supported(),
        bf16 = torch.cuda.is_bf16_supported(),
        eval_steps=100,
        num_train_epochs=3,
        warmup_ratio=0.1,
        lr_scheduler_type="linear",
        report_to="wandb",
        seed=42,
)

peft_config = LoraConfig(
        r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        task_type=TaskType.CAUSAL_LM,
        target_modules=target_modules,
)

様々なパラメータがあり、lora_rやlora_alphaが大事みたいなことも書かれていますが、
いろいろ試して、target_modulesの選択で大きく精度が変わるなと思いました。

target_modulesは下記のように設定されていましたが、他に何があるのかなと。

# 'target_modules' is a list of the modules in the model that will be replaced with LoRA layers.
target_modules= ['k_proj', 'q_proj', 'v_proj', 'o_proj', "gate_proj", "down_proj", "up_proj"]

HugginFaceのサイトに、pretrainedのモデルからモジュールを表示することで、
target_modulesの候補がわかるそうです。

# If you need to find out the target_modules for peft used this code
from transformers import AutoModelForCausalLM

# Load the model
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct")

# Print out the named modules
for name, module in model.named_modules():
    print(name)
出力されたtarget_modulesの候補

model
model.embed_tokens
model.embed_dropout
model.layers
model.layers.0
model.layers.0.self_attn
model.layers.0.self_attn.o_proj
model.layers.0.self_attn.qkv_proj
model.layers.0.self_attn.rotary_emb
model.layers.0.mlp
model.layers.0.mlp.gate_up_proj
model.layers.0.mlp.down_proj
model.layers.0.mlp.activation_fn
model.layers.0.input_layernorm
model.layers.0.resid_attn_dropout
model.layers.0.resid_mlp_dropout
model.layers.0.post_attention_layernorm
model.layers.1
model.layers.1.self_attn
model.layers.1.self_attn.o_proj
model.layers.1.self_attn.qkv_proj
model.layers.1.self_attn.rotary_emb
model.layers.1.mlp
model.layers.1.mlp.gate_up_proj
model.layers.1.mlp.down_proj
model.layers.1.mlp.activation_fn
model.layers.1.input_layernorm
model.layers.1.resid_attn_dropout
model.layers.1.resid_mlp_dropout
model.layers.1.post_attention_layernorm
model.layers.2
model.layers.2.self_attn
model.layers.2.self_attn.o_proj
model.layers.2.self_attn.qkv_proj
model.layers.2.self_attn.rotary_emb
model.layers.2.mlp
model.layers.2.mlp.gate_up_proj
model.layers.2.mlp.down_proj
model.layers.2.mlp.activation_fn
model.layers.2.input_layernorm
model.layers.2.resid_attn_dropout
model.layers.2.resid_mlp_dropout
model.layers.2.post_attention_layernorm
model.layers.3
model.layers.3.self_attn
model.layers.3.self_attn.o_proj
model.layers.3.self_attn.qkv_proj
model.layers.3.self_attn.rotary_emb
model.layers.3.mlp
model.layers.3.mlp.gate_up_proj
model.layers.3.mlp.down_proj
model.layers.3.mlp.activation_fn
model.layers.3.input_layernorm
model.layers.3.resid_attn_dropout
model.layers.3.resid_mlp_dropout
model.layers.3.post_attention_layernorm
model.layers.4
model.layers.4.self_attn
model.layers.4.self_attn.o_proj
model.layers.4.self_attn.qkv_proj
model.layers.4.self_attn.rotary_emb
model.layers.4.mlp
model.layers.4.mlp.gate_up_proj
model.layers.4.mlp.down_proj
model.layers.4.mlp.activation_fn
model.layers.4.input_layernorm
model.layers.4.resid_attn_dropout
model.layers.4.resid_mlp_dropout
model.layers.4.post_attention_layernorm
model.layers.5
model.layers.5.self_attn
model.layers.5.self_attn.o_proj
model.layers.5.self_attn.qkv_proj
model.layers.5.self_attn.rotary_emb
model.layers.5.mlp
model.layers.5.mlp.gate_up_proj
model.layers.5.mlp.down_proj
model.layers.5.mlp.activation_fn
model.layers.5.input_layernorm
model.layers.5.resid_attn_dropout
model.layers.5.resid_mlp_dropout
model.layers.5.post_attention_layernorm
model.layers.6
model.layers.6.self_attn
model.layers.6.self_attn.o_proj
model.layers.6.self_attn.qkv_proj
model.layers.6.self_attn.rotary_emb
model.layers.6.mlp
model.layers.6.mlp.gate_up_proj
model.layers.6.mlp.down_proj
model.layers.6.mlp.activation_fn
model.layers.6.input_layernorm
model.layers.6.resid_attn_dropout
model.layers.6.resid_mlp_dropout
model.layers.6.post_attention_layernorm
model.layers.7
model.layers.7.self_attn
model.layers.7.self_attn.o_proj
model.layers.7.self_attn.qkv_proj
model.layers.7.self_attn.rotary_emb
model.layers.7.mlp
model.layers.7.mlp.gate_up_proj
model.layers.7.mlp.down_proj
model.layers.7.mlp.activation_fn
model.layers.7.input_layernorm
model.layers.7.resid_attn_dropout
model.layers.7.resid_mlp_dropout
model.layers.7.post_attention_layernorm
model.layers.8
model.layers.8.self_attn
model.layers.8.self_attn.o_proj
model.layers.8.self_attn.qkv_proj
model.layers.8.self_attn.rotary_emb
model.layers.8.mlp
model.layers.8.mlp.gate_up_proj
model.layers.8.mlp.down_proj
model.layers.8.mlp.activation_fn
model.layers.8.input_layernorm
model.layers.8.resid_attn_dropout
model.layers.8.resid_mlp_dropout
model.layers.8.post_attention_layernorm
model.layers.9
model.layers.9.self_attn
model.layers.9.self_attn.o_proj
model.layers.9.self_attn.qkv_proj
model.layers.9.self_attn.rotary_emb
model.layers.9.mlp
model.layers.9.mlp.gate_up_proj
model.layers.9.mlp.down_proj
model.layers.9.mlp.activation_fn
model.layers.9.input_layernorm
model.layers.9.resid_attn_dropout
model.layers.9.resid_mlp_dropout
model.layers.9.post_attention_layernorm
model.layers.10
model.layers.10.self_attn
model.layers.10.self_attn.o_proj
model.layers.10.self_attn.qkv_proj
model.layers.10.self_attn.rotary_emb
model.layers.10.mlp
model.layers.10.mlp.gate_up_proj
model.layers.10.mlp.down_proj
model.layers.10.mlp.activation_fn
model.layers.10.input_layernorm
model.layers.10.resid_attn_dropout
model.layers.10.resid_mlp_dropout
model.layers.10.post_attention_layernorm
model.layers.11
model.layers.11.self_attn
model.layers.11.self_attn.o_proj
model.layers.11.self_attn.qkv_proj
model.layers.11.self_attn.rotary_emb
model.layers.11.mlp
model.layers.11.mlp.gate_up_proj
model.layers.11.mlp.down_proj
model.layers.11.mlp.activation_fn
model.layers.11.input_layernorm
model.layers.11.resid_attn_dropout
model.layers.11.resid_mlp_dropout
model.layers.11.post_attention_layernorm
model.layers.12
model.layers.12.self_attn
model.layers.12.self_attn.o_proj
model.layers.12.self_attn.qkv_proj
model.layers.12.self_attn.rotary_emb
model.layers.12.mlp
model.layers.12.mlp.gate_up_proj
model.layers.12.mlp.down_proj
model.layers.12.mlp.activation_fn
model.layers.12.input_layernorm
model.layers.12.resid_attn_dropout
model.layers.12.resid_mlp_dropout
model.layers.12.post_attention_layernorm
model.layers.13
model.layers.13.self_attn
model.layers.13.self_attn.o_proj
model.layers.13.self_attn.qkv_proj
model.layers.13.self_attn.rotary_emb
model.layers.13.mlp
model.layers.13.mlp.gate_up_proj
model.layers.13.mlp.down_proj
model.layers.13.mlp.activation_fn
model.layers.13.input_layernorm
model.layers.13.resid_attn_dropout
model.layers.13.resid_mlp_dropout
model.layers.13.post_attention_layernorm
model.layers.14
model.layers.14.self_attn
model.layers.14.self_attn.o_proj
model.layers.14.self_attn.qkv_proj
model.layers.14.self_attn.rotary_emb
model.layers.14.mlp
model.layers.14.mlp.gate_up_proj
model.layers.14.mlp.down_proj
model.layers.14.mlp.activation_fn
model.layers.14.input_layernorm
model.layers.14.resid_attn_dropout
model.layers.14.resid_mlp_dropout
model.layers.14.post_attention_layernorm
model.layers.15
model.layers.15.self_attn
model.layers.15.self_attn.o_proj
model.layers.15.self_attn.qkv_proj
model.layers.15.self_attn.rotary_emb
model.layers.15.mlp
model.layers.15.mlp.gate_up_proj
model.layers.15.mlp.down_proj
model.layers.15.mlp.activation_fn
model.layers.15.input_layernorm
model.layers.15.resid_attn_dropout
model.layers.15.resid_mlp_dropout
model.layers.15.post_attention_layernorm
model.layers.16
model.layers.16.self_attn
model.layers.16.self_attn.o_proj
model.layers.16.self_attn.qkv_proj
model.layers.16.self_attn.rotary_emb
model.layers.16.mlp
model.layers.16.mlp.gate_up_proj
model.layers.16.mlp.down_proj
model.layers.16.mlp.activation_fn
model.layers.16.input_layernorm
model.layers.16.resid_attn_dropout
model.layers.16.resid_mlp_dropout
model.layers.16.post_attention_layernorm
model.layers.17
model.layers.17.self_attn
model.layers.17.self_attn.o_proj
model.layers.17.self_attn.qkv_proj
model.layers.17.self_attn.rotary_emb
model.layers.17.mlp
model.layers.17.mlp.gate_up_proj
model.layers.17.mlp.down_proj
model.layers.17.mlp.activation_fn
model.layers.17.input_layernorm
model.layers.17.resid_attn_dropout
model.layers.17.resid_mlp_dropout
model.layers.17.post_attention_layernorm
model.layers.18
model.layers.18.self_attn
model.layers.18.self_attn.o_proj
model.layers.18.self_attn.qkv_proj
model.layers.18.self_attn.rotary_emb
model.layers.18.mlp
model.layers.18.mlp.gate_up_proj
model.layers.18.mlp.down_proj
model.layers.18.mlp.activation_fn
model.layers.18.input_layernorm
model.layers.18.resid_attn_dropout
model.layers.18.resid_mlp_dropout
model.layers.18.post_attention_layernorm
model.layers.19
model.layers.19.self_attn
model.layers.19.self_attn.o_proj
model.layers.19.self_attn.qkv_proj
model.layers.19.self_attn.rotary_emb
model.layers.19.mlp
model.layers.19.mlp.gate_up_proj
model.layers.19.mlp.down_proj
model.layers.19.mlp.activation_fn
model.layers.19.input_layernorm
model.layers.19.resid_attn_dropout
model.layers.19.resid_mlp_dropout
model.layers.19.post_attention_layernorm
model.layers.20
model.layers.20.self_attn
model.layers.20.self_attn.o_proj
model.layers.20.self_attn.qkv_proj
model.layers.20.self_attn.rotary_emb
model.layers.20.mlp
model.layers.20.mlp.gate_up_proj
model.layers.20.mlp.down_proj
model.layers.20.mlp.activation_fn
model.layers.20.input_layernorm
model.layers.20.resid_attn_dropout
model.layers.20.resid_mlp_dropout
model.layers.20.post_attention_layernorm
model.layers.21
model.layers.21.self_attn
model.layers.21.self_attn.o_proj
model.layers.21.self_attn.qkv_proj
model.layers.21.self_attn.rotary_emb
model.layers.21.mlp
model.layers.21.mlp.gate_up_proj
model.layers.21.mlp.down_proj
model.layers.21.mlp.activation_fn
model.layers.21.input_layernorm
model.layers.21.resid_attn_dropout
model.layers.21.resid_mlp_dropout
model.layers.21.post_attention_layernorm
model.layers.22
model.layers.22.self_attn
model.layers.22.self_attn.o_proj
model.layers.22.self_attn.qkv_proj
model.layers.22.self_attn.rotary_emb
model.layers.22.mlp
model.layers.22.mlp.gate_up_proj
model.layers.22.mlp.down_proj
model.layers.22.mlp.activation_fn
model.layers.22.input_layernorm
model.layers.22.resid_attn_dropout
model.layers.22.resid_mlp_dropout
model.layers.22.post_attention_layernorm
model.layers.23
model.layers.23.self_attn
model.layers.23.self_attn.o_proj
model.layers.23.self_attn.qkv_proj
model.layers.23.self_attn.rotary_emb
model.layers.23.mlp
model.layers.23.mlp.gate_up_proj
model.layers.23.mlp.down_proj
model.layers.23.mlp.activation_fn
model.layers.23.input_layernorm
model.layers.23.resid_attn_dropout
model.layers.23.resid_mlp_dropout
model.layers.23.post_attention_layernorm
model.layers.24
model.layers.24.self_attn
model.layers.24.self_attn.o_proj
model.layers.24.self_attn.qkv_proj
model.layers.24.self_attn.rotary_emb
model.layers.24.mlp
model.layers.24.mlp.gate_up_proj
model.layers.24.mlp.down_proj
model.layers.24.mlp.activation_fn
model.layers.24.input_layernorm
model.layers.24.resid_attn_dropout
model.layers.24.resid_mlp_dropout
model.layers.24.post_attention_layernorm
model.layers.25
model.layers.25.self_attn
model.layers.25.self_attn.o_proj
model.layers.25.self_attn.qkv_proj
model.layers.25.self_attn.rotary_emb
model.layers.25.mlp
model.layers.25.mlp.gate_up_proj
model.layers.25.mlp.down_proj
model.layers.25.mlp.activation_fn
model.layers.25.input_layernorm
model.layers.25.resid_attn_dropout
model.layers.25.resid_mlp_dropout
model.layers.25.post_attention_layernorm
model.layers.26
model.layers.26.self_attn
model.layers.26.self_attn.o_proj
model.layers.26.self_attn.qkv_proj
model.layers.26.self_attn.rotary_emb
model.layers.26.mlp
model.layers.26.mlp.gate_up_proj
model.layers.26.mlp.down_proj
model.layers.26.mlp.activation_fn
model.layers.26.input_layernorm
model.layers.26.resid_attn_dropout
model.layers.26.resid_mlp_dropout
model.layers.26.post_attention_layernorm
model.layers.27
model.layers.27.self_attn
model.layers.27.self_attn.o_proj
model.layers.27.self_attn.qkv_proj
model.layers.27.self_attn.rotary_emb
model.layers.27.mlp
model.layers.27.mlp.gate_up_proj
model.layers.27.mlp.down_proj
model.layers.27.mlp.activation_fn
model.layers.27.input_layernorm
model.layers.27.resid_attn_dropout
model.layers.27.resid_mlp_dropout
model.layers.27.post_attention_layernorm
model.layers.28
model.layers.28.self_attn
model.layers.28.self_attn.o_proj
model.layers.28.self_attn.qkv_proj
model.layers.28.self_attn.rotary_emb
model.layers.28.mlp
model.layers.28.mlp.gate_up_proj
model.layers.28.mlp.down_proj
model.layers.28.mlp.activation_fn
model.layers.28.input_layernorm
model.layers.28.resid_attn_dropout
model.layers.28.resid_mlp_dropout
model.layers.28.post_attention_layernorm
model.layers.29
model.layers.29.self_attn
model.layers.29.self_attn.o_proj
model.layers.29.self_attn.qkv_proj
model.layers.29.self_attn.rotary_emb
model.layers.29.mlp
model.layers.29.mlp.gate_up_proj
model.layers.29.mlp.down_proj
model.layers.29.mlp.activation_fn
model.layers.29.input_layernorm
model.layers.29.resid_attn_dropout
model.layers.29.resid_mlp_dropout
model.layers.29.post_attention_layernorm
model.layers.30
model.layers.30.self_attn
model.layers.30.self_attn.o_proj
model.layers.30.self_attn.qkv_proj
model.layers.30.self_attn.rotary_emb
model.layers.30.mlp
model.layers.30.mlp.gate_up_proj
model.layers.30.mlp.down_proj
model.layers.30.mlp.activation_fn
model.layers.30.input_layernorm
model.layers.30.resid_attn_dropout
model.layers.30.resid_mlp_dropout
model.layers.30.post_attention_layernorm
model.layers.31
model.layers.31.self_attn
model.layers.31.self_attn.o_proj
model.layers.31.self_attn.qkv_proj
model.layers.31.self_attn.rotary_emb
model.layers.31.mlp
model.layers.31.mlp.gate_up_proj
model.layers.31.mlp.down_proj
model.layers.31.mlp.activation_fn
model.layers.31.input_layernorm
model.layers.31.resid_attn_dropout
model.layers.31.resid_mlp_dropout
model.layers.31.post_attention_layernorm
model.norm
lm_head

まとめ

上記の中からLoRAの更新対象モジュールを選択しますが、
Phi-3のtarget_modulesではqkv_projとo_projが選択されていることが多いみたいです。

試したところlm_headとgate_projを指定したら、意図した回答が生成される気がしました。

逆にdown_projとup_projを指定したら、epochが少なかったからかもですが、異なるQAの回答を返してしまったりしたので、何でもかんでも追加するのは微妙かと思いました。

もちろんepoch数やlearning_rateでも精度が変わるので、データとタスクによっていろいろ試す必要がありそうです。

ヘッドウォータース

Discussion