InternLM / InternLM/lmdeploy

[Bug] Qwen/Qwen2-72B-Instruct AWQ Quantization NaN Error

Open
#1,786 9 comments 0 reactions 1 assignee Claimed by @AllentDan View on GitHub
Dominant language
Python
Stars
8.1k
Forks
748
Avg merge
6d 2h
Merged PRs (30d)
54

Description

### Checklist

- [X] 1. I have searched related issues but cannot get the expected help.
- [X] 2. The bug has not been fixed in the latest version.

### Describe the bug

When quantizing Qwen2-72B-Instruct, it fails with the assertion at this layer `model.layers.2.mlp.gate_proj` by,

```
assert torch.isnan(p).sum() == 0
```
I tried `--search-scale` from the issue [#1656](https://github.com/InternLM/lmdeploy/issues/1656), which runs into the same error.

It turns out that when [calculating weight scales](https://github.com/InternLM/lmdeploy/blob/bd431bfc74a7132c146c67a21fdd3446fc5870c4/lmdeploy/lite/quantization/awq.py#L64C1-L72C17), many groups of weights happen to be all zeros which caused zero division. I changed to the following to avoid it,

```python
@torch.no_grad()
def get_weight_scale(weight, q_group_size=-1):
org_shape = weight.shape
if q_group_size > 0:
weight = weight.view(-1, q_group_size)
# scale = weight.abs() / (weight.abs().amax(dim=1, keepdim=True))
epsilon = 1e-6
max_abs = weight.abs() + epsilon
scale = weight.abs() / max_abs.amax(dim=1, keepdim=True)
scale = scale.view(org_shape)
scale = scale.mean(0)
return scale
```

A similar assertion happens when calculating smoothed scales [here](https://github.com/InternLM/lmdeploy/blob/bd431bfc74a7132c146c67a21fdd3446fc5870c4/lmdeploy/lite/quantization/awq.py#L153), I changed into the following,

```python
concat_w = torch.cat([fc.weight for fc in fcs], dim=0)
w_scales = get_weight_scale(concat_w, group_size)

# fix div by zero
zero_mask = (w_scales == 0)
if zero_mask.any():
w_scales[zero_mask] = 1e-6

scales = (act_scales.pow(alpha) /
w_scales.pow(1 - alpha)).clamp(min=1e-4).to(device).to(dtype)
scales = scales / (scales.max() * scales.min()).sqrt()

# fix div by zero
zero_mask = (scales == 0)
if zero_mask.any():
scales[zero_mask] = 1
```

Then the quantization went without error. I haven't checked the accuracy of the model yet. Please correct me I've made mistakes.

### Reproduction

```
lmdeploy lite auto_awq /path/to/Qwen2-72B-Instruct \
--calib-dataset c4 --calib-samples 32 --calib-seqlen 512 --w-bits 4 --w-group-size 128 \
--work-dir /path/to/Qwen2-72B-Instruct-Quant
```

### Environment

```Shell
sys.platform: linux
Python: 3.8.18 (default, Sep 11 2023, 13:40:15) [GCC 11.2.0]
CUDA available: True
MUSA available: False
numpy_random_seed: 2147483648
GPU 0,1: NVIDIA A100-SXM4-80GB
CUDA_HOME: /usr/local/cuda
NVCC: Cuda compilation tools, release 11.7, V11.7.99
GCC: gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
PyTorch: 2.2.1+cu118
PyTorch compiling details: PyTorch built with:
- GCC 9.3
- C++ Version: 201703
- Intel(R) oneAPI Math Kernel Library Version 2022.2-Product Build 20220804 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v3.3.2 (Git Hash 2dc95a2ad0841e29db8b22fbccaf3e5da7992b01)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- LAPACK is enabled (usually provided by MKL)
- NNPACK is enabled
- CPU capability usage: AVX2
- CUDA Runtime 11.8
- NVCC architecture flags: -gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_90,code=sm_90
- CuDNN 8.9.2 (built against CUDA 12.1)
- Built with CuDNN 8.7
- Magma 2.6.1
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.8, CUDNN_VERSION=8.7.0, CXX_COMPILER=/opt/rh/devtoolset-9/root/usr/bin/c++, CXX_FLAGS= -D_GLIBCXX_USE_CXX11_ABI=0 -fabi-version=11 -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-stringop-overflow -Wsuggest-override -Wno-psabi -Wno-error=pedantic -Wno-error=old-style-cast -Wno-missing-braces -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=2.2.1, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=1, USE_NNPACK=ON, USE_OPENMP=ON, USE_ROCM=OFF, USE_ROCM_KERNEL_ASSERT=OFF,

TorchVision: 0.17.1+cu118
LMDeploy: 0.4.2+0b4660c
transformers: 4.40.0
gradio: Not Found
fastapi: 0.110.2
pydantic: 2.7.0
triton: 2.2.0
```

### Error traceback

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.