Skip to content

[LLVMCPU] Gather codegen for gather on strided access is numerically incorrect #19749

Closed

Description

@Groverkss

Input IR:

module @module {
  func.func @main(%arg0: !torch.vtensor<[57,2,128],f16>, %arg1: !torch.vtensor<[2],si64>) -> !torch.vtensor<[2,128],f16> attributes {torch.assume_strict_symbolic_shapes} {
    %int0 = torch.constant.int 0
    %int0_0 = torch.constant.int 0
    %int9223372036854775807 = torch.constant.int 9223372036854775807
    %int1 = torch.constant.int 1
    %0 = torch.aten.slice.Tensor %arg0, %int0, %int0_0, %int9223372036854775807, %int1 : !torch.vtensor<[57,2,128],f16>, !torch.int, !torch.int, !torch.int, !torch.int -> !torch.vtensor<[57,2,128],f16>
    %int1_1 = torch.constant.int 1
    %int0_2 = torch.constant.int 0
    %1 = torch.aten.select.int %0, %int1_1, %int0_2 : !torch.vtensor<[57,2,128],f16>, !torch.int, !torch.int -> !torch.vtensor<[57,128],f16>
    %int0_3 = torch.constant.int 0
    %2 = torch.aten.index_select %1, %int0_3, %arg1 : !torch.vtensor<[57,128],f16>, !torch.int, !torch.vtensor<[2],si64> -> !torch.vtensor<[2,128],f16>
    return %2 : !torch.vtensor<[2,128],f16>
  }
}

Reproduce instructions:

  1. Compile:
iree-compile <input-file> --iree-hal-target-device=llvm-cpu --iree-llvmcpu-target-cpu=generic -o compiled.vmfb
  1. Generate inputs using numpy:
import numpy as np

cache = np.random.rand(57, 2, 128).astype(np.float16)
indices = np.arange(2, dtype=np.int64)
selected = cache[indices, 0]

np.save("cache.npy", cache)
np.save("indices.npy", indices)
np.save("expected.npy", selected)
  1. Run:
iree-run-module --module=compiled.vmfb [email protected] [email protected] [email protected]

IREE Commit: c320935

