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

Support order= in ndarray bound function reshape. #5435

Open
2 tasks done
TomMaullin opened this issue Mar 26, 2020 · 5 comments
Open
2 tasks done

Support order= in ndarray bound function reshape. #5435

TomMaullin opened this issue Mar 26, 2020 · 5 comments

Comments

@TomMaullin
Copy link

Reporting a bug

I have written the following function, which works out a permutation I need for my code, it works well without numba but fails with it.

def permOfIkKkI2D(k1,k2,n1,n2):

  # First we need the permutation represented by matrix K in vector format
  permP = np.arange(n1*k2).reshape((n1, k2), order='F').ravel()

  # Now we work out the permutation obtained by the first kronecker product (i.e. I kron K)
  permKron1 = np.repeat(np.arange(k1),n1*k2)*n1*k2+np.tile(permP,k1)

  # Now we work out the final permutation by appplying the second kronecker product (i.e. I kron K kron I)
  p = np.repeat(permKron1,n2)*n2+np.tile(np.arange(n2),n1*k1*k2)

  # Return the permutation
  return(p)

With numba I get this error:

    perm = permOfIkKkI2D(n2,n1,n2,n1)
  File "/home/tommaullin/.local/lib/python3.6/site-packages/numba/dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/tommaullin/.local/lib/python3.6/site-packages/numba/dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "/home/tommaullin/.local/lib/python3.6/site-packages/numba/six.py", line 668, 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 0x7f0202c460f0>.
�[1m�[1m�[1m�[0m
�[0m�[1m[1] During: resolving callee type: BoundFunction(array.reshape for array(int64, 1d, C))�[0m
�[0m�[1m[2] During: typing of call at /home/tommaullin/Documents/BLMM_creation/BLMM/lib/tools2d.py (409)
�[0m
Enable logging at debug level for details.
�[1m
File "../../lib/tools2d.py", line 409:�[0m
�[1mdef permOfIkKkI2D(k1,k2,n1,n2):
    <source elided>
  # First we need the permutation represented by matrix K in vector format
�[1m  permP = np.arange(n1*k2).reshape((n1, k2), order='F').ravel()
�[0m  �[1m^�[0m�[0m
@stuartarchibald stuartarchibald added needtriage more info needed This issue needs more information and removed needtriage labels Mar 26, 2020
@stuartarchibald
Copy link
Contributor

Thanks for the report. Please could you provide more information about what caused this problem:

  1. The code that you were running (as a minimal working reproducer, if you are unsure how to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).
  2. Any data needed to run the code.
  3. The full trace back of the exception.

Once we have a reproducer someone can take a look and work on a fix. Thanks for your help.

@TomMaullin
Copy link
Author

Sure!

# Generate random numbers
n1=np.random.randint(1,10)
n2=np.random.randint(1,10)
k1=np.random.randint(1,10)
k2=np.random.randint(1,10)

# Work out the permutation 
perm = permOfIkKkI2D(k1,k2,n1,n2)

@esc esc added needtriage and removed more info needed This issue needs more information labels Apr 1, 2020
@esc
Copy link
Member

esc commented Apr 1, 2020

@TomMaullin thanks for submitting the code to run the example. For convenience, I have patched up a self contained runnable version, (including imports). As a recommendation for the future, if you can provide such an example that we can copy and paste it makes debugging much quicker and easier.

from numba import njit
import numpy as np

@njit
def permOfIkKkI2D(k1,k2,n1,n2):

  # First we need the permutation represented by matrix K in vector format
  permP = np.arange(n1*k2).reshape((n1, k2), order='F').ravel()

  # Now we work out the permutation obtained by the first kronecker product (i.e. I kron K)
  permKron1 = np.repeat(np.arange(k1),n1*k2)*n1*k2+np.tile(permP,k1)

  # Now we work out the final permutation by appplying the second kronecker product (i.e. I kron K kron I)
  p = np.repeat(permKron1,n2)*n2+np.tile(np.arange(n2),n1*k1*k2)

  # Return the permutation
  return(p)

# Generate random numbers
n1=np.random.randint(1,10)
n2=np.random.randint(1,10)
k1=np.random.randint(1,10)
k2=np.random.randint(1,10)

# Work out the permutation
perm = permOfIkKkI2D(k1,k2,n1,n2)

Also, I can reproduce this locally but not yet sure what the problem is, so I will label it as needing triage.

@stuartarchibald
Copy link
Contributor

Doing: NUMBA_DEVELOPER_MODE=1 python issue5435.py
Gives:

Traceback (most recent call last):
  File "<path>/numba/numba/core/errors.py", line 769, in new_error_context
    yield
  File "<path>/numba/numba/core/typeinfer.py", line 487, in __call__
    self.resolve(typeinfer, typevars, fnty)
  File "<path>/numba/numba/core/typeinfer.py", line 507, in resolve
    sig = typeinfer.resolve_call(fnty, pos_args, kw_args)
  File "<path>/numba/numba/core/typeinfer.py", line 1451, in resolve_call
    return self.context.resolve_function_type(fnty, pos_args, kw_args)
  File "<path>/numba/numba/core/typing/context.py", line 197, in resolve_function_type
    res = self._resolve_user_function_type(func, args, kws)
  File "<path>/numba/numba/core/typing/context.py", line 249, in _resolve_user_function_type
    return func.get_call_type(self, args, kws)
  File "<path>/numba/numba/core/types/functions.py", line 215, in get_call_type
    out = template.apply(args, kws)
  File "<path>/numba/numba/core/typing/templates.py", line 290, in apply
    sig = generic(args, kws)
  File "<path>/numba/numba/core/typing/templates.py", line 908, in generic
    sig = method_resolver(self, ty, args, kws)
  File "<path>/numba/numba/core/typing/arraydecl.py", line 375, in resolve_reshape
    assert not kws
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<path>/numba/numba/core/typeinfer.py", line 149, in propagate
    constraint(typeinfer)
  File "<path>/numba/numba/core/typeinfer.py", line 487, in __call__
    self.resolve(typeinfer, typevars, fnty)
  File "<py_path>/lib/python3.8/contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "<path>/numba/numba/core/errors.py", line 776, in new_error_context
    reraise(type(newerr), newerr, tb)
  File "<path>/numba/numba/core/utils.py", line 80, in reraise
    raise value
numba.core.errors.InternalError: 
[1] During: resolving callee type: BoundFunction(array.reshape for array(int64, 1d, C))
[2] During: typing of call at issue5435.py (8)


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "issue5435.py", line 26, in <module>
    perm = permOfIkKkI2D(k1,k2,n1,n2)
  File "<path>/numba/numba/core/dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "<path>/numba/numba/core/dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "<path>/numba/numba/core/utils.py", line 79, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0x7f8c197c8a60>.

[1] During: resolving callee type: BoundFunction(array.reshape for array(int64, 1d, C))
[2] During: typing of call at issue5435.py (8)

Enable logging at debug level for details.

File "issue5435.py", line 8:
def permOfIkKkI2D(k1,k2,n1,n2):
    <source elided>
  # First we need the permutation represented by matrix K in vector format
  permP = np.arange(n1*k2).reshape((n1, k2), order='F').ravel()
  ^

the:

    assert not kws
AssertionError

is the important bit. The bound function ndarray.reshape doesn't support keyword arguments, hence the problem is the order='F'.

@stuartarchibald stuartarchibald changed the title BoundFunction(array.reshape for array(int64, 1d, C)) Support order= in ndarray bound function reshape. Apr 1, 2020
@stuartarchibald
Copy link
Contributor

Updated title and made into feature request.

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