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

Allow empty sub-containers in reductions #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions arraycontext/container/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def rec_map_reduce_array_container(

or any other such traversal.
"""
def rec(_ary: ArrayOrContainerT) -> ArrayOrContainerT:
def rec(_ary: ArrayOrContainerT) -> Optional[ArrayOrContainerT]:
if type(_ary) is leaf_class:
return map_func(_ary)
else:
Expand All @@ -473,11 +473,22 @@ def rec(_ary: ArrayOrContainerT) -> ArrayOrContainerT:
except NotAnArrayContainerError:
return map_func(_ary)
else:
return reduce_func([
rec(subary) for _, subary in iterable
])
subary_results = [
rec(subary) for _, subary in iterable]
filtered_subary_results = [
result for result in subary_results
if result is not None]
if len(filtered_subary_results) > 0:
return reduce_func(filtered_subary_results)
else:
return None

return rec(ary)
result = rec(ary)

if result is None:
raise ValueError("cannot reduce empty array container")
Comment on lines +488 to +489
Copy link
Owner

Choose a reason for hiding this comment

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

Personally, I think reducing over an empty container is well-defined: just return the neutral element. reduce_func should be trusted to do the right thing. What was the reasoning for putting in this error message? Basically, what I'm pushing for is to get rid of it and return reduce_func([]) instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If reduce_func worked for an empty list, I think this PR wouldn't be needed, right?

The motivation for this was for things like actx.np.max where reduce_func doesn't (currently) behave nicely for empty inputs. Should that maybe be fixed instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

For reference, calling np.max on an empty list raises

>>> np.max([])
ValueError: zero-size array to reduction operation maximum which has no identity

and requires specifying an initial value with np.max([], initial=np.inf). We should probably do something like that in actx.np too.

Copy link
Owner

Choose a reason for hiding this comment

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

The motivation for this was for things like actx.np.max where reduce_func doesn't (currently) behave nicely for empty inputs.

That's a great point. I had not thought far enough to realize that, but I agree now.

Should that maybe be fixed instead?

I think that's exactly what we should do.

We should probably do something like that in actx.np too.

I agree. While I wasn't loving numpy's interface of "no implicit neutral" element at first, I think it makes sense from the perspective of integers. I think we should aim to replicate this, both at the level of actx.np, and for the array container reductions under discussion here.

One subtlety is the ValueError for empty arrays. For symbolically-shaped pytato arrays, it may not be possible to do a perfect job.

Copy link
Owner

Choose a reason for hiding this comment

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

Did some work towards that:

That latter PR needs some more work done to it. @majosm, could you take a look?


return result


def rec_multimap_reduce_array_container(
Expand All @@ -503,12 +514,23 @@ def rec_multimap_reduce_array_container(
# NOTE: this wrapper matches the signature of `deserialize_container`
# to make plugging into `_multimap_array_container_impl` easier
def _reduce_wrapper(ary: ContainerT, iterable: Iterable[Tuple[Any, Any]]) -> Any:
return reduce_func([subary for _, subary in iterable])
filtered_subary_results = [
result for _, result in iterable
if result is not None]
if len(filtered_subary_results) > 0:
return reduce_func(filtered_subary_results)
else:
return None

return _multimap_array_container_impl(
result = _multimap_array_container_impl(
map_func, *args,
reduce_func=_reduce_wrapper, leaf_cls=leaf_class, recursive=True)

if result is None:
raise ValueError("cannot reduce empty array container")
Comment on lines +529 to +530
Copy link
Owner

Choose a reason for hiding this comment

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

As above.


return result

# }}}


Expand Down