GPTQModel icon indicating copy to clipboard operation
GPTQModel copied to clipboard

Attempted to use GPTQ 8-bit quantization on the llama-2-7B-chat model, but encountered a `damp_percent` issue.

Open fousdfrf opened this issue 1 year ago • 9 comments

code:

from datasets import load_dataset from gptqmodel import GPTQModel, QuantizeConfig , BACKEND

model_id = "/data/public/model/llama2/new/Llama-2-7b-chat-hf" quant_path = "Llama-2-7B-Instruct-gptqmodel-8bit"

calibration_dataset = load_dataset( "/HOME/scw6anf/run/zlb/GPTQModel-main/c4", data_files="en/c4-train.00001-of-01024.json.gz", split="train" ).select(range(1024))["text"]

quant_config = QuantizeConfig(bits=8, group_size=128)

model = GPTQModel.load(model_id_or_path = model_id, quantize_config = quant_config , backend=BACKEND.TORCH)

model.quantize(calibration_dataset, batch_size=2)

model.save(quant_path)

model = GPTQModel.load(quant_path) result = model.generate("Uncovering deep insights begins with")[0] # tokens print(model.tokenizer.decode(result)) # string output

issue: Image

Image

Name: torch Version: 2.6.0 Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration Home-page: https://pytorch.org/ Author: PyTorch Team Author-email: [email protected] License: BSD-3-Clause Location: /data/run01/scw6anf/.conda/envs/zlb_gptqmodel/lib/python3.10/site-packages Requires: filelock, fsspec, jinja2, networkx, nvidia-cublas-cu12, nvidia-cuda-cupti-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-runtime-cu12, nvidia-cudnn-cu12, nvidia-cufft-cu12, nvidia-curand-cu12, nvidia-cusolver-cu12, nvidia-cusparse-cu12, nvidia-cusparselt-cu12, nvidia-nccl-cu12, nvidia-nvjitlink-cu12, nvidia-nvtx-cu12, sympy, triton, typing-extensions Required-by: accelerate, gptqmodel

Name: gptqmodel Version: 2.0.0 Summary: A LLM quantization package with user-friendly apis. Based on GPTQ algorithm. Home-page: https://github.com/ModelCloud/GPTQModel Author: ModelCloud Author-email: [email protected] License: Apache 2.0 Location: /data/run01/scw6anf/.conda/envs/zlb_gptqmodel/lib/python3.10/site-packages Requires: accelerate, datasets, device-smi, hf_transfer, huggingface_hub, logbar, numpy, packaging, pillow, protobuf, safetensors, threadpoolctl, tokenicer, torch, transformers Required-by:

fousdfrf avatar Mar 05 '25 07:03 fousdfrf

quant_config = QuantizeConfig(bits=4 , group_size=128)
Using 4-bit quantization will also encounter the same issue.

fousdfrf avatar Mar 05 '25 11:03 fousdfrf

quant_config = QuantizeConfig(bits=4 , group_size=128)
Using 4-bit quantization will also encounter the same issue.

This strange. We already auto fix 99.9% of all damp errors.

I highly suspect you have tokenization problem. Where did you download this model? check hf for updates

Qubitium avatar Mar 05 '25 12:03 Qubitium

if you can provide reproducible code with downloadable model and dataset we can replicate this and fix.

Qubitium avatar Mar 05 '25 13:03 Qubitium

model : https://www.modelscope.cn/models/shakechen/Llama-2-7b-chat-hf/files

installation command: pip install -r requirements.txt pip install -v . --no-build-isolation

cuda/12.4 cudnn/9.0.0-cuda12.x

code :


from datasets import load_dataset
from gptqmodel import GPTQModel, QuantizeConfig , BACKEND

model_id = "/data/public/model/llama2/new/Llama-2-7b-chat-hf"
quant_path = "Llama-2-7B-Instruct-gptqmodel-4bit"

calibration_dataset = load_dataset(
    "/HOME/scw6anf/run/zlb/GPTQModel-main/c4",
    data_files="en/c4-train.00001-of-01024.json.gz",
    split="train"
  ).select(range(1024))["text"]
print("calibration_dataset")
print(calibration_dataset)

quant_config = QuantizeConfig(bits=4, group_size=128)
#quant_config = QuantizeConfig(bits=8, group_size=128)

model = GPTQModel.load(model_id_or_path = model_id, quantize_config = quant_config , backend=BACKEND.TORCH)


model.quantize(calibration_dataset, batch_size=2)

model.save(quant_path)


model = GPTQModel.load(quant_path)
result = model.generate("Uncovering deep insights begins with")[0] 
print(model.tokenizer.decode(result))

fousdfrf avatar Mar 06 '25 02:03 fousdfrf

@fousdfrf

I just tested the following. I don't trust non-Chinese models hosted on modelscope due to data sync issues. You have no idea if the model configs are synced.

from datasets import load_dataset
from gptqmodel import GPTQModel, QuantizeConfig , BACKEND

model_id = " meta/Llama-2-7b-chat-hf"
quant_path = "Llama-2-7B-Instruct-gptqmodel-4bit"

