TypeError: Buffer dtype cannot be buffer when using custom numpy dtype

0 views
Skip to first unread message

gilfo...@gmail.com

unread,
May 19, 2016, 5:01:00 PM5/19/16
to Numba Public Discussion - Public


$ python -c "import numba; print(numba.__version__)"
0.25.0
$ python
--version
Python 3.5.1 :: Anaconda 2.4.1 (64-bit)



I'm not sure why the following causes problems.  I have a numpy dtype that holds coordinates and a multipole array

cell_dtype = numpy.dtype({'names':['x','y','z','multipole'],
                   
'formats':[numpy.float64,
                               numpy
.float64,
                               numpy
.float64,
                               
(numpy.float64, 10)]})


I have a function that calculates multipoles on the basis of other cells multipoles, simplified here to:


@njit
def multipole(parent, child):
    dx
, dy, dz = (parent.x - child.x,
                    parent
.y - child.y,
                    parent
.z - child.z)

    parent.multipole += child.multipole

but this raises the following error when I call it with two arrays, each of one element that have dtype `cell_dtype`:

a = numpy.ones(1, dtype=cell_dtype)
b = numpy.ones(1, dtype=cell_dtype)
multipole(a,b)

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-3-4ecd9d435976> in <module>()
----> 1 multipole(a,b)

/home/gil/anaconda/lib/python3.5/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    286             else:
    287                 real_args.append(self.typeof_pyval(a))
--> 288         return self.compile(tuple(real_args))
    289
    290     def inspect_llvm(self, signature=None):

/home/gil/anaconda/lib/python3.5/site-packages/numba/dispatcher.py in compile(self, sig)
    504
    505             self._cache_misses[sig] += 1
--> 506             cres = self._compiler.compile(args, return_type)
    507             self.add_overload(cres)
    508             self._cache.save_overload(sig, cres)

/home/gil/anaconda/lib/python3.5/site-packages/numba/dispatcher.py in compile(self, args, return_type)
     83                                       impl,
     84                                       args=args, return_type=return_type,
---> 85                                       flags=flags, locals=self.locals)
     86         # Check typing error if object mode is used
     87         if cres.typing_error is not None and not flags.enable_pyobject:

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
    688     pipeline = Pipeline(typingctx, targetctx, library,
    689                         args, return_type, flags, locals)
--> 690     return pipeline.compile_extra(func)
    691
    692

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in compile_extra(self, func)
    376                 raise e
    377
--> 378         return self.compile_bytecode(bc, func_attr=self.func_attr)
    379
    380     def compile_bytecode(self, bc, lifted=(), lifted_from=None,

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in compile_bytecode(self, bc, lifted, lifted_from, func_attr)
    385         self.lifted_from = lifted_from
    386         self.func_attr = func_attr
--> 387         return self._compile_bytecode()
    388
    389     def compile_internal(self, bc, func_attr=DEFAULT_FUNCTION_ATTRIBUTES):

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in _compile_bytecode(self)
    669
    670         pm.finalize()
--> 671         res = pm.run(self.status)
    672         if res is not None:
    673             # Early pipeline completion

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in run(self, status)
    255                     # No more fallback pipelines?
    256                     if is_final_pipeline:
--> 257                         raise patched_exception
    258                     # Go to next fallback pipeline
    259                     else:

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in run(self, status)
    247                 try:
    248                     event(stage_name)
--> 249                     stage()
    250                 except _EarlyPipelineCompletion as e:
    251                     return e.result

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in stage_nopython_frontend(self)
    473                 self.args,
    474                 self.return_type,
--> 475                 self.locals)
    476
    477         with self.fallback_context('Function "%s" has invalid return type'

/home/gil/anaconda/lib/python3.5/site-packages/numba/compiler.py in type_inference_stage(typingctx, interp, args, return_type, locals)
    797
    798     infer.build_constraint()
--> 799     infer.propagate()
    800     typemap, restype, calltypes = infer.unify()
    801

/home/gil/anaconda/lib/python3.5/site-packages/numba/typeinfer.py in propagate(self)
    611             self.debug.propagate_finished()
    612         if errors:
--> 613             raise errors[0]
    614
    615     def add_type(self, var, tp, unless_locked=False):

TypingError: Failed at nopython (nopython frontend)
Internal error at resolving type of attribute "multipole" of "parent":
--%<-----------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typeinfer.py", line 114, in propagate
    constraint(typeinfer)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typeinfer.py", line 399, in __call__
    attrty = typeinfer.context.resolve_getattr(ty, self.attr)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typing/context.py", line 146, in resolve_getattr
    ret = attrinfo.resolve(typ, attr)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typing/templates.py", line 334, in resolve
    return self._resolve(value, attr)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typing/templates.py", line 356, in _resolve
    return fn(value, attr)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/typing/arraydecl.py", line 397, in generic_resolve
    return ary.copy(dtype=ary.dtype.typeof(attr), layout='A')
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/types/npytypes.py", line 292, in copy
    aligned=self.aligned)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/types/abstract.py", line 60, in __call__
    inst = type.__call__(cls, *args, **kwargs)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/types/npytypes.py", line 280, in __init__
    super(Array, self).__init__(dtype, ndim, layout, name=name)
  File "/home/gil/anaconda/lib/python3.5/site-packages/numba/types/common.py", line 51, in __init__
    raise TypeError("Buffer dtype cannot be buffer")
TypeError: Buffer dtype cannot be buffer
--%<-----------------------------------------------------------------


if I unroll the array operation, it works




@njit
def multipole(parent, child):
    dx
, dy, dz = (parent.x - child.x,
                    parent
.y - child.y,
                    parent
.z - child.z)

   
for j in range(parent.multipole.shape[0]):
        parent
.multipole[j] += child.multipole[j]



If I use "regular" numpy arrays (without the attribute access notation) this operation works

@njit
def add(a,b):
    return a += b

a = numpy.ones(5)
b = numpy.ones(5)

add(a,b)

print(a)
array([2., 2., 2., 2., 2.])


Is this a limitation of the type inference when using custom dtypes?  Many thanks for any and all suggestions.
Reply all
Reply to author
Forward
0 new messages