Skip to content

[BUG]: Linux-arm64 crashes with a TypeInitializationException #1192

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

Open
BartjeD opened this issue May 18, 2025 · 22 comments
Open

[BUG]: Linux-arm64 crashes with a TypeInitializationException #1192

BartjeD opened this issue May 18, 2025 · 22 comments

Comments

@BartjeD
Copy link

BartjeD commented May 18, 2025

Description

When attempting to run a LLamasharp API on linux-arm64, compiled als a self-contained runtime: I get a runtime error for type initialization, which says the CPU library is missing.

But I'm very sure it's installed. And of course the API works locally on Windows 11, without using a GPU.

Exception:
System.TypeInitializationException: The type initializer for 'LLama.Native.NativeApi' threw an exception. ---> LLama.Exceptions.RuntimeError: The native library cannot be correctly loaded. It could be one of the following reasons: 1. No LLamaSharp backend was installed. Please search LLamaSharp.Backend and install one of them. 2. You are using a device with only CPU but installed cuda backend. Please install cpu backend instead. 3. One of the dependency of the native library is missed

I checked the release build files, for the given API.
To be sure that the cpu lib ggml library is present, which it seems to be:
Image

Is ARM-64 supported? Do I need to install any libraries on the hosting environment?

Reproduction Steps