calibration_dataset = load_dataset(
    "allenai/c4",
    data_files="en/c4-train.00001-of-01024.json.gz",
    split="train"
  ).select(range(512))["text"]

quant_config = QuantizeConfig(bits=4, group_size=128)

model = GPTQModel.load(model_id_or_path = model_id, quantize_config = quant_config)

model.quantize(calibration_dataset, batch_size=2)

model.save(quant_path)

model = GPTQModel.load(quant_path)
result = model.generate("The capital city of France is")[0]
print(model.tokenizer.decode(result))
INFO  {'process': 'gptq', 'layer': 30, 'module': 'self_attn.k_proj', 'loss': '20.25118', 'damp': '0.01000', 'time': '0.991', 'fwd_time': '3.460'}                                                       
INFO  {'process': 'gptq', 'layer': 30, 'module': 'self_attn.v_proj', 'loss': '18.44740', 'damp': '0.01000', 'time': '0.959', 'fwd_time': '3.460'}                                                       
INFO  {'process': 'gptq', 'layer': 30, 'module': 'self_attn.q_proj', 'loss': '21.94669', 'damp': '0.01000', 'time': '0.959', 'fwd_time': '3.460'}                                                       
INFO  {'process': 'gptq', 'layer': 30, 'module': 'self_attn.o_proj', 'loss': '1.36576', 'damp': '0.01000', 'time': '0.983', 'fwd_time': '2.090'}                                                        
INFO  {'process': 'gptq', 'layer': 30, 'module': 'mlp.up_proj', 'loss': '29.56236', 'damp': '0.01000', 'time': '1.001', 'fwd_time': '2.676'}                                                            
INFO  {'process': 'gptq', 'layer': 30, 'module': 'mlp.gate_proj', 'loss': '32.95630', 'damp': '0.01000', 'time': '0.988', 'fwd_time': '2.676'}                                                          
INFO  {'process': 'gptq', 'layer': 30, 'module': 'mlp.down_proj', 'loss': '3289.33643', 'damp': '0.01000', 'time': '2.904', 'fwd_time': '6.282'}                                                        
INFO  {'process': 'gptq', 'layer': 31, 'module': 'self_attn.k_proj', 'loss': '14.55291', 'damp': '0.01000', 'time': '0.981', 'fwd_time': '3.402'}                                                       
INFO  {'process': 'gptq', 'layer': 31, 'module': 'self_attn.v_proj', 'loss': '10.08940', 'damp': '0.01000', 'time': '0.908', 'fwd_time': '3.402'}                                                       
INFO  {'process': 'gptq', 'layer': 31, 'module': 'self_attn.q_proj', 'loss': '14.09182', 'damp': '0.01000', 'time': '0.931', 'fwd_time': '3.402'}                                                       
INFO  {'process': 'gptq', 'layer': 31, 'module': 'self_attn.o_proj', 'loss': '1.68695', 'damp': '0.01000', 'time': '0.934', 'fwd_time': '1.946'}                                                        
INFO  {'process': 'gptq', 'layer': 31, 'module': 'mlp.up_proj', 'loss': '24.62547', 'damp': '0.01000', 'time': '0.959', 'fwd_time': '2.676'}                                                            
INFO  {'process': 'gptq', 'layer': 31, 'module': 'mlp.gate_proj', 'loss': '27.62420', 'damp': '0.01000', 'time': '0.921', 'fwd_time': '2.676'}                                                          
INFO  {'process': 'gptq', 'layer': 31, 'module': 'mlp.down_proj', 'loss': '13.96576', 'damp': '0.01000', 'time': '2.756', 'fwd_time': '6.159'}                                                          
INFO  Packing model...                                                                                                                                                                                  
INFO  Kernel: Auto-selection: adding candidate `TritonV2QuantLinear`                                                                                                                                    
INFO  Kernel: Auto-selection: adding candidate `TorchQuantLinear`                                                                                                                                       
INFO  Kernel: candidates -> `[TritonV2QuantLinear, TorchQuantLinear]`                                                                                                                                   
INFO  Kernel: selected -> `TritonV2QuantLinear`.                                                                                                                                                        
INFO  Model packed.                                                                                                                                                                                     
INFO  Saved Quantize Config: 
{
  "bits": 4,
  "group_size": 128,
  "desc_act": true,
  "sym": true,
  "lm_head": false,
  "quant_method": "gptq",
  "checkpoint_format": "gptq",
  "pack_dtype": "int32",
  "meta": {
    "quantizer": [
      "gptqmodel:2.0.0"
    ],
    "uri": "https://github.com/modelcloud/gptqmodel",
    "damp_percent": 0.01,
    "damp_auto_increment": 0.0025,
    "static_groups": false,
    "true_sequential": true,
    "mse": 0.0
  }
}
Files in directory:
config.json
generation_config.json
quant_log.csv
quantize_config.json
Content of saved `generation_config.json`:
{
    "bos_token_id": 1,
    "do_sample": true,
    "eos_token_id": 2,
    "max_length": 4096,
    "pad_token_id": 0,
    "temperature": 0.6,
    "top_p": 0.9,
    "transformers_version": "4.49.0"
}
Content of saved `config.json`:
{
    "_name_or_path": "/monster/data/model/Llama-2-7b-chat-hf",
    "architectures": [
        "LlamaForCausalLM"
    ],
    "attention_bias": false,
    "attention_dropout": 0.0,
    "bos_token_id": 1,
    "eos_token_id": 2,
    "head_dim": 128,
    "hidden_act": "silu",
    "hidden_size": 4096,
    "initializer_range": 0.02,
    "intermediate_size": 11008,
    "max_position_embeddings": 4096,
    "mlp_bias": false,
    "model_type": "llama",
    "num_attention_heads": 32,
    "num_hidden_layers": 32,
    "num_key_value_heads": 32,
    "pretraining_tp": 1,
    "quantization_config": {
        "bits": 4,
        "checkpoint_format": "gptq",
        "desc_act": true,
        "group_size": 128,
        "lm_head": false,
        "meta": {
            "damp_auto_increment": 0.0025,
            "damp_percent": 0.01,
            "mse": 0.0,
            "quantizer": [
                "gptqmodel:2.0.0"
            ],
            "static_groups": false,
            "true_sequential": true,
            "uri": "https://github.com/modelcloud/gptqmodel"
        },
        "pack_dtype": "int32",
        "quant_method": "gptq",
        "sym": true
    },
    "rms_norm_eps": 1e-05,
    "rope_scaling": null,
    "rope_theta": 10000.0,
    "tie_word_embeddings": false,
    "torch_dtype": "float16",
    "transformers_version": "4.49.0",
    "use_cache": true,
    "vocab_size": 32000
}
INFO  Pre-Quantized model size: 25705.17MB, 25.10GB                                                                                                                                                     
INFO  Quantized model size: 3713.58MB, 3.63GB                                                                                                                                                           
INFO  Size difference: 21991.59MB, 21.48GB - 85.55%                                                                                                                                                     
from_quantized: adapter: None
INFO  Estimated Quantization BPW (bits per weight): 4.2875 bpw, based on [bits: 4, group_size: 128]                                                                                                     
INFO  Kernel: Auto-selection: adding candidate `MarlinQuantLinear`                                                                                                                                      
INFO  Kernel: Auto-selection: adding candidate `TritonV2QuantLinear`                                                                                                                                    
INFO  Kernel: Auto-selection: adding candidate `TorchQuantLinear`                                                                                                                                       
INFO  Kernel: candidates -> `[MarlinQuantLinear, TritonV2QuantLinear, TorchQuantLinear]`                                                                                                                
INFO  Kernel: selected -> `MarlinQuantLinear`.                                                                                                                                                          
INFO  Kernel: Auto-selection: adding candidate `MarlinQuantLinear`                                                                                                                                      
INFO  Model: Loaded `generation_config`: GenerationConfig {
  "bos_token_id": 1,
  "eos_token_id": 2
}
                                                                                                 
