3

I used numba to accelerate parts of my Python code using the autojit decorator. Tests pass and %timeit shows that the code is indeed accelerated.

The first execution is slow because, I assume, numba is compiling the code. Makes sense. But when I run a suite of tests, they run extremely slowly, from 10 tests in about 10 seconds before my changes to the same tests in 117 seconds. It seems like numba must be compiling again and again, separately for each test.

Can I avoid this? I have tried running one simple test in the setUp function, thinking that might compile the function there once for all the tests, but this did not change the run time significantly.

Charles
  • 50,943
  • 13
  • 104
  • 142
Dan Allan
  • 34,073
  • 6
  • 70
  • 63
  • Seems like caching numba compilations would be the best solution (but it isn't implemented yet). The best thing I can think up off the top of my head is making a custom decorator that turns numba off during unittests. – U2EF1 Jan 26 '14 at 22:36
  • Do you happen to know the recommended solution for turning numba off temporarily? – Dan Allan Jan 26 '14 at 22:53

1 Answers1

1

Something like this might work:

from numba import autojit

def autojit_except_when_unit_testing(func):
    if UNIT_TESTING:
        return func
    return autojit(func)

Possibly also bump numba's issues regarding caching, as this is a pretty important use case. I would normally be pretty hesitant to run unit tests and production code in such different environments, but unit tests that take forever don't get run as often. You should almost certainly test with numba as well, just less often.

U2EF1
  • 12,907
  • 3
  • 35
  • 37
  • Absolutely. I think I will run both, and hope for caching soon. Thanks! P.S. This is the [caching feature request issue](https://github.com/numba/numba/issues/224). – Dan Allan Jan 26 '14 at 23:07
  • That sounds like a bad idea. You want to test that your function works correctly when autojitted. – asmeurer Jan 27 '14 at 17:12
  • @asmeurer It's a tradeoff. If the tests run fast, he will run them more often. But you are correct that he should definitely be running the real thing on a regular basis. – U2EF1 Jan 27 '14 at 19:09
  • But you have to understand that autojit can potentially change the semantics of the program (especially if there is a bug in numba). So for tests, you really need to test what the code will actually be doing. – asmeurer Jan 27 '14 at 21:40
  • @asmeurer Is there a way to test jitted functions? Some test is better that no test. Been looking for a way to test jit functions with pytest, but can't find anything anywhere – Paul Russell Nov 16 '22 at 18:00