Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIFT Illegal Instruction error on macOS Intel with OpenCV 4.6.0 installed with brew #1106

Closed
fspindle opened this issue Oct 4, 2022 · 1 comment
Labels

Comments

@fspindle
Copy link
Contributor

fspindle commented Oct 4, 2022

System: macOS Monterey 12.6
Processor: 2,6 GHz Intel Core i7
OpenCV version: 4.6.0 (brew)

On macOS installing OpenCV 4.6.0 with brew install opencv produce an Illegal Instruction error when running ViSP following tests: testKeyPoint-5 and testKeyPoint-7.

This is an OpenCV brew specific error that could be reproduced by building asift.cpp from source with the following CMakeLists.txt file over aero1.jpg and aero3.jpg images:

project(test-opencv-sift)

cmake_minimum_required(VERSION 3.0)
find_package(openCV)

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(asift asift.cpp)
target_link_libraries(asift ${OpenCV_LIBRARIES})

The issue is the following:

$ ./asift --feature=sift --image1=aero1.jpg --image2=aero3.jpg
extracting with sift...
Illegal instruction: 4

Note:

  • The previous test works with BRISK and ORB keypoints
  • The issue doesn't occur on macOS Monterey 12.6 with an Apple M1 Pro processor
  • The issue doesn't occur when OpenCV either 4.6.0 or master branch build from source:
    $ cd opencv-build
    $ ./bin/example_cpp_asift --feature=sift --image1=../opencv-master/samples/data/aero1.jpg --image2=../opencv-master/samples/data/aero3.jpg
    extracting with sift...
    img1 - 59289 features, img2 - 41386 features
    matching with bruteforce...
    execution time: 12889.65 ms
    21 / 119 inliers/matched
    visualizing...
    done
    
fspindle added a commit to fspindle/visp that referenced this issue Oct 4, 2022
fspindle added a commit to fspindle/visp that referenced this issue Oct 4, 2022
@s-trinh
Copy link
Contributor

s-trinh commented Oct 5, 2022

Some tests for curiosity using Github actions.


  • OpenCV from brew:
General configuration for OpenCV 4.6.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /tmp/opencv-20220815-67092-1ytr5vd/opencv-4.6.0/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2022-06-05T15:32:44Z
    Host:                        Darwin 21.5.0 x86_64
    CMake:                       3.24.0
    CMake generator:             Unix Makefiles
    CMake build tool:            gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      requested:                 SSE4_2
      required:                  SSE4_2
    Dispatched code generation:  FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      avformat:                  YES (59.27.100)
      avutil:                    YES (57.28.100)
      swscale:                   YES (6.7.100)
      avresample:                NO
    AVFoundation:                YES

  Parallel framework:            TBB (ver 2021.5 interface 12050)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   /tmp/opencv-20220815-67092-1ytr5vd/opencv-4.6.0/build_shared/3rdparty/ippicv/ippicv_mac/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                /tmp/opencv-20220815-67092-1ytr5vd/opencv-4.6.0/build_shared/3rdparty/ippicv/ippicv_mac/iw
    Lapack:                      YES (/usr/local/opt/openblas/lib/libopenblas.dylib)
    Eigen:                       YES (ver 3.4.0)
    Custom HAL:                  NO
    Protobuf:                    /usr/local/lib/libprotobuf.dylib (3.21.5)

  OpenCL:                        YES (no extra features)
    Include path:                NO
    Link libraries:              -framework OpenCL

  Python 3:
    Interpreter:                 /usr/local/opt/python@3.10/bin/python3.10 (ver 3.10.6)
    Libraries:                   /usr/local/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib (ver 3.10.6)
    numpy:                       /usr/local/lib/python3.10/site-packages/numpy/core/include (ver 1.23.2)
    install path:                lib/python3.10/site-packages/cv2/python-3.10

  Python (for build):            /usr/local/opt/python@3.10/bin/python3.10

  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr/local/Cellar/opencv/4.6.0
-----------------------------------------------------------------
  • OpenCV 4.6.0 built on Github action on macOS-12:
General configuration for OpenCV 4.6.0 =====================================
--   Version control:               4.6.0
-- 
--   Platform:
--     Timestamp:                   2022-10-05T15:54:19Z
--     Host:                        Darwin 21.6.0 x86_64
--     CMake:                       3.24.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1
--       requested:                 DETECT
--     Dispatched code generation:  SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_2 (2 files):          + POPCNT SSE4_2
--       FP16 (1 files):            + POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + POPCNT SSE4_2 AVX
--       AVX2 (33 files):           + POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (8 files):      + POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /Applications/Xcode_14.0.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 14.0.0.14000029)
--     C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /Applications/Xcode_14.0.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,-dead_strip  
--     Linker flags (Debug):        -Wl,-dead_strip  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo python3 stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 python2
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI:                           COCOA
--     Cocoa:                       YES
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        zlib (ver 1.2.12)
--     JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         libpng (ver 1.6.37)
--     TIFF:                        libtiff (ver 42 / 4.2.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     OpenEXR:                     OpenEXR::OpenEXR (ver 3.1.5)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.6)
--     FFMPEG:                      YES
--       avcodec:                   YES (59.37.100)
--       avformat:                  YES (59.27.100)
--       avutil:                    YES (57.28.100)
--       swscale:                   YES (6.7.100)
--       avresample:                NO
--     GStreamer:                   NO
--     AVFoundation:                YES
-- 
--   Parallel framework:            GCD
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /Users/runner/opencv/build/3rdparty/ippicv/ippicv_mac/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /Users/runner/opencv/build/3rdparty/ippicv/ippicv_mac/iw
--     Lapack:                      YES (/Applications/Xcode_14.0.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl)
--     Eigen:                       YES (ver 3.4.0)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                NO
--     Link libraries:              -framework OpenCL
-- 
--   Python 3:
--     Interpreter:                 /usr/local/bin/python3 (ver 3.10.6)
--     Libraries:                   /usr/local/Frameworks/Python.framework/Versions/3.10/lib/libpython3.10.dylib (ver 3.10.6)
--     numpy:                       /usr/local/lib/python3.10/site-packages/numpy/core/include (ver 1.23.3)
--     install path:                lib/python3.10/site-packages/cv2/python-3.10
-- 
--   Python (for build):            /usr/local/bin/python2.7
-- 
--   Java:                          
--     ant:                         /usr/local/bin/ant (ver 1.10.12)
--     JNI:                         /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.345-1/x64/Contents/Home/include /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.345-1/x64/Contents/Home/include/darwin /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.345-1/x64/Contents/Home/include
--     Java wrappers:               YES
--     Java tests:                  YES
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------

The Illegal instruction: 4 error can come from a binary built with some CPU compiler flag and a CPU that does not support these instructions (e.g. too old).
With OpenCV there are :

  • baseline CPU features which means they must be supported by the CPU
  • dispatched CPU features which means at runtime there are some checks to see if the current CPU supports these instructions

Not all CPU features are made dispatched mainly to avoid too big binary size for the lib.


On Github actions, the macOS-12 is installed on:

Run sysctl -a | grep machdep.cpu
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH MMX FXSR SSE SSE2 SS HTT SSE3 PCLMULQDQ MON VMX SSSE3 CX16 SSE4.1 SSE4.2 x2APIC POPCNT AES VMM PCID XSAVE OSXSAVE TSCTMR AVX1.0 RDRAND F16C

With Homebrew:

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      requested:                 SSE4_2
      required:                  SSE4_2
    Dispatched code generation:  FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX

When built from source:

--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1
--       requested:                 DETECT
--     Dispatched code generation:  SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX

From my understanding requested: DETECT means that at runtime there are some CPU checks to see which of these dispatched instructions are supported by the current CPU.
For OpenCV from Homebrew, I think:

      requested:                 SSE4_2
      required:                  SSE4_2

means that at build time it has been requested to support as a baseline feature SSE4_2?

Anyway, this is still strange for me this bug since the dispatching mechanism should make that the correct code path is properly taken wrt the current CPU.
Else maybe OpenCV from Homebrew is badly configured?


Maybe we should warn the people at Homebrew of this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants