xai-org/x-algorithm · error · ValueError

Input must not be scalar.

Error message

Input must not be scalar.

What it means

Linear.__call__ requires inputs to have at least one dimension because it reads inputs.shape[-1] to infer input_size. A scalar (shape ()) has no last axis, so the call is rejected with ValueError before any weights are created.

Solutions

  1. Reshape the scalar to at least 1D before the layer: x[None] or jnp.atleast_1d(x).
  2. Keep dims during reductions: x.sum(axis=-1, keepdims=True).
  3. Batch per-example scalars into a [B, 1] tensor upstream.

Example fix

# before
y = linear(jnp.float32(0.5))

# after
y = linear(jnp.atleast_1d(jnp.float32(0.5)))
Defensive patterns

Strategy: type-guard

Validate before calling

import jax.numpy as jnp
x = jnp.atleast_1d(x) if getattr(x, "ndim", 1) == 0 else x

Type guard

def is_non_scalar(x) -> bool:
    return hasattr(x, "shape") and len(x.shape) > 0

Prevention

When it happens

Trigger: Passing a JAX scalar (e.g. jnp.float32(1.0), the output of .sum() or .mean() without keepdims) into a Haiku Linear layer.

Common situations: Reduced tensors losing their dims in feature pipelines; indexing that yields 0-d arrays; feeding per-scalar summaries into a projection layer.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/92ad9f67ae8b78c8. Report an issue: GitHub.

Appendix: source

Thrown at phoenix/xrex/models/linear_layer.py:56

        self.input_size = None
        self.output_size = output_size
        self.with_bias = with_bias
        self.w_init = w_init
        self.b_init = b_init or jnp.zeros

        self.config = config
        self.rms_clip_axes = rms_clip_axes

        self.sharding_context = sharding_context
        self.pspec = pspec

    def __call__(
        self,
        inputs: jax.Array,
    ) -> jax.Array:
        fprop_dtype = inputs.dtype
        if not inputs.shape:
            raise ValueError("Input must not be scalar.")

        input_size = self.input_size = inputs.shape[-1]
        output_size = self.output_size

        w_init = self.w_init
        if w_init is None:
            stddev = self.config.init_scale / math.sqrt(self.input_size)
            w_init = hk.initializers.TruncatedNormal(stddev=stddev)

        w = get_parameter(
            "w",
            [input_size, output_size],
            jnp.float32,
            init=w_init,
            pspec=self.pspec,
            lr_multiplier=self.config.lr_multiplier,
            rms_clip_axes=self.rms_clip_axes,
        )

View on GitHub (pinned to 24c60942c5)