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

Add support for clamp op #1093

Merged
merged 1 commit into from
Nov 14, 2024
Merged

Add support for clamp op #1093

merged 1 commit into from
Nov 14, 2024

Conversation

mmanzoorTT
Copy link
Contributor

@mmanzoorTT mmanzoorTT commented Oct 28, 2024

  • Add end-to-end implementation of the ops.
  • Add stablehlo to ttir conversion for clamp op.

@mmanzoorTT mmanzoorTT force-pushed the mmanzoor/clamp-op branch 5 times, most recently from 9e02a6c to e577be2 Compare October 29, 2024 22:08
@mmanzoorTT mmanzoorTT changed the title Clamp op Add support for clamp and clip op Oct 29, 2024
@mmanzoorTT mmanzoorTT marked this pull request as ready for review October 29, 2024 22:13
Copy link
Contributor

@tapspatel tapspatel left a comment

Choose a reason for hiding this comment

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

add a clamp and clip test for perf_unit under Silicon/TTNN/perf_unit

@mmanzoorTT
Copy link
Contributor Author

add a clamp and clip test for perf_unit under Silicon/TTNN/perf_unit

@tapspatel tests added. thanks

Copy link
Contributor

@tapspatel tapspatel left a comment

Choose a reason for hiding this comment

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

flatbuffer files and .mlir files look good!

@mmanzoorTT mmanzoorTT mentioned this pull request Oct 30, 2024
@sdjordjevicTT
Copy link
Contributor

sdjordjevicTT commented Oct 30, 2024

Clip is just an alias for clamp op, do we want both ops in TTIR?

@mmanzoorTT
Copy link
Contributor Author

Clip is just an alias for clamp op, do we want both ops in TTIR?

I added it to have one-to-one mapping between TTIR and TTNN. I can remove ttir.clip op.

@sdjordjevicTT
Copy link
Contributor

Clip is just an alias for clamp op, do we want both ops in TTIR?

I added it to have one-to-one mapping between TTIR and TTNN. I can remove ttir.clip op.

I am not blocking, just wondering what would be the best solution. I saw that TTNN also aliases one op to another, hence maybe we should keep them both...

@sdjordjevicTT
Copy link
Contributor

We agreed to keep a single op per the discussion here:
#852 (comment)

Please keep only the clamp and remove the clip op from this PR.

@mmanzoorTT
Copy link
Contributor Author

We agreed to keep a single op per the discussion here: #852 (comment)

Please keep only the clamp and remove the clip op from this PR.

@sdjordjevicTT Do you mean to remove ttir.clip op only OR remove it entirely (ttir.clip op along with ttnn.clip and flatbuffer/runtime support)?

@sdjordjevicTT
Copy link
Contributor

We agreed to keep a single op per the discussion here: #852 (comment)
Please keep only the clamp and remove the clip op from this PR.

@sdjordjevicTT Do you mean to remove ttir.clip op only OR remove it entirely (ttir.clip op along with ttnn.clip and flatbuffer/runtime support)?

We synced over the slack and agreed to remove the ttir.clip op entirely.

@mmanzoorTT
Copy link
Contributor Author

@sdjordjevicTT clip op removed entirely.

@mmanzoorTT mmanzoorTT changed the title Add support for clamp and clip op Add support for clamp op Nov 5, 2024
Copy link
Contributor

@jnie-TT jnie-TT left a comment

Choose a reason for hiding this comment

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

Runtime changes look good!

-> %out = [[2, 2, 2, 3, 4, 5, 5, 5]]
}];

let arguments = (ins Variadic<AnyRankedTensor>:$inputs,
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are using TTNNToFlatbuffer.cpp::createEltwiseOp(...) to handle both binary and unary ops. Variadic inputs provide a way to handle all eltwise ops with same code. I can refactor the code and have separate function to handle unary ops. Then we won't need variadic inputs. I can refactor it in a separate PR. what do you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, I'm not sure, but I think we are breaking some logical structure here. If we leave it like this, we are not making an abstraction of unary op in MLIR, but we do in the runtime. We should either inherit from the unary operation table definition in MLIR or handle this operation separately in runtime, similar to how we manage the Softmax operation. What are your thoughts?

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 am more inclined towards refactoring createEltwiseOp into 1) createEltwiseUnaryOp and 2) createEltwiseBinaryOp (and match it with runtime abstraction). We should also convert variadic input/output definitions to non-variadic definitions for unary/binary ops which will make TTIR/TTNN dialect more precise.
My initial runtime implementation handled clamp op independently similar to Softmax op but we decided to handle clamp op as unary to match tt-metal implementation.

Copy link
Contributor

@jnie-TT jnie-TT Nov 13, 2024

Choose a reason for hiding this comment

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

@mmanzoorTT @sdjordjevicTT chiming in here about the runtime implementation. Softmax is implemented separately because it's not modelled as an eltwise op in ttnn. It's a separate normalization op in the ttnn lib, therefore we model it as an independent normalization op in our runtime. Clamp however is modelled as an eltwise unary op in ttnn lib so we should model it as a unary op in our runtime as well. This follows the general north star of our runtime reflecting the modelling of ttnn runtime as closely as possible

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarification folks, really appreciated it. I see, in general for unary ops, we are extending the TTNN_ElementwiseUnaryOp which further extends TTNN_ElementwiseOp and it actually has variadic inputs and outputs and is modeled as DPS.

I'm wondering if we could implement a similar structure for elementwise operations that are not DPS. Instead of simply inheriting from TTNN_Op, could we establish a hierarchy as follows:

TTNN_ClampOp -> TTNN_ElementwiseUnaryOpNonDPS -> TTNN_ElementwiseOpNonDPS -> TTNN_Op

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @jnie-TT for the input. I agree with you @sdjordjevicTT. I'll create an issue for this change and handle them in a separate PR. We have few other non DPS ops and I prefer to change all of them together.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

issue created #1270. Please have a look @sdjordjevicTT

* Add end-to-end implementation of the ops.
* Add stablehlo to ttir conversion for clamp op.
@mmanzoorTT mmanzoorTT merged commit 0432a34 into main Nov 14, 2024
17 checks passed
@mmanzoorTT mmanzoorTT deleted the mmanzoor/clamp-op branch January 21, 2025 15:24
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.

5 participants