INFO  Model: Auto-fixed `generation_config` mismatch between model and `generation_config.json`.                                                                                                        
INFO  Model: Updated `generation_config`: GenerationConfig {
  "bos_token_id": 1,
  "do_sample": true,
  "eos_token_id": 2,
  "max_length": 4096,
  "pad_token_id": 0,
  "temperature": 0.6,
  "top_p": 0.9
}

INFO  Kernel: loaded -> `[MarlinQuantLinear]`   

<s> The capital city of France is Paris.
The currency of France is the Euro.
France is a member of the European Union.

damp errors are almost always caused by (1) insufficient or bad calibration dataset (2) tokenizer config

Qubitium avatar Mar 06 '25 03:03 Qubitium

You are correct. Changing the line of code from:

calibration_dataset = load_dataset(
    "/HOME/scw6anf/run/zlb/GPTQModel-main/c4",
    data_files="en/c4-train.00001-of-01024.json.gz",
    split="train"
).select(range(1024))["text"]

to

calibration_dataset = load_dataset(
    "/HOME/scw6anf/run/zlb/GPTQModel-main/c4",
    data_files="en/c4-train.00001-of-01024.json.gz",
    split="train"
).select(range(512))["text"]

allows for successful quantization.

fousdfrf avatar Mar 06 '25 05:03 fousdfrf

@fousdfrf Then it looks like dataset offset 513 to 1024 contains bad data or data that thus model does not like. I only changed to 512 to make it run faster for my test.

Qubitium avatar Mar 06 '25 05:03 Qubitium

Can the following loading method be used to load and run the int8 quantized model of llama-2-chat-7B on a 1050 GPU?

model = GPTQModel.load( quant_path, device_map="auto",
offload_folder="offload",
offload_state_dict=True )

fousdfrf avatar Mar 06 '25 07:03 fousdfrf

Can the following loading method be used to load and run the int8 quantized model of llama-2-chat-7B on a 1050 GPU?

model = GPTQModel.load( quant_path, device_map="auto",
offload_folder="offload",
offload_state_dict=True )

Not sure. offload has neve been tested by us. Due to latency issues, if offload, might as well just run model on pure cpu using IPEX kernel which may be faster than offloaded gpu model.

Qubitium avatar Mar 06 '25 09:03 Qubitium