Activity

  1. added
    bug 🐞
    on Jan 21, 2025
  2. Groverkss commented on Jan 21, 2025

    @Groverkss
    ContributorAuthor

    After some investigation, it looks like this is coming from vector.gather virtual vector lowering.

    Before lowering:

    func.func @main$async_dispatch_0_elementwise_broadcast_2x128_i64xf16() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDoubleTilingExpert>} {
      %cst = arith.constant dense<128> : vector<4x1xindex>
      %cst_0 = arith.constant dense<0.000000e+00> : vector<1x4xf16>
      %cst_1 = arith.constant dense<true> : vector<1x4xi1>
      %c0_i64 = arith.constant 0 : i64
      %c4 = arith.constant 4 : index
      %c1 = arith.constant 1 : index
      %c128 = arith.constant 128 : index
      %c2 = arith.constant 2 : index
      %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") : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %0, 64 : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>>
      %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") : memref<2xi64, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %1, 64 : memref<2xi64, #hal.descriptor_type<storage_buffer>>
      %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) : memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %2, 64 : memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
      %subview = memref.subview %0[0, 0, 0] [57, 1, 128] [1, 1, 1] : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>> to memref<57x128xf16, strided<[256, 1]>, #hal.descriptor_type<storage_buffer>>
      %3 = vector.step : vector<4xindex>
      scf.for %arg0 = %c0 to %c2 step %c1 {
        %4 = vector.transfer_read %1[%arg0], %c0_i64 {in_bounds = [true]} : memref<2xi64, #hal.descriptor_type<storage_buffer>>, vector<1xi64>
        %5 = arith.index_cast %4 : vector<1xi64> to vector<1xindex>
        %6 = vector.broadcast %5 : vector<1xindex> to vector<4x1xindex>
        %7 = vector.shape_cast %6 : vector<4x1xindex> to vector<4xindex>
        %8 = vector.shape_cast %cst : vector<4x1xindex> to vector<4xindex>
        %9 = arith.muli %7, %8 : vector<4xindex>
        scf.for %arg1 = %c0 to %c128 step %c4 {
          %10 = vector.broadcast %arg1 : index to vector<4xindex>
          %11 = arith.addi %3, %10 : vector<4xindex>
          %12 = arith.addi %11, %9 : vector<4xindex>
          %13 = vector.shape_cast %12 : vector<4xindex> to vector<1x4xindex>
          %14 = vector.gather %subview[%c0, %c0] [%13], %cst_1, %cst_0 : memref<57x128xf16, strided<[256, 1]>, #hal.descriptor_type<storage_buffer>>, vector<1x4xindex>, vector<1x4xi1>, vector<1x4xf16> into vector<1x4xf16>
          %15 = vector.extract %14[0] : vector<4xf16> from vector<1x4xf16>
          vector.transfer_write %15, %2[%arg0, %arg1] {in_bounds = [true]} : vector<4xf16>, memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
        }
      }
      return
    }

    Note that the vector.gather takes in linearized indices (which is actually not the best thing). But after LLVMCPUVirtualVector lowering, we seem to not take the strides into account for the linearized indices:

    func.func @main$async_dispatch_0_elementwise_broadcast_2x128_i64xf16() attributes {translation_info = #iree_codegen.translation_info<pipeline = CPUDoubleTilingExpert>} {
      %cst = arith.constant dense<128> : vector<4x1xindex>
      %cst_0 = arith.constant dense<0.000000e+00> : vector<1x4xf16>
      %cst_1 = arith.constant dense<true> : vector<1x4xi1>
      %c0_i64 = arith.constant 0 : i64
      %c4 = arith.constant 4 : index
      %c1 = arith.constant 1 : index
      %c128 = arith.constant 128 : index
      %c2 = arith.constant 2 : index
      %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") : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %0, 64 : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>>
      %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") : memref<2xi64, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %1, 64 : memref<2xi64, #hal.descriptor_type<storage_buffer>>
      %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) : memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
      memref.assume_alignment %2, 64 : memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
      %subview = memref.subview %0[0, 0, 0] [57, 1, 128] [1, 1, 1] : memref<57x2x128xf16, #hal.descriptor_type<storage_buffer>> to memref<57x128xf16, strided<[256, 1]>, #hal.descriptor_type<storage_buffer>>
      %3 = vector.step : vector<4xindex>
      scf.for %arg0 = %c0 to %c2 step %c1 {
        %4 = vector.transfer_read %1[%arg0], %c0_i64 {in_bounds = [true]} : memref<2xi64, #hal.descriptor_type<storage_buffer>>, vector<1xi64>
        %5 = arith.index_cast %4 : vector<1xi64> to vector<1xindex>
        %6 = vector.broadcast %5 : vector<1xindex> to vector<4x1xindex>
        %7 = vector.shape_cast %6 : vector<4x1xindex> to vector<4xindex>
        %8 = vector.shape_cast %cst : vector<4x1xindex> to vector<4xindex>
        %9 = arith.muli %7, %8 : vector<4xindex>
        scf.for %arg1 = %c0 to %c128 step %c4 {
          %10 = vector.broadcast %arg1 : index to vector<4xindex>
          %11 = arith.addi %3, %10 : vector<4xindex>
          %12 = arith.addi %11, %9 : vector<4xindex>
          %13 = vector.shape_cast %12 : vector<4xindex> to vector<1x4xindex>
          %14 = vector.gather %subview[%c0, %c0] [%13], %cst_1, %cst_0 : memref<57x128xf16, strided<[256, 1]>, #hal.descriptor_type<storage_buffer>>, vector<1x4xindex>, vector<1x4xi1>, vector<1x4xf16> into vector<1x4xf16>
          %15 = vector.extract %14[0] : vector<4xf16> from vector<1x4xf16>
          vector.transfer_write %15, %2[%arg0, %arg1] {in_bounds = [true]} : vector<4xf16>, memref<2x128xf16, #hal.descriptor_type<storage_buffer>>
        }
      }
      return
    }
  3. Groverkss commented on Jan 21, 2025

    @Groverkss
    ContributorAuthor

    Another interesting thing about this IR, it's effectively doing a sliced index_select, which in numpy can be written as:

    import numpy as np
    
    cache = np.random.rand(57, 2, 128).astype(np.float16)
    indices = np.arange(2, dtype=np.int64)
    selected = cache[indices, 0]
    

    This error does not appear if we do not do the strided access on dim=1:

    import numpy as np
    
    cache = np.random.rand(57, 128).astype(np.float16)
    indices = np.arange(2, dtype=np.int64)
    selected = cache[indices]
    

    MLIR IR produced for the above numpy file actually works correctly, which points to a strided memref access issue

  4. Groverkss commented on Jan 21, 2025

    @Groverkss
    ContributorAuthor

    This IR was generated using iree-turbine with the following file: https://gist.github.com/Groverkss/08b291c62572abe6578e79590df2db30 in case you want to play around with the IR to see how the strides affect the output.

  5. hanhanW commented on Feb 19, 2025

    @hanhanW
    Contributor

    I think we end up with the same observation. :) I took a look at it in Ian's issue: #20011 (comment) The other repro is smaller, so I left a comment here. We can collapse two issues though.

  6. hanhanW commented on Mar 17, 2026

    @hanhanW
    Contributor

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

  7. self-assigned this
    on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐞

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions