ibnzterrell commited on
Commit
601bbc4
·
verified ·
1 Parent(s): 703824a

Update README.md with model documentation and reproduction details

Browse files
Files changed (1) hide show
  1. README.md +343 -3
README.md CHANGED
@@ -1,3 +1,343 @@
1
- ---
2
- license: llama3.1
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: llama3.1
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
+ datasets:
19
+ - nvidia/HelpSteer2
20
+ base_model:
21
+ - nvidia/Llama-3.1-Nemotron-70B-Instruct-HF
22
+ ---
23
+
24
+ ## Quantized Model Information
25
+
26
+ > [!IMPORTANT]
27
+ > This repository is an AWQ 4-bit quantized version of the [`nvidia/Llama-3.1-Nemotron-70B-Instruct-HF`](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Instruct-HF) model, which is an NVIDIA customized version of [`meta-llama/Meta-Llama-3.1-70B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-70B-Instruct), originally released by Meta AI.
28
+
29
+ This model was quantized using [AutoAWQ](https://github.com/casper-hansen/AutoAWQ) from FP16 down to INT4 using GEMM kernels, with zero-point quantization and a group size of 128.
30
+
31
+ Hardware: Intel Xeon CPU E5-2699A v4 @ 2.40GHz, 256GB of RAM, and 2x NVIDIA RTX 3090. I have only tested this with vLLM, but this should work on any platform that supports LLama 3.1 70B Instruct AWQ INT4. The primary limiting factor seems to be whether the platform supports Rotary Positional Embeddings (RoPE).
32
+
33
+ Model usage (inference) information for Transformers, AutoAWQ, Text Generation Interface (TGI), and vLLM , as well as quantization reproduction details, are below.
34
+
35
+ ## Original Model Information
36
+
37
+ Llama-3.1-Nemotron-70B-Instruct is a large language model customized by NVIDIA to improve the helpfulness of LLM generated responses to user queries.
38
+
39
+ This model reaches [Arena Hard](https://github.com/lmarena/arena-hard-auto) of 85.0, [AlpacaEval 2 LC](https://tatsu-lab.github.io/alpaca_eval/) of 57.6 and [GPT-4-Turbo MT-Bench](https://github.com/lm-sys/FastChat/pull/3158) of 8.98, which are known to be predictive of [LMSys Chatbot Arena Elo](https://huggingface.co/spaces/lmsys/chatbot-arena-leaderboard)
40
+
41
+ As of 1 Oct 2024, this model is #1 on all three automatic alignment benchmarks (verified tab for AlpacaEval 2 LC), edging out strong frontier models such as GPT-4o and Claude 3.5 Sonnet.
42
+
43
+ As of Oct 24th, 2024 the model has Elo Score of 1267(+-7), rank 9 and style controlled rank of 26 on [ChatBot Arena leaderboard](https://lmarena.ai/?leaderboard).
44
+
45
+ The original model was trained using RLHF (specifically, REINFORCE), [Llama-3.1-Nemotron-70B-Reward](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Reward) and [HelpSteer2-Preference prompts](https://huggingface.co/datasets/nvidia/HelpSteer2) on a Llama-3.1-70B-Instruct model as the initial policy.
46
+
47
+ [nvidia/Llama-3.1-Nemotron-70B-Instruct-HF](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Instr) has been converted from [Llama-3.1-Nemotron-70B-Instruct](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Instruct) to support it in the HuggingFace Transformers codebase. Please note that evaluation results might be slightly different from the [Llama-3.1-Nemotron-70B-Instruct](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Instruct) as evaluated in NeMo-Aligner, which the evaluation results are based on.
48
+
49
+ > [!NOTE]
50
+ > Note from Terrell: Quantization to AWQ 4-bit will further affect evaluation results.
51
+
52
+ ## Model Usage
53
+
54
+ In order to use this quantized model, support is offered for different solutions such as `transformers,` `autoawq,` or `text-generation-inference.`
55
+
56
+ > [!NOTE]
57
+ > 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.
58
+
59
+ ### 🤗 Transformers
60
+
61
+ In order to run the inference with Llama 3.1 Nemotron 70B Instruct AWQ in INT4, you need to install the following packages:
62
+
63
+ ```bash
64
+ pip install -q --upgrade transformers autoawq accelerate
65
+ ```
66
+
67
+ To run inference of Llama 3.1 Nemotron 70B Instruct AWQ in INT4 precision, the AWQ model can be instantiated as any other causal language modeling model via `AutoModelForCausalLM`. Run inference as usual.
68
+
69
+ ```python
70
+ import torch
71
+ from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
72
+
73
+ model_id = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4"
74
+ quantization_config = AwqConfig(
75
+ bits=4,
76
+ fuse_max_seq_len=512, # Note: Update this as per your use-case
77
+ do_fuse=True,
78
+ )
79
+
80
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
81
+ model = AutoModelForCausalLM.from_pretrained(
82
+ model_id,
83
+ torch_dtype=torch.float16,
84
+ low_cpu_mem_usage=True,
85
+ device_map="auto",
86
+ quantization_config=quantization_config
87
+ )
88
+
89
+ prompt = [
90
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
91
+ {"role": "user", "content": "What's Deep Learning?"},
92
+ ]
93
+ inputs = tokenizer.apply_chat_template(
94
+ prompt,
95
+ tokenize=True,
96
+ add_generation_prompt=True,
97
+ return_tensors="pt",
98
+ return_dict=True,
99
+ ).to("cuda")
100
+
101
+ outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
102
+ print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
103
+ ```
104
+
105
+ ### AutoAWQ
106
+
107
+ In order to run the inference with Llama 3.1 Nemotron 70B Instruct AWQ in INT4, you need to install the following packages:
108
+
109
+ ```bash
110
+ pip install -q --upgrade transformers autoawq accelerate
111
+ ```
112
+
113
+ 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.
114
+
115
+ ```python
116
+ import torch
117
+ from awq import AutoAWQForCausalLM
118
+ from transformers import AutoModelForCausalLM, AutoTokenizer
119
+
120
+ model_id = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4"
121
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
122
+ model = AutoAWQForCausalLM.from_pretrained(
123
+ model_id,
124
+ torch_dtype=torch.float16,
125
+ low_cpu_mem_usage=True,
126
+ device_map="auto",
127
+ )
128
+
129
+ prompt = [
130
+ {"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
131
+ {"role": "user", "content": "What's Deep Learning?"},
132
+ ]
133
+ inputs = tokenizer.apply_chat_template(
134
+ prompt,
135
+ tokenize=True,
136
+ add_generation_prompt=True,
137
+ return_tensors="pt",
138
+ return_dict=True,
139
+ ).to("cuda")
140
+
141
+ outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
142
+ print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
143
+ ```
144
+
145
+ The AutoAWQ script has been adapted from [AutoAWQ/examples/generate.py](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/generate.py).
146
+
147
+ ### 🤗 Text Generation Inference (TGI)
148
+
149
+ To run the `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](https://docs.docker.com/engine/install/)) and the `huggingface_hub` Python package as you need to login to the Hugging Face Hub.
150
+
151
+ ```bash
152
+ pip install -q --upgrade huggingface_hub
153
+ huggingface-cli login
154
+ ```
155
+
156
+ Then you just need to run the TGI v2.2.0 (or higher) Docker container as follows:
157
+
158
+ ```bash
159
+ docker run --gpus all --shm-size 1g -ti -p 8080:80 \
160
+ -v hf_cache:/data \
161
+ -e MODEL_ID=ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4 \
162
+ -e NUM_SHARD=4 \
163
+ -e QUANTIZE=awq \
164
+ -e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
165
+ -e MAX_INPUT_LENGTH=4000 \
166
+ -e MAX_TOTAL_TOKENS=4096 \
167
+ ghcr.io/huggingface/text-generation-inference:2.2.0
168
+ ```
169
+
170
+ > [!NOTE]
171
+ > TGI will expose different endpoints, to see all the endpoints available check [TGI OpenAPI Specification](https://huggingface.github.io/text-generation-inference/#/).
172
+
173
+ To send request to the deployed TGI endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
174
+
175
+ ```bash
176
+ curl 0.0.0.0:8080/v1/chat/completions \
177
+ -X POST \
178
+ -H 'Content-Type: application/json' \
179
+ -d '{
180
+ "model": "tgi",
181
+ "messages": [
182
+ {
183
+ "role": "system",
184
+ "content": "You are a helpful assistant."
185
+ },
186
+ {
187
+ "role": "user",
188
+ "content": "What is Deep Learning?"
189
+ }
190
+ ],
191
+ "max_tokens": 128
192
+ }'
193
+ ```
194
+
195
+ Or programatically via the `huggingface_hub` Python client as follows:
196
+
197
+ ```python
198
+ import os
199
+ from huggingface_hub import InferenceClient
200
+
201
+ client = InferenceClient(base_url="http://0.0.0.0:8080", api_key=os.getenv("HF_TOKEN", "-"))
202
+
203
+ chat_completion = client.chat.completions.create(
204
+ model="ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4",
205
+ messages=[
206
+ {"role": "system", "content": "You are a helpful assistant."},
207
+ {"role": "user", "content": "What is Deep Learning?"},
208
+ ],
209
+ max_tokens=128,
210
+ )
211
+ ```
212
+
213
+ Alternatively, the OpenAI Python client can also be used (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
214
+
215
+ ```python
216
+ import os
217
+ from openai import OpenAI
218
+
219
+ client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key=os.getenv("OPENAI_API_KEY", "-"))
220
+
221
+ chat_completion = client.chat.completions.create(
222
+ model="tgi",
223
+ messages=[
224
+ {"role": "system", "content": "You are a helpful assistant."},
225
+ {"role": "user", "content": "What is Deep Learning?"},
226
+ ],
227
+ max_tokens=128,
228
+ )
229
+ ```
230
+
231
+ ### vLLM
232
+
233
+ To run vLLM with Llama 3.1 70B Instruct AWQ in INT4, you will need to have Docker installed (see [installation notes](https://docs.docker.com/engine/install/)) and run the latest vLLM Docker container as follows:
234
+
235
+ ```bash
236
+ docker run --runtime nvidia --gpus all --ipc=host -p 8000:8000 \
237
+ -v hf_cache:/root/.cache/huggingface \
238
+ vllm/vllm-openai:latest \
239
+ --model ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4 \
240
+ --tensor-parallel-size 4 \
241
+ --max-model-len 4096
242
+ ```
243
+
244
+ To send request to the deployed vLLM endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
245
+
246
+ ```bash
247
+ curl 0.0.0.0:8000/v1/chat/completions \
248
+ -X POST \
249
+ -H 'Content-Type: application/json' \
250
+ -d '{
251
+ "model": "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4",
252
+ "messages": [
253
+ {
254
+ "role": "system",
255
+ "content": "You are a helpful assistant."
256
+ },
257
+ {
258
+ "role": "user",
259
+ "content": "What is Deep Learning?"
260
+ }
261
+ ],
262
+ "max_tokens": 128
263
+ }'
264
+ ```
265
+
266
+ Or programatically via the `openai` Python client (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
267
+
268
+ ```python
269
+ import os
270
+ from openai import OpenAI
271
+
272
+ client = OpenAI(base_url="http://0.0.0.0:8000/v1", api_key=os.getenv("VLLM_API_KEY", "-"))
273
+
274
+ chat_completion = client.chat.completions.create(
275
+ model="ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4",
276
+ messages=[
277
+ {"role": "system", "content": "You are a helpful assistant."},
278
+ {"role": "user", "content": "What is Deep Learning?"},
279
+ ],
280
+ max_tokens=128,
281
+ )
282
+ ```
283
+
284
+ ## Quantization Reproduction Information
285
+
286
+ > [!NOTE]
287
+ > 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.
288
+
289
+ In order to quantize Llama 3.1 Nemotron 70B Instruct, first install the following packages:
290
+
291
+ ```bash
292
+ pip install -q --upgrade transformers autoawq accelerate
293
+ ```
294
+
295
+ This quantization was produced using a single node with an Intel Xeon CPU E5-2699A v4 @ 2.40GHz, 256GB of RAM, and 2x NVIDIA RTX 3090 (24GB VRAM each, for a total of 48 GB VRAM).
296
+
297
+ I initially adapted [hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4](https://huggingface.co/hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4/blob/main/README.md), so many thanks to the Hugging Quants team, the AutoAWQ team, and the MIT HAN Lab for [LLM-AWQ](https://github.com/mit-han-lab/llm-awq). I'd also like to thank Professor David Dobolyi over at University of Colorado Boulder and Marc Sun at Hugging Face for their work, specifically [AutoAWQ PR#630](https://github.com/casper-hansen/AutoAWQ/pull/630).
298
+
299
+ Adapted from [`AutoAWQ/examples/quantize.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/quantize.py) and [hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4](https://huggingface.co/hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4/blob/main/README.md):
300
+
301
+ ```python
302
+ from awq import AutoAWQForCausalLM
303
+ from transformers import AutoTokenizer
304
+ import torch
305
+
306
+ # Empty Cache
307
+ torch.cuda.empty_cache()
308
+
309
+ # Memory Limits - Set this according to your hardware limits
310
+ max_memory = {0: "22GiB", 1: "22GiB", "cpu": "160GiB"}
311
+
312
+ model_path = "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
313
+ quant_path = "ibnzterrell/Nvidia-Llama-3.1-Nemotron-70B-Instruct-HF-AWQ-INT4"
314
+ quant_config = {
315
+ "zero_point": True,
316
+ "q_group_size": 128,
317
+ "w_bit": 4,
318
+ "version": "GEMM"
319
+
320
+ }
321
+
322
+ # 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)
323
+ model = AutoAWQForCausalLM.from_pretrained(
324
+ model_path,
325
+ use_cache=False,
326
+ max_memory=max_memory,
327
+ device_map="cpu"
328
+ )
329
+
330
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
331
+
332
+ # Quantize
333
+ model.quantize(
334
+ tokenizer,
335
+ quant_config=quant_config
336
+ )
337
+
338
+ # Save quantized model
339
+ model.save_quantized(quant_path)
340
+ tokenizer.save_pretrained(quant_path)
341
+
342
+ print(f'Model is quantized and saved at "{quant_path}"')
343
+ ```