tensorflow/models · error · ValueError

The input size ( ) is not a multiple of the number of…

Error message

The input size (%d) is not a multiple of the number of attention heads (%d)

What it means

Divisibility check in TransformerEncoderBlockQuantized.build: the input width (last dimension) is split into attention heads, so it must be an exact multiple of num_attention_heads; otherwise head reshaping would fail and this ValueError fires.

Solutions

  1. Set the model width to a multiple of num_attention_heads (e.g. 128 per head)
  2. Adjust num_attention_heads to divide the input width
  3. Check for a config mismatch between embedding size and head count
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/qat/nlp/modeling/layers/transformer_encoder_block.py:160 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/6c4a5f5d00bf9cff. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/qat/nlp/modeling/layers/transformer_encoder_block.py:160

    else:
      self._attention_initializer = self._kernel_initializer
    self._attention_axes = attention_axes

  def build(self, input_shape):
    if isinstance(input_shape, tf.TensorShape):
      input_tensor_shape = input_shape
    elif isinstance(input_shape, (list, tuple)):
      input_tensor_shape = tf.TensorShape(input_shape[0])
    else:
      raise ValueError(
          "The type of input shape argument is not supported, got: %s" %
          type(input_shape))
    if len(input_tensor_shape.as_list()) != 3:
      raise ValueError("TransformerEncoderBlock expects a three-dimensional "
                       "input of shape [batch, sequence, width].")
    hidden_size = input_tensor_shape[-1]
    if hidden_size % self._num_heads != 0:
      raise ValueError(
          "The input size (%d) is not a multiple of the number of attention "
          "heads (%d)" % (hidden_size, self._num_heads))
    self._attention_head_size = int(hidden_size // self._num_heads)
    common_kwargs = dict(
        bias_initializer=self._bias_initializer,
        kernel_regularizer=self._kernel_regularizer,
        bias_regularizer=self._bias_regularizer,
        activity_regularizer=self._activity_regularizer,
        kernel_constraint=self._kernel_constraint,
        bias_constraint=self._bias_constraint)
    self._attention_layer = _quantized_multi_head_attention(
        num_heads=self._num_heads,
        key_dim=self._attention_head_size,
        dropout=self._attention_dropout,
        use_bias=self._use_bias,
        kernel_initializer=self._attention_initializer,
        attention_axes=self._attention_axes,
        name="self_attention",

View on GitHub (pinned to e006f5f0d5)