Skip to content

Error running on M1 Mac #5

Closed
Closed

Description

@DrBenjamin

Hi!

I am having issues with using it on a M1 Mac:

from llama_cpp import Llama
produces this error:

zsh: illegal hardware instruction

Best,
Benjamin

Activity

  1. added
    bug
    on Mar 27, 2023
  2. abetlen commented on Mar 27, 2023

    @abetlen
    Owner

    Do you mind posting the complete error message? I'm assuming this occured when importing the package. I'm currently using cmake to specify the llama.cpp project as a t

    Can you try installing the package from source? Just run the following commands in a terminal

    git clone https://github.com/abetlen/llama-cpp-python.git
    cd llama-cpp-python
    python3 setup.py develop

    EDIT: Additionally (maybe good to check first), please confirm that llama.cpp can compile and run by running:

    git clone https://github.com/ggerganov/llama.cpp
    cd llama.cpp
    make
    ./main -m ./models/7B/ggml-model-q4_0.bin -n 128
    
  3. DrBenjamin commented on Mar 28, 2023

    @DrBenjamin
    Author

    The install from source with

    python3 setup.py develop
    

    runs into this error:

    CMake Error at CMakeLists.txt:7 (add_subdirectory):
      The source directory
    
        /Users/ben/Downloads/llama-cpp-python/vendor/llama.cpp
    
      does not contain a CMakeLists.txt file.
    
    
    CMake Error at CMakeLists.txt:9 (install):
      install TARGETS given target "llama" which does not exist.
    
    
    -- Configuring incomplete, errors occurred!
    Traceback (most recent call last):
      File "/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/skbuild/setuptools_wrap.py", line 634, in setup
        env = cmkr.configure(
      File "/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/skbuild/cmaker.py", line 332, in configure
        raise SKBuildError(
    

    llama.cpp did go through the compilation, but

    ./main -m models/models/7B/ggml-model-q4_0.bin -n 128
    

    throws this error:

    main: seed = 1679966146
    llama_model_load: loading model from 'models/models/7B/ggml-model-q4_0.bin' - please wait ...
    llama_model_load: invalid model file 'models/models/7B/ggml-model-q4_0.bin' (too old, regenerate your model files!)
    llama_init_from_file: failed to load model
    main: error: failed to load model 'models/models/7B/ggml-model-q4_0.bin'
    

    That is weird as dalai (which downloaded the alpaca 7B model) runs without any problems with this model. Also this script works fine https://github.com/shaunabanana/llama.py with the model.

  4. DrBenjamin commented on Mar 28, 2023

    @DrBenjamin
    Author

    Ok, I learned that I need to put the llama.cpp files into .vendor/llama.cpp. Then it worked. Giving me this error:

    OSError: dlopen(/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/llama_cpp/libllama.dylib, 0x0006): tried: '/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/llama_cpp/libllama.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')),

    '/System/Volumes/Preboot/Cryptexes/OS/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/llama_cpp/libllama.dylib' (no such file), '/Users/ben/opt/miniconda3.10/miniconda3/lib/python3.10/site-packages/llama_cpp/libllama.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))

  5. abetlen commented on Mar 28, 2023

    @abetlen
    Owner
    CMake Error at CMakeLists.txt:7 (add_subdirectory):
      The source directory
    
        /Users/ben/Downloads/llama-cpp-python/vendor/llama.cpp
    
      does not contain a CMakeLists.txt file.
    

    My bad, I forgot that vendor/llama.cpp is a git submodule so it doesn't get cloned automatically (shouldn't be an issue with the PyPI version though). Running git submodule init and git submodule update after cloning the repo should work.

    main: seed = 1679966146
    llama_model_load: loading model from 'models/models/7B/ggml-model-q4_0.bin' - please wait ...
    llama_model_load: invalid model file 'models/models/7B/ggml-model-q4_0.bin' (too old, regenerate your model files!)
    llama_init_from_file: failed to load model
    main: error: failed to load model 'models/models/7B/ggml-model-q4_0.bin'
    

    The error you're getting here is because llama.cpp recently updated the binary format for the model weights. I believe dalai is using an older version of llama.cpp which is why it's not giving you an error. You'll need to run the convert model and quantize steps on the original weights documented here.

    python3 convert-pth-to-ggml.py models/7B/ 1
    python3 quantize.py 7B

    Finally, back to our original error

    (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')),
    

    I've actually tested the install (through pip not conda) on an M1 Macbook Air and it all worked, this seems to be an issue with the Mac M1s x86_64 compatibility mode. I think it has to do with installing it through conda but I'm not sure. I'll try conda installing the repo tomorrow and see what happens.

    In the meantime can you try running arch -x86_64 before running the script?

  6. DrBenjamin commented on Mar 28, 2023

    @DrBenjamin
    Author

    arch -x86_64 leads to the same error as in the beginning:

    arch -x86_64 python Test.py  
    zsh: illegal hardware instruction  arch -x86_64 python Test.py
    
  7. DrBenjamin commented on Mar 28, 2023

    @DrBenjamin
    Author

    After converting the model and quantizing steps on the original weights

    ./main -m models/models/7B/ggml-model-q4_0.bin -n 128
    

    is running. Thanks for this so far!

  8. pjlegato commented on Apr 6, 2023

    @pjlegato

    The "illegal instruction" errors usually mean that the binary was compiled with an extension instruction set specified which is not supported by the current CPU at runtime. Look for a line in the makefile with flags similar to -msse3. These -m instructions toggle on various instruction set extensions (see https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html for details.)

    Unless there's a specific reason to choose the extensions manually (like cross-compiling for some other CPU), it's usually better to specify -march=native rather than manually specifying instruction set extensions. -march=native auto-selects the best extensions available on the current CPU.

    tl;dr remove all those -mwhatevers in the Makefile. Add -march=native where they were. Clean and rebuild, and it should work.

  9. pjlegato commented on Apr 6, 2023

    @pjlegato

    @abetlen Expanding on the above, if a user installs a precompiled llama.cpp via pip install llama-cpp-python, they get a binary that often has been compiled on a different CPU than the one they are running on. Since llama.cpp specifies -march=native, it selects instruction set extensions for the compile-time CPU. If the end user has a different CPU with different extensions available, they will get the "illegal instruction" errors above.

    The options are:

    • Disable extensions altogether, get a slower llama.cpp, but which runs everywhere;
    • Recompile llama.cpp upon each installation (somehow) for that user's specific CPU, using whatever extensions are supported there;
  10. abetlen commented on Apr 6, 2023

    @abetlen
    Owner

    @pjlegato thank you for the info!

    wrt the PyPI package I'm actually not distributing binaries for this reason, each shared library should be built on installation which is why this issue is really confusing me.

    Currently the package is using scikit-build and building llama.cpp as a shared library via cmake. Some people have pointed out that using make may be better on some systems but I wasn't aware of any bugs using one vs. the other, just assumed some targetted certain optimizations better.

  11. pjlegato commented on Apr 6, 2023

    @pjlegato

    I'm not sure, either. If I rebuild llama-cpp-python from source, I get a working llama.cpp main binary in vendor/llama.cpp.

    If I use text-generation-webui's --cpu flag, which delegates to an auto-installed llama-cpp-python==0.1.23 in its requirements.txt, I get "illegal instruction" errors.

    Maybe some package distributor upstream is somehow pre-buiilding the package and not rebuilding it locally each time?

  12. abetlen commented on Apr 6, 2023

    @abetlen
    Owner

    I think I found the answer (or at least the root of the issue) here https://github.com/saharNooby/rwkv.cpp#linux--macos

  13. pjlegato commented on Apr 6, 2023

    @pjlegato

    Wild guess: I've always compiled llama.cpp on Mac and Linux with plain make (no C), which seems to work.

    llama-cpp-python probably compiles llama.cpp by using CMake rather than plain make.

    It appears that upstream llama.cpp's CMakeLists.txt tries to be clever and select instruction set extensions manually, rather than just using -march=native: https://github.com/ggerganov/llama.cpp/blob/master/CMakeLists.txt#L193-L219

    Could this be it?

  14. pjlegato commented on Apr 6, 2023

    @pjlegato

    I am not familiar with CMake. If there's some way you can override llama.cpp's CMakeLists.txt at build time so that LLAMA_NATIVE is ON and all the instruction set selectors at line 54 are OFF, that should fix it.

  15. abetlen commented on Apr 6, 2023

    @abetlen
    Owner

    Hmm, I may then just move to using make for Mac and Linux (skbuild should support this) and have Windows install via cmake.

    @pjlegato just want to confirm, when you've compiled llama.cpp with just make, you haven't had to set any special environment variables?

  16. tilo-schroeder commented on Apr 7, 2023

    @tilo-schroeder

    Hey,
    Sorry to chime in there. I have the same problem and tried to resolve the issue with the comments in this thread. However, I am a bit lost about what the solution is. I cloned the repo and installed from source with the setup.py. I also built and quantized llama.cpp in the vendor folder so that ./main -m ./models/7B/ggml-model-q4_0.bin -n 128 runs fine in there. With all this in place and making sure that my conda environment uses the llama-cpp-python installed from source, I still get the illegal instruction error when doing from llama_cpp import Llama.

    Is there something I am missing / doing wrong? I would be very glad if someone could clear this up a bit - I tried so many different ways that I am confusing myself at this point.

  17. abetlen commented on Apr 7, 2023

    @abetlen
    Owner

    @tilo-schroeder I'm working on a potential fix, would appreciate if you could test something out for me, can you add set(CMAKE_SYSTEM_PROCESSOR "arm64") to the top of the CMakeLists.txt at the root of this repo and try to re-install with python3 setup.py develop and let me know if that works.

  18. tilo-schroeder commented on Apr 7, 2023

    @tilo-schroeder

    @abetlen I added the instruction so that the file now looks like this:

    set(CMAKE_SYSTEM_PROCESSOR "arm64")
    cmake_minimum_required(VERSION 3.4...3.22)
    
    project(llama_cpp)
    
    set(BUILD_SHARED_LIBS "On")
    
    add_subdirectory(vendor/llama.cpp)
    
    install(TARGETS llama LIBRARY DESTINATION llama_cpp)
    

    After reinstalling I still get the illegal instruction when I try to import. If that is all that has to be changed, I am afraid that is not the fix.

  19. tilo-schroeder commented on Apr 7, 2023

    @tilo-schroeder

    I also just noticed that during the installation process it says "x86 detected"

    image

    This happened in my test run with the set(CMAKE_SYSTEM_PROCESSOR "arm64") in place which seems weird to me... but to be fair my knowledge on these build tools is very limited :D

  20. tgcandido commented on Apr 8, 2023

    @tgcandido

    adding the set(CMAKE_SYSTEM_PROCESSOR "arm64") line i got the following error

    Traceback (most recent call last): File "/Users/tgcandido/Documents/code/llang-test/textbook.live.py", line 113, in <module> vectorstore = Chroma.from_documents(documents, embeddings) File "/opt/anaconda3/lib/python3.9/site-packages/langchain/vectorstores/chroma.py", line 334, in from_documents return cls.from_texts( File "/opt/anaconda3/lib/python3.9/site-packages/langchain/vectorstores/chroma.py", line 303, in from_texts chroma_collection.add_texts(texts=texts, metadatas=metadatas, ids=ids) File "/opt/anaconda3/lib/python3.9/site-packages/langchain/vectorstores/chroma.py", line 113, in add_texts embeddings = self._embedding_function.embed_documents(list(texts)) File "/opt/anaconda3/lib/python3.9/site-packages/langchain/embeddings/llamacpp.py", line 111, in embed_documents embeddings = [self.client.embed(text) for text in texts] File "/opt/anaconda3/lib/python3.9/site-packages/langchain/embeddings/llamacpp.py", line 111, in <listcomp> embeddings = [self.client.embed(text) for text in texts] File "/opt/anaconda3/lib/python3.9/site-packages/llama_cpp_python-0.1.25-py3.9-macosx-10.16-x86_64.egg/llama_cpp/llama.py", line 289, in embed return list(map(float, self.create_embedding(input)["data"][0]["embedding"])) File "/opt/anaconda3/lib/python3.9/site-packages/llama_cpp_python-0.1.25-py3.9-macosx-10.16-x86_64.egg/llama_cpp/llama.py", line 243, in create_embedding assert self.ctx is not None AssertionError Exiting: Cleaning up .chroma directory

  21. tgcandido commented on Apr 8, 2023

    @tgcandido

    this also pops up before the error

    llama_model_load: loading model from '/Users/tgcandido/dalai/llama/models/7B/ggml-model-q4_0.bin' - please wait ... /Users/tgcandido/dalai/llama/models/7B/ggml-model-q4_0.bin: invalid model file (bad magic [got 0x67676d66 want 0x67676a74]) you most likely need to regenerate your ggml files the benefit is you'll get 10-100x faster load times see https://github.com/ggerganov/llama.cpp/issues/91 use convert-pth-to-ggml.py to regenerate from original pth use migrate-ggml-2023-03-30-pr613.py if you deleted originals llama_init_from_file: failed to load model AVX = 0 | AVX2 = 0 | AVX512 = 0 | FMA = 0 | NEON = 0 | ARM_FMA = 0 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | VSX = 0 | llama_model_load: loading model from '/Users/tgcandido/dalai/llama/models/7B/ggml-model-q4_0.bin' - please wait ... /Users/tgcandido/dalai/llama/models/7B/ggml-model-q4_0.bin: invalid model file (bad magic [got 0x67676d66 want 0x67676a74]) you most likely need to regenerate your ggml files the benefit is you'll get 10-100x faster load times see https://github.com/ggerganov/llama.cpp/issues/91 use convert-pth-to-ggml.py to regenerate from original pth use migrate-ggml-2023-03-30-pr613.py if you deleted originals llama_init_from_file: failed to load model AVX = 0 | AVX2 = 0 | AVX512 = 0 | FMA = 0 | NEON = 0 | ARM_FMA = 0 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | VSX = 0 |

  22. tgcandido commented on Apr 8, 2023

    @tgcandido

    model works fine running via command line and with the dalai package... got the same error using conda and pip and installing this package from pip or building with cmake set to arm64

  23. abetlen commented on Apr 8, 2023

    @abetlen
    Owner

    @tilo-schroeder @tgcandido I've pushed a potential fix to this repo that builds the package using make on linux and macos, would one of you mind testing it out to confirm it works? You should be able to test it by just following the development instructions in the README.

  24. tilo-schroeder commented on Apr 8, 2023

    @tilo-schroeder

    @abetlen I can import now without a problem. Thank you so much!

  25. abetlen commented on Apr 8, 2023

    @abetlen
    Owner

    Fantastic @tilo-schroeder appreciate the help!

  26. added a commit that references this issue on Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions