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

[AMDAIEFusePackIntoLoop] Handle case where there is no extract_slice #983

Merged
merged 2 commits into from
Dec 12, 2024

Conversation

newling
Copy link
Contributor

@newling newling commented Dec 11, 2024

This is very similar to #976

When the AIE grid we're using is m x n where one or both of m and n is 1, for a matmul we get pack ops that produce operands for matmuls directly. i.e. as opposed to pack->extract_slice->matmul we have pack->matmul.

TODO(newling) add a test and see if the pass can be simplified further.

/// A utility function specific to this pass which, given a value, would
/// traverse the def-chain till it either finds a tensor.extract_slice op or a
/// BlockArgument.
FailureOr<Operation *> getParentBeforeLoop(BlockArgument blkArg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add explanations for this utility function.

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 inlined the function

FailureOr<Operation *> getParentBeforeLoop(BlockArgument blkArg) {
Operation *parent = blkArg.getOwner()->getParentOp();
LoopLikeOpInterface loop = dyn_cast<LoopLikeOpInterface>(parent);
if (!loop) return failure();
Copy link
Contributor

Choose a reason for hiding this comment

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

emit error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No failure here isn't necessary

operand = defOp->getOperand(0);
// roll back through all the packs immediately preceding `operand`.
while (operand.getDefiningOp() &&
isa<tensor::PackOp>(operand.getDefiningOp())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can use while (isa_and_present<tensor::PackOp>(operand.getDefiningOp())).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 53 to 46
Operation *defOp = operand.getDefiningOp();
if (!defOp) return failure();
tensor::ExtractSliceOp sliceOp = dyn_cast<tensor::ExtractSliceOp>(defOp);
if (!sliceOp) return failure();
Copy link
Contributor

Choose a reason for hiding this comment

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

Like commented above, you can simply check dyn_cast_if_present<tensor::ExtractSliceOp>(operand.getDefiningOp()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

else {
if (auto parent =
dyn_cast_if_present<tensor::PackOp>(operand.getDefiningOp())) {
Block *genericBlock = genericOp->getBlock();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think here you need to use else if, and then add another else in which adding some debug message and return.

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'll change to else-if, but the logic is correct I think so won't return and don't think debug print message is useful here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Logically the else condition is not needed, but I was thinking it may good to add some debug message so people know why the pack is not fused into the loop in some specific situation. You can decide whether to add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The operand might not even come from a pack though, it could be anything...

@newling newling force-pushed the fuse_pack_without_slice branch from ad87490 to 7aacd04 Compare December 12, 2024 20:46
Copy link
Contributor

@yzhang93 yzhang93 left a comment

Choose a reason for hiding this comment

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

Thanks. Just one more comment.

}

// Case 2 outlined above.
else if (auto blkArg = dyn_cast<BlockArgument>(sliceOp.getSource())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use if instead.

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 don't want it to enter both statements which might happen if this is if

Copy link
Contributor

Choose a reason for hiding this comment

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

How would it enter both statements? After it enters the first it will return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no it doesn't?

@newling newling merged commit a55ee84 into nod-ai:main Dec 12, 2024
7 checks passed
@newling newling deleted the fuse_pack_without_slice branch December 12, 2024 23:06
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