Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Llava conversion for models that use safetensors to store weights #35406

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/transformers/models/llava/convert_llava_weights_to_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import glob

import torch
from huggingface_hub import hf_hub_download, snapshot_download
from huggingface_hub import file_exists, hf_hub_download, snapshot_download
from safetensors import safe_open

from transformers import (
Expand Down Expand Up @@ -140,11 +140,12 @@ def convert_llava_llama_to_hf(text_model_id, vision_model_id, output_hub_path, o
with torch.device("meta"):
model = LlavaForConditionalGeneration(config)

if "Qwen" in text_model_id:
state_dict = load_original_state_dict(old_state_dict_id)
else:
# Some llava variants like microsoft/llava-med-v1.5-mistral-7b use safetensors to store weights
if file_exists(old_state_dict_id, "model_state_dict.bin"):
state_dict_path = hf_hub_download(old_state_dict_id, "model_state_dict.bin")
state_dict = torch.load(state_dict_path, map_location="cpu")
state_dict = torch.load(state_dict_path, map_location="cpu", weights_only=True)
else:
state_dict = load_original_state_dict(old_state_dict_id)

state_dict = convert_state_dict_to_hf(state_dict)
model.load_state_dict(state_dict, strict=True, assign=True)
Expand Down