alvarobartt HF staff commited on
Commit
3cb7941
1 Parent(s): 7eb6642

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +225 -0
README.md ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ - autogptq
18
+ ---
19
+
20
+ > [!IMPORTANT]
21
+ > This repository is a community-driven quantized version of the original model [`meta-llama/Meta-Llama-3.1-405B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-405B-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, 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-405B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-405B-Instruct) quantized using [AutoGPTQ](https://github.com/AutoGPTQ/AutoGPTQ) from FP16 down to INT4 using the GPTQ 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 405B Instruct GPTQ in INT4, around 203 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`, `autogptq`, or `text-generation-inference`.
35
+
36
+ ### 🤗 transformers
37
+
38
+ In order to run the inference with Llama 3.1 405B Instruct GPTQ in INT4, both `torch` and `autogptq` need to be installed as:
39
+
40
+ ```bash
41
+ pip install "torch>=2.2.0,<2.3.0" --upgrade
42
+ pip install auto-gptq --no-build-isolation
43
+ ```
44
+
45
+ Otherwise, running the model may fail, since the AutoGPTQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
46
+
47
+ Then, the latest version of `transformers` need to be installed including the `accelerate` extra, being 4.43.0 or higher, as:
48
+
49
+ ```bash
50
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
51
+ ```
52
+
53
+ Finally, in order to use `autogptq`, `optimum` also needs to be installed:
54
+
55
+ ```bash
56
+ pip install optimum --upgrade
57
+ ```
58
+
59
+ To run the inference on top of Llama 3.1 405B Instruct GPTQ in INT4 precision, the GPTQ model can be instantiated as any other causal language modeling model via `AutoModelForCausalLM` and run the inference normally.
60
+
61
+ ```python
62
+ import torch
63
+ from transformers import AutoModelForCausalLM, AutoTokenizer
64
+
65
+ model_id = "hugging-quants/Meta-Llama-3.1-405B-Instruct-GPTQ-INT4"
66
+ prompt = [
67
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
68
+ {"role": "user", "content": "What's Deep Learning?"},
69
+ ]
70
+
71
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
72
+
73
+ inputs = tokenizer.apply_chat_template(
74
+ prompt,
75
+ tokenize=True,
76
+ add_generation_prompt=True,
77
+ return_tensors="pt",
78
+ return_dict=True,
79
+ ).to("cuda")
80
+
81
+ model = AutoModelForCausalLM.from_pretrained(
82
+ model_id,
83
+ torch_dtype=torch.float16,
84
+ low_cpu_mem_usage=True,
85
+ device_map="auto",
86
+ )
87
+
88
+ outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
89
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
90
+ ```
91
+
92
+ ### AutoGPTQ
93
+
94
+ Alternatively, one may want to run that via `AutoGPTQ` even though it's built on top of 🤗 `transformers`, which is the recommended approach instead as described above.
95
+
96
+ In order to run the inference with Llama 3.1 405B Instruct GPTQ in INT4, both `torch` and `autogptq` need to be installed as:
97
+
98
+ ```bash
99
+ pip install "torch>=2.2.0,<2.3.0" --upgrade
100
+ pip install auto-gptq --no-build-isolation
101
+ ```
102
+
103
+ Otherwise, running the model may fail, since the AutoGPTQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
104
+
105
+ Then, the latest version of `transformers` need to be installed including the `accelerate` extra, being 4.43.0 or higher, as:
106
+
107
+ ```bash
108
+ pip install "transformers[accelerate]>=4.43.0" --upgrade
109
+ ```
110
+
111
+ Finally, in order to use `autogptq`, `optimum` also needs to be installed:
112
+
113
+ ```bash
114
+ pip install optimum --upgrade
115
+ ```
116
+
117
+ And then run it as follows:
118
+
119
+ ```python
120
+ import torch
121
+ from auto_gptq import AutoGPTQForCausalLM
122
+ from transformers import AutoModelForCausalLM, AutoTokenizer
123
+
124
+ model_id = "hugging-quants/Meta-Llama-3.1-405B-Instruct-GPTQ-INT4"
125
+ prompt = [
126
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
127
+ {"role": "user", "content": "What's Deep Learning?"},
128
+ ]
129
+
130
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
131
+
132
+ inputs = tokenizer.apply_chat_template(prompt, tokenize=True, add_generation_prompt=True, return_tensors="pt").cuda()
133
+
134
+ model = AutoGPTQForCausalLM.from_pretrained(
135
+ model_id,
136
+ torch_dtype=torch.float16,
137
+ low_cpu_mem_usage=True,
138
+ device_map="auto",
139
+ )
140
+
141
+ outputs = model.generate(inputs, do_sample=True, max_new_tokens=256)
142
+ print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
143
+ ```
144
+
145
+ The AutoGPTQ script has been adapted from [AutoGPTQ/examples/quantization/basic_usage.py](https://github.com/AutoGPTQ/AutoGPTQ/blob/main/examples/quantization/basic_usage.py).
146
+
147
+ ### 🤗 Text Generation Inference (TGI)
148
+
149
+ Coming soon!
150
+
151
+ ## Quantization Reproduction
152
+
153
+ > [!NOTE]
154
+ > In order to quantize Llama 3.1 405B Instruct using AutoGPTQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~800GiB, and an NVIDIA GPU with 80GiB of VRAM to quantize it.
155
+
156
+ In order to quantize Llama 3.1 405B Instruct, first install `torch` and `autoqptq` as follows:
157
+
158
+ ```bash
159
+ pip install "torch>=2.2.0,<2.3.0" --upgrade
160
+ pip install auto-gptq --no-build-isolation
161
+ ```
162
+
163
+ Otherwise the quantization may fail, since the AutoGPTQ kernels are built with PyTorch 2.2.1, meaning that those will break with PyTorch 2.3.0.
164
+
165
+ Then install the latest version of `transformers` as follows:
166
+
167
+ ```bash
168
+ pip install "transformers>=4.43.0" --upgrade
169
+ ```
170
+
171
+ And then, run the following script, adapted from [AutoGPTQ/examples/quantization/basic_usage.py](https://github.com/AutoGPTQ/AutoGPTQ/blob/main/examples/quantization/basic_usage.py).
172
+
173
+ ```python
174
+ import random
175
+
176
+ import numpy as np
177
+ import torch
178
+
179
+ from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
180
+ from datasets import load_dataset
181
+ from transformers import AutoTokenizer
182
+
183
+ pretrained_model_dir = "meta-llama/Meta-Llama-3.1-405B-Instruct"
184
+ quantized_model_dir = "meta-llama/Meta-Llama-3.1-405B-Instruct-GPTQ-INT4"
185
+
186
+ print("Loading tokenizer, dataset, and tokenizing the dataset...")
187
+ tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True)
188
+ dataset = load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="train")
189
+ encodings = tokenizer("\n\n".join(dataset["text"]), return_tensors="pt")
190
+
191
+ print("Setting random seeds...")
192
+ random.seed(0)
193
+ np.random.seed(0)
194
+ torch.random.manual_seed(0)
195
+
196
+ print("Setting calibration samples...")
197
+ nsamples = 128
198
+ seqlen = 2048
199
+ calibration_samples = []
200
+ for _ in range(nsamples):
201
+ i = random.randint(0, encodings.input_ids.shape[1] - seqlen - 1)
202
+ j = i + seqlen
203
+ input_ids = encodings.input_ids[:, i:j]
204
+ attention_mask = torch.ones_like(input_ids)
205
+ calibration_samples.append({"input_ids": input_ids, "attention_mask": attention_mask})
206
+
207
+ quantize_config = BaseQuantizeConfig(
208
+ bits=4, # quantize model to 4-bit
209
+ group_size=128, # it is recommended to set the value to 128
210
+ desc_act=True, # set to False can significantly speed up inference but the perplexity may slightly bad
211
+ sym=True, # using symmetric quantization so that the range is symmetric allowing the value 0 to be precisely represented (can provide speedups)
212
+ damp_percent=0.1, # see https://github.com/AutoGPTQ/AutoGPTQ/issues/196
213
+ )
214
+
215
+ # load un-quantized model, by default, the model will always be loaded into CPU memory
216
+ print("Load unquantized model...")
217
+ model = AutoGPTQForCausalLM.from_pretrained(pretrained_model_dir, quantize_config)
218
+
219
+ # quantize model, the examples should be list of dict whose keys can only be "input_ids" and "attention_mask"
220
+ print("Quantize model with calibration samples...")
221
+ model.quantize(calibration_samples)
222
+
223
+ # save quantized model using safetensors
224
+ model.save_quantized(quantized_model_dir, use_safetensors=True)
225
+ ```