keras-team/keras · error · ValueError

Invalid tensor type: {tensor_type}

Error message

Invalid tensor type: {tensor_type}

What it means

_adjust_scale_for_quant in EinsumDense accepts only tensor_type 'kernel' or 'input' when reshaping a quantization scale. Any other string reaches the else branch and raises ValueError. This is an internal helper, so hitting it usually means a subclass or custom quantization path passed an unsupported type.

Source

Thrown at keras/src/layers/core/einsum_dense.py:1672

        This is the forward order of operations used when building the layer.

        Args:
            scale: The scale tensor to adjust.
            tensor_type: The type of tensor to adjust the scale for.
                "kernel" or "input".
        Returns:
            The adjusted scale tensor.
        """
        if tensor_type == "kernel":
            transpose_axes = self._kernel_transpose_axes
            expand_axes = self._kernel_expand_axes
            squeeze_axes = self._kernel_squeeze_axes
        elif tensor_type == "input":
            transpose_axes = self._input_transpose_axes
            expand_axes = self._input_expand_axes
            squeeze_axes = self._input_squeeze_axes
        else:
            raise ValueError(f"Invalid tensor type: {tensor_type}")

        if transpose_axes:
            scale = ops.transpose(scale, transpose_axes)
        if expand_axes:
            scale = ops.expand_dims(scale, axis=expand_axes)
        if squeeze_axes:
            scale = ops.squeeze(scale, axis=squeeze_axes)
        return scale

    def _set_quantization_info(self):
        if hasattr(self, "_input_reduced_axes"):
            # Already set.
            return
        (
            self._input_reduced_axes,
            self._kernel_reduced_axes,
            self._input_transpose_axes,
            self._kernel_transpose_axes,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass exactly 'kernel' or 'input' as tensor_type
  2. Check the two branches above the raise to see accepted values
  3. If a new tensor kind is genuinely needed, extend the if/elif chain before the else branch

Example fix

# before
_scale = layer._adjust_scale_for_quant(scale, 'weight')
# after
_scale = layer._adjust_scale_for_quant(scale, 'kernel')
Defensive patterns

Strategy: validation

Validate before calling

TENSOR_TYPES = {'kernel', 'input'}
assert tensor_type in TENSOR_TYPES

Type guard

def is_valid_tensor_type(t):
    return t in ('kernel', 'input')

Prevention

When it happens

Trigger: Calling einsum_with_inputs_gradient, einsum_per_channel_with_inputs_gradient, quantize, or _get_kernel_with_merged_lora after the tensor_type argument was changed to a value other than 'kernel'/'input' (e.g. 'weight', 'bias', None) in a subclass override.

Common situations: Custom EinsumDense subclasses or custom quantizers that override quantization helpers and pass a renamed tensor type; version upgrades that renamed 'kernel' to 'weight' without updating call sites.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/07001b7a937d1583. Report an issue: GitHub.