Lowering error with global default value for a parameter
See original GitHub issueA=[1., 2.]
@numba.njit
def f(f_values=A):
return 0
f()
results in something which is hard to interpret to me (below). At the same time
f(A)
works fine, which suggests that this could be easily fixed.
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/targets/base.py in get_constant_generic(self, builder, ty, val)
453 try:
--> 454 impl = self._get_constants.find((ty,))
455 return impl(self, builder, ty, val)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/targets/base.py in find(self, sig)
47 if out is None:
---> 48 out = self._find(sig)
49 self._cache[sig] = out
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/targets/base.py in _find(self, sig)
56 else:
---> 57 raise NotImplementedError(self, sig)
58
NotImplementedError: (<numba.targets.base.OverloadSelector object at 0x7ffa086aa320>, (reflected list(float64),))
During handling of the above exception, another exception occurred:
NotImplementedError Traceback (most recent call last)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
258 try:
--> 259 yield
260 except NumbaError as e:
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_block(self, block)
215 loc=self.loc, errcls_=defaulterrcls):
--> 216 self.lower_inst(inst)
217
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_inst(self, inst)
264 ty = self.typeof(inst.target.name)
--> 265 val = self.lower_assign(ty, inst)
266 self.storevar(val, inst.target.name)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_assign(self, ty, inst)
428 const = self.context.get_constant_generic(self.builder, valty,
--> 429 pyval)
430 # cast it to the variable type
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/targets/base.py in get_constant_generic(self, builder, ty, val)
456 except NotImplementedError:
--> 457 raise NotImplementedError("cannot lower constant of type '%s'" % (ty,))
458
NotImplementedError: cannot lower constant of type 'reflected list(float64)'
During handling of the above exception, another exception occurred:
LoweringError Traceback (most recent call last)
<ipython-input-18-a5b8ed516f38> in <module>()
4 return 0
5
----> 6 f()
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
305 argtypes.append(self.typeof_pyval(a))
306 try:
--> 307 return self.compile(tuple(argtypes))
308 except errors.TypingError as e:
309 # Intercept typing error that may be due to an argument
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/dispatcher.py in compile(self, sig)
577
578 self._cache_misses[sig] += 1
--> 579 cres = self._compiler.compile(args, return_type)
580 self.add_overload(cres)
581 self._cache.save_overload(sig, cres)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/dispatcher.py in compile(self, args, return_type)
78 impl,
79 args=args, return_type=return_type,
---> 80 flags=flags, locals=self.locals)
81 # Check typing error if object mode is used
82 if cres.typing_error is not None and not flags.enable_pyobject:
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
777 pipeline = Pipeline(typingctx, targetctx, library,
778 args, return_type, flags, locals)
--> 779 return pipeline.compile_extra(func)
780
781
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in compile_extra(self, func)
360 self.lifted = ()
361 self.lifted_from = None
--> 362 return self._compile_bytecode()
363
364 def compile_ir(self, func_ir, lifted=(), lifted_from=None):
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in _compile_bytecode(self)
736 """
737 assert self.func_ir is None
--> 738 return self._compile_core()
739
740 def _compile_ir(self):
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in _compile_core(self)
723
724 pm.finalize()
--> 725 res = pm.run(self.status)
726 if res is not None:
727 # Early pipeline completion
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in run(self, status)
246 # No more fallback pipelines?
247 if is_final_pipeline:
--> 248 raise patched_exception
249 # Go to next fallback pipeline
250 else:
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in run(self, status)
238 try:
239 event(stage_name)
--> 240 stage()
241 except _EarlyPipelineCompletion as e:
242 return e.result
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in stage_nopython_backend(self)
656 """
657 lowerfn = self.backend_nopython_mode
--> 658 self._backend(lowerfn, objectmode=False)
659
660 def stage_compile_interp_mode(self):
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in _backend(self, lowerfn, objectmode)
611 self.library.enable_object_caching()
612
--> 613 lowered = lowerfn()
614 signature = typing.signature(self.return_type, *self.args)
615 self.cr = compile_result(typing_context=self.typingctx,
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in backend_nopython_mode(self)
598 self.return_type,
599 self.calltypes,
--> 600 self.flags)
601
602 def _backend(self, lowerfn, objectmode):
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/compiler.py in native_lowering_stage(targetctx, library, interp, typemap, restype, calltypes, flags)
896
897 lower = lowering.Lower(targetctx, library, fndesc, interp)
--> 898 lower.lower()
899 if not flags.no_cpython_wrapper:
900 lower.create_cpython_wrapper(flags.release_gil)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower(self)
133 if self.generator_info is None:
134 self.genlower = None
--> 135 self.lower_normal_function(self.fndesc)
136 else:
137 self.genlower = self.GeneratorLower(self)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_normal_function(self, fndesc)
174 # Init argument values
175 self.extract_function_arguments()
--> 176 entry_block_tail = self.lower_function_body()
177
178 # Close tail of entry block
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_function_body(self)
199 bb = self.blkmap[offset]
200 self.builder.position_at_end(bb)
--> 201 self.lower_block(block)
202
203 self.post_lower()
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_block(self, block)
214 with new_error_context('lowering "{inst}" at {loc}', inst=inst,
215 loc=self.loc, errcls_=defaulterrcls):
--> 216 self.lower_inst(inst)
217
218 def create_cpython_wrapper(self, release_gil=False):
~/miniconda3/envs/emmy/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
97 value = type()
98 try:
---> 99 self.gen.throw(type, value, traceback)
100 except StopIteration as exc:
101 # Suppress StopIteration *unless* it's the same exception that
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
263 except Exception as e:
264 newerr = errcls(e).add_context(_format_msg(fmt_, args, kwargs))
--> 265 six.reraise(type(newerr), newerr, sys.exc_info()[2])
266
267
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/six.py in reraise(tp, value, tb)
656 value = tp()
657 if value.__traceback__ is not tb:
--> 658 raise value.with_traceback(tb)
659 raise value
660
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
257
258 try:
--> 259 yield
260 except NumbaError as e:
261 e.add_context(_format_msg(fmt_, args, kwargs))
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_block(self, block)
214 with new_error_context('lowering "{inst}" at {loc}', inst=inst,
215 loc=self.loc, errcls_=defaulterrcls):
--> 216 self.lower_inst(inst)
217
218 def create_cpython_wrapper(self, release_gil=False):
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_inst(self, inst)
263 if isinstance(inst, ir.Assign):
264 ty = self.typeof(inst.target.name)
--> 265 val = self.lower_assign(ty, inst)
266 self.storevar(val, inst.target.name)
267
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/lowering.py in lower_assign(self, ty, inst)
427 valty = self.context.typing_context.resolve_value_type(pyval)
428 const = self.context.get_constant_generic(self.builder, valty,
--> 429 pyval)
430 # cast it to the variable type
431 res = self.context.cast(self.builder, const, valty, ty)
~/miniconda3/envs/emmy/lib/python3.6/site-packages/numba/targets/base.py in get_constant_generic(self, builder, ty, val)
455 return impl(self, builder, ty, val)
456 except NotImplementedError:
--> 457 raise NotImplementedError("cannot lower constant of type '%s'" % (ty,))
458
459 def get_constant(self, ty, val):
LoweringError: Failed at nopython (nopython mode backend)
cannot lower constant of type 'reflected list(float64)'
File "<ipython-input-18-a5b8ed516f38>", line 4
[1] During: lowering "f_values = arg(0, name=f_values)" at <ipython-input-18-a5b8ed516f38> (4)
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Python: Optional parameter, wrong default value [duplicate]
The default value is evaluated and stored once when the function is defined. It is not re-checked each time the function is called....
Read more >Reducing Calldata Size with Optional/Default Function ...
This generated function would simply call the “parent” function with the specified default value. If no default value was specified, the global ......
Read more >Be careful when using || to set default values in JavaScript
When no value is passed into a function the value of the parameter will be undefined . Using OR however means that the...
Read more >passing variable as default value to procedure parameter
> >value. If it is equal to the "default" value, use the global. ... > Yuck. ... > "args" variadic-argument list? ... >...
Read more >Named and Optional Arguments - C# Programming Guide
Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The “reflected” part is referring to the fact that a list that comes into a Numba function as an argument needs to have any changes in contents of the nopython mode version of that list reflected in the original Python list object when the function exits. Tuples do not have this complexity in Numba (and generally work more places) because they are immutable.
(Incidentally, this is also why using a list as a default argument value is typically avoided in Python. It is very easy for one call to the function to accidentally mutate the default value and affect future calls to the function.)
I expect this is because Numba is treating
A
inf_values=A
as a compile-time-constant as it is a global http://numba.pydata.org/numba-doc/latest/glossary.html#term-compile-time-constant. However there is no mechanism written in Numba for lowering (translating from the higher level representation to a lower level) a reflected list as a constant type. If you makeA
into a constant type that can be lowered, like a homogeneous tuple or a scalar then compilation will succeed.