3

I'm unable to get a vectorized ufunc to run. Regular @njit works fine and the @vectorize documentation suggests that the vectorize decorators are the same as njit. I'm running on Windows 10, if that makes a difference

The demo program is as follows. From the output below that we can see that the njit function runs without incident and there's a type error with the vectorized function.

import sys
import numpy
import numba

Structured = numpy.dtype([("a", numpy.int32), ("b", numpy.float64)])
numba_dtype = numba.from_dtype(Structured)

@numba.njit([numba.float64(numba_dtype)])
def jitted(x):
    x['b'] = 17.5
    return 18.

@numba.vectorize([numba.float64(numba_dtype)], target="cpu", nopython=True)
def vectorized(x):
    x['b'] = 17.5
    return 12.1

print('python version = ', sys.implementation.version)    
print('numpy version = ', numpy.__version__)
print('numba version = ', numba.__version__)
for struct in numpy.empty((3,), dtype=Structured):
    print(jitted(struct))

print(vectorized(numpy.empty((3,), dtype=Structured)))

And the output is

python version = sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)
numpy version = 1.17.3
numba version = 0.48.0
18.0
18.0
18.0
Traceback (most recent call last): File "scratch.py", line 49, in
print(vectorized(numpy.empty((3,), dtype=Structured))) TypeError: ufunc 'vectorized' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

1472580
  • 163
  • 1
  • 8
  • I haven't used this `numba.vectorize`, but the docs say it creates a `ufunc` (like) function. `ufunc` return a new array; they don't operate in-place. I realize this is just a test function, but with `x=np.empty(3,), dtype=Structured)`, `x['b'] = 17.5` works just fine without `numba`. – hpaulj Feb 25 '20 at 19:48
  • I can't get anything working with `vectorize` and this dtype. I'm not aware of any `numpy.ufunc` that work with structured dtypes. You may need to step back and explain why you need this. What is your goal? – hpaulj Feb 25 '20 at 20:45
  • 2
    The [Numba docs](https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html) mention that Numpy structured scalars are supported, as are Numpy arrays of supported types. Additionally, I saw no mention to the contrary in the [@vectorize docs](https://numba.pydata.org/numba-doc/dev/user/vectorize.html). My current goal is to either figure out how to get this example to work or understand why it does not work. There are other approaches that I'm exploring, but vectorizing structured arrays is the topic of investigation for this particular question. – 1472580 Feb 26 '20 at 15:57
  • The issue could be with `numpy's` own handling of structured dtypes in `ufunc`. For example I found this section on writing your own ufunc: https://docs.scipy.org/doc/numpy-1.13.0/user/c-info.ufunc-tutorial.html#example-numpy-ufunc-with-structured-array-dtype-arguments. `numba` might not be handling the required details. – hpaulj Feb 28 '20 at 01:19
  • 1
    I think you might be correct... I opened an [issue](https://github.com/numba/numba/issues/5329) in github. I'll see if there's any response over there. – 1472580 Feb 28 '20 at 17:36

1 Answers1

3

It looks like this is not supported, has been converted to a feature request

1472580
  • 163
  • 1
  • 8