Run CPU only instance on linux-arm64, it will fail with LLama runtime error: `System.TypeInitializationException.... see above.

Environment & Configuration

  • Operating system: Debian 12
  • .NET runtime version: .net 9, 9.0.101
  • LLamaSharp version: 0.24.0
  • CUDA version (if you are using cuda backend): n/a
  • CPU & GPU device: arm64 Ampere Altra

Known Workarounds

N/a

@BartjeD BartjeD changed the title [BUG]: [BUG]: Linux-arm64 crashes with a TypeInitializationException May 18, 2025
@BartjeD
Copy link
Author

BartjeD commented May 18, 2025

I'm assuming it's supported, since these changes were comitted: #1137 and: #1138

@martindevans
Copy link
Member

Arm64 should be supported, althougn it's qutie new and not tested in CI yet so I expect some bugs there!

Does it work if you run it from VS (or with dotnet run) instead of an actual build? I ask because there's been a known issue for a while with self-contained builds placing the native runtime files in the wrong directory (it flattens the folder hierarchy for some reason) - that moans the loading system can't find the DLLs it's expecting.

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Should I put the files 's in the same folder they end up in, in the debug build?
I can give that a try on the server

/runtimes/linux-arm64/native/

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Still gives the same exception:

Image

@martindevans
Copy link
Member

That looks like the right path. If you add this to the start of your program (before any other LLamaSharp calls) you can get verbose logging info which should show the exact paths it's looking for:

NativeLibraryConfig
   .All
   .WithLogCallback((level, message) => Console.WriteLine($"LL# [{level}]: {message}");

@sangyuxiaowu
Copy link
Contributor

Aha, I have experience with this. When using the runtimes path, I need to be on the Windows platform. My Jetson Agx arm64 device requires me to compile the CUDA version myself and copy the SO file to the same directory as the program.
You can try compiling llama.cpp yourself on your arm64 device.

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Oke 1 step further now:

The file is found, but cannot be loaded

Image

@martindevans
Copy link
Member

Could you try running ldd on the file (linux-arm64/native/libllama.so), that'll tell us if there's some dependency missing.

@sangyuxiaowu
Copy link
Contributor

I seem to have found the problem. Need to upgrade glibc.

My device can run normally as I compiled it myself, but there are indeed issues with the Nuget service, which can be used as a reference:

My compiled Cuda

agx@ubuntu:/work/gpt$ ldd libllama.so
        linux-vdso.so.1 (0x0000ffffb7886000)
        libggml.so (0x0000ffffb76b0000)
        libggml-base.so (0x0000ffffb75d0000)
        libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000ffffb7390000)
        libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000ffffb72f0000)
        libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000ffffb72c0000)
        libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffffb7110000)
        /lib/ld-linux-aarch64.so.1 (0x0000ffffb784d000)
        libggml-cpu.so (0x0000ffffb7040000)
        libggml-cuda.so (0x0000ffffb3fa0000)
        libgomp.so.1 => /lib/aarch64-linux-gnu/libgomp.so.1 (0x0000ffffb3f40000)
        libcudart.so.12 => /usr/local/cuda/lib64/libcudart.so.12 (0x0000ffffb3e70000)
        libcublas.so.12 => /usr/local/cuda/lib64/libcublas.so.12 (0x0000ffffac9a0000)
        libcuda.so.1 => /usr/local/cuda/compat/libcuda.so.1 (0x0000ffffaad00000)
        libdl.so.2 => /lib/aarch64-linux-gnu/libdl.so.2 (0x0000ffffaace0000)
        libpthread.so.0 => /lib/aarch64-linux-gnu/libpthread.so.0 (0x0000ffffaacc0000)
        librt.so.1 => /lib/aarch64-linux-gnu/librt.so.1 (0x0000ffffaaca0000)
        libcublasLt.so.12 => /usr/local/cuda/lib64/libcublasLt.so.12 (0x0000ffff8c530000)
        libnvrm_gpu.so => /usr/lib/aarch64-linux-gnu/tegra/libnvrm_gpu.so (0x0000ffff8c4c0000)
        libnvrm_mem.so => /usr/lib/aarch64-linux-gnu/tegra/libnvrm_mem.so (0x0000ffff8c4a0000)
        libnvos.so => /usr/lib/aarch64-linux-gnu/tegra/libnvos.so (0x0000ffff8c470000)
        libnvsocsys.so => /usr/lib/aarch64-linux-gnu/tegra/libnvsocsys.so (0x0000ffff8c450000)
        libnvrm_sync.so => /usr/lib/aarch64-linux-gnu/tegra/libnvrm_sync.so (0x0000ffff8c430000)
        libnvsciipc.so => /usr/lib/aarch64-linux-gnu/tegra/libnvsciipc.so (0x0000ffff8c400000)
        libnvrm_chip.so => /usr/lib/aarch64-linux-gnu/tegra/libnvrm_chip.so (0x0000ffff8c3e0000)
        libnvrm_host1x.so => /usr/lib/aarch64-linux-gnu/tegra/libnvrm_host1x.so (0x0000ffff8c3b0000)

Nuget

agx@ubuntu:/work/gpt/so$ ldd libllama.so
./libllama.so: /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.38' not found (required by ./libllama.so)
./libllama.so: /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.38' not found (required by libggml-base.so)
        linux-vdso.so.1 (0x0000ffff8d1f6000)
        libggml.so (0x0000ffff8d000000)
        libggml-base.so (0x0000ffff8cf20000)
        libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000ffff8cce0000)
        libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000ffff8cc40000)
        libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000ffff8cc10000)
        libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffff8ca60000)
        /lib/ld-linux-aarch64.so.1 (0x0000ffff8d1bd000)
        libggml-cpu.so (0x0000ffff8c980000)
        libgomp.so.1 => /lib/aarch64-linux-gnu/libgomp.so.1 (0x0000ffff8c920000)

@sangyuxiaowu
Copy link
Contributor

This situation is indeed quite common in embedded systems, as embedded devices typically run relatively older, stable versions of operating systems. So, it is recommended to compile it yourself. Upgrading glibc may pose greater risks.

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Yes:
version `GLIBC_2.38' not found (required by libggml-base.so)
linux-vdso.so.1 (0x0000ffff9a390000)
libggml.so (0x0000ffff9a190000)
libggml-base.so (0x0000ffff9a0b0000)
libstdc++.so.6 => /lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000ffff99e90000)
libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000ffff99df0000)
libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000ffff99db0000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000ffff99c00000)
/lib/ld-linux-aarch64.so.1 (0x0000ffff9a353000)
libggml-cpu.so (0x0000ffff99b20000)
libgomp.so.1 => not found

@sangyuxiaowu
Copy link
Contributor

If you don't want to compile, you can also try the CUDA version I compiled first. It should return to the CPU by itself, maybe it can be used.
Try using the SO file in the compressed file.
LLamaWorker-cuda12.2-linux-arm64-b5415.zip

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

If I switch to Ubuntu 24.04 LTS it will have GLIBC_2.39, which I think will be OK, we'll find out

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Ok, now on Ubuntu 24.04,

it says libgomp.so.1 => not found
The glibc error is resolved.

The exception is still the same: System.TypeInitializationException: The type initializer for 'LLama.Native.NativeApi' threw an exception. ---> LLama.Exceptions.RuntimeError: The native library cannot be correctly loaded

Image

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

So now I'm installing libgomp1, let's see what that does...

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Okay, all dependencies are loaded into adress space,

Image

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

Now it's a hard crash of the API

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

I can't find any other logs, I suppose it's all in the dump

Image

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

If you don't want to compile, you can also try the CUDA version I compiled first. It should return to the CPU by itself, maybe it can be used. Try using the SO file in the compressed file. LLamaWorker-cuda12.2-linux-arm64-b5415.zip

These also gave a dump

@martindevans
Copy link
Member

