命令行界面

Numba 是一个 Python 包,通常您从 Python 中 import numba 并使用 Python 应用程序编程接口 (API)。然而,Numba 也带有一个命令行界面 (CLI),即一个在您安装 Numba 时会安装的工具 numba

当前,CLI 的唯一目的是让您快速显示有关您的系统和安装的信息,或者快速获取使用 Numba 的 Python 脚本的调试信息。

用法

要从终端使用 Numba CLI,请使用 numba,后面跟着选项和参数,例如 --help-s,如下所述。

有时当您输入 numba 时,可能会出现“命令未找到”错误,因为您的 PATH 未正确配置。在这种情况下,您可以使用等效命令 python -m numba。如果仍然出现“命令未找到”,请尝试 import numba,如下文所示:依赖列表

numbapython -m numba 这两个版本是相同的。第一个输入起来更短,但是如果您的 PATH 不包含 numba 的安装位置而导致您收到“命令未找到”错误,那么拥有 python -m numba 变体就很有用。

要从 IPython 或 Jupyter 使用 Numba CLI,请使用 !numba,即在命令前加上感叹号。这是一项通用的 IPython/Jupyter 功能,用于执行 shell 命令,它在常规的 python 终端中不可用。

帮助

要查看所有可用选项,请使用 numba --help

$ numba --help
usage: numba [-h] [--annotate] [--dump-llvm] [--dump-optimized]
             [--dump-assembly] [--annotate-html ANNOTATE_HTML] [-s]
             [--sys-json SYS_JSON]
             [filename]

positional arguments:
filename              Python source filename

optional arguments:
-h, --help            show this help message and exit
--annotate            Annotate source
--dump-llvm           Print generated llvm assembly
--dump-optimized      Dump the optimized llvm assembly
--dump-assembly       Dump the LLVM generated assembly
--annotate-html ANNOTATE_HTML
                        Output source annotation as html
-s, --sysinfo         Output system information for bug reporting
--sys-json SYS_JSON   Saves the system info dict as a json file

系统信息

numba -s(或等效的 numba --sysinfo)命令会打印大量关于您的系统、Numba 安装以及相关依赖项的信息。

请记住:您可以使用带感叹号的 !numba -s 从 IPython 或 Jupyter 中查看此信息。

示例输出

$ numba -s

System info:
--------------------------------------------------------------------------------
__Time Stamp__
Report started (local time)                   : 2022-11-30 15:40:42.368114
UTC start time                                : 2022-11-30 15:40:42.368129
Running time (s)                              : 2.563586

__Hardware Information__
Machine                                       : x86_64
CPU Name                                      : ivybridge
CPU Count                                     : 3
Number of accessible CPUs                     : ?
List of accessible CPUs cores                 : ?
CFS Restrictions (CPUs worth of runtime)      : None

CPU Features                                  : 64bit aes avx cmov cx16 cx8 f16c
                                                fsgsbase fxsr mmx pclmul popcnt
                                                rdrnd sahf sse sse2 sse3 sse4.1
                                                sse4.2 ssse3 xsave

Memory Total (MB)                             : 14336
Memory Available (MB)                         : 11540

__OS Information__
Platform Name                                 : macOS-10.16-x86_64-i386-64bit
Platform Release                              : 20.6.0
OS Name                                       : Darwin
OS Version                                    : Darwin Kernel Version 20.6.0: Thu Sep 29 20:15:11 PDT 2022; root:xnu-7195.141.42~1/RELEASE_X86_64
OS Specific Version                           : 10.16   x86_64
Libc Version                                  : ?

__Python Information__
Python Compiler                               : Clang 14.0.6
Python Implementation                         : CPython
Python Version                                : 3.10.8
Python Locale                                 : en_US.UTF-8

__Numba Toolchain Versions__
Numba Version                                 : 0+untagged.gb91eec710
llvmlite Version                              : 0.40.0dev0+43.g7783803

__LLVM Information__
LLVM Version                                  : 11.1.0

__CUDA Information__
CUDA Device Initialized                       : False
CUDA Driver Version                           : ?
CUDA Runtime Version                          : ?
CUDA NVIDIA Bindings Available                : ?
CUDA NVIDIA Bindings In Use                   : ?
CUDA Detect Output:
None
CUDA Libraries Test Output:
None

__NumPy Information__
NumPy Version                                 : 1.23.4
NumPy Supported SIMD features                 : ('MMX', 'SSE', 'SSE2', 'SSE3', 'SSSE3', 'SSE41', 'POPCNT', 'SSE42', 'AVX', 'F16C')
NumPy Supported SIMD dispatch                 : ('SSSE3', 'SSE41', 'POPCNT', 'SSE42', 'AVX', 'F16C', 'FMA3', 'AVX2', 'AVX512F', 'AVX512CD', 'AVX512_KNL', 'AVX512_SKX', 'AVX512_CLX', 'AVX512_CNL', 'AVX512_ICL')
NumPy Supported SIMD baseline                 : ('SSE', 'SSE2', 'SSE3')
NumPy AVX512_SKX support detected             : False

__SVML Information__
SVML State, config.USING_SVML                 : False
SVML Library Loaded                           : False
llvmlite Using SVML Patched LLVM              : True
SVML Operational                              : False

__Threading Layer Information__
TBB Threading Layer Available                 : True
+-->TBB imported successfully.
OpenMP Threading Layer Available              : True
+-->Vendor: Intel
Workqueue Threading Layer Available           : True
+-->Workqueue imported successfully.

__Numba Environment Variable Information__
None found.

__Conda Information__
Conda Build                                   : not installed
Conda Env                                     : 4.12.0
Conda Platform                                : osx-64
Conda Python Version                          : 3.9.12.final.0
Conda Root Writable                           : True

__Installed Packages__
(output truncated due to length)

调试

如上述帮助输出所示,numba 命令包含可以帮助您调试 Numba 编译代码的选项。

要试用它,请创建一个名为 myscript.py 的示例脚本

import numba

@numba.jit
def f(x):
    return 2 * x

f(42)

然后执行以下命令之一

$ numba myscript.py --annotate
$ numba myscript.py --annotate-html myscript.html
$ numba myscript.py --dump-llvm
$ numba myscript.py --dump-optimized
$ numba myscript.py --dump-assembly