Views
No views yet
[!IMPORTANT] This repository is an AWQ 4-bit quantized version of thenvidia/Llama-3.1-Nemotron-70B-Instruct-HFmodel, which is an NVIDIA customized version ofmeta-llama/Meta-Llama-3.1-70B-Instruct, originally released by Meta AI.
[!NOTE] Note from Terrell: Quantization to AWQ 4-bit will further affect evaluation results.
transformers, autoawq, or text-generation-inference.[!NOTE] In order to run inference with Llama 3.1 Nemotron 70B Instruct AWQ in INT4, around 35 GiB of VRAM are needed 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.
pip install -q --upgrade transformers autoawq accelerateAutoModelForCausalLM. Run inference as usual.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
3
4model_id = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4"
5quantization_config = AwqConfig(
6 bits=4,
7 fuse_max_seq_len=512, # Note: Update this as per your use-case
8 do_fuse=True,
9)
10
11tokenizer = AutoTokenizer.from_pretrained(model_id)
12model = AutoModelForCausalLM.from_pretrained(
13 model_id,
14 torch_dtype=torch.float16,
15 low_cpu_mem_usage=True,
16 device_map="auto",
17 quantization_config=quantization_config
18)
19
20prompt = [
21 {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
22 {"role": "user", "content": "What's Deep Learning?"},
23]
24inputs = tokenizer.apply_chat_template(
25 prompt,
26 tokenize=True,
27 add_generation_prompt=True,
28 return_tensors="pt",
29 return_dict=True,
30).to("cuda")
31
32outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
33print(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 = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-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])text-generation-launcher with Llama 3.1 Nemotron 70B 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=ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4 \
4 -e NUM_SHARD=4 \
5 -e QUANTIZE=awq \
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="ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-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 ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4 \
5 --tensor-parallel-size 4 \
6 --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": "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-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="ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-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 Nemotron 70B Instruct using AutoAWQ, 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.
pip install -q --upgrade transformers autoawq accelerate1from awq import AutoAWQForCausalLM
2from transformers import AutoTokenizer
3import torch
4
5# Empty Cache
6torch.cuda.empty_cache()
7
8# Memory Limits - Set this according to your hardware limits
9max_memory = {0: "22GiB", 1: "22GiB", "cpu": "160GiB"}
10
11model_path = "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
12quant_path = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4"
13quant_config = {
14 "zero_point": True,
15 "q_group_size": 128,
16 "w_bit": 4,
17 "version": "GEMM"
18
19}
20
21# Load model - Note: while this loads the layers into the CPU, the GPUs (and the VRAM) are still required for quantization! (Verified with nvida-smi)
22model = AutoAWQForCausalLM.from_pretrained(
23 model_path,
24 use_cache=False,
25 max_memory=max_memory,
26 device_map="cpu"
27)
28
29tokenizer = AutoTokenizer.from_pretrained(model_path)
30
31# Quantize
32model.quantize(
33 tokenizer,
34 quant_config=quant_config
35)
36
37# Save quantized model
38model.save_quantized(quant_path)
39tokenizer.save_pretrained(quant_path)
40
41print(f'Model is quantized and saved at "{quant_path}"')