-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
illegal instructions error on Android #402
Comments
Please review and use our issue template to provide more details so we can try and better understand your problem and attempt to answer you. |
the issue happens when you don't have enough RAM |
your a52 might just not allow termux to allocate that much ram. weird given you have 6GB |
@dalnk @himanshu09010 that is sad since i heard people are running it on a raspberry pi with 4gb of ram :( |
I believe I have encountered the same problem. I have tried to compile the binary directly on my phone (Samsung S22 Ultra with Snapdragon CPU, model SM-S908E). I followed these steps in Termux:
This fails with Running The |
Did you compile with the NDK instructions first? @aicoat on raspberrypi it's possible to enable swap which afaik I haven't gotten working on Android yet. If I ever get my hands on an a52 or similar I'll try to recreate your issue. |
@dalnk the "illegal instruction" error arises when the source is compiled on the device itself. I'm not sure the NDK is relevant in this case. ...however, building the binary on Linux with NDK doesn't work either due to a different error: #495 (comment) |
I just compiled it directly on a Xiaomi Mi 11T Pro (Snapdragon 888), and it's woking using the GPT4All model. Maybe the last commits related to ARM_NEON fixed the issue indirectly? |
Just tried recompiling the project for the latest commit ( |
I'm curious if this works for you when compiling locally @dniku last time I tried it, I had success without having to compile it on NDK |
@dalnk compiling alpaca.cpp directly on Android produces the exact same error caused by the same instruction. |
it looks like a snapdragon issue. I have 2 phones, one with sd865(6gb ram) and other one with sd8 gen2(12gb ram). |
@GH228 I agree — according to this website, my CPU is |
Are you also getting illegal instruction error when you run it? |
@GH228
Yes. Running |
I have found a solution. It might not be the best one, but it seems to work. First, apply the following patch: diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1a434f0..bd98b97 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -186,7 +186,7 @@ if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES
# TODO: arm msvc?
else()
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
- add_compile_options(-mcpu=native)
+ add_compile_options(-mcpu=generic)
endif()
# TODO: armv6,7,8 version specific flags
endif() Next, mkdir build && \
cd build && \
cmake .. && \
make main && \
mv bin/main .. && \
cd .. && \
rm -r build After this, Funny enough, none of By the way, this solution was suggested by ChatGPT: |
How to apply this "patch"? is this a script file or something. How can i run it? |
@GH228 the easiest way is to edit |
Yes, i have found a way to patch using your script. It seems that it works only with llama.cpp, not alpaca.cpp what is the difference between arm v7 and arm v8? I just asked the same model, but running on my pc the same question, the respond is:
i just compiled alpaca.cpp, replacing all mcpu flags, yeah, it works. I'm pretty sure it is model issue. But i didnt find any other models that would run with llama.cpp, besides that. So, if you know how to quantize to 4 bit from source, free to go. I would recommend to use alpaca.cpp |
The CMakeLists.txt seems to select CPU instruction set extensions manually (https://github.com/ggerganov/llama.cpp/blob/master/CMakeLists.txt#L54-L58 and https://github.com/ggerganov/llama.cpp/blob/master/CMakeLists.txt#L193-L219). This will cause the output binary to throw "illegal instruction" errors when run on a CPU that does not support the selected instruction sets. Unless deliberately cross-compiling to generate a binary that will run on some other CPU (which is rare), the extension instruction sets should not be specified manually. Setting Even if you are cross-compiling, it's generally better to do More info at https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html |
That's theoretically true, but this whole thread is dedicated to the fact that when compiling the project on the Android device itself |
Oh my god, this proved to be a deep rabbit hole. I was able to reproduce this with a First off, the problematic instructions on your gdb screenshots ( A much more interesting question is why this happens in the first place. As far as I understand, clang uses this function to parse the first 32 lines of My initial guess was that only some of the cores implemented SVE, but it turned out that all 3 Cortex models are ARMv9, where both SVE and its extension SVE2 are mandatory. Every description of Snapdragon 8 I can find on the Internet proudly declares that it is ARMv9-A and supports SVE(2). However, there are no indications of that in the #include <stdio.h>
#include <inttypes.h>
int main() {
uint64_t features = 0;
asm("mrs %0, ID_AA64PFR0_EL1" : "=r"(features));
printf("features: %lu\n", (features >> 32) & 0x1F);
} It also prints 0 every time I run it. I don't believe it is possible for an OS to influence reads from this register, so it appears to be a genuine issue with the CPU. This comment also seems to confirm this. So, to summarize: Qualcomm cores pretend to be ARMv9 cores while not being compliant ARMv9 cores, and clang believes them. |
Did you manage to resolve you problem? A52 seems to use an older Kryo core based on Cortex-A76, so the problem you were seeing might have been unrelated to the SVE one that @dniku and @GH228 stumbled upon. If you still are seeing |
I had the same problem compiling the code in my phone, a Xiaomi Mi 9 Se (Snapdragon 712 with 6Gb of Ram). Your edit to CMakeLists.txt fixed the issue. |
-mcpu=generic worked for me |
|
…terial-9.1.16 Bump mkdocs-material from 9.1.15 to 9.1.16
@aicoat How did you compile the source code on Android? Would you mind sharing the commands you used? |
LostRuins fixed it by removing the mcpu native: ifneq ($(filter aarch64%,$(UNAME_M)),)
# Apple M1, M2, etc.
# Raspberry Pi 3, 4, Zero 2 (64-bit)
# Nvidia Jetson
MK_CFLAGS +=
MK_CXXFLAGS += |
New tag b4358 feature detection os not working properly on Snapdragon 8 gen 1. |
first thanks for the wonderful works so far !!!
i manged to compile it in Linux and windows but i have a problem with android.
i have A52 6 GB but i get "illegal instructions" error.
i compiled the source using wsl2 with ndk r25 without any errors. i moved the llama folder from sd card to "home" directory in (Termux) in order to have the execute command working. and i converted to original model using the newer source code to avoid "too old" error message but at the end i get this error.
i believe it is because of having avx, avx2 and other instruction already enabled in my build which is arm processors cant handle them but i cant figure it out how to change it to get it working on my android device.

thanks in advanced <3
The text was updated successfully, but these errors were encountered: