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
{{ message }}
Repository navigation
Compile error with mixed-types linalg.matmul vectorization #15241
This arises when trying to add e2e matmul tests with mixed-types such as f16 x f16 -> f32, enabling data-tiling, and not using microkernels (as the issue is in vectorization, which is circumvented by microkernels).
Here, vectorization has turned the mixed-types linalg.matmul into a mixed-types vector.contract. This is the likely root cause problem, because unlike linalg.matmul, vector.contract does not really support mixed-types. It's a bit of a gray area because its effective semantics are not fully defined and in practice defined by lowerings, but a bunch of existing code treats vector.contract as not supporting mixed types and the error generated here seems to be about ConvertToLLVM not supporting mixed-types vector.contract.
The likely fix is this to fix GenericVectorization (iree-codegen-generic-vectorization) to generate arith.ext* ops on the operands of the vector.contract to make all its operands share the same element type.
To add to @bjacob's comment, that fix is right, but I think on GPU side mixed types are supported in hardware. So with the fix to the vectorizer on the GPU side an extra folding pattern should also be added to fold the ext -> vector.contract back to the right instruction.
Basically we should stop relying on mixed types vector.contract. best will be if all patches on this path are tested on CI in IREE before landing in LLVM to find regressions before hand
Nit: CPU ISAs have mixed-types multiplication instructions, too. So the difference between CPU and GPU here is not about ISA (let alone hardware). It's a compiler implementation choice: whereas Codegen/CPU has chosen to treat mixed-types vector.contract as illegal, Codegen/GPU has (if I understand correctly) actually provided lowerings for it, and thus expects a mixed-types contraction to be represented as a single mixed-types vector.contract, not multiple ops (arith.ext -> vector.contract) and we should avoid causing any regression on GPU by changing that.
We have been compiling i8, i8 -> i32 for a while, at least for RISC-V, but I don't remember what happened at vector.contract level. I also remember some examples from SVE/SME? (@banach-space)
According to the doc, vector.contract supports different types for the accumulator:
// Vector contraction with mixed typed. lhs/rhs have different element
// types than accumulator/result.
%5 = vector.contract #contraction_trait %0, %1, %2
: vector<10xf16>, vector<10xf16> into f32
vector.contract is a relatively high-level operation that still preserves quite some structure so using a wider accumulator seems something that fits well at that level, to some extent. Not sure if the issue is at the LLVM conversion level, though, because this op should have been lowered into a lower level vector representation (outerproduct, fmas, etc.) far before we lower to LLVM itself... and it doesn't look like that is the case:
I think there's been back and forth of this as the status of mixed-types vector.contract wasn't clarified early, so at the moment we are left with different parts of the code taking a different stance on this. For example, the compile error that this Issue is about suggests that ConvertToLLVM doesn't fully support mixed-types vector.contract. Some 2 years ago, I prepared a LLVM PR (then under Phabricator ) to make vector.contract lowerings more fully support mixed types, and that was turned down by @nicolasvasilache as not something that we actually wanted to support. I still think that Nicolas' decision then was the right one.
Trying to support mixed-types vector.contract runs into tricky issue when one thinks about integer arithmetic support. As we use signless integers throughout, a mixed-types integer vector.contract can't know whether to interpret operands as signed or unsigned. So, with integer types, we have to have explicit arith.extui/extsi on operands anyway, and which point we don't need mixed-type vector.contract anymore.
All in all, while it certainly is possible to resolve this by treating mixed-type vector.contract as legal and trying to make ConvertToLLVM handle it (except perhaps for signless integers, where that seems under-defined), I believe that here the simpler resolution is to treat it as something to avoid generating in vectorization.
There is a default behavior for integers but it can be kind of surprising to the user:
If operands and the result have types of different bitwidths, operands are promoted to have the same
bitwidth as the result before performing the contraction. For integer types, only signless integer types
are supported, and the promotion happens via sign extension.
We are not generating vector.contract ops directly from the vectorizer. There are some patterns that are matching vector reduction ops and raising them to vector.contract so we should fix those if we decide to go with that approach.
I don't have a preference but I may see people using this inconsistently. I wouldn't be surprised if we had some direct conversions from mixed-type vector.contract ops to Nvidia mma ops (totally guessing here).
There is a default behavior for integers but it can be kind of surprising to the user:
If operands and the result have types of different bitwidths, operands are promoted to have the same
bitwidth as the result before performing the contraction. For integer types, only signless integer types
are supported, and the promotion happens via sign extension.
We are not generating vector.contract ops directly from the vectorizer. There are some patterns that are matching vector reduction ops and raising them to vector.contract so we should fix those if we decide to go with that approach.
I don't have a preference but I may see people using this inconsistently.
The only way to fix it is to add that to the verifier to ensure consistent usage. But that will break downstream users. We should send RFC and make the change. First order is to remove such behavior in IREE.
I wouldn't be surprised if we had some direct conversions from mixed-type vector.contract ops to Nvidia mma ops (totally guessing here).
We probably do, but that should be changed to a peephole that matches a DAG that includes the ext to fold it into the nvgpu op
One thing to keep in mind however is that representing some things we care about in LLVM can get quite tricky.
I even wonder whether some cases are possible at all.
It's important that we can target these from MLIR - I am not expecting to get much help from the backend in form of e.g. peephole optimisations. There are some SVE examples of widening instructions too, but for 1d scalable vectors we can expect more help from the backend (worth double-checking though).
The likely fix is this to fix GenericVectorization (iree-codegen-generic-vectorization) to generate arith.ext* ops on the operands of the vector.contract to make all its operands share the same element type.
This should be fine for SME - we could provide our our conversion patterns for SME to target the widening outer-product instructions/intrinsics.
Given the issue with representing signed/unsigned extension semantics at the vector level, to be able to target dot prod instructions with mixed data types, we probably need to start mirroring those in dialects in MLIR that are closer to hardware (like the NVGPU dialect, etc.)
Also, do you mean mirroring higher level vector Ops like vector.contract, or just to lower level like vector.outerproduct?
As it exists today, I think that would be for all vector dialect ops. Without this I am not sure how to represent the right sign extension semantics for examples.
Also, do you mean mirroring higher level vector Ops like vector.contract, or just to lower level like vector.outerproduct?
I started reading from the middle of the thread, but I think the general rules for ops like this (e.g., vector.reduce, vector.contract, etc.) that only accept signless integers is that the semantics must be fully defined by the op and it's attributes. For example, vector.reduction <add> is fine because integer addition is signless, but something like min/max is not, and we can't rely on any zero/sign-extension operands to define what the reduction means. IE for the same, bit-identical, operands, it must produce the same result regardless of which ops these come from.
For reduction over signless operands we need signed and unsigned variants of min/max. I think this is currently a bug in the vector.reduction op definition which reads:
Reduces an 1-D vector “horizontally” into a scalar using the given operation (add/mul/min/max for int/fp and and/or/xor for int only). Reductions also allow an optional fused accumulator.
Ouch, we would also have to add the unsigned/signed min/max ops to the contraction kind if it's not already there.
To unblock this, I think we could just add the optional expansion attribute to the contraction traits dict and set it to signed/unsigned for integer inputs. It the simplest fix, preserves existing behavior and I can't think of a case where it wouldn't work. Then, we could think later if we want to explore other routes. WDYT?
Ouch, we would also have to add the unsigned/signed min/max ops to the contraction kind if it's not already there. To unblock this, I think we could just add the optional expansion attribute to the contraction traits dict and set it to signed/unsigned for integer inputs. It the simplest fix, preserves existing behavior and I can't think of a case where it wouldn't work. Then, we could think later if we want to explore other routes. WDYT?
The signed/unsigned wouldnt work for float types.... The fix really should be to deprecate min/max and add smin/smax/umin/umax attributes.
The testcase in the original issue description here is now successfully compiling.
There seems to be no consensus about whether mixed-types vector.contract is legal. It seems to be only lowerings-defined. The CPU codegen pipeline is treating that as illegal, while the GPU codegen pipeline is treating that as legal. The fix in Disable folding casting ops into contraction ops by default. #15342 makes us avoid creating mixed-types vector.contract in cases where the subsequent codegen lowerings are going to treat that as illegal.
In my opinion, for the reasons discussed above, the only viable resolution is to clarify that mixed-types vector.contract is illegal. As we had already discussed 2 years ago on some Phabricator review where I was trying to provide missing lowerings for mixed-types vector.contract and you clearly explained then that this shouldn't be supported :-) (can dig that one up if that helps).
Resolving that should be an issue filed against vector dialect upstream, no need to keep this present issue open.
Activity
bjacob commentedon Oct 19, 2023
Discord thread: https://discord.com/channels/689900678990135345/1164644829641322546/1164644832837369896
bjacob commentedon Oct 20, 2023
In the print-ir-after-all log from the above gist, the problem seems to arise at this point:
Here, vectorization has turned the mixed-types
linalg.matmulinto a mixed-typesvector.contract. This is the likely root cause problem, because unlikelinalg.matmul,vector.contractdoes not really support mixed-types. It's a bit of a gray area because its effective semantics are not fully defined and in practice defined by lowerings, but a bunch of existing code treatsvector.contractas not supporting mixed types and the error generated here seems to be about ConvertToLLVM not supporting mixed-typesvector.contract.The likely fix is this to fix GenericVectorization (iree-codegen-generic-vectorization) to generate
arith.ext*ops on the operands of thevector.contractto make all its operands share the same element type.MaheshRavishankar commentedon Oct 20, 2023
To add to @bjacob's comment, that fix is right, but I think on GPU side mixed types are supported in hardware. So with the fix to the vectorizer on the GPU side an extra folding pattern should also be added to fold the ext -> vector.contract back to the right instruction.
Basically we should stop relying on mixed types vector.contract. best will be if all patches on this path are tested on CI in IREE before landing in LLVM to find regressions before hand
bjacob commentedon Oct 20, 2023
Nit: CPU ISAs have mixed-types multiplication instructions, too. So the difference between CPU and GPU here is not about ISA (let alone hardware). It's a compiler implementation choice: whereas Codegen/CPU has chosen to treat mixed-types vector.contract as illegal, Codegen/GPU has (if I understand correctly) actually provided lowerings for it, and thus expects a mixed-types contraction to be represented as a single mixed-types vector.contract, not multiple ops (arith.ext -> vector.contract) and we should avoid causing any regression on GPU by changing that.
dcaballe commentedon Oct 20, 2023
We have been compiling
i8, i8 -> i32for a while, at least for RISC-V, but I don't remember what happened atvector.contractlevel. I also remember some examples from SVE/SME? (@banach-space)According to the doc,
vector.contractsupports different types for the accumulator:vector.contractis a relatively high-level operation that still preserves quite some structure so using a wider accumulator seems something that fits well at that level, to some extent. Not sure if the issue is at the LLVM conversion level, though, because this op should have been lowered into a lower level vector representation (outerproduct, fmas, etc.) far before we lower to LLVM itself... and it doesn't look like that is the case:bjacob commentedon Oct 20, 2023
I think there's been back and forth of this as the status of mixed-types vector.contract wasn't clarified early, so at the moment we are left with different parts of the code taking a different stance on this. For example, the compile error that this Issue is about suggests that ConvertToLLVM doesn't fully support mixed-types vector.contract. Some 2 years ago, I prepared a LLVM PR (then under Phabricator ) to make vector.contract lowerings more fully support mixed types, and that was turned down by @nicolasvasilache as not something that we actually wanted to support. I still think that Nicolas' decision then was the right one.
Trying to support mixed-types vector.contract runs into tricky issue when one thinks about integer arithmetic support. As we use signless integers throughout, a mixed-types integer vector.contract can't know whether to interpret operands as signed or unsigned. So, with integer types, we have to have explicit arith.extui/extsi on operands anyway, and which point we don't need mixed-type vector.contract anymore.
All in all, while it certainly is possible to resolve this by treating mixed-type vector.contract as legal and trying to make ConvertToLLVM handle it (except perhaps for signless integers, where that seems under-defined), I believe that here the simpler resolution is to treat it as something to avoid generating in vectorization.
dcaballe commentedon Oct 21, 2023
There is a default behavior for integers but it can be kind of surprising to the user:
We are not generating
vector.contractops directly from the vectorizer. There are some patterns that are matching vector reduction ops and raising them tovector.contractso we should fix those if we decide to go with that approach.I don't have a preference but I may see people using this inconsistently. I wouldn't be surprised if we had some direct conversions from mixed-type
vector.contractops to Nvidia mma ops (totally guessing here).MaheshRavishankar commentedon Oct 21, 2023
The only way to fix it is to add that to the verifier to ensure consistent usage. But that will break downstream users. We should send RFC and make the change. First order is to remove such behavior in IREE.
We probably do, but that should be changed to a peephole that matches a DAG that includes the ext to fold it into the nvgpu op
nicolasvasilache commentedon Oct 23, 2023
I generally agree with the discussion here.
One thing to keep in mind however is that representing some things we care about in LLVM can get quite tricky.
I even wonder whether some cases are possible at all.
For instance, I dug up the
peephole that matches a DAG that includes the ext to fold itforpmaddubswhttps://github.com/llvm/llvm-project/blob/3651f377f632d9152f2bd8fc2f9302ec9f9bdd5e/llvm/test/CodeGen/X86/pmaddubsw.ll#L10 and it can get pretty hairy.banach-space commentedon Oct 23, 2023
For SME there are multiple widening outer-product instructions, e.g.:
It's important that we can target these from MLIR - I am not expecting to get much help from the backend in form of e.g. peephole optimisations. There are some SVE examples of widening instructions too, but for 1d scalable vectors we can expect more help from the backend (worth double-checking though).
This should be fine for SME - we could provide our our conversion patterns for SME to target the widening outer-product instructions/intrinsics.
MaheshRavishankar commentedon Oct 23, 2023
Given the issue with representing signed/unsigned extension semantics at the vector level, to be able to target dot prod instructions with mixed data types, we probably need to start mirroring those in dialects in MLIR that are closer to hardware (like the NVGPU dialect, etc.)
banach-space commentedon Oct 23, 2023
That's our plan for SME's outer product, but even then we will have to match patterns like this (hypothetical example):
Also, do you mean mirroring higher level vector Ops like
vector.contract, or just to lower level likevector.outerproduct?MaheshRavishankar commentedon Oct 23, 2023
As it exists today, I think that would be for all
vectordialect ops. Without this I am not sure how to represent the right sign extension semantics for examples.5 remaining items
kuhar commentedon Oct 27, 2023
I started reading from the middle of the thread, but I think the general rules for ops like this (e.g.,
vector.reduce,vector.contract, etc.) that only accept signless integers is that the semantics must be fully defined by the op and it's attributes. For example,vector.reduction <add>is fine because integer addition is signless, but something likemin/maxis not, and we can't rely on any zero/sign-extension operands to define what the reduction means. IE for the same, bit-identical, operands, it must produce the same result regardless of which ops these come from.For reduction over signless operands we need signed and unsigned variants of
min/max. I think this is currently a bug in thevector.reductionop definition which reads:dcaballe commentedon Oct 29, 2023
Ouch, we would also have to add the unsigned/signed min/max ops to the contraction kind if it's not already there.
To unblock this, I think we could just add the optional
expansionattribute to the contraction traits dict and set it tosigned/unsignedfor integer inputs. It the simplest fix, preserves existing behavior and I can't think of a case where it wouldn't work. Then, we could think later if we want to explore other routes. WDYT?MaheshRavishankar commentedon Oct 29, 2023
The
signed/unsignedwouldnt work for float types.... The fix really should be to deprecatemin/maxand addsmin/smax/umin/umaxattributes.dcaballe commentedon Oct 29, 2023
Sorry, I'm still missing why not. Would you mind providing a concrete example? That would help.
Yes, that's what I said but we also have to fix the integer extension problem for the rest of the combiners.
kuhar commentedon Oct 30, 2023
Turns out that signed/unsigned min/max are already there, it's just that the documentation doesn't make it super clear: https://github.com/llvm/llvm-project/blob/72e6c1c70d5e07bbc8cb7cae2ed915108daf93aa/mlir/include/mlir/Dialect/Vector/IR/VectorAttributes.td#L22-L27
ukernels: stop abusing signless as signed (#15338)
Disable folding casting ops into contraction ops by default. (#15342)
nicolasvasilache commentedon Oct 31, 2023
Is this really solved or merely worked around ?
Reopening for now.
bjacob commentedon Oct 31, 2023
This is fixed:
Record narrow static M/N sizes in `EncodingAttr` and rationalize Mate…
ukernels: stop abusing signless as signed (iree-org#15338)
Disable folding casting ops into contraction ops by default. (iree-or…
Record narrow static M/N sizes in `EncodingAttr` and rationalize Mate…