Skip to content

Commit b401b02

Browse files
authored
Add PT2E cv&llm example (#1853)
Signed-off-by: Kaihui-intel <kaihui.tang@intel.com>
1 parent e470f6c commit b401b02

File tree

11 files changed

+926
-54
lines changed

11 files changed

+926
-54
lines changed

docs/3x/PT_StaticQuant.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
PyTorch Static Quantization
22
========================================
3-
43
1. [Introduction](#introduction)
54
2. [Get Started](#get-started) \
65
2.1 [Static Quantization with IPEX Backend](#static-quantization-with-ipex-backend) \
@@ -9,6 +8,7 @@ PyTorch Static Quantization
98
2.1.3 [Model Examples](#model-examples) \
109
2.2 [Static Quantization with PT2E Backend](#static-quantization-with-pt2e-backend) \
1110
2.2.1 [Usage Sample with PT2E](#usage-sample-with-pt2e)
11+
2.2.2 [Model Examples with PT2E](#model-examples-with-pt2e)
1212

1313

1414
## Introduction
@@ -102,3 +102,7 @@ opt_model = torch.compile(q_model)
102102
```
103103

104104
> Note: The `set_local` of `StaticQuantConfig` will be supported after the torch 2.4 release.
105+
106+
#### Model Examples with PT2E
107+
108+
Users could refer to [cv examples](https://github.com/intel/neural-compressor/blob/master/examples/3.x_api/pytorch/cv/static_quant) and [llm examples](https://github.com/intel/neural-compressor/blob/master/examples/3.x_api/pytorch/nlp/huggingface_models/language-modeling/quantization/static_quant/pt2e) on how to quantize a new model.
+67-53
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,67 @@
1-
{
2-
"pytorch": {
3-
"gpt_j_ipex":{
4-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
5-
"dataset_location": "",
6-
"input_model": "",
7-
"main_script": "run_clm_no_trainer.py",
8-
"batch_size": 1
9-
},
10-
"gpt_j_ipex_sq":{
11-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
12-
"dataset_location": "",
13-
"input_model": "",
14-
"main_script": "run_clm_no_trainer.py",
15-
"batch_size": 1
16-
},
17-
"llama2_7b_ipex":{
18-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
19-
"dataset_location": "",
20-
"input_model": "",
21-
"main_script": "run_clm_no_trainer.py",
22-
"batch_size": 1
23-
},
24-
"llama2_7b_ipex_sq":{
25-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
26-
"dataset_location": "",
27-
"input_model": "",
28-
"main_script": "run_clm_no_trainer.py",
29-
"batch_size": 1
30-
},
31-
"opt_125m_ipex":{
32-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
33-
"dataset_location": "",
34-
"input_model": "",
35-
"main_script": "run_clm_no_trainer.py",
36-
"batch_size": 8
37-
},
38-
"opt_125m_ipex_sq":{
39-
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
40-
"dataset_location": "",
41-
"input_model": "",
42-
"main_script": "run_clm_no_trainer.py",
43-
"batch_size": 8
44-
},
45-
"dlrm_ipex": {
46-
"model_src_dir": "recommendation/dlrm/static_quant/ipex",
47-
"dataset_location": "/mnt/local_disk3/dataset/dlrm/dlrm/input",
48-
"input_model": "/mnt/local_disk3/dataset/dlrm/dlrm/dlrm_weight/tb00_40M.pt",
49-
"main_script": "dlrm_s_pytorch.py",
50-
"batch_size": 16384
51-
}
52-
}
53-
}
1+
{
2+
"pytorch": {
3+
"gpt_j_ipex":{
4+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
5+
"dataset_location": "",
6+
"input_model": "",
7+
"main_script": "run_clm_no_trainer.py",
8+
"batch_size": 1
9+
},
10+
"gpt_j_ipex_sq":{
11+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
12+
"dataset_location": "",
13+
"input_model": "",
14+
"main_script": "run_clm_no_trainer.py",
15+
"batch_size": 1
16+
},
17+
"llama2_7b_ipex":{
18+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
19+
"dataset_location": "",
20+
"input_model": "",
21+
"main_script": "run_clm_no_trainer.py",
22+
"batch_size": 1
23+
},
24+
"llama2_7b_ipex_sq":{
25+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
26+
"dataset_location": "",
27+
"input_model": "",
28+
"main_script": "run_clm_no_trainer.py",
29+
"batch_size": 1
30+
},
31+
"opt_125m_ipex":{
32+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant",
33+
"dataset_location": "",
34+
"input_model": "",
35+
"main_script": "run_clm_no_trainer.py",
36+
"batch_size": 8
37+
},
38+
"opt_125m_ipex_sq":{
39+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/smooth_quant",
40+
"dataset_location": "",
41+
"input_model": "",
42+
"main_script": "run_clm_no_trainer.py",
43+
"batch_size": 8
44+
},
45+
"dlrm_ipex": {
46+
"model_src_dir": "recommendation/dlrm/static_quant/ipex",
47+
"dataset_location": "/mnt/local_disk3/dataset/dlrm/dlrm/input",
48+
"input_model": "/mnt/local_disk3/dataset/dlrm/dlrm/dlrm_weight/tb00_40M.pt",
49+
"main_script": "dlrm_s_pytorch.py",
50+
"batch_size": 16384
51+
},
52+
"resnet18_pt2e_static":{
53+
"model_src_dir": "cv/static_quant",
54+
"dataset_location": "/tf_dataset/pytorch/ImageNet/raw",
55+
"input_model": "",
56+
"main_script": "main.py",
57+
"batch_size": 1
58+
},
59+
"opt_125m_pt2e_static":{
60+
"model_src_dir": "nlp/huggingface_models/language-modeling/quantization/static_quant/pt2e",
61+
"dataset_location": "",
62+
"input_model": "",
63+
"main_script": "run_clm_no_trainer.py",
64+
"batch_size": 1
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ImageNet Quantization
2+
3+
This implements quantization of popular model architectures, such as ResNet on the ImageNet dataset.
4+
5+
## Requirements
6+
7+
- Install requirements
8+
- `pip install -r requirements.txt`
9+
- Download the ImageNet dataset from http://www.image-net.org/
10+
- Then, move and extract the training and validation images to labeled subfolders, using [the following shell script](extract_ILSVRC.sh)
11+
12+
## Quantizaiton
13+
14+
To quant a model and validate accaracy, run `main.py` with the desired model architecture and the path to the ImageNet dataset:
15+
16+
```bash
17+
python main.py -a resnet18 [imagenet-folder with train and val folders] -q -e
18+
```
19+
20+
21+
## Use Dummy Data
22+
23+
ImageNet dataset is large and time-consuming to download. To get started quickly, run `main.py` using dummy data by "--dummy". Note that the loss or accuracy is useless in this case.
24+
25+
```bash
26+
python main.py -a resnet18 --dummy -q -e
27+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
#
3+
# script to extract ImageNet dataset
4+
# ILSVRC2012_img_train.tar (about 138 GB)
5+
# ILSVRC2012_img_val.tar (about 6.3 GB)
6+
# make sure ILSVRC2012_img_train.tar & ILSVRC2012_img_val.tar in your current directory
7+
#
8+
# Adapted from:
9+
# https://github.com/facebook/fb.resnet.torch/blob/master/INSTALL.md
10+
# https://gist.github.com/BIGBALLON/8a71d225eff18d88e469e6ea9b39cef4
11+
#
12+
# imagenet/train/
13+
# ├── n01440764
14+
# │ ├── n01440764_10026.JPEG
15+
# │ ├── n01440764_10027.JPEG
16+
# │ ├── ......
17+
# ├── ......
18+
# imagenet/val/
19+
# ├── n01440764
20+
# │ ├── ILSVRC2012_val_00000293.JPEG
21+
# │ ├── ILSVRC2012_val_00002138.JPEG
22+
# │ ├── ......
23+
# ├── ......
24+
#
25+
#
26+
# Make imagnet directory
27+
#
28+
mkdir imagenet
29+
#
30+
# Extract the training data:
31+
#
32+
# Create train directory; move .tar file; change directory
33+
mkdir imagenet/train && mv ILSVRC2012_img_train.tar imagenet/train/ && cd imagenet/train
34+
# Extract training set; remove compressed file
35+
tar -xvf ILSVRC2012_img_train.tar && rm -f ILSVRC2012_img_train.tar
36+
#
37+
# At this stage imagenet/train will contain 1000 compressed .tar files, one for each category
38+
#
39+
# For each .tar file:
40+
# 1. create directory with same name as .tar file
41+
# 2. extract and copy contents of .tar file into directory
42+
# 3. remove .tar file
43+
find . -name "*.tar" | while read NAME ; do mkdir -p "${NAME%.tar}"; tar -xvf "${NAME}" -C "${NAME%.tar}"; rm -f "${NAME}"; done
44+
#
45+
# This results in a training directory like so:
46+
#
47+
# imagenet/train/
48+
# ├── n01440764
49+
# │ ├── n01440764_10026.JPEG
50+
# │ ├── n01440764_10027.JPEG
51+
# │ ├── ......
52+
# ├── ......
53+
#
54+
# Change back to original directory
55+
cd ../..
56+
#
57+
# Extract the validation data and move images to subfolders:
58+
#
59+
# Create validation directory; move .tar file; change directory; extract validation .tar; remove compressed file
60+
mkdir imagenet/val && mv ILSVRC2012_img_val.tar imagenet/val/ && cd imagenet/val && tar -xvf ILSVRC2012_img_val.tar && rm -f ILSVRC2012_img_val.tar
61+
# get script from soumith and run; this script creates all class directories and moves images into corresponding directories
62+
wget -qO- https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh | bash
63+
#
64+
# This results in a validation directory like so:
65+
#
66+
# imagenet/val/
67+
# ├── n01440764
68+
# │ ├── ILSVRC2012_val_00000293.JPEG
69+
# │ ├── ILSVRC2012_val_00002138.JPEG
70+
# │ ├── ......
71+
# ├── ......
72+
#
73+
#
74+
# Check total files after extract
75+
#
76+
# $ find train/ -name "*.JPEG" | wc -l
77+
# 1281167
78+
# $ find val/ -name "*.JPEG" | wc -l
79+
# 50000
80+
#

0 commit comments

Comments
 (0)