bridge.training.post_training.lsq#

Scale learning (LSQ) for ModelOpt-quantized models.

ModelOpt’s LSQ algorithm makes the per-block amax of static-block weight quantizers learnable (_amax_pre / _amax_post). This module gives expert scales the expert reduction group, an optional optimizer parameter group for the scales, and a pre-wrap hook for scale-only learning.

Module Contents#

Classes#

LSQOptimizerConfigOverrideProvider

Put ModelOpt LSQ scale parameters in their own optimizer parameter group.

Functions#

propagate_expert_allreduce_to_lsq_params

Copy each quantized weight’s allreduce attribute onto its LSQ scale parameters.

_check_modelopt_lsq_gradients

Raise if the installed ModelOpt leaves some LSQ scales without a gradient in Megatron models.

freeze_all_but_lsq_scales

Pre-wrap hook for scale-only learning: freeze every parameter except the LSQ scales.

_recompute_needs_input_grad

Whether a transformer block must take a grad-requiring input when it runs.

Data#

LSQ_SCALE_PARAM_PATTERNS

Name patterns (fnmatch) of the ModelOpt LSQ scale parameters.

_LSQ_SCALE_PARAM_KEY

API#

bridge.training.post_training.lsq.LSQ_SCALE_PARAM_PATTERNS#

(’_amax_pre’, ‘_amax_post’)

Name patterns (fnmatch) of the ModelOpt LSQ scale parameters.

bridge.training.post_training.lsq._LSQ_SCALE_PARAM_KEY#

‘ParamKey(…)’

bridge.training.post_training.lsq.propagate_expert_allreduce_to_lsq_params(
model: torch.nn.Module,
) → int#

Copy each quantized weight’s allreduce attribute onto its LSQ scale parameters.

Megatron-Core reduces and shards every parameter without allreduce=False over the data-parallel group, so expert scales need the flag of their expert weight. Run after the ModelOpt state is restored and before DDP wrapping.

Parameters:

model – Unwrapped model chunk.

Returns:

Number of scale parameters updated.

bridge.training.post_training.lsq._check_modelopt_lsq_gradients() → None#

Raise if the installed ModelOpt leaves some LSQ scales without a gradient in Megatron models.

Without NVIDIA/Model-Optimizer#3069, the scales of Transformer Engine LayerNormLinear layers, and of linear layers whose weight is frozen, get no or an all-zero gradient, so training leaves them unchanged.

bridge.training.post_training.lsq.freeze_all_but_lsq_scales(
model: list[megatron.core.transformer.module.MegatronModule],
) → list[megatron.core.transformer.module.MegatronModule]#

Pre-wrap hook for scale-only learning: freeze every parameter except the LSQ scales.

Frozen weights take no DDP gradient buffer or optimizer state, and MoE router expert biases stop updating. The hook may run before or after the ModelOpt state is restored; the scales the restore creates stay trainable. Gradients through frozen quantized weights need a ModelOpt version that computes them.

Parameters:

model – Unwrapped model chunks.

Returns:

The same model chunks.

Raises:
  • RuntimeError – If the installed ModelOpt cannot compute the scale gradients.

  • ValueError – If the restored ModelOpt state has no LSQ scale parameters.

bridge.training.post_training.lsq._recompute_needs_input_grad(block: torch.nn.Module) → bool#

Whether a transformer block must take a grad-requiring input when it runs.

Megatron-Core’s reentrant activation recompute skips a chunk’s backward when no input requires grad, as for the first block once the embedding is frozen. Trainability is checked when the block runs, since the scales may be created after this hook; a distillation teacher’s blocks run unchanged. Later pipeline stages pass None and inference may pass a WrappedTensor; the patched forward leaves both unchanged.

class bridge.training.post_training.lsq.LSQOptimizerConfigOverrideProvider#

Bases: megatron.bridge.training.config.OptimizerConfigOverrideProvider

Put ModelOpt LSQ scale parameters in their own optimizer parameter group.

The group follows the base learning-rate schedule with its own peak and minimum learning rate. Only settings that differ from the base config are overridden: a scale group identical to the base group would be conflated with it when optimizer state is loaded from a checkpoint.

Parameters:
  • scale_lr – Peak learning rate of the scales.

  • scale_min_lr – Minimum learning rate of the scales; None uses optimizer.min_lr.

  • scale_wd_mult – Weight-decay multiplier of the scales; 0.0 keeps weight decay from pulling them toward zero.

scale_lr: float#

None

scale_min_lr: float | None#

None

scale_wd_mult: float#

None

build_config_overrides(
context: megatron.bridge.training.config.OptimizerConfigOverrideProviderContext,
) → dict[megatron.core.optimizer.ParamKey, megatron.core.optimizer_param_scheduler.ParamGroupOverride] | None#

Standard overrides plus a parameter group for the LSQ scales.

Parameters:

context – Scheduler, optimizer, and model context for override construction.

Returns:

Mapping from ParamKey matchers to per-group optimizer overrides, or None.

Raises:

RuntimeError – If the model has LSQ scales and the installed ModelOpt cannot compute their gradients.