Skip to content

Cover more cases of log1mexp stabilization #1483

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

Merged
merged 5 commits into from
Jun 18, 2025
Merged

Conversation

lciti
Copy link
Contributor

@lciti lciti commented Jun 17, 2025

This PR fixes #1476 by reimplementing the rewrite log1pmexp_to_log1mexp using is_neg so it works with neg() as well as mul(-1.0, ...), which is the canonicalised form. It adds a test for #1476 (which now passes) as well as other tests for expressions that should simplify to log1mexp (I realised that expressions like log(-exp(x) + 1) would not work). It then implements the missing rewrite log(-expm1(x)) -> log1mexp(x) to make these additional tests pass.

Description

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pytensor--1483.org.readthedocs.build/en/1483/

Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Looks good, just a question about readability at the cost of performance. We can merge as is if you disagree

@ricardoV94 ricardoV94 changed the title Reimplement the rewrite log1pmexp_to_log1mexp using is_neg (fixes #1476) Reimplement the rewrite log1pmexp_to_log1mexp using is_neg to cover more cases Jun 17, 2025
@ricardoV94 ricardoV94 added the enhancement New feature or request label Jun 17, 2025
@lciti
Copy link
Contributor Author

lciti commented Jun 17, 2025

I am not sure if you noticed, there is an opportunity to implement log1pexp_to_softplus directly within log1pmexp_to_log1mexp (which could be called log1p_pm_exp_to_log1pexp_log1mexp or sth like that) and get rid of a separate log1pexp_to_softplus function. The only disadvantage would be the inability to include/exclude just one of the two rewrites. Please let me know what you prefer.

@ricardoV94
Copy link
Member

Yeah I think we can merge them, they're similar in nature

@lciti
Copy link
Contributor Author

lciti commented Jun 18, 2025

I think it's ready to go.

@ricardoV94 ricardoV94 changed the title Reimplement the rewrite log1pmexp_to_log1mexp using is_neg to cover more cases Cover more cases of log1mexp stabilization Jun 18, 2025
@ricardoV94
Copy link
Member

Great, running tests

@lciti
Copy link
Contributor Author

lciti commented Jun 18, 2025

Ops, I think found a mistake. I'll commit asap.

Copy link

codecov bot commented Jun 18, 2025

Codecov Report

Attention: Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.

Project coverage is 82.01%. Comparing base (6aeed97) to head (9f9bc8b).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/tensor/rewriting/math.py 95.45% 0 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (95.45%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1483      +/-   ##
==========================================
+ Coverage   82.00%   82.01%   +0.01%     
==========================================
  Files         214      214              
  Lines       50413    50431      +18     
  Branches     8902     8907       +5     
==========================================
+ Hits        41342    41362      +20     
+ Misses       6863     6861       -2     
  Partials     2208     2208              
Files with missing lines Coverage Δ
pytensor/tensor/rewriting/math.py 89.49% <95.45%> (+0.24%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ricardoV94 ricardoV94 merged commit d3bbc20 into pymc-devs:main Jun 18, 2025
72 of 73 checks passed
@ricardoV94
Copy link
Member

Thanks @lciti ! Let us know if you find more things to tweak

@lciti
Copy link
Contributor Author

lciti commented Jun 18, 2025

Many thanks to you for your support and, more in general, for developing/maintaining such an invaluable research tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request graph rewriting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The rewrite log1pmexp_to_log1mexp is not applied
2 participants