Phi 4 Mini Instruct 8da4w
模型概述
Phi-4-mini 是一個輕量級的自然語言處理模型,適用於代碼生成、數學推理、聊天對話等多種任務。
模型特點
高效量化
採用8位嵌入和8位動態激活,以及4位權重線性層(8da4w)的量化方案,顯著減少模型大小和內存佔用。
移動端部署
支持通過 ExecuTorch 在移動設備上運行,適合資源受限的環境。
高性能推理
在 iPhone 15 Pro 上,模型運行速度為每秒17.3個令牌,內存佔用為3206 MB。
模型能力
文本生成
代碼生成
數學推理
聊天對話
使用案例
自然語言處理
聊天機器人
用於構建高效的聊天機器人,支持多輪對話。
響應速度快,適合移動端應用。
代碼輔助
幫助開發者生成代碼片段或解決編程問題。
支持多種編程語言,生成質量較高。
教育
數學輔導
用於解答數學問題或提供解題思路。
在 GSM8K 數據集上表現良好。
🚀 Phi-4-mini-instruct量化模型
Phi-4-mini-instruct量化模型基於microsoft/Phi-4-mini-instruct
模型,由PyTorch團隊使用torchao
進行量化處理。該模型採用8位嵌入和8位動態激活以及4位權重線性(8da4w)的量化方案,適用於使用ExecuTorch進行移動端部署。我們提供了可直接在ExecuTorch中使用的量化pte文件。
✨ 主要特性
- 量化處理:使用
torchao
進行8位嵌入和8位動態激活以及4位權重線性(8da4w)的量化,減少模型內存佔用。 - 移動端部署:可在移動設備上使用ExecuTorch運行,如iPhone 15 Pro。
- 多語言支持:支持多種語言的文本生成任務。
📦 安裝指南
首先,你需要安裝所需的包:
pip install git+https://github.com/huggingface/transformers@main
pip install torchao
💻 使用示例
基礎用法
以下是如何在移動應用中運行模型的示例:
# 下載pte文件
wget https://huggingface.co/pytorch/Phi-4-mini-instruct-8da4w/blob/main/phi4-mini-8da4w.pte
# 在iOS上運行的說明
https://pytorch.org/executorch/main/llm/llama-demo-ios.html
高級用法
以下是量化模型的詳細步驟:
解綁嵌入權重
from transformers import (
AutoModelForCausalLM,
AutoProcessor,
AutoTokenizer,
)
import torch
model_id = "microsoft/Phi-4-mini-instruct"
untied_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)
print(untied_model)
from transformers.modeling_utils import find_tied_parameters
print("tied weights:", find_tied_parameters(untied_model))
if getattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings"):
setattr(untied_model.config.get_text_config(decoder=True), "tie_word_embeddings", False)
untied_model._tied_weights_keys = []
untied_model.lm_head.weight = torch.nn.Parameter(untied_model.lm_head.weight.clone())
print("tied weights:", find_tied_parameters(untied_model))
USER_ID = "YOUR_USER_ID"
MODEL_NAME = model_id.split("/")[-1]
save_to = f"{USER_ID}/{MODEL_NAME}-untied-weights"
untied_model.push_to_hub(save_to)
tokenizer.push_to_hub(save_to)
# or save locally
save_to_local_path = f"{MODEL_NAME}-untied-weights"
untied_model.save_pretrained(save_to_local_path)
tokenizer.save_pretrained(save_to)
量化模型
from transformers import (
AutoModelForCausalLM,
AutoProcessor,
AutoTokenizer,
TorchAoConfig,
)
from torchao.quantization.quant_api import (
IntxWeightOnlyConfig,
Int8DynamicActivationIntxWeightConfig,
AOPerModuleConfig,
quantize_,
)
from torchao.quantization.granularity import PerGroup, PerAxis
import torch
# we start from the model with untied weights
model_id = "microsoft/Phi-4-mini-instruct"
USER_ID = "YOUR_USER_ID"
MODEL_NAME = model_id.split("/")[-1]
untied_model_id = f"{USER_ID}/{MODEL_NAME}-untied-weights"
untied_model_local_path = f"{MODEL_NAME}-untied-weights"
embedding_config = IntxWeightOnlyConfig(
weight_dtype=torch.int8,
granularity=PerAxis(0),
)
linear_config = Int8DynamicActivationIntxWeightConfig(
weight_dtype=torch.int4,
weight_granularity=PerGroup(32),
weight_scale_dtype=torch.bfloat16,
)
quant_config = AOPerModuleConfig({"_default": linear_config, "model.embed_tokens": embedding_config})
quantization_config = TorchAoConfig(quant_type=quant_config, include_embedding=True, untie_embedding_weights=True, modules_to_not_convert=[])
# either use `untied_model_id` or `untied_model_local_path`
quantized_model = AutoModelForCausalLM.from_pretrained(untied_model_id, torch_dtype=torch.float32, device_map="auto", quantization_config=quantization_config)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Push to hub
MODEL_NAME = model_id.split("/")[-1]
save_to = f"{USER_ID}/{MODEL_NAME}-untied-8da4w"
quantized_model.push_to_hub(save_to, safe_serialization=False)
tokenizer.push_to_hub(save_to)
# Manual testing
prompt = "Hey, are you conscious? Can you talk to me?"
messages = [
{
"role": "system",
"content": "",
},
{"role": "user", "content": prompt},
]
templated_prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
print("Prompt:", prompt)
print("Templated prompt:", templated_prompt)
inputs = tokenizer(
templated_prompt,
return_tensors="pt",
).to("cuda")
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
output_text = tokenizer.batch_decode(
generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Response:", output_text[0][len(prompt):])
📚 詳細文檔
模型質量評估
我們使用lm-evaluation-harness來評估量化模型的質量。
安裝lm-eval
git clone https://github.com/EleutherAI/lm-evaluation-harness
cd lm-evaluation-harness
pip install -e .
基準測試
# 基線模型
lm_eval --model hf --model_args pretrained=microsoft/Phi-4-mini-instruct --tasks hellaswag --device cuda:0 --batch_size 8
# 8da4w量化模型
lm_eval --model hf --model_args pretrained=pytorch/Phi-4-mini-instruct-8da4w --tasks hellaswag --device cuda:0 --batch_size 8
評估結果
基準測試 | Phi-4-mini-ins | Phi-4-mini-instruct-8da4w |
---|---|---|
流行綜合基準測試 | ||
mmlu (0 shot) | 66.73 | 60.75 |
mmlu_pro (5-shot) | 46.43 | 11.75 |
推理能力 | ||
arc_challenge | 56.91 | 48.46 |
gpqa_main_zeroshot | 30.13 | 30.80 |
hellaswag | 54.57 | 50.35 |
openbookqa | 33.00 | 30.40 |
piqa (0-shot) | 77.64 | 74.43 |
siqa | 49.59 | 44.98 |
truthfulqa_mc2 (0-shot) | 48.39 | 51.35 |
winogrande (0-shot) | 71.11 | 70.32 |
多語言能力 | ||
mgsm_en_cot_en | 60.80 | 57.60 |
數學能力 | ||
gsm8k (5-shot) | 81.88 | 61.71 |
Mathqa (0-shot) | 42.31 | 36.95 |
總體表現 | 55.35 | 48.45 |
導出到ExecuTorch
我們可以使用ExecuTorch在移動設備上運行量化模型。
轉換檢查點
python -m executorch.examples.models.phi_4_mini.convert_weights pytorch_model.bin pytorch_model_converted.bin
導出到pte格式
PARAMS="executorch/examples/models/phi_4_mini/config.json"
python -m executorch.examples.models.llama.export_llama \
--model "phi_4_mini" \
--checkpoint "pytorch_model_converted.bin" \
--params "$PARAMS" \
-kv \
--use_sdpa_with_kv_cache \
-X \
--metadata '{"get_bos_id":199999, "get_eos_ids":[200020,199999]}' \
--max_seq_length 128 \
--max_context_length 128 \
--output_name="phi4-mini-8da4w.pte"
🔧 技術細節
- 量化方案:採用8位嵌入和8位動態激活以及4位權重線性(8da4w)的量化方案。
- 移動端性能:在iPhone 15 Pro上,模型運行速度為17.3 tokens/sec,內存佔用為3206 Mb。
📄 許可證
本項目採用MIT許可證。
⚠️ 重要提示
PyTorch尚未對量化模型進行安全評估或紅隊測試。性能特徵、輸出和行為可能與原始模型不同。用戶需自行負責選擇合適的用例,評估和減輕準確性、安全性和公平性問題,確保安全性,並遵守所有適用的法律法規。
本模型卡片中的任何內容均不應被解釋為或視為對模型發佈時所遵循的許可證的限制或修改,包括其中提供的任何責任限制或保修免責聲明。
Phi 2 GGUF
其他
Phi-2是微軟開發的一個小型但強大的語言模型,具有27億參數,專注於高效推理和高質量文本生成。
大型語言模型 支持多種語言
P
TheBloke
41.5M
205
Roberta Large
MIT
基於掩碼語言建模目標預訓練的大型英語語言模型,採用改進的BERT訓練方法
大型語言模型 英語
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基礎模型的蒸餾版本,在保持相近性能的同時更輕量高效,適用於序列分類、標記分類等自然語言處理任務。
大型語言模型 英語
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一個多語言大語言模型,針對多語言對話用例進行了優化,在常見的行業基準測試中表現優異。
大型語言模型 英語
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基於100種語言的2.5TB過濾CommonCrawl數據預訓練的多語言模型,採用掩碼語言建模目標進行訓練。
大型語言模型 支持多種語言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基於Transformer架構的英語預訓練模型,通過掩碼語言建模目標在海量文本上訓練,支持文本特徵提取和下游任務微調
大型語言模型 英語
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI發佈的開放預訓練Transformer語言模型套件,參數量從1.25億到1750億,旨在對標GPT-3系列性能,同時促進大規模語言模型的開放研究。
大型語言模型 英語
O
facebook
6.3M
198
1
基於transformers庫的預訓練模型,適用於多種NLP任務
大型語言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多語言大語言模型系列,包含8B、70B和405B參數規模,支持8種語言和代碼生成,優化了多語言對話場景。
大型語言模型
Transformers 支持多種語言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基礎版是由Google開發的文本到文本轉換Transformer模型,參數規模2.2億,支持多語言NLP任務。
大型語言模型 支持多種語言
T
google-t5
5.4M
702
精選推薦AI模型
Llama 3 Typhoon V1.5x 8b Instruct
專為泰語設計的80億參數指令模型,性能媲美GPT-3.5-turbo,優化了應用場景、檢索增強生成、受限生成和推理任務
大型語言模型
Transformers 支持多種語言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一個基於SODA數據集訓練的超小型對話模型,專為邊緣設備推理設計,體積僅為Cosmo-3B模型的2%左右。
對話系統
Transformers 英語

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基於RoBERTa架構的中文抽取式問答模型,適用於從給定文本中提取答案的任務。
問答系統 中文
R
uer
2,694
98