-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adding einsum atom
#2970
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
Adding einsum atom
#2970
Conversation
|
Benchmarks that have stayed the same: |
Transurgeon
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NDevanathan sorry it took so long to review your code. I noticed one small nitpicky import in your einsum implementation.
Otherwise I think overall the code is a bit difficult to follow but definitely seems to be correct (seeing all the tests you wrote pass). We still need to understand why the flaky test is happening (it is really so weird! could it be because we are importing some numpy core functions?).
One thing on my end would be to add more docstrings and also add the einsum atom to the documentation somewhere (there is a table and also some API references to update).
Thanks for doing this, really great work!
cvxpy/atoms/affine/einsum.py
Outdated
|
|
||
| from cvxpy.atoms.affine.binary_operators import ( | ||
| multiply, | ||
| reshape, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked and there is no reshape in binary_operators? Are you sure you are importing the right one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I fixed the reshape import.
I updated the docstrings to be better. I will try to add einsum to the docs. |
I think there might be an issue with how I implement einsum and how DGP problems are canonicalized. The error occurs at solve time, and DCP solves work. So, I think the issue occurs in doing the DGP canonicalization chain of einsum. I will also try to dig into this issue. |
|
Sorry for the great delay, I have fixed the issues that were leading to test failures I think @Transurgeon . I will add docs today or tomorrow. |
PTNobel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work Nikhil; the tests look great.
Here's what I'd like to see:
- Some plan to handle if NumPy changes their internal function names in the future (see below)
- A tad more documentation at the top of einsum on either the algorithm being applied, or just linking to a reference.
cvxpy/atoms/affine/einsum.py
Outdated
| Expression | ||
| The contracted expression. | ||
| """ | ||
| # Initial parsing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment that provides the overview of what happens in this function? It can be a reference to external documentation or what the implementation is based on, but this is pretty opaque and involves a lot of complicated functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in most recent commit.
PTNobel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last two comments!
cvxpy/atoms/affine/einsum.py
Outdated
| is cubic in the number of distinct subscripts. We typically expect the greedy | ||
| search to produce the optimal path for most problems. | ||
| Here, einsum is implemented using the CVXPY sum, multiply, permute_dims, and reshape atoms. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't need to be in the doc comment! This is just for devs, so you can put it in a comment block below the doc comment!
cvxpy/atoms/affine/einsum.py
Outdated
| input_subscripts, output_subscript = subscripts.split("->") | ||
| ``` | ||
| The rest is validation. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are empty spaces on this line.
cvxpy/atoms/affine/einsum.py
Outdated
| """Note for maintainers: | ||
| Here, einsum is implemented using the CVXPY sum, multiply, permute_dims, and reshape atoms. | ||
| The implementation proceeds as follows: | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
Description
I implemented an
einsumatom (based onnumpy.einsum). The atom is affine and provides a simple interface for describing linear operations on tensors in CVXPY expressions. I wrote tests for the atom incvxpy/tests/test_einsum.py. All but one of the tests is passing.The last block of asserts in
test_einsum_solve()incvxpy/tests/test_einsum.pyfails (at solve time) non-deterministically on my machine (it seems to fail roughly 50% of the time). Specifically, I get an error reshaping a CVXPY expression with shape(4,)to shape(4,1), and I am unsure of how to resolve this issue.Issue link (if applicable): N/A
Type of change
Contribution checklist