python numba.jit(该装饰器用于将Python函数编译为本机代码、python运算加速器)
生活随笔
收集整理的這篇文章主要介紹了
python numba.jit(该装饰器用于将Python函数编译为本机代码、python运算加速器)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from decorators.py
def jit(signature_or_function=None, locals={}, target='cpu', cache=False,pipeline_class=None, **options):"""This decorator is used to compile a Python function into native code.該裝飾器用于將Python函數編譯為本機代碼。Args-----signature:The (optional) signature or list of signatures to be compiled.If not passed, required signatures will be compiled when thedecorated function is called, depending on the argument values.As a convenience, you can directly pass the function to be compiledinstead.(可選)簽名或要編譯的簽名列表。如果未傳遞,則在調用裝飾函數時,將根據參數值編譯所需的簽名。為方便起見,您可以直接傳遞要編譯的函數。locals: dictMapping of local variable names to Numba types. Used to override thetypes deduced by Numba's type inference engine.字典本地變量名稱到Numba類型的映射。 用于覆蓋由Numba的類型推斷引擎推斷的類型。target: strSpecifies the target platform to compile for. Valid targets are cpu,gpu, npyufunc, and cuda. Defaults to cpu.字符串指定要編譯的目標平臺。 有效的目標是cpu,gpu,npyufunc和cuda。 默認為cpu。pipeline_class: type numba.compiler.CompilerBaseThe compiler pipeline type for customizing the compilation stages.鍵入numba.compiler.CompilerBase用于定制編譯階段的編譯器管道類型。options:For a cpu target, valid options are:對于cpu目標,有效的選項是:nopython: boolSet to True to disable the use of PyObjects and Python APIcalls. The default behavior is to allow the use of PyObjectsand Python API. Default value is False.布爾設置為True以禁用PyObjects和Python API的使用電話。 默認行為是允許使用PyObjects和Python API。 默認值為False。forceobj: boolSet to True to force the use of PyObjects for every value.Default value is False.布爾設置為True以強制對每個值使用PyObjects。默認值為False。looplift: boolSet to True to enable jitting loops in nopython mode whileleaving surrounding code in object mode. This allows functionsto allocate NumPy arrays and use Python objects, while thetight loops in the function can still be compiled in nopythonmode. Any arrays that the tight loop uses should be createdbefore the loop is entered. Default value is True.布爾設置為True可在nopython模式下啟用快速循環,而將周圍的代碼保留在對象模式下。 這允許函數分配NumPy數組并使用Python對象,而函數中的緊密循環仍可以在nopython模式下進行編譯。 進入循環之前,應創建緊密循環使用的任何數組。 默認值為True。error_model: strThe error-model affects divide-by-zero behavior.Valid values are 'python' and 'numpy'. The 'python' modelraises exception. The 'numpy' model sets the result to*+/-inf* or *nan*. Default value is 'python'.字符串 誤差模型會影響除零行為。 有效值為'python'和'numpy'。 “ python”模型引發異常。 “ numpy”模型將結果設置為* + /-inf *或* nan *。 默認值為“ python”。inline: str or callableThe inline option will determine whether a function is inlinedat into its caller if called. String options are 'never'(default) which will never inline, and 'always', which willalways inline. If a callable is provided it will be called withthe call expression node that is requesting inlining, thecaller's IR and callee's IR as arguments, it is expected toreturn Truthy as to whether to inline.NOTE: This inlining is performed at the Numba IR level and is inno way related to LLVM inlining.字符串或可調用內聯選項將確定函數是否在調用方內聯。 字符串選項是“從不”(默認)和行“永遠”(將始終內聯),“從不”(將永遠不內聯)。 如果提供了callable,它將使用請求內聯的調用表達式節點,調用方的IR和被調用方的IR作為自變量進行調用,則將返回Truthy關于是否內聯。注意:此內聯是在Numba IR級別執行的,與LLVM內聯無關。Returns--------A callable usable as a compiled function. Actual compiling will bedone lazily if no explicit signatures are passed.可調用的可用作編譯函數。 如果未傳遞任何顯式簽名,則將延遲進行實際編譯。Examples--------The function can be used in the following ways:該功能可以通過以下方式使用:1) jit(signatures, target='cpu', **targetoptions) -> jit(function)Equivalent to:d = dispatcher(function, targetoptions)for signature in signatures:d.compile(signature)Create a dispatcher object for a python function. Then, compilethe function with the given signature(s).為python函數創建一個調度程序對象。 然后,使用給定的簽名編譯函數。Example:@jit("int32(int32, int32)")def foo(x, y):return x + y@jit(["int32(int32, int32)", "float32(float32, float32)"])def bar(x, y):return x + y2) jit(function, target='cpu', **targetoptions) -> dispatcherCreate a dispatcher function object that specializes at call site.創建一個專門針對呼叫站點的調度程序功能對象。Examples:@jitdef foo(x, y):return x + y@jit(target='cpu', nopython=True)def bar(x, y):return x + y"""if 'argtypes' in options:raise DeprecationError(_msg_deprecated_signature_arg.format('argtypes'))if 'restype' in options:raise DeprecationError(_msg_deprecated_signature_arg.format('restype'))# Handle signatureif signature_or_function is None:# No signature, no functionpyfunc = Nonesigs = Noneelif isinstance(signature_or_function, list):# A list of signatures is passedpyfunc = Nonesigs = signature_or_functionelif sigutils.is_signature(signature_or_function):# A single signature is passedpyfunc = Nonesigs = [signature_or_function]else:# A function is passedpyfunc = signature_or_functionsigs = Nonedispatcher_args = {}if pipeline_class is not None:dispatcher_args['pipeline_class'] = pipeline_classwrapper = _jit(sigs, locals=locals, target=target, cache=cache,targetoptions=options, **dispatcher_args)if pyfunc is not None:return wrapper(pyfunc)else:return wrapper使用示例:
總結
以上是生活随笔為你收集整理的python numba.jit(该装饰器用于将Python函数编译为本机代码、python运算加速器)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中函数能够随处定义与调用吗?
- 下一篇: python numba.jit 警告: