AVX2 Instructions not found warning, when they are actually availabe.
Expected Behavior
When importing dlib in python I expect not to get any warning messages about AVX2 instructions not being available.
I have an Intel(R) Core(TM) i7-7700K cpu. It has AVX2 instructions since: grep avx2 /proc/cpuinfo matches avx2 in the flags section.
Also on Intel's webpage for the i7-7700K it says that AVX2 is included in the 'Instruction Set Extensions' section:
https://www.intel.com/content/www/us/en/products/processors/core/i7-processors/i7-7700k.html
Current Behavior
When importing dlib in python I get the following warning message every time:
Dlib was compiled to use AVX2 instructions, but these aren't available on your machine.
I am unsure if other aspects of dlib are impacted, or if it's only the warning message that gets printed.
This appears to be caused by lines 150 and 155-157 in dlib/simd/simd_check.h. Since the function on line 150 returns false instead of true.
Steps to Reproduce
Use an i7-7700K CPU. Compile dlib for Python 3 with CUDA support, using the following compiler environment variables and build command in bash:
export CC=$(which gcc-6)
export CXX=$(which g++-6)
python3 setup.py build -j "$(nproc)" --yes USE_AVX_INSTRUCTIONS --yes USE_SSE2_INSTRUCTIONS --yes USE_SSE4_INSTRUCTIONS
Then run the following install command:
python3 setup.py install --skip-build --prefix=/usr --root="<install dir>" --optimize=1
- Version: 19.10
- Where did you get dlib: The source code was downloaded from dlib.net and then compiled using the following PKGBUILD: https://aur.archlinux.org/pkgbase/python-dlib-cuda/
The PKGBUILD in essence runs the commands mentioned in the 'Steps to Reproduce' section.
- Platform:
lsb_release -aoutput: LSB Version: 1.4 Distributor ID: Arch Description: Arch Linux Release: rolling Codename: n/ainxioutput: CPU: Quad Core Intel Core i7-7700K (-MT MCP-) speed/min/max: 3300/800/5000 MHz GPU: NVIDIA GP104 [GeForce GTX 1080] Kernel: 4.15.13-1-ARCH x86_64 Up: 2:56 Mem: 4276.1/16002.3 MiB (26.7%) HDD: 2.27 TiB (94.6% used) Procs: 295 Shell: bash 4.4.19 inxi: 2.9.07
Python version: 3.6.4 CUDA version: 9.1.85 CUDNN version: 7.1.2
- Compiler:
CC: gcc-6 (GCC) 6.4.1 20171003
(assigned by
export CC=$(which gcc-6)) CXX: gcc-6 (GCC) 6.4.1 20171003 (assigned byexport CXX=$(which g++-6))
Edit: fix typos Edit2: specify python version
That's what you did? There is no -j in dlib's setup.py. I just tried it to be sure and got an error. I also have the same CPU in my workstation and I don't see this issue.
@davisking the -j option is available on line 150 in setup.py:
build_args += ['--', '-j'+str(num_available_cpu_cores(2))]
Although it only works with Python 3, and will indeed cause an error when compiling with Python 2.
I forgot to specify that I am building for Python 3. (Since on Arch python refers to python3.)
I checked and with Python 2 I don't get the warning message.
I have just rebuilt it for Python 3 without using the -j option, but I still get the warning message .
150 in setup.py doesn't parse your -j option. But yes, I see that when using pyhton3 you don't get an error when giving -j. My point was more that your instructions couldn't be correct.
I also just ran your exact commands on a machine with a i7-7700K and I don't see the warning. I suspect you aren't really running things exactly the way you think.
My bad about the -j option.
You are correct that things were not being run the way I thought. :)
I have isolated the warning to being caused by having the CXXFLAGS environment variable set as the following:
CXXFLAGS="-march=native -O2 -pipe -fstack-protector-strong -fno-plt"
However when using the system default the warning message does not appear:
CXXFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt"
From experimenting I have found the specific flag that leads to the problem is: -mavx2
If I don't pass -mavx2 directly or via -march=native, but I still use --yes USE_AVX_INSTRUCTIONS, does it actually compile dlib to support both avx and avx2?
I realize this may be a silly question but I'm just a little confused, since when passing only -mavx via:
-march=x86-64 -mavx -mtune=generic
there are no issues, but when passing -mavx2 via:
-march=x86-64 -mavx2 -mtune=generic or -march=x86-64 -mavx -mavx2 -mtune=generic
I get the warning about avx2 not being available.
I have no idea. It's either enabling something your cpu doesn't have or the check is wrong.
Fair enough.
I'll close the issue since I found the cause and the way to fix it.
Out of curiosity, what happens when you run this program?
#include <iostream>
#include <dlib/simd.h>
using namespace std;
using namespace dlib;
int main()
{
cout << "has avx: "<< cpu_has_avx_instructions() << endl;
cout << "has avx2: "<< cpu_has_avx2_instructions() << endl;
}
Those are the things it's checking, and for me using the same CPU as you, no matter what compiler settings I use it correctly says I have both avx and avx2.
No worries I'll test it and tell you the result. However, since I'm currently visiting my parents I won't be able to test it until this Thursday.
Before opening my issue I did try running that part of the code from dlib/simd/simd_check.h that checks for CPU extensions, and if I recall correctly all of them were found except for avx2.
Hey @davisking,
I've run the little test file by compiling it in my regular system and inside a clean chroot (I recompiled dlib inside the chroot as well to have a clean install of it there). In all scenarios it failed to detect avx2:
# I copy pasted your test script into dlib-test.cpp
# Results
# Normal system, with dlib built in the normal system environment
cd <dlib-test dir>
g++ -o dlib-test dlib-test.cpp
chmod u+x ./dlib-test
./dlib-test
has avx: 1
has avx2: 0
# Chroot system, with dlib built inside the chroot environment
# Result from chrooting into clean chroot
[root@ArchYin /]# dlib-test
has avx: 1
has avx2: 0
# Normal system using dlib-test built inside clean chroot
[lev@ArchYin chroot-test]$ dlib-test
has avx: 1
has avx2: 0
Here is the PKGBUILD file I used to build the chroot version (it's in essence the same build instructions, but it just packages it for the ArchLinux format); the PKGBUILD was based on the dlib PKGBUILD that's distributed through the AUR (hence all the references to dlib inside it).
Huh, beats me. Something is funny on your system. It's also a little weird that you didn't have to give -std=c++11 to g++