🚀 Meta-Llama-3-70B-Instruct 量化模型(w8a16)
Meta-Llama-3-70B-Instruct-quantized.w8a16 是 Meta-Llama-3-70B-Instruct 的量化版本,主要用於英文的商業和研究用途,能高效地進行類助手聊天。
✨ 主要特性
- 模型架構:基於 Meta-Llama-3 架構,輸入和輸出均為文本。
- 模型優化:對 Meta-Llama-3-70B-Instruct 的權重進行 INT8 量化,將每個參數的位數從 16 位減少到 8 位,使磁盤大小和 GPU 內存需求降低約 50%。僅對 Transformer 塊內線性算子的權重進行量化,採用對稱的逐通道量化。
- 適用場景:適用於英文的商業和研究用途,可用於類助手聊天。
- 不適用場景:禁止用於違反適用法律法規(包括貿易合規法律)的場景,且僅支持英文。
- 發佈日期:2024 年 7 月 2 日
- 版本:1.0
- 許可證:Llama3
- 模型開發者:Neural Magic
該量化模型在 OpenLLM 基準測試(版本 1)中平均得分 77.90,而未量化模型得分為 79.18。
🚀 快速開始
使用 vLLM 部署
可以使用 vLLM 後端高效部署該模型,以下是使用 2 個 GPU 的示例代碼:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "neuralmagic/Meta-Llama-3-70B-Instruct-quantized.w8a16"
number_gpus = 2
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)
outputs = llm.generate(prompts, sampling_params)
generated_text = outputs[0].outputs[0].text
print(generated_text)
vLLM 還支持與 OpenAI 兼容的服務,更多詳情請參閱 文檔。
使用 Transformers 部署
該模型可通過 Transformers 與 AutoGPTQ 數據格式的集成來使用,以下示例展示瞭如何使用 generate()
函數:
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "neuralmagic/Meta-Llama-3-70B-Instruct-quantized.w8a16"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype="auto",
device_map="auto",
)
messages = [
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
{"role": "user", "content": "Who are you?"},
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
outputs = model.generate(
input_ids,
max_new_tokens=256,
eos_token_id=terminators,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
🔧 技術細節
模型創建
該模型使用 AutoGPTQ 庫創建,代碼示例如下:
from transformers import AutoTokenizer
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
from datasets import load_dataset
model_id = "meta-llama/Meta-Llama-3-70B-Instruct"
num_samples = 128
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)
examples = [tokenizer(example["text"], padding=False, max_length=max_seq_len, truncation=True) for example in ds]
quantize_config = BaseQuantizeConfig(
bits=8,
group_size=-1,
desc_act=False,
model_file_base_name="model",
damp_percent=0.1,
)
model = AutoGPTQForCausalLM.from_pretrained(
model_id,
quantize_config,
device_map="auto",
)
model.quantize(examples)
model.save_pretrained("Meta-Llama-3-70B-Instruct-quantized.w8a16")
雖然此模型使用了 AutoGPTQ,但 Neural Magic 正在過渡到使用 llm-compressor,它支持多種量化方案和 AutoGPTQ 不支持的模型。
模型評估
該模型在 OpenLLM 排行榜任務(版本 1)上進行了評估,使用 lm-evaluation-harness(提交版本 383bbd54bc621086e05aa1b030d8d4d5635b25e6)和 vLLM 引擎,使用以下命令(使用 8 個 GPU):
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Meta-Llama-3-70B-Instruct-quantized.w8a16",tensor_parallel_size=8,dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096 \
--tasks openllm \
--batch_size auto
準確率
Open LLM 排行榜評估得分
基準測試 |
Meta-Llama-3-70B-Instruct |
Meta-Llama-3-70B-Instruct-quantized.w8a16(本模型) |
恢復率 |
MMLU (5-shot) |
80.18 |
78.69 |
98.1% |
ARC Challenge (25-shot) |
72.44 |
71.59 |
98.8% |
GSM-8K (5-shot, strict-match) |
90.83 |
86.43 |
95.2% |
Hellaswag (10-shot) |
85.54 |
85.65 |
100.1% |
Winogrande (5-shot) |
83.19 |
83.11 |
98.8% |
TruthfulQA (0-shot) |
62.92 |
61.94 |
98.4% |
平均 |
79.18 |
77.90 |
98.4% |
📄 許可證
本模型使用 Llama3 許可證。