jax-ml/jax · error · TypeError

dot_general requires rhs batch dimensions to be distinct, go

Error message

dot_general requires rhs batch dimensions to be distinct, got rhs_batch {rhs_batch}.

What it means

Raised by lax.dot_general validation when the rhs_batch sequence contains duplicate dimension indices. Each rhs batch dimension must be distinct so it pairs one-to-one with a distinct lhs batch dimension.

Source

Thrown at jax/_src/lax/lax.py:5716

    msg = ("dot_general requires rhs dimension numbers to be nonnegative and "
           "less than the number of axes of the rhs value, got "
           f"rhs_batch of {rhs_batch} and rhs_contracting of {rhs_contracting} "
           f"for rhs of rank {rhs.ndim}")
    raise TypeError(msg)
  if len(lhs_batch) != len(rhs_batch):
    msg = ("dot_general requires equal numbers of lhs_batch and rhs_batch "
           "dimensions, got lhs_batch {} and rhs_batch {}.")
    raise TypeError(msg.format(lhs_batch, rhs_batch))
  lhs_contracting_set, lhs_batch_set = set(lhs_contracting), set(lhs_batch)
  rhs_contracting_set, rhs_batch_set = set(rhs_contracting), set(rhs_batch)
  if len(lhs_batch_set) != len(lhs_batch):
    msg = ("dot_general requires lhs batch dimensions to be distinct, got "
           f"lhs_batch {lhs_batch}.")
    raise TypeError(msg)
  if len(rhs_batch_set) != len(rhs_batch):
    msg = ("dot_general requires rhs batch dimensions to be distinct, got "
           f"rhs_batch {rhs_batch}.")
    raise TypeError(msg)
  if len(lhs_contracting_set) != len(lhs_contracting):
    msg = ("dot_general requires lhs contracting dimensions to be distinct, "
           f"got lhs_contracting {lhs_contracting}.")
    raise TypeError(msg)
  if len(rhs_contracting_set) != len(rhs_contracting):
    msg = ("dot_general requires rhs contracting dimensions to be distinct, "
           f"got rhs_contracting {rhs_contracting}.")
    raise TypeError(msg)
  if lhs_contracting_set & lhs_batch_set:
    msg = ("dot_general requires lhs batch dimensions to be disjoint from "
           "contracting dimensions, got lhs_batch {} and lhs_contracting {}.")
    raise TypeError(msg.format(lhs_batch, lhs_contracting))
  if rhs_contracting_set & rhs_batch_set:
    msg = ("dot_general requires rhs batch dimensions to be disjoint from "
           "contracting dimensions, got rhs_batch {} and rhs_contracting {}.")
    raise TypeError(msg.format(rhs_batch, rhs_contracting))
  lhs_batch_shape = tuple(lhs.shape[i] for i in lhs_batch)
  rhs_batch_shape = tuple(rhs.shape[i] for i in rhs_batch)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Deduplicate rhs_batch entries; each rhs batch axis may be listed only once
  2. Cross-check that len(lhs_batch) == len(rhs_batch) and indices align pairwise
  3. Use jnp.einsum or vmap(matmul) instead of hand-written dimension numbers

Example fix

// before
res = lax.dot_general(x, y, (((), ()), ((0, 1), (1, 1))))
// after
res = lax.dot_general(x, y, (((), ()), ((0, 1), (0, 1))))
Defensive patterns

Strategy: validation

Validate before calling

rhs_batch = dimension_numbers[1][1]
assert len(set(rhs_batch)) == len(rhs_batch), 'duplicate rhs batch dims'

Type guard

def valid_batch_dims(dn):
    (lc, rc), (lb, rb) = dn
    return all(len(set(x)) == len(x) for x in (lb, rb))

Prevention

When it happens

Trigger: jax.lax.dot_general with dimension_numbers where dimension_numbers[1][1] (rhs batch dims) has repeats, e.g. (((), ()), ((0, 1), (2, 2))).

Common situations: Manually writing batch dimension tuples for custom vmap/jit rules or porting HLO DotDimensionNumbers; mismatched tuple lengths paired with duplicated indices.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/70880d4d08274b1d. Report an issue: GitHub.