-
Update the save_weights_only Configuration: If you are using the Trainer and saved only the weights, check if the configuration is set to save in the SafeTensors format. You can explicitly set your saving mechanisms in the TrainingArguments when initializing your Trainer. Here’s an example:
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./checkpoints",
save_strategy="epoch", # or "steps"
save_total_limit=3, # Limit the total amount of checkpoints, delete the older checkpoints
save_weights_only=False, # Set to False to save the full model as .bin
# other arguments ...
)
Setting save_weights_only=False will ensure that the full model gets saved, including the config files.
-
Check Your Hugging Face Transformers Library Version: Ensure you are using a relatively recent version of the Hugging Face Transformers library. The SafeTensors format is supported in newer releases, so consider upgrading if you are on an older version:
pip install --upgrade transformers
-
Loading SafeTensors Models: If your model is saved in the SafeTensors format, you can load it using the same APIs for loading models. The from_pretrained method can automatically handle the SafeTensors format as long as the directory structure is correct:
from transformers import BlipProcessor, BlipForConditionalGeneration
model_directory = "./your_model_directory"
processor = BlipProcessor.from_pretrained(model_directory)
model = BlipForConditionalGeneration.from_pretrained(model_directory)
The key difference is that you don't need to worry about the file extension; the library will manage the format transparently.
-
Convert SafeTensors to PyTorch Format (if needed): If you specifically need a pytorch_model.bin, you can load the model.safetensors file and then save it again as a .bin file:
from safetensors.torch import load_file
import torch
# Load the SafeTensors model
state_dict = load_file("./checkpoints/model.safetensors")
# Create a model instance
model = BlipForConditionalGeneration.from_config('./your_model_directory/config.json')
# Load state dict
model.load_state_dict(state_dict)
# Save as PyTorch model
torch.save(model.state_dict(), "./checkpoints/pytorch_model.bin")
-
Consult the Documentation: If this behavior is unexpected or you are unsure about settings, check the documentation or release notes for transformers and safetensors to understand the latest features and configurations.
By ensuring that your training and checkpointing configurations are set correctly, you can control the format in which your model weights are saved and ensure smooth loading later on.