{"record":{"id":"67c4895fd84d80a0","repo":"jax-ml/jax","slug":"only-bitcast-between-types-of-the-same-bitwidth-su","errorCode":null,"errorMessage":"Only bitcast between types of the same bitwidth supported","messagePattern":"Only bitcast between types of the same bitwidth supported","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/mosaic/gpu/fragmented_array.py","lineNumber":2044,"sourceCode":"          args_slice = [utils.vector_slice(a, slice(offset, slice_end)) for a in args]\n          slices.append(fast_instr(*args_slice))\n          offset = slice_end\n        return utils.vector_concat(slices)\n    return fast_instr\n\n  def bitcast(\n      self, elt: ir.Type, *, output_is_signed: bool | None = None\n  ) -> FragmentedArray:\n    if (output_is_signed is not None) != isinstance(elt, ir.IntegerType):\n      raise TypeError(\n          \"output_is_signed must be non-None if and only if the MLIR type is an\"\n          f\" integer type, got {output_is_signed=} for {elt}\"\n      )\n\n    if elt == self.mlir_dtype:\n      return self\n    if utils.bitwidth(elt) != utils.bitwidth(self.mlir_dtype):\n      raise ValueError(\"Only bitcast between types of the same bitwidth supported\")\n    reg_type = self.registers.flat[0].type\n    if isinstance(reg_type, ir.VectorType):\n      reg_shape = ir.VectorType(reg_type).shape\n      ty = ir.VectorType.get(reg_shape, elt)\n    else:\n      ty = elt\n\n    return self._pointwise(\n        lambda x: arith.bitcast(ty, x), output_is_signed=output_is_signed, restrict_bitwidth=False\n    )\n\n  def __getitem__(self, idx) -> FragmentedArray:\n    base_idx, slice_shape, is_squeezed = utils.parse_indices(idx, self.shape)\n    if isinstance(self.layout, WGSplatFragLayout):\n      shape = tuple(d for d, s in zip(slice_shape, is_squeezed) if not s)\n      return self.splat(self.registers.item(), shape, is_signed=self.is_signed)\n    if not isinstance(self.layout, TiledLayout):\n      raise NotImplementedError(\"Only arrays with tiled layouts can be sliced\")","sourceCodeStart":2026,"sourceCodeEnd":2062,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/mosaic/gpu/fragmented_array.py#L2026-L2062","documentation":"FragmentedArray.bitcast only reinterprets bits between types of identical bitwidth (e.g. f32<->i32, f16<->i16); mismatched widths raise ValueError('Only bitcast between types of the same bitwidth supported'). Use to() for width-changing conversions.","triggerScenarios":"fa.bitcast(ir.IntegerType.get_signless(32)) on an f16 fragment (16 vs 32 bits), or bitcasting f32 to i8.","commonSituations":"Quantization/dequantization code that tries to reinterpret a float as a narrower int, mistaking bitcast for a numeric cast.","solutions":["Use fa.to(target_type) for width-changing, value-preserving casts","Pick a target type with matching bitwidth for bitcast (f32<->i32, f16<->i16, bf16<->i16)","Check utils.bitwidth(src)==utils.bitwidth(dst) before bitcasting"],"exampleFix":"# before\nq = f32_frag.bitcast(ir.IntegerType.get_signless(8))\n# after\nq = f32_frag.to(ir.IntegerType.get_signless(8))  # numeric cast\n# or same-width reinterpret:\nr = f32_frag.bitcast(ir.IntegerType.get_signless(32), output_is_signed=True)","handlingStrategy":"validation","validationCode":"assert utils.bitwidth(src) == utils.bitwidth(dst), 'bitcast width mismatch'","typeGuard":"def same_bitwidth(a: ir.Type, b: ir.Type) -> bool:\n    import jax.experimental.mosaic.gpu.utils as u\n    return u.bitwidth(a) == u.bitwidth(b)","tryCatchPattern":null,"preventionTips":["Use to() for width-changing casts, bitcast only for reinterpretation","Memorize pairs: f32<->i32, f16/bf16<->i16, f64<->i64"],"tags":["mosaic","gpu","bitcast","bitwidth"],"backgroundTag":"bitwidth-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}