<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 10, 2021 at 11:41 PM Sebastian Berg <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, 2021-03-10 at 13:44 -0700, Aaron Meurer wrote:<br>
> On Wed, Mar 10, 2021 at 10:42 AM Sebastian Berg<br>
> <<a href="mailto:sebastian@sipsolutions.net" target="_blank">sebastian@sipsolutions.net</a>> wrote:<br><br>
> > <br>
> > 2. `np.result_type` special cases array-scalars (the current PR),<br>
> > NEP<br>
> > 47 promises it will not.  The PR could attempt to work around that<br>
> > using `arr.dtype` int `result_type`, I expect there are more<br>
> > details to<br>
> > fight with there, but I am not sure.<br>
> <br>
> The idea is to work around it everywhere, so that it follows the<br>
> rules<br>
> in the spec (no array scalars, no value-based casting). I haven't<br>
> started it yet, though, so I don't know yet how hard it will be. If<br>
> it<br>
> ends up being too hard we could put it in the same camp as device<br>
> support and dlpack support where it needs some basic implementation<br>
> in<br>
> numpy itself first before we can properly do it in the array API<br>
> namespace.<br>
<br>
Quite frankly. If you really want to implement a minimal API, it may be<br>
best to just write it yourself and ditch NumPy. (Of course I currently<br>
doubt that the NEP 47 implementation should be minimal.)<br></blockquote><div><br></div><div>I'm not really sure what to say other than that I don't think anyone will be served by "ditching NumPy".</div><div><br></div><div>The goal for this "minimal" part is to provide an API that you can write code against that will work portably across other array libraries. That seems like a valuable goal, right? And if you want NumPy-specific things that other libraries don't commonly (or at all) implement and are not supported by array_api, then you don't use this API but the existing main numpy namespace.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
About doing promotion yourself  ("promotion" as in what ufuncs do; I<br>
call `np.result_type` "common DType", because it is used e.g. in<br>
`concatenate`):<br>
<br>
Ufuncs have at least one more rule for true-division, plus there may be<br>
mixed float-int loops, etc.  Since the standard is very limited and you<br>
only have numeric dtypes that might be all though.<br>
<br>
In any case, my point is: If NumPy does strange things (and it does<br>
with 0-D arrays currently).  You could cook your own soup there also,<br>
and implement it in NumPy by using `signature=...` in the ufunc call.<br></blockquote><div><br></div><div>Interesting idea.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> > 4. Now that I looked at the above, I do not feel its reasonable to<br>
> > limit this functionality to numeric dtypes.  If someone uses a<br>
> > NumPy<br>
> > rational-dtype, why should a SciPy function currently implemented<br>
> > in<br>
> > pure NumPy reject that?  In other words, I think this is the point<br>
> > where trying to be "minimal" is counterproductive.<br></blockquote><div><br></div><div>SciPy would still be free to implement *both* a portable code path and a numpy-specific path (if that makes sense, which I doubt in many cases). There's just no way those two code paths can be 100% common, because no other library implements a rational dtype.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> <br>
> The idea of minimality is to make it so users can be sure they will<br>
> be<br>
> able to use other libraries, once they also have array API compliant<br>
> namespaces. A rational-dtype wouldn't ever be implemented in those<br>
> other libraries, because it isn't part of the standard, so if a user<br>
> is using those, that is a sign they are using things that aren't in<br>
> the array API, so they can't expect to be able to swap out their<br>
> dtypes. If a user wants to use something that's only in NumPy, then<br>
> they should just use NumPy.<br>
> <br>
<br>
This is not about the "user", in your scenario the end-user does use<br>
NumPy.  The way I understand this is not a prerequisite.  If it is, a<br>
lot of things will be simpler though, and most of my doubts will go<br>
away (but be replaced with uncertainty about the usefulness). <br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
The problem is that SciPy as the "library author" wants to to use NEP<br>
47 without limiting the end-user (or the end-user even noticing!).<br>
The distinction between end-user and library author (someone who writes<br>
a function that should work with numpy, pytorch, etc.) is very<br>
important here and too all of these "protocol" discussions.<br></blockquote><div><br></div><div>The example feels a little forced. >99% of end user code written against libraries like SciPy uses standard numerical dtypes. Things like a rational dtype are very niche. A rationale dtype works with most NumPy functions, but is not at all guaranteed to work with SciPy functions - and if it does it's accidental, untested and may break if SciPy would change its implementation (e.g. move from pure Python + NumPy to Cython or C++).</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
I assume that SciPy should be able to have the cake and eat it to:<br>
<br>
* Uses the limited array-api and make sure to only rely on the minimal<br>
  subset.<br>
* Not artificially limit end-users who pass in NumPy arrays.<br>
<br>
The second point can also be read as: SciPy would be able to support<br>
practically all current NumPy array use cases without jumping through<br>
any additional hoops (or well, maybe a bit of churn, but churn that is<br>
made easy by as of now undefined API). <br></blockquote><div><br></div><div>I suspect you have things in mind that are not actually supported by SciPy today. The rational dtype is one example, but so are ndarray subclasses. Take masked arrays as an example - these are not supported today, except for scipy.stats.mstats functionality - where support is intentional, special-cases and tested.</div><div><br></div><div>For masked arrays as well as other arbitrary fancy subclasses, there's some not-well-defined subset of functionality that may work today, but that is fragile, untested and can break without warning in any release. Only Liskov-substitutable ndarray subclasses are not fragile - those are simply coerced to ndarray via the ubiquitous `np.asarray` pattern, and ndarrays are returned. That must and will remain working.<br></div><div><br> </div><div>This is a complex topic, and it's possible that I'm missing other use cases you have in mind, so I thought I'd make a diagram to explain the difference between the custom dtypes & subclasses that are supported by NumPy itself but not by downstream libraries:</div><div><a href="https://github.com/rgommers/numpy/blob/numpy-scipy-custom-inputs/doc/neps/_static/nep-0047-numpy-scipy-custominputs.png">https://github.com/rgommers/numpy/blob/numpy-scipy-custom-inputs/doc/neps/_static/nep-0047-numpy-scipy-custominputs.png</a></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br><br>
> > 6. Is there any provision on how to deal with mixed array-like<br>
> > inputs?<br>
> > CuPy+numpy, etc.?<br>
> <br>
> Neither of these are defined in the spec. The spec only deals with<br>
> staying inside of the compliant namespace. It doesn't require any<br>
> behavior mixing things from other namespaces. That's generally<br>
> considered a much harder problem, and there is the data interchange<br>
> protocol to deal with it<br>
> ( <br>
> <a href="https://data-apis.github.io/array-api/latest/design_topics/data_interchange.html" rel="noreferrer" target="_blank">https://data-apis.github.io/array-api/latest/design_topics/data_interchange.html</a><br>
> ).<br>
> <br>
<br>
OK, maybe you can get away with it, since the current proposal seems to<br>
be that `get_namespace()` raises on mixed input. Still seems like<br>
something that should probably raise an error rather than coerce to<br>
NumPy when calling: `nep47_array_object + dask_array`.<br></blockquote><div><br></div><div>Agreed, this must raise too.</div><div><br></div><div>Cheers,<br></div><div>Ralf</div><div><br></div></div></div>