Views
No views yet
[!IMPORTANT] This repository is a community-driven quantized version of the original modelmeta-llama/Meta-Llama-3.1-8B-Instructwhich is the BF16 half-precision official version released by Meta AI.
meta-llama/Meta-Llama-3.1-8B-Instruct quantized using AutoAWQ from FP16 down to INT4 using the GEMM kernels performing zero-point quantization with a group size of 128.[!NOTE] In order to run the inference with Llama 3.1 8B Instruct AWQ in INT4, around 4 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, autoawq, or text-generation-inference.pip install -q --upgrade transformers autoawq accelerateAutoModelForCausalLM and run the inference normally.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
3
4model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
5
6quantization_config = AwqConfig(
7 bits=4,
8 fuse_max_seq_len=512, # Note: Update this as per your use-case
9 do_fuse=True,
10)
11
12tokenizer = AutoTokenizer.from_pretrained(model_id)
13model = AutoModelForCausalLM.from_pretrained(
14 model_id,
15 torch_dtype=torch.float16,
16 low_cpu_mem_usage=True,
17 device_map="auto",
18 quantization_config=quantization_config
19)
20
21prompt = [
22 {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
23 {"role": "user", "content": "What's Deep Learning?"},
24]
25inputs = tokenizer.apply_chat_template(
26 prompt,
27 tokenize=True,
28 add_generation_prompt=True,
29 return_tensors="pt",
30 return_dict=True,
31).to("cuda")
32
33outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
34print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])pip install -q --upgrade transformers autoawq accelerateAutoAWQ even though it's built on top of 🤗 transformers, which is the recommended approach instead as described above.1import torch
2from awq import AutoAWQForCausalLM
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoAWQForCausalLM.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[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])AutoAWQ/examples/generate.py.text-generation-launcher with Llama 3.1 8B Instruct AWQ 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-8B-Instruct-AWQ-INT4 \
4 -e QUANTIZE=awq \
5 -e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
6 -e MAX_INPUT_LENGTH=4000 \
7 -e MAX_TOTAL_TOKENS=4096 \
8 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-8B-Instruct-AWQ-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-8B-Instruct-AWQ-INT4 \
5 --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-8B-Instruct-AWQ-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-8B-Instruct-AWQ-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 8B Instruct using AutoAWQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~8GiB, and an NVIDIA GPU with 16GiB of VRAM to quantize it.
pip install -q --upgrade transformers autoawq accelerateAutoAWQ/examples/quantize.py:1from awq import AutoAWQForCausalLM
2from transformers import AutoTokenizer
3
4model_path = "meta-llama/Meta-Llama-3.1-8B-Instruct"
5quant_path = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
6quant_config = {
7 "zero_point": True,
8 "q_group_size": 128,
9 "w_bit": 4,
10 "version": "GEMM",
11}
12
13# Load model
14model = AutoAWQForCausalLM.from_pretrained(
15 model_path, low_cpu_mem_usage=True, use_cache=False,
16)
17tokenizer = AutoTokenizer.from_pretrained(model_path)
18
19# Quantize
20model.quantize(tokenizer, quant_config=quant_config)
21
22# Save quantized model
23model.save_quantized(quant_path)
24tokenizer.save_pretrained(quant_path)
25
26print(f'Model is quantized and saved at "{quant_path}"')