Views
No views yet
| 原始模型 | INT8 量化 | INT4 量化 | |
|---|---|---|---|
| 精度 | FP32 | INT8 | INT4 |
| 大小 | ~6.45 GB | ~1.91 GB | ~1.29 GB |
| 品質 | 基準 | 幾乎無損 | 略有下降 |
embed_tokens和lm_head保持 FP16 精度,避免量化過度造成輸出品質下降。
1from optimum.quanto import QuantizedModelForCausalLM
2from transformers import AutoTokenizer
3import torch
4
5model_name = "renhehuang/qwen3-1.7b-coffee-sft-quanto-int8"
6tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
7model = QuantizedModelForCausalLM.from_pretrained(model_name)
8
9messages = [
10 {"role": "system", "content": "你是一位專業的咖啡點餐助理,負責協助使用者完成點餐。菜單包含:美式、拿鐵、燕麥奶拿鐵、鮮奶。"},
11 {"role": "user", "content": "我想要一杯冰拿鐵"}
12]
13
14input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
15inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
16
17with torch.no_grad():
18 outputs = model.generate(
19 **inputs,
20 max_new_tokens=128,
21 do_sample=True,
22 temperature=0.7,
23 top_p=0.9,
24 )
25
26response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
27print(response)| 項目 | 值 |
|---|---|
| 量化工具 | optimum-quanto |
| 量化精度 | INT8 (qint8) |
| 量化範圍 | weights only(排除 embed_tokens、lm_head) |
| 原始模型 | renhehuang/qwen3-1.7b-coffee-sft |
| 基礎模型 | Qwen/Qwen3-1.7B |