This is an archive of the discontinued LLVM Phabricator instance.

[mlir][tosa] Constant folding for reciprocal
ClosedPublic

Authored by TinaAMD on May 15 2023, 8:04 AM.

Details

Summary

Add constant fold for tosa.reciprocal, which can be applied if the input is a dense constant tensor. The reciprocal is computed for every element and the result is a tensor with the same dimensions as the input tensor.

As the input tensor might require a lot of memory and the folding might double the required memory, a heuristic decides when to actually apply the folding. Currently, the operation will be replaced only if the input constant is a splat (i.e. requires little memory) or has in single user (similar to the already existing fold for constant transposes). This keeps the additionally required space low.

We implemented folds for various operators and would like to contribute them successively. Those folds are for:

  • tosa.add
  • tosa.cast
  • tosa.clamp
  • tosa.mul
  • tosa.pow
  • tosa.rsqrt

Diff Detail

Event Timeline

TinaAMD created this revision.May 15 2023, 8:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2023, 8:04 AM
TinaAMD requested review of this revision.May 15 2023, 8:04 AM
TinaAMD edited the summary of this revision. (Show Details)May 15 2023, 8:06 AM
TinaAMD added reviewers: eric-k256, rsuderman.
eric-k256 accepted this revision.May 15 2023, 5:16 PM
eric-k256 edited reviewers, added: jpienaar; removed: nicolasvasilache.
eric-k256 added a subscriber: jpienaar.

This seems like a reasonable fold to me, and a good heuristic to start with. Adding @jpienaar in case he sees anything I've missed.

This revision is now accepted and ready to land.May 15 2023, 5:16 PM

I can land now

rsuderman requested changes to this revision.Jun 15 2023, 11:20 AM

One change before landing. Everything is broken up between 3-4 files of <100 lines in length. It doesn't seem worth fracturing this up to this degree. Could you have TosaFolders.cpp and merge them all together, including the transpose pass)

This revision now requires changes to proceed.Jun 15 2023, 11:20 AM
TinaAMD updated this revision to Diff 534469.Jun 26 2023, 2:25 AM
TinaAMD edited the summary of this revision. (Show Details)

Thanks for the feedback!

I merged the changes into a single new file TosaFolders.cpp, it also includes the already pre-existing transpose fold.

If you are happy with the revision, could you commit the changes for me? As this is my first contribution, I don't have commit access yet.
Would sending you my email address by Discord work for you (username tinaamd there as well)?

I would suggest parts of the description as commit message:
Add constant fold for tosa.reciprocal, which can be applied if the input is a dense constant tensor. The reciprocal is computed for every element and the result is a tensor with the same dimensions as the input tensor.

As the input tensor might require a lot of memory and the folding might double the required memory, a heuristic decides when to actually apply the folding. Currently, the operation will be replaced only if the input constant is a splat (i.e. requires little memory) or has in single user (similar to the already existing fold for constant transposes). This keeps the additionally required space low.

If you are happy with the revision, could you commit the changes for me? As this is my first contribution, I don't have commit access yet.
Would sending you my email address by Discord work for you (username tinaamd there as well)?

I would suggest parts of the description as commit message:
Add constant fold for tosa.reciprocal, which can be applied if the input is a dense constant tensor. The reciprocal is computed for every element and the result is a tensor with the same dimensions as the input tensor.

As the input tensor might require a lot of memory and the folding might double the required memory, a heuristic decides when to actually apply the folding. Currently, the operation will be replaced only if the input constant is a splat (i.e. requires little memory) or has in single user (similar to the already existing fold for constant transposes). This keeps the additionally required space low.

Hey, I'm happy to commit this patch for you.

This revision was not accepted when it landed; it landed in state Needs Review.Jul 5 2023, 2:39 AM
This revision was automatically updated to reflect the committed changes.
mlir/include/mlir/Dialect/Tosa/Transforms/Passes.h