You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add IntelPytorch Quantization code samples
* fix the spelling error in the README file
* use john's README with grammar fix and title change
* Rename third-party-grograms.txt to third-party-programs.txt
Co-authored-by: Jimmy Wei <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# `Optimize PyTorch Models using Intel® Extension for PyTorch* Quantization` Sample
2
+
3
+
The `Optimize PyTorch Models using Intel® Extension for PyTorch* Quantization` sample demonstrates how to quantize a ResNet50 model that is calibrated by the CIFAR10 dataset using the Intel® Extension for PyTorch*.
4
+
5
+
The Intel® Extension for PyTorch* extends PyTorch* with optimizations for extra performance boost on Intel® hardware. While most of the optimizations will be included in future PyTorch* releases, the extension delivers up-to-date features and optimizations for PyTorch on Intel® hardware. For example, newer optimizations include AVX-512 Vector Neural Network Instructions (AVX512 VNNI) and Intel® Advanced Matrix Extensions (Intel® AMX).
6
+
7
+
| Area | Description
8
+
|:--- |:---
9
+
| What you will learn | Inference performance improvements using Intel® Extension for PyTorch* (IPEX) with feature quantization
10
+
| Time to complete | 5 minutes
11
+
| Category | Concepts and Functionality
12
+
13
+
## Purpose
14
+
15
+
The Intel® Extension for PyTorch* gives users the ability to speed up inference on Intel® Xeon Scalable processors with INT8 data format and specialized computer instructions. The INT8 data format uses quarter the bit width of floating-point-32 (FP32), lowering the amount of memory needed and execution time to process.
16
+
17
+
## Prerequisites
18
+
19
+
| Optimized for | Description
20
+
|:--- |:---
21
+
| OS | Ubuntu* 18.04 or newer
22
+
| Hardware | Intel® Xeon® Scalable Processor family
23
+
| Software | Intel® Extension for PyTorch*
24
+
25
+
### For Local Development Environments
26
+
27
+
You will need to download and install the following toolkits, tools, and components to use the sample.
28
+
29
+
-**Intel® AI Analytics Toolkit (AI Kit)**
30
+
31
+
You can get the AI Kit from [Intel® oneAPI Toolkits](https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html#analytics-kit). <br> See [*Get Started with the Intel® AI Analytics Toolkit for Linux**](https://www.intel.com/content/www/us/en/develop/documentation/get-started-with-ai-linux) for AI Kit installation information and post-installation steps and scripts.
32
+
33
+
-**Jupyter Notebook**
34
+
35
+
Install using PIP: `$pip install notebook`. <br> Alternatively, see [*Installing Jupyter*](https://jupyter.org/install) for detailed installation instructions.
36
+
37
+
-**Additional Packages**
38
+
39
+
You will need to install these additional packages: **Matplotlib** and **tqdm**.
40
+
```
41
+
python -m pip install matplotlib
42
+
python -m pip install tqdm
43
+
```
44
+
45
+
### For Intel® DevCloud
46
+
47
+
The necessary tools and components are already installed in the environment. You do not need to install additional components. See *[Intel® DevCloud for oneAPI](https://DevCloud.intel.com/oneapi/get_started/)* for information.
48
+
49
+
## Key Implementation Details
50
+
51
+
This code sample quantizes a ResNet50 model that is calibrated with the CIFAR10 dataset while using Intel® Extension for PyTorch*. The model is inferenced using FP32 and INT8 precision, including the use of Intel® Advanced Matrix Extensions (AMX). AMX is supported on BF16 and INT8 data types starting with 4th Gen Xeon Scalable Processors. The inference time will be compared, showcasing the speedup of INT8.
52
+
53
+
The sample tutorial contains one Jupyter Notebook and a Python script. You can use either.
54
+
55
+
### Jupyter Notebook
56
+
57
+
| Notebook | Description
58
+
|:--- |:---
59
+
|`IntelPyTorch_Quantization.ipynb` | Performs inference with IPEX quantization in Jupyter Notebook.
60
+
61
+
### Python Scripts
62
+
63
+
| Script | Description
64
+
|:--- |:---
65
+
|`IntelPyTorch_Quantization.py` | The script performs inference with IPEX quantization and compares the performance against the baseline
66
+
67
+
## Set Environment Variables
68
+
69
+
When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the `setvars` script every time you open a new terminal window. This practice ensures that your compiler, libraries, and tools are ready for development.
70
+
71
+
## Run the `Optimize PyTorch Models using Intel® Extension for PyTorch* Quantization` Sample
72
+
73
+
### On Linux*
74
+
75
+
> **Note**: If you have not already done so, set up your CLI
76
+
> environment by sourcing the `setvars` script in the root of your oneAPI installation.
77
+
>
78
+
> Linux*:
79
+
> - For system wide installations: `. /opt/intel/oneapi/setvars.sh`
80
+
> - For private installations: ` . ~/intel/oneapi/setvars.sh`
81
+
> - For non-POSIX shells, like csh, use the following command: `bash -c 'source <install-dir>/setvars.sh ; exec csh'`
82
+
>
83
+
> For more information on configuring environment variables, see *[Use the setvars Script with Linux* or macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html)*.
84
+
#### Activate Conda
85
+
86
+
1. Activate the Conda environment.
87
+
```
88
+
conda activate pytorch
89
+
```
90
+
2. Activate Conda environment without Root access (Optional).
91
+
92
+
By default, the AI Kit is installed in the `/opt/intel/oneapi` folder and requires root privileges to manage it.
93
+
94
+
You can choose to activate Conda environment without root access. To bypass root access to manage your Conda environment, clone and activate your desired Conda environment using the following commands similar to the following.
3. Follow the instructions to open the URL with the token in your browser.
109
+
4. Locate and select the Notebook.
110
+
```
111
+
IntelPyTorch_Quantization.ipynb
112
+
```
113
+
5. Change your Jupyter Notebook kernel to **PyTorch (AI kit)**.
114
+
6. Run every cell in the Notebook in sequence.
115
+
116
+
#### Running on the Command Line (Optional)
117
+
118
+
1. Change to the sample directory.
119
+
2. Run the script.
120
+
```
121
+
python IntelPyTorch_Quantization.py
122
+
```
123
+
124
+
### Run the `Optimize PyTorch Models using Intel® Extension for PyTorch* Quantization` Sample on Intel® DevCloud
125
+
126
+
1. If you do not already have an account, request an Intel® DevCloud account at [*Create an Intel® DevCloud Account*](https://intelsoftwaresites.secure.force.com/DevCloud/oneapi).
127
+
2. On a Linux* system, open a terminal.
128
+
3. SSH into Intel® DevCloud.
129
+
```
130
+
ssh DevCloud
131
+
```
132
+
> **Note**: You can find information about configuring your Linux system and connecting to Intel DevCloud at Intel® DevCloud for oneAPI [Get Started](https://DevCloud.intel.com/oneapi/get_started).
133
+
134
+
4. Follow the instructions to open the URL with the token in your browser.
135
+
5. Locate and select the Notebook.
136
+
```
137
+
IntelPyTorch_Quantization.ipynb
138
+
````
139
+
6. Change the kernel to **PyTorch (AI kit)**.
140
+
7. Run every cell in the Notebook in sequence.
141
+
142
+
### Troubleshooting
143
+
144
+
If you receive an error message, troubleshoot the problem using the **Diagnostics Utility for Intel® oneAPI Toolkits**. The diagnostic utility provides configuration and system checks to help find missing dependencies, permissions errors, and other issues. See the *[Diagnostics Utility for Intel® oneAPI Toolkits User Guide](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html)* for more information on using the utility.
145
+
146
+
## Example Output
147
+
148
+
If successful, the sample displays `[CODE_SAMPLE_COMPLETED_SUCCESSFULLY]`. Additionally, the sample generates performance and analysis diagrams for comparison.
149
+
150
+
The following image shows approximate performance speed increases using AMX INT8 during inference.
151
+
152
+

153
+
154
+
## License
155
+
156
+
Code samples are licensed under the MIT license. See
157
+
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt) for details.
158
+
159
+
Third party program Licenses can be found here: [third-party-programs.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/third-party-programs.txt)
0 commit comments