numba用户手册 10调试

numba用户手册

1.numba基础

2.@jit

3.使用自动并行化@jit

4.性能提升

5.创建ufunc

6.@jitclass

7.@cfunc

8.提前编译代码AOT

9.numba线程

10.调试

 

---------------------------------------------------------------------------------------------------------------------

8.1.调试



#脚本 myscript.py

import numba



@numba.jit

def f(x):

    return 2 * x



f(42)



# 调试:

C:\Users\Administrator>numba myscript.py --annotate

C:\Users\Administrator>numba myscript.py --annotate-html myscript.html

C:\Users\Administrator> numba myscript.py --dump-llvm

C:\Users\Administrator>numba myscript.py --dump-optimized

C:\Users\Administrator> numba myscript.py --dump-assemb



---------------------------------------------------------------------------------------------------------------------



8.2.诊断# 设置环境变量 NUMBA_PARALLEL_DIAGNOSTICS



@njit(parallel=True)

def test(x):

    n = x.shape[0]

    a = np.sin(x)

    b = np.cos(a * a)

    acc = 0

    for i in prange(n - 2):

        for j in prange(n - 1):

            acc += b[i] + b[j + 1]

    return acc



test(np.arange(10))

test.parallel_diagnostics(level=4)                 #诊断信息详细程度1最不详细,4详细



---------------------------------------------------------------------------------------------------------------------

8.3.numba帮助



C:\Users\Administrator>numba --help

C:\Users\Administrator>numba -snumba --sysinfo

IPython或Jupyter查看信息:!numba -s



---------------------------------------------------------------------------------------------------------------------

8.4.故障排除和技巧

1)什么编译:建议只尝试编译关键代码小程序加速不明显;如函数执行时间少于10 µs则保留该函数.例外jitted函数调用该函数(@jit)



2)代码无法编译

(1)类型推断失败

    @jit(nopython=True)

    def f(x, y): return x + y



    f(1, 2)                                       #numba能够正确地推断出类型

    f(1, (2,))                                    #如用元组和数字来调用函数编译错误



(2)不能静态地确定函数的返回类型

    @jit(nopython=True)

    def f(x):return ((1,) if x>10 else 1)



    f(10)                                         #错误



(3)无类型的列表问题                               #列表必须包含有相同类型

    @jit(nopython=True)

    def f(): return [1, 2, 3]                     #已知类型的列表



    @jit(nopython=True)

    def f(x):

        tmp = [] # defined empty

        for i in range(x):tmp.append(i)           #使用空列表,但类型可以推断 `i`

        return tmp



    @jit(nopython=True)

    def f(x): tmp = [] ;return (tmp, x)           #ERROR: 类型无法推断the type of `tmp` is unknown



    #指定空列表类型,或指定参数为nb.typed.List()           #the type of `tmp` is known, but it is still empty

    @jit(nopython=True)

    def f(x):return ([np.float32(x) for x in range(0)],x)



 ---------------------------------------------------------------------------------------------------------------------



3)编译的代码太慢#编译nopython模式失败

    @jit

    def f(a, b):

        s = a + float(b)

        return s



    f(1, 2)#3.0

    f.inspect_types()                            #查看类型是否推断成功 f (int64, int64)



    f(1, "2")# 3.0

    f.inspect_types()                            #f (int64, unicode_type)...



---------------------------------------------------------------------------------------------------------------------

4)将函数(@jit)作为实参#有额外的开销

    @jit(nopython=True)

    def f(g, x):return g(x) + g(-x)



    result = f(jitted_g_function, 1)



    def make_f(g):                               #可以使用工厂函数来捕获闭包中的函数参数

        @jit(nopython=True)                      #Note: a new f() is created each time make_f() is called!

        def f(x):

            return g(x) + g(-x)

        return f



    f = make_f(jitted_g_function)

    result = f(1)



---------------------------------------------------------------------------------------------------------------------

5)PyInstaller打包



llvmlite可能会遇到问题.llvmlite需一个非Python DLL才能工作,但打包程序不会自动检测到它.

必将DLL位置告知打包程序:C:\ProgramData\Anaconda3\Lib\site-packages\llvmlite\binding\llvmlite.dll(xx.so)



---------------------------------------------------------------------------------------------------------------------

6)numba语言环境--错误消息:



RuntimeError: Failed at nopython (nopython mode backend)

LLVM will produce incorrect floating-point code in the current locale



遇到LLVM错误(例如matplotlib的Qt后端)会发生这种情况.

解决:将区域设置强制恢复为其默认值locale.setlocale(locale.LC_NUMERIC, 'C')



---------------------------------------------------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值