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

Introduce ReductionOperation class, accept 'initial' in reductions #238

Merged
merged 1 commit into from
Dec 29, 2021

Conversation

inducer
Copy link
Owner

@inducer inducer commented Dec 28, 2021

This replaces the strings we used to use to identify reductions with something a bit more structured. The impulse for this came from inducer/arraycontext#129, which needs to supply initial to array reductions to match the numpy interface for empty min/max reductions. This only adds very minimal support for supplying initial, in that it allows the neutral element to be passed (and obtaining that is what led to the structured reduction op types), and it better mimics numpy's behavior for empty reductions.

cc @majosm

Copy link
Collaborator

@kaushikcfd kaushikcfd left a comment

Choose a reason for hiding this comment

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

This almost looks great! Found some minor numpy compat issues, feel free to merge after that.

Thanks!

pytato/reductions.py Outdated Show resolved Hide resolved
pytato/reductions.py Outdated Show resolved Hide resolved


def prod(a: Array, axis: Optional[Union[int, Tuple[int]]] = None) -> Array:
def prod(a: Array, axis: Optional[Union[int, Tuple[int]]] = None,
initial: Any = 1) -> Array:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be _NoValue as well? Numpy does it, see https://numpy.org/doc/stable/reference/generated/numpy.prod.html#numpy.prod.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think this is OK. Numpy will return 1 for an empty prod. I think for an immutable object like 1 it does not matter whether that's done via _NoValue and then setting internally, or directly like this.

pytato/reductions.py Outdated Show resolved Hide resolved
pytato/reductions.py Outdated Show resolved Hide resolved
pytato/reductions.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@kaushikcfd kaushikcfd left a comment

Choose a reason for hiding this comment

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

Found out some more places where the implementation disagrees Numpy, good to go after that.

pytato/reductions.py Outdated Show resolved Hide resolved
pytato/reductions.py Show resolved Hide resolved
@inducer inducer enabled auto-merge (rebase) December 29, 2021 17:32
@inducer inducer merged commit 8cf2575 into main Dec 29, 2021
@inducer inducer deleted the reduction-op-obj branch December 29, 2021 18:06
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