Skip to content

Add semiring(A @ B @ C) that applies semiring to both matmuls #501

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 18 commits into from
Nov 4, 2023

Conversation

eriknw
Copy link
Member

@eriknw eriknw commented Sep 5, 2023

This is an experiment and possible beginning of implementation to address #132 and #498. This allows a semiring to be applied to many matrix multiplies, such as gb.op.plus_plus(A @ B @ C @ D).

I believe this pattern does arise in practice, and I expect this to be used in a way that is readable and intuitive. Currently, this:

D << gb.op.plus_plus(A @ B @ C)

is equivalent to:

D << gb.op.plus_plus(op.plus_times(A @ B) @ C)

which may be surprising and counterintuitive.

TODO:

  • tests
  • document (where/how?)
  • seek consensus
  • decide if we want to do similar for ewise infix such as binaryop(A | B & C) (do not support for now)

@codecov
Copy link

codecov bot commented Oct 29, 2023

Codecov Report

Merging #501 (baf4709) into main (7935e50) will increase coverage by 0.19%.
The diff coverage is 99.69%.

@@            Coverage Diff             @@
##             main     #501      +/-   ##
==========================================
+ Coverage   99.20%   99.39%   +0.19%     
==========================================
  Files          95       95              
  Lines       21291    21884     +593     
  Branches     3999     4138     +139     
==========================================
+ Hits        21121    21751     +630     
+ Misses        124       75      -49     
- Partials       46       58      +12     

@eriknw eriknw marked this pull request as ready for review October 30, 2023 03:35
@eriknw
Copy link
Member Author

eriknw commented Oct 30, 2023

Hey @jim22k @SultanOrazbayev @michelp, this is pretty much ready if you want to take a look--I just need to write the error message when mixing infix notations such as binop(x & y | z).

This allows operators for ewise add, ewise mult, and matrix multiplication to apply to all the operands inside the operator when using infix notation such as plus(a | b | c | d) or plus_plus(A @ B @ C). This doesn't do any fancy reordering, applying of masks, or reuse of intermediates--it simply automatically computes the intermediate values (regardless of gb.config["autocompute"] setting).

Mixing ewise infix notation now raises: binop(x & y | z) is not allowed. This is because I think many people get tripped up by the operator precedence of & and |. The only time this may be a nuisance is if you are dealing with boolean values and are comfortable using & and | together, but I think it's better for the sake of future readers to require explicit lor and land as appropriate.

Applying operators like this also only apply to infix notation. plus(a | b | c) is not the same as a.ewise_add(b | c, plus) or (a | b).ewise_add(c, plus) (both of which are kind of weird and will probably raise).

Overall I think this change is for the better. I think the main "gotcha" this introduces is this:

>>> C = A @ B
>>> E << min_plus(C @ D)

Did they mean C = min_plus(A @ B) or C = plus_times(A @ B)? This could be avoided many different ways (depending on what is intended):

  1. C = (A @ B).new()
  2. C << A @ B
  3. C = semiring(A @ B)
  4. C << semiring(A @ B)
  5. AatB = A @ B # Using a better name to indicate an operator may still be applied

Hence, it's best practice to either be explicit with a semiring, call .new(), or assign into an output via <<.

Unless we want to detect whether an expression is assigned or inlined (which is possible, but let's not introduce that voodoo magic if we don't absolutely need to), this is the tradeoff we are stuck with. I think the multi-infix notation can be used well, as @alugowski did in #498. Edge cases exist here no matter what we do (I think), and I'm inclined to support what multiple users expected to work, which is what this PR does. This PR goes further by detecting and disallowing other syntax that is likely to be incorrect.

When we discussed this possibility before in #132, @ParticularMiner liked applying the operator to all infix operands as done in this PR (see: #132 (comment))

@eriknw
Copy link
Member Author

eriknw commented Oct 30, 2023

This PR should now be easier to review b/c I added unrelated changes into main via #517.

@jim22k
Copy link
Member

jim22k commented Oct 31, 2023

I'm okay with this approach. I agree that gb.min_plus(A @ B @ C) is more likely than X = A @ B; gb.min_plus(X @ C) and there will be some confusion either way. Such is a nature of delayed computation systems.

@eriknw eriknw merged commit c6d1e31 into python-graphblas:main Nov 4, 2023
@eriknw
Copy link
Member Author

eriknw commented Nov 4, 2023

This is in--thanks all!

I found it nice to use this syntax when creating #518 (I wrote semiring(I @ A @ I))

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.

2 participants