模型简介
模型特点
模型能力
使用案例
许可证:llama3.2
语言:
- 英语
- 德语
- 法语
- 意大利语
- 葡萄牙语
- 印地语
- 西班牙语
- 泰语
任务标签:文本生成
标签: - llama
- llama-3
- neuralmagic
- llmcompressor
基础模型:meta-llama/Llama-3.2-1B-Instruct
Llama-3.2-1B-Instruct-FP8
模型概述
- 模型架构: Llama-3
- 输入: 文本
- 输出: 文本
- 模型优化:
- 激活量化: FP8
- 权重量化: FP8
- 预期用途: 适用于商业和研究用途的多语言场景。与Llama-3.2-1B-Instruct类似,本模型适用于类助手式对话。
- 非适用范围: 任何违反适用法律或法规(包括贸易合规法律)的使用方式。
- 发布日期: 2024年9月25日
- 版本: 1.0
- 许可证: Llama3.2
- 模型开发者: Neural Magic
Llama-3.2-1B-Instruct的量化版本。
在MMLU、ARC-Challenge、GSM-8k、Hellaswag、Winogrande和TruthfulQA等基准测试中,其得分与未量化模型的差距在1.0%以内。
模型优化
该模型通过对Llama-3.2-1B-Instruct的权重进行FP8数据类型量化获得。
此优化将权重和激活的表示位数从16位减少到8位,降低了GPU内存需求(约减少50%),并提高了矩阵乘法计算吞吐量(约提升2倍)。
权重量化还使磁盘空间需求减少了约50%。
仅对Transformer块内线性算子的权重和激活进行量化。
权重采用对称静态逐通道方案量化,其中每个输出通道维度在FP8和浮点表示之间应用固定的线性缩放因子。
激活采用对称逐张量方案量化,其中整个激活张量在FP8和浮点表示之间应用固定的线性缩放因子。
权重通过四舍五入到最近的FP8表示进行量化。
使用llm-compressor库对模型进行量化,校准数据来自Neural Magic的LLM压缩校准数据集中的512条序列。
部署
该模型可使用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": "你是一个海盗聊天机器人,总是用海盗语气回答!"},
{"role": "user", "content": "你是谁?"},
]
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兼容的服务。详见文档。
创建
该模型使用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 Leaderboard评估得分
基准测试 | 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,严格匹配) | 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


