首页 > 解决方案 > __init__() 得到了一个意外的关键字参数“py_func”

问题描述

我们将为要在 GPU 上计算的函数使用 numba.jit 装饰器。装饰器有几个参数,但我们只使用目标参数。Target 告诉 jit 为哪个源(“CPU”或“Cuda”)编译代码。“Cuda”对应GPU。但是,如果 CPU 作为参数传递,那么 jit 会尝试优化代码在 CPU 上运行得更快,并提高速度。但我得到了错误——init()得到了一个意外的关键字参数'py_func'

from numba import jit, cuda 
import numpy as np 
# to measure exec time 
from timeit import default_timer as timer    

# normal function to run on cpu 
def func(a):                                 
    for i in range(10000000): 
        a[i]+= 1      

# function optimized to run on gpu  
@jit(target ="cuda")                          
def func2(a): 
    for i in range(10000000): 
        a[i]+= 1
if __name__=="__main__": 
    n = 10000000                            
    a = np.ones(n, dtype = np.float64) 
    b = np.ones(n, dtype = np.float32) 
  
    start = timer() 
    func(a) 
    print("without GPU:", timer()-start)     
  
    start = timer() 
    func2(a) 
    print("with GPU:", timer()-start) 

标签: pythonpython-3.xpython-2.7gpunumba

解决方案


我遇到了同样的问题并尝试了@Tim Roberts 提供的解决方案。但是,我收到以下错误:

ValueError:未指定内核启动配置。使用语法:

kernel_function[blockspergrid, threadsperblock](arg0, arg1, ..., argn)

请参阅 https://numba.pydata.org/numba-doc/latest/cuda/kernels.html#kernel-invocation 寻求帮助。


推荐阅读