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 more checks for multiplication results #16146

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

Conversation

knightXun
Copy link
Contributor

Add positive checks for multiplication results

This commit introduces a new function that determines if the result of a multiplication is a positive number. It handles the cases where both operands are negative or both are positive, ensuring that the result is treated as a positive number if the product of two negatives is involved.

The function checks the sign of the operands and applies the mathematical rule that the product of two negative numbers is positive. This is crucial for ensuring the correctness of calculations in various scenarios, such as financial calculations or scientific computations where the sign of the result is significant.

@NaiyerRizz NaiyerRizz self-assigned this Aug 16, 2024
@NaiyerRizz
Copy link

Hi @knightXun
Can you please add a test case for the same.
Thank you

@NaiyerRizz NaiyerRizz added the kokoro:force-run Forces CI to rerun label Aug 16, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Forces CI to rerun label Aug 16, 2024
@NaiyerRizz
Copy link

Hi @knightXun
Can you please look into it as checks are failing
Thank you

@knightXun knightXun force-pushed the alge branch 3 times, most recently from 8697315 to e145f3b Compare August 23, 2024 02:04
@NaiyerRizz NaiyerRizz added the kokoro:force-run Forces CI to rerun label Aug 23, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Forces CI to rerun label Aug 23, 2024
@knightXun
Copy link
Contributor Author

@NaiyerRizz nicely ping.

@NaiyerRizz NaiyerRizz requested a review from loislo August 26, 2024 09:34
@NaiyerRizz NaiyerRizz added the kokoro:force-run Forces CI to rerun label Aug 26, 2024
@kokoro-team kokoro-team removed the kokoro:force-run Forces CI to rerun label Aug 26, 2024
@NaiyerRizz NaiyerRizz requested review from penpornk and removed request for penpornk August 28, 2024 06:58
@NaiyerRizz
Copy link

Hi @penpornk
Can you please have a look on this PR
Thank you

@knightXun
Copy link
Contributor Author

@beckerhe Can you help me review this PR?

@beckerhe
Copy link
Member

beckerhe commented Sep 2, 2024

Hi,
I'm not familiar enough with the AlgebraicSimplifier to do an effective review.

@reedwm can you take a look or forward this to someone?

Comment on lines +507 to +508
case HloOpcode::kExp:
return true;
Copy link
Member

Choose a reason for hiding this comment

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

Exp can be zero if the argument is negative infinity. Similarly, for the added kMultiply case where the LHS and RHS are positive, the result can underflow to zero.

Hmmm, but I see existing cases have similar problems, even without this PR. E.g. kPower can also be zero if the RHS is negative infinity. Perhaps we don't care about cases where underflow happen, or where arguments are infinity? @akuegel, do you have an opinion here?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I somehow missed this question. Good point, I think we should not return true for kExp. And same for kPower. We can return true for them in the IsNonNegative function.
Also, good catch regarding underflow to zero, I think we should also not return true for kMultiply.

Comment on lines +523 to +526
if (!IsNonNegative(hlo->operand(0), options) &&
!IsNonNegative(hlo->operand(1), options)) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Remove this case. If either the LHS or RHS is zero, the output will be zero, not positive.

@loislo loislo removed their request for review September 9, 2024 11:16
@NaiyerRizz
Copy link

Hi @knightXun
Can you please look into comment provided by @reedwm
Thank you

@penpornk penpornk removed their request for review September 16, 2024 14:20
Copy link
Member

@akuegel akuegel left a comment

Choose a reason for hiding this comment

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

@knightXun please move these checks to the IsNonNegative function. Like @reedwm pointed out, we can have zero results.

Comment on lines +507 to +508
case HloOpcode::kExp:
return true;
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I somehow missed this question. Good point, I think we should not return true for kExp. And same for kPower. We can return true for them in the IsNonNegative function.
Also, good catch regarding underflow to zero, I think we should also not return true for kMultiply.

@dimitar-asenov
Copy link
Member

@knightXun I see this PR has been inactive for a while. If you're still interested in merging it, could you please do the requested changes above? Otherwise, we will close it after some more time of inactivity.

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.

7 participants