{"record":{"id":"3ace60f2eb874019","repo":"jax-ml/jax","slug":"attempting-to-convert-array-of-shape-operand-shap","errorCode":null,"errorMessage":"Attempting to convert array of shape {operand.shape} from {old_dtype} of size {old_nbits} bits to {new_dtype} of size {new_nbits}, bits but {dim_size} * {old_nbits} != {new_nbits}","messagePattern":"Attempting to convert array of shape (.+?) from (.+?) of size (.+?) bits to (.+?) of size (.+?), bits but (.+?) \\* (.+?) != (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/lax.py","lineNumber":5607,"sourceCode":"    lambda ct, x, dtype: [to_edtype_p.bind(ct, edtype=x.dtype)]\nbatching.defvectorized(from_edtype_p)\nmlir.register_lowering(from_edtype_p, lambda _, x, **__: [x])\n\n\ndef _bitcast_convert_type_shape_rule(operand, *, new_dtype):\n  old_dtype = operand.dtype\n\n  old_nbits = dtypes.itemsize_bits(old_dtype)\n  new_nbits = dtypes.itemsize_bits(new_dtype)\n\n  if old_nbits == new_nbits:\n    return operand.shape\n  elif old_nbits > new_nbits:\n    return (*operand.shape, old_nbits // new_nbits)\n  else:\n    dim_size = operand.shape[-1] if operand.shape else 1\n    if dim_size * old_nbits != new_nbits:\n      raise ValueError(\n        f\"Attempting to convert array of shape {operand.shape} \"\n        f\"from {old_dtype} of size {old_nbits} bits \"\n        f\"to {new_dtype} of size {new_nbits}, bits \"\n        f\"but {dim_size} * {old_nbits} != {new_nbits}\")\n    return operand.shape[:-1]\n\ndef _bitcast_convert_type_sharding_rule(operand, *, new_dtype):\n  old_dtype = operand.dtype\n\n  old_nbits = dtypes.itemsize_bits(old_dtype)\n  new_nbits = dtypes.itemsize_bits(new_dtype)\n\n  if old_nbits == new_nbits:\n    return operand.sharding\n  elif old_nbits > new_nbits:\n    return operand.sharding.update(spec=(*operand.sharding.spec, None))\n  else:\n    return operand.sharding.update(spec=operand.sharding.spec[:-1])","sourceCodeStart":5589,"sourceCodeEnd":5625,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/lax.py#L5589-L5625","documentation":"bitcast_convert_type's output shape rule: when upsizing (old_nbits < new_nbits, e.g. uint8 -> uint16), the last dimension of the operand must exactly supply the bits of one new element: dim_size * old_nbits == new_nbits. Otherwise the bit-level reinterpretation is impossible and ValueError is raised.","triggerScenarios":"jax.lax.bitcast_convert_type(x, jnp.uint32) where x is uint8 with last-dim size not divisible by 4 (e.g. shape (10,)), or bitcasting a scalar to a wider type.","commonSituations":"Packing/unpacking sub-byte data (uint8 views of float4/float8 payloads); resizing tensors before bitcast; forgetting that bitcast to wider types consumes the last axis.","solutions":["Reshape so the last dimension groups old elements into whole new elements (e.g. (10,) uint8 -> (2, 5) or (10//4, 4) for uint32)","Pick a new_dtype of equal bit width (shape preserved) or narrower (shape gains a trailing axis)","Pad the last dim to a multiple of new_nbits/old_nbits if semantics allow","Handle the scalar case by adding a trailing axis of the required group size"],"exampleFix":"// before\ny = lax.bitcast_convert_type(x_uint8.reshape(10), jnp.uint32)  # 10*8 != 32\n\n// after\ny = lax.bitcast_convert_type(x_uint8.reshape(10, 1), jnp.uint8)  # same width\n# or group: x_uint8.reshape(2, 5) is invalid; use (…, 4)->uint32\ny = lax.bitcast_convert_type(x_uint8[:8].reshape(2, 4), jnp.uint32)","handlingStrategy":"validation","validationCode":"old_b, new_b = np.dtype(x.dtype).itemsize*8, np.dtype(new_dtype).itemsize*8\nif old_b < new_b:\n    group = new_b // old_b\n    assert x.shape[-1] % group == 0 if x.ndim else False or new_b == old_b, 'regroup last axis'","typeGuard":null,"tryCatchPattern":"try:\n    y = lax.bitcast_convert_type(x, dt)\nexcept ValueError:\n    g = new_bits // old_bits\n    y = lax.bitcast_convert_type(x.reshape(*x.shape[:-1], -1, g), dt)","preventionTips":["Compute the bit-group size before widening bitcasts","Reshape to (..., new/old) grouped form as a standard preprocessing step","Prefer equal-width bitcasts when shape stability matters"],"tags":["jax","bitcast","dtype","shape-validation"],"backgroundTag":"bitcast-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}