{"record":{"id":"16aedbb40330c7ab","repo":"jax-ml/jax","slug":"second-dimension-x-shape-1-must-be-divisible-by","errorCode":null,"errorMessage":"Second dimension {x.shape[1]} must be divisible by batch_size {batch_size}","messagePattern":"Second dimension (.+?) must be divisible by batch_size (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/mosaic_gpu/core.py","lineNumber":1319,"sourceCode":"class ExpandLeadingBatchDimensionsTransform(state_types.Transform):\n  \"\"\"The inverse of CollapseLeadingBatchDimensionsTransform.\n\n  Specifically, it maps `(m, math.prod(batch_shape) * n)` to `(*batch_shape, m,\n  n)`.\n  \"\"\"\n\n  batch_shape: tuple[int, ...] = jax.tree.static()\n\n  def transform_type(\n      self, x: jax_core.AbstractValue\n  ) -> state_types.AbstractRef:\n    match x:\n      case jax_core.ShapedArray():\n        if x.ndim != 2:\n          raise ValueError(f\"Unsupported shape: {x.shape}\")\n        batch_size = math.prod(self.batch_shape)\n        if x.shape[1] % batch_size != 0:\n          raise ValueError(\n              f\"Second dimension {x.shape[1]} must be divisible by batch_size\"\n              f\" {batch_size}\"\n          )\n        transformed_shape = self.batch_shape + (\n            x.shape[0],\n            x.shape[1] // batch_size,\n        )\n        return x.update(shape=transformed_shape)\n      case state_types.AbstractRef():\n        return x.update(inner_aval=self.transform_type(x.inner_aval))\n      case _:\n        raise TypeError(f\"Unsupported type: {x}\")\n\n  def commute_ndindexer(\n      self, aval: jax_core.AbstractValue, indexer: indexing.NDIndexer\n  ) -> tuple[indexing.NDIndexer, state_types.Transform]:\n    del aval\n    batch_shape = self.batch_shape","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/mosaic_gpu/core.py#L1301-L1337","documentation":"When expanding batch dimensions, the second (column) dimension of the 2-D physical array must be divisible by the product of batch_shape, because each logical row holds batch*n elements. A column extent that isn't a multiple raises this ValueError.","triggerScenarios":"Passing a 2-D aval whose shape[1] % prod(batch_shape) != 0 to ExpandLeadingBatchDimensionsTransform.transform_type (e.g. batch_shape (3,) with a shape[1] of 64).","commonSituations":"Allocating physical buffers sized without accounting for the batch multiplier; irregular batch sizes (non-power-of-2 pipelines) in WGMMA layouts.","solutions":["Size the physical second dim as batch_size * n","Pad n so shape[1] is a multiple of batch_size","Correct the batch_shape so it divides the column extent"],"exampleFix":"// before\nphys = ShapedArray((m, 64), dt)  # batch_shape=(3,) -> 64 % 3 != 0\n// after\nphys = ShapedArray((m, 66), dt)  # 66 = 3 * 22","handlingStrategy":"validation","validationCode":"import math\nbs = math.prod(batch_shape)\nassert shape[1] % bs == 0, f'{shape[1]} not divisible by batch_size {bs}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Size columns as batch_size * n","Pad columns up to a multiple of prod(batch_shape)"],"tags":["jax","pallas","shape-validation","divisibility"],"backgroundTag":"shape-divisibility-violation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}