Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fortran array signature does not accept array that is both F and C contiguous #3569

Open
2 tasks done
NicolasHug opened this issue Dec 7, 2018 · 2 comments
Open
2 tasks done

Comments

@NicolasHug
Copy link
Contributor

Reporting a bug

I'm not sure if that is a bug or a feature, but passing a 2D array that is both Fortran and C contiguous will fail if the signature expected a Fortran-aligned array:

from numba import jitclass, uint8
import numpy as np

@jitclass([
    ('X', uint8[::1, :]),  # fortran aligned array
])
class Class:
    def __init__(self, X):
        self.X = X

# F contiguous, not C contiguous
X = np.arange(10).reshape(2, 5).astype(np.uint8)
X = np.asfortranarray(X)
assert not X.flags['C_CONTIGUOUS']
assert X.flags['F_CONTIGUOUS']
Class(X)  # OK

X = np.arange(10).reshape(10, 1).astype(np.uint8)
assert X.flags['C_CONTIGUOUS']
assert X.flags['F_CONTIGUOUS']
Class(X)  # fails
Traceback (most recent call last):
  File "numba_lol.py", line 21, in <module>
    Class(X)  # fails
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/jitclass/base.py", line 123, in __call__
    return cls._ctor(*args, **kwargs)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/dispatcher.py", line 348, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/dispatcher.py", line 315, in error_rewrite
    reraise(type(e), e, None)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/six.py", line 658, in reraise
    raise value.with_traceback(tb)
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x7fe5fa702160>:
--%<----------------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/errors.py", line 599, in new_error_context
    yield
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 254, in lower_block
    self.lower_inst(inst)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 393, in lower_inst
    signature.args[1])
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/targets/base.py", line 677, in cast
    return impl(self, builder, fromty, toty, val)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/targets/arrayobj.py", line 4673, in array_to_array
    assert fromty.mutable != toty.mutable or toty.layout == 'A'
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typeinfer.py", line 142, in propagate
    constraint(typeinfer)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typeinfer.py", line 423, in __call__
    self.resolve(typeinfer, typevars, fnty)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typeinfer.py", line 450, in resolve
    literals=literals)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typeinfer.py", line 1173, in resolve_call
    literals=literals)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typing/context.py", line 211, in resolve_function_type
    return func.get_call_type_with_literals(self, args, kws, literals)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/types/abstract.py", line 268, in get_call_type_with_literals
    return self.get_call_type(context, args, kws)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/types/misc.py", line 375, in get_call_type
    return self.ctor_template(context).apply(args, kws)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/typing/templates.py", line 207, in apply
    sig = generic(args, kws)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/jitclass/base.py", line 240, in generic
    sig = disp_type.get_call_type(self.context, boundargs, kws)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/types/functions.py", line 258, in get_call_type
    template, pysig, args, kws = self.dispatcher.get_call_template(args, kws)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/dispatcher.py", line 272, in get_call_template
    self.compile(tuple(args))
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/dispatcher.py", line 655, in compile
    cres = self._compiler.compile(args, return_type)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/dispatcher.py", line 82, in compile
    pipeline_class=self.pipeline_class)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 904, in compile_extra
    return pipeline.compile_extra(func)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 367, in compile_extra
    return self._compile_bytecode()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 835, in _compile_bytecode
    return self._compile_core()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 822, in _compile_core
    res = pm.run(self.status)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 253, in run
    raise patched_exception
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 244, in run
    stage()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 696, in stage_nopython_backend
    self._backend(lowerfn, objectmode=False)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 646, in _backend
    lowered = lowerfn()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 633, in backend_nopython_mode
    self.flags)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/compiler.py", line 1023, in native_lowering_stage
    lower.lower()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 173, in lower
    self.lower_normal_function(self.fndesc)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 214, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 239, in lower_function_body
    self.lower_block(block)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/lowering.py", line 254, in lower_block
    self.lower_inst(inst)
  File "/usr/lib64/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/errors.py", line 607, in new_error_context
    six.reraise(type(newerr), newerr, tb)
  File "/home/nico/.virtualenvs/pygbm/lib/python3.7/site-packages/numba/six.py", line 659, in reraise
    raise value
numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)


File "numba_lol.py", line 9:
    def __init__(self, X):
        self.X = X
        ^

[1] During: lowering "(self).X = X" at numba_lol.py (9)
[2] During: resolving callee type: jitclass.Class#55644369be18<X:array(uint8, 2d, F)>
[3] During: typing of call at <string> (3)

--%<----------------------------------------------------------------------------


@ogrisel
Copy link

ogrisel commented Dec 10, 2018

I think this is a bug (or a limitation of the type system). As the example shows, being C-contiguous or F-contiguous is not exclusive for numpy arrays.

@stuartarchibald
Copy link
Contributor

Thanks for the report. I think this is a limitation, the typing mech at present requires types inheriting from buffer protocol to have a single layout, one of LAYOUTS = frozenset(['C', 'F', 'CS', 'FS', 'A']), with preference given to C based layouts.

Reproducer with no jitclass:

from numba import njit, float64, typeof
import numpy as np

@njit([float64(float64[::1, :]),])
def foo(x):
    return x[0, 0]

# F contiguous, not C contiguous
X = np.arange(10.).reshape(2, 5)
X = np.asfortranarray(X)
print(typeof(X))
assert not X.flags['C_CONTIGUOUS']
assert X.flags['F_CONTIGUOUS']
foo(X)  # OK

X = np.arange(10.).reshape(10, 1)
print(typeof(X))
assert X.flags['C_CONTIGUOUS']
assert X.flags['F_CONTIGUOUS']
foo(X)  # fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants