模型概述
模型特點
模型能力
使用案例
🚀 Llama-3.2-1B-Instruct-FP8
Llama-3.2-1B-Instruct-FP8 是 Llama-3.2-1B-Instruct 的量化版本,在多語言處理上表現出色,適用於商業和研究用途。它通過量化優化,減少了 GPU 內存需求和磁盤空間佔用,同時保持了較高的性能。
🚀 快速開始
此模型可以使用 vLLM 後端高效部署,以下是一個示例代碼:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "neuralmagic/Llama-3.2-1B-Instruct-FP8"
number_gpus = 1
max_model_len = 8192
sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
tokenizer = AutoTokenizer.from_pretrained(model_id)
messages = [
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
{"role": "user", "content": "Who are you?"},
]
prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
llm = LLM(model=model_id, tensor_parallel_size=number_gpus, max_model_len=max_model_len)
outputs = llm.generate(prompts, sampling_params)
generated_text = outputs[0].outputs[0].text
print(generated_text)
vLLM 還支持與 OpenAI 兼容的服務,更多詳細信息請參閱 文檔。
✨ 主要特性
- 多語言支持:支持英語、德語、法語、意大利語、葡萄牙語、印地語、西班牙語和泰語等多種語言。
- 模型優化:通過將權重和激活量化為 FP8 數據類型,減少了 GPU 內存需求(約 50%),提高了矩陣乘法計算吞吐量(約 2 倍),同時也減少了磁盤大小需求(約 50%)。
- 高性能表現:在 MMLU、ARC-Challenge、GSM-8k、Hellaswag、Winogrande 和 TruthfulQA 等基準測試中,得分與未量化模型的得分相差在 1.0% 以內。
📦 安裝指南
文檔中未提及具體安裝步驟,可參考相關依賴庫(如 vLLM、transformers 等)的官方文檔進行安裝。
💻 使用示例
基礎用法
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "neuralmagic/Llama-3.2-1B-Instruct-FP8"
number_gpus = 1
max_model_len = 8192
sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
tokenizer = AutoTokenizer.from_pretrained(model_id)
messages = [
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
{"role": "user", "content": "Who are you?"},
]
prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
llm = LLM(model=model_id, tensor_parallel_size=number_gpus, max_model_len=max_model_len)
outputs = llm.generate(prompts, sampling_params)
generated_text = outputs[0].outputs[0].text
print(generated_text)
高級用法
文檔中未明確提及高級用法相關代碼,可根據具體需求調整參數或結合其他技術進行使用。
📚 詳細文檔
模型概述
屬性 | 詳情 |
---|---|
模型類型 | Llama-3 |
輸入 | 文本 |
輸出 | 文本 |
模型優化 | 激活量化:FP8;權重量化:FP8 |
預期用例 | 適用於多語言的商業和研究用途,類似於 Llama-3.2-1B-Instruct,用於類似助手的聊天場景 |
超出範圍 | 任何違反適用法律法規(包括貿易合規法律)的使用方式 |
發佈日期 | 2024 年 9 月 25 日 |
版本 | 1.0 |
許可證 | Llama3.2 |
模型開發者 | Neural Magic |
模型優化
該模型通過將 Llama-3.2-1B-Instruct 的權重量化為 FP8 數據類型獲得。這種優化將表示權重和激活的位數從 16 位減少到 8 位,從而減少了 GPU 內存需求(約 50%),並提高了矩陣乘法計算吞吐量(約 2 倍)。權重量化還將磁盤大小需求減少了約 50%。
僅對 Transformer 塊內線性算子的權重和激活進行量化。權重採用對稱靜態逐通道方案進行量化,即對每個輸出通道維度在 FP8 和浮點表示之間應用固定的線性縮放因子。激活採用對稱逐張量方案進行量化,即對整個激活張量在 FP8 和浮點表示之間應用固定的線性縮放因子。權重通過四捨五入到最接近的 FP8 表示進行量化。使用 llm-compressor 庫對模型進行量化,使用了來自 Neural Magic 的 LLM 壓縮校準數據集 的 512 個序列。
模型創建
該模型使用 llm-compressor 庫創建,以下是創建模型的代碼示例:
from transformers import AutoTokenizer
from datasets import load_dataset
from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier
model_id = "meta-llama/Llama-3.2-1B-Instruct"
num_samples = 512
max_seq_len = 8192
tokenizer = AutoTokenizer.from_pretrained(model_id)
def preprocess_fn(example):
return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
ds = ds.shuffle().select(range(num_samples))
ds = ds.map(preprocess_fn)
recipe = QuantizationModifier(
targets="Linear",
scheme="FP8",
ignore=["lm_head"],
)
]
model = SparseAutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
)
oneshot(
model=model,
dataset=ds,
recipe=recipe,
max_seq_length=max_seq_len,
num_calibration_samples=num_samples,
)
model.save_pretrained("Llama-3.2-1B-Instruct-FP8")
模型評估
該模型在 MMLU、ARC-Challenge、GSM-8K、Hellaswag、Winogrande 和 TruthfulQA 上進行了評估。評估使用了 Neural Magic 對 lm-evaluation-harness 的分支(llama_3.1_instruct 分支)和 vLLM 引擎。此版本的 lm-evaluation-harness 包含與 Meta-Llama-3.1-Instruct-evals 提示風格匹配的 MMLU、ARC-Challenge 和 GSM-8K 版本。
準確性
Open LLM 排行榜評估得分
基準測試 | Llama-3.2-1B-Instruct | Llama-3.2-1B-Instruct-FP8 (本模型) | 恢復率 |
---|---|---|---|
MMLU (5-shot) | 47.66 | 47.76 | 100.2% |
MMLU (CoT, 0-shot) | 47.10 | 47.24 | 94.8% |
ARC Challenge (0-shot) | 58.36 | 57.85 | 99.1% |
GSM-8K (CoT, 8-shot, strict-match) | 45.72 | 45.49 | 99.5% |
Hellaswag (10-shot) | 61.01 | 61.00 | 100.0% |
Winogrande (5-shot) | 62.27 | 62.35 | 100.1% |
TruthfulQA (0-shot, mc2) | 43.52 | 43.08 | 99.0% |
平均 | 52.24 | 52.11 | 99.8% |
復現結果
使用以下命令可以復現評估結果:
MMLU
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=3850,max_gen_toks=10,tensor_parallel_size=1 \
--tasks mmlu_llama_3.1_instruct \
--fewshot_as_multiturn \
--apply_chat_template \
--num_fewshot 5 \
--batch_size auto
MMLU-CoT
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=4064,max_gen_toks=1024,tensor_parallel_size=1 \
--tasks mmlu_cot_0shot_llama_3.1_instruct \
--apply_chat_template \
--num_fewshot 0 \
--batch_size auto
ARC-Challenge
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=3940,max_gen_toks=100,tensor_parallel_size=1 \
--tasks arc_challenge_llama_3.1_instruct \
--apply_chat_template \
--num_fewshot 0 \
--batch_size auto
GSM-8K
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=4096,max_gen_toks=1024,tensor_parallel_size=1 \
--tasks gsm8k_cot_llama_3.1_instruct \
--fewshot_as_multiturn \
--apply_chat_template \
--num_fewshot 8 \
--batch_size auto
Hellaswag
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
--tasks hellaswag \
--num_fewshot 10 \
--batch_size auto
Winogrande
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
--tasks winogrande \
--num_fewshot 5 \
--batch_size auto
TruthfulQA
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Llama-3.2-1B-Instruct-FP8",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
--tasks truthfulqa \
--num_fewshot 0 \
--batch_size auto
🔧 技術細節
量化方案
- 權重量化:採用對稱靜態逐通道方案,對每個輸出通道維度在 FP8 和浮點表示之間應用固定的線性縮放因子。
- 激活量化:採用對稱逐張量方案,對整個激活張量在 FP8 和浮點表示之間應用固定的線性縮放因子。
量化過程
權重通過四捨五入到最接近的 FP8 表示進行量化。使用 llm-compressor 庫對模型進行量化,使用了來自 Neural Magic 的 LLM 壓縮校準數據集 的 512 個序列。
📄 許可證
本模型使用 Llama3.2 許可證。



