|
| 1 | +# W4A16 LLM Model Deployment |
| 2 | + |
| 3 | +LMDeploy supports LLM model inference of 4-bit weight, with the minimum requirement for NVIDIA graphics cards being sm80, such as A10, A100, Geforce 30/40 series. |
| 4 | + |
| 5 | +Before proceeding with the inference, please ensure that lmdeploy(>=v0.0.4) is installed. |
| 6 | + |
| 7 | +```shell |
| 8 | +pip install lmdeploy |
| 9 | +``` |
| 10 | + |
| 11 | +## 4-bit LLM model Inference |
| 12 | + |
| 13 | +You can download the pre-quantized 4-bit weight models from LMDeploy's [model zoo](https://huggingface.co/lmdeploy) and conduct inference using the following command. |
| 14 | + |
| 15 | +Alternatively, you can quantize 16-bit weights to 4-bit weights following the ["4-bit Weight Quantization"](#4-bit-weight-quantization) section, and then perform inference as per the below instructions. |
| 16 | + |
| 17 | +Take the 4-bit Llama-2-chat-7B model from the model zoo as an example: |
| 18 | + |
| 19 | +```shell |
| 20 | +git-lfs install |
| 21 | +git clone https://huggingface.co/lmdeploy/llama2-chat-7b-w4 |
| 22 | +``` |
| 23 | + |
| 24 | +As demonstrated in the command below, first convert the model's layout using `turbomind.deploy`, and then you can interact with the AI assistant in the terminal |
| 25 | + |
| 26 | +```shell |
| 27 | + |
| 28 | +## Convert the model's layout and store it in the default path, ./workspace. |
| 29 | +python3 -m lmdeploy.serve.turbomind.deploy \ |
| 30 | + --model-name llama2 \ |
| 31 | + --model-path ./llama2-chat-7b-w4 \ |
| 32 | + --model-format awq \ |
| 33 | + --group-size 128 |
| 34 | + |
| 35 | +## inference |
| 36 | +python3 -m lmdeploy.turbomind.chat ./workspace |
| 37 | +``` |
| 38 | + |
| 39 | +## Serve with gradio |
| 40 | + |
| 41 | +If you wish to interact with the model via web ui, please initiate the gradio server as indicated below: |
| 42 | + |
| 43 | +```shell |
| 44 | +python3 -m lmdeploy.serve.turbomind ./workspace --server_name {ip_addr} ----server_port {port} |
| 45 | +``` |
| 46 | + |
| 47 | +Subsequently, you can open the website `http://{ip_addr}:{port}` in your browser and interact with the model |
| 48 | + |
| 49 | +## Inference Performance |
| 50 | + |
| 51 | +We benchmarked the Llama-2-7B-chat and Llama-2-13B-chat models with 4-bit quantization on NVIDIA GeForce RTX 4090 using [profile_generation.py](https://github.com/InternLM/lmdeploy/blob/main/benchmark/profile_generation.py). And we measure the token generation throughput (tokens/s) by setting a single prompt token and generating 512 tokens. All the results are measured for single batch inference. |
| 52 | + |
| 53 | +| model | llm-awq | mlc-llm | turbomind | |
| 54 | +| ---------------- | ------- | ------- | --------- | |
| 55 | +| Llama-2-7B-chat | 112.9 | 159.4 | 206.4 | |
| 56 | +| Llama-2-13B-chat | N/A | 90.7 | 115.8 | |
| 57 | + |
| 58 | +Memory (GB) comparison results between 4-bit and 16-bit model with context size 2048 and 4096 respectively, |
| 59 | + |
| 60 | +| model | 16bit(2048) | 4bit(2048) | 16bit(4096) | 4bit(4096) | |
| 61 | +| ---------------- | ----------- | ---------- | ----------- | ---------- | |
| 62 | +| Llama-2-7B-chat | 15.1 | 6.3 | 16.2 | 7.5 | |
| 63 | +| Llama-2-13B-chat | OOM | 10.3 | OOM | 12.0 | |
| 64 | + |
| 65 | +```shell |
| 66 | +python benchmark/profile_generation.py \ |
| 67 | + ./workspace \ |
| 68 | + --concurrency 1 --input_seqlen 1 --output_seqlen 512 |
| 69 | +``` |
| 70 | + |
| 71 | +## 4-bit Weight Quantization |
| 72 | + |
| 73 | +It includes two steps: |
| 74 | + |
| 75 | +- generate quantization parameter |
| 76 | +- quantize model according to the parameter |
| 77 | + |
| 78 | +### Step 1: Generate Quantization Parameter |
| 79 | + |
| 80 | +```shell |
| 81 | +python3 -m lmdeploy.lite.apis.calibrate \ |
| 82 | + --model $HF_MODEL \ |
| 83 | + --calib_dataset 'c4' \ # Calibration dataset, supports c4, ptb, wikitext2, pileval |
| 84 | + --calib_samples 128 \ # Number of samples in the calibration set, if memory is insufficient, you can appropriately reduce this |
| 85 | + --calib_seqlen 2048 \ # Length of a single piece of text, if memory is insufficient, you can appropriately reduce this |
| 86 | + --work_dir $WORK_DIR \ # Folder storing Pytorch format quantization statistics parameters and post-quantization weight |
| 87 | +``` |
| 88 | + |
| 89 | +### Step2: Quantize Weights |
| 90 | + |
| 91 | +LMDeploy employs AWQ algorithm for model weight quantization. |
| 92 | + |
| 93 | +```shell |
| 94 | +python3 -m lmdeploy.lite.apis.auto_awq \ |
| 95 | + --model $HF_MODEL \ |
| 96 | + --w_bits 4 \ # Bit number for weight quantization |
| 97 | + --w_group_size 128 \ # Group size for weight quantization statistics |
| 98 | + --work_dir $WORK_DIR \ # Directory saving quantization parameters from Step 1 |
| 99 | +``` |
| 100 | + |
| 101 | +After the quantization is complete, the quantized model is saved to `$WORK_DIR`. Then you can proceed with model inference according to the instructions in the ["4-Bit Weight Model Inference"](#4-bit-llm-model-inference) section. |
0 commit comments