Views
No views yet
[!IMPORTANT] This repository is a community-driven quantized version of the original modelgoogle/gemma-2-9b-itwhich is the BF16 half-precision official version released by Google.
[!WARNING] This model has been quantized usingtransformers4.45.0, meaning that the tokenizer available in this repository won't be compatible with lower versions. Same applies for e.g. Text Generation Inference (TGI) that only installstransformers4.45.0 or higher starting in v2.3.1.
google/gemma-2-9b-it 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 Gemma2 9B Instruct AWQ in INT4, around 6 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.1pip install -q --upgrade "transformers>=4.45.0" accelerate
2INSTALL_KERNELS=1 pip install -q git+https://github.com/casper-hansen/AutoAWQ.git@79547665bdb27768a9b392ef375776b020acbf0cAutoModelForCausalLM and run the inference normally.1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
3
4model_id = "hugging-quants/gemma-2-9b-it-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": "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])1pip install -q --upgrade "transformers>=4.45.0" accelerate
2INSTALL_KERNELS=1 pip install -q git+https://github.com/casper-hansen/AutoAWQ.git@79547665bdb27768a9b392ef375776b020acbf0cAutoAWQ 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/gemma-2-9b-it-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": "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[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])AutoAWQ/examples/generate.py.text-generation-launcher with Gemma2 9B Instruct AWQ in INT4 with Marlin kernels for optimized inference speed, you will need to have Docker installed (see installation notes).1docker run --gpus all --shm-size 1g -ti -p 8080:80 \
2 -v hf_cache:/data \
3 -e MODEL_ID=hugging-quants/gemma-2-9b-it-AWQ-INT4 \
4 -e QUANTIZE=awq \
5 -e MAX_INPUT_LENGTH=4000 \
6 -e MAX_TOTAL_TOKENS=4096 \
7 ghcr.io/huggingface/text-generation-inference:2.3.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": "user",
9 "content": "What is Deep Learning?"
10 }
11 ],
12 "max_tokens": 128
13 }'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="-")
5
6chat_completion = client.chat.completions.create(
7 model="hugging-quants/gemma-2-9b-it-AWQ-INT4",
8 messages=[
9 {"role": "user", "content": "What is Deep Learning?"},
10 ],
11 max_tokens=128,
12)1import os
2from openai import OpenAI
3
4client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key="-")
5
6chat_completion = client.chat.completions.create(
7 model="tgi",
8 messages=[
9 {"role": "user", "content": "What is Deep Learning?"},
10 ],
11 max_tokens=128,
12)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/gemma-2-9b-it-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/gemma-2-9b-it-AWQ-INT4",
6 "messages": [
7 {
8 "role": "user",
9 "content": "What is Deep Learning?"
10 }
11 ],
12 "max_tokens": 128
13 }'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/gemma-2-9b-it-AWQ-INT4",
8 messages=[
9 {"role": "user", "content": "What is Deep Learning?"},
10 ],
11 max_tokens=128,
12)[!IMPORTANT] In order to quantize Gemma2 9B Instruct using AutoAWQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~20GiB, and an NVIDIA GPU with 16GiB of VRAM to quantize it.Additionally, you also need to accept the Gemma2 access conditions, as it is a gated model that requires accepting those first.
1pip install -q --upgrade "torch==2.3.0" "transformers>=4.45.0" accelerate
2INSTALL_KERNELS=1 pip install -q git+https://github.com/casper-hansen/AutoAWQ.git@79547665bdb27768a9b392ef375776b020acbf0chuggingface_hub Python SDK and login to the Hugging Face Hub.1pip install -q --upgrade huggingface_hub
2huggingface-cli loginAutoAWQ/examples/quantize.py:1from awq import AutoAWQForCausalLM
2from transformers import AutoTokenizer
3
4model_path = "google/gemma-2-9b-it"
5quant_path = "hugging-quants/gemma-2-9b-it-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}"')