tensorflow/models · error · ValueError

The width of the input tensor

Error message

The width of the input tensor {input_width} != hidden size {self.hidden_size}

What it means

Runtime shape check in the quantized MobileBERT transformer layer's call: the last dimension of the input tensor must equal the layer's hidden_size set at construction; a mismatched input_width (e.g. wrong embedding output or stacked layer) raises this ValueError.

Solutions

  1. Make the embedding/previous layer output width match the transformer's hidden_size
  2. Verify the config's hidden_size matches the checkpoint being restored
  3. Check that the input tensor is not transposed or reshaped incorrectly upstream
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/projects/qat/nlp/modeling/layers/mobile_bert_layers.py:421 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/b441ad5e4a99d089. Report an issue: GitHub.

Appendix: source

Thrown at official/projects/qat/nlp/modeling/layers/mobile_bert_layers.py:421

    Args:
      input_tensor: Float tensor of shape `(batch_size, seq_length,
        hidden_size)`.
      attention_mask: (optional) int32 tensor of shape `(batch_size, seq_length,
        seq_length)`, with 1 for positions that can be attended to and 0 in
        positions that should not be.
      return_attention_scores: If return attention score.

    Returns:
      layer_output: Float tensor of shape
        `(batch_size, seq_length, hidden_size)`.
      attention_scores (Optional): Only when return_attention_scores is True.

    Raises:
      ValueError: A Tensor shape or parameter is invalid.
    """
    input_width = input_tensor.shape.as_list()[-1]
    if input_width != self.hidden_size:
      raise ValueError(
          (f'The width of the input tensor {input_width} != '
           f'hidden size {self.hidden_size}'))

    prev_output = input_tensor
    # input bottleneck
    dense_layer = self.block_layers['bottleneck_input'][0]
    layer_norm = self.block_layers['bottleneck_input'][1]
    layer_input = dense_layer(prev_output)
    layer_input = layer_norm(layer_input)

    if self.use_bottleneck_attention:
      key_tensor = layer_input
      query_tensor = layer_input
      value_tensor = layer_input
    elif self.key_query_shared_bottleneck:
      dense_layer = self.block_layers['kq_shared_bottleneck'][0]
      layer_norm = self.block_layers['kq_shared_bottleneck'][1]
      shared_attention_input = dense_layer(prev_output)

View on GitHub (pinned to e006f5f0d5)