{"record":{"id":"7f66b7fb9d6b4b5a","repo":"jax-ml/jax","slug":"only-support-memref-cast-where-the-input-and-outpu","errorCode":null,"errorMessage":"Only support memref.cast where the input and output types are the same up to offset, but got {in_ty=} and {out_ty=}.","messagePattern":"Only support memref\\.cast where the input and output types are the same up to offset, but got (.+?) and (.+?)\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"jax/experimental/mosaic/gpu/dialect_lowering.py","lineNumber":2078,"sourceCode":"  out_ty = ir.MemRefType(op.result.type)\n  in_strides, _ = in_ty.get_strides_and_offset()\n  out_strides, _ = out_ty.get_strides_and_offset()\n\n  unoffseted_in_ty = ir.MemRefType.get(\n      in_ty.shape,\n      in_ty.element_type,\n      memory_space=in_ty.memory_space,\n      layout=ir.StridedLayoutAttr.get(0, in_strides),\n  )\n  unoffseted_out_ty = ir.MemRefType.get(\n      out_ty.shape,\n      out_ty.element_type,\n      memory_space=out_ty.memory_space,\n      layout=ir.StridedLayoutAttr.get(0, out_strides),\n  )\n\n  if unoffseted_in_ty != unoffseted_out_ty:\n    raise NotImplementedError(\n        \"Only support memref.cast where the input and output types are the \"\n        f\"same up to offset, but got {in_ty=} and {out_ty=}.\"\n    )\n\n  memory_space = ir.MemRefType(op.result.type).memory_space\n  if memory_space == utils.smem():\n    [in_transforms] = inference_utils.in_transforms(op)\n    [out_transforms] = inference_utils.out_transforms(op)\n    if in_transforms != out_transforms:\n      raise NotImplementedError(\n          \"memref.cast transforms must have identical transforms for both \"\n          f\"input and output but got {in_transforms=} and {out_transforms=}\"\n      )\n    result = memref.cast(\n        transform_type(ir.MemRefType(op.result.type), out_transforms),\n        unwrap_transformed_memref(op.source, in_transforms),\n    )\n    return [wrap_transformed_memref(result, op.result.type, out_transforms)]","sourceCodeStart":2060,"sourceCodeEnd":2096,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/mosaic/gpu/dialect_lowering.py#L2060-L2096","documentation":"memref.cast lowering requires the input and output memref types to be identical after erasing offsets. This error means the cast changes shape, strides, layout, or element type beyond a constant offset, which the lowering cannot represent.","triggerScenarios":"Issuing memref.cast where ir.MemRefType(source) and ir.MemRefType(result) differ in shape/strides/element type/memory space after normalizing offset to zero, e.g. casting a 2D memref to one with different dimensions.","commonSituations":"Using Mosaic's cast_to_smem/tmem helpers on a tensor whose layout was reshaped or re-strided first; mixing memory spaces or dtypes in a cast instead of using proper load/store conversion.","solutions":["Make the cast type-preserving: only the offset may differ; keep shape, strides, element type and memory space equal","Use memref.reshape / expand_shape / collapse_shape for shape changes and a load+store or convert op for dtype changes","Verify with ir.MemRefType(x).get_strides_and_offset() that only the offset differs before emitting the cast"],"exampleFix":"// before\nref2 = t.memref.cast(ref, new_ty_with_different_shape)\n// after\nref2 = t.memref.collapse_shape(ref, reassociation)  # shape change\nref3 = t.memref.cast(ref2, same_shape_offset_ty)     # cast only","handlingStrategy":"type-guard","validationCode":"in_ty, out_ty = ir.MemRefType(src.type), ir.MemRefType(dst.type)\nassert in_ty.shape == out_ty.shape and in_ty.element_type == out_ty.element_type","typeGuard":"def castable_up_to_offset(in_ty, out_ty) -> bool:\n    return (in_ty.shape == out_ty.shape\n            and in_ty.element_type == out_ty.element_type\n            and in_ty.memory_space == out_ty.memory_space)","tryCatchPattern":null,"preventionTips":["Only use cast for offset-only changes","Use reshape ops for shape changes"],"tags":["jax","mosaic-gpu","memref","cast","type-mismatch"],"backgroundTag":"memref-type-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}