Skip to navigation

nemo_automodel.components.quantization.qat

View as MarkdownOpen in Claude

TorchAO Quantization-Aware Training (QAT) helpers for NeMo-AutoModel.

This module provides:

  • QATConfig: Configuration class for QAT settings
  • Thin wrappers to instantiate and apply torchao QAT quantizers to models (prepare)
  • Toggle fake-quant on/off during training (for delayed fake-quant)

Module Contents

Classes

NameDescription
QATConfigConfiguration for Quantization-Aware Training (QAT).
_QATConfigDict-
_QATConfigRequired-
_QATQuantizer-
_QATQuantizerKwargs-

Functions

NameDescription
get_disable_fake_quant_fnReturn the disable fake-quant function for a given quantizer mode.
get_enable_fake_quant_fnReturn the enable fake-quant function for a given quantizer mode.
get_quantizer_modeReturn a short mode string for a known torchao QAT quantizer.
prepare_qat_modelApply a torchao QAT quantizer to the given model.

Data

QATQuantizerType

_DISABLE_FN_BY_MODE

_ENABLE_FN_BY_MODE

_QAT_QUANTIZER_KWARG_KEYS

_QUANTIZER_TO_MODE

logger

API

class nemo_automodel.components.quantization.qat.QATConfig(
quantizer_type: nemo_automodel.components.quantization.qat.QATQuantizerType = 'int8_dynact_int4weight',
)
Dataclass

Configuration for Quantization-Aware Training (QAT).

This config controls how QAT quantizers are instantiated and applied to models. QAT is enabled when this config is provided to from_pretrained/from_config.

**quantizer_kwargs
Literal['int8_dynact_int4weight', 'int4_weight_only']

Additional keyword arguments forwarded directly to the torchao quantizer constructor (e.g. groupsize, padding_allowed, inner_k_tiles).

nemo_automodel.components.quantization.qat.QATConfig.create_quantizer() -> torchao.quantization.qat.Int8DynActInt4WeightQATQuantizer | torchao.quantization.qat.Int4WeightOnlyQATQuantizer

Create and return the appropriate QAT quantizer based on config.

Returns: Int8DynActInt4WeightQATQuantizer | Int4WeightOnlyQATQuantizer

A torchao QAT quantizer instance (Int8DynActInt4WeightQATQuantizer

Raises:

  • ValueError: If quantizer_type or quantizer_kwargs are not recognized.
nemo_automodel.components.quantization.qat.QATConfig.to_dict() -> nemo_automodel.components.quantization.qat._QATConfigDict

Convert config to dictionary.

class nemo_automodel.components.quantization.qat._QATConfigDict()
class nemo_automodel.components.quantization.qat._QATConfigRequired

Bases: typing.TypedDict

quantizer_type
QATQuantizerType
class nemo_automodel.components.quantization.qat._QATQuantizer()
Protocol
nemo_automodel.components.quantization.qat._QATQuantizer.prepare(
model: torch.nn.Module
) -> torch.nn.Module
class nemo_automodel.components.quantization.qat._QATQuantizerKwargs

Bases: typing.TypedDict

groupsize
int
inner_k_tiles
int | None
padding_allowed
bool
precision
dtype
scales_precision
dtype
nemo_automodel.components.quantization.qat.get_disable_fake_quant_fn(
mode: str
) -> typing.Callable[[nn.Module], None] | None

Return the disable fake-quant function for a given quantizer mode.

nemo_automodel.components.quantization.qat.get_enable_fake_quant_fn(
mode: str
) -> typing.Callable[[nn.Module], None] | None

Return the enable fake-quant function for a given quantizer mode.

nemo_automodel.components.quantization.qat.get_quantizer_mode(
quantizer: object
) -> str | None

Return a short mode string for a known torchao QAT quantizer.

Returns None when the quantizer is unrecognized.

nemo_automodel.components.quantization.qat.prepare_qat_model(
model: torch.nn.Module,
) -> tuple[torch.nn.Module, str | None]

Apply a torchao QAT quantizer to the given model.

Returns the (possibly wrapped) model and a mode string if recognized.

nemo_automodel.components.quantization.qat.QATQuantizerType = Literal['int8_dynact_int4weight', 'int4_weight_only']
nemo_automodel.components.quantization.qat._DISABLE_FN_BY_MODE: dict[str, Callable[[nn.Module], None]] = {'8da4w-qat': disable_8da4w_fake_quant, '4w-qat': disable_4w_fake_quant}
nemo_automodel.components.quantization.qat._ENABLE_FN_BY_MODE: dict[str, Callable[[nn.Module], None]] = {'8da4w-qat': enable_8da4w_fake_quant, '4w-qat': enable_4w_fake_quant}
nemo_automodel.components.quantization.qat._QAT_QUANTIZER_KWARG_KEYS = frozenset({'groupsize', 'padding_allowed', 'inner_k_tiles', 'precision', 'scales...
nemo_automodel.components.quantization.qat._QUANTIZER_TO_MODE: dict[type, str] = {Int8DynActInt4WeightQATQuantizer: '8da4w-qat', Int4WeightOnlyQATQuantizer: '4w-...
nemo_automodel.components.quantization.qat.logger = logging.getLogger(__name__)