0cc4m/GPTQ-for-LLaMa

4 bits quantization of LLMs using GPTQ

Python

48

342 commits

updated Jul 26, 2023

See the code

README

GPTQ-for-LLaMA

4 bits quantization of LLaMA using GPTQ

GPTQ is SOTA one-shot weight quantization method

This code is based on GPTQ

This version has been created and tested for use with KoboldAI

New Features

  • Optimized CPU Offloading
  • Optimized GPU Splitting
  • Backwards Compatibility with older GPTQ-models

Currently, groupsize and act-order do not work together and you must choose one of them.

Result

LLaMA-7B(click me)
LLaMA-7BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-139405.6812.5
RTN4--6.29-
GPTQ4-47406.093.5
RTN3--25.54-
GPTQ3-38528.072.7
GPTQ312841166.613.0
LLaMA-13B
LLaMA-13BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM5.0924.2
RTN4--5.53-
GPTQ4-84105.366.5
RTN3--11.40-
GPTQ3-68706.635.1
GPTQ312872775.625.4
LLaMA-33B
LLaMa-33BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM4.1060.5
RTN4--4.54-
GPTQ4-194934.4515.7
RTN3--14.89-
GPTQ3-154935.6912.0
GPTQ3128165664.8013.0
LLaMA-65B
LLaMA-65BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM3.53121.0
RTN4--3.92-
GPTQ4-OOM3.8431.1
RTN3--10.59-
GPTQ3-OOM5.0423.6
GPTQ3128OOM4.1725.6

Quantization requires a large amount of CPU memory. However, the memory required can be reduced by using swap memory.

Depending on the GPUs/drivers, there may be a difference in performance, which decreases as the model size increases.(https://github.com/IST-DASLab/gptq/issues/1)

According to GPTQ paper, As the size of the model increases, the difference in performance between FP16 and GPTQ decreases.

Installation

pip install git+https://github.com/0cc4m/GPTQ-for-LLaMa@c884b421a233f9603d8224c9b22c2d83dd2c1fc4

old instructions:

If you don't have conda, install it first.

  conda create --name gptq python=3.9 -y
  conda activate gptq
  conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
  # Or, if you're having trouble with conda, use pip with python3.9:
  # pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

  git clone https://github.com/0cc4m/GPTQ-for-LLaMa
  cd GPTQ-for-LLaMa
  pip install -r requirements.txt
  python setup_cuda.py install

  # Benchmark performance for FC2 layer of LLaMa-7B
  CUDA_VISIBLE_DEVICES=0 python test_kernel.py
  

Dependencies

All experiments were run on a single NVIDIA RTX3090.

Language Generation

LLaMA

The format for using this version of GPTQ has changed from specifying python files, to specifying the module name.

Old CommandNew Way
python llama.pypython -m gptq.llama
python gptj.pypython -m gptq.gptj
python opt.pypython -m gptq.opt
python gptneox.pypython -m gptq.gptneox
python llama_inference.pypython -m gptq.llama_inference
python llama_inference_offload.pypython -m gptq.llama_inference_offload
python convert_llama_weights_to_hf.pypython -m gptq. convert_llama_weights_to_hf
#convert LLaMA to hf
python -m gptq.convert_llama_weights_to_hf --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir ./llama-hf

# Benchmark language generation with 4-bit LLaMA-7B:

# Save compressed model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --true-sequential --act-order --save llama7b-4bit.pt
# Or save compressed `.safetensors` model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --true-sequential --act-order --save_safetensors llama7b-4bit.safetensors
# Benchmark generating a 2048 token sequence with the saved model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --load llama7b-4bit.pt --benchmark 2048 --check
# Benchmark FP16 baseline, note that the model will be split across all listed GPUs
CUDA_VISIBLE_DEVICES=0,1,2,3,4 python -m gptq.llama ./llama-hf/llama-7b c4 --benchmark 2048 --check

# model inference with the saved model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama_inference ./llama-hf/llama-7b --wbits 4 --load llama7b-4bit.pt --text "this is llama"
# model inference with the saved model with offload(This is very slow. This is a simple implementation and could be improved with technologies like flexgen(https://github.com/FMInference/FlexGen).
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama_inference_offload ./llama-hf/llama-7b --wbits 4 --load llama7b-4bit.pt --text "this is llama" --pre_layer 16
It takes about 180 seconds to generate 45 tokens(5->50 tokens) on single RTX3090 based on LLaMa-65B. pre_layer is set to 50.

CUDA Kernels support 2,3,4,8 bits and Faster CUDA Kernels support 2,3,4 bits.

Basically, 4-bit quantization and 128 groupsize are recommended.

Acknowledgements

This code is based on GPTQ

Thanks to Meta AI for releasing LLaMA, a powerful LLM.

0cc4m/GPTQ-for-LLaMa

4 bits quantization of LLMs using GPTQ

Python

48

342 commits

updated Jul 26, 2023

See the code

README

GPTQ-for-LLaMA

4 bits quantization of LLaMA using GPTQ

GPTQ is SOTA one-shot weight quantization method

This code is based on GPTQ

This version has been created and tested for use with KoboldAI

New Features

  • Optimized CPU Offloading
  • Optimized GPU Splitting
  • Backwards Compatibility with older GPTQ-models

Currently, groupsize and act-order do not work together and you must choose one of them.

Result

LLaMA-7B(click me)
LLaMA-7BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-139405.6812.5
RTN4--6.29-
GPTQ4-47406.093.5
RTN3--25.54-
GPTQ3-38528.072.7
GPTQ312841166.613.0
LLaMA-13B
LLaMA-13BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM5.0924.2
RTN4--5.53-
GPTQ4-84105.366.5
RTN3--11.40-
GPTQ3-68706.635.1
GPTQ312872775.625.4
LLaMA-33B
LLaMa-33BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM4.1060.5
RTN4--4.54-
GPTQ4-194934.4515.7
RTN3--14.89-
GPTQ3-154935.6912.0
GPTQ3128165664.8013.0
LLaMA-65B
LLaMA-65BBitsgroup-sizememory(MiB)Wikitext2checkpoint size(GB)
FP1616-OOM3.53121.0
RTN4--3.92-
GPTQ4-OOM3.8431.1
RTN3--10.59-
GPTQ3-OOM5.0423.6
GPTQ3128OOM4.1725.6

Quantization requires a large amount of CPU memory. However, the memory required can be reduced by using swap memory.

Depending on the GPUs/drivers, there may be a difference in performance, which decreases as the model size increases.(https://github.com/IST-DASLab/gptq/issues/1)

According to GPTQ paper, As the size of the model increases, the difference in performance between FP16 and GPTQ decreases.

Installation

pip install git+https://github.com/0cc4m/GPTQ-for-LLaMa@c884b421a233f9603d8224c9b22c2d83dd2c1fc4

old instructions:

If you don't have conda, install it first.

  conda create --name gptq python=3.9 -y
  conda activate gptq
  conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
  # Or, if you're having trouble with conda, use pip with python3.9:
  # pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

  git clone https://github.com/0cc4m/GPTQ-for-LLaMa
  cd GPTQ-for-LLaMa
  pip install -r requirements.txt
  python setup_cuda.py install

  # Benchmark performance for FC2 layer of LLaMa-7B
  CUDA_VISIBLE_DEVICES=0 python test_kernel.py
  

Dependencies

All experiments were run on a single NVIDIA RTX3090.

Language Generation

LLaMA

The format for using this version of GPTQ has changed from specifying python files, to specifying the module name.

Old CommandNew Way
python llama.pypython -m gptq.llama
python gptj.pypython -m gptq.gptj
python opt.pypython -m gptq.opt
python gptneox.pypython -m gptq.gptneox
python llama_inference.pypython -m gptq.llama_inference
python llama_inference_offload.pypython -m gptq.llama_inference_offload
python convert_llama_weights_to_hf.pypython -m gptq. convert_llama_weights_to_hf
#convert LLaMA to hf
python -m gptq.convert_llama_weights_to_hf --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir ./llama-hf

# Benchmark language generation with 4-bit LLaMA-7B:

# Save compressed model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --true-sequential --act-order --save llama7b-4bit.pt
# Or save compressed `.safetensors` model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --true-sequential --act-order --save_safetensors llama7b-4bit.safetensors
# Benchmark generating a 2048 token sequence with the saved model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama ./llama-hf/llama-7b c4 --wbits 4 --load llama7b-4bit.pt --benchmark 2048 --check
# Benchmark FP16 baseline, note that the model will be split across all listed GPUs
CUDA_VISIBLE_DEVICES=0,1,2,3,4 python -m gptq.llama ./llama-hf/llama-7b c4 --benchmark 2048 --check

# model inference with the saved model
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama_inference ./llama-hf/llama-7b --wbits 4 --load llama7b-4bit.pt --text "this is llama"
# model inference with the saved model with offload(This is very slow. This is a simple implementation and could be improved with technologies like flexgen(https://github.com/FMInference/FlexGen).
CUDA_VISIBLE_DEVICES=0 python -m gptq.llama_inference_offload ./llama-hf/llama-7b --wbits 4 --load llama7b-4bit.pt --text "this is llama" --pre_layer 16
It takes about 180 seconds to generate 45 tokens(5->50 tokens) on single RTX3090 based on LLaMa-65B. pre_layer is set to 50.

CUDA Kernels support 2,3,4,8 bits and Faster CUDA Kernels support 2,3,4 bits.

Basically, 4-bit quantization and 128 groupsize are recommended.

Acknowledgements

This code is based on GPTQ

Thanks to Meta AI for releasing LLaMA, a powerful LLM.

Significant stargazers

nate.river

327 followers · starred Feb 2025