[For Numba beginners] It is better to set nopython = True to check if the speedup is realized (for version 0.50.0 or earlier)

Note: object mode fall-back will be abolished after Numba version 0.50.0 (http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode- fall-back-behaviour-when-using-jit), the content of this article is limited to earlier versions.

What is Numba? Please see this article for those who say: https://qiita.com/gyu-don/items/9d223b007ca620e95abc


In Numba, we first try to speed up in nopython mode, and if that fails, we switch to object mode (which is rather slow) and execute it (object mode fall-back). However, this article says that it is better to set nopython = True from the beginning for safety, because it will change whether or not it will be in nopython mode with a relatively small code change. It is the contents of. (The default is nopython = False, and if you set this to nopython = True, it will return an error if nopython mode fails.)

For example, in the following code, if you comment out the assert statement, it will be executed in nopython mode, and if you do not comment it out, it will be executed in object mode.

import numpy as np
import numba

@numba.jit
def myfunc():
    for i in range(1000):
        for k in range(1000):
            #assert 0<1
            pass
    hoge = [1,2,3]
    hoge = np.array(hoge)

myfunc()

In the case of this code, it is not good Numba writing at the time of assigning variables of different types (list and ndarray) to the same variable name hoge, but even in such bad writing, in nopython mode It seems that it may be executed. However, when there was an assert statement in the for loop, it immediately went into object mode (I don't know the cause). Also, even if you write it well, if you use a function that nopython mode does not support, you will be in object mode. Example ⇒ http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#id1

In recent versions of Numba, a warning message is displayed when execution of nopython mode fails, but in older versions of Numba, no warning message is displayed even if nopython mode fails __ (version 0.48.0) I got a warning, but version 0.41.0 did not give a warning), so even if the execution speed suddenly slows down while playing with various codes, it is difficult to notice the cause.

@ numba.jit (nopython = True) is safe because it will return an error if the execution of nopython mode fails.

Recommended Posts

[For Numba beginners] It is better to set nopython = True to check if the speedup is realized (for version 0.50.0 or earlier)
How to use any or all to check if it is in a dictionary (Hash)
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
Check "[Windows] How to tell if the exe is x64 or x86 Part2" on MacOS Go
I made a function to check if the webhook is received in Lambda for the time being
[Blender] Script to check if the selected one is a mesh
Tensorflow-GPU seems to be together if it is TF2.0 or later?