Views
No views yet
[!IMPORTANT] This repository is a community-driven quantized version of the original modelmeta-llama/Meta-Llama-3.1-70B-Instructwhich is the FP16 half-precision official version released by Meta AI.
meta-llama/Meta-Llama-3.1-70B-Instruct quantized using AutoGPTQ from FP16 down to INT4 using the GPTQ kernels performing zero-point quantization with a group size of 128.[!NOTE] In order to run the inference with Llama 3.1 70B Instruct GPTQ in INT4, around 35 GiB of VRAM are needed only for loading the model checkpoint, without including the KV cache or the CUDA graphs, meaning that there should be a bit over that VRAM available.
transformers, autogptq, or text-generation-inference.1pip install -q --upgrade transformers accelerate optimum
2pip install -q --no-build-isolation auto-gptqAutoModelForCausalLM and run the inference normally.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id,
8 torch_dtype=torch.float16,
9 low_cpu_mem_usage=True,
10 device_map="auto",
11)
12
13prompt = [
14 {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
15 {"role": "user", "content": "What's Deep Learning?"},
16]
17inputs = tokenizer.apply_chat_template(
18 prompt,
19 tokenize=True,
20 add_generation_prompt=True,
21 return_tensors="pt",
22 return_dict=True,
23).to("cuda")
24
25outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
26print(tokenizer.batch_decode(outputs, skip_special_tokens=True))1pip install -q --upgrade transformers accelerate optimum
2pip install -q --no-build-isolation auto-gptqAutoGPTQ even though it's built on top of 🤗 transformers, which is the recommended approach instead as described above.1import torch
2from auto_gptq import AutoGPTQForCausalLM
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4"
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoGPTQForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.float16,
10 low_cpu_mem_usage=True,
11 device_map="auto",
12)
13
14prompt = [
15 {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
16 {"role": "user", "content": "What's Deep Learning?"},
17]
18inputs = tokenizer.apply_chat_template(
19 prompt,
20 tokenize=True,
21 add_generation_prompt=True,
22 return_tensors="pt",
23 return_dict=True,
24).to("cuda")
25
26outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
27print(tokenizer.batch_decode(outputs, skip_special_tokens=True))AutoGPTQ/examples/quantization/basic_usage.py.text-generation-launcher with Llama 3.1 70B Instruct GPTQ in INT4 with Marlin kernels for optimized inference speed, you will need to have Docker installed (see installation notes) and the huggingface_hub Python package as you need to login to the Hugging Face Hub.1pip install -q --upgrade huggingface_hub
2huggingface-cli login1docker run --gpus all --shm-size 1g -ti -p 8080:80 \
2 -v hf_cache:/data \
3 -e MODEL_ID=hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4 \
4 -e NUM_SHARD=4 \
5 -e QUANTIZE=gptq \
6 -e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
7 -e MAX_INPUT_LENGTH=4000 \
8 -e MAX_TOTAL_TOKENS=4096 \
9 ghcr.io/huggingface/text-generation-inference:2.2.0[!NOTE] TGI will expose different endpoints, to see all the endpoints available check TGI OpenAPI Specification.
/v1/chat/completions:1curl 0.0.0.0:8080/v1/chat/completions \
2 -X POST \
3 -H 'Content-Type: application/json' \
4 -d '{
5 "model": "tgi",
6 "messages": [
7 {
8 "role": "system",
9 "content": "You are a helpful assistant."
10 },
11 {
12 "role": "user",
13 "content": "What is Deep Learning?"
14 }
15 ],
16 "max_tokens": 128
17 }'huggingface_hub Python client as follows:1import os
2from huggingface_hub import InferenceClient
3
4client = InferenceClient(base_url="http://0.0.0.0:8080", api_key=os.getenv("HF_TOKEN", "-"))
5
6chat_completion = client.chat.completions.create(
7 model="hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4",
8 messages=[
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "What is Deep Learning?"},
11 ],
12 max_tokens=128,
13)1import os
2from openai import OpenAI
3
4client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key=os.getenv("OPENAI_API_KEY", "-"))
5
6chat_completion = client.chat.completions.create(
7 model="tgi",
8 messages=[
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "What is Deep Learning?"},
11 ],
12 max_tokens=128,
13)1docker run --runtime nvidia --gpus all --ipc=host -p 8000:8000 \
2 -v hf_cache:/root/.cache/huggingface \
3 vllm/vllm-openai:latest \
4 --model hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4 \
5 --quantization gptq_marlin \
6 --tensor-parallel-size 4 \
7 --max-model-len 4096/v1/chat/completions:1curl 0.0.0.0:8000/v1/chat/completions \
2 -X POST \
3 -H 'Content-Type: application/json' \
4 -d '{
5 "model": "hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4",
6 "messages": [
7 {
8 "role": "system",
9 "content": "You are a helpful assistant."
10 },
11 {
12 "role": "user",
13 "content": "What is Deep Learning?"
14 }
15 ],
16 "max_tokens": 128
17 }'openai Python client (see installation notes) as follows:1import os
2from openai import OpenAI
3
4client = OpenAI(base_url="http://0.0.0.0:8000/v1", api_key=os.getenv("VLLM_API_KEY", "-"))
5
6chat_completion = client.chat.completions.create(
7 model="hugging-quants/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4",
8 messages=[
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "What is Deep Learning?"},
11 ],
12 max_tokens=128,
13)[!NOTE] In order to quantize Llama 3.1 70B Instruct using AutoGPTQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~140GiB, and an NVIDIA GPU with 40GiB of VRAM to quantize it.
1pip install -q --upgrade transformers accelerate optimum
2pip install -q --no-build-isolation auto-gptqAutoGPTQ/examples/quantization/basic_usage.py.1import random
2
3import numpy as np
4import torch
5
6from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
7from datasets import load_dataset
8from transformers import AutoTokenizer
9
10pretrained_model_dir = "meta-llama/Meta-Llama-3.1-70B-Instruct"
11quantized_model_dir = "meta-llama/Meta-Llama-3.1-70B-Instruct-GPTQ-INT4"
12
13print("Loading tokenizer, dataset, and tokenizing the dataset...")
14tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True)
15dataset = load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="train")
16encodings = tokenizer("\n\n".join(dataset["text"]), return_tensors="pt")
17
18print("Setting random seeds...")
19random.seed(0)
20np.random.seed(0)
21torch.random.manual_seed(0)
22
23print("Setting calibration samples...")
24nsamples = 128
25seqlen = 2048
26calibration_samples = []
27for _ in range(nsamples):
28 i = random.randint(0, encodings.input_ids.shape[1] - seqlen - 1)
29 j = i + seqlen
30 input_ids = encodings.input_ids[:, i:j]
31 attention_mask = torch.ones_like(input_ids)
32 calibration_samples.append({"input_ids": input_ids, "attention_mask": attention_mask})
33
34quantize_config = BaseQuantizeConfig(
35 bits=4, # quantize model to 4-bit
36 group_size=128, # it is recommended to set the value to 128
37 desc_act=True, # set to False can significantly speed up inference but the perplexity may slightly bad
38 sym=True, # using symmetric quantization so that the range is symmetric allowing the value 0 to be precisely represented (can provide speedups)
39 damp_percent=0.1, # see https://github.com/AutoGPTQ/AutoGPTQ/issues/196
40)
41
42# load un-quantized model, by default, the model will always be loaded into CPU memory
43print("Load unquantized model...")
44model = AutoGPTQForCausalLM.from_pretrained(pretrained_model_dir, quantize_config)
45
46# quantize model, the examples should be list of dict whose keys can only be "input_ids" and "attention_mask"
47print("Quantize model with calibration samples...")
48model.quantize(calibration_samples)
49
50# save quantized model using safetensors
51model.save_quantized(quantized_model_dir, use_safetensors=True)