Skip to content

Commit d7c2204

Browse files
authored
new README (#48)
* new README' * update
1 parent 0babda5 commit d7c2204

File tree

1 file changed

+46
-73
lines changed

1 file changed

+46
-73
lines changed

README.md

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
# torchao
1+
# torchao: PyTorch Architecture Optimization
22

3-
**Note: This repository is currently under heavy development - if you have suggestions on the API or use-cases you'd like to be covered, please open an github issue or reach out. We'd love to hear about how you're using the APIs.**
3+
**Note: This repository is currently under heavy development - if you have suggestions on the API or use-cases you'd like to be covered, please open an github issue**
4+
5+
The `torchao` package allows you to quantize and prune your models using native PyTorch.
6+
7+
The repo hosts both
8+
1. lower precision [dtypes](./torchao/dtypes) such as nf4, uint4
9+
2. Quantization [algorithms](./torchao/quantization) such as dynamic quant, smoothquant
10+
3. Sparsity [algorithms](./torchao/sparsity) such as Wanda
11+
12+
## Success stories
13+
Our kernels have has been used to achieve SOTA inference performance on
14+
15+
1. Image segmentation modelss with [sam-fast](pytorch.org/blog/accelerating-generative-ai)
16+
2. Language models with [gpt-fast](pytorch.org/blog/accelerating-generative-ai-2)
17+
3. Diffusion models with [sd-fast](pytorch.org/blog/accelerating-generative-ai-3)
418

5-
The torchao package contains apis and workflows used to apply AO techniques like quantization and pruning to models using only native pytorch.
619

720
## Installation
821

@@ -18,43 +31,23 @@ pip install torchao
1831
```Shell
1932
git clone https://github.com/pytorch-labs/ao
2033
cd ao
21-
python setup.py install
22-
```
23-
24-
Verify Installation:
25-
26-
```Shell
27-
pip list | grep torchao
28-
```
29-
30-
Expected Output
31-
```Shell
32-
torchao 0.0.1 <install dir>
34+
pip install -e .
3335
```
3436

35-
## Usage
37+
## Examples
3638

37-
Relevant APIs can be found in torchao.quantization.quant_api
38-
39-
Note: While these techniques are designed to improve model performance, in some cases the opposite can occur.
40-
This is because quantization adds additional overhead to the model that is hopefully made up for by faster matmuls (dynamic quantization) or loading weights faster (weight-only quantization). If your matmuls are small enough or your non-quantized perf isn't bottlenecked by weight load time, these techniques may reduce performance.
41-
42-
The following apis use quantized [tensor subclasses](https://pytorch.org/docs/stable/notes/extending.html#subclassing-torch-tensor). By taking a linear op/module and replacing the original weight with a q-tensor subclass, we're able to convert it into a quantized version of the op. Upon replacement, these q-tensor subclasses quantize the original weight and override the dispatch for linear ops to instead use the subclass' _quantized_op method.
43-
44-
This tensor subclass method of quantization is preferred over older module swap based methods because it doesn't modify the graph and is generally more composable and flexible.
39+
Typically quantization algorithms will have different schemes for how the activation and weights are quantized so A16W8 for instance means the activations are quantized to 16 bits wheras the weights are quantized to 8 bits. Trying out different quantization schemes in `torchao` is generally a 1 line change.
4540

4641
### A8W8 Dynamic Quantization
4742

48-
The `change_linear_weights_to_int8_dqtensors` function converts the linear weights in a model to a quantized tensor subclass `Int8DynamicallyQuantizedLinearWeight`. In practice this
49-
converts the floating point linear matmul of the original linear op to a dynamically quantized linear matmul.
50-
51-
Example
52-
5343
```Python
5444
import torch
5545
from torchao.quantization import quant_api
5646

57-
# some user model and example input
47+
# Fuse the int8*int8 -> int32 matmul and subsequent mul op avoiding materialization of the int32 intermediary tensor
48+
torch._inductor.config.force_fuse_int_mm_with_mul = True
49+
50+
# Plug in your model and example input
5851
model = torch.nn.Sequential(torch.nn.Linear(32, 64)).cuda().to(torch.bfloat16)
5952
input = torch.randn(32,32, dtype=torch.bfloat16, device='cuda')
6053

@@ -66,78 +59,54 @@ model = torch.compile(model, mode='max-autotune')
6659
model(input)
6760
```
6861

69-
This technique works best when the torch._inductor.config.force_fuse_int_mm_with_mul option is enabled. This allows fusion of the int8*int8 -> int32 matmul and subsequent mul op, thereby avoiding materialization of the int32 intermediary tensor.
70-
71-
7262
### A16W8 WeightOnly Quantization
7363

74-
The `change_linear_weights_to_int8_woqtensors` function converts the linear weights in a model to a quantized tensor subclass `Int8WeightOnlyQuantizedLinearWeight`. In practice this
75-
converts the floating point linear matmul of the original linear op to a weight only quantized linear matmul
76-
77-
Example
78-
79-
```Python
80-
# some user model and example input
81-
...
82-
83-
# convert linear modules to quantized linear modules
64+
```python
8465
quant_api.change_linear_weights_to_int8_woqtensors(model)
85-
86-
# compile the model to improve performance
87-
...
8866
```
8967

9068
This technique works best when the torch._inductor.config.use_mixed_mm option is enabled. This avoids dequantizing the weight tensor before the matmul, instead fusing the dequantization into the matmul, thereby avoiding materialization of a large floating point weight tensor.
9169

9270

9371
### A16W4 WeightOnly Quantization
9472

95-
The `change_linear_weights_to_int4_woqtensors` function converts the linear weights in a model to a quantized tensor subclass `Int4WeightOnlyQuantizedLinearWeight`. In practice this
96-
converts the floating point linear matmul of the original linear op to a weight only quantized linear matmul
97-
98-
Example
99-
100-
```Python
101-
# some user model and example input
102-
...
103-
104-
# convert linear modules to quantized linear modules
73+
```python
10574
quant_api.change_linear_weights_to_int4_woqtensors(model)
106-
107-
# compile the model to improve performance
108-
...
10975
```
11076

111-
The quantization error incurred by applying int4 quantization to your model can be fairly significant, so using external techniques like GPTQ may be necessary to obtain a usable model.
112-
113-
## Other APIs
77+
Note: The quantization error incurred by applying int4 quantization to your model can be fairly significant, so using external techniques like GPTQ may be necessary to obtain a usable model.
11478

115-
### Module Swap APIs
116-
117-
The `apply_dynamic_quant` and `apply_weight_only_int8_quant` apis can be used in the same formula as above to achieve dynamic and weight-only quantization using module swaps instead of quantized tensor subclasses.
11879

11980
### A8W8 Dynamic Quantization with Smoothquant
12081

121-
We've also implemented a version of [smoothquant](https://arxiv.org/abs/2211.10438) with the same GEMM format as above.
122-
Due to requiring calibration, the API is slightly more complicated and currently only exists with a module swap api.
82+
We've also implemented a version of [smoothquant](https://arxiv.org/abs/2211.10438) with the same GEMM format as above. Due to requiring calibration, the API is more complicated.
12383

12484
Example
12585

12686
```Python
12787
import torch
12888
from torchao.quantization.smoothquant import swap_linear_with_smooth_fq_linear, smooth_fq_linear_to_inference
12989

130-
# some user model
90+
# Fuse the int8*int8 -> int32 matmul and subsequent mul op avoiding materialization of the int32 intermediary tensor
91+
torch._inductor.config.force_fuse_int_mm_with_mul = True
92+
93+
# plug in your model
13194
model = get_model()
13295

13396
# convert linear modules to smoothquant
13497
# linear module in calibration mode
13598
swap_linear_with_smooth_fq_linear(model)
13699

137-
# calibration
138-
for i in range(calibration_amount):
139-
input = get_input()
140-
model(input)
100+
# Create a data loader for calibration
101+
calibration_data = get_calibration_data()
102+
calibration_dataset = MyDataset(calibration_data)
103+
calibration_loader = DataLoader(calibration_dataset, batch_size=32, shuffle=True)
104+
105+
# Calibrate the model
106+
model.train()
107+
for batch in calibration_loader:
108+
inputs = batch
109+
model(inputs)
141110

142111
# set it to inference mode
143112
smooth_fq_linear_to_inference(model)
@@ -147,7 +116,11 @@ model = torch.compile(model, mode='max-autotune')
147116
model(input)
148117
```
149118

150-
like the other dynamic quantization apis, the torch._inductor.config.force_fuse_int_mm_with_mul option may significantly improve performance if enabled.
119+
## Sharp edges
120+
121+
1. While these techniques are designed to improve model performance, in some cases the opposite can occur. This is because quantization adds additional overhead to the model that is hopefully made up for by faster matmuls (dynamic quantization) or loading weights faster (weight-only quantization). If your matmuls are small enough or your non-quantized perf isn't bottlenecked by weight load time, these techniques may reduce performance.
122+
2. Use the PyTorch nightlies so you can leverage [tensor subclasses](https://pytorch.org/docs/stable/notes/extending.html#subclassing-torch-tensor) which is preferred over older module swap based methods because it doesn't modify the graph and is generally more composable and flexible.
123+
151124

152125
## License
153126

0 commit comments

Comments
 (0)