How to convert pretrained hugging face model to .pt for deploy?
Author: vonexel
Pull request: no

I'm attempting to convert this [model](https://huggingface.co/UrukHan/wav2vec2-russian) in .pt format. It's working fine for me so i dont want to fine-tune it. How can i export it to .pt and run interface for example in flask?

I tried using this to convert to .pt:

```
from transformers import AutoConfig, AutoProcessor, AutoModelForCTC, AutoTokenizer, Wav2Vec2Processor
import librosa
import torch



# Define the model name
model_name = "UrukHan/wav2vec2-russian"

# Load the model and tokenizer
config = AutoConfig.from_pretrained(model_name)
model = AutoModelForCTC.from_pretrained(model_name, config=config)
processor = Wav2Vec2Processor.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Save the model as a .pt file
torch.save(model.state_dict(), "model.pt")

# Save the tokenizer as well if needed
tokenizer.save_pretrained("model-tokenizer")
```

but unfortunately its not running the interface and not loading model from path :

```
model = AutoModelForCTC.from_pretrained("model.pt")
processor = AutoProcessor.from_pretrained("model.pt")


# Perform inference with the model
FILE = 'here is wav.wav'
audio, _ = librosa.load(FILE, sr = 16000)
audio = list(audio)
def map_to_result(batch):
  with torch.no_grad():
    input_values = torch.tensor(batch, device="cpu").unsqueeze(0) #, device="cuda"
    logits = model(input_values).logits
  pred_ids = torch.argmax(logits, dim=-1)
  batch = processor.batch_decode(pred_ids)[0]
  return batch
map_to_result(audio)
print(map_to_result(audio))


model.eval()
```

And encountered an error: 
`model.pt is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'`

What am i doing wrong?
If you can provide guideline on how to convert model to .pt and run it it will be appreciated!Thanks in advance!

Comments:
Hi,

It's recommended to use [save_pretrained](https://huggingface.co/docs/transformers/en/main_classes/model#transformers.PreTrainedModel.save_pretrained) rather than `torch.save`, as models in the Transformers library require both the weights and the configuration saved. This can then be reloaded using the `from_pretrained` method (which can be used in your Flask app). It should point to a folder containing the config.json file and the weights, which are now by default saved using the [safetensors](https://github.com/huggingface/safetensors) format.

Thank you for your response @NielsRogge, I've tried saving  model according to your recommendation using save_pretrained. During the saving process, there were no difficulties.

```
Some weights of the model checkpoint at UrukHan/wav2vec2-russian were not used when initializing Wav2Vec2ForCTC: ['wav2vec2.encoder.pos_conv_embed.conv.weight_g', 'wav2vec2.encoder.pos_conv_embed.conv.weight_v']
- This IS expected if you are initializing Wav2Vec2ForCTC from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing Wav2Vec2ForCTC from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of Wav2Vec2ForCTC were not initialized from the model checkpoint at UrukHan/wav2vec2-russian and are newly initialized: ['wav2vec2.encoder.pos_conv_embed.conv.parametrizations.weight.original0', 'wav2vec2.encoder.pos_conv_embed.conv.parametrizations.weight.original1']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
```

But, when trying to load the model, I encountered an error loading weights :

```
\transformers\modeling_utils.py", line 532, in load_state_dict
    return torch.load(
           ^^^^^^^^^^^
 \torch\serialization.py", line 1039, in load
    raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
_pickle.UnpicklingError: Weights only load failed. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution.Do it only if you get the file from a trusted source. WeightsUnpickler error: Unsupported operand 123

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "\transformers\modeling_utils.py", line 548, in load_state_dict
    raise ValueError(
ValueError: Unable to locate the file config.json which is necessary to load this pretrained model. Make sure you have saved the model properly.

  File "\transformers\modeling_utils.py", line 553, in load_state_dict
    raise OSError(
OSError: Unable to load weights from pytorch checkpoint file for 'config.json' at 'config.json'. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
```
Any ideas on how to fix that?
I'm using:
```
transformers==4.39.1
tokenizers==0.15.2
torch==2.2.1
safetensors==0.4.2
```


could you share full reproducers? 🤗 

@NielsRogge , @ArthurZucker thanks for feedback!
Usage of save_pretrained fixed the issue.

is there a way to convert a .pt file that is a finetuned version of a huggingface model to safetensors

You should just use `model.from_pretrained()` and automatic conversion will start. There is also https://huggingface.co/docs/safetensors/en/convert-weights that documents it well 

That only supports bin files not .pt files

If renaming the file does not work it means the .pt's state dict is just not compatible