When you compiled llama.cpp did you use exactly the commit hash listed here?

@BartjeD
Copy link
Author

BartjeD commented May 22, 2025

I did not do a compile myself, I did a release build via .NET CLI.
Do I need to compile the binaries myself?

This is the core dump:

Module libgomp.so.1 from deb gcc-14-14.2.0-4ubuntu2~24.04.arm64
                Module libgcc_s.so.1 from deb gcc-14-14.2.0-4ubuntu2~24.04.arm64
                Module libstdc++.so.6 from deb gcc-14-14.2.0-4ubuntu2~24.04.arm64
                Stack trace of thread 9735:
                #0  0x0000ea53d66ce720 n/a (/root/opt/hosting/btec/llm-api/libllama.so + 0x5e720)
                #1  0x0000ea94b5624cf4 n/a (n/a + 0x0)
                #2  0x0000ea94b5624c34 n/a (n/a + 0x0)
                #3  0x0000ea94b5624bc4 n/a (n/a + 0x0)
                #4  0x0000ea94b5624a00 n/a (n/a + 0x0)
                #5  0x0000ea94b5624834 n/a (n/a + 0x0)
                #6  0x0000ea94b55e8c80 n/a (n/a + 0x0)
                #7  0x0000ea94ef9d9d48 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x4a9d48)
                #8  0x0000ea94ef83ff24 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x30ff24)
                #9  0x0000ea94ef8dd718 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x3ad718)
                #10 0x0000ea94af7d3654 n/a (n/a + 0x0)
                #11 0x0000ea94af7d3314 n/a (n/a + 0x0)
                #12 0x0000ea94af7de744 n/a (n/a + 0x0)
                #13 0x0000ea94b05a6dc4 n/a (n/a + 0x0)
                #14 0x0000ea94b05a340c n/a (n/a + 0x0)
                #15 0x0000ea94b05ab658 n/a (n/a + 0x0)
                #16 0x0000ea94b05a2eb8 n/a (n/a + 0x0)
                #17 0x0000ea94b05a2b38 n/a (n/a + 0x0)
                #18 0x0000ea94b05b2068 n/a (n/a + 0x0)
                #19 0x0000ea94b059f37c n/a (n/a + 0x0)
                #20 0x0000ea94b05b1ed0 n/a (n/a + 0x0)
                #21 0x0000ea94b05a3e98 n/a (n/a + 0x0)
                #22 0x0000ea94b1e75978 n/a (n/a + 0x0)
                #23 0x0000ea94b1eb9e00 n/a (n/a + 0x0)
                #24 0x0000ea94b55dd614 n/a (n/a + 0x0)
                #25 0x0000ea94b55dd558 n/a (n/a + 0x0)
                #26 0x0000ea94b1e60a00 n/a (n/a + 0x0)
                #27 0x0000ea94b1ed5d00 n/a (n/a + 0x0)
                #28 0x0000ea94b55dc0fc n/a (n/a + 0x0)
                #29 0x0000ea94b55dc010 n/a (n/a + 0x0)
                #30 0x0000ea94b1ecfe08 n/a (n/a + 0x0)
                #31 0x0000ea94b1e817a4 n/a (n/a + 0x0)
                #32 0x0000ea94b1e823ac n/a (n/a + 0x0)
                #33 0x0000ea94b1e8a3dc n/a (n/a + 0x0)
                #34 0x0000ea94b1e89e3c n/a (n/a + 0x0)
                #35 0x0000ea94b1e89bd8 n/a (n/a + 0x0)
                #36 0x0000ea94b1eb51b4 n/a (n/a + 0x0)
                #37 0x0000ea94b368c698 n/a (n/a + 0x0)
                #38 0x0000ea94b437e274 n/a (n/a + 0x0)
                #39 0x0000ea94b437e1c0 n/a (n/a + 0x0)
                #40 0x0000ea94b3688080 n/a (n/a + 0x0)
                #41 0x0000ea94b43779c0 n/a (n/a + 0x0)
                #42 0x0000ea94b33dd298 n/a (n/a + 0x0)
                #43 0x0000ea94b36224e4 n/a (n/a + 0x0)
                #44 0x0000ea94b33dd298 n/a (n/a + 0x0)
                #45 0x0000ea94b4376e74 n/a (n/a + 0x0)
                #46 0x0000ea94b4376618 n/a (n/a + 0x0)
                #47 0x0000ea94b4376570 n/a (n/a + 0x0)
                #48 0x0000ea94b437650c n/a (n/a + 0x0)
                #49 0x0000ea94b43755f8 n/a (n/a + 0x0)
                #50 0x0000ea94b4375414 n/a (n/a + 0x0)
                #51 0x0000ea94b4375370 n/a (n/a + 0x0)
                #52 0x0000ea94b4375310 n/a (n/a + 0x0)
                #53 0x0000ea94b3621710 n/a (n/a + 0x0)
                #54 0x0000ea94b3552918 n/a (n/a + 0x0)
                #55 0x0000ea94b4366fb8 n/a (n/a + 0x0)
                #56 0x0000ea94b4366ddc n/a (n/a + 0x0)
                #57 0x0000ea94b4366d28 n/a (n/a + 0x0)
                #58 0x0000ea94b4366cc8 n/a (n/a + 0x0)
                #59 0x0000ea94b4363404 n/a (n/a + 0x0)
                #60 0x0000ea94b4362668 n/a (n/a + 0x0)
                #61 0x0000ea94b43625b8 n/a (n/a + 0x0)
                #62 0x0000ea94b4362558 n/a (n/a + 0x0)
                #63 0x0000ea94b436235c n/a (n/a + 0x0)

                Stack trace of thread 9454:
                #0  0x0000ea94efe11e9c n/a (libc.so.6 + 0x81e9c)
                #1  0x0000ea94efe14b20 pthread_cond_wait (libc.so.6 + 0x84b20)
                #2  0x0000ea94efb5f9e0 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x62f9e0)
                #3  0x0000ea94efb5f9e0 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x62f9e0)
                #4  0x0000ea94efb5f734 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x62f734)
                #5  0x0000ea94efb63ecc n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x633ecc)
                #6  0x0000ea94ef80d53c n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x2dd53c)
                #7  0x0000ea94ef80824c n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x2d824c)
                #8  0x0000ea94ef80e47c n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x2de47c)
                #9  0x0000ea94ef806804 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x2d6804)
                #10 0x0000ea94efaef5a0 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x5bf5a0)
                #11 0x0000ea94af721bdc n/a (n/a + 0x0)
                #12 0x0000ea94af735a34 n/a (n/a + 0x0)
                #13 0x0000ea94af754fb4 n/a (n/a + 0x0)
                #14 0x0000ea94af754c38 n/a (n/a + 0x0)
                #15 0x0000ea94af7c6b54 n/a (n/a + 0x0)
                #16 0x0000ea94b05d1038 n/a (n/a + 0x0)
                #17 0x0000ea94b05620b8 n/a (n/a + 0x0)
                #18 0x0000ea94b0561e34 n/a (n/a + 0x0)
                #19 0x0000ea94b0561d90 n/a (n/a + 0x0)
                #20 0x0000ea94b0561d2c n/a (n/a + 0x0)
                #21 0x0000ea94b0561c78 n/a (n/a + 0x0)
                #22 0x0000ea94ef9d9d48 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x4a9d48)
                #23 0x0000ea94ef8405bc n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x3105bc)
                #24 0x0000ea94ef736ff4 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x206ff4)
                #25 0x0000ea94ef737364 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x207364)
                #26 0x0000ea94ef75db94 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x22db94)
                #27 0x0000ea94ef723e18 n/a (/root/opt/hosting/btec/llm-api/libcoreclr.so + 0x1f3e18)
                #28 0x0000ea94efccae3c n/a (/root/opt/hosting/btec/llm-api/libhostpolicy.so + 0x3ae3c)
                #29 0x0000ea94efccbe10 n/a (/root/opt/hosting/btec/llm-api/libhostpolicy.so + 0x3be10)
                #30 0x0000ea94efd3f200 n/a (/root/opt/hosting/btec/llm-api/libhostfxr.so + 0x2f200)
                #31 0x0000ea94efd3e524 n/a (/root/opt/hosting/btec/llm-api/libhostfxr.so + 0x2e524)
                #32 0x0000ea94efd38dc4 n/a (/root/opt/hosting/btec/llm-api/libhostfxr.so + 0x28dc4)
                #33 0x0000ae1c60c37438 n/a (/root/opt/hosting/btec/llm-api/BTEC.LLM.API + 0x17438)
                #34 0x0000ae1c60c37768 n/a (/root/opt/hosting/btec/llm-api/BTEC.LLM.API + 0x17768)
                #35 0x0000ea94efdb84c4 n/a (libc.so.6 + 0x284c4)
                #36 0x0000000000000011 n/a (n/a + 0x0)
                ELF object binary architecture: AARCH64

@martindevans
Copy link
Member

Sorry I misread and thought you'd switched to compiling yourself. If the prebuilt binaries are loading, but not working I think a reasonable next step would be to compile them yourself to see if it produces the same error.

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

No branches or pull requests

3 participants