The Quantizing Transformer Model using Intel® Extension for Transformers (ITREX) sample illustrates the process of quantizing the Intel/neural-chat-7b-v3-3 language model. This model, a fine-tuned iteration of Mistral-7B, undergoes quantization utilizing Weight Only Quantization (WOQ) techniques provided by Intel® Extension for Transformers (ITREX).
By leveraging WOQ techniques, developers can optimize the model's memory footprint and computational efficiency without sacrificing performance or accuracy. This sample serves as a practical demonstration of how ITREX empowers users to maximize the potential of transformer models in various applications, especially in resource-constrained environments.
| Area | Description |
|---|---|
| What you will learn | How to quantize transformer models using Intel® Extension for Transformers (ITREX) |
| Time to complete | 20 minutes |
| Category | Concepts and Functionality |
Intel® Extension for Transformers (ITREX) serves as a comprehensive toolkit tailored to enhance the performance of GenAI/LLM (General Artificial Intelligence/Large Language Models) workloads across diverse Intel platforms. Among its key features is the capability to seamlessly quantize transformer models to 4-bit or 8-bit integer precision.
This quantization functionality not only facilitates significant reduction in memory footprint but also offers developers the flexibility to fine-tune the quantization method. This customization empowers developers to mitigate accuracy loss, a crucial concern in low-precision inference scenarios. By striking a balance between memory efficiency and model accuracy, ITREX enables efficient deployment of transformer models in resource-constrained environments without compromising on performance or quality.
This sample demonstrates how to quantize a pre-trained language model, specifically the Intel/neural-chat-7b-v3-3 model from Intel. Quantization enables more memory-efficient inference, significantly reducing the model's memory footprint.
Using the INT8 data format, which employs a quarter of the bit width of floating-point-32 (FP32), memory usage can be lowered by up to 75%. Additionally, execution time for arithmetic operations is reduced. The INT4 data type takes memory optimization even further, consuming 8 times less memory than FP32.
Quantization thus offers a compelling approach to deploying language models in resource-constrained environments, ensuring both efficient memory utilization and faster inference times.
| Optimized for | Description |
|---|---|
| OS | Ubuntu* 22.04.3 LTS (or newer) |
| Hardware | Intel® Xeon® Scalable Processor family |
| Software | Intel® Extension for Transformers (ITREX) |
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 showcases the implementation of quantization for memory-efficient text generation utilizing Intel® Extension for Transformers (ITREX).
The sample includes both a Jupyter Notebook and a Python Script. While the notebook serves as a tutorial for learning purposes, it's recommended to use the Python script in production setups for optimal performance.
| Notebook | Description |
|---|---|
quantize_transformer_models_with_itrex.ipynb |
This notebook provides detailed steps for performing INT4/INT8 quantization of transformer models using Intel® Extension for Transformers (ITREX). It's designed to aid understanding and experimentation. |
| Script | Description |
|---|---|
quantize_transformer_models_with_itrex.py |
The Python script conducts INT4/INT8 quantization of transformer models leveraging Intel® Extension for Transformers (ITREX). It allows text generation based on an initial prompt, giving users the option to select either INT4 or INT8 quantization. |
These components offer flexibility for both learning and practical application, empowering users to harness the benefits of quantization for transformer models efficiently.
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 intelpython environment's activate 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
intelpythonenvironment's activate script in the root of your oneAPI installation.Linux*:
- For POXIS shells, run:
source ${HOME}/intel/oneapi/intelpython/bin/activate- For non-POSIX shells, like csh, use the following command:
bash -c 'source ${HOME}/intel/oneapi/intelpython/bin/activate ; 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
<home>/intel/oneapi/intelpythonfolder 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
- Run the following command:
pip install -r requirements.txt
This script will automatically install all the required dependencies.
- Navigate to the sample directory in your terminal.
- Launch Jupyter Notebook with the following command:
jupyter notebook --ip=0.0.0.0 --port 8888 --allow-root - Follow the instructions provided in the terminal to open the URL with the token in your web browser.
- Locate and select the notebook file named
quantize_transformer_models_with_itrex.ipynb. - Ensure that you change the Jupyter Notebook kernel to the corresponding environment.
- Run each cell in the notebook sequentially.
-
Navigate to the sample directory in your terminal.
-
Execute the script using the following command:
OMP_NUM_THREADS=<number of physical cores> numactl -m <node index> -C <CPU list> python quantize_transformer_models_with_itrex.py
Note: You can use the command
numactl -Hto identify the number of nodes, node indices, and CPU lists. Additionally, thelscpucommand provides information about the number of physical cores available.For example, if you have 8 physical cores, 1 node (index 0), and want to use CPUs 0-3, you would run:
OMP_NUM_THREADS=4 numactl -m 0 -C 0-3 python quantize_transformer_models_with_itrex.py
It is generally recommended to utilize all available physical cores and CPUs within a single node. Thus, you can simplify the command as follows:
OMP_NUM_THREADS=<number of physical cores> numactl -m 0 -C all python quantize_transformer_models_with_itrex.py
Here are some examples demonstrating how to use the quantize_transformer_models_with_itrex.py script:
-
Quantize the model to INT4, and specify a maximum number of new tokens:
OMP_NUM_THREADS=<number of physical cores> numactl -m 0 -C all \ python quantize_transformer_models_with_itrex.py \ --model_name "Intel/neural-chat-7b-v3-1" \ --quantize "int8" \ --max_new_tokens 100
-
Quantize the model to INT4, disable Neural Speed, and specify a custom prompt:
OMP_NUM_THREADS=<number of physical cores> numactl -m 0 -C all \ python quantize_transformer_models_with_itrex.py \ --model_name "Intel/neural-chat-7b-v3-1" \ --no_neural_speed \ --quantize "int4" \ --prompt "Custom prompt text goes here" \ --max_new_tokens 50
-
Use a Llama2-7B GGUF model from HuggingFace model hub . When using GGUF model,
tokenizer_nameandmodel_namearguments are required and Neural Speed is enable by default. Note: You will need to request access for Llama2 on HuggingFace to run the below command.OMP_NUM_THREADS=<number of physical cores> numactl -m 0 -C all \ python quantize_transformer_models_with_itrex.py \ --model_name "TheBloke/Llama-2-7B-Chat-GGUF" \ --model_file "llama-2-7b-chat.Q4_0.gguf"\ --tokenizer_name "meta-llama/Llama-2-7b-chat-hf" \ --prompt "Custom prompt text goes here" \ --max_new_tokens 50
- Ensure that you follow the provided instructions carefully to execute the project successfully.
- Make sure to adjust the command parameters based on your specific system configuration and requirements.
-
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 DevCloudNote: 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.
quantize_transformer_models_with_itrex.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 model size and memory comsumption, before and after quantization.
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.