Skip to content
Merged
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
88 changes: 56 additions & 32 deletions src/nncf/quantization/algorithms/weight_compression/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@
TensorDataType.f8e4m3,
TensorDataType.f8e5m2,
]
FIXED_GROUP_SIZE_MODES = {
CompressWeightsMode.MXFP4: 32,
CompressWeightsMode.MXFP8_E4M3: 32,
CompressWeightsMode.NVFP4: 16,
}
PER_CHANNEL_BY_DEFAULT_MODES = [
*INT8_MODES,
CompressWeightsMode.CODEBOOK,
CompressWeightsMode.CB4,
CompressWeightsMode.ADAPTIVE_CODEBOOK,
]
DEFAULT_GROUP_SIZE_FOR_NON_INT8_MODES = 128


def get_default_group_size(mode: CompressWeightsMode) -> int:
"""
Returns the group size to use for the given mode when no group size is requested explicitly.

:param mode: Compression mode to get the default group size for.
:return: Default group size for the given mode.
"""
if mode in FIXED_GROUP_SIZE_MODES:
return FIXED_GROUP_SIZE_MODES[mode]
if mode in PER_CHANNEL_BY_DEFAULT_MODES:
return -1
return DEFAULT_GROUP_SIZE_FOR_NON_INT8_MODES


def get_weight_compression_configuration(
Expand All @@ -90,22 +116,8 @@ def get_weight_compression_configuration(
"""
Generates a configuration dictionary for weight compression based on the provided parameters.
"""
if group_size is None and mode in INT8_MODES:
group_size = -1
elif group_size is None and mode in NON_INT8_MODES:
if mode in [CompressWeightsMode.MXFP4, CompressWeightsMode.MXFP8_E4M3]:
group_size = 32
elif mode == CompressWeightsMode.NVFP4:
group_size = 16
elif mode in [
CompressWeightsMode.CODEBOOK,
CompressWeightsMode.CB4,
CompressWeightsMode.ADAPTIVE_CODEBOOK,
]:
group_size = -1
else:
group_size = 128

if group_size is None:
group_size = get_default_group_size(mode)
if backup_mode is None:
if mode in [CompressWeightsMode.MXFP4, CompressWeightsMode.MXFP8_E4M3]:
backup_mode = BackupMode.MXFP8_E4M3
Expand Down Expand Up @@ -138,6 +150,33 @@ def get_weight_compression_configuration(
}


def validate_fixed_group_size(
mode: CompressWeightsMode, group_size: int | None, advanced_parameters: AdvancedCompressionParameters | None = None
) -> None:
"""
Validates the group size requested for a mode which accepts only specific group sizes.

:param mode: Compression mode to validate.
:param group_size: Group size requested for the given mode. None means that the default value is used.
:param advanced_parameters: Advanced compression parameters that may affect the validation.
"""
fixed_group_size = FIXED_GROUP_SIZE_MODES.get(mode)
if fixed_group_size is not None and group_size not in [None, fixed_group_size]:
msg = f"{mode.value} type only supports group size of {fixed_group_size}, group size of {group_size} is given"
raise nncf.ValidationError(msg)
if (
fixed_group_size is not None
and advanced_parameters
and advanced_parameters.group_size_fallback_mode is GroupSizeFallbackMode.ADJUST
):
msg = (
f"{mode.value} type only supports group size of {fixed_group_size}, so it does not support the group size"
f" fallback mode {advanced_parameters.group_size_fallback_mode.value}."
" Please use other group size fallback mode."
)
raise nncf.ValidationError(msg)


def check_user_compression_configuration(
mode: CompressWeightsMode,
subset_size: int,
Expand Down Expand Up @@ -278,22 +317,7 @@ def check_user_compression_configuration(
f"Supported modes are: {[e.value for e in GroupSizeFallbackMode]}."
)
raise nncf.ValidationError(msg)
if mode in [CompressWeightsMode.MXFP4, CompressWeightsMode.MXFP8_E4M3, CompressWeightsMode.NVFP4]:
if mode in [CompressWeightsMode.MXFP4, CompressWeightsMode.MXFP8_E4M3] and group_size not in [None, 32]:
msg = f"MXFP4 and MXFP8_E4M3 types only support group size of 32, group size of {group_size} is given"
raise nncf.ValidationError(msg)

if mode == CompressWeightsMode.NVFP4 and group_size not in [None, 16]:
msg = f"NVFP4 type only supports group size of 16, group size of {group_size} is given"
raise nncf.ValidationError(msg)

if advanced_parameters and advanced_parameters.group_size_fallback_mode is GroupSizeFallbackMode.ADJUST:
msg = (
"MXFP4, MXFP8_E4M3 and NVFP4 types do not support the group size"
f" fallback mode {advanced_parameters.group_size_fallback_mode.value}."
" Please use other group size fallback mode."
)
raise nncf.ValidationError(msg)
validate_fixed_group_size(mode, group_size, advanced_parameters)


class WeightCompression(Algorithm):
Expand Down
Loading