saving and loading safe tensors
While current llm model mostly uses safe tensor, we can force an existing model to save and load safe tensor by using the following command
!pip install torch
!pip install -U transformers datasets evaluate accelerate timm
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3.5-2B", dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3.5-2B")
model_inputs = tokenizer(["The secret to baking a good cake is "], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_length=30)
tokenizer.batch_decode(generated_ids)[0]
## This is where we save the model as safe tensor
model.save_pretrained("model", safe_serialization=True)
Then we can reload it using this code
model_safe = AutoModelForCausalLM.from_pretrained(
"./model",
trust_remote_code=True
)
generated_ids = model_safe.generate(**model_inputs, max_length=30)
tokenizer.batch_decode(generated_ids)[0]
The link can be found here
https://colab.research.google.com/drive/1x9x2zVUfGGYmlHJ_k7I8O309RWN8fyfh?usp=sharing
Comments