Skip to content
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

[TOSA] Add upsample_nearest2d, split_dim, outer, GELU tanh mode and misc #3886

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

justin-ngo-arm
Copy link
Contributor

  • Add Torch to TOSA lowering for the following ops:
    • torch.aten.upsample_nearest2d
    • torch.aten.upsample_nearest2d.vec
    • torch.aten.outer
    • torch.prims.split_dim
  • Add Tanh approximation mode for GELU lowering
  • Add different types support for compare ops
  • Add different input and output types support for linalg vector norm lowering
  • Update xfail with new e2e results
  • Add new LIT tests to basic.mlir

Change-Id: I7b1d44d94319cf94fcc9d234cc07708ef9ce321e

- Add Torch to TOSA lowering for the following ops:
  + torch.aten.upsample_nearest2d
  + torch.aten.upsample_nearest2d.vec
  + torch.aten.outer
  + torch.prims.split_dim
- Add Tanh approximation mode for GELU lowering
- Add different types support for compare ops
- Add different input and output types support for linalg vector norm
  lowering
- Update xfail with new e2e results
- Add new LIT tests to basic.mlir

Signed-off-by: Justin Ngo <[email protected]>
Change-Id: I7b1d44d94319cf94fcc9d234cc07708ef9ce321e
@sjarus sjarus requested a review from sahas3 November 21, 2024 19:15
// "tanh" approximate
// GELU(x) = 0.5 * x * (1 + Tanh(sqrt(2/pi) * (x + 0.044715 * x^3))
auto selfShape = selfType.getShape();
auto numElem = std::accumulate(selfShape.begin(), selfShape.end(), 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes the shape is all static right? Can you add a failure check for dynamic shape?

Alternatively, can't we rely on broadcasting semantics of TosaOps to correctly expand the shape even for dynamic dims if these constants are defined with size 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a static shape check for tanh approximate. I've tried using size 1 constant but for some reasons it crashed fx_importer_target (specifically the ElementwiseGeluApproximateTanhModule_basic test), so I will resort to static shape requirement for now since TOSA currently only supports static shape.

lib/Conversion/TorchToTosa/TorchToTosa.cpp Show resolved Hide resolved
self, rewriter.getDenseI64ArrayAttr(reshapedSelfShape));

// Calculate PyTorch-styled gather indices
SmallVector<int32_t> targetIndicesVec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be int64_t as well since all the other types are int64_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently setting it int32_t since other places in the code that use TOSA GatherOp or ScatterOp all have i32 indices conversion/definition (even in your AtenIndexTensorHackedTwin commit about a month ago, you also used i32 indices conversion). I will double-check this matter later and if it needs to be changed then I will change all occurrences in a later PR.

static_cast<double>(outputWidth) / static_cast<double>(selfWidth);
}

if (isOutputSizeNone) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit: can be merged with the previous if block since isOutputSizeNone implies !isScaleFactorsNone

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've rewritten the if-else flow here to make it clearer and more concise. It should address this nit too.

test/Conversion/TorchToTosa/basic.mlir Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants