Lightning-AI / Lightning-AI/litgpt

Failed to load the finetuned model with `AutoModelForCausalLM.from_pretrained(name, state_dict=state_dict)`

Open
#1,362 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3rd party bug help wanted
Dominant language
Python
Stars
13.7k
Forks
1.5k
Avg merge
15h 37m
Merged PRs (30d)
1

Description

I fine-tuned llama3-8b with Lora and followed the tutorial in the repository to convert the final result into `model.pth`. However, when I try to load the fine-tuned weights into the model using `AutoModelForCausalLM.from_pretrained`, I am unable to do so correctly. Below is my test:
```python
state_dict = torch.load('out/convert/hf-llama3-instruct-esconv/model.pth')
print("state_dict: ", state_dict)
model = AutoModelForCausalLM.from_pretrained('checkpoints/meta-llama/Meta-Llama-3-8B',
device_map=device_map, torch_dtype=torch.float16,
state_dict=state_dict)

print("model.weights", model.state_dict())
```
But I found that the `state_dict` of `torch.load` doesn't equal to the `model.state_dict()`, as shown following:
torch.load:
![c62077774b213ae19704e33b6fb8ee1](https://github.com/Lightning-AI/litgpt/assets/144892591/fba11ac4-3bf4-4e62-a4d3-05ec8c4bffa2)
model.state_dict()
![e24af415a1cd401e1743546b0a5314b](https://github.com/Lightning-AI/litgpt/assets/144892591/7dd72441-8942-4d6f-b10f-f27f2f3bf9a0)

I noticed that even though I passed the `state_dict`, `from_pretrained` still returns the weights of the model loaded by name. Did I make any mistakes in my code, and how can I solve this? Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the repository tutorial's conversion to model.pth, then reproduce the AutoModelForCausalLM.from_pretrained call using the saved state_dict. Compare the keys and loading behavior with model.state_dict(); done means explaining whether this loading path is supported and documenting the correct way to load the converted weights.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
ai, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.