Skip to content

Instantly share code, notes, and snippets.

@sitex
Last active October 2, 2023 07:57
Show Gist options
  • Save sitex/b6a2f37afef2e357b4a16d3589f96b5c to your computer and use it in GitHub Desktop.
Save sitex/b6a2f37afef2e357b4a16d3589f96b5c to your computer and use it in GitHub Desktop.
# pip install git+https://github.com/huggingface/transformers --force-reinstall
# pip install git+https://github.com/bayartsogt-ya/whisper-multiple-hf-datasets
from multiple_datasets.hub_default_utils import convert_hf_whisper
convert_hf_whisper("mitchelldehaven/whisper-large-v2-ru", "large-v2-ru.pt")
# We also provide a script to convert any Whisper models compatible with the Transformers library. They could be the original OpenAI models or user fine-tuned models.
# For example the command below converts the original "large-v2" Whisper model and saves the weights in FP16:
!pip install transformers[torch]>=4.23
!ct2-transformers-converter --model openai/whisper-large-v2 --output_dir whisper-large-v2-ct2 \
--copy_files tokenizer.json --quantization float16
# The option --model accepts a model name on the Hub or a path to a model directory.
# If the option --copy_files tokenizer.json is not used, the tokenizer configuration is automatically downloaded when the model is loaded later.
@sitex
Copy link
Author

sitex commented Oct 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment