reach-vb HF staff commited on
Commit
88a6bd6
1 Parent(s): 62c1d84

Create README.md (#1)

Browse files

- Create README.md (625581a2a3dad81f6bc966fd3b0a51eacbfbee93)

Files changed (1) hide show
  1. README.md +173 -0
README.md ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ language:
4
+ - en
5
+ - de
6
+ - fr
7
+ - it
8
+ - pt
9
+ - hi
10
+ - es
11
+ - th
12
+ library_name: transformers
13
+ pipeline_tag: text-generation
14
+ tags:
15
+ - llama-3.1
16
+ - meta
17
+ - autoawq
18
+ ---
19
+
20
+ > [!IMPORTANT]
21
+ > This repository is a community-driven quantized version of the original model [`meta-llama/Meta-Llama-3.1-8B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) which is the BF16 half-precision official version released by Meta AI.
22
+
23
+ ## Model Information
24
+
25
+ The Meta Llama 3.1 collection of multilingual large language models (LLMs) is a collection of pretrained and instruction tuned generative models in 8B, 70B and 405B sizes (text in/text out). The Llama 3.1 instruction tuned text only models (8B, 70B, 405B) are optimized for multilingual dialogue use cases and outperform many of the available open source and closed chat models on common industry benchmarks.
26
+
27
+ This repository contains [`meta-llama/Meta-Llama-3.1-8B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) quantized using [AutoAWQ](https://github.com/casperhansen/AutoAWQ) from FP16 down to INT4 using the GEMM kernels performing zero-point quantization with a group size of 128.
28
+
29
+ ## Model Usage
30
+
31
+ > [!NOTE]
32
+ > 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.
33
+
34
+ In order to use the current quantized model, support is offered for different solutions as `transformers`, `autoawq`, or `text-generation-inference`.
35
+
36
+ ### 🤗 transformers
37
+
38
+ In order to run the inference with Llama 3.1 8B Instruct AWQ in INT4, both `torch` and `autoawq` need to be installed as:
39
+
40
+ ```bash
41
+ pip install "torch>=2.2.0,<2.3.0" autoawq --upgrade
42
+ ```
43
+
44
+ Then, the latest version of `transformers` need to be installed, being 4.43.0 or higher, as:
45
+
46
+ ```bash
47
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
48
+ ```
49
+
50
+ To run the inference on top of Llama 3.1 8B Instruct AWQ in INT4 precision, the AWQ model can be instantiated as any other causal language modeling model via `AutoModelForCausalLM` and run the inference normally.
51
+
52
+ ```python
53
+ import torch
54
+ from transformers import AutoModelForCausalLM, AutoTokenizer
55
+
56
+ model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
57
+ prompt = [
58
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
59
+ {"role": "user", "content": "What's Deep Learning?"},
60
+ ]
61
+
62
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
63
+
64
+ inputs = tokenizer.apply_chat_template(prompt, tokenize=True, add_generation_prompt=True, return_tensors="pt").cuda()
65
+
66
+ model = AutoModelForCausalLM.from_pretrained(
67
+ model_id,
68
+ torch_dtype=torch.float16,
69
+ low_cpu_mem_usage=True,
70
+ device_map="auto",
71
+ )
72
+
73
+ outputs = model.generate(inputs, do_sample=True, max_new_tokens=256)
74
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
75
+ ```
76
+
77
+ ### AutoAWQ
78
+
79
+ In order to run the inference with Llama 3.1 8B Instruct AWQ in INT4, both `torch` and `autoawq` need to be installed as:
80
+
81
+ ```bash
82
+ pip install "torch>=2.2.0,<2.3.0" autoawq --upgrade
83
+ ```
84
+
85
+ Then, the latest version of `transformers` need to be installed, being 4.43.0 or higher, as:
86
+
87
+ ```bash
88
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
89
+ ```
90
+
91
+ Alternatively, one may want to run that via `AutoAWQ` even though it's built on top of 🤗 `transformers`, which is the recommended approach instead as described above.
92
+
93
+ ```python
94
+ import torch
95
+ from autoawq import AutoAWQForCausalLM
96
+ from transformers import AutoModelForCausalLM, AutoTokenizer
97
+
98
+ model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
99
+ prompt = [
100
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
101
+ {"role": "user", "content": "What's Deep Learning?"},
102
+ ]
103
+
104
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
105
+
106
+ inputs = tokenizer.apply_chat_template(prompt, tokenize=True, add_generation_prompt=True, return_tensors="pt").cuda()
107
+
108
+ model = AutoAWQForCausalLM.from_pretrained(
109
+ model_id,
110
+ torch_dtype=torch.float16,
111
+ low_cpu_mem_usage=True,
112
+ device_map="auto",
113
+ )
114
+
115
+ outputs = model.generate(inputs, do_sample=True, max_new_tokens=256)
116
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
117
+ ```
118
+
119
+ The AutoAWQ script has been adapted from [AutoAWQ/examples/generate.py](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/generate.py).
120
+
121
+ ### 🤗 Text Generation Inference (TGI)
122
+
123
+ Coming soon!
124
+
125
+ ## Quantization Reproduction
126
+
127
+ > [!NOTE]
128
+ > 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 24GiB of VRAM to quantize it.
129
+
130
+ In order to quantize Llama 3.1 8B Instruct, first install `torch` and `autoawq` as follows:
131
+
132
+ ```bash
133
+ pip install "torch>=2.2.0,<2.3.0" autoawq --upgrade
134
+ ```
135
+
136
+ Otherwise the quantization may fail, since the AutoAWQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
137
+
138
+ Then install the latest version of `transformers` as follows:
139
+
140
+ ```bash
141
+ pip install "transformers>=4.43.0" --upgrade
142
+ ```
143
+
144
+ And then, run the following script, adapted from [`AutoAWQ/examples/quantize.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/quantize.py) as follows:
145
+
146
+ ```python
147
+ from awq import AutoAWQForCausalLM
148
+ from transformers import AutoTokenizer
149
+
150
+ model_path = "meta-llama/Meta-Llama-3.1-8B-Instruct"
151
+ quant_path = "hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4"
152
+ quant_config = {
153
+ "zero_point": True,
154
+ "q_group_size": 128,
155
+ "w_bit": 4,
156
+ "version": "GEMM",
157
+ }
158
+
159
+ # Load model
160
+ model = AutoAWQForCausalLM.from_pretrained(
161
+ model_path, **{"low_cpu_mem_usage": True, "use_cache": False}
162
+ )
163
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
164
+
165
+ # Quantize
166
+ model.quantize(tokenizer, quant_config=quant_config)
167
+
168
+ # Save quantized model
169
+ model.save_quantized(quant_path)
170
+ tokenizer.save_pretrained(quant_path)
171
+
172
+ print(f'Model is quantized and saved at "{quant_path}"')
173
+ ```