模型简介
模型特点
模型能力
使用案例
🚀 Meta-Llama-3.1-70B-Instruct量化模型(w8a8)
本模型是 Meta-Llama-3.1-70B-Instruct 的量化版本,通过将权重量化为INT8数据类型,降低了GPU内存需求和磁盘空间要求,同时提高了矩阵乘法的计算吞吐量。该模型在多种语言的商业和研究场景中表现出色,适用于类似助手的聊天场景。
🚀 快速开始
你可以使用 vLLM 后端高效部署此模型,以下是一个示例代码:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "neuralmagic/Meta-Llama-3.1-70B-Instruct-quantized.w8a8"
number_gpus = 2
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 兼容的服务,更多详细信息请参阅 文档。
✨ 主要特性
- 多语言支持:支持英语、德语、法语、意大利语、葡萄牙语、印地语、西班牙语和泰语等多种语言。
- 模型优化:采用 INT8 激活量化和权重量化,减少了 GPU 内存需求(约 50%),提高了矩阵乘法计算吞吐量(约 2 倍),同时也减少了磁盘空间需求(约 50%)。
- 高性能表现:在多个基准测试中,该量化模型与未量化的 Meta-Llama-3.1-70B-Instruct 模型表现相近,恢复率较高。
📦 安装指南
文档未提供具体安装步骤,暂不展示。
💻 使用示例
基础用法
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "neuralmagic/Meta-Llama-3.1-70B-Instruct-quantized.w8a8"
number_gpus = 2
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)
高级用法
# 此模型的创建过程示例
from transformers import AutoTokenizer, AutoModelForCausalLM
from datasets import load_dataset
from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
from llmcompressor.modifiers.quantization import GPTQModifier
from llmcompressor.transformers.compression.helpers import calculate_offload_device_map
model_stub = "meta-llama/Meta-Llama-3.1-70B-Instruct"
model_name = model_stub.split("/")[-1]
num_samples = 256
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.map(preprocess_fn)
recipe = GPTQModifier(
targets="Linear",
scheme="W8A8",
ignore=["lm_head"],
dampening_frac=0.1,
)
device_map = calculate_offload_device_map(
model_stub,
reserve_for_hessians=True,
num_gpus=2,
torch_dtype="auto",
)
model = AutoModelForCausalLM.from_pretrained(
model_stub,
device_map="auto",
dtype="auto",
)
oneshot(
model=model,
dataset=ds,
recipe=recipe,
max_seq_length=max_seq_len,
num_calibration_samples=num_samples,
)
save_path = model_name + "-quantized.w8a8"
model.save_pretrained(save_path)
tokenizer.save_pretrained(save_path)
print(f"Model and tokenizer saved to: {save_path}")
📚 详细文档
模型概述
- 模型架构:LlamaForCausalLM
- 输入:文本
- 输出:文本
- 模型优化:
- 激活量化:INT8
- 权重量化:INT8
- 预期用例:适用于多种语言的商业和研究用途,与 Meta-Llama-3.1-70B-Instruct 类似,该模型用于类似助手的聊天场景。
- 适用范围外:禁止以任何违反适用法律法规(包括贸易合规法律)的方式使用。
- 发布日期:2024 年 7 月 29 日
- 版本:1.0
- 许可证:Llama3.1
- 模型开发者:Neural Magic
模型优化
该模型通过将 Meta-Llama-3.1-70B-Instruct 的权重量化为 INT8 数据类型获得。这种优化将表示权重和激活的位数从 16 位减少到 8 位,降低了 GPU 内存需求(约 50%),提高了矩阵乘法的计算吞吐量(约 2 倍)。权重量化还将磁盘空间需求减少了约 50%。
仅对 Transformer 块内线性算子的权重和激活进行量化。权重采用对称静态每通道方案进行量化,为每个输出通道维度在 INT8 和浮点表示之间应用固定的线性缩放因子。激活采用对称动态每令牌方案进行量化,在运行时为每个令牌计算 INT8 和浮点表示之间的线性缩放因子。量化使用 GPTQ 算法,该算法在 llm-compressor 库中实现。GPTQ 使用 10% 的阻尼因子和从 Neural Magic 的 LLM 压缩校准数据集 中选取的 256 个序列。
模型部署
此模型可以使用 vLLM 后端高效部署。
模型创建
该模型使用 llm-compressor 库创建,具体代码见上文高级用法示例。
模型评估
该模型在著名的 Arena-Hard、OpenLLM v1、OpenLLM v2、HumanEval 和 HumanEval+ 基准测试中进行了评估。所有情况下,模型输出均使用 vLLM 引擎生成。
- Arena-Hard 评估:使用 Arena-Hard-Auto 仓库进行。模型为 Arena-Hard 中的每个提示生成一个答案,每个答案由 GPT-4 评判两次。
- OpenLLM v1 和 v2 评估:使用 Neural Magic 对 lm-evaluation-harness 的分支(llama_3.1_instruct)进行。此版本的 lm-evaluation-harness 包含与 Meta-Llama-3.1-Instruct-evals 提示风格匹配的 MMLU、ARC-Challenge 和 GSM-8K 版本,并对 OpenLLM v2 任务进行了一些修复。
- HumanEval 和 HumanEval+ 评估:使用 Neural Magic 对 EvalPlus 仓库的分支进行。
详细的模型输出可作为 HuggingFace 数据集获取,分别为 Arena-Hard、OpenLLM v2 和 HumanEval。
注意:在 Meta 修改聊天模板后,结果已更新。
准确性
基准测试 | Meta-Llama-3.1-70B-Instruct | Meta-Llama-3.1-70B-Instruct 量化模型(w8a8) | 恢复率 |
---|---|---|---|
Arena Hard | 57.0 (55.8 / 58.2) | 56.3 (56.0 / 56.6) | 98.8% |
OpenLLM v1 | |||
MMLU (5-shot) | 83.9 | 83.7 | 99.7% |
MMLU (CoT, 0-shot) | 86.2 | 85.8 | 99.5% |
ARC Challenge (0-shot) | 93.3 | 93.1 | 99.7% |
GSM-8K (CoT, 8-shot, strict-match) | 95.4 | 94.2 | 98.8% |
Hellaswag (10-shot) | 86.7 | 86.7 | 100.0% |
Winogrande (5-shot) | 85.3 | 85.1 | 100.1% |
TruthfulQA (0-shot, mc2) | 60.7 | 61.4 | 101.3% |
平均 | 84.5 | 84.3 | 99.9% |
OpenLLM v2 | |||
MMLU-Pro (5-shot) | 48.1 | 47.1 | 97.9% |
IFEval (0-shot) | 86.4 | 86.6 | 100.2% |
BBH (3-shot) | 55.8 | 55.2 | 98.9% |
Math- | v | -5 (4-shot) | 26.1 |
GPQA (0-shot) | 15.4 | 13.6 | 88.4% |
MuSR (0-shot) | 18.2 | 16.8 | 92.6% |
平均 | 41.7 | 40.5 | 97.3% |
编码 | |||
HumanEval pass@1 | 79.7 | 78.7 | 98.7% |
HumanEval+ pass@1 | 74.8 | 74.0 | 98.9% |
复现结果
可以使用以下命令复现评估结果:
MMLU
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",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/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
--tasks truthfulqa \
--num_fewshot 0 \
--batch_size auto
OpenLLM v2
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Meta-Llama-3.1-70B-Instruct-quantized.w8a8",dtype=auto,max_model_len=4096,tensor_parallel_size=1",enable_chunked_prefill=True \
--apply_chat_template \
--fewshot_as_multiturn \
--tasks leaderboard \
--batch_size auto
HumanEval 和 HumanEval+
生成
python3 codegen/generate.py \
--model neuralmagic/Meta-Llama-3.1-70B-Instruct-quantized.w8a8 \
--bs 16 \
--temperature 0.2 \
--n_samples 50 \
--root "." \
--dataset humaneval
清理
python3 evalplus/sanitize.py \
humaneval/neuralmagic--Meta-Llama-3.1-70B-Instruct-quantized.w8a8_vllm_temp_0.2
评估
evalplus.evaluate \
--dataset humaneval \
--samples humaneval/neuralmagic--Meta-Llama-3.1-70B-Instruct-quantized.w8a8_vllm_temp_0.2-sanitized
🔧 技术细节
该模型的技术细节主要体现在量化过程中。仅对 Transformer 块内线性算子的权重和激活进行量化。权重采用对称静态每通道方案进行量化,为每个输出通道维度在 INT8 和浮点表示之间应用固定的线性缩放因子。激活采用对称动态每令牌方案进行量化,在运行时为每个令牌计算 INT8 和浮点表示之间的线性缩放因子。量化使用 GPTQ 算法,该算法在 llm-compressor 库中实现。GPTQ 使用 10% 的阻尼因子和从 Neural Magic 的 LLM 压缩校准数据集 中选取的 256 个序列。
📄 许可证
本模型使用 Llama3.1 许可证。



