tensorflow/models · error · ValueError

Inconsistent shapes in shard_tensors: first is

Error message

Inconsistent shapes in shard_tensors: first is {tensors[0].shape} and other is {tensor.shape}

What it means

Raised in shard_tensors when input tensors have mismatched sizes along the sharded dimension; all tensors must share consistent shapes to be sharded together.

Solutions

  1. Shard only tensors that all have identical shapes.
  2. Pad or trim the tensors so every shard tensor has the same shape before calling shard_tensors.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/vision/modeling/layers/edgetpu.py:125 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/b467127590001219. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/modeling/layers/edgetpu.py:125

  Args:
    axis: axis to be used to split tensors
    block_size: block size to split tensors.
    tensors: list of tensors.

  Returns:
    List of shards, each shard has exactly one peace of each input tesnor.

  Raises:
    ValueError: if input tensors has different size of sharded dimension.
  """
  if not all(tensor.shape.is_fully_defined() for tensor in tensors):
    return [tensors]
  for validate_axis in range(axis + 1):
    consistent_length: int = tensors[0].shape[validate_axis]
    for tensor in tensors:
      if tensor.shape[validate_axis] != consistent_length:
        raise ValueError('Inconsistent shapes in shard_tensors: first is '
                         f'{tensors[0].shape} and other is {tensor.shape}')
  batch_size: int = tensors[0].shape[axis]
  if block_size >= batch_size:
    return [tensors]
  else:
    blocks = batch_size // block_size
    remainder = batch_size % block_size
    if remainder:
      tensor_parts = []
      for tensor in tensors:
        shape: tf.TensorShape = tensor.shape
        body: tf.Tensor = tf.slice(tensor, [0] * len(shape), [
            size if i != axis else blocks * block_size
            for i, size in enumerate(shape)
        ])
        tail: tf.Tensor = tf.slice(tensor, [
            0 if i != axis else (blocks * block_size)
            for i, _ in enumerate(shape)

View on GitHub (pinned to e006f5f0d5)