How to convert the model(pytorch_model.bin) to .pt file?

#61
by zhaofeng3012 - opened

Hello everyone, I have a question.

I fine-tuned the model and got some files including pytorch_model.bin(about 6.17G).

As the title describes, I want convert the model(pytorch_model.bin, about 6.17G) to .pt file(about 2.87G).

I tried to use torch.load() and torch.save(), but the output .pt file can't load to use. I'm not sure whether I'm using these functions incorrectly.

I would be grateful if someone could answer my doubts.
THX!!!

Did you find any solution to this? I have the same problem. Have a finetuned Whisper model in .bin and want a PT file so I can use it in the audio webui! :)

zhaofeng3012 changed discussion status to closed

You might need to use some quantisation / casting of the weights?

The weights pytorch_model.bin are saved using torch.save under-the-hood when we call .save_pretrained: https://github.com/huggingface/transformers/blob/034bc5d26ad7c0e284265d92d3da39d786138545/src/transformers/modeling_utils.py#L1752

Thus, you can load them with torch.load:

from transformers import WhisperForConditionalGeneration
import tempfile
import torch

model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")

with tempfile.TemporaryDirectory() as tmp_dir_name:
    model.save_pretrained(tmp_dir_name)
    state_dict = torch.load(f"{tmp_dir_name}/pytorch_model.bin")

Of course, if you have the model weights saved locally already, there is no need to save the state dict again, just load the state dict from the saved path.

I found the discussion here helpful https://github.com/openai/whisper/discussions/830

Sign up or log in to comment