tensorflow/models · error · ValueError

`set_quantize_weights` called on layer

Error message

`set_quantize_weights` called on layer {} with {} weight parameters, but layer expects {} values.

What it means

Cardinality check in set_quantize_weights (default 8-bit QuantizeConfig): the number of supplied quantize weight tensors must equal the number of weight attributes the config declares (weight_attrs); a mismatch means the tfmot wrapper and config disagree on how many weights to quantize, so this ValueError fires.

Solutions

  1. Ensure weight_attrs lists exactly the weights the layer exposes for quantization
  2. Check that the QuantizeConfig matches the layer it annotates
  3. Update the config's weight_attrs when the layer definition changes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/qat/nlp/quantization/configs.py:334 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/efdcf8f404cac2ae. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/qat/nlp/quantization/configs.py:334

    # TODO(pulkitb): For some layers such as Conv2D, per_axis should be True.
    # Add mapping for which layers support per_axis.
    self.weight_quantizer = LastValueQuantizer(
        num_bits=8, per_axis=False, symmetric=True, narrow_range=True)
    self.activation_quantizer = MovingAverageQuantizer(
        num_bits=8, per_axis=False, symmetric=False, narrow_range=False)

  def get_weights_and_quantizers(self, layer):
    return [(getattr(layer, weight_attr), self.weight_quantizer)
            for weight_attr in self.weight_attrs]

  def get_activations_and_quantizers(self, layer):
    return [(getattr(layer, activation_attr), self.activation_quantizer)
            for activation_attr in self.activation_attrs]

  def set_quantize_weights(self, layer, quantize_weights):
    if len(self.weight_attrs) != len(quantize_weights):
      raise ValueError(
          '`set_quantize_weights` called on layer {} with {} '
          'weight parameters, but layer expects {} values.'.format(
              layer.name, len(quantize_weights), len(self.weight_attrs)))

    for weight_attr, weight in zip(self.weight_attrs, quantize_weights):
      current_weight = getattr(layer, weight_attr)
      if current_weight.shape != weight.shape:
        raise ValueError('Existing layer weight shape {} is incompatible with'
                         'provided weight shape {}'.format(
                             current_weight.shape, weight.shape))

      setattr(layer, weight_attr, weight)

  def set_quantize_activations(self, layer, quantize_activations):
    if len(self.activation_attrs) != len(quantize_activations):
      raise ValueError(
          '`set_quantize_activations` called on layer {} with {} '
          'activation parameters, but layer expects {} values.'.format(

View on GitHub (pinned to e006f5f0d5)