alvarobartt HF staff commited on
Commit
a9b4ba1
1 Parent(s): e9f6887

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +189 -0
README.md ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-70B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-70B-Instruct) which is the FP16 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, 70B) 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-70B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-70B-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 70B Instruct AWQ 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.
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 70B 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
+ Otherwise, running the inference may fail, since the AutoAWQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
45
+
46
+ Then, the latest version of `transformers` need to be installed, being 4.43.0 or higher, as:
47
+
48
+ ```bash
49
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
50
+ ```
51
+
52
+ To run the inference on top of Llama 3.1 70B 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.
53
+
54
+ ```python
55
+ import torch
56
+ from transformers import AutoModelForCausalLM, AutoTokenizer
57
+
58
+ model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
59
+ prompt = [
60
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
61
+ {"role": "user", "content": "What's Deep Learning?"},
62
+ ]
63
+
64
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
65
+
66
+ inputs = tokenizer.apply_chat_template(
67
+ prompt,
68
+ tokenize=True,
69
+ add_generation_prompt=True,
70
+ return_tensors="pt",
71
+ return_dict=True,
72
+ ).to("cuda")
73
+
74
+ model = AutoModelForCausalLM.from_pretrained(
75
+ model_id,
76
+ torch_dtype=torch.float16,
77
+ low_cpu_mem_usage=True,
78
+ device_map="auto",
79
+ )
80
+
81
+ outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
82
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
83
+ ```
84
+
85
+ ### AutoAWQ
86
+
87
+ In order to run the inference with Llama 3.1 70B Instruct AWQ in INT4, both `torch` and `autoawq` need to be installed as:
88
+
89
+ ```bash
90
+ pip install "torch>=2.2.0,<2.3.0" autoawq --upgrade
91
+ ```
92
+
93
+ Otherwise, running the inference may fail, since the AutoAWQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
94
+
95
+ Then, the latest version of `transformers` need to be installed, being 4.43.0 or higher, as:
96
+
97
+ ```bash
98
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
99
+ ```
100
+
101
+ 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.
102
+
103
+ ```python
104
+ import torch
105
+ from autoawq import AutoAWQForCausalLM
106
+ from transformers import AutoModelForCausalLM, AutoTokenizer
107
+
108
+ model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
109
+ prompt = [
110
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
111
+ {"role": "user", "content": "What's Deep Learning?"},
112
+ ]
113
+
114
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
115
+
116
+ inputs = tokenizer.apply_chat_template(
117
+ prompt,
118
+ tokenize=True,
119
+ add_generation_prompt=True,
120
+ return_tensors="pt",
121
+ return_dict=True,
122
+ ).to("cuda")
123
+
124
+ model = AutoAWQForCausalLM.from_pretrained(
125
+ model_id,
126
+ torch_dtype=torch.float16,
127
+ low_cpu_mem_usage=True,
128
+ device_map="auto",
129
+ )
130
+
131
+ outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
132
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
133
+ ```
134
+
135
+ The AutoAWQ script has been adapted from [AutoAWQ/examples/generate.py](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/generate.py).
136
+
137
+ ### 🤗 Text Generation Inference (TGI)
138
+
139
+ Coming soon!
140
+
141
+ ## Quantization Reproduction
142
+
143
+ > [!NOTE]
144
+ > In order to quantize Llama 3.1 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.
145
+
146
+ In order to quantize Llama 3.1 70B Instruct, first install `torch` and `autoawq` as follows:
147
+
148
+ ```bash
149
+ pip install "torch>=2.2.0,<2.3.0" autoawq --upgrade
150
+ ```
151
+
152
+ 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.
153
+
154
+ Then install the latest version of `transformers` as follows:
155
+
156
+ ```bash
157
+ pip install "transformers>=4.43.0" --upgrade
158
+ ```
159
+
160
+ 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:
161
+
162
+ ```python
163
+ from awq import AutoAWQForCausalLM
164
+ from transformers import AutoTokenizer
165
+
166
+ model_path = "meta-llama/Meta-Llama-3.1-70B-Instruct"
167
+ quant_path = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
168
+ quant_config = {
169
+ "zero_point": True,
170
+ "q_group_size": 128,
171
+ "w_bit": 4,
172
+ "version": "GEMM",
173
+ }
174
+
175
+ # Load model
176
+ model = AutoAWQForCausalLM.from_pretrained(
177
+ model_path, **{"low_cpu_mem_usage": True, "use_cache": False}
178
+ )
179
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
180
+
181
+ # Quantize
182
+ model.quantize(tokenizer, quant_config=quant_config)
183
+
184
+ # Save quantized model
185
+ model.save_quantized(quant_path)
186
+ tokenizer.save_pretrained(quant_path)
187
+
188
+ print(f'Model is quantized and saved at "{quant_path}"')
189
+ ```