Skip to content

Conversation

@kit1980
Copy link
Contributor

@kit1980 kit1980 commented Jan 6, 2025

Suggest using torch.logsumexp(x) instead of torch.log(torch.sum(torch.exp(x)).

https://pytorch.org/docs/stable/generated/torch.logsumexp.html

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 6, 2025
@kit1980 kit1980 marked this pull request as ready for review January 6, 2025 23:13
@kit1980 kit1980 requested a review from sbrugman January 6, 2025 23:14
Copy link
Contributor

@clee2000 clee2000 left a comment

Choose a reason for hiding this comment

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

Does the README need to be updated with this new rule?

@kit1980
Copy link
Contributor Author

kit1980 commented Jan 6, 2025

Does the README need to be updated with this new rule?

Eventually, not everything is listed anyway. It should be probably generated from the code.

Comment on lines +10 to +11
y = torch.log(torch.sum(torch.exp(x), 1, keepdim=True) + 2.5)
y = torch.log(torch.sum(torch.exp(x) + 2.5, 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you want to provide a bit more anti-patterns to make sure you don't assume that torch.log always have a two tested functions?

Suggested change
y = torch.log(torch.sum(torch.exp(x), 1, keepdim=True) + 2.5)
y = torch.log(torch.sum(torch.exp(x) + 2.5, 1))
y = torch.log(torch.sum(torch.exp(x), 1, keepdim=True) + 2.5)
y = torch.log(torch.sum(torch.exp(x) + 2.5, 1))
y = torch.log(1 + x)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some more tests. (Not exactly torch.log(1 + x), as it triggers another rule for log1p).

@malfet
Copy link
Contributor

malfet commented Jan 6, 2025

A bit unrelated, is there already a rule that say torch.log(torch.exp(x)) should be transformed to x, torch.sqrt(torch.sqr(x)) into torch.abs(x) and so on?

@kit1980
Copy link
Contributor Author

kit1980 commented Jan 6, 2025

A bit unrelated, is there already a rule that say torch.log(torch.exp(x)) should be transformed to x, torch.sqrt(torch.sqr(x)) into torch.abs(x) and so on?

No, is it common? Should be easy to add the checkers.

@kit1980 kit1980 merged commit 28f1a5f into main Jan 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants