Can numba be disabled during unit tests?

342 views
Skip to first unread message

James Adams

unread,
Dec 23, 2017, 3:05:35 PM12/23/17
to Numba Public Discussion - Public
My tests can run faster/error-free without numba, is there a way to disable numba annotations when the annotated function is being run as part of unit tests?

BTW I'm seeing the following error with a build on codeship.com, it looks to be numba related (and possibly eliminated if I can disable numba for testing?):

====================================================================== ERROR: Failure: ImportError (libpython3.6m.so.1.0: cannot open shared object file: No such file or directory) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/nose/failure.py", line 39, in runTest raise self.exc_val.with_traceback(self.tb) File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/nose/loader.py", line 418, in loadTestsFromName addr.filename, addr.module) File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/nose/importer.py", line 47, in importFromPath return self.importFromDir(dir_path, fqname) File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/nose/importer.py", line 94, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/imp.py", line 235, in load_module return load_source(name, filename, file) File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/imp.py", line 172, in load_source module = _load(spec) File "<frozen importlib._bootstrap>", line 684, in _load File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/rof/src/github.com/monocongo/indices_python/tests/test_compute.py", line 8, in <module> from context import compute File "/home/rof/src/github.com/monocongo/indices_python/tests/context.py", line 5, in <module> import compute File "/home/rof/src/github.com/monocongo/indices_python/compute.py", line 5, in <module> import numba File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/__init__.py", line 12, in <module> from .special import typeof, prange File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/special.py", line 3, in <module> from .typing.typeof import typeof File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/typing/__init__.py", line 2, in <module> from .context import BaseContext, Context File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/typing/context.py", line 10, in <module> from numba.typeconv import Conversion, rules File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/typeconv/rules.py", line 3, in <module> from .typeconv import TypeManager, TypeCastingRules File "/home/rof/.pyenv/versions/3.6.3/lib/python3.6/site-packages/numba/typeconv/typeconv.py", line 3, in <module> from . import _typeconv, castgraph, Conversion ImportError: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

Juan Nunez-Iglesias

unread,
Dec 23, 2017, 8:51:46 PM12/23/17
to Numba Public Discussion - Public
Yes, run “export NUMBA_DISABLE_JIT=1” before running the tests. I use this to measure test coverage.
--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/0fdbb0e0-4d1e-4e03-8e80-6f29b03e5f79%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

James Adams

unread,
Dec 24, 2017, 8:40:57 AM12/24/17
to numba...@continuum.io
Thanks, Juan. This is exactly what I was looking for.


To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.

--
You received this message because you are subscribed to a topic in the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/numba-users/0S406h5O56w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to numba-users+unsubscribe@continuum.io.

To post to this group, send email to numba...@continuum.io.

Mani Sarkar

unread,
Apr 28, 2020, 9:43:05 AM4/28/20
to Numba Public Discussion - Public
On the back of the same question I'd like to extend further the inquire about NUMBA_DISABLE_JIT:
I used the below implementation in all the tests I want Numba to be disabled:

from typing import Tuple

import pandas as pd
import time

import os
# We do this to make sure all the Numba implementation are disabled for the performance tests
os.environ['NUMBA_DISABLE_JIT'] = '1'


and the below where the Numba implementation should stay enabled:

from typing import Tuple

import pandas as pd
import time

import os
# We do this to make sure all the Numba implementation are enabled for the performance tests
os.environ['NUMBA_DISABLE_JIT'] = '0'

Running the tests and checking the coverage report shows no change, I still get the same coverage report like before showing the @jit or @njit block of code with no coverage.

Any ideas what must be happening?

I can't do as suggested above i.e export NUMBA_DISABLE_JIT=1 simply because I have various groups of tests, some need to use it while others do not, a mixed bag. I thought this be a way to do it/

Juan Nunez-Iglesias

unread,
Apr 28, 2020, 10:19:07 AM4/28/20
to numba...@continuum.io
Maybe try

# We do this to make sure all the Numba implementation are enabled for the performance tests
os.environ['NUMBA_DISABLE_JIT'] = ''

?
--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.

Aaron Meurer

unread,
Apr 28, 2020, 2:14:31 PM4/28/20
to numba...@continuum.io
Is it possible numba is already imported from another file? Setting
the environment variable will only have an effect if it is done before
numba is imported. You can check if 'numba' is in sys.modules to see
if it has already been imported.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/1c4a09cc-8a97-4676-8d6f-f2709939d98d%40www.fastmail.com.

Valentin Haenel

unread,
Apr 28, 2020, 4:21:46 PM4/28/20
to numba...@continuum.io
Hi Mani,

as far as I know, it is not possible to run some parts of your code with `NUMBA_DISABLE_JIT` activated and others without it. It is an all-or-nothing option.

Best wishes,

V-

Mani Sarkar

unread,
Apr 29, 2020, 8:36:45 AM4/29/20
to numba...@continuum.io
Even if it’s different modules That import numba each time separately?

Thanks

You received this message because you are subscribed to a topic in the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/numba-users/0S406h5O56w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to numba-users...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/CAJSA9TKEr3No_NjGvP_9CDS8JX7j14jzKSPzMAmz3dT%2BF2_JGA%40mail.gmail.com.
--
--
@theNeomatrix369  |  Blogs: https://medium.com/@neomatrix369  | @adoptopenjdk @graalvm @graal @truffleruby  |  Github: https://github.com/neomatrix369  |  Slideshare: https://slideshare.net/neomatrix369 | LinkedIn: https://uk.linkedin.com/in/mani-sarkar

Don't chase success, rather aim for "Excellence", and success will come chasing after you!

Valentin Haenel

unread,
Apr 29, 2020, 10:43:13 AM4/29/20
to numba...@continuum.io
Dear Mani,

I did some more research and here is method that might allow you to achieve what you want:

from numba import njit, config

config.DISABLE_JIT = True

@njit
def foo():
j = 0
for i in (1,2,3):
j += i
return j

foo()
print(foo)

config.DISABLE_JIT = False

@njit
def bar():
j = 0
for i in (1,2,3):
j += i
return j
bar()
print(bar)

This prints:

<function foo at 0x1105793a0>
CPUDispatcher(<function bar at 0x1226074c0>)

So perhaps this could be used to achieve what you want?

V-

Mani Sarkar

unread,
Apr 29, 2020, 12:56:38 PM4/29/20
to numba...@continuum.io
Thanks Valentin

I will give this a try.

Do you think I can trigger:
config.DISABLE_JIT = True
From inside test cases, that would be ideal but I will play around and see what I can do.




--

Aaron Meurer

unread,
Apr 29, 2020, 1:22:14 PM4/29/20
to numba...@continuum.io
Yes, that doesn't matter. When Python imports a module like numba, it
only imports it once. After that, it is cached in sys.modules. You can
check for yourself that running 'import numba' the first time takes
about a half a second for numba to load, but any subsequent 'import
numba' runs instantly. Since numba only reads the environment variable
when it is imported, that means once it is loaded, changes to the
environment have no effect.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/CAGHtMW%3DWW_URO9Lv52BiZnJ3_JahcASV-_Frvmd6iv43kCPuSw%40mail.gmail.com.

Valentin Haenel

unread,
Apr 29, 2020, 1:25:35 PM4/29/20
to numba...@continuum.io
Dear Mani,

Yes, but you need to set it before using `njit` and friends. I believe the decorator looks at the value of `DISABLE_JIT` in `config` and then either returns the original function if it is set to True but does the usual compile dance otherwise. You might need to experiment a bit. Also, if you succeed please do report back and let us know how much mileage you got!

Best wishes and good luck!

V-

Mani Sarkar

unread,
May 4, 2020, 9:58:12 AM5/4/20
to numba...@continuum.io
Hi Valentin,

The example you showed works if everything is in the same module or one file.

In my case the functionality I’m testing is in nested modules and they get loaded only once. Unless those modules are reloaded each time I run my tests it going to be hard for me to implement your suggestions.

Do you recommend using importlib.reload? How can we make the system reload the module into its cache ?

So far it’s getting hard for me to make it override the decorator classes.

Thanks 

Regards
Mani

Valentin Haenel

unread,
May 4, 2020, 2:05:52 PM5/4/20
to numba...@continuum.io
Hi Mani,

I'm not quite sure I understand what you mean about loading modules only once and needing reloading. Do you perhaps have an example, with some files and directories to showcase how you would like the setting to work?

Best wishes,

V-

Mani Sarkar

unread,
May 4, 2020, 2:45:35 PM5/4/20
to numba...@continuum.io
Hi Valentin,

What I meant was this is how my modules look like:

Test class:
from abc import run_tasks

def test_one:
      time_taken = run_tasks()
      assert time_taken < too_much


Implementation class: abc

from numba_is_used import numba_function

def run_tasks():
      ...
      abc = numba_function()
      return nn - abc

Implementation class: numba_is_used
import numba ....

<--code to enable/disable numba-->

@jit
def numba_function()
      ....
      return fast_results

So you see from the above you can see that the function called from the test is nested inside two layers of modules, so the way to enable disable Numba by using the config.DISABLED_JIT flag can only work if we can control it from the test modules/classes.

So I have overcome this issue:

Test class:

import os
os.environ['NUMBA_DISABLE_JIT'] = '1'
from importlib import reload
reload(numba_is_used)

from abc import run_tasks

def test_one:
      time_taken = run_tasks()
      assert time_taken < too_much

Implementation class: numba_is_used.py

import numba ....

config.DISABLE_JIT=False
if 'NUMBA_DISABLE_JIT' in os.environ
      if os.environ['NUMBA_DISABLE_JIT'] == '1'
       config.DISABLE_JIT=True
@jit
def numba_function()
      ....
      return fast_results

I hope this explains, what I did, and it worked for me. I don't think it's an elegant solution but it can help solve the issue for now. If you or anyone else can think of a better solution then I'd like to know what it is. I have seen reloading the class where numba is implemented coupled with the environment variable can certainly help toggle the decorator between a numba compiled function or a NoOp.

Thanks for your initial help because I have built the above on top of your suggestions.

Let me know what you think.

Regards,
Mani

Reply all
Reply to author
Forward
0 new messages