Skip to content

[GPU][DT] Fix encoding on scalar tensor failure #21307

Closed

Description

@jtuyls

When hoisting encodings for a dispatch containing encodings on a scalar tensor, the hoisted set_encoding on the scalar tensor results in a new encoding dispatch and codegen for this fails in LLVM Translation with:

<unknown>:0: error: LLVM Translation failed for operation: builtin.unrealized_conversion_cast

Example input dispatch for the HoistEncodings pass:

%17 = flow.dispatch.region -> (tensor<?x14336xf8E4M3FNUZ>{%14}) {
  %cst = arith.constant 2.400000e+02 : f32
  %cst_8 = arith.constant -2.400000e+02 : f32
  %cst_9 = arith.constant 1.000000e+00 : f32
  %cst_10 = arith.constant 0.000000e+00 : f32
  %19 = iree_encoding.set_encoding %expanded_2 : tensor<?x4096xf8E4M3FNUZ> -> tensor<?x4096xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 0 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %20 = iree_encoding.set_encoding %expanded : tensor<14336x4096xf8E4M3FNUZ> -> tensor<14336x4096xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 1 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %21 = tensor.empty(%15) : tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %22 = linalg.fill ins(%cst_10 : f32) outs(%21 : tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>) -> tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %23 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"]} ins(%19, %20 : tensor<?x4096xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 0 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>, tensor<14336x4096xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 1 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>) outs(%22 : tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>) {
  ^bb0(%in: f8E4M3FNUZ, %in_11: f8E4M3FNUZ, %out: f32):
    %31 = arith.extf %in : f8E4M3FNUZ to f32
    %32 = arith.extf %in_11 : f8E4M3FNUZ to f32
    %33 = arith.mulf %31, %32 : f32
    %34 = arith.addf %out, %33 : f32
    linalg.yield %34 : f32
  } -> tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %24 = iree_encoding.set_encoding %5 : tensor<f32> -> tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>
  %25 = iree_encoding.set_encoding %collapsed_3 : tensor<?x14336xf32> -> tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>]]>>
  %26 = iree_encoding.set_encoding %6 : tensor<f32> -> tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>
  %27 = iree_encoding.set_encoding %7 : tensor<f32> -> tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>
  %28 = tensor.empty(%16) : tensor<?x14336xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %29 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> ()>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> ()>, affine_map<(d0, d1) -> ()>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%23, %24, %25, %26, %27 : tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>, tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>, tensor<?x14336xf32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>]]>>, tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>, tensor<f32, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1) -> ()>]]>>) outs(%28 : tensor<?x14336xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>) {
  ^bb0(%in: f32, %in_11: f32, %in_12: f32, %in_13: f32, %in_14: f32, %out: f8E4M3FNUZ):
    %31 = arith.mulf %in, %in_11 : f32
    %32 = arith.mulf %in_12, %in_13 : f32
    %33 = arith.negf %31 : f32
    %34 = math.exp %33 : f32
    %35 = arith.addf %34, %cst_9 : f32
    %36 = arith.divf %cst_9, %35 : f32
    %37 = arith.mulf %36, %31 : f32
    %38 = arith.mulf %37, %32 : f32
    %39 = arith.divf %38, %in_14 : f32
    %40 = arith.cmpf ult, %39, %cst_8 : f32
    %41 = arith.select %40, %cst_8, %39 : f32
    %42 = arith.cmpf ugt, %41, %cst : f32
    %43 = arith.select %42, %cst, %41 : f32
    %44 = arith.truncf %43 : f32 to f8E4M3FNUZ
    linalg.yield %44 : f8E4M3FNUZ
  } -> tensor<?x14336xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>>
  %30 = iree_encoding.unset_encoding %29 : tensor<?x14336xf8E4M3FNUZ, #iree_encoding.encoding<operand_index = 2 : index, op_type =  matmul, element_types = [f8E4M3FNUZ, f8E4M3FNUZ, f32], user_indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>]>> -> tensor<?x14336xf8E4M3FNUZ>{%15}
  flow.return %30 : tensor<?x14336xf8E4M3FNUZ>
}

This leads to following dispatch being created for the hoisted set_encoding on the scalar tensors:

func.func @_encoding_2_encode_f32_to_f32(%arg0: !stream.binding, %arg1: !stream.binding) {
  %c0 = arith.constant 0 : index
  %0 = stream.binding.subspan %arg0[%c0] : !stream.binding -> !iree_tensor_ext.dispatch.tensor<readonly:tensor<f32>>
  %1 = stream.binding.subspan %arg1[%c0] : !stream.binding -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<f32, #encoding1>>
  %2 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [], sizes = [], strides = [] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<f32>> -> tensor<f32>
  %3 = iree_encoding.set_encoding %2 : tensor<f32> -> tensor<f32, #encoding1>
  iree_tensor_ext.dispatch.tensor.store %3, %1, offsets = [], sizes = [], strides = [] : tensor<f32, #encoding1> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<f32, #encoding1>>
  return
}

This eventually leads to the following code that fails in LLVM translation.

