Skip to content

BUG: Fix that reduce-likes honor out always (and live in the future)#20793

Merged
charris merged 1 commit intonumpy:mainfrom
seberg:evil-reducelike-no-value-based
Jan 12, 2022
Merged

BUG: Fix that reduce-likes honor out always (and live in the future)#20793
charris merged 1 commit intonumpy:mainfrom
seberg:evil-reducelike-no-value-based

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Jan 11, 2022

Reducelikes should have lived in the future where the out dtype
is correctly honoured always and used as one of the inputs.
However, when legacy fallback occurs, this leads to problems because
the legacy code path has 0-D fallbacks.

There are two probable solutions to this:

  • Live with weird value-based stuff here even though it was never
    actually better especially for reducelikes.
    (enforce value-based promotion)
  • Avoid value based promotion completely.

This does the second one, using a terrible hack by just mutating
the dimension of out to tell the resolvers that value-based logic
cannot be used.

Is that hack safe? Yes, so long nobody has super-crazy custom type
resolvers (the only one I know is PyGEOS and pyerfa and they are fine).
It also relies on the GIL of course, but...

The future? We need to ditch this value-based stuff, do annoying
acrobatics with dynamically created DType classes, or something similar
(so ditching seems best, it is topping my TODO list currently).

Testing this is tricky, running the test:

python runtests.py -t numpy/core/tests/test_ufunc.py::TestUfunc::test_reducelike_out_promotes

triggers it, but because reducelikes do not enforce value-based promotion
the failure can be "hidden" (which is why the test succeeds in a full test
run).

Closes gh-20739


@mhvk could you take a moment and see if this makes enough sense to you?

@seberg seberg force-pushed the evil-reducelike-no-value-based branch 2 times, most recently from 335bb0c to fd1488e Compare January 11, 2022 17:48
@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jan 11, 2022
@charris
Copy link
Copy Markdown
Member

charris commented Jan 11, 2022

Does this need a backport? The issue is marked for 1.22.1

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Jan 11, 2022

Yes, it needs a backport. Optimistically, we can all pull together in one direction on removing value-based promotion and pull a plug on all of this by the next release.

Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

It is a pretty evil hack, but also really nicely localized, so I think it is OK.

I guess to make it less hacky, one could take a view as a 1-dim, 1-element array (in which case it should not be moved, obviously). But it seems extra work for little benefit - in a way, it is good that this is clearly a hack,

@charris charris added this to the 1.22.1 release milestone Jan 11, 2022
Reducelikes should have lived in the future where the `out` dtype
is correctly honoured always and used as one of the *inputs*.
However, when legacy fallback occurs, this leads to problems because
the legacy code path has 0-D fallbacks.

There are two probable solutions to this:
* Live with weird value-based stuff here even though it was never
  actually better especially for reducelikes.
  (enforce value-based promotion)
* Avoid value based promotion completely.

This does the second one, using a terrible hack by just mutating
the dimension of `out` to tell the resolvers that value-based logic
cannot be used.

Is that hack safe?  Yes, so long nobody has super-crazy custom type
resolvers (the only one I know is pyerfa and they are fine, PyGEOS
I think has no custom type resolver).
It also relies on the GIL of course, but...

The future? We need to ditch this value-based stuff, do annoying
acrobatics with dynamically created DType classes, or something similar
(so ditching seems best, it is topping my TODO list currently).

Testing this is tricky, running the test:
```
python runtests.py -t numpy/core/tests/test_ufunc.py::TestUfunc::test_reducelike_out_promotes
```
triggers it, but because reducelikes do not enforce value-based promotion
the failure can be "hidden" (which is why the test succeeds in a full test
run).

Closes numpygh-20739
@seberg seberg force-pushed the evil-reducelike-no-value-based branch from fd1488e to f435a33 Compare January 11, 2022 22:07
Copy link
Copy Markdown
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@charris
Copy link
Copy Markdown
Member

charris commented Jan 12, 2022

Well, let's give this a shot., Thanks Sebastian.

@charris charris merged commit f637d75 into numpy:main Jan 12, 2022
@seberg seberg deleted the evil-reducelike-no-value-based branch January 12, 2022 16:39
@charris charris changed the title BUG: Fix that reducelikes honour out always (and live int he future) BUG: Fix that reducelikes honour out always (and live in the future) Jan 12, 2022
@charris charris changed the title BUG: Fix that reducelikes honour out always (and live in the future) BUG: Fix that reduce-likes honor out always (and live in the future) Jan 12, 2022
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 12, 2022
@charris charris removed this from the 1.22.1 release milestone Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Cache failure for reducelikes with output casting

3 participants