Allow mixing dask and numpy arrays in @guvectorize#6863
Conversation
|
ping @quasiben or maybe suggest someone else to review |
dask/array/utils.py
Outdated
| """ | ||
| if not is_arraylike(x): | ||
| return x | ||
| return np.ones((1,) * x.ndim, dtype=x.dtype) |
There was a problem hiding this comment.
Perhaps this should use [np.ones_like[(https://numpy.org/doc/stable/reference/generated/numpy.ones_like.html) instead. cc @pentschev
There was a problem hiding this comment.
If this is indeed necessary, we would probably want
Lines 340 to 349 in 8f9fc16
meta_from_array instead.
dask/array/core.py
Outdated
| np.ones((1,) * x.ndim, dtype=x.dtype) if isinstance(x, Array) else x | ||
| for x in args | ||
| ] | ||
| args = [meta_nonempty(x) for x in args] |
There was a problem hiding this comment.
Wouldn't using meta_from_array suffice here?
There was a problem hiding this comment.
My understanding is it needs to be a nonempty array for gufuncs to work. If I switch this to meta_from_array I get:
ValueError: `dtype` inference failed in `apply_gufunc`.
Please specify the dtype explicitly using the `output_dtypes` kwarg.
Original error is below:
------------------------
ValueError('cannot call `vectorize` on size 0 inputs unless `otypes` is set')
Traceback:
---------
File "/home/julia/dask/dask/array/core.py", line 377, in apply_infer_dtype
o = func(*args, **kwargs)
File "/home/julia/conda/envs/dask-dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 2091, in __call__
return self._vectorize_call(func=func, args=vargs)
File "/home/julia/conda/envs/dask-dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 2157, in _vectorize_call
res = self._vectorize_call_with_signature(func, args)
File "/home/julia/conda/envs/dask-dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 2226, in _vectorize_call_with_signature
raise ValueError('cannot call `vectorize` on size 0 inputs '
There was a problem hiding this comment.
In that case, couldn't you just replace np.ones by ones_like_safe in the old code? That way you wouldn't even need the new meta_from_array function, unless I'm missing something.
dask/array/utils.py
Outdated
| """ | ||
| if not is_arraylike(x): | ||
| return x | ||
| return np.ones((1,) * x.ndim, dtype=x.dtype) |
There was a problem hiding this comment.
If this is indeed necessary, we would probably want
Lines 340 to 349 in 8f9fc16
meta_from_array instead.
77f1fdf to
9ae7018
Compare
|
Thanks for the feedback @pentschev. I think I had made it overly complicated for no reason. |
|
I am going to merge this if it passes. |
dask/array/utils.py
Outdated
|
|
||
| try: | ||
| return np.ones_like(a, shape=shape, **kwargs) | ||
| return _array_like_safe(np.ones_like, ones_like, a, shape=shape, **kwargs) |
There was a problem hiding this comment.
I think this is a neccessary improvement on this function, but @pentschev please correct me if this is wrong.
There was a problem hiding this comment.
No, this isn't necessary. ones_like already existed before we added like= to creation functions like np.asarray, in fact, the like= argument name comes from the *_like functions. We have added like= to all creation functions, including np.ones, but using np.ones_like is equivalent to np.ones(..., like=...). Therefore, ones_like_safe already works without the change proposed here.
There was a problem hiding this comment.
Ok thank you. I will think more about what I am doing wrong then. I was getting a segfault without this change which made me think that maybe I was hitting a wierd loop or somthing. I
|
Ok I made |
|
@jrbourbeau I plan to merge this tomorrow unless you have any comments. |
Co-authored-by: Peter Andreas Entschev <peter@entschev.com>
|
Thanks for all the reviews! 😅 |
black dask/flake8 daskI am not sure that this is the right time for adding a meta_nonempty for dask.array but I thought it might be? Also I suspect that this'll have repercussions for cupy @quasiben.