ValueError
peft.peft_config.ValueError: target_modules not found
Stack trace
ValueError: target_modules ['q_proj', 'v_proj'] not found in model's modules. Available modules are: ['query', 'value', 'key', 'out_proj']
Why it happens
The LoraConfig expects exact module names in the base model to apply LoRA adapters. If the target_modules list contains names that do not match any module in the model, PEFT raises this error. This often happens due to model architecture differences or typos in module names.
Detection
Check the model's named modules via model.named_modules() or model.config to verify available module names before passing them to LoraConfig.
Causes & fixes
Incorrect or misspelled module names in target_modules list that do not exist in the base model.
Inspect the base model's module names using model.named_modules() and update target_modules to match exactly.
Using a model architecture that does not contain the expected projection layers (e.g., 'q_proj', 'v_proj') because it uses different naming conventions.
Adapt target_modules to the actual module names of the model, such as 'query', 'value', or 'key', depending on the architecture.
Passing target_modules before loading or initializing the base model properly, so the model's modules are not accessible yet.
Ensure the base model is fully loaded and initialized before creating LoraConfig with target_modules.
Code: broken vs fixed
from peft import LoraConfig
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained('gpt2')
lora_config = LoraConfig(target_modules=['q_proj', 'v_proj'], r=8, lora_alpha=32, lora_dropout=0.1) # This line triggers ValueError import os
from peft import LoraConfig
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(os.environ['HF_MODEL_NAME'])
# Inspect model modules to find correct target_modules
# For GPT2, use 'query' and 'value' instead of 'q_proj' and 'v_proj'
lora_config = LoraConfig(target_modules=['query', 'value'], r=8, lora_alpha=32, lora_dropout=0.1) # Fixed target_modules
print('LoraConfig created successfully with correct target_modules') Workaround
Wrap LoraConfig creation in try/except ValueError, then log model.named_modules() output to manually identify correct module names and retry with corrected list.
Prevention
Always inspect the base model's module names before configuring LoRA adapters to ensure target_modules align with the model's architecture and naming conventions.