LostRuins / koboldcpp

A simple one-file way to run various GGML and GGUF models with KoboldAI's UI
https://github.com/lostruins/koboldcpp
GNU Affero General Public License v3.0
4.36k stars 312 forks source link

[BUG 1.48] Linking fails on MacOS Sonoma without adding -ld_classic #509

Open kmilner opened 8 months ago

kmilner commented 8 months ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Expected Behavior

make compiles the program on ARM MacOS

Current Behavior

c++ -I. -I./common -I./include -I./include/CL -I./otherarch -I./otherarch/tools -Ofast -DNDEBUG -std=c++11 -fPIC -DLOG_DISABLE_LOGS -D_GNU_SOURCE -pthread -s -Wno-multichar -Wno-write-strings -pthread -DGGML_USE_METAL  ggml.o ggml_v2.o ggml_v1.o expose.o common.o gpttype_adapter.o ggml-quants.o ggml-alloc.o ggml-backend.o grammar-parser.o ggml-metal.o -shared -o koboldcpp_default.so  -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
Your OS  does not appear to be Windows. For faster speeds, install and link a BLAS library. Set LLAMA_OPENBLAS=1 to compile with OpenBLAS support or LLAMA_CLBLAST=1 to compile with ClBlast support. This is just a reminder, not an error.
ld: warning: -s is obsolete
0  0x104157648  __assert_rtn + 72
1  0x10407fc5c  ld::Fixup::applyFixup(ld::Atom const*, ld::LayoutLinkedImage const&, unsigned char*) const + 8268
2  0x1041127d8  ___ZN2ld16LayoutExecutable27writeContentWithoutLinkEditENSt3__14spanIhLm18446744073709551615EEEy_block_invoke + 332
3  0x1827c7950  _dispatch_client_callout2 + 20
4  0x1827dc1a4  _dispatch_apply_invoke_and_wait + 176
5  0x1827db464  _dispatch_apply_with_attr_f + 1176
6  0x1827db650  dispatch_apply + 96
7  0x1041129e4  void mapReduce<ld::Atom const*, mach_o::Error>(std::__1::span<ld::Atom const*, 18446744073709551615ul>, unsigned long, void (unsigned long, mach_o::Error&, std::__1::span<ld::Atom const*, 18446744073709551615ul>) block_pointer, void (std::__1::span<mach_o::Error, 18446744073709551615ul>) block_pointer) + 336
8  0x104112594  ld::LayoutExecutable::writeContentWithoutLinkEdit(std::__1::span<unsigned char, 18446744073709551615ul>, unsigned long long) + 1180
9  0x104118020  ld::LayoutExecutable::writeToFile(char const*) + 15248
10  0x1040ca2e8  main + 9424
ld: Assertion failed: (extras.otherInstrOffset != 0 && "Kind::arm64_adrp_ldr missing extra info"), function applyFixup, file Fixup.cpp, line 793.
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Environment and Context

ARM MacOS Sonoma

Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Failure Information (for bugs)

This appears to be a bug with the new Apple clang 15 linker.

On Sonoma, it can be fixed with the following patch, which adds the -ld_classic linker flag for MacOS:

diff --git a/Makefile b/Makefile
index 43381581..48a32afb 100644
--- a/Makefile
+++ b/Makefile
@@ -73,6 +73,7 @@ endif
 ifeq ($(UNAME_S),Darwin)
        CFLAGS   += -pthread
        CXXFLAGS += -pthread
+       LDFLAGS  += -ld_classic
 endif
 ifeq ($(UNAME_S),FreeBSD)
        CFLAGS   += -pthread

I don't know whether this will cause problems with earlier versions of MacOS though, I'm not sure how long the ld_classic flag has existed.

LostRuins commented 8 months ago

Hi, does it work for previous versions before 1.48? I didn't change the macOs makefile. New issue?

kmilner commented 8 months ago

This is the first time I've seen the issue myself. Just tested and the base makefile works fine in v1.47.2 and v1.46.1 using the same clang version.

Unfortunately some code change must have caused the linker to hit a bug (in it, not in this repo) it wasn't hitting before.

kmilner commented 8 months ago

https://github.com/godotengine/godot/pull/83088/files looks like -ld_classic does not exist prior to clang 15 based on what godot had to do for fixing builds, so that's not great.

kmilner commented 8 months ago

If you want a (hacky) patch that checks the clang version:

diff --git a/Makefile b/Makefile
index 43381581..03086e6a 100644
--- a/Makefile
+++ b/Makefile
@@ -73,6 +73,10 @@ endif
 ifeq ($(UNAME_S),Darwin)
        CFLAGS   += -pthread
        CXXFLAGS += -pthread
+       CLANG_VER = $(shell clang -v 2>&1 | head -n 1 | awk 'BEGIN {FS="[. ]"};{print $$1 $$2 $$4}')
+       ifeq ($(CLANG_VER),Appleclang15)
+               LDFLAGS += -ld_classic
+       endif
 endif
 ifeq ($(UNAME_S),FreeBSD)
        CFLAGS   += -pthread
LostRuins commented 8 months ago

I don't have a macOS device to test with, but i'll merge the hacky patch. Please let me know if it causes issues. https://github.com/LostRuins/koboldcpp/commit/2b32b170a1473172f26e8954943265978f5b70ac

beebopkim commented 8 months ago

I don't have a macOS device to test with, but i'll merge the hacky patch. Please let me know if it causes issues. 2b32b17

@kmilner 's amend works very well with most recent clang, Apple clang version 15.0.0 (clang-1500.0.40.1)