Skip to content

[CPU] Incorrect result with CPU for onnx ReverseSequence #20011

Closed

Description

@IanWood1

What happened?

I added a folding pattern in #20006 which caused a onnx test to fail. I was able to create a minimal reproducer that shows the difference between correct and incorrect results by placing the tensor.extract_slice in a dispatch so that it must be copied. It seems that in the bad accessing the tensor along the innermost dimension instead of the outermost. The "incorrect" example produces correct results when run on rocm.

Steps to reproduce your issue

  1. Save the reproducers. They are functionally equivalent but produce different results.

Incorrect Results

util.func public @main(%0 : tensor<4x4xf32>) -> (tensor<?xf32>, index) {
  %13 = arith.constant 4 : index
  %extracted_slice_0 = tensor.extract_slice %0[0, 0] [%13, 2] [1, 1] : tensor<4x4xf32> to tensor<?x2xf32>
  %c1 = arith.constant 1 : index
  %c0 = arith.constant 0 : index
  %14 = arith.subi %13, %c1 : index
  %15 = tensor.empty(%13) : tensor<?xf32>
  %16 = flow.dispatch.region -> (tensor<?xf32>{%13}) {
    %61 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} outs(%15 : tensor<?xf32>) {
    ^bb0(%out: f32):
      %62 = linalg.index 0 : index
      %63 = arith.subi %14, %62 : index
      %extracted_20 = tensor.extract %extracted_slice_0[%63, %c0] : tensor<?x2xf32>
      linalg.yield %extracted_20 : f32
    } -> tensor<?xf32>
    flow.return %61 : tensor<?xf32>
  }
  util.return %16, %13 : tensor<?xf32>, index
}

Correct Results

util.func public @main(%0 : tensor<4x4xf32>) -> (tensor<?xf32>, index) {
  %13 = arith.constant 4 : index
  %reg = flow.dispatch.region -> (tensor<?x2xf32>{%13}) {
   %extracted_slice_0 = tensor.extract_slice %0[0, 0] [%13, 2] [1, 1] : tensor<4x4xf32> to tensor<?x2xf32>
   flow.return %extracted_slice_0 : tensor<?x2xf32>
  }

  %c1 = arith.constant 1 : index
  %c0 = arith.constant 0 : index
  %14 = arith.subi %13, %c1 : index
  %15 = tensor.empty(%13) : tensor<?xf32>
  %16 = flow.dispatch.region -> (tensor<?xf32>{%13}) {
    %61 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} outs(%15 : tensor<?xf32>) {
    ^bb0(%out: f32):
      %62 = linalg.index 0 : index
      %63 = arith.subi %14, %62 : index
      %extracted_20 = tensor.extract %reg[%63, %c0] : tensor<?x2xf32>
      linalg.yield %extracted_20 : f32
    } -> tensor<?xf32>
    flow.return %61 : tensor<?xf32>
  }
  util.return %16, %13 : tensor<?xf32>, index
}
  1. Compile, run and compare
## Incorrect
iree-compile repro.bad.mlir --iree-hal-target-backends=llvm-cpu -o model_cpu_llvm_sync.vmfb
iree-run-module --module=model_cpu_llvm_sync.vmfb --device=local-sync --input=4x4xf32=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

## Output
EXEC @main
result[0]: hal.buffer_view
4xf32=6 4 2 0
result[1]: i64=4
## Correct
iree-compile repro.good.mlir --iree-hal-target-backends=llvm-cpu -o model_cpu_llvm_sync.vmfb
iree-run-module --module=model_cpu_llvm_sync.vmfb --device=local-sync --input=4x4xf32=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

## Output
EXEC @main
result[0]: hal.buffer_view
4xf32=12 8 4 0
result[1]: i64=4

What component(s) does this issue relate to?

Compiler

Version information

cdba184

Additional context

No response

