Quantization techniques reduces memory and computational costs by representing weights and activations with lower-precision data types like 8-bit integers (int8). This enables loading larger models you normally wouldn’t be able to fit into memory, and speeding up inference. Transformers supports the AWQ and GPTQ quantization algorithms and it supports 8-bit and 4-bit quantization with bitsandbytes.
Learn how to quantize models in the Quantization guide.
( bits: int = 4 group_size: int = 128 zero_point: bool = True version: AWQLinearVersion = <AWQLinearVersion.GEMM: 'gemm'> backend: AwqBackendPackingMethod = <AwqBackendPackingMethod.AUTOAWQ: 'autoawq'> do_fuse: typing.Optional[bool] = None fuse_max_seq_len: typing.Optional[int] = None modules_to_fuse: typing.Optional[dict] = None modules_to_not_convert: typing.Optional[typing.List] = None **kwargs )
Parameters
int
, optional, defaults to 4) —
The number of bits to quantize to. int
, optional, defaults to 128) —
The group size to use for quantization. Recommended value is 128 and -1 uses per-column quantization. bool
, optional, defaults to True
) —
Whether to use zero point quantization. AWQLinearVersion
, optional, defaults to AWQLinearVersion.GEMM
) —
The version of the quantization algorithm to use. GEMM is better for big batch_size (e.g. >= 8) otherwise,
GEMV is better (e.g. < 8 ) AwqBackendPackingMethod
, optional, defaults to AwqBackendPackingMethod.AUTOAWQ
) —
The quantization backend. Some models might be quantized using llm-awq
backend. This is useful for users
that quantize their own models using llm-awq
library. bool
, optional, defaults to False
) —
Whether to fuse attention and mlp layers together for faster inference int
, optional) —
The Maximum sequence length to generate when using fusing. dict
, optional, default to None
) —
Overwrite the natively supported fusing scheme with the one specified by the users. list
, optional, default to None
) —
The list of modules to not quantize, useful for quantizing models that explicitly require to have
some modules left in their original precision (e.g. Whisper encoder, Llava encoder, Mixtral gate layers).
Note you cannot quantize directly with transformers, please refer to AutoAWQ
documentation for quantizing HF models. This is a wrapper class about all possible attributes and features that you can play with a model that has been
loaded using auto-awq
library awq quantization relying on auto_awq backend.
Safety checker that arguments are correct
( bits: int tokenizer: typing.Any = None dataset: typing.Union[str, typing.List[str], NoneType] = None group_size: int = 128 damp_percent: float = 0.1 desc_act: bool = False sym: bool = True true_sequential: bool = True use_cuda_fp16: bool = False model_seqlen: typing.Optional[int] = None block_name_to_quantize: typing.Optional[str] = None module_name_preceding_first_block: typing.Optional[typing.List[str]] = None batch_size: int = 1 pad_token_id: typing.Optional[int] = None use_exllama: typing.Optional[bool] = None max_input_length: typing.Optional[int] = None exllama_config: typing.Union[typing.Dict[str, typing.Any], NoneType] = None cache_block_outputs: bool = True modules_in_block_to_quantize: typing.Optional[typing.List[typing.List[str]]] = None **kwargs )
Parameters
int
) —
The number of bits to quantize to, supported numbers are (2, 3, 4, 8). str
or PreTrainedTokenizerBase
, optional) —
The tokenizer used to process the dataset. You can pass either:bert-base-uncased
, or namespaced under a
user or organization name, like dbmdz/bert-base-german-cased
../my_model_directory/
.Union[List[str]]
, optional) —
The dataset used for quantization. You can provide your own dataset in a list of string or just use the
original datasets used in GPTQ paper [‘wikitext2’,‘c4’,‘c4-new’,‘ptb’,‘ptb-new’] int
, optional, defaults to 128) —
The group size to use for quantization. Recommended value is 128 and -1 uses per-column quantization. float
, optional, defaults to 0.1) —
The percent of the average Hessian diagonal to use for dampening. Recommended value is 0.1. bool
, optional, defaults to False
) —
Whether to quantize columns in order of decreasing activation size. Setting it to False can significantly
speed up inference but the perplexity may become slightly worse. Also known as act-order. bool
, optional, defaults to True
) —
Whether to use symetric quantization. bool
, optional, defaults to True
) —
Whether to perform sequential quantization even within a single Transformer block. Instead of quantizing
the entire block at once, we perform layer-wise quantization. As a result, each layer undergoes
quantization using inputs that have passed through the previously quantized layers. bool
, optional, defaults to False
) —
Whether or not to use optimized cuda kernel for fp16 model. Need to have model in fp16. int
, optional) —
The maximum sequence length that the model can take. str
, optional) —
The transformers block name to quantize. If None, we will infer the block name using common patterns (e.g. model.layers) List[str]
, optional) —
The layers that are preceding the first Transformer block. int
, optional, defaults to 1) —
The batch size used when processing the dataset int
, optional) —
The pad token id. Needed to prepare the dataset when batch_size
> 1. bool
, optional) —
Whether to use exllama backend. Defaults to True
if unset. Only works with bits
= 4. int
, optional) —
The maximum input length. This is needed to initialize a buffer that depends on the maximum expected input
length. It is specific to the exllama backend with act-order. Dict[str, Any]
, optional) —
The exllama config. You can specify the version of the exllama kernel through the version
key. Defaults
to {"version": 1}
if unset. bool
, optional, defaults to True
) —
Whether to cache block outputs to reuse as inputs for the succeeding block. List[List[str]]
, optional) —
List of list of module names to quantize in the specified block. This argument is useful to exclude certain linear modules from being quantized.
The block to quantize can be specified by setting block_name_to_quantize
. We will quantize each list sequentially. If not set, we will quantize all linear layers.
Example: modules_in_block_to_quantize =[["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"], ["self_attn.o_proj"]]
.
In this example, we will first quantize the q,k,v layers simultaneously since they are independent.
Then, we will quantize self_attn.o_proj
layer with the q,k,v layers quantized. This way, we will get
better results since it reflects the real input self_attn.o_proj
will get when the model is quantized. This is a wrapper class about all possible attributes and features that you can play with a model that has been
loaded using optimum
api for gptq quantization relying on auto_gptq backend.
Get compatible class with optimum gptq config dict
Safety checker that arguments are correct
Get compatible dict for optimum gptq config
( load_in_8bit = False load_in_4bit = False llm_int8_threshold = 6.0 llm_int8_skip_modules = None llm_int8_enable_fp32_cpu_offload = False llm_int8_has_fp16_weight = False bnb_4bit_compute_dtype = None bnb_4bit_quant_type = 'fp4' bnb_4bit_use_double_quant = False **kwargs )
Parameters
bool
, optional, defaults to False
) —
This flag is used to enable 8-bit quantization with LLM.int8(). bool
, optional, defaults to False
) —
This flag is used to enable 4-bit quantization by replacing the Linear layers with FP4/NF4 layers from
bitsandbytes
. float
, optional, defaults to 6.0) —
This corresponds to the outlier threshold for outlier detection as described in LLM.int8() : 8-bit Matrix Multiplication for Transformers at Scale
paper: https://arxiv.org/abs/2208.07339 Any hidden states value
that is above this threshold will be considered an outlier and the operation on those values will be done
in fp16. Values are usually normally distributed, that is, most values are in the range [-3.5, 3.5], but
there are some exceptional systematic outliers that are very differently distributed for large models.
These outliers are often in the interval [-60, -6] or [6, 60]. Int8 quantization works well for values of
magnitude ~5, but beyond that, there is a significant performance penalty. A good default threshold is 6,
but a lower threshold might be needed for more unstable models (small models, fine-tuning). List[str]
, optional) —
An explicit list of the modules that we do not want to convert in 8-bit. This is useful for models such as
Jukebox that has several heads in different places and not necessarily at the last position. For example
for CausalLM
models, the last lm_head
is kept in its original dtype
. bool
, optional, defaults to False
) —
This flag is used for advanced use cases and users that are aware of this feature. If you want to split
your model in different parts and run some parts in int8 on GPU and some parts in fp32 on CPU, you can use
this flag. This is useful for offloading large models such as google/flan-t5-xxl
. Note that the int8
operations will not be run on CPU. bool
, optional, defaults to False
) —
This flag runs LLM.int8() with 16-bit main weights. This is useful for fine-tuning as the weights do not
have to be converted back and forth for the backward pass. torch.dtype
or str, optional, defaults to torch.float32
) —
This sets the computational type which might be different than the input time. For example, inputs might be
fp32, but computation can be set to bf16 for speedups. str
, optional, defaults to "fp4"
) —
This sets the quantization data type in the bnb.nn.Linear4Bit layers. Options are FP4 and NF4 data types
which are specified by fp4
or nf4
. bool
, optional, defaults to False
) —
This flag is used for nested quantization where the quantization constants from the first quantization are
quantized again. Dict[str, Any]
, optional) —
Additional parameters from which to initialize the configuration object. This is a wrapper class about all possible attributes and features that you can play with a model that has been
loaded using bitsandbytes
.
This replaces load_in_8bit
or load_in_4bit
therefore both options are mutually exclusive.
Currently only supports LLM.int8()
, FP4
, and NF4
quantization. If more methods are added to bitsandbytes
,
then more arguments will be added to this class.
Returns True
if the model is quantizable, False
otherwise.
Safety checker that arguments are correct - also replaces some NoneType arguments with their default values.
This method returns the quantization method used for the model. If the model is not quantizable, it returns
None
.
( ) → Dict[str, Any]
Returns
Dict[str, Any]
Dictionary of all the attributes that make up this configuration instance,
Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary.