Skip to content

[CodeGen] Hang during CombineLayoutTransformationPass #21622

Closed

Description

@zjgarvey

What happened?

A pattern FuseTilableForallConsumers seems to be caught in a loop of creating ops and failing, causing a hang.

debug print looks like repeated sequences of:


//===-------------------------------------------===//
Processing operation : 'linalg.generic'(0x598460fbecb0) {

  * Pattern mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers : 'linalg.generic -> ()' {
[pattern-application:1] PatternApplicator.cpp:209 Trying to match "mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers"
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | linalg.generic
    ** Insert  : 'linalg.generic'(0x598460fbecb0)
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | tensor.expand_shape
    ** Insert  : 'tensor.expand_shape'(0x5984610a4730)
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | tensor.dim
    ** Insert  : 'tensor.dim'(0x598461082100)
    ** Match Failure : could not fetch consumer to fuse
[pattern-application:1] PatternApplicator.cpp:224  -> matchAndRewrite failed
  } -> failure : pattern failed to match
} -> failure : all patterns failed to match
//===-------------------------------------------===//

Steps to reproduce your issue

Apply

iree-opt --pass-pipeline="builtin.module(func.func(iree-codegen-combine-layout-transformation))" --debug

to:

func.func @conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async_dispatch_0_conv_16x225x225x64x3x64_bf16xbf16xf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = LLVMGPUTileAndFuse workgroup_size = [256, 1, 1] subgroup_size = 64, {gpu_pipeline_options = #iree_gpu.pipeline_options<prefetch_shared_memory = true, use_igemm_convolution = true>, llvm_func_attrs = {"amdgpu-waves-per-eu" = "2"}}>} {
  %c256 = arith.constant 256 : index
  %c512 = arith.constant 512 : index
  %c3 = arith.constant 3 : index
  %c1 = arith.constant 1 : index
  %cst = arith.constant 0.000000e+00 : bf16
  %cst_0 = arith.constant 0.000000e+00 : f32
  %c0 = arith.constant 0 : index
  %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>>
  %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>>
  %2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(2) alignment(64) offset(%c0) flags(Indirect) {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
  %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>> -> tensor<16x225x225x64xbf16>
  %4 = iree_tensor_ext.dispatch.tensor.load %1, offsets = [0, 0], sizes = [64, 192], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>> -> tensor<64x192xbf16>
  %5 = tensor.empty() : tensor<16x225x225x64xbf16>
  %6 = scf.forall (%arg0, %arg1, %arg2) in (4, 225, 15) shared_outs(%arg3 = %5) -> (tensor<16x225x225x64xbf16>) {
    %7 = affine.apply affine_map<(d0) -> (d0 * 16)>(%arg2)
    %8 = affine.apply affine_map<(d0) -> (d0 * 4)>(%arg0)
    %9 = affine.min affine_map<(d0) -> (d0 * -16 + 225, 16)>(%arg2)
    %extracted_slice = tensor.extract_slice %arg3[%8, %arg1, %7, 0] [4, 1, %9, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<4x1x?x64xbf16>
    %10 = affine.min affine_map<(d0) -> (d0 * -16 + 225, 0, 16)>(%arg2)
    %11 = affine.min affine_map<(d0, d1) -> (16, d0 - d1)>(%9, %10)
    %12 = bufferization.alloc_tensor() {memory_space = #gpu.address_space<workgroup>} : tensor<4x16x1x64xbf16>
    %13 = bufferization.alloc_tensor() {memory_space = #gpu.address_space<workgroup>} : tensor<64x64xbf16>
    %14 = tensor.empty(%9) : tensor<4x1x1x?x4x16xf32>
    %15 = scf.forall (%arg4, %arg5, %arg6, %arg7, %arg8) in (1, 1, 1, 4, 64) shared_outs(%arg9 = %14) -> (tensor<4x1x1x?x4x16xf32>) {
      %17:3 = affine.delinearize_index %arg8 into (4, 16) : index, index, index
      %18 = affine.linearize_index disjoint [%17#1, %c0] by (4, 4) : index
      %19 = affine.max affine_map<(d0, d1) -> (d0 - d1, 0)>(%9, %18)
      %20 = affine.min affine_map<(d0) -> (d0, 4)>(%19)
      %21 = affine.max affine_map<(d0) -> (-d0 + 4, 0)>(%arg7)
      %22 = affine.min affine_map<(d0) -> (d0, 1)>(%21)
      %23 = affine.max affine_map<(d0) -> (-d0 + 16, 0)>(%17#2)
      %24 = affine.min affine_map<(d0) -> (d0, 1)>(%23)
      %25 = tensor.empty() : tensor<4x1x1x1x4x1xf32>
      %26 = linalg.fill ins(%cst_0 : f32) outs(%25 : tensor<4x1x1x1x4x1xf32>) -> tensor<4x1x1x1x4x1xf32>
      %27 = scf.for %arg10 = %c0 to %c3 step %c1 iter_args(%arg11 = %26) -> (tensor<4x1x1x1x4x1xf32>) {
        %31 = iree_gpu.barrier_region ins(%12 : tensor<4x16x1x64xbf16>) {
        ^bb0(%arg12: tensor<4x16x1x64xbf16>):
          %36 = scf.for %arg13 = %c0 to %c512 step %c256 iter_args(%arg14 = %arg12) -> (tensor<4x16x1x64xbf16>) {
            %37 = affine.apply affine_map<(d0, d1, d2, d3, d4, d5) -> (d0 + d1 + d2 * 64 + d3 * 256 + d4 * 256 + d5 * 256)>(%arg13, %arg8, %arg7, %c0, %c0, %c0)
            %38:4 = affine.delinearize_index %37 into (4, 16, 1, 8) : index, index, index, index
            %39 = affine.apply affine_map<(d0) -> (d0 * 8)>(%38#3)
            %extracted_slice_7 = tensor.extract_slice %arg14[%38#0, %38#1, 0, %39] [1, 1, 1, 8] [1, 1, 1, 1] : tensor<4x16x1x64xbf16> to tensor<1x1x1x8xbf16>
            %40 = affine.min affine_map<(d0, d1, d2) -> (16, d1 - d2, d0)>(%38#1, %9, %10)
            %41 = affine.min affine_map<(d0, d1) -> (1, d0 - d1)>(%11, %40)
            %42 = affine.apply affine_map<(d0) -> (-d0 + 1)>(%41)
            %43 = affine.apply affine_map<(d0)[s0] -> (d0 * 4 + s0)>(%arg0)[%38#0]
            %44 = affine.apply affine_map<(d0)[s0, s1] -> (d0 * 16 + s0 + s1)>(%arg2)[%40, %10]
            %extracted_slice_8 = tensor.extract_slice %3[%43, 0, %44, 0] [1, 225, %41, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<1x225x?x64xbf16>
            %padded = tensor.pad %extracted_slice_8 low[0, 1, 0, 0] high[0, 1, 0, 0] {
            ^bb0(%arg15: index, %arg16: index, %arg17: index, %arg18: index):
              tensor.yield %cst : bf16
            } : tensor<1x225x?x64xbf16> to tensor<1x227x?x64xbf16>
            %45 = tensor.empty(%41) : tensor<1x?x1x8xbf16>
            %46 = affine.apply affine_map<(d0, d1) -> (d0 * 64 + d1 * 8)>(%arg10, %38#3)
            %47 = iree_linalg_ext.im2col strides = [1] dilations = [1] kernel_size = [3] m_offset = [%arg1] * [1] k_offset = [%46] * [1] batch_pos = [0, 2] m_pos = [1] k_pos = [3] input_k_perm = [0, 1] ins(%padded : tensor<1x227x?x64xbf16>) outs(%45 : tensor<1x?x1x8xbf16>) -> tensor<1x?x1x8xbf16>
            %padded_9 = tensor.pad %47 low[0, 0, 0, 0] high[0, %42, 0, 0] {
            ^bb0(%arg15: index, %arg16: index, %arg17: index, %arg18: index):
              tensor.yield %cst : bf16
            } : tensor<1x?x1x8xbf16> to tensor<1x1x1x8xbf16>
            %48 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%padded_9 : tensor<1x1x1x8xbf16>) outs(%extracted_slice_7 : tensor<1x1x1x8xbf16>) -> tensor<1x1x1x8xbf16>
            %inserted_slice = tensor.insert_slice %48 into %arg14[%38#0, %38#1, 0, %39] [1, 1, 1, 8] [1, 1, 1, 1] : tensor<1x1x1x8xbf16> into tensor<4x16x1x64xbf16>
            scf.yield %inserted_slice : tensor<4x16x1x64xbf16>
          } {unroll_loop}
          iree_gpu.yield %36 : tensor<4x16x1x64xbf16>
        } : tensor<4x16x1x64xbf16>
        %32 = iree_gpu.barrier_region ins(%13 : tensor<64x64xbf16>) {
        ^bb0(%arg12: tensor<64x64xbf16>):
          %36 = scf.for %arg13 = %c0 to %c512 step %c256 iter_args(%arg14 = %arg12) -> (tensor<64x64xbf16>) {
            %37 = affine.apply affine_map<(d0, d1, d2, d3, d4, d5) -> (d0 + d1 + d2 * 64 + d3 * 256 + d4 * 256 + d5 * 256)>(%arg13, %arg8, %arg7, %c0, %c0, %c0)
            %38:2 = affine.delinearize_index %37 into (64, 8) : index, index
            %39 = affine.apply affine_map<(d0) -> (d0 * 8)>(%38#1)
            %extracted_slice_7 = tensor.extract_slice %arg14[%38#0, %39] [1, 8] [1, 1] : tensor<64x64xbf16> to tensor<1x8xbf16>
            %40 = affine.apply affine_map<(d0, d1) -> (d0 * 64 + d1 * 8)>(%arg10, %38#1)
            %extracted_slice_8 = tensor.extract_slice %4[%38#0, %40] [1, 8] [1, 1] : tensor<64x192xbf16> to tensor<1x8xbf16>
            %41 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%extracted_slice_8 : tensor<1x8xbf16>) outs(%extracted_slice_7 : tensor<1x8xbf16>) -> tensor<1x8xbf16>
            %inserted_slice = tensor.insert_slice %41 into %arg14[%38#0, %39] [1, 8] [1, 1] : tensor<1x8xbf16> into tensor<64x64xbf16>
            scf.yield %inserted_slice : tensor<64x64xbf16>
          } {unroll_loop}
          iree_gpu.yield %36 : tensor<64x64xbf16>
        } : tensor<64x64xbf16>
        %expanded = tensor.expand_shape %32 [[0, 1], [2, 3]] output_shape [4, 16, 4, 16] : tensor<64x64xbf16> into tensor<4x16x4x16xbf16>
        %extracted_slice_2 = tensor.extract_slice %expanded[%arg7, %17#2, 0, %18] [1, 1, 4, 4] [1, 1, 1, 1] : tensor<4x16x4x16xbf16> to tensor<1x1x4x4xbf16>
        %expanded_3 = tensor.expand_shape %31 [[0], [1, 2], [3], [4, 5]] output_shape [4, 1, 16, 1, 4, 16] : tensor<4x16x1x64xbf16> into tensor<4x1x16x1x4x16xbf16>
        %extracted_slice_4 = tensor.extract_slice %expanded_3[0, 0, %17#2, 0, 0, %18] [4, 1, 1, 1, 4, 4] [1, 1, 1, 1, 1, 1] : tensor<4x1x16x1x4x16xbf16> to tensor<4x1x1x1x4x4xbf16>
        %33 = tensor.empty() : tensor<4x1x1x4x1x4xbf16>
        %transposed_5 = linalg.transpose ins(%extracted_slice_4 : tensor<4x1x1x1x4x4xbf16>) outs(%33 : tensor<4x1x1x4x1x4xbf16>) permutation = [0, 1, 3, 4, 2, 5] 
        %34 = tensor.empty() : tensor<1x4x1x4xbf16>
        %transposed_6 = linalg.transpose ins(%extracted_slice_2 : tensor<1x1x4x4xbf16>) outs(%34 : tensor<1x4x1x4xbf16>) permutation = [0, 2, 1, 3] 
        %35 = iree_codegen.inner_tiled ins(%transposed_5, %transposed_6) outs(%arg11) {indexing_maps = [affine_map<(d0, d1, d2, d3, d4) -> (d0, d2, d1, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d3, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3)>], iterator_types = [#linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<reduction>], kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, lowering_config = #iree_gpu.lowering_config<{mma_kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, padding = [4, 1, 16, 64, 64], promote_operands = [0, 1], reduction = [0, 0, 0, 0, 4], subgroup = [4, 1, 1, 1, 0], subgroup_m_count = 1 : i64, subgroup_n_count = 4 : i64, workgroup = [4, 1, 16, 64, 0]}>} : tensor<4x1x1x4x1x4xbf16>, tensor<1x4x1x4xbf16> into tensor<4x1x1x1x4x1xf32>
        scf.yield %35 : tensor<4x1x1x1x4x1xf32>
      }
      %28 = tensor.empty() : tensor<4x1x1x4x1x1xf32>
      %transposed = linalg.transpose ins(%27 : tensor<4x1x1x1x4x1xf32>) outs(%28 : tensor<4x1x1x4x1x1xf32>) permutation = [0, 1, 2, 4, 3, 5] 
      %29 = tensor.empty() : tensor<4x1x1x4x1x1xf32>
      %30 = linalg.copy ins(%transposed : tensor<4x1x1x4x1x1xf32>) outs(%29 : tensor<4x1x1x4x1x1xf32>) -> tensor<4x1x1x4x1x1xf32>
      %extracted_slice_1 = tensor.extract_slice %30[0, 0, 0, 0, 0, 0] [4, 1, 1, %20, %22, %24] [1, 1, 1, 1, 1, 1] : tensor<4x1x1x4x1x1xf32> to tensor<4x1x1x?x?x?xf32>
      scf.forall.in_parallel {
        tensor.parallel_insert_slice %extracted_slice_1 into %arg9[0, 0, 0, %18, %arg7, %17#2] [4, 1, 1, %20, %22, %24] [1, 1, 1, 1, 1, 1] : tensor<4x1x1x?x?x?xf32> into tensor<4x1x1x?x4x16xf32>
      }
    } {mapping = [#gpu.thread<linear_dim_4>, #gpu.thread<linear_dim_3>, #gpu.thread<linear_dim_2>, #gpu.thread<linear_dim_1>, #gpu.thread<linear_dim_0>]}
    %collapsed = tensor.collapse_shape %15 [[0], [1], [2, 3], [4, 5]] : tensor<4x1x1x?x4x16xf32> into tensor<4x1x?x64xf32>
    %16 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%collapsed : tensor<4x1x?x64xf32>) outs(%extracted_slice : tensor<4x1x?x64xbf16>) {
    ^bb0(%in: f32, %out: bf16):
      %17 = arith.truncf %in : f32 to bf16
      linalg.yield %17 : bf16
    } -> tensor<4x1x?x64xbf16>
    scf.forall.in_parallel {
      tensor.parallel_insert_slice %16 into %arg3[%8, %arg1, %7, 0] [4, 1, %9, 64] [1, 1, 1, 1] : tensor<4x1x?x64xbf16> into tensor<16x225x225x64xbf16>
    }
  } {mapping = [#iree_codegen.workgroup_mapping<z>, #iree_codegen.workgroup_mapping<y>, #iree_codegen.workgroup_mapping<x>]}
  iree_tensor_ext.dispatch.tensor.store %6, %2, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : tensor<16x225x225x64xbf16> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
  return
}

What component(s) does this issue relate to?

Compiler

Version information

I haven't bisected a commit yet, but I'm looking at 9323cd2 and the issue is present.

Will update if I find an earlier commit.

Additional context

No response

Activity

  1. added
    bug 🐞
    on Aug 7, 2025
  2. self-assigned this
    on Aug 7, 2025
  3. nirvedhmeshram commented on Aug 7, 2025

    @nirvedhmeshram
    Contributor

    I dont have broarder context here but after some debugging it seems this section of code https://github.com/iree-org/iree/blob/main/compiler/src/iree/compiler/Codegen/Common/Transforms.cpp#L75-L91 is causing trouble and in this specific case is not necessary. I noticed without it we are able to fuse the consumer into the thread loop @Max191

  4. hanhanW commented on Aug 8, 2025

    @hanhanW
    Contributor

    I also hit the hang issue after we enable more fusion in data-tiling fusion; reverting 9c77258 fixes the issue.

  5. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    Thanks for investigating a bit guys, we can revert the commit for now and I will triage the issue. The commit was just for encoding fusions with multi-result dispatches, which is not on by default right now anyway.

  6. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    Revert PR: #21632

  7. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    Taking a look at this now. As a separate question, @zjgarvey could you also share the full original dump before the hang? The input IR that you shared looks off, since the trunc is happening after all the unpacking, so I'm curious how we got this IR.

  8. zjgarvey commented on Aug 8, 2025

    @zjgarvey
    ContributorAuthor

    @Max191 Here is the IR dump, the source IR, and a compile command.

    Notably, without the tuning specs, it will actually fail compilation instead of hanging.

    /home/zjgar/code/iree-turbine/boo_cache_0/conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g/conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g.mlir:6:3: error: function 'conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async_dispatch_0_conv_16x225x225x64x3x64_bf16xbf16xf32' uses 77824 bytes of shared memory; exceeded the limit of 65536 bytes
      util.func public @conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async(%arg0: !hal.buffer_view, %arg1: !hal.buffer_view, %arg2: !hal.fence, %arg3: !hal.fence) -> !hal.buffer_view attributes {inlining_policy = #util.inline.never, iree.abi.model = "coarse-fences", iree.abi.stub, preprocessing_pipeline = #util.preprocessing_pipeline<"iree-preprocessing-make-single-dispatch">} {
      ^
    /home/zjgar/code/iree-turbine/boo_cache_0/conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g/conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g.mlir:6:3: error: failed to run translation of source executable to target executable for backend #hal.executable.target<"rocm", "rocm-hsaco-fb", {abi = "hip", iree.gpu.target = #iree_gpu.target<arch = "gfx942", features = "", wgp = <compute =  fp64|fp32|fp16|int64|int32|int16|int8, storage =  b64|b32|b16|b8, subgroup =  shuffle|arithmetic, dot =  dp4xi8toi32, mma = [<MFMA_F32_16x16x16_BF16>, <MFMA_F32_32x32x8_BF16>, <MFMA_F32_16x16x32_F8E5M2FNUZ>, <MFMA_F32_16x16x32_F8E5M2FNUZ_F8E4M3FNUZ>, <MFMA_F32_16x16x32_F8E4M3FNUZ>, <MFMA_F32_16x16x32_F8E4M3FNUZ_F8E5M2FNUZ>, <MFMA_F32_32x32x16_F8E5M2FNUZ>, <MFMA_F32_32x32x16_F8E5M2FNUZ_F8E4M3FNUZ>, <MFMA_F32_32x32x16_F8E4M3FNUZ>, <MFMA_F32_32x32x16_F8E4M3FNUZ_F8E5M2FNUZ>, <MFMA_I32_16x16x32_I8>, <MFMA_I32_32x32x16_I8>, <MFMA_F64_16x16x4_F64>, <MFMA_F32_16x16x4_F32>, <MFMA_F32_16x16x16_F16>, <MFMA_F32_32x32x8_F16>], scaled_mma = [], subgroup_size_choices = [64], max_workgroup_sizes = [1024, 1024, 1024], max_thread_count_per_workgroup = 1024, max_workgroup_memory_bytes = 65536, max_workgroup_counts = [2147483647, 2147483647, 2147483647], max_load_instruction_bits = 128, simds_per_wgp = 4, vgpr_space_bits = 16384>, chip = <wgp_count = 304, sku = "mi300x">>, iree_codegen.default_tuning_spec = #rocm.builtin.tuning_module<"iree_default_tuning_spec_mi300x.mlir">, ukernels = "none"}>
      util.func public @conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async(%arg0: !hal.buffer_view, %arg1: !hal.buffer_view, %arg2: !hal.fence, %arg3: !hal.fence) -> !hal.buffer_view attributes {inlining_policy = #util.inline.never, iree.abi.model = "coarse-fences", iree.abi.stub, preprocessing_pipeline = #util.preprocessing_pipeline<"iree-preprocessing-make-single-dispatch">} {
      ^
    
  9. hanhanW commented on Aug 8, 2025

    @hanhanW
    Contributor

    @Max191 do you need a repro from me? The IR could be simpler because it happens in the first distribution level.

  10. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    Ok, this looks like something that we were aware of, but hadn't seen yet until now. This is the dump after GPUPackToIntrinsics:

    // -----// IR Dump Before DecomposeBoundaryPackUnPackOpsPass (iree-codegen-decompose-boundary-pack-unpack-ops) //----- //
    func.func @conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async_dispatch_0_conv_16x225x225x64x3x64_bf16xbf16xf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = LLVMGPUTileAndFuse workgroup_size = [256, 1, 1] subgroup_size = 64, {gpu_pipeline_options = #iree_gpu.pipeline_options<prefetch_shared_memory = true, use_igemm_convolution = true>, llvm_func_attrs = {"amdgpu-waves-per-eu" = "2"}}>} {
      %c2 = arith.constant 2 : index
      %c1 = arith.constant 1 : index
      %cst = arith.constant 0.000000e+00 : bf16
      %cst_0 = arith.constant 0.000000e+00 : f32
      %c0 = arith.constant 0 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>>
      %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>>
      %2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(2) alignment(64) offset(%c0) flags(Indirect) {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
      %3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>> -> tensor<16x225x225x64xbf16>
      %4 = iree_tensor_ext.dispatch.tensor.load %1, offsets = [0, 0], sizes = [64, 192], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>> -> tensor<64x192xbf16>
      %5 = tensor.empty() : tensor<16x225x225x64xbf16>
      %6 = scf.forall (%arg0, %arg1, %arg2) = (0, 0, 0) to (16, 225, 225) step (4, 1, 16) shared_outs(%arg3 = %5) -> (tensor<16x225x225x64xbf16>) {
        %7 = affine.min affine_map<(d0) -> (-d0 + 225, 16)>(%arg2)
        %extracted_slice = tensor.extract_slice %3[%arg0, 0, %arg2, 0] [4, 225, %7, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<4x225x?x64xbf16>
        %padded = tensor.pad %extracted_slice low[0, 1, 0, 0] high[0, 1, 0, 0] {
        ^bb0(%arg4: index, %arg5: index, %arg6: index, %arg7: index):
          tensor.yield %cst : bf16
        } : tensor<4x225x?x64xbf16> to tensor<4x227x?x64xbf16>
        %8 = tensor.empty(%7) : tensor<4x?x1x192xbf16>
        %9 = iree_linalg_ext.im2col strides = [1] dilations = [1] kernel_size = [3] m_offset = [%arg1] * [1] k_offset = [0] * [1] batch_pos = [0, 2] m_pos = [1] k_pos = [3] input_k_perm = [0, 1] ins(%padded : tensor<4x227x?x64xbf16>) outs(%8 : tensor<4x?x1x192xbf16>) -> tensor<4x?x1x192xbf16>
        %10 = tensor.empty(%7) : tensor<4x1x?x64xf32>
        %11 = linalg.fill ins(%cst_0 : f32) outs(%10 : tensor<4x1x?x64xf32>) -> tensor<4x1x?x64xf32>
        %dim = tensor.dim %9, %c1 : tensor<4x?x1x192xbf16>
        %12 = affine.apply affine_map<(d0) -> (-d0 + 16)>(%dim)
        %padded_1 = tensor.pad %9 low[0, 0, 0, 0] high[0, %12, 0, 0] {
        ^bb0(%arg4: index, %arg5: index, %arg6: index, %arg7: index):
          tensor.yield %cst : bf16
        } : tensor<4x?x1x192xbf16> to tensor<4x16x1x192xbf16>
        %dim_2 = tensor.dim %11, %c2 : tensor<4x1x?x64xf32>
        %13 = affine.apply affine_map<(d0) -> (-d0 + 16)>(%dim_2)
        %padded_3 = tensor.pad %11 low[0, 0, 0, 0] high[0, 0, %13, 0] {
        ^bb0(%arg4: index, %arg5: index, %arg6: index, %arg7: index):
          tensor.yield %cst_0 : f32
        } : tensor<4x1x?x64xf32> to tensor<4x1x16x64xf32>
        %dim_4 = tensor.dim %9, %c1 : tensor<4x?x1x192xbf16>
        %14 = tensor.empty() : tensor<4x16x1x192xbf16>
        %15 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%padded_1 : tensor<4x16x1x192xbf16>) outs(%14 : tensor<4x16x1x192xbf16>) -> tensor<4x16x1x192xbf16>
        %16 = tensor.empty() : tensor<64x192xbf16>
        %17 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%4 : tensor<64x192xbf16>) outs(%16 : tensor<64x192xbf16>) -> tensor<64x192xbf16>
        %18 = tensor.empty() : tensor<4x1x1x12x16x16xbf16>
        %pack = linalg.pack %15 inner_dims_pos = [1, 3] inner_tiles = [16, 16] into %18 : tensor<4x16x1x192xbf16> -> tensor<4x1x1x12x16x16xbf16>
        %19 = tensor.empty() : tensor<4x12x16x16xbf16>
        %pack_5 = linalg.pack %17 inner_dims_pos = [0, 1] inner_tiles = [16, 16] into %19 : tensor<64x192xbf16> -> tensor<4x12x16x16xbf16>
        %20 = tensor.empty() : tensor<4x1x1x4x16x16xf32>
        %pack_6 = linalg.pack %padded_3 inner_dims_pos = [2, 3] inner_tiles = [16, 16] into %20 : tensor<4x1x16x64xf32> -> tensor<4x1x1x4x16x16xf32>
        %21 = iree_codegen.inner_tiled ins(%pack, %pack_5) outs(%pack_6) {indexing_maps = [affine_map<(d0, d1, d2, d3, d4) -> (d0, d2, d1, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d3, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3)>], iterator_types = [#linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<reduction>], kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, lowering_config = #iree_gpu.lowering_config<{mma_kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, padding = [4, 1, 16, 64, 64], promote_operands = [0, 1], reduction = [0, 0, 0, 0, 4], subgroup = [4, 1, 1, 1, 0], subgroup_m_count = 1 : i64, subgroup_n_count = 4 : i64, workgroup = [4, 1, 16, 64, 0]}>, permutations = [array<i64: 0, 1>, array<i64: 1, 0>, array<i64: 0, 1>]} : tensor<4x1x1x12x16x16xbf16>, tensor<4x12x16x16xbf16> into tensor<4x1x1x4x16x16xf32>
        %unpack = linalg.unpack %21 inner_dims_pos = [2, 3] inner_tiles = [16, 16] into %padded_3 : tensor<4x1x1x4x16x16xf32> -> tensor<4x1x16x64xf32>
        %extracted_slice_7 = tensor.extract_slice %unpack[0, 0, 0, 0] [4, 1, %dim_4, 64] [1, 1, 1, 1] : tensor<4x1x16x64xf32> to tensor<4x1x?x64xf32>
        %extracted_slice_8 = tensor.extract_slice %arg3[%arg0, %arg1, %arg2, 0] [4, 1, %7, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<4x1x?x64xbf16>
        %22 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%extracted_slice_7 : tensor<4x1x?x64xf32>) outs(%extracted_slice_8 : tensor<4x1x?x64xbf16>) {
        ^bb0(%in: f32, %out: bf16):
          %23 = arith.truncf %in : f32 to bf16
          linalg.yield %23 : bf16
        } -> tensor<4x1x?x64xbf16>
        scf.forall.in_parallel {
          tensor.parallel_insert_slice %22 into %arg3[%arg0, %arg1, %arg2, 0] [4, 1, %7, 64] [1, 1, 1, 1] : tensor<4x1x?x64xbf16> into tensor<16x225x225x64xbf16>
        }
      } {mapping = [#iree_codegen.workgroup_mapping<z>, #iree_codegen.workgroup_mapping<y>, #iree_codegen.workgroup_mapping<x>]}
      iree_tensor_ext.dispatch.tensor.store %6, %2, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : tensor<16x225x225x64xbf16> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
      return
    }

    The problem is that the unpack and extract_slice cannot fold into each other anymore:

    %unpack = linalg.unpack %21 inner_dims_pos = [2, 3] inner_tiles = [16, 16] into %padded_3 : tensor<4x1x1x4x16x16xf32> -> tensor<4x1x16x64xf32>
    %extracted_slice_7 = tensor.extract_slice %unpack[0, 0, 0, 0] [4, 1, %dim_4, 64] [1, 1, 1, 1] : tensor<4x1x16x64xf32> to tensor<4x1x?x64xf32>

    This is because of the recently updated semantics of unpack: llvm/llvm-project#150272

    We will need to implement some propagation patterns for tensor.extract_slice ops through consumer ops, but it is tricky because extract_slice is a special op with respect to tiling, so we have to be careful. I will think about a reasonable way to handle this.

  11. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    @Max191 do you need a repro from me? The IR could be simpler because it happens in the first distribution level.

    No, the one Zach shared is good enough. I see what went wrong there. I also have a fix for the original issue with the hang, which I'll send out shortly.

  12. Max191 commented on Aug 8, 2025

    @Max191
    Contributor

    This reapplies the commit with the fix: #21637

  13. Max191 commented on Aug 12, 2025

    @Max191
    Contributor

    This is fixed now

  14. Max191 commented on Aug 12, 2025

    @Max191
    Contributor

    There was another problem that this issue brought up (unpack propagation not happening), but that is tracked in another issue: #21635

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

Metadata

Metadata

Labels

bug 🐞

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions