You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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(
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.
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'))
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.
(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?
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.
@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;
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.
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?
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.
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.
@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.
I also just noticed that during the installation process it says "x86 detected"
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
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
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 |
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
@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.
Activity
abetlen commentedon Mar 27, 2023
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.cppproject as a tCan 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 developEDIT: Additionally (maybe good to check first), please confirm that
llama.cppcan compile and run by running:DrBenjamin commentedon Mar 28, 2023
The install from source with
runs into this error:
llama.cpp did go through the compilation, but
throws this error:
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.
DrBenjamin commentedon Mar 28, 2023
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'))
abetlen commentedon Mar 28, 2023
My bad, I forgot that
vendor/llama.cppis a git submodule so it doesn't get cloned automatically (shouldn't be an issue with the PyPI version though). Runninggit submodule initandgit submodule updateafter cloning the repo should work.The error you're getting here is because
llama.cpprecently updated the binary format for the model weights. I believe dalai is using an older version ofllama.cppwhich 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.Finally, back to our original error
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_64before running the script?DrBenjamin commentedon Mar 28, 2023
arch -x86_64leads to the same error as in the beginning:DrBenjamin commentedon Mar 28, 2023
After converting the model and quantizing steps on the original weights
is running. Thanks for this so far!
pjlegato commentedon Apr 6, 2023
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=nativerather than manually specifying instruction set extensions.-march=nativeauto-selects the best extensions available on the current CPU.tl;dr remove all those
-mwhatevers in the Makefile. Add-march=nativewhere they were. Clean and rebuild, and it should work.pjlegato commentedon Apr 6, 2023
@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:
abetlen commentedon Apr 6, 2023
@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.
pjlegato commentedon Apr 6, 2023
I'm not sure, either. If I rebuild llama-cpp-python from source, I get a working llama.cpp
mainbinary invendor/llama.cpp.If I use text-generation-webui's
--cpuflag, 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?
abetlen commentedon Apr 6, 2023
I think I found the answer (or at least the root of the issue) here https://github.com/saharNooby/rwkv.cpp#linux--macos
pjlegato commentedon Apr 6, 2023
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-L219Could this be it?
pjlegato commentedon Apr 6, 2023
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_NATIVEisONand all the instruction set selectors at line 54 areOFF, that should fix it.abetlen commentedon Apr 6, 2023
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?
tilo-schroeder commentedon Apr 7, 2023
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 128runs 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 doingfrom 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.
abetlen commentedon Apr 7, 2023
@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 withpython3 setup.py developand let me know if that works.tilo-schroeder commentedon Apr 7, 2023
@abetlen I added the instruction so that the file now looks like this:
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.
tilo-schroeder commentedon Apr 7, 2023
I also just noticed that during the installation process it says "x86 detected"
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 :Dtgcandido commentedon Apr 8, 2023
adding the
set(CMAKE_SYSTEM_PROCESSOR "arm64")line i got the following errorTraceback (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 directorytgcandido commentedon Apr 8, 2023
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 |tgcandido commentedon Apr 8, 2023
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
abetlen commentedon Apr 8, 2023
@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.
tilo-schroeder commentedon Apr 8, 2023
@abetlen I can import now without a problem. Thank you so much!
abetlen commentedon Apr 8, 2023
Fantastic @tilo-schroeder appreciate the help!
ROCm Port (abetlen#1087)
wheels.yml: switch to pytorch/manylinux-builder:cuda12.8 + macOS x86_…