Skip to content

Commit 5e03d05

Browse files
DOC: Explain uninitialized weights warning (#2369)
Users sometimes get confused by the warning from transformers that some weights are uninitialized and need to be trained when they use models for classification. A recent example is #2367. Even though the warning does not come from PEFT, let's add a section to the docs to explain this warning, as the situation is a bit different here. --------- Co-authored-by: Steven Liu <[email protected]>
1 parent 40fe166 commit 5e03d05

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/source/developer_guides/troubleshooting.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,34 @@ For inference, load the base model first and resize it the same way you did befo
148148

149149
For a complete example, please check out [this notebook](https://github.com/huggingface/peft/blob/main/examples/causal_language_modeling/peft_lora_clm_with_additional_tokens.ipynb).
150150

151+
### Getting a warning about "weights not being initialized from the model checkpoint"
152+
153+
When you load your PEFT model which has been trained on a task (for example, classification), you may get a warning like:
154+
155+
> Some weights of LlamaForSequenceClassification were not initialized from the model checkpoint at meta-llama/Llama-3.2-1B and are newly initialized: ['score.weight']. You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
156+
157+
Although this looks scary, it is most likely nothing to worry about. This warning comes from Transformers, and it isn't a PEFT specific warning. It lets you know that a randomly initialized classification head (`score`) is attached to the base model, and the head must be trained to produce sensible predictions.
158+
159+
When you get this warning _before_ training the model, PEFT automatically takes care of making the classification head trainable if you correctly passed the `task_type` argument to the PEFT config.
160+
161+
```python
162+
from peft import LoraConfig, TaskType
163+
164+
lora_config = LoraConfig(..., task_type=TaskType.SEQ_CLS)
165+
```
166+
167+
If your classification head does not follow the usual naming conventions from Transformers (which is rare), you have to explicitly tell PEFT the name of the head in `modules_to_save`.
168+
169+
```python
170+
lora_config = LoraConfig(..., modules_to_save=["name-of-classification-head"])
171+
```
172+
173+
To check the name of the classification head, print the model and it should be the last module.
174+
175+
If you get this warning from your inference code, i.e. _after_ training the model, when you load the PEFT model, you always have to load the Transformers model first. Since Transformers does not know that you will load PEFT weights afterwards, it still gives the warning.
176+
177+
As always, it is best practice to ensure the model works correctly for inference by running some validation on it.
178+
151179
### Check layer and model status
152180

153181
Sometimes a PEFT model can end up in a bad state, especially when handling multiple adapters. There can be some confusion around what adapters exist, which one is active, which one is merged, etc. To help investigate this issue, call the [`~peft.PeftModel.get_layer_status`] and the [`~peft.PeftModel.get_model_status`] methods.

0 commit comments

Comments
 (0)