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

[WIP] add a preprocessor #207

Closed
wants to merge 4 commits into from
Closed

[WIP] add a preprocessor #207

wants to merge 4 commits into from

Conversation

jcmgray
Copy link
Collaborator

@jcmgray jcmgray commented Nov 12, 2022

Description

This adds a potential preprocessor that takes an equation and converts it into a minimal form with the following simplifications:

  • All indices which only appear on a single input (and not the output) are summed over.
  • All indices which appear multiple times on the same term are traced.
  • All scalars are multiplied into the smallest other term
  • All terms with the same indices are multiplied (hamadard product / elementwise) into a single term ('de-duplication').

along with a function, that transforms input arrays into the inputs for the new simplified eq. It is a function so that it could be part of a contract 'expression'.

This would address (#114, #99, #112, #167, #189, ...).

Example from the docstring:

eq = ',ab,abee,,cd,cd,dd->ac'
arrays = helpers.build_views(eq)
new_eq, simplifier = make_simplifier(eq)
new_eq
# 'ab,cd,d->ac'
sarrays = simplifier(arrays)
oe.contract(new_eq, *sarrays)
# array([[0.65245661, 0.14684493, 0.42543411, 0.32350895],
#        [0.38357005, 0.08632807, 0.25010672, 0.19018636]])

Todos

Notable points that this PR has either accomplished or will accomplish.

  • when and how should this be called?
  • return a partial path rather than a 'simplifier'?
  • should shape information (size_dict) be used and propagated for use with a contraction expression (and so that the smallest tensor to multiply scalars into can be most accurately chosen)?
  • other simplifications are possible (e.g. hadamard deduplication up to transpose, performing all non-rank increasing contractions)...
  • currently the parsing is a single sweep, should it iteratively sweep til no more changes? E.g. currently examples like ab,ab-> transform to ab-> but if processed again could transform to ->

Status

  • Ready to go

@codecov
Copy link

codecov bot commented Nov 12, 2022

Codecov Report

Merging #207 (d155c1f) into master (4c7eb61) will decrease coverage by 6.18%.
The diff coverage is 0.00%.

Copy link
Owner

@dgasmith dgasmith left a comment

Choose a reason for hiding this comment

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

Makes sense, it would be good to start seeing some test here to find edge cases. A few notes:

  • I would make a comment pass, see if we can highlight which operation we are reducing at each stage.
  • The input/output pairing worries me a little bit. I wonder if we leave this as "internal" oe.contract(..., preprocess=True)
  • It would be good to have a pathway for the propocess=True pathway.

Super cool addition!

from .parser import find_output_str


def unique(it):
Copy link
Owner

Choose a reason for hiding this comment

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

Can we type these? I cannot quite tell what this is doing, somewhat looks like a set operation.

)


def make_simplifier(*args, backend="numpy"):
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
def make_simplifier(*args, backend="numpy"):
def expression_simplifier(*args, backend="numpy"):

array([[0.65245661, 0.14684493, 0.42543411, 0.32350895],
[0.38357005, 0.08632807, 0.25010672, 0.19018636]])
"""
try:
Copy link
Owner

Choose a reason for hiding this comment

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

We will need quite a few tests around this tech, too bad we cannot reproduce the original parsing tech here.

@jcmgray jcmgray closed this Sep 2, 2023
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