Activity

  1. added
    bug 🐞
    on Feb 17, 2025
  2. IanWood1 commented on Feb 18, 2025

    @IanWood1
    MemberAuthor

    Seems related to #19749

  3. IanWood1 commented on Feb 18, 2025

    @IanWood1
    MemberAuthor

    There is a subview without strides (but the result type has strides) that is getting folded away. I think this is the cause

     %subview = memref.subview %0[0, 0] [4, 2] [1, 1] : memref<4x4xf32, #hal.descriptor_type<storage_buffer>> to memref<4x2xf32, strided<[4, 1]>, #hal.descriptor_type<storage_buffer>>
  4. hanhanW commented on Feb 18, 2025

    @hanhanW
    Contributor

    Good find! I can reproduce what you meant. Inline the IR for better visibility. Yes, it looks like the subview (with strided) is getting folded away, and the strides are not handled.

    // -----// IR Dump After VectorTransferLoweringPass (iree-codegen-vector-transfer-lowering) //----- //
    func.func @main_dispatch_0_elementwise_broadcast_4_f32() {
      %cst = arith.constant dense<2> : vector<4xindex>
      %cst_0 = arith.constant dense<0.000000e+00> : vector<4xf32>
      %cst_1 = arith.constant dense<3> : vector<4xindex>
      %c0 = arith.constant 0 : index
      %c4 = arith.constant 4 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : memref<4x4xf32>
      memref.assume_alignment %0, 64 : memref<4x4xf32>
      %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : memref<?xf32>{%c4}
      memref.assume_alignment %1, 64 : memref<?xf32>
      %subview = memref.subview %0[0, 0] [4, 2] [1, 1] : memref<4x4xf32> to memref<4x2xf32, strided<[4, 1]>>
      %2 = vector.step : vector<4xindex>
      %3 = arith.subi %cst_1, %2 : vector<4xindex>
      %4 = arith.muli %3, %cst : vector<4xindex>
      %5 = vector.extract %4[0] : index from vector<4xindex>
      %6 = vector.load %subview[%c0, %5] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %7 = vector.extract %6[0] : f32 from vector<1xf32>
      %8 = vector.insert %7, %cst_0 [0] : f32 into vector<4xf32>
      %9 = vector.extract %4[1] : index from vector<4xindex>
      %10 = vector.load %subview[%c0, %9] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %11 = vector.extract %10[0] : f32 from vector<1xf32>
      %12 = vector.insert %11, %8 [1] : f32 into vector<4xf32>
      %13 = vector.extract %4[2] : index from vector<4xindex>
      %14 = vector.load %subview[%c0, %13] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %15 = vector.extract %14[0] : f32 from vector<1xf32>
      %16 = vector.insert %15, %12 [2] : f32 into vector<4xf32>
      %17 = vector.extract %4[3] : index from vector<4xindex>
      %18 = vector.load %subview[%c0, %17] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %19 = vector.extract %18[0] : f32 from vector<1xf32>
      %20 = vector.insert %19, %16 [3] : f32 into vector<4xf32>
      vector.store %20, %1[%c0] : memref<?xf32>, vector<4xf32>
      return
    }
    
    // -----// IR Dump After FoldMemRefAliasOps (fold-memref-alias-ops) //----- //
    func.func @main_dispatch_0_elementwise_broadcast_4_f32() {
      %cst = arith.constant dense<2> : vector<4xindex>
      %cst_0 = arith.constant dense<0.000000e+00> : vector<4xf32>
      %cst_1 = arith.constant dense<3> : vector<4xindex>
      %c0 = arith.constant 0 : index
      %c4 = arith.constant 4 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : memref<4x4xf32>
      memref.assume_alignment %0, 64 : memref<4x4xf32>
      %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : memref<?xf32>{%c4}
      memref.assume_alignment %1, 64 : memref<?xf32>
      %2 = vector.step : vector<4xindex>
      %3 = arith.subi %cst_1, %2 : vector<4xindex>
      %4 = arith.muli %3, %cst : vector<4xindex>
      %5 = vector.extract %4[0] : index from vector<4xindex>
      %6 = vector.load %0[%c0, %5] : memref<4x4xf32>, vector<1xf32>
      %7 = vector.extract %6[0] : f32 from vector<1xf32>
      %8 = vector.insert %7, %cst_0 [0] : f32 into vector<4xf32>
      %9 = vector.extract %4[1] : index from vector<4xindex>
      %10 = vector.load %0[%c0, %9] : memref<4x4xf32>, vector<1xf32>
      %11 = vector.extract %10[0] : f32 from vector<1xf32>
      %12 = vector.insert %11, %8 [1] : f32 into vector<4xf32>
      %13 = vector.extract %4[2] : index from vector<4xindex>
      %14 = vector.load %0[%c0, %13] : memref<4x4xf32>, vector<1xf32>
      %15 = vector.extract %14[0] : f32 from vector<1xf32>
      %16 = vector.insert %15, %12 [2] : f32 into vector<4xf32>
      %17 = vector.extract %4[3] : index from vector<4xindex>
      %18 = vector.load %0[%c0, %17] : memref<4x4xf32>, vector<1xf32>
      %19 = vector.extract %18[0] : f32 from vector<1xf32>
      %20 = vector.insert %19, %16 [3] : f32 into vector<4xf32>
      vector.store %20, %1[%c0] : memref<?xf32>, vector<4xf32>
      return
    }
  5. hanhanW commented on Feb 19, 2025

    @hanhanW
    Contributor

    Taking a closer look, I think the issue might not be related to the strides? Looking at the vector.load pattern, the first index is always %c0, so the strides do not matter. They are always %c0 * %stride = %c0.

    %6 = vector.load %subview[%c0, %5] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
    %10 = vector.load %subview[%c0, %9] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
    %14 = vector.load %subview[%c0, %13] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
    %18 = vector.load %subview[%c0, %17] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
  6. hanhanW commented on Feb 19, 2025

    @hanhanW
    Contributor

    I think I know what's happening, but I do not have a solution at this moment.

    Starting from the input, there is a "strided tensor", which becomes the memref.subview with strides that @IanWood1 pointed out. %4 is a 4x2xf32 subtensor of the binding.

    func.func @main_dispatch_0_elementwise_broadcast_4_f32() {
      %c0 = arith.constant 0 : index
      %c3 = arith.constant 3 : index
      %c4 = arith.constant 4 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !
      %1 = flow.dispatch.workload.ordinal %c4, 0 : index
      %2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : !flow.dispat
      %3 = tensor.empty() : tensor<4xf32>
      %4 = flow.dispatch.tensor.load %0, offsets = [0, 0], sizes = [4, 2], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<4x4xf32>> -> tensor<4x2xf32>
      %5 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} outs(%3 : tensor<4xf32>) {
      ^bb0(%out: f32):
        %6 = linalg.index 0 : index
        %7 = arith.subi %c3, %6 : index
        %extracted = tensor.extract %4[%7, %c0] : tensor<4x2xf32>
        linalg.yield %extracted : f32
      } -> tensor<4xf32>
      %cast = tensor.cast %5 : tensor<4xf32> to tensor<?xf32>
      flow.dispatch.tensor.store %cast, %2, offsets = [0], sizes = [%1], strides = [1] : tensor<?xf32> -> !flow.dispatch.tensor<writeonly:tensor<?xf32>>{%1}
      return
    }

    Then we vectorize the gather op, it becomes the below snippet. The indices are ([3, 3, 3, 3] - [0, 1 ,2 ,3]) * 2 = [6, 4, 2, 0], where 2 is the dim size of the last dimension. This is okay if we look it at the memref level.

      %2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : !flow.dispat
      %3 = tensor.empty() : tensor<4xf32>
      %4 = flow.dispatch.tensor.load %0, offsets = [0, 0], sizes = [4, 2], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<4x4xf32>> -> tensor<4x2xf32>
      %5 = vector.step : vector<4xindex>
      %6 = arith.subi %cst_2, %5 : vector<4xindex>
      %7 = arith.muli %6, %cst : vector<4xindex>
      %8 = vector.gather %4[%c0, %c0] [%7], %cst_1, %cst_0 : tensor<4x2xf32>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32>

    At memref level, it becomes the below snippet. The strides are introduced.

      %subview = memref.subview %0[0, 0] [4, 2] [1, 1] : memref<4x4xf32, #hal.descriptor_type<storage_buffer>> to memref<4x2xf32, strided<[4, 1]>, #hal.descriptor_type<storage_buffer>>
      %3 = vector.step : vector<4xindex>
      %4 = arith.subi %cst_2, %3 : vector<4xindex>
      %5 = arith.muli %4, %cst : vector<4xindex>
      %6 = vector.gather %subview[%c0, %c0] [%5], %cst_1, %cst_0 : memref<4x2xf32, strided<[4, 1]>, #hal.descriptor_type<storage_buffer>>, vector<4xindex>, vector<4xi1>, vector<4xf32> into vector<4xf32>

    Then we lower it to a sequence of extract op and load op, which eventually becomes

    func.func @main_dispatch_0_elementwise_broadcast_4_f32() {
      %cst = arith.constant dense<2> : vector<4xindex>
      %cst_0 = arith.constant dense<0.000000e+00> : vector<4xf32>
      %cst_1 = arith.constant dense<3> : vector<4xindex>
      %c0 = arith.constant 0 : index
      %c4 = arith.constant 4 : index
      %0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : memref<4x4xf32>
      memref.assume_alignment %0, 64 : memref<4x4xf32>
      %1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags(Indirect) : memref<?xf32>{%c4}
      memref.assume_alignment %1, 64 : memref<?xf32>
      %subview = memref.subview %0[0, 0] [4, 2] [1, 1] : memref<4x4xf32> to memref<4x2xf32, strided<[4, 1]>>
      %2 = vector.step : vector<4xindex>
      %3 = arith.subi %cst_1, %2 : vector<4xindex>
      %4 = arith.muli %3, %cst : vector<4xindex>
      %5 = vector.extract %4[0] : index from vector<4xindex>
      %6 = vector.load %subview[%c0, %5] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %7 = vector.extract %6[0] : f32 from vector<1xf32>
      %8 = vector.insert %7, %cst_0 [0] : f32 into vector<4xf32>
      %9 = vector.extract %4[1] : index from vector<4xindex>
      %10 = vector.load %subview[%c0, %9] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %11 = vector.extract %10[0] : f32 from vector<1xf32>
      %12 = vector.insert %11, %8 [1] : f32 into vector<4xf32>
      %13 = vector.extract %4[2] : index from vector<4xindex>
      %14 = vector.load %subview[%c0, %13] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %15 = vector.extract %14[0] : f32 from vector<1xf32>
      %16 = vector.insert %15, %12 [2] : f32 into vector<4xf32>
      %17 = vector.extract %4[3] : index from vector<4xindex>
      %18 = vector.load %subview[%c0, %17] : memref<4x2xf32, strided<[4, 1]>>, vector<1xf32>
      %19 = vector.extract %18[0] : f32 from vector<1xf32>
      %20 = vector.insert %19, %16 [3] : f32 into vector<4xf32>
      vector.store %20, %1[%c0] : memref<?xf32>, vector<4xf32>
      return
    }

    The issue is that we somewhat linearize the gather indices during the vectorization. It looks fine if it is at tensor level. We also consider the strides when we lower the vector.gather op. Below is the snippet of vector.gather lowering. They all look fine. However, the indices are overflowed. They are [6, 4, 2, 0] in the instance, while the dim size is just 2. The compiler does not know that the indices are overflowed, so it ignores the strides. We have to scale the indices as 2x somewhere, IMO. I don't have a solution at this moment. (cc @Groverkss who looked at the issue)

      %3 = vector.step : vector<4xindex>
      %4 = arith.subi %cst_2, %3 : vector<4xindex>
      %5 = arith.muli %4, %cst : vector<4xindex>
      %6 = vector.extract %cst_1[0] : i1 from vector<4xi1>
      %7 = vector.extract %5[0] : index from vector<4xindex>
      %8 = scf.if %6 -> (vector<4xf32>) {
        %18 = vector.load %subview[%c0, %7] : memref<4x2xf32, strided<[4, 1]>, #hal.descriptor_type<storage_buffer>>, vector<1xf32>
        %19 = vector.extract %18[0] : f32 from vector<1xf32>
        %20 = vector.insert %19, %cst_0 [0] : f32 into vector<4xf32>
        scf.yield %20 : vector<4xf32>
      } else {
        scf.yield %cst_0 : vector<4xf32>
      }
      %9 = vector.extract %cst_1[1] : i1 from vector<4xi1>
      %10 = vector.extract %5[1] : index from vector<4xindex>
      %11 = scf.if %9 -> (vector<4xf32>) {
        %18 = vector.load %subview[%c0, %10] : memref<4x2xf32, strided<[4, 1]>, #hal.descriptor_type<storage_buffer>>, vector<1xf32>
        %19 = vector.extract %18[0] : f32 from vector<1xf32>
        %20 = vector.insert %19, %8 [1] : f32 into vector<4xf32>
        scf.yield %20 : vector<4xf32>
      } else {
        scf.yield %8 : vector<4xf32>
      }
  7. hanhanW commented on Mar 17, 2026

    @hanhanW
    Contributor

    Confirmed that it is fixed by llvm/llvm-project#184706, closing the issue.

  8. added a commit that references this issue on May 6, 2026
    f9cf809
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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions