Skip to content

Minor speedup for infer_quantization_format when save_compressed=False #1636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 45 additions & 48 deletions src/llmcompressor/transformers/compression/quantization_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from compressed_tensors import CompressionFormat
from compressed_tensors.config import SparsityStructure
from compressed_tensors.quantization import QuantizationStrategy, QuantizationType
from compressed_tensors.quantization.utils import (
is_model_quantized,
is_module_quantized,
)
from compressed_tensors.quantization.utils import is_module_quantized

__all__ = ["infer_quantization_format"]

Expand Down Expand Up @@ -47,57 +44,57 @@ def infer_quantization_format(
:param save_compressed: used to infer a quantization format if None is provided
:return compression format appropriate for model
"""
if not is_model_quantized(model):
return None

if quantization_format is not None:
return quantization_format

if save_compressed:
weight_args, input_args = _get_unique_quant_args(model)
is_24_structure = (
SparsityStructure(sparsity_structure) == SparsityStructure.TWO_FOUR
if not save_compressed:
# format will be inferred from config
return None

weight_args, input_args = _get_unique_quant_args(model)
if len(weight_args) <= 0:
return None

is_24_structure = (
SparsityStructure(sparsity_structure) == SparsityStructure.TWO_FOUR
)
is_weight_only = len(input_args) == 0 and len(weight_args) > 0

if (
weight_args[0].num_bits == 4
and weight_args[0].type == QuantizationType.FLOAT.value
):
return CompressionFormat.nvfp4_pack_quantized

if is_weight_only: # w4a16 and w8a16
is_valid_pack = all(
weight_arg.num_bits in [4, 8]
and weight_arg.type == QuantizationType.INT.value
for weight_arg in weight_args
)
is_weight_only = len(input_args) == 0 and len(weight_args) > 0

if (
weight_args[0].num_bits == 4
and weight_args[0].type == QuantizationType.FLOAT.value
):
return CompressionFormat.nvfp4_pack_quantized

if is_weight_only: # w4a16 and w8a16
is_valid_pack = all(
weight_arg.num_bits in [4, 8]
and weight_arg.type == QuantizationType.INT.value
for weight_arg in weight_args
)
if not is_valid_pack: # packing only valid for int4 and int 8
return CompressionFormat.naive_quantized
if is_24_structure:
for arg in weight_args:
if (
arg.strategy is not QuantizationStrategy.CHANNEL.value
and arg.strategy is not QuantizationStrategy.GROUP.value
):
# marlin24 kernel only applicable for channel/group quantization
return CompressionFormat.pack_quantized
return CompressionFormat.marlin_24
return CompressionFormat.pack_quantized
else: # w8a8 float and int
if len(weight_args) == 1:
if not is_valid_pack: # packing only valid for int4 and int 8
return CompressionFormat.naive_quantized
if is_24_structure:
for arg in weight_args:
if (
weight_args[0].type == QuantizationType.FLOAT.value
and weight_args[0].num_bits == 8
arg.strategy is not QuantizationStrategy.CHANNEL.value
and arg.strategy is not QuantizationStrategy.GROUP.value
):
return CompressionFormat.float_quantized
if weight_args[0].type == QuantizationType.INT.value:
return CompressionFormat.int_quantized
# marlin24 kernel only applicable for channel/group quantization
return CompressionFormat.pack_quantized
return CompressionFormat.marlin_24
return CompressionFormat.pack_quantized
else: # w8a8 float and int
if len(weight_args) == 1:
if (
weight_args[0].type == QuantizationType.FLOAT.value
and weight_args[0].num_bits == 8
):
return CompressionFormat.float_quantized
if weight_args[0].type == QuantizationType.INT.value:
return CompressionFormat.int_quantized

return CompressionFormat.naive_quantized
else:
# format will be inferred from config
return None
return CompressionFormat.naive_quantized


def _get_unique_quant_args(model):
Expand Down