{"record":{"id":"fcf4223fc79666db","repo":"jax-ml/jax","slug":"dot-general-requires-lhs-dimension-numbers-to-be-n","errorCode":null,"errorMessage":"dot_general requires lhs dimension numbers to be nonnegative and less than the number of axes of the lhs value, got lhs_batch of {lhs_batch} and lhs_contracting of {lhs_contracting} for lhs of rank {lhs.ndim}","messagePattern":"dot_general requires lhs dimension numbers to be nonnegative and less than the number of axes of the lhs value, got lhs_batch of (.+?) and lhs_contracting of (.+?) for lhs of rank (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5695,"sourceCode":"  preferred_bitwidth = np.dtype(preferred_element_type).itemsize\n  if preferred_bitwidth < input_bitwidth:\n    raise TypeError(\"`preferred_element_type` must not be narrower than the \"\n                    \"original type.\")\n\n\ndef _dot_general_shape_rule(lhs, rhs, *, dimension_numbers, precision,\n                            preferred_element_type: DTypeLike | None,\n                            out_sharding):\n  if out_sharding is not None and not isinstance(out_sharding, NamedSharding):\n    raise NotImplementedError\n  (lhs_contracting, rhs_contracting), (lhs_batch, rhs_batch) = _from_maybe_ragged(dimension_numbers)\n  if not all(np.all(np.greater_equal(d, 0)) and np.all(np.less(d, lhs.ndim))\n             for d in (lhs_contracting, lhs_batch)):\n    msg = (\"dot_general requires lhs dimension numbers to be nonnegative and \"\n           \"less than the number of axes of the lhs value, got \"\n           f\"lhs_batch of {lhs_batch} and lhs_contracting of {lhs_contracting} \"\n           f\"for lhs of rank {lhs.ndim}\")\n    raise TypeError(msg)\n  if not all(np.all(np.greater_equal(d, 0)) and np.all(np.less(d, rhs.ndim))\n             for d in (rhs_contracting, rhs_batch)):\n    msg = (\"dot_general requires rhs dimension numbers to be nonnegative and \"\n           \"less than the number of axes of the rhs value, got \"\n           f\"rhs_batch of {rhs_batch} and rhs_contracting of {rhs_contracting} \"\n           f\"for rhs of rank {rhs.ndim}\")\n    raise TypeError(msg)\n  if len(lhs_batch) != len(rhs_batch):\n    msg = (\"dot_general requires equal numbers of lhs_batch and rhs_batch \"\n           \"dimensions, got lhs_batch {} and rhs_batch {}.\")\n    raise TypeError(msg.format(lhs_batch, rhs_batch))\n  lhs_contracting_set, lhs_batch_set = set(lhs_contracting), set(lhs_batch)\n  rhs_contracting_set, rhs_batch_set = set(rhs_contracting), set(rhs_batch)\n  if len(lhs_batch_set) != len(lhs_batch):\n    msg = (\"dot_general requires lhs batch dimensions to be distinct, got \"\n           f\"lhs_batch {lhs_batch}.\")\n    raise TypeError(msg)\n  if len(rhs_batch_set) != len(rhs_batch):","sourceCodeStart":5677,"sourceCodeEnd":5713,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5677-L5713","documentation":"dot_general takes dimension_numbers = ((lhs_contracting, rhs_contracting), (lhs_batch, rhs_batch)). All lhs dimension indices must be in [0, lhs.ndim). Negative or too-large indices (numpy-style negative indexing is not supported) raise TypeError with the offending lists and rank.","triggerScenarios":"lax.dot_general(a, b, ((-1,), ()), (...)) using a negative axis; passing contracting dim 3 for a rank-2 lhs; misordered tuple where batch dims land in the contracting slot.","commonSituations":"Porting numpy einsum or jnp.tensordot axis lists that allow negative indices; misreading the nested dimension_numbers tuple structure; off-by-one axis constants after refactoring.","solutions":["Use nonnegative axis indices within range for lhs contracting and batch dims","Remember structure ((lhs_contract, rhs_contract), (lhs_batch, rhs_batch)) and verify each list","Prefer jnp.einsum/jnp.tensordot/jnp.matmul which accept friendlier axis specs","Validate indices against lhs.ndim before calling"],"exampleFix":"// before\nout = lax.dot_general(a, b, ((-1,), (0,)), ((), ()))\n\n// after\nout = lax.dot_general(a, b, ((a.ndim - 1,), (0,)), ((), ()))","handlingStrategy":"validation","validationCode":"assert all(0 <= d < lhs.ndim for d in (*lhs_contracting, *lhs_batch)), 'bad lhs dims'","typeGuard":"def valid_lhs_dims(dn, lhs) -> bool:\n    (lc, _), (lb, _) = dn\n    return all(0 <= d < lhs.ndim for d in (*lc, *lb))","tryCatchPattern":null,"preventionTips":["Never use negative axis indices in dot_general dimension_numbers","Validate both dim lists against operand ranks before calling","Prefer einsum/tensordot for hand-derived contractions"],"tags":["jax","dot-general","dimension-numbers","index-out-of-range"],"backgroundTag":"axis-index-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}