-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][tosa] Support DenseResourceElementsAttr
in TOSA transpose folders
#124532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
#include "mlir/Dialect/Utils/IndexingUtils.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/DialectResourceBlobManager.h" | ||
#include "mlir/IR/Matchers.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "llvm/ADT/APFloat.h" | ||
|
@@ -176,13 +177,36 @@ DenseElementsAttr transposeType(const RangeType &data, ShapedType inputType, | |
llvm::ArrayRef<ElementType>(outputValues)); | ||
} | ||
|
||
// Try to get the values of a DenseResourceElementsAttr construct | ||
template <typename T> | ||
std::optional<ArrayRef<T>> tryGetDenseResourceValues(ElementsAttr attr) { | ||
if (auto denseResource = dyn_cast<DenseResourceElementsAttr>(attr)) { | ||
// Check that the resource memory blob exists | ||
AsmResourceBlob *blob = denseResource.getRawHandle().getBlob(); | ||
if (!blob) | ||
return std::nullopt; | ||
|
||
// Check that the data are in a valid form | ||
bool isSplat = false; | ||
if (!DenseElementsAttr::isValidRawBuffer(attr.getShapedType(), | ||
blob->getData(), isSplat)) { | ||
return std::nullopt; | ||
} | ||
|
||
return blob->template getDataAs<T>(); | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
// A type specialized transposition of an ElementsAttr. | ||
// This implementation tries to operate on the underlying data in its raw | ||
// representation when possible to avoid allocating a large number of Attribute | ||
// objects. | ||
DenseElementsAttr transpose(ElementsAttr attr, ShapedType inputType, | ||
ShapedType outputType, | ||
llvm::ArrayRef<int64_t> permValues) { | ||
// Handle generic ElementsAttr | ||
if (auto data = attr.tryGetValues<bool>()) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
|
@@ -204,6 +228,35 @@ DenseElementsAttr transpose(ElementsAttr attr, ShapedType inputType, | |
if (auto data = attr.tryGetValues<APFloat>()) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
// Handle DenseResourceElementsAttr | ||
if (isa<DenseResourceElementsAttr>(attr)) { | ||
auto elementTy = attr.getElementType(); | ||
|
||
if (auto data = tryGetDenseResourceValues<bool>(attr); | ||
data && elementTy.isInteger(1)) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
if (auto data = tryGetDenseResourceValues<int8_t>(attr); | ||
data && elementTy.isInteger(8)) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
if (auto data = tryGetDenseResourceValues<int16_t>(attr); | ||
data && elementTy.isInteger(16)) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
if (auto data = tryGetDenseResourceValues<int32_t>(attr); | ||
data && elementTy.isInteger(32)) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
if (auto data = tryGetDenseResourceValues<int64_t>(attr); | ||
data && elementTy.isInteger(64)) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
|
||
if (auto data = tryGetDenseResourceValues<float>(attr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we missing other floating point types like FP16 per spec? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs handling through APFloat I presume. Need to look into this. Happy to do this as part of this patch if you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So had a look at this and is not as simple. I think this requires more effort to layer cleanly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding this as a TODO below? |
||
data && elementTy.isF32()) | ||
return transposeType(*data, inputType, outputType, permValues); | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.