Repository navigation
Closed
Labels
bug 🐞Something isn't working
Description
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
- 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
}- 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=4What component(s) does this issue relate to?
Compiler
Version information
Additional context
No response
Activity
IanWood1 commentedon Feb 18, 2025
Seems related to #19749
IanWood1 commentedon Feb 18, 2025
There is a subview without strides (but the result type has strides) that is getting folded away. I think this is the cause
hanhanW commentedon Feb 18, 2025
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.
hanhanW commentedon Feb 19, 2025
Taking a closer look, I think the issue might not be related to the strides? Looking at the
vector.loadpattern, the first index is always%c0, so the strides do not matter. They are always%c0 * %stride = %c0.hanhanW commentedon Feb 19, 2025
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.subviewwith strides that @IanWood1 pointed out.%4is a 4x2xf32 subtensor of the binding.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], where2is the dim size of the last dimension. This is okay if we look it at the memref level.At memref level, it becomes the below snippet. The strides are introduced.
Then we lower it to a sequence of extract op and load op, which eventually becomes
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 just2. 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)[Dispatch Creation] Drop unit dims from tensor.extract ops (#22503)
[Dispatch Creation] Drop unit dims from tensor.extract ops (iree-org#…
[Dispatch Creation] Drop unit dims from tensor.extract ops (iree-org#…
hanhanW commentedon Mar 17, 2026
Confirmed that it is fixed by llvm/llvm-project#184706, closing the issue.
[Test] Drop test_reversesequence_time from cpu_llvm_sync xfails