Repository navigation
Description
Today we only inline splat constants (constant dense<5> : tensor<4xi32>), via the logic here:
https://github.com/google/iree/blob/main/iree/compiler/Dialect/Flow/Transforms/RematerializeDispatchConstants.cpp#L41-L57
This excludes things like like constant dense<[0,1,2,3]> : tensor<4xi32> or constant dense<[1.0, 2.0]> : tensor<2xf32>. As part of buffer allocation I'm noticing a large number of small constants that then aren't getting inlined and thus need to be planned for externally and passed in. We're talking specifically about reasonable constants to inline, not MB-sized things. Think more like 16-element lookup tables, initialization vectors, channel masks, etc.
Since SPIR-V and LLVM both support constants this may just be a missing conversion or two between what we are taking in (mhlo on the old path, linalg on the new path) and those targets.
Mahesh says:
We should be able to use tensor_to_memref here to wire things together and finally SPIRV and LLVM should handle the constant
This can be tested by changing the isSplatConstant to return true if the DenseElementsAttr logic is changed to:
} else if (auto value = constantOp.getValue().dyn_cast<DenseElementsAttr>()) {
auto shapedType = constantOp.getType().cast<ShapedType>();
uint64_t estimatedByteLength =
(shapedType.getNumElements() * shapedType.getElementTypeBitWidth()) / 8;
return denseAttr.isSplat() ||
estimatedByteLength <= 256; // or whatever
} else {That should cause the constants to end up in the dispatch regions. Here's an example dispatch region that fails today (masking from a clamp or something) with the above change:
func @test() -> tensor<5xi32> {
%c5 = constant 5 : index
%3 = iree.unfoldable_constant dense<4.0> : tensor<f32>
%4 = flow.dispatch.region[%c5 : index](%arg2 = %3 : tensor<5xf32>) -> (tensor<5xi32>) {
%cst_4 = constant dense<0.000000e+00> : tensor<5xf32>
%49 = "mhlo.compare"(%arg2, %cst_4) {comparison_direction = "EQ"} : (tensor<5xf32>, tensor<5xf32>) -> tensor<5xi1>
%50 = "mhlo.convert"(%49) : (tensor<5xi1>) -> tensor<5xi32>
%cst_5 = constant dense<[1, 2, 3, 4, 5]> : tensor<5xi32>
%51 = mhlo.multiply %50, %cst_5 : tensor<5xi32>
flow.return %51 : tensor<5xi32>
}
return %4 : tensor<5xi32>
}
Activity
MaheshRavishankar commentedon Feb 20, 2021
@hanhanW temporarily passing this to you (ill pick it up later in the week if you dont get to it by then). Adding support on the existing codegen path (i.e. not linalg on tensors) would require two steps
std.constanttostd.constant -> tensor_to_memrefin HLOToLinalgOnBufferstensor_to_memrefin SPIR-V lowering (basicall iy is a no-op but needs to have the pattern for it.Ill add the support on the linalg on tensors path
hanhanW commentedon Feb 21, 2021
Is the idea to convert below snippet
to
And drop
TensorToMerefOpin ConvertToSPIRVPass (like linalg::TensorReshapeOp)?MaheshRavishankar commentedon Feb 21, 2021
I guess by drop it you mean just forward the
!spv.array. Yes, thats what I mean. We can just add this pattern to core..hanhanW commentedon Feb 22, 2021
Yes, I meant to forward the
spv.arrayby replacing the op with operands, like:https://github.com/google/iree/blob/87d169a59bc6203b8e2813438d9b3a835fbe05da/iree/compiler/Conversion/LinalgToSPIRV/ConvertToSPIRVPass.cpp#L275-L287
MaheshRavishankar commentedon Feb 22, 2021
Right!
MaheshRavishankar commentedon Feb 23, 2021
@hanhanW did you already get to this. I am hitting this on some unrelated change but might as well fix it. Just looking to fix it on the linalg on tensors path though for now. Let me know.
hanhanW commentedon Feb 23, 2021
No, I haven't started the work yet.
MaheshRavishankar commentedon Feb 23, 2021
Ok, ill update here if I get this working on the linalg on tensors path.
MaheshRavishankar commentedon Mar 5, 2021
Started looking into this on the linalg on tensors path briefly (WIP commit here MaheshRavishankar@489d97c).
It stumped right now cause a canonicalizer in core takes a
constant -> tensor_to_memref -> loadand converts it intoconstant -> tensor.extract. Which might be OK. It meanstensor.extractneeds to be supported now in Standard/Tensor SPIR-V now. See dump IR after all hereOn the LLVM side it looks there is a tensor-constant-bufferize pass (here) that we might need to use, but that might work out of the box.
Leaving notes here if someone else has time to pick this up and finish. I am going to be switching to some higher priority tasks for a bit.
antiagainst commentedon Mar 5, 2021
I see we are extracting from constant using dynamic indices. Then for SPIR-V we need to create local variables to hold the constant and then use access chain, which supports dynamic indices. I can throw in a pattern for it.
25 remaining items
ScottTodd commentedon Mar 17, 2021
Ah... yeah. We could check for equality with these functions: https://github.com/google/iree/blob/a6a827784b0726c5a9e702d1af1a51379cd8f975/iree/compiler/Dialect/Flow/Transforms/DeduplicateExecutables.cpp#L47-L50
(which could be upstreamed, see #3996)
That gist has multiple constants named
__constant_2x2xf32with different values:asaadaldien commentedon Mar 17, 2021
We can also use the renaming utils as Ben mentioned and CSE after the linking which will remove globas with the same values.
MaheshRavishankar commentedon Mar 17, 2021
Awesome! Seems like we have options. Lets chat in a more group setting to come to a consensus on way forward.
benvanik commentedon Mar 17, 2021
does CSE handle LLVM globals? I think it needs a dedicated pass that doesn't exist - that's one of the reasons we have DeduplicateExecutables. If we can prevent the duplicates from being inserted in the first place that'd be best.
MaheshRavishankar commentedon Mar 19, 2021
Tried this out here. Unfortunately it turns out this is a bit more complex than I thought it was. The "private" you see on the symbols before
LinkExecutablesPassin the gist posted above is actually the symbol in LLVM dialect and not the MLIR symbol visibility. So that makes things more involved cause it seems like those are actually not marked as "Private" symbols in MLIR. There needs to be some way of mapping the LLVM symbol visibility to MLIR symbol visibility, which probably requires some discussion and cant be done as a quick fix.So wondering if we should disable this linking of executables (like it is done on the SPIR-V side) till this can be resolved. Note that this is the issue possibly happening in #5152.
asaadaldien commentedon Mar 19, 2021
Thanks @MaheshRavishankar for giving this a shot, linking executables is very important for perf and its analysis and we would like to keep it enabled for LLVM. Another short term fix is to add pass after
TensorConstantBufferizeto alterglobal_memrefsym_name and append func names so we will have__constant_2x2xf32_foo_dispatch_1,__constant_2x2xf32_foo_dispatch_2.MaheshRavishankar commentedon Mar 19, 2021
Ok, I'll see if I can add this to the upstream pass, though there might be push back against it (Which would be valid, but it is a small change). I'll take a stab at it.
asaadaldien commentedon Mar 19, 2021
Thanks @MaheshRavishankar , If there is a push back for
sym_namesin upstream we can add another Module pass here that does renamememref_globalsymbol names.IMO the real long term fix is to get the symbol_table rename you [linked]((MaheshRavishankar@7d3baeb) working.
benvanik commentedon Mar 19, 2021
It'd be nice if private LLVM-dialect symbols responded to the same symbol trait queries as any other dialect - may call for an interface (if one doesn't exist already) so that dialects can map things. That said, the linking phase is HAL target-specific and we could perform whatever logic we wanted as part of that - such as a cleanup of duplicated global_memref symbols post-linking (as we could run prior to verification and handle the name conflicts ourselves).
benvanik commentedon Mar 19, 2021
linking happens here: https://github.com/google/iree/blob/98f2135ed300be57145c705ebe3a14b9641452ef/iree/compiler/Dialect/HAL/Target/LLVM/LLVMAOTTarget.cpp#L114-L117
we can do the cleanup prior to returning
MaheshRavishankar commentedon Mar 19, 2021
Thanks for the pointer. I am just going to try to do that there now, cause that is the cleanest solution I can see.
Resolve name conflicts while linking executables in LLVM. (#5179)
Handle symbol name conflicts during executable linking. (#5978)