llvm.func @_encoding_2_encode_f32_to_f32(%arg0: !llvm.ptr<1> {llvm.align = 16 : i32, llvm.inreg, llvm.noalias, llvm.nonnull, llvm.noundef, llvm.readonly}, %arg1: !llvm.ptr<1> {llvm.align = 16 : i32, llvm.inreg, llvm.noalias, llvm.nonnull, llvm.noundef}, %arg2: i32 {llvm.inreg, llvm.noundef, llvm.range = #llvm.constant_range<i32, 117440512, 117440641>}) attributes {rocdl.flat_work_group_size = "1,1", rocdl.kernel, rocdl.reqd_work_group_size = array<i32: 1, 1, 1>} {
  %0 = llvm.mlir.constant(8 : i64) : i64
  %1 = llvm.mlir.constant(32 : i64) : i64
  %2 = llvm.mlir.poison : !llvm.struct<(ptr<7>, ptr<7>, i64)>
  %3 = llvm.mlir.constant(159744 : i32) : i32
  %4 = llvm.mlir.constant(0 : i16) : i16
  %5 = llvm.mlir.constant(4 : i32) : i32
  %6 = llvm.mlir.constant(0 : i32) : i32
  %7 = llvm.mlir.constant(0 : index) : i64
  %8 = llvm.mlir.constant(64 : i32) : i32
  %9 = llvm.urem %arg2, %8 : i32
  %10 = llvm.icmp "eq" %9, %6 : i32
  llvm.intr.assume %10 : i1
  %11 = llvm.zext %arg2 : i32 to i64
  %12 = rocdl.make.buffer.rsrc %arg0, %4, %5, %3 : <1> to <7>
  %13 = llvm.insertvalue %12, %2[0] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %14 = llvm.insertvalue %12, %13[1] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %15 = llvm.insertvalue %7, %14[2] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %16 = builtin.unrealized_conversion_cast %15 : !llvm.struct<(ptr<7>, ptr<7>, i64)> to memref<f32, #amdgpu.address_space<fat_raw_buffer>>
  %17 = llvm.mul %11, %0 : i64
  %18 = llvm.udiv %17, %1 : i64
  %19 = llvm.getelementptr %arg1[%18] : (!llvm.ptr<1>, i64) -> !llvm.ptr<1>, f32
  %20 = rocdl.make.buffer.rsrc %19, %4, %5, %3 : <1> to <7>
  %21 = llvm.insertvalue %20, %2[0] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %22 = llvm.insertvalue %20, %21[1] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %23 = llvm.insertvalue %7, %22[2] : !llvm.struct<(ptr<7>, ptr<7>, i64)> 
  %24 = builtin.unrealized_conversion_cast %23 : !llvm.struct<(ptr<7>, ptr<7>, i64)> to memref<f32, strided<[]>, #amdgpu.address_space<fat_raw_buffer>>
  %25 = iree_codegen.load_from_buffer %16 : memref<f32, #amdgpu.address_space<fat_raw_buffer>> -> tensor<f32>
  iree_codegen.store_to_buffer %25, %24 : tensor<f32> into memref<f32, strided<[]>, #amdgpu.address_space<fat_raw_buffer>>
  llvm.return
}

Activity

  1. self-assigned this
    on Jul 8, 2025
  2. jtuyls commented on Jul 8, 2025

    @jtuyls
    ContributorAuthor

    @hanhanW Currently, I get around this issue by not hoisting set_encoding ops on scalar tensor types. I think it can be solved in codegen as well by inserting a copy operation, but I think we would want to get rid of the dispatch altogether? Is there a better place to avoid/get rid of these dispatches or do we want to adjust the HoistEncoding pass for this?

  3. changed the title [-]Fix encoding on scalar tensor failure[/-] [+][GPU][DT] Fix encoding on scalar tensor failure[/+] on Jul 8, 2025
  4. hanhanW commented on Jul 8, 2025

    @hanhanW
    Contributor

    I feel that we don't want to hoist set_encoding ops on 0-D tensor as it is just a broadcast op?

    cc @Max191 @bjacob @MaheshRavishankar

  5. Max191 commented on Jul 8, 2025

    @Max191
    Contributor

    I feel that we don't want to hoist set_encoding ops on 0-D tensor as it is just a broadcast op?

    Yeah, there's no need to hoist, since I think it will just stay a scalar tensor after materialization (if it doesn't then it should).

  6. jtuyls commented on Jul 8, 2025

    @jtuyls
    ContributorAuthor

    I feel that we don't want to hoist set_encoding ops on 0-D tensor as it is just a broadcast op?

    Yeah, there's no need to hoist, since I think it will just stay a scalar tensor after materialization (if it doesn't then it should).

    That's a bit of the question, can we have encodings that would change a scalar tensor? If so, then we should hoist them, but then we end up with an additional 'copy' dispatch in all (I think) current cases. Even in the case of a padding encoding, I don't think we would ever want to pad scalar tensors? Maybe there is a way though that we could check the effect of an encoding on a scalar tensor (or add an interface method for that)?

  7. Max191 commented on Jul 8, 2025

    @Max191
    Contributor

    That's a bit of the question, can we have encodings that would change a scalar tensor?

    For now there is nothing that will resolve into anything other than a scalar value AFAIK, although an encoding that changes a scalar tensor is unlikely to be anything other than a broadcast, which we should always fuse with its consumer (i.e., don't hoist).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions