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

Using Python macros to accelerate Pyomo expression kernels #1161

Closed
whart222 opened this issue Nov 7, 2019 · 5 comments
Closed

Using Python macros to accelerate Pyomo expression kernels #1161

whart222 opened this issue Nov 7, 2019 · 5 comments

Comments

@whart222
Copy link
Member

whart222 commented Nov 7, 2019

One of the key ideas exploited in Julia's JuMP package is macros. These rewrite the Julia expression tree to optimize the creation of Julia data structures for JuMP constraints and expressions.

The macropy package provides support for similar syntactic macros in Python. Pyomo could leverage this capability to optimize the execution of expression construction.

Specifically, a decorator could be created for rule functions that replace them with functions that are reworked using syntactic macro operations to streamline the execution of expression setup. Consider the following rule:

def rule(model, i)
return 2*model.x[i] == model.y[i]
model.c = Constraint(range(1000000), rule=rule)

In every call to rule, Pyomo creates an expression tree. In so doing, Pyomo incrementally recognizes the monomial term 2*x[i], and it incrementally combines the LHS and RHS. This rule would be more efficiently written by directly creating the expression objects, rather than doing operator overloading.

Other developers have made a similar observation, and specifically they have noted that directly creating Pyomo's linear expression object leads to a significant speed-up in the construction of exprssions. We can automate this activity using syntactic macros.

NOTE:

  • Delving into AST expressions is fraught with peril, so this is a high risk/high reward activity
  • Support for the macropy package is not great, so this performance optimization idea is probably not something we'd want to commit to being a default behavior in Pyomo
  • There are limitations to macropy (and related macro ideas in Python). They override Python's import logic, so these tricks probably couldn't be used by users developing concrete models unless they used rules (and even then there may be some limitations).
@whart222
Copy link
Member Author

whart222 commented Nov 7, 2019

Here's a publication describing a similar idea for ML: https://www.sysml.cc/doc/2019/194.pdf

@whart222
Copy link
Member Author

Carl noted that we should consider having a generic mechanism for users to create a canonical-like expression, which should have a noticable impact on model writers.

@carldlaird
Copy link
Member

I think, if we are smart, this could set the bar for how much faster we could be on the expression creation and manipulation side. If we had an efficient way to build the canonical directly (with left, body, right as well), this would be the target for other efficient expression building.

@michaelbynum
Copy link
Contributor

This is different from, but strongly related to, templates. See #1153, which I am about to flush out.

@jsiirola
Copy link
Member

jsiirola commented May 8, 2020

Archived on the master Performance Proposals Issue (#1430). Closing this performance proposal until active development has begun.

@jsiirola jsiirola closed this as completed May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants