Deploy the Qwen-7B-Chat model on an 8th-generation Intel instance

更新时间:
复制 MD 格式

This article uses an AI chatbot as an example to demonstrate how to deploy the Qwen-7B-Chat language model on an Intel-based c8i instance by using the xFasterTransformer framework.

Background

Qwen-7B-Chat

Qwen-7B is a 7-billion-parameter model in the Qwen large language model series developed by Alibaba Cloud. Qwen-7B is a Transformer-based large language model trained on a massive and diverse pre-training dataset that includes web text, professional books, and code. The AI assistant, Qwen-7B-Chat, is based on Qwen-7B and fine-tuned using alignment techniques.

Alibaba Cloud 8th-generation Intel CPU instances

Alibaba Cloud 8th-generation instances (g8i, c8i, r8i, hfc8i, hfg8i, and hfr8i) are powered by Intel® Xeon® Emerald Rapids or Intel® Xeon® Sapphire Rapids processors. These instances support the new Advanced Matrix Extensions (AMX) instruction set to accelerate AI tasks. Intel® AMX allows these instances to deliver significantly improved inference and training performance compared to the previous generation.

xFasterTransformer

xFasterTransformer is an open-source inference framework from Intel. It provides a highly optimized solution for deploying large language models (LLMs) on the x86 CPU platform. It also supports distributed deployment across multiple CPU nodes, enabling the deployment of extra-large models on CPUs. Additionally, xFasterTransformer offers both C++ and Python APIs, covering a range of calls from high-level to low-level, making it easy to integrate xFasterTransformer into custom service frameworks. xFasterTransformer currently supports the following models:

Models

Framework

Distribution

Pytorch

C++

ChatGLM

ChatGLM2

ChatGLM3

Llama

Llama2

Baichuan

QWen

SecLLM(YaRN-Llama)

Opt

xFasterTransformer supports various low-precision data types to accelerate model deployment. In addition to single-precision types, it also supports mixed precision to make better use of CPU compute and bandwidth resources, thereby increasing the inference speed of large language models. The following single-precision and mixed-precision types are supported by xFasterTransformer:

  • FP16

  • BF16

  • INT8

  • W8A8

  • INT4

  • NF4

  • BF16_FP16

  • BF16_INT8

  • BF16_W8A8

  • BF16_INT4

  • BF16_NF4

  • W8A8_INT8

  • W8A8_int4

  • W8A8_NF4

Important

The code for Qwen-7B-Chat is open-sourced under the LICENSE. A commercial use license application is required for free commercial use. You must comply with the user agreements, usage specifications, and relevant laws and regulations for third-party models. You are solely responsible for the legality and compliance of your use of third-party models.

Step 1: Create an ECS instance

  1. Go to the instance creation page.

  2. Follow the on-screen instructions to configure the parameters and create an ECS instance.

    The following parameters are important. For information about how to configure other parameters, see Customize and purchase an instance.

    • Instance: Qwen-7B-Chat requires more than 16 GiB of memory. To ensure stable model performance, select at least ecs.c8i.4xlarge (32 GiB memory) for the instance type.

    • Image: Alibaba Cloud Linux 3.2104 LTS 64-bit.

    • Public IP: Select Assign Public IPv4 Address. Set the billing method for bandwidth to Pay-By-Traffic and the peak bandwidth to 100 Mbps to accelerate model downloads.

      image..png

    • System disk: The Qwen-7B-Chat model requires 60 GiB of storage for data downloading, conversion, and execution. We recommend setting the system disk size to 100 GiB to ensure that the model runs smoothly.

  3. Add a security group rule.

    In the inbound direction of the security group for the ECS instance, add a rule to allow traffic on port 22 for SSH access and port 7860 for WebUI access. For detailed instructions, see Add a security group rule.

Step 2: Install the container environment

  1. Connect to the ECS instance.

    For detailed instructions, see Connect to a Linux instance by using Workbench.

  2. Install and start Docker.

    For detailed instructions, see Install and use Docker and Docker Compose.

  3. Pull and run the Intel xFasterTransformer container.

    sudo docker pull registry.openanolis.cn/openanolis/xfastertransformer:1.7.3-23
    sudo docker run -it --name xFT -h xFT --privileged --shm-size=16g --network host -v /mnt:/mnt -w /mnt/xFasterTransformer registry.openanolis.cn/openanolis/xfastertransformer:1.7.3-23

    The following output indicates that the xFasterTransformer container is running.

    image

    Important

    All subsequent operations must be performed within the container. If you exit the container, you can restart it and attach to its shell by running the following commands:

    sudo docker start xFT
    sudo docker exec -it xFT bash
  4. (Optional) Update the xFasterTransformer scripts.

    The xFasterTransformer image already includes the scripts for the corresponding version. You can update to the latest test scripts.

    yum update -y
    yum install -y git
    cd /root/xFasterTransformer
    git pull

