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

Exploit repeated named expressions in identify_variables #3190

Merged
merged 25 commits into from
May 8, 2024

Conversation

Robbybp
Copy link
Contributor

@Robbybp Robbybp commented Mar 12, 2024

Fixes #3187

Summary/Motivation:

identify_variables can be responsible for a non-trivial portion of runtime for models with many occurrences of large named expressions. This PR adds a new variable visitor that supports descend_into_named_expressions=False and updates get_vars_from_components to use this new visitor to avoid re-checking the same named expression multiple times.

The potential performance hit is most pronounced when identifying variables for many expressions in a model, so I've only used this visitor in get_vars_from_components. This functionality could also be useful in identify_variables, but I wanted to get some feedback before implementing this.

Edit: This PR now updates identify_variables.

Changes proposed in this PR:

  • Add a new visitor for identifying variables in an expression (for now, called _StreamVariableVisitor... I'm open to suggestions)
  • Update identify_variables to use this visitor and add an optional named_expression_cache argument
  • Use named_expression_cache in get_vars_from_components

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@Robbybp
Copy link
Contributor Author

Robbybp commented Mar 12, 2024

Test failures are in a GDP baseline file comparison, likely due to changes in variable order generated by get_vars_from_components.

@blnicho
Copy link
Member

blnicho commented Mar 12, 2024

We discussed a few implementation changes during the dev call today so I'm going to mark this as a draft for now

@blnicho blnicho marked this pull request as draft March 12, 2024 21:53
@Robbybp
Copy link
Contributor Author

Robbybp commented Mar 15, 2024

I had to update some tests to use ComponentSet to get test_visitor.py to work, so this doesn't give the same variable order that identify_variables used to. For that reason, I don't expect tests to pass yet.

I think the reason for the difference is that _VariableVisitor uses xbfs_yield_leaves, while StreamBasedExpressionVisitor seems to use DFS (at least in the recursive implementation).

@Robbybp
Copy link
Contributor Author

Robbybp commented Mar 15, 2024

With this patch, the motivating example from #3186 looks pretty good:

Identifier                                     ncalls   cumtime   percall      %
--------------------------------------------------------------------------------
root                                                1     2.348     2.348  100.0
     ---------------------------------------------------------------------------
     full model post-solve                          1     0.460     0.460   19.6
     solve-scc                                      1     1.888     1.888   80.4
                          ------------------------------------------------------
                          identify-variables      546     0.188     0.000   10.0
                          other                   n/a     1.700       n/a   90.0
                          ======================================================
     other                                        n/a     0.000       n/a    0.0
     ===========================================================================
================================================================================

@Robbybp Robbybp changed the title Exploit repeated named expressions in get_vars_from_components Exploit repeated named expressions in identify_variables Mar 19, 2024
@Robbybp Robbybp marked this pull request as ready for review March 19, 2024 14:04
Copy link

codecov bot commented Apr 3, 2024

Codecov Report

Attention: Patch coverage is 96.29630% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 88.40%. Comparing base (c849905) to head (22e11e0).
Report is 146 commits behind head on main.

Files Patch % Lines
pyomo/core/expr/visitor.py 96.05% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3190   +/-   ##
=======================================
  Coverage   88.40%   88.40%           
=======================================
  Files         846      846           
  Lines       95156    95207   +51     
=======================================
+ Hits        84118    84169   +51     
  Misses      11038    11038           
Flag Coverage Δ
linux 86.34% <93.82%> (+<0.01%) ⬆️
osx 76.18% <93.82%> (+0.01%) ⬆️
other 86.54% <93.82%> (+<0.01%) ⬆️
win 83.84% <93.82%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@jsiirola jsiirola left a comment

Choose a reason for hiding this comment

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

I think there is a significantly more compact implementation of this, but there is certainly nothing wrong with this. I propose merging this, and defer improvements into a later PR.

@mrmundt mrmundt merged commit f8459a7 into Pyomo:main May 8, 2024
32 of 33 checks passed
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.

Exploit repeated named expressions when identifying variables in expressions
5 participants