Repository navigation
Closed
Description
I used the safetensors model saved by save_pretrained_gguf, but the output did not have the fine-tuned lora parameters
import os, torch
from unsloth import FastLanguageModel
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # 只使用 3070 Ti
MODEL_DIR = "model" # ← 你的合并后HF目录
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
os.environ["http_proxy"] = "http://192.168.1.222:8080"
os.environ["https_proxy"] = "http://192.168.1.222:8080"
# 建议用半精度推理;若显存紧缺可换 torch.float16
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = MODEL_DIR,
max_seq_length = 2048,
load_in_4bit = True, # 合并后通常按全精/8bit保存;若你确实保存为4bit可设 True
# torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32,
)
# FastLanguageModel.for_inference(model) # 推理加速
if model.device.type != DEVICE:
model.to(DEVICE)
model.eval()
def chat(user_text: str, max_new_tokens: int = 2048) -> str:
messages = [
# {"role":"user","content":"你是一个体能训练大模型"},
{"role":"user","content": user_text},
]
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True,
# 注意:不是 reasoning 基座就不要开 enable_thinking
enable_thinking=True,
)
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
with torch.no_grad():
output_ids = model.generate(
**inputs,
do_sample=True, temperature=0.6, top_p=0.95, top_k=20,
max_new_tokens=max_new_tokens,
renormalize_logits=True, # 防止数值异常
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id,
)
new_ids = output_ids[0, inputs["input_ids"].shape[1]:]
return tokenizer.decode(new_ids, skip_special_tokens=True).strip()
print(chat("你是一个体能训练大模型,你是谁开发的?"))
Activity
rolandtannous commentedon Aug 28, 2025
Hello,
Use
save_pretrained_mergedto merge base model+lorasAlso we are currently dealing with some issues related to save_pretrained_gguf that we are working on resolving. Meanwhile there is some code that you could manually use with llama.cpp to convert to gguf : https://colab.research.google.com/drive/1EevDuEa_CYZt59eMGl7acB6DhlApj61O?usp=sharing
Please take note that if you are trying to save a vision model you might have to also adjust the code i linked above to align with the docs at llama.cpp as shown here:
https://github.com/ggml-org/llama.cpp/tree/master/docs/multimodal
Hopefully we'll be resolving the gguf saving issues soon.
Thanks
rolandtannous commentedon Oct 14, 2025
@songjiahao-wq
We just pushed a major fix for llamacpp integration.
1- Please make sure to update your local install of unsloth
If you're using a notebook on kaggle or colab, it should pull the latest version automatically.
2- You can now use
save_pretrained_ggufto quantize and save to GGUF format:For vision/multimodal models, use
tokenizer=processor3- You can pass it one or several quant methods (as a list):
or
We support all the quants mentioned in this page except the iq variants.
4- If the model is supported by Ollama, it will now also automatically create a
Modelfilefor you that you can use to create your model in ollama using:5- Use
push_to_hub_ggufif you want to convert to gguf and push to Huggingface Hub instead.6- Should work for all models supported by unsloth and ollama.
7- It now fully supports linux debian and RPM variants. We are still testing windows (if someone has a windows machine and is on our discord channel ping me to help debug)
Have fun
rolandtannous commentedon Oct 23, 2025
Will be closing this for now as the issue has been resolved. Please make sure to follow the instructions given in my last response.
Meanwhile , please feel free to respond if you're still having any issues.
Thank you