Step 3: Prepare the model data

  1. In the container, install the required dependencies.

    yum update -y
    yum install -y wget git git-lfs vim tmux
  2. Enable Git LFS.

    Git LFS is required to download the pre-trained model.

    git lfs install
  3. Create and navigate to the model data directory.

    mkdir /mnt/data
    cd /mnt/data
  4. Create a tmux session.

    tmux
    Important

    Downloading the pre-trained model can be time-consuming, and download times vary depending on network conditions. We recommend downloading the model within a tmux session to prevent the download from being interrupted by a lost SSH connection.

  5. Download the Qwen-7B-Chat pre-trained model.

    git clone https://www.modelscope.cn/qwen/Qwen-7B-Chat.git /mnt/data/qwen-7b-chat
    Important

    After you run the git clone command, Git starts downloading the repository and all its large files through Git LFS. This process can take from several minutes to a few hours, depending on the model size and your network conditions. Wait for the download to complete.

    The following output indicates that the model download is complete.

    image

  6. Convert the model data.

    The downloaded model is in Hugging Face format and must be converted to the xFasterTransformer format. This conversion generates the model in the /mnt/data/qwen-7b-chat-xft directory.

    python -c 'import xfastertransformer as xft; xft.QwenConvert().convert("/mnt/data/qwen-7b-chat")'

    The following output indicates that the model conversion is complete.

    image

    Note

    Different models require different Convert classes. xFasterTransformer supports the following model conversion classes:

    • LlamaConvert

    • ChatGLMConvert

    • ChatGLM2Convert

    • ChatGLM3Convert

    • OPTConvert

    • BaichuanConvert

    • QwenConvert

Step 4: Run the model for AI chat

Web UI

  1. In the container, run the following commands to install dependencies for the web UI.

    cd /root/xFasterTransformer/examples/web_demo
    pip install -r requirements.txt
  2. Run the following command to upgrade gradio to avoid conflicts with fastapi.

    pip install --upgrade gradio
  3. Run the following command to start the web UI.

    OMP_NUM_THREADS=$(($(lscpu | grep "^CPU(s):" | awk '{print $NF}') / 2)) GRADIO_SERVER_NAME="0.0.0.0" numactl -C $(seq -s, 0 2 $(($(lscpu | grep "^CPU(s):" | awk '{print $NF}') - 2))) -m 0 python Qwen.py -t /mnt/data/qwen-7b-chat -m /mnt/data/qwen-7b-chat-xft -d bf16

    The following output indicates that the web UI service has started.

    image

  4. In your browser's address bar, enter http://<ECS public IP>:7860, replacing the <ECS public IP> placeholder with the public IP address of your instance.

    image

  5. In the chat box, enter your text and click Submit to start the AI conversation.

    image

Terminal

Run the following command to start the AI chat program.

cd /root/xFasterTransformer/examples/pytorch
OMP_NUM_THREADS=$(($(lscpu | grep "^CPU(s):" | awk '{print $NF}') / 2)) LD_PRELOAD=libiomp5.so numactl -C $(seq -s, 0 2 $(($(lscpu | grep "^CPU(s):" | awk '{print $NF}') - 2))) -m 0 python demo.py -t /mnt/data/qwen-7b-chat -m /mnt/data/qwen-7b-chat-xft -d bf16 --chat true

image

Benchmark model performance

The benchmark script uses dummy model data by default, so you do not need to download a model. Run the following command to benchmark the model's performance.

cd /root/xFasterTransformer/benchmark
XFT_CLOUD_ENV=1 bash run_benchmark.sh -m qwen-7b -d bf16 -bs 1 -in 32 -out 32 -i 10

You can adjust the following runtime parameters to test performance in specific scenarios:

  • -d or --dtype: Specifies the model quantization type.

    • bf16 (default)

    • bf16_fp16

    • int8

    • bf16_int8

    • fp16

    • bf16_int4

    • int4

    • bf16_nf4

    • nf4

    • bf16_w8a8

    • w8a8

    • w8a8_int8

    • w8a8_int4

    • w8a8_nf4

  • -bs or --batch_size: Specifies the batch size. The default value is 1.

  • -in or --input_tokens: Specifies the input length. To use a custom length, configure the corresponding prompt in the prompt.json file. The default value is 32.

  • -out or --output_tokens: Specifies the output length. The default value is 32.

  • -i or --iter: Specifies the number of iterations. A larger number of iterations increases the test duration. The default value is 10.

Example output:

image