You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm getting incorrect results with a custom RoPE kernel when run on the CPU backend. The bug appears to only effect certain sizes/shapes (more info below).
repro.mlir: the problematic IR that produces incorrect results with the llvm-cpu backend
repro2.mlir: same as repro.mlir but with a slight change to one of the dimension sizes. This IR produces correct results (maybe it will help with tracking down the problem)
run.py: it will generate the inputs & expected outputs to run either repro.mlir or repro2.mlir through both iree-compile and iree-run-module
For context, the commands I used that generated incorrect results:
I might know what's happening. The indexing map is too bad. It has d3 floordiv 2, and tiling should not happen in this case.
The correct one passes just because it sets the tile size to 1, which scalarizes the dimension. It is off when the tile size is not a multiple of 2. IMO, we should not tile such dimension at all.
Huh, I didnt realize that tiling is off. It is not immediately obvious to me why though. AFAICS you should still be able to tile this generic op even if floordiv exists...
I need to think more about it. It is not obvious to me how tiling works if there is floordiv. I can only understand the tiling when exprs are in strided form I think. Even for strided form, I'm only like 80% sure. I need to write things down to help me think.
If the tile size is 7, we get two slices. The | is the boundary of two slices.
The offsets are not taken into account, so it produces (0 0 1 1 2 2 3) in the second slice. The correct version should be something like (0 1 1 2 2 3 3).
This is why I think tiling can not be supported in this generic op. We are missing a symbol (which represents the offset) in the affine map.
Does it make sense?
I can't give a formal definition of what kind of generic ops can be tiled atm, but I'll provide a fix tomorrow.
For now, my feeling is that we can not tile the ops when div or mod is involved in any of affine map. (There are exceptions, but it is not easy to identify the case. E.g., for this op, we can tile d0, but not d1.)
If the tile size is 7, we get two slices. The | is the boundary of two slices.
The offsets are not taken into account, so it produces (0 0 1 1 2 2 3) in the second slice. The correct version should be something like (0 1 1 2 2 3 3).
This is why I think tiling can not be supported in this generic op. We are missing a symbol (which represents the offset) in the affine map.
Does it make sense?
I can't give a formal definition of what kind of generic ops can be tiled atm, but I'll provide a fix tomorrow.
For now, my feeling is that we can not tile the ops when div or mod is involved in any of affine map. (There are exceptions, but it is not easy to identify the case. E.g., for this op, we can tile d0, but not d1.)
Thanks Hanhan,
I think its a matter of adjust the indexing maps produced while computing the slice offsets and sizes. I dont know what the math looks like though, but I think there is a closed form solution here. Basically the offset computation generation for Linalg ops needs to include "some extra math" to compute the right offset.
Yeah, that would need some more eyes, and I'm happy to help with this! This kind of work is always fun. (I'm also happy that we are not just looking for a workaround.)
For now, do we disable the tiling for some dimensions, which makes it function? If the op is a root op, it is easier. If not, we need to track the SSA chain and make the decision. It is doable, it is just a little more complicated.
The workaround in my mind is to enhance the LinalgOpInfo to carry such information, and we force some tile sizes to 0 when it identifies the case.
For now, do we disable the tiling for some dimensions, which makes it function? If the op is a root op, it is easier.
For some context, I think this would unblock the shark-ai PR. All the dispatches have this op as the root
Basically the offset computation generation for Linalg ops needs to include "some extra math" to compute the right offset.
I don't think adjusting the offsets alone is enough. I'm not too familiar with tiling but this is my reasoning:
Using Hanhan's example, we need the first tile to be (0 0 1 1 2 2 3) and the second to be (0 1 1 2 2 3 3). If the affine map is f(x) = x floordiv 2 an offset N has the effect of f(x) = (x floordiv 2) + N. This means we must transform the first sequence into the second sequence with only N as a parameter (shift the sequence left by 1). But this transformation only works if the function is affine: f(x) = ax+b could be shifted by subtracting a. However, since f(x) is not affine this appears impossible.
What we really need (but I don't think it works) is for the first map to be x floordiv 2 and the second to be (x + 1) floordiv 2
I havent figured it out yet, but just posting my starting point to ese if that helps. Going back to basics, lets start with a loop and any function of the induction variable used for accessing into some memory.
for (i = 0; i < N ; ++i) {
index = f(i);
}
f could be any arbitrary function, but lets put a constraint that f isnt "strided", i.e that the set of all index values is contiguous.
Now tiling this loop by tile size t is
for (i = 0; i < N ; i += t) {
for (i0 = i; i0 < affine_min(t, N - i); i0 += t) {
index = f(i0)
}
}
This is basically what we do in linalg tiling
for (i = 0; i < N ; i += t) {
i0_start = i
i0_end = affine_min(t, N - i) // This is "exclusive" limit, i.e i0_end is 1 past the last value of `i0`.
index_start = f(i0_start)
index_end = f(i0_end - 1) + 1 // the +1 at the end makes index_end "exclusive" limit of `index`
slice_offset = index_start
slice_size = index_end - index_start.
}
In our case, f(i) is just floorDiv(i, k) where k is some constant. The logic above should still hold. I am not sure why it is not.
The floorDiv case is tricky, and it is broken atm. We don't have a robust and easy solution, atm. The tile sizes can only be multiple of divisor in current tiling implementation. In this example, the tile size of d1 must be a multiple of 2.
Next steps:
Update upstream to return a failure when floorDiv is present and the tile size is not valid. Also need to return a failure if mod is present. This prevents miscompiling the code.
Force the tile sizes to be multiple of divisors in CPU backend. This makes CPU codegen work reasonably.
Do the same thing for GPU and switch it to TileAndFuse pipeline.
Activity
hanhanW commentedon May 15, 2025
I haven't seen complex types passed into generic ops for a while, interesting. I'll take a look at it.
(It's so dumb that I use incognito mode every time when I need to click a shark-ai issue. :p)
hanhanW commentedon May 15, 2025
I might know what's happening. The indexing map is too bad. It has
d3 floordiv 2, and tiling should not happen in this case.The correct one passes just because it sets the tile size to 1, which scalarizes the dimension. It is off when the tile size is not a multiple of 2. IMO, we should not tile such dimension at all.
If what I claimed is correct, there are action items:
@MaheshRavishankar Is it correct that tiling can happen only if all the result exprs are in strided form?
hanhanW commentedon May 15, 2025
Here is a generalized convolution, and the result exprs are all in strided form.
MaheshRavishankar commentedon May 15, 2025
Huh, I didnt realize that tiling is off. It is not immediately obvious to me why though. AFAICS you should still be able to tile this generic op even if floordiv exists...
hanhanW commentedon May 15, 2025
I need to think more about it. It is not obvious to me how tiling works if there is floordiv. I can only understand the tiling when exprs are in strided form I think. Even for strided form, I'm only like 80% sure. I need to write things down to help me think.
MaheshRavishankar commentedon May 15, 2025
if you have the IR, could you post the IR before and after tiling
hanhanW commentedon May 15, 2025
Yes, I have the IR. Here is the IR before and after scf.forall distrubution and first level of scf.for tiling: https://gist.githubusercontent.com/hanhanW/c0249ba70bd9e05f037dbacaff1b59b4/raw/19cf910e1bd69736e7c59cf386fd8381f5c2c057/dump.mlir
Inlined:
hanhanW commentedon May 15, 2025
Putting a note here: I did not know that GPU produces correct output. It does not tile the dimension because the lowering config is
[1, 128].hanhanW commentedon May 16, 2025
Here is the IR without distribution and setting the tile size to [0, 7]: https://gist.github.com/hanhanW/cb7686911e4541fe4837daf1ccb1eb36
hanhanW commentedon May 16, 2025
Mirroring the discussion between @MaheshRavishankar and me to the issue:
Okay, I think the tiling should not happen in this type of generic op. Reason:
The indexing formula is f(x) = x / 2. (floordiv)
The original indexing mapping is
If the tile size is 7, we get two slices. The
|is the boundary of two slices.The offsets are not taken into account, so it produces (0 0 1 1 2 2 3) in the second slice. The correct version should be something like (0 1 1 2 2 3 3).
This is why I think tiling can not be supported in this generic op. We are missing a symbol (which represents the offset) in the affine map.
Does it make sense?
I can't give a formal definition of what kind of generic ops can be tiled atm, but I'll provide a fix tomorrow.
For now, my feeling is that we can not tile the ops when div or mod is involved in any of affine map. (There are exceptions, but it is not easy to identify the case. E.g., for this op, we can tile
d0, but notd1.)MaheshRavishankar commentedon May 16, 2025
Thanks Hanhan,
I think its a matter of adjust the indexing maps produced while computing the slice offsets and sizes. I dont know what the math looks like though, but I think there is a closed form solution here. Basically the offset computation generation for Linalg ops needs to include "some extra math" to compute the right offset.
Time to pick up a linear algebra book.
hanhanW commentedon May 16, 2025
Yeah, that would need some more eyes, and I'm happy to help with this! This kind of work is always fun. (I'm also happy that we are not just looking for a workaround.)
For now, do we disable the tiling for some dimensions, which makes it function? If the op is a root op, it is easier. If not, we need to track the SSA chain and make the decision. It is doable, it is just a little more complicated.
The workaround in my mind is to enhance the LinalgOpInfo to carry such information, and we force some tile sizes to 0 when it identifies the case.
iree/compiler/src/iree/compiler/Codegen/Utils/LinalgOpInfo.h
Lines 15 to 39 in e66fc15
IanWood1 commentedon May 16, 2025
For some context, I think this would unblock the shark-ai PR. All the dispatches have this op as the root
I don't think adjusting the offsets alone is enough. I'm not too familiar with tiling but this is my reasoning:
Using Hanhan's example, we need the first tile to be
(0 0 1 1 2 2 3)and the second to be(0 1 1 2 2 3 3). If the affine map isf(x) = x floordiv 2an offset N has the effect off(x) = (x floordiv 2) + N. This means we must transform the first sequence into the second sequence with onlyNas a parameter (shift the sequence left by 1). But this transformation only works if the function is affine:f(x) = ax+bcould be shifted by subtractinga. However, sincef(x)is not affine this appears impossible.What we really need (but I don't think it works) is for the first map to be
x floordiv 2and the second to be(x + 1) floordiv 2hanhanW commentedon May 19, 2025
Smaller repro:
iree-compile --iree-hal-target-device=local --iree-hal-local-target-device-backends=llvm-cpu --iree-llvmcpu-target-cpu=host ~/repro.mlirOutput:
14xf32=0 0 1 1 2 2 3 3 3 4 4 5 5 6Expected output:
14xf32=0 0 1 1 2 2 3 3 4 4 5 5 6 6.(You can replace
7with0in the lowering_config, which disables the tiling. It generates the expected output.)hanhanW commentedon May 19, 2025
Repro for tiling result:
iree-opt --transform-interpreter ~/repro.mlirOutput:
(I haven't figured out the expected output.)
MaheshRavishankar commentedon May 19, 2025
I havent figured it out yet, but just posting my starting point to ese if that helps. Going back to basics, lets start with a loop and any function of the induction variable used for accessing into some memory.
fcould be any arbitrary function, but lets put a constraint thatfisnt "strided", i.e that the set of allindexvalues is contiguous.Now tiling this loop by tile size
tisThis is basically what we do in linalg tiling
In our case,
f(i)is justfloorDiv(i, k)wherekis some constant. The logic above should still hold. I am not sure why it is not.hanhanW commentedon May 20, 2025
Leaving a note for the discussion that @MaheshRavishankar, @IanWood1 and I had in offline.
The floorDiv case is tricky, and it is broken atm. We don't have a robust and easy solution, atm. The tile sizes can only be multiple of divisor in current tiling implementation. In this example, the tile size of d1 must be a multiple of 2.
Next steps:
hanhanW commentedon May 20, 2025
I forgot a note, the tile size can also be 1, which is basically scalarize the generic op.