在DigitalOcean服务器中消除PyTorch的NNPACK警告方案求助
解决NNPACK初始化警告干扰JSON输出的问题
问题背景
我开发了一款基于Python的二维码识别脚本,使用依赖PyTorch的qrdet库。在DigitalOcean Linux服务器运行时,出现以下警告:
[W NNPACK.cpp:64] Could not initialize NNPACK! Reason: Unsupported hardware.
该警告不影响功能,但会破坏其他进程依赖的合法JSON输出。已尝试6种无效方案:
- 运行脚本前执行
export USE_NNPACK=0设置环境变量 - 安装PyTorch前设置
USE_NNPACK=0 - 运行脚本前设置
CUDA_VISIBLE_DEVICES="" - 通过
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu重装PyTorch - 在脚本内配置PyTorch:
x = torch.tensor([1, 2, 3]) x = x.to('cpu') torch.backends.cudnn.enabled = False torch.cuda.is_available = lambda : False
- 使用
warning包移除警告
环境版本信息
PyTorch version: 2.0.1+cpu Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A OS: Ubuntu 18.04.6 LTS (x86_64) GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 Clang version: Could not collect CMake version: version 3.27.4 Libc version: glibc-2.27 Python version: 3.8.0 (default, Dec 9 2021, 17:53:27) [GCC 8.4.0] (64-bit runtime) Python platform: Linux-4.15.0-213-generic-x86_64-with-glibc2.27 Is CUDA available: False CUDA runtime version: No CUDA CUDA_MODULE_LOADING set to: N/A GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 1 Core(s) per socket: 8 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 63 Model name: DO-Regular Stepping: 2 CPU MHz: 2199.996 BogoMIPS: 4399.99 Virtualization: VT-x Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0-7 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm cpuid_fault invpcid_single pti ssbd ibrs ibpb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat md_clear Versions of relevant libraries: [pip3] numpy==1.24.4 [pip3] torch==2.0.1+cpu [pip3] torchvision==0.15.2+cpu [conda] Could not collect
可行解决方案
方案1:直接禁用PyTorch的NNPACK后端
在脚本最开头添加以下代码,强制PyTorch不使用NNPACK:
import torch # 禁用NNPACK后端 torch.backends.nnpack.enabled = False # 可选:若不需要XNNPACK也可一并禁用 torch.backends.xnnpack.enabled = False
方案2:过滤PyTorch底层C++警告
由于该警告来自PyTorch的C++层,Python的warnings模块无法捕获,可通过重定向stderr过滤特定警告:
import sys original_stderr = sys.stderr class FilteredStderr: def write(self, line): if "Could not initialize NNPACK! Reason: Unsupported hardware" not in line: original_stderr.write(line) def flush(self): original_stderr.flush() sys.stderr = FilteredStderr() # 必须在重定向后导入torch和qrdet import torch import qrdet # 后续业务代码...
方案3:降级PyTorch版本
PyTorch 2.0.x在部分虚拟化CPU环境下的NNPACK检测逻辑存在问题,降级到1.13.1+cpu版本可规避:
pip3 uninstall -y torch torchvision pip3 install torch==1.13.1+cpu torchvision==0.14.1+cpu --index-url https://download.pytorch.org/whl/cpu
方案4:全局环境变量禁用NNPACK
在启动脚本的命令前添加环境变量,确保PyTorch加载时直接禁用NNPACK:
export TORCH_NNPACK_ENABLED=0 && python your_script.py
内容的提问来源于stack exchange,提问作者Nicolás Rossi
相关产品推荐
相关产品推荐

