The Fine-tuning Text Classification Model with Intel® Neural Compressor (INC)
sample demonstrates how to fine-tune BERT tiny model for emotion classification task using Quantization Aware Training (QAT) from Intel® Neural Compressor (INC).
Area | Description |
---|---|
What you will learn | How to fine-tune text model using Intel® Neural Compressor (INC) Quantization Aware Training |
Time to complete | 10 minutes |
Category | Concepts and Functionality |
Intel® Neural Compressor (INC) simplifies the process of converting the FP32 model to INT8/BF16. At the same time, Intel® Neural Compressor (INC) tunes the quantization method to reduce the accuracy loss, which is a big blocker for low-precision inference as part of AI Tools.
This sample shows how to fine-tune text model for emotion classification on pre-trained bert-tiny
model from Hugging Face and how to perform fine-tuning using Intel® Neural Compressor (INC) Quantization Aware Training. Fine-tuning allows you to speed up operations on 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 with minimum to zero accuracy loss.
Optimized for | Description |
---|---|
OS | Ubuntu* 20.04 (or newer) |
Hardware | Intel® Xeon® Scalable Processor family |
Software | Intel® Neural Compressor (INC) |
You will need to download and install the following toolkits, tools, and components to use the sample.
-
AI Tools
You can get the AI Tools from Intel® oneAPI Toolkits.
See Get Started with the AI Tools for Linux* for AI Tools installation information and post-installation steps and scripts. -
Jupyter Notebook
Install using PIP:
$pip install notebook
.
Alternatively, see Installing Jupyter for detailed installation instructions. -
Additional Packages
You will need to install the additional packages in requirements.txt.
pip install -r requirements.txt
The necessary tools and components are already installed in the environment. You do not need to install additional components. See Intel® DevCloud for oneAPI for information.
This code sample implements fine-tuning process for text classification using Intel® Neural Compressor (INC) quantization aware training.
The sample tutorial contains one Jupyter Notebook and a Python script. You can use either.
Notebook | Description |
---|---|
mINC_QuantizationAwareTraining_TextClassification.ipynb |
Performs chat creation with Intel® Extension for PyTorch (IPEX) quantization and provides interface for interactions in Jupyter Notebook. |
Script | Description |
---|---|
INC_QuantizationAwareTraining_TextClassification.py |
The script performs chat creation with Intel® Extension for PyTorch (IPEX) quantization and provides simple interactions based on prepared input. |
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.
Note: If you have not already done so, set up your CLI environment by sourcing the
setvars
script in the root of your oneAPI installation.Linux*:
- For system wide installations:
. /opt/intel/oneapi/setvars.sh
- For private installations:
. ~/intel/oneapi/setvars.sh
- For non-POSIX shells, like csh, use the following command:
bash -c 'source <install-dir>/setvars.sh ; exec csh'
For more information on configuring environment variables, see Use the setvars Script with Linux* or macOS*.
-
Activate the Conda environment.
conda activate pytorch
-
Activate Conda environment without Root access (Optional).
By default, the AI Tools is installed in the
/opt/intel/oneapi
folder and requires root privileges to manage it.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.
conda create --name user_pytorch --clone pytorch conda activate user_pytorch
- Change to the sample directory.
- Launch Jupyter Notebook.
jupyter notebook --ip=0.0.0.0 --port 8888 --allow-root
- Follow the instructions to open the URL with the token in your browser.
- Locate and select the Notebook.
INC_QuantizationAwareTraining_TextClassification.ipynb
- Change your Jupyter Notebook kernel to corresponding environment.
- Run every cell in the Notebook in sequence.
- Change to the sample directory.
- Run the script.
python INC_QuantizationAwareTraining_TextClassification.py
-
If you do not already have an account, request an Intel® DevCloud account at Create an Intel® DevCloud Account.
-
On a Linux* system, open a terminal.
-
SSH into Intel® DevCloud.
ssh DevCloud
Note: You can find information about configuring your Linux system and connecting to Intel DevCloud at Intel® DevCloud for oneAPI Get Started.
-
Follow the instructions to open the URL with the token in your browser.
-
Locate and select the Notebook.
INC_QuantizationAwareTraining_TextClassification.ipynb
-
Change your Jupyter Notebook kernel to corresponding environment.
-
Run every cell in the Notebook in sequence.
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 for more information on using the utility.
If successful, the sample displays [CODE_SAMPLE_COMPLETED_SUCCESSFULLY]
. Additionally, the sample shows statistics for training, quantization information and classification results, before and after fine-tuning.
Code samples are licensed under the MIT license. See License.txt for details.
Third party program Licenses can be found here: third-party-programs.txt.