Quansight-Labs / numpy_pytorch_interop

19 stars 4 forks source link

ENH: introduce NEP 50 "weak scalars" #140

Closed ev-br closed 1 year ago

ev-br commented 1 year ago

Make python scalars "weak" [1], meaning that in type promotion, they do not type promote arrays:

(np.int8(3) + 4).dtype == int8

[1] https://numpy.org/neps/nep-0050-scalar-promotion.html

This is an alternative to gh-137, which it supersedes and closes.

NB: tests fail. The majority of failures are, I believe, in internals of torch_np.testing which are vendored from numpy. And that is incredibly messy, so they are due in for a face-lift. Am migrating them to rely on torch.testing.assert_close.

ev-br commented 1 year ago

Grrr, no. There's more to NEP 50. Converting to draft for now.

ev-br commented 1 year ago

CI's green, finally! There is one more known failure, will need to special-case complex <op> float32 promotion path. Will also add some more tests along the lines of https://gist.github.com/ev-br/27dee81aae8e24193db8082ee886f6e4 . Other than these two, this is ready.

ev-br commented 1 year ago

Based on an off-line discussion with @lezcano , this PR now:

lezcano commented 1 year ago

Perhaps @seberg could have a look at this one as well?

seberg commented 1 year ago

The truly complicated stuff is if you have more than 2 operands, without that, this seems fine (no you cannot just do a reduce(two_op_result_type, values)).

I am not actually sure about that dot example, not that dot worries me, but overall the issue remains a bit that some functions effectively call np.asarray() on entry, and thus disable any chance of "weak" handling. And... I have not figured that out fully, we may want to move more functions to weak handling.

(OTOH, I do feel that beyond ufuncs the issue is probably small enough that missing a few functions isn't the end of the world.)

ev-br commented 1 year ago

OK, two updates:

ev-br commented 1 year ago

This PR now includes @lezcano 's updates from gh-143