Python 运行导入 tensorflow 后出现非法指令(核心转储)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49094597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal instruction (core dumped) after running import tensorflow
提问by Gerry
I created a fresh virtual environment: virtualenv -p python2 test_venv/
And installed tensorflow: pip install --upgrade --no-cache-dir tensorflow
我创建了一个全新的虚拟环境:virtualenv -p python2 test_venv/
并安装了 tensorflow:pip install --upgrade --no-cache-dir tensorflow
import tensorflow
gives me Illegal instruction (core dumped)
import tensorflow
给我 Illegal instruction (core dumped)
Please help me understand what's going on and how I can fix it. Thank you.
请帮助我了解发生了什么以及如何解决。谢谢你。
CPU information:
CPU信息:
-cpu
description: CPU
product: Intel(R) Core(TM) i3 CPU M 330 @ 2.13GHz
bus info: cpu@0
version: CPU Version
capabilities: x86-64 fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt lahf_lm tpr_shadow vnmi flexpriority ept vpid dtherm arat cpufreq
Stacktrace obtained with gdb:
使用 gdb 获得的堆栈跟踪:
#0 0x00007fffe5793880 in std::pair<std::__detail::_Node_iterator<std::pair<tensorflow::StringPiece const, std::function<bool (tensorflow::Variant*)> >, false, true>, bool> std::_Hashtable<tensorflow::StringPiece, std::pair<tensorflow::StringPiece const, std::function<bool (tensorflow::Variant*)> >, std::allocator<std::pair<tensorflow::StringPiece const, std::function<bool (tensorflow::Variant*)> > >, std::__detail::_Select1st, std::equal_to<tensorflow::StringPiece>, tensorflow::StringPieceHasher, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_M_emplace<std::pair<tensorflow::StringPiece, std::function<bool (tensorflow::Variant*)> > >(std::integral_constant<bool, true>, std::pair<tensorflow::StringPiece, std::function<bool (tensorflow::Variant*)> >&&) ()
from /media/gerry/hdd_1/ws_hdd/test_venv/local/lib/python2.7/site-packages/tensorflow/python/../libtensorflow_framework.so
#1 0x00007fffe5795735 in tensorflow::UnaryVariantOpRegistry::RegisterDecodeFn(std::string const&, std::function<bool (tensorflow::Variant*)> const&) () from /media/gerry/hdd_1/ws_hdd/test_venv/local/lib/python2.7/site-packages/tensorflow/python/../libtensorflow_framework.so
#2 0x00007fffe5770a7c in tensorflow::variant_op_registry_fn_registration::UnaryVariantDecodeRegistration<tensorflow::Tensor>::UnaryVariantDecodeRegistration(std::string const&) ()
from /media/gerry/hdd_1/ws_hdd/test_venv/local/lib/python2.7/site-packages/tensorflow/python/../libtensorflow_framework.so
#3 0x00007fffe56ea165 in _GLOBAL__sub_I_tensor.cc ()
from /media/gerry/hdd_1/ws_hdd/test_venv/local/lib/python2.7/site-packages/tensorflow/python/../libtensorflow_framework.so
#4 0x00007ffff7de76ba in call_init (l=<optimized out>, argc=argc@entry=2, argv=argv@entry=0x7fffffffd5c8, env=env@entry=0xa7b4d0)
at dl-init.c:72
#5 0x00007ffff7de77cb in call_init (env=0xa7b4d0, argv=0x7fffffffd5c8, argc=2, l=<optimized out>) at dl-init.c:30
#6 _dl_init (main_map=main_map@entry=0xa11920, argc=2, argv=0x7fffffffd5c8, env=0xa7b4d0) at dl-init.c:120
#7 0x00007ffff7dec8e2 in dl_open_worker (a=a@entry=0x7fffffffb5c0) at dl-open.c:575
#8 0x00007ffff7de7564 in _dl_catch_error (objname=objname@entry=0x7fffffffb5b0, errstring=errstring@entry=0x7fffffffb5b8,
mallocedp=mallocedp@entry=0x7fffffffb5af, operate=operate@entry=0x7ffff7dec4d0 <dl_open_worker>, args=args@entry=0x7fffffffb5c0)
at dl-error.c:187
#9 0x00007ffff7debda9 in _dl_open (
file=0x7fffea7cbc34 "/media/gerry/hdd_1/ws_hdd/test_venv/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so", mode=-2147483646, caller_dlopen=0x51ad19 <_PyImport_GetDynLoadFunc+233>, nsid=-2, argc=<optimized out>, argv=<optimized out>, env=0xa7b4d0)
at dl-open.c:660
#10 0x00007ffff75ecf09 in dlopen_doit (a=a@entry=0x7fffffffb7f0) at dlopen.c:66
#11 0x00007ffff7de7564 in _dl_catch_error (objname=0x9b1870, errstring=0x9b1878, mallocedp=0x9b1868, operate=0x7ffff75eceb0 <dlopen_doit>,
args=0x7fffffffb7f0) at dl-error.c:187
#12 0x00007ffff75ed571 in _dlerror_run (operate=operate@entry=0x7ffff75eceb0 <dlopen_doit>, args=args@entry=0x7fffffffb7f0) at dlerror.c:163
#13 0x00007ffff75ecfa1 in __dlopen (file=<optimized out>, mode=<optimized out>) at dlopen.c:87
#14 0x000000000051ad19 in _PyImport_GetDynLoadFunc ()
#15 0x000000000051a8e4 in _PyImport_LoadDynamicModule ()
#16 0x00000000005b7b1b in ?? ()
#17 0x00000000004bc3fa in PyEval_EvalFrameEx ()
#18 0x00000000004c136f in PyEval_EvalFrameEx ()
#19 0x00000000004b9ab6 in PyEval_EvalCodeEx ()
#20 0x00000000004b97a6 in PyEval_EvalCode ()
#21 0x00000000004b96df in PyImport_ExecCodeModuleEx ()
#22 0x00000000004b2b06 in ?? ()
#23 0x00000000004a4ae1 in ?? ()
回答by edit
I would use older version. Looks like your CPU does not support AVX instructions.
我会使用旧版本。看起来您的 CPU 不支持 AVX 指令。
Quoting from their Release Page
引用他们的发布页面
Breaking Changes
Prebuilt binaries are now built against CUDA 9.0 and cuDNN 7.
Prebuilt binaries will use AVX instructions. This may break TF on older CPUs.
You have atleast two options:
你至少有两个选择:
Use tensorflow 1.5 or older
Build from source
使用 tensorflow 1.5 或更早版本
从源代码构建
Regarding your concern for differences, you will miss out on new features, but most basic features and documentations are not that different.
关于您对差异的关注,您会错过新功能,但大多数基本功能和文档并没有什么不同。
回答by mjflory
Unfortunately, 1.6 has given many people the same error. I received it after installing 1.7 on a machine with an old Core2 CPU. I've settled with 1.5, as I can't fit the big graphics card in the machine with the up-to-date processor!
不幸的是,1.6 给了很多人同样的错误。我在一台旧的 Core2 CPU 的机器上安装了 1.7 后收到了它。我已经解决了 1.5,因为我无法在装有最新处理器的机器中安装大显卡!
回答by mikaelfs
As explained in the accepted answer, this issue can be fixed either by installing older version of TensorFlow (v1.5) or building from source. Between the two, building from source is arguably a preferred route despite the additional effort. Granted that the binary contains the most updated components of TensorFlow.
如已接受的答案中所述,可以通过安装旧版本的 TensorFlow (v1.5) 或从源代码构建来解决此问题。在这两者之间,尽管付出了额外的努力,但从源头构建可以说是首选途径。假定二进制文件包含 TensorFlow 的最新组件。
This articleexplains how to build TensorFlow from sources and optimizes for the older CPU. The key is in detecting the CPU flags and enable all the CPU flags for optimization when configuring the build.
本文介绍了如何从源代码构建 TensorFlow 并针对旧 CPU 进行优化。关键在于检测 CPU 标志并在配置构建时启用所有 CPU 标志以进行优化。
The following command is used to detect common CPU optimization flags:
以下命令用于检测常见的 CPU 优化标志:
$ grep flags -m1 /proc/cpuinfo | cut -d ":" -f 2 | tr '[:upper:]' '[:lower:]' | { read FLAGS; OPT="-march=native"; for flag in $FLAGS; do case "$flag" in "sse4_1" | "sse4_2" | "ssse3" | "fma" | "cx16" | "popcnt" | "avx" | "avx2") OPT+=" -m$flag";; esac; done; MODOPT=${OPT//_/\.}; echo "$MODOPT"; }
If by executing the command, -mavx
and/or -mavx2
is not shown, it can be confirmed that AVX support is missing and the source build should be done with other optimization flags displayed in the output.
如果通过执行命令,-mavx
和/或未-mavx2
显示,则可以确认缺少 AVX 支持,并且应使用输出中显示的其他优化标志来完成源构建。
In a related article, the common root cause of this issue is discussed in more details, which is provided as an additional reference.
在相关文章 中,更详细地讨论了此问题的常见根本原因,作为额外参考提供。
回答by Ehab AlBadawy
I had a similar issue and it turned out that it is due to I have slightly old CPU and that doesn't work very well with 1.6+ versions of TensorFlow https://www.tensorflow.org/install/source
我有一个类似的问题,结果证明这是因为我的 CPU 有点旧,并且在 1.6+ 版本的 TensorFlow https://www.tensorflow.org/install/source上不能很好地工作
Note: Starting with TensorFlow 1.6, binaries use AVX instructions which may not run on older CPUs.
注意:从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在较旧的 CPU 上运行。
So as mentioned before you can either install TensorFlow 1.5, or if you still want the latest version of TF, you will need to install it with conda instead (both solutions worked with me)
因此,如前所述,您可以安装 TensorFlow 1.5,或者如果您仍然想要最新版本的 TF,则需要使用 conda 来安装它(这两种解决方案都适用于我)
For conda installation:
对于 conda 安装:
conda create -n tensorflow
conda install tensorflow-gpu -n tensorflow
回答by Laurent S
There is an issue on githubabout this, which seems to have gotten little interest from the tensorflow team, unfortunately.
github 上有一个关于此的问题,不幸的是,tensorflow 团队似乎对此兴趣不大。
There are a few community builds around the web that might work depending on your situation:
有一些围绕 Web 的社区构建可能会根据您的情况起作用: