Intel® Extension for PyTorch* Large Language Model (LLM) Feature Get Started For Llama 3 models

Intel® Extension for PyTorch* provides dedicated optimization for running Llama 3 models faster, including technical points like paged attention, ROPE fusion, etc. And a set of data types are supported for various scenarios, including BF16, Weight Only Quantization, etc. You are welcome to have a try on AWS m7i.metal-48xl instance with Ubuntu 22.04.

1. Environment Setup

There are several environment setup methodologies provided. You can choose either of them according to your usage scenario. The Docker-based ones are recommended.

1.2 Conda-based environment setup with pre-built wheels

# Get the Intel® Extension for PyTorch* source code
git clone https://github.com/intel/intel-extension-for-pytorch.git
cd intel-extension-for-pytorch
git checkout 2.3-rc1-sp
git submodule sync
git submodule update --init --recursive

# Create a conda environment (pre-built wheel only available with python=3.10)
conda create -n llm python=3.10 -y
conda activate llm

# Setup the environment with the provided script
# A sample "prompt.json" file for benchmarking is also downloaded
cd examples/cpu/inference/python/llm
bash ./tools/env_setup.sh 7

# Activate environment variables
source ./tools/env_activate.sh

2. How To Run Llama 3 with ipex.llm

ipex.llm provides a single script to facilitate running generation tasks as below:

# if you are using a docker container built from commands above in Sec. 1.1, the placeholder LLM_DIR below is /home/ubuntu/llm
# if you are using a conda env created with commands above in Sec. 1.2, the placeholder LLM_DIR below is intel-extension-for-pytorch/examples/cpu/inference/python/llm
cd <LLM_DIR>
python run.py --help # for more detailed usages
Key args of run.py Notes
model id "--model-name-or-path" or "-m" to specify the , it is model id from Huggingface or downloaded local path
generation default: beam search (beam size = 4), "--greedy" for greedy search
input tokens provide fixed sizes for input prompt size, use "--input-tokens" for in [1024, 2048, 4096, 8192]; if "--input-tokens" is not used, use "--prompt" to choose other strings as inputs
output tokens default: 32, use "--max-new-tokens" to choose any other size
batch size default: 1, use "--batch-size" to choose any other size
token latency enable "--token-latency" to print out the first or next token latency
generation iterations use "--num-iter" and "--num-warmup" to control the repeated iterations of generation, default: 100-iter/10-warmup
streaming mode output greedy search only (work with "--greedy"), use "--streaming" to enable the streaming generation output

Note: You may need to log in your HuggingFace account to access the model files. Please refer to HuggingFace login.

2.1 Usage of running Llama 3 models

The <LLAMA3_MODEL_ID_OR_LOCAL_PATH> in the below commands specifies the Llama 3 model you will run, which can be found from HuggingFace Models.

2.1.1 Run generation with one socket inference

2.1.1.1 BF16:

  • Command:

OMP_NUM_THREADS=<physical cores num> numactl -m <node N> -C <physical cores list> python run.py --benchmark -m <LLAMA3_MODEL_ID_OR_LOCAL_PATH> --dtype bfloat16 --ipex --greedy --input-tokens <INPUT_LENGTH> 

2.1.1.2 Weight-only quantization (INT8):

By default, for weight-only quantization, we use quantization with Automatic Mixed Precision inference (”–quant-with-amp”) to get peak performance and fair accuracy.

  • Command:

OMP_NUM_THREADS=<physical cores num> numactl -m <node N> -C <physical cores list>  python run.py  --benchmark -m <LLAMA3_MODEL_ID_OR_LOCAL_PATH> --ipex-weight-only-quantization --weight-dtype INT8 --quant-with-amp --output-dir "saved_results"  --greedy --input-tokens <INPUT_LENGTH>
# Note: you can add "--group-size" to tune good accuracy, suggested range as one of [32, 64, 128, 256, 512].

2.1.1.3 Notes:

(1) numactl is used to specify memory and cores of your hardware to get better performance. <node N> specifies the numa node id (e.g., 0 to use the memory from the first numa node). <physical cores list> specifies phsysical cores which you are using from the <node N> numa node (e.g., 0-47 from the first numa node of AWS m7i.metal-48xl instance). You can use lscpu command in Linux to check the numa node information.

(2) For all quantization benchmarks, both quantization and inference stages will be triggered by default. For quantization stage, it will auto-generate the quantized model named “best_model.pt” in the “–output-dir” path, and for inference stage, it will launch the inference with the quantized model “best_model.pt”. For inference-only benchmarks (avoid the repeating quantization stage), you can also reuse these quantized models for by adding “–quantized-model-path <output_dir + “best_model.pt”>” .

2.1.2 Run generation with two sockets inference (DeepSpeed autotp)

2.1.2.1 Prepare:

unset KMP_AFFINITY

In the DeepSpeed cases below, we recommend “–shard-model” to shard model weight sizes more even for better memory usage when running with DeepSpeed.

If using “–shard-model”, it will save a copy of the shard model weights file in the path of “–output-dir” (default path is “./saved_results” if not provided). If you have used “–shard-model” and generated such a shard model path (or your model weights files are already well sharded), in further repeated benchmarks, please remove “–shard-model”, and replace “-m <LLAMA3_MODEL_ID_OR_LOCAL_PATH>” with “-m ” to skip the repeated shard steps.

Besides, the standalone shard model function/scripts are also provided in section 2.1.2.4, in case you would like to generate the shard model weights files in advance before running distributed inference.

2.1.2.2 BF16:

  • Command:

deepspeed --bind_cores_to_rank  run.py --benchmark -m <LLAMA3_MODEL_ID_OR_LOCAL_PATH> --dtype bfloat16 --ipex  --greedy --input-tokens <INPUT_LENGTH> --autotp --shard-model

2.1.2.3 Weight-only quantization (INT8):

By default, for weight-only quantization, we use quantization with Automatic Mixed Precision inference (”–quant-with-amp”) to get peak performance and fair accuracy. For weight-only quantization with deepspeed, we quantize the model then run the benchmark. The quantized model won’t be saved.

  • Command:

deepspeed --bind_cores_to_rank run.py  --benchmark -m <LLAMA3_MODEL_ID_OR_LOCAL_PATH> --ipex --ipex-weight-only-quantization --weight-dtype INT8 --quant-with-amp --greedy --input-tokens <INPUT_LENGTH>  --autotp --shard-model --output-dir "saved_results"
# Note: you can add "--group-size" to tune good accuracy, suggested range as one of [32, 64, 128, 256, 512].

2.1.2.4 How to Shard Model weight files for Distributed Inference with DeepSpeed

To save memory usage, we could shard the model weights files under the local path before we launch distributed tests with DeepSpeed.

cd ./utils
# general command:
python create_shard_model.py -m <LLAMA3_MODEL_ID_OR_LOCAL_PATH>  --save-path ./local_llama3_model_shard
# After sharding the model, using "-m ./local_llama3_model_shard" in later tests

Miscellaneous Tips

Intel® Extension for PyTorch* also provides dedicated optimization for many other Large Language Models (LLM), which cover a set of data types that are supported for various scenarios. For more details, please check this Intel® Extension for PyTorch* doc.