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

np.arange with njit showing erratic behaviour #1299

Closed
nunocalaim opened this issue Jul 7, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@nunocalaim
Copy link

commented Jul 7, 2015

I am having some issues with the np.arange() function with nopython mode
numba 0.20.0

from numba import njit
import numpy as np

def f():
    g = 4
    g += 1
    a = np.arange(g)
    print(a[5])

@njit
def fjit():
    g = 4
    g += 1
    a = np.arange(g)
    print(a[5])

f()

IndexError: index 5 is out of bounds for axis 0 with size 5

fjit()

1125904337183552

Is this the intended behaviour? I stumbled upon this because I am having a mysterious error with the np.arange(variable) when variable has been subject to initialization

variable = 0
for i in range(4):
    variable = variable + 1
np.arange(variable)

Error in nopython mode!

@pitrou

This comment has been minimized.

Copy link
Contributor

commented Jul 7, 2015

Is this the intended behaviour?

Yes, Numba currently doesn't check array bounds when indexing. We would have to research whether LLVM is able to elide the bounds checking code in common cases, otherwise that would slow common usecases down by a significant amount.

I am having a mysterious error with the np.arange(variable) when variable has been subject to initialization

Can you paste the entire error? For the record, I've tried to reproduce and get the following error message:

numba.errors.TypingError: Failed at nopython (nopython frontend)
Can't unify types of variable '$39.5': $39.5 := {array(int32, 1d, C), array(int64, 1d, C)}
@nunocalaim

This comment has been minimized.

Copy link
Author

commented Jul 7, 2015

Yep, This is the full error I get

Minimal working example:

import numpy as np
from numba import njit
@njit
def f():
    variable = 0
    for i in range(4):
        variable = variable + 1
    np.arange(variable)

f()

TypingError                               Traceback (most recent call last)
<ipython-input-28-35b6f834120e> in <module>()
----> 1 jit_analytical_simulation(D / lambd, x, xhatinit, tmax, lambd, np.zeros(D.shape[1]))

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    157         assert not kws
    158         sig = tuple([self.typeof_pyval(a) for a in args])
--> 159         return self.compile(sig)
    160 
    161     def inspect_llvm(self, signature=None):

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/dispatcher.py in compile(self, sig)
    320                                           self.py_func,
    321                                           args=args, return_type=return_type,
--> 322                                           flags=flags, locals=self.locals)
    323 
    324             # Check typing error if object mode is used

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
    592     pipeline = Pipeline(typingctx, targetctx, library,
    593                         args, return_type, flags, locals)
--> 594     return pipeline.compile_extra(func)
    595 
    596 

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in compile_extra(self, func)
    315                 raise e
    316 
--> 317         return self.compile_bytecode(bc, func_attr=self.func_attr)
    318 
    319     def compile_bytecode(self, bc, lifted=(), lifted_from=None,

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in compile_bytecode(self, bc, lifted, lifted_from, func_attr)
    324         self.lifted_from = lifted_from
    325         self.func_attr = func_attr
--> 326         return self._compile_bytecode()
    327 
    328     def compile_internal(self, bc, func_attr=DEFAULT_FUNCTION_ATTRIBUTES):

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in _compile_bytecode(self)
    579 
    580         pm.finalize()
--> 581         return pm.run(self.status)
    582 
    583 

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in run(self, status)
    207                     # No more fallback pipelines?
    208                     if is_final_pipeline:
--> 209                         raise patched_exception
    210                     # Go to next fallback pipeline
    211                     else:

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in run(self, status)
    199             for stage, stage_name in self.pipeline_stages[pipeline_name]:
    200                 try:
--> 201                     res = stage()
    202                 except _EarlyPipelineCompletion as e:
    203                     return e.result

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in stage_nopython_frontend(self)
    412                 self.args,
    413                 self.return_type,
--> 414                 self.locals)
    415 
    416         with self.fallback_context('Function "%s" has invalid return type'

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/compiler.py in type_inference_stage(typingctx, interp, args, return_type, locals)
    708     infer.build_constrain()
    709     infer.propagate()
--> 710     typemap, restype, calltypes = infer.unify()
    711 
    712     if config.DEBUG:

/Users/calaim/anaconda/lib/python3.4/site-packages/numba/typeinfer.py in unify(self)
    412                 unified = self.context.unify_types(*tv.get())
    413             if unified is types.pyobject:
--> 414                 raise TypingError("Can't unify types of variable '%s': %s" % (var, tv))
    415             typdict[var] = unified
    416         retty = self.get_return_type(typdict)

TypingError: Failed at nopython (nopython frontend)
Can't unify types of variable 'nuno': nuno := {array(int64, 1d, C), array(int32, 1d, C)}
@nunocalaim

This comment has been minimized.

Copy link
Author

commented Jul 8, 2015

should I close this issue as indeed the first "error" was the intended behavior?

Should I open a new one about the second error? Unfortunately my work is stopped because of this error!

@pitrou

This comment has been minimized.

Copy link
Contributor

commented Jul 8, 2015

Yes, I would suggest opening a new issue, thank you. By the way, you can probably work around it by using variable = np.int64(0) instead.

@nunocalaim

This comment has been minimized.

Copy link
Author

commented Jul 8, 2015

Closing as suggested by @pitrou

@nunocalaim nunocalaim closed this Jul 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.