{"record":{"id":"342cc9d4a96d28ad","repo":"jax-ml/jax","slug":"length-of-major-to-minor-and-the-rank-of-the-value","errorCode":null,"errorMessage":"Length of major_to_minor and the rank of the value should match. Got major_to_minor={self.major_to_minor} and shape={aval_shape}","messagePattern":"Length of major_to_minor and the rank of the value should match\\. Got major_to_minor=(.+?) and shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/layout.py","lineNumber":122,"sourceCode":"                  kwargs['sub_byte_element_size_in_bits'])\n\n  def _to_xla_layout(self, dtype) -> xc.Layout:\n    if self.tiling is None:\n      xla_layout = xc.Layout(self.major_to_minor[::-1])\n    else:\n      if self.sub_byte_element_size_in_bits != 0:\n        sub_byte_size = self.sub_byte_element_size_in_bits\n      elif issubdtype(dtype, np.integer):\n        sub_byte_size = iinfo(dtype).bits if iinfo(dtype).bits < 8 else 0\n      else:\n        sub_byte_size = 0\n      xla_layout = xc.Layout(self.major_to_minor[::-1], self.tiling,\n                              sub_byte_size)\n    return xla_layout\n\n  def check_compatible_aval(self, aval_shape: Shape):\n    if len(self.major_to_minor) != len(aval_shape):\n      raise ValueError(\n          f'Length of major_to_minor and the rank of the value should match.'\n          f' Got major_to_minor={self.major_to_minor} and shape={aval_shape}')\n\n\nLayoutOptions = Layout | None | AutoLayoutSingleton\nShardingOptions = Sharding | None\n\n\nclass Format:\n  __slots__ = ['layout', 'sharding']\n\n  def __init__(self, layout: LayoutOptions = None,\n               sharding: ShardingOptions = None):\n    # If layout is concrete and sharding is not, error.\n    if isinstance(layout, Layout) and sharding is None:\n      raise ValueError(\n          'Sharding has to be concrete when layout is of type'\n          f' {type(layout)}. Please pass a'","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/layout.py#L104-L140","documentation":"A jax Layout's major_to_minor permutation must have exactly as many entries as the array's rank. check_compatible_aval enforces this before using the layout with a value of a given shape.","triggerScenarios":"Creating a Layout(major_to_minor=[0,1]) and applying it to a 3-D array's abstract value.","commonSituations":"Reusing a layout object across tensors of different rank in sharding/layout APIs; constructing layouts from hardcoded permutations.","solutions":["Build major_to_minor with length equal to the target rank, e.g. list(range(ndim))[::-1]","Derive the layout from the array: use ndim of the actual value when constructing"],"exampleFix":"# before\nlayout = Layout(major_to_minor=(1,0))\nlayout.check_compatible_aval(x.shape)  # x is 3-D\n# after\nlayout = Layout(major_to_minor=tuple(range(x.ndim)[::-1]))\nlayout.check_compatible_aval(x.shape)","handlingStrategy":"validation","validationCode":"assert len(layout.major_to_minor) == len(shape), f'{len(layout.major_to_minor)} vs rank {len(shape)}'","typeGuard":"def layout_fits(layout, shape): return len(layout.major_to_minor) == len(shape)","tryCatchPattern":null,"preventionTips":["Construct layouts per-tensor from its actual ndim"],"tags":["jax","layout","rank-mismatch","validation"],"backgroundTag":"layout-rank-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}