{"record":{"id":"76307cbe40f55899","repo":"jax-ml/jax","slug":"reshape-new-sizes-must-all-be-positive-got","errorCode":null,"errorMessage":"reshape new_sizes must all be positive, got {}.","messagePattern":"reshape new_sizes must all be positive, got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":7800,"sourceCode":"\ndef shape_as_value(shape: core.Shape):\n  \"\"\"Converts a shape that may contain Poly values into a JAX value.\"\"\"\n  dtype = lax_utils.int_dtype_for_shape(shape, signed=True)\n  if len(shape) == 0:\n    return full((0,), np.array(0, dtype=dtype))\n  if core.is_constant_shape(shape):\n    return np.asarray(shape, dtype=dtype)\n  dims = [\n      expand_dims(convert_element_type(core.dimension_as_value(d), dtype),\n                  (0,))\n      for d in shape\n  ]\n  return concatenate(dims, dimension=0)\n\ndef _reshape_shape_rule(operand, *, new_sizes, dimensions, sharding):\n  if not all(d >= 0 for d in new_sizes):\n    msg = 'reshape new_sizes must all be positive, got {}.'\n    raise TypeError(msg.format(new_sizes))\n  # TODO(necula): re-enable this check\n  if dimensions is not None:\n    if set(dimensions) != set(range(np.ndim(operand))):\n      msg = ('reshape dimensions must be a permutation of operand dimensions, '\n             'got dimensions {} for shape {}.')\n      raise TypeError(msg.format(dimensions, np.shape(operand)))\n  return tuple(new_sizes)\n\nclass ReshapeExplicitError(Exception):\n  pass\n\ndef _split_on_one_axis(op_shape, new_sizes):\n  op_shape = [s for s in op_shape if s != 1]\n  new_sizes = [s for s in new_sizes if s != 1]\n\n  if len(new_sizes) <= len(op_shape):\n    return False, []\n","sourceCodeStart":7782,"sourceCodeEnd":7818,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L7782-L7818","documentation":"jax.lax.reshape requires every entry of new_sizes to be >= 0 (the error text says 'positive' but the check is d >= 0, allowing 0-sized dims); any negative dimension raises this TypeError. Unlike NumPy's reshape, lax.reshape does not accept a -1 wildcard — the target shape must be explicit.","triggerScenarios":"jax.lax.reshape(x, new_sizes=[-1, 128]) — using NumPy's infer-dimension idiom; computed target dims going negative.","commonSituations":"Porting numpy.reshape code with -1 directly to jax.lax.reshape; size arithmetic (e.g. n - k) producing negatives when k > n; dynamic batch inference code.","solutions":["Use jnp.reshape(x, (-1, 128)) or x.reshape(-1, 128), which supports -1 inference","Compute the flat size and derive dims explicitly: flat = x.size; rows = flat // 128","Validate all target dims are >= 0 before calling lax.reshape"],"exampleFix":"# before\ny = jax.lax.reshape(x, new_sizes=[-1, 128])\n# after\ny = x.reshape(-1, 128)  # or jax.lax.reshape(x, (x.size // 128, 128))","handlingStrategy":"validation","validationCode":"assert all(d >= 0 for d in new_sizes), new_sizes\n# for -1 inference, use jnp-level reshape or compute explicitly:\nnew_sizes = (x.size // known_dim, known_dim)","typeGuard":"def valid_new_sizes(sizes) -> bool:\n    return all(d >= 0 for d in sizes)","tryCatchPattern":null,"preventionTips":["Use x.reshape(-1, k) instead of lax.reshape when you need -1 inference","Derive target dims from x.size to avoid negatives in computed shapes"],"tags":["jax","reshape","negative-dimension","shape-validation"],"backgroundTag":"invalid-reshape-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}