{"record":{"id":"dbc288a95b0d370e","repo":"jax-ml/jax","slug":"can-t-bitcast-x-type-to-new-type","errorCode":null,"errorMessage":"Can't bitcast {x.type} to {new_type}","messagePattern":"Can't bitcast (.+?) to (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/mosaic/gpu/utils.py","lineNumber":2092,"sourceCode":"    return vector.extract(\n        vector.bitcast(ir.VectorType.get((1,), new_type), x),\n        dynamic_position=[],\n        static_position=ir.DenseI64ArrayAttr.get([0]),\n    )\n  if isinstance(x.type, ir.IntegerType) and isinstance(new_type, ir.VectorType):\n    new_type = ir.VectorType(new_type)\n    x_ty = ir.IntegerType(x.type)\n    assert x_ty.width == bitwidth(new_type.element_type) * math.prod(\n        new_type.shape\n    )\n    return vector.bitcast(\n        new_type, vector.broadcast(ir.VectorType.get((1,), x_ty), x)\n    )\n  if isinstance(x.type, ir.VectorType) and isinstance(new_type, ir.VectorType):\n    x_ty = ir.VectorType(x.type)\n    new_ty = ir.VectorType(new_type)\n    if bitwidth(x_ty) != bitwidth(new_ty):\n      raise ValueError(f\"Can't bitcast {x.type} to {new_type}\")\n    return vector.bitcast(new_type, x)\n  if isinstance(x.type, ir.IntegerType) and isinstance(new_type, ir.FloatType):\n    return arith.bitcast(new_type, x)\n  if isinstance(x.type, ir.FloatType) and isinstance(new_type, ir.IntegerType):\n    return arith.bitcast(new_type, x)\n  if isinstance(x.type, ir.FloatType) and isinstance(new_type, ir.FloatType):\n    return arith.bitcast(new_type, x)\n  raise ValueError(f\"Can't bitcast {x.type} to {new_type}\")\n\n\ndef ceil_div(x: int, y: int):\n  return (x + y - 1) // y\n\n\ndef vector_slice(v: ir.Value, s: slice):\n  v_ty = ir.VectorType(v.type)\n  if len(v_ty.shape) != 1:\n    raise NotImplementedError(f\"Only 1D vectors are supported {v_ty}\")","sourceCodeStart":2074,"sourceCodeEnd":2110,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/mosaic/gpu/utils.py#L2074-L2110","documentation":"In the vector-to-vector bitcast path, both vectors must have identical total bitwidth even though their shapes/element types may differ. A mismatch (e.g. vector<4xf32> to vector<2xf32>) raises this ValueError.","triggerScenarios":"Calling bitcast with two ir.VectorType operands whose element counts times element bitwidths differ, e.g. vector<8xi8> -> vector<4xi8>, or vector<4xf32> -> vector<16xi8> is fine but vector<4xf32> -> vector<8xi8> is not.","commonSituations":"Reshaping packed low-precision data (bf16/f16 vectors) where the element-count math is off by one; changing a vector width in one place but not its bitcast mirror.","solutions":["Recompute lengths: target element count = source element count * source bitwidth / target element bitwidth","Use vector.shape_cast to change the number of elements of the same type first, then bitcast element-wise widths","Add an assert comparing bitwidth(x_ty) == bitwidth(new_ty) in debug builds of your kernel"],"exampleFix":"# before\nbitcast(v_f32_4, ir.VectorType.get((2,), f32))  # 128 vs 64 bits\n# after\nbitcast(v_f32_4, ir.VectorType.get((8,), f16))  # 128 == 128 bits","handlingStrategy":"validation","validationCode":"xt, nt = ir.VectorType(x.type), ir.VectorType(new_type)\nassert bitwidth(xt) == bitwidth(nt), f'vector bitcast width mismatch {xt} vs {nt}'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pair every vector shape change with a matching bitcast update","Test pack/unpack round-trips for width invariants"],"tags":["mosaic-gpu","bitcast","vector-types"],"backgroundTag":"bitwidth-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}