Skip to content

Commit 224b258

Browse files
committed
quantization
1 parent 08ad55b commit 224b258

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ Designed for interactive graphical conversations using the familiar web browser
103103

104104
Quantization is the process of converting a model into a more memory-efficient representation. Quantization is particularly important for accelerators -- to take advantage of the available memory bandwidth, and fit in the often limited high-speed memory in accelerators – and mobile devices – to fit in the typically very limited memory of mobile devices.
105105

106-
Depending on the model and the target device, different quantization recipes may be applied. Torchchat contains two example configurations to optimize performance for GPU-based systems `config/data/cuda.json` , and mobile systems `config/data/mobile.json`. The GPU configuration is targeted towards optimizing for memory bandwidth which is a scarce resource in powerful GPUs (and to a less degree, memory footprint to fit large models into a device's memory). The mobile configuration is targeted towards optimizing for memory fotoprint because in many devices, a single application is limited to as little as GB or less of memory.
106+
Depending on the model and the target device, different quantization recipes may be applied. Torchchat contains two example configurations to optimize performance for GPU-based systems `config/data/server.json` , and mobile systems `config/data/mobile.json`. The GPU configuration is targeted towards optimizing for memory bandwidth which is a scarce resource in powerful GPUs (and to a less degree, memory footprint to fit large models into a device's memory). The mobile configuration is targeted towards optimizing for memory fotoprint because in many devices, a single application is limited to as little as GB or less of memory.
107107

108-
You can use the quantization recipes in conjunction with any of the `chat`, `generate` and `browser` commands to test their impact and accelerate model execution. You will apply these recipes to the export comamnds below, to optimize the exported models. To adapt these recipes or wrote your own, please refer to the [quantization overview](docs/quantization.md).
108+
You can use the quantization recipes in conjunction with any of the `chat`, `generate` and `browser` commands to test their impact and accelerate model execution. You will apply these recipes to the export commands below, to optimize the exported models. To adapt these recipes or wrote your own, please refer to the [quantization overview](docs/quantization.md).
109109

110110
---
111111
*TO BE REPLACED BY SUITABLE ORDING PROVIDED BY LEGAL*
@@ -215,31 +215,32 @@ python3 torchchat.py chat codellama
215215
AOT compiles models into machine code before execution, enhancing performance and predictability. It's particularly beneficial for frequently used models or those requiring quick start times. However, it may lead to larger binary sizes and lacks the runtime flexibility of eager mode.
216216

217217
**Examples**
218-
The following example uses the Stories15M model.
218+
The following example uses the Llama3 8B model.
219219
```
220220
# Compile
221-
python3 torchchat.py export stories15M --output-dso-path stories15M.so
221+
python3 torchchat.py export llama3 --output-dso-path llama3.so
222222
223223
# Execute
224-
python3 torchchat.py generate --dso-path stories15M.so --prompt "Hello my name is"
224+
python3 torchchat.py generate llama3 --quantize config/data/server.json--dso-path llama3.so --prompt "Hello my name is"
225225
```
226226

227-
NOTE: The exported model will be large. We suggest you quantize the model, explained further down, before deploying the model on device.
227+
NOTE: We use `--quantize config/data/server.json` to quantize the llama3 model to reduce model size and improve performance for on-device use cases.
228228

229229
### ExecuTorch
230230

231231
ExecuTorch enables you to optimize your model for execution on a mobile or embedded device, but can also be used on desktop for testing.
232232
Before running ExecuTorch commands, you must first set-up ExecuTorch in torchchat, see [Set-up Executorch](docs/executorch_setup.md).
233233

234234
**Examples**
235-
The following example uses the Stories15M model.
235+
The following example uses the Llama3 8B model.
236236
```
237237
# Compile
238-
python3 torchchat.py export stories15M --output-pte-path stories15M.pte
238+
python3 torchchat.py export llama3 --quantize config/data/mobile.json --output-pte-path llama3.pte
239239
240240
# Execute
241-
python3 torchchat.py generate --device cpu --pte-path stories15M.pte --prompt "Hello my name is"
241+
python3 torchchat.py generate llama3 --device cpu --pte-path llama3.pte --prompt "Hello my name is"
242242
```
243+
NOTE: We use `--quantize config/data/mobile.json` to quantize the llama3 model to reduce model size and improve performance for on-device use cases.
243244

244245
See below under [Mobile Execution](#run-mobile) if you want to deploy and execute a model in your iOS or Android app.
245246

File renamed without changes.

docs/quantization.md

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,58 @@
44
## Introduction
55
Quantization focuses on reducing the precision of model parameters and computations from floating-point to lower-bit integers, such as 8-bit integers. This approach aims to minimize memory requirements, accelerate inference speeds, and decrease power consumption, making models more feasible for deployment on edge devices with limited computational resources. While quantization can potentially degrade the model's performance, the methods supported by torchchat are designed to mitigate this effect, maintaining a balance between efficiency and accuracy.
66

7-
## Supported quantization techniques
7+
## Weight Quantization
8+
| compression | FP Precision | weight quantization (bitwidth)| weight quantization (group size) | dynamic activation quantization | Eager | AOTI | ExecuTorch |
9+
|--|--|--|--|--|--|--|--|
10+
| linear (asymmetric) | fp32, fp16, bf16 | [8, 4]* | [32, 64, 128, 256]** | ||||
11+
| linear (symmetric) | | | | a8w4dq ||||
12+
| linear (asymmetric) with GPTQ | | | | ||||
13+
| linear (asymmetric) with HQQ | | | | ||||
14+
15+
## Embedding Quantization
16+
Due to the larger vocabulary size of llama3, we also recommend quantizing the embeddings to further reduce the model size for on-device usecases.
17+
18+
| compression | FP Precision | weight quantization (bitwidth)| weight quantization (group size) | dynamic activation quantization | Eager | AOTI | ExecuTorch |
19+
|--|--|--|--|--|--|--|--|
20+
| embedding table (symmetric) | fp32, fp16, bf16 | [8, 4]* | [32, 64, 128, 256]** | ||||
21+
22+
*These are the only valid bitwidth options.
23+
24+
**There are many valid group size options, including 512, 1024, etc.
25+
26+
## Examples
27+
We can mix and match weight quantization with embedding quantization. Quantization options are passed in json format either as a config file (see [server.json](../config/data/server.json) and [mobile.json](../config/data/mobile.json)) or a json string.
28+
29+
* Config file
30+
```
31+
--quantize quant_config.json
32+
```
33+
* Single quantization scheme
34+
```
35+
--quantize '{"linear:a8w4dq": {"groupsize" : 256}}'
36+
```
37+
* Multiple quantization schemes
38+
```
39+
--quantize '{"embedding": {"bitwidth": 4, "groupsize":32}, "linear:a8w4dq": {"groupsize" : 256}}'
40+
```
41+
Quantization recipes can be applied in conjunction with any of the `chat`, `generate`, `browser` and `export` commands. Below are examples showcasing eager mode with `generate` and AOTI and ExecuTorch with `export`.
42+
### Eager mode
43+
```
44+
python3 generate.py [--compile] --checkpoint-path ${MODEL_PATH} --prompt "Hello, my name is" --quantize '{"embedding" : {"bitwidth": 8, "groupsize": 0}}' --device cpu
45+
```
46+
### AOTI
47+
```
48+
python3 torchchat.py export llama3 --quantize '{"embedding": {"bitwidth": 4, "groupsize":32}, "linear:a8w4dq": {"groupsize" : 256}}' --output-dso-path llama3.dso
49+
50+
python3 generate.py --dso-path llama3.dso --prompt "Hello my name is"
51+
```
52+
### ExecuTorch
53+
```
54+
python3 torchchat.py export llama3 --quantize '{"embedding": {"bitwidth": 4, "groupsize":32}, "linear:a8w4dq": {"groupsize" : 256}}' --output-pte-path llama3.pte
55+
56+
python3 generate.py --pte-path llama3.pte --prompt "Hello my name is"
57+
```
858

9-
| compression | FP precision | weight quantization | dynamic activation quantization |
10-
|--|--|--|--|
11-
embedding table (symmetric) | fp32, fp16, bf16 | 8b (group/channel), 4b (group/channel) | n/a |
12-
linear operator (symmetric) | fp32, fp16, bf16 | 8b (group/channel) | n/a |
13-
linear operator (asymmetric) | n/a | 4b (group), a6w4dq | a8w4dq (group) |
14-
linear operator (asymmetric) with GPTQ | n/a | 4b (group) | n/a |
15-
linear operator (asymmetric) with HQQ | n/a | work in progress | n/a |
1659

1760
## Model precision (dtype precision setting)
1861
You can generate models (for both export and generate, with eager, torch.compile, AOTI, ET, for all backends - mobile at present will primarily support fp32, with all options) specify the precision of the model with
@@ -24,7 +67,7 @@ python3 export.py --dtype [bf16 | fp16 | fp32] ...
2467
```
2568

2669
Unlike gpt-fast which uses bfloat16 as default, torchchat uses float32 as the default. As a consequence you will have to set to --dtype bf16 or --dtype fp16 on server / desktop for best performance.
27-
Support for FP16 and BF16 is limited in many embedded processors. Additional executorch support for 16-bit floating point types may be added in the future based on hardware support.
70+
Support for FP16 and BF16 is limited in many embedded processors. Additional ExecuTorch support for 16-bit floating point types may be added in the future based on hardware support.
2871

2972
## Making your models fit and execute fast!
3073

@@ -214,12 +257,12 @@ python3 generate.py [--compile] --checkpoint-path ${MODEL_PATH} --prompt "Hello,
214257
```
215258

216259
```
217-
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:int4": {"groupsize" : 32} }' [ --output-pte-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.pte | --output-dso-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.dso]
260+
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:int4": {"groupsize" : 32} }' --output-dso-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.dso
218261
```
219262
Now you can run your model with the same command as before:
220263

221264
```
222-
python3 generate.py [ --pte-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.pte | --dso-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.dso] --prompt "Hello my name is"
265+
python3 generate.py --dso-path ${MODEL_OUT}/${MODEL_NAME}_int4-gw32.dso --prompt "Hello my name is"
223266
```
224267

225268
## 4-Bit Integer Linear Quantization (a8w4dq)
@@ -247,12 +290,12 @@ python3 generate.py [--compile] --checkpoint-path ${MODEL_PATH} --prompt "Hello,
247290
```
248291

249292
```
250-
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:gptq": {"groupsize" : 32} }' [ --output-pte-path ${MODEL_OUT}/${MODEL_NAME}_gptq.pte | ...dso... ]
293+
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:gptq": {"groupsize" : 32} }' --output-dso-path ${MODEL_OUT}/${MODEL_NAME}_gptq.dso
251294
```
252295
Now you can run your model with the same command as before:
253296

254297
```
255-
python3 generate.py [ --pte-path ${MODEL_OUT}/${MODEL_NAME}_gptq.pte | ...dso...] --prompt "Hello my name is"
298+
python3 generate.py --dso-path ${MODEL_OUT}/${MODEL_NAME}_gptq.dso --prompt "Hello my name is"
256299
```
257300

258301
## 4-bit Integer Linear Quantization with HQQ (hqq)
@@ -267,12 +310,12 @@ python3 generate.py [--compile] --checkpoint-path ${MODEL_PATH} --prompt "Hello,
267310
```
268311

269312
```
270-
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:hqq": {"groupsize" : 32} }' [ --output-pte-path ${MODEL_OUT}/${MODEL_NAME}_hqq.pte | ...dso... ]
313+
python3 export.py --checkpoint-path ${MODEL_PATH} -d fp32 --quantize '{"linear:hqq": {"groupsize" : 32} }' --output-dso-path ${MODEL_OUT}/${MODEL_NAME}_hqq.dso
271314
```
272315
Now you can run your model with the same command as before:
273316

274317
```
275-
python3 generate.py [ --pte-path ${MODEL_OUT}/${MODEL_NAME}_hqq.pte | ...dso...] --prompt "Hello my name is"
318+
python3 generate.py --dso-path ${MODEL_OUT}/${MODEL_NAME}_hqq.dso --prompt "Hello my name is"
276319
277320
278321
## Adding additional quantization schemes

download.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def download_and_convert(
9494
# overwriting if necessary.
9595
if os.path.isdir(model_dir):
9696
shutil.rmtree(model_dir)
97-
os.makedirs(model_dir, exist_ok=True)
9897
shutil.move(temp_dir, model_dir)
9998

10099
finally:

0 commit comments

Comments
 (0)