fix: Fix support for arch-specific ggml-cpu source files with new arrangement
In https://github.com/ggml-org/llama.cpp/pull/13892, all arch-specific implementations were split out into a nested tree structure under ggml-cpu/arch. This conflicts with standard CGO layout where all arch-specific source files are expected to live in the same directory as the parent go module and use suffixes based on GOOS and GOARCH. As such, there were really two options for getting this to work: 1. Add a patch on top of the GGML sync to rearrange the files to match the GO layout convention 2. Use CGO directives to conditionally include the nested source files in the compilation units This commit does (2) in order to minimize the set of changes needed on top of the upstream file layout. To get this to work, there are two key things needed: 1. In cpu.go, #cgo directives are added to explicitly set __${GOARCH}__ in the preprocessor directives 2. In arch-impls.c|cpp, use an #ifdef | #elif defined | #endif chain to explicitly include the .c|.cpp files for the given architecture from the nested directory Branch: GraniteFour Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
This commit is contained in:
parent
7334a0ea07
commit
dbd8ee2654
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* This is a custom source file that explicitly incorporates the cpu-arch
|
||||
* implementation files for the target CPU. It is not part of the GGML source
|
||||
* and is only used to bind the arch implementations to CGO.
|
||||
*
|
||||
* The preprocessor defines are specified in cpu.go and correspond to GOARCH
|
||||
* values.
|
||||
*
|
||||
* https://github.com/golang/go/blob/master/src/internal/syslist/syslist.go#L58
|
||||
*/
|
||||
|
||||
#ifdef __amd64__
|
||||
#include "./arch/x86/quants.c"
|
||||
|
||||
#elif defined __arm64__
|
||||
#include "./arch/arm/quants.c"
|
||||
|
||||
#elif defined __loong64__
|
||||
#include "./arch/loongarch/quants.c"
|
||||
|
||||
#elif defined __ppc64__
|
||||
#include "./arch/powerpc/quants.c"
|
||||
|
||||
#elif defined __riscv64__
|
||||
#include "./arch/riscv/quants.c"
|
||||
|
||||
#elif defined __s390x__
|
||||
#include "./arch/s390/quants.c"
|
||||
|
||||
#elif defined __wasm__
|
||||
#include "./arch/wasm/quants.c"
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* This is a custom source file that explicitly incorporates the cpu-arch
|
||||
* implementation files for the target CPU. It is not part of the GGML source
|
||||
* and is only used to bind the arch implementations to CGO.
|
||||
*
|
||||
* The preprocessor defines are specified in cpu.go and correspond to GOARCH
|
||||
* values.
|
||||
*
|
||||
* https://github.com/golang/go/blob/master/src/internal/syslist/syslist.go#L58
|
||||
*/
|
||||
|
||||
#ifdef __amd64__
|
||||
#include "./arch/x86/cpu-feats.cpp"
|
||||
#include "./arch/x86/repack.cpp"
|
||||
|
||||
#elif defined __arm64__
|
||||
#include "./arch/arm/repack.cpp"
|
||||
|
||||
#elif defined __loong64__
|
||||
// No cpp
|
||||
|
||||
#elif defined __ppc64__
|
||||
#include "./arch/arm/cpu-feats.cpp"
|
||||
|
||||
#elif defined __riscv64__
|
||||
#include "./arch/arm/repack.cpp"
|
||||
|
||||
#elif defined __s390x__
|
||||
// No cpp
|
||||
|
||||
#elif defined __wasm__
|
||||
// No cpp
|
||||
|
||||
#endif
|
||||
|
|
@ -7,5 +7,26 @@ package cpu
|
|||
// #cgo linux CPPFLAGS: -D_GNU_SOURCE
|
||||
// #cgo darwin,arm64 CPPFLAGS: -DGGML_USE_ACCELERATE -DACCELERATE_NEW_LAPACK -DACCELERATE_LAPACK_ILP64
|
||||
// #cgo darwin,arm64 LDFLAGS: -framework Accelerate
|
||||
//
|
||||
// #cgo amd64 CFLAGS: -D__amd64__
|
||||
// #cgo amd64 CPPFLAGS: -D__amd64__
|
||||
//
|
||||
// #cgo arm64 CFLAGS: -D__arm64__
|
||||
// #cgo arm64 CPPFLAGS: -D__arm64__
|
||||
//
|
||||
// #cgo loong64 CFLAGS: -D__loong64__
|
||||
// #cgo loong64 CPPFLAGS: -D__loong64__
|
||||
//
|
||||
// #cgo ppc64 CFLAGS: -D__ppc64__
|
||||
// #cgo ppc64 CPPFLAGS: -D__ppc64__
|
||||
//
|
||||
// #cgo riscv64 CFLAGS: -D__riscv64__
|
||||
// #cgo riscv64 CPPFLAGS: -D__riscv64__
|
||||
//
|
||||
// #cgo s390x CFLAGS: -D__s390x__
|
||||
// #cgo s390x CPPFLAGS: -D__s390x__
|
||||
//
|
||||
// #cgo wasm CFLAGS: -D__wasm__
|
||||
// #cgo wasm CPPFLAGS: -D__wasm__
|
||||
import "C"
|
||||
import _ "github.com/ollama/ollama/ml/backend/ggml/ggml/src/ggml-cpu/llamafile"
|
||||
|
|
|
|||
Loading…
Reference in New Issue