{"record":{"id":"c6844bb208f824bc","repo":"jax-ml/jax","slug":"cannot-cast-src-tp-dst-type","errorCode":null,"errorMessage":"cannot cast {src} tp {dst_type}","messagePattern":"cannot cast (.+?) tp (.+?)","errorType":"validation","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/_src/pallas/triton/lowering.py","lineNumber":1595,"sourceCode":"  if dst_element_type.width == 1:\n    return _not_equal(src, _zeros_like(src), signed=signed)\n\n  if src_element_type.width == dst_element_type.width:\n    return arith_dialect.bitcast(dst_type, src)\n  elif src_element_type.width > dst_element_type.width:\n    return arith_dialect.trunci(dst_type, src)\n  elif signed and src_element_type.width != 1:\n    return arith_dialect.extsi(dst_type, src)\n  else:\n    return arith_dialect.extui(dst_type, src)\n\n\ndef _float_int_cast(\n    src: ir.Value, dst_type: ir.Type, *, signed: bool\n) -> ir.Value:\n  src_element_type = _element_type(src.type)\n  if not isinstance(src_element_type, (ir.BF16Type, ir.F16Type, ir.F32Type, ir.F64Type)):\n    raise NotImplementedError(f\"cannot cast {src} tp {dst_type}\")\n  dst_element_type = ir.IntegerType(_element_type(dst_type))\n  if dst_element_type.width == 1:\n    return _not_equal(src, _zeros_like(src), signed=signed)\n  else:\n    # We clamp the float value to the min/max integer destination value\n    # in order to match JAX/XLA casting behavior. Note that this differs\n    # from numpy casting behavior.\n    if signed:\n      maxint = 2**(dst_element_type.width-1) - 1\n      minint = -2**(dst_element_type.width-1)\n    else:\n      maxint = 2**dst_element_type.width - 1\n      minint = 0\n    src = arith_dialect.minimumf(src, _full(src.type, maxint))\n    src = arith_dialect.maximumf(src, _full(src.type, minint))\n    if signed:\n      return arith_dialect.fptosi(dst_type, src)\n    else:","sourceCodeStart":1577,"sourceCodeEnd":1613,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/pallas/triton/lowering.py#L1577-L1613","documentation":"_float_int_cast lowers float→int casts, but only accepts source floats of bf16/f16/f32/f64. Any other source element type (float8 variants, custom float types) fails with 'cannot cast {src} tp {dst_type}' (note the 'tp' typo in the message). This path also implements bool casts via a not-equal comparison against zero.","triggerScenarios":"Casting a float8 tensor (e.g. e4m3/e5m2 from ml_dtypes) to an integer or bool inside a Triton Pallas kernel: x.astype(jnp.int32) or bool(x_f8); any custom FloatType outside the four supported ones reaching _ir_cast.","commonSituations":"Mixed-precision float8 kernels that quantize to int; dequantize/quantize pipelines written for XLA being ported to Pallas/Triton; ml_dtypes float8 usage becoming more common on H100/Blackwell GPUs.","solutions":["First upcast the float8 value to float32, then cast to int: x.astype(jnp.float32).astype(jnp.int32)","Restructure the kernel to keep values in supported dtypes (f32 accumulators) and convert at the boundaries","Upgrade JAX / check for float8 cast support in pallas triton lowering and file an issue with the exact dtype pair"],"exampleFix":"// before\ni = x_f8.astype(jnp.int32)\n// after\ni = x_f8.astype(jnp.float32).astype(jnp.int32)","handlingStrategy":"fallback","validationCode":"TRITON_FLOATS = (jnp.bfloat16, jnp.float16, jnp.float32, jnp.float64)\ndef cast_int_safe(x, dt):\n    if x.dtype not in TRITON_FLOATS:\n        x = x.astype(jnp.float32)\n    return x.astype(dt)","typeGuard":"def is_triton_castable_float(dt):\n    return dt in (jnp.bfloat16, jnp.float16, jnp.float32, jnp.float64)","tryCatchPattern":"try:\n    out = pallas_kernel(x)\nexcept NotImplementedError as e:\n    if 'cannot cast' in str(e):\n        x = x.astype(jnp.float32)\n        out = pallas_kernel(x)\n    else:\n        raise","preventionTips":["Upcast float8 values to f32 before any int/bool cast in Triton kernels","Keep quantize/dequantize steps in f32 and convert at kernel boundaries"],"tags":["jax","pallas","triton","dtype-cast","float8","notimplementederror"],"backgroundTag":"unsupported-dtype-cast","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}