mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-30 08:42:00 +00:00 
			
		
		
		
	ggml-cpu : rework weak alias on apple targets (#14146)
* ggml-cpu : rework weak alias on apple targets * fix powerpc detection * fix ppc detection * fix powerpc detection on darwin
This commit is contained in:
		| @@ -36,8 +36,7 @@ function(ggml_get_system_arch) | ||||
|             (NOT CMAKE_OSX_ARCHITECTURES AND NOT CMAKE_GENERATOR_PLATFORM_LWR AND | ||||
|             CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|i686|AMD64|amd64)$")) | ||||
|         set(GGML_SYSTEM_ARCH "x86" PARENT_SCOPE) | ||||
|     elseif ("${CMAKE_SYSTEM_PROCESSOR} " STREQUAL "ppc64le " OR | ||||
|             "${CMAKE_SYSTEM_PROCESSOR} " STREQUAL "powerpc ") | ||||
|     elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc|power") | ||||
|         set(GGML_SYSTEM_ARCH "PowerPC" PARENT_SCOPE) | ||||
|     elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "loongarch64") | ||||
|         set(GGML_SYSTEM_ARCH "loongarch64"  PARENT_SCOPE) | ||||
|   | ||||
							
								
								
									
										88
									
								
								ggml/src/ggml-cpu/apple-fallback.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								ggml/src/ggml-cpu/apple-fallback.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,88 @@ | ||||
| #pragma once | ||||
|  | ||||
| // Solve alias issue for Apple targets (currently PowerPC, x86, and ARM64). | ||||
| // Mach-O has a weak alias equivalent but no practical compiler support can | ||||
| // be found, so we need to do it manually. | ||||
| // ref: https://stackoverflow.com/questions/42757744 | ||||
| // | ||||
| // This file is a complement to native implementations in the `arch` folder. | ||||
| // A kernel in quants.c or repack.cpp is either: | ||||
| // - implemented in the `arch` folder, or | ||||
| // - defined in this file to remove the `_generic` suffix | ||||
|  | ||||
| #if defined(GGML_CPU_GENERIC) | ||||
| // quants.c | ||||
| #define quantize_row_q8_0_generic quantize_row_q8_0 | ||||
| #define quantize_row_q8_1_generic quantize_row_q8_1 | ||||
| #define quantize_row_q8_K_generic quantize_row_q8_K | ||||
| #define ggml_vec_dot_q4_0_q8_0_generic ggml_vec_dot_q4_0_q8_0 | ||||
| #define ggml_vec_dot_q4_1_q8_1_generic ggml_vec_dot_q4_1_q8_1 | ||||
| #define ggml_vec_dot_q5_0_q8_0_generic ggml_vec_dot_q5_0_q8_0 | ||||
| #define ggml_vec_dot_q5_1_q8_1_generic ggml_vec_dot_q5_1_q8_1 | ||||
| #define ggml_vec_dot_q8_0_q8_0_generic ggml_vec_dot_q8_0_q8_0 | ||||
| #define ggml_vec_dot_tq1_0_q8_K_generic ggml_vec_dot_tq1_0_q8_K | ||||
| #define ggml_vec_dot_tq2_0_q8_K_generic ggml_vec_dot_tq2_0_q8_K | ||||
| #define ggml_vec_dot_q2_K_q8_K_generic ggml_vec_dot_q2_K_q8_K | ||||
| #define ggml_vec_dot_q3_K_q8_K_generic ggml_vec_dot_q3_K_q8_K | ||||
| #define ggml_vec_dot_q4_K_q8_K_generic ggml_vec_dot_q4_K_q8_K | ||||
| #define ggml_vec_dot_q5_K_q8_K_generic ggml_vec_dot_q5_K_q8_K | ||||
| #define ggml_vec_dot_q6_K_q8_K_generic ggml_vec_dot_q6_K_q8_K | ||||
| #define ggml_vec_dot_iq2_xxs_q8_K_generic ggml_vec_dot_iq2_xxs_q8_K | ||||
| #define ggml_vec_dot_iq2_xs_q8_K_generic ggml_vec_dot_iq2_xs_q8_K | ||||
| #define ggml_vec_dot_iq2_s_q8_K_generic ggml_vec_dot_iq2_s_q8_K | ||||
| #define ggml_vec_dot_iq3_xxs_q8_K_generic ggml_vec_dot_iq3_xxs_q8_K | ||||
| #define ggml_vec_dot_iq3_s_q8_K_generic ggml_vec_dot_iq3_s_q8_K | ||||
| #define ggml_vec_dot_iq1_s_q8_K_generic ggml_vec_dot_iq1_s_q8_K | ||||
| #define ggml_vec_dot_iq1_m_q8_K_generic ggml_vec_dot_iq1_m_q8_K | ||||
| #define ggml_vec_dot_iq4_nl_q8_0_generic ggml_vec_dot_iq4_nl_q8_0 | ||||
| #define ggml_vec_dot_iq4_xs_q8_K_generic ggml_vec_dot_iq4_xs_q8_K | ||||
| // repack.cpp | ||||
| #define ggml_quantize_mat_q8_0_4x4_generic ggml_quantize_mat_q8_0_4x4 | ||||
| #define ggml_quantize_mat_q8_0_4x8_generic ggml_quantize_mat_q8_0_4x8 | ||||
| #define ggml_quantize_mat_q8_K_4x8_generic ggml_quantize_mat_q8_K_4x8 | ||||
| #define ggml_gemv_q4_0_4x4_q8_0_generic ggml_gemv_q4_0_4x4_q8_0 | ||||
| #define ggml_gemv_q4_0_4x8_q8_0_generic ggml_gemv_q4_0_4x8_q8_0 | ||||
| #define ggml_gemv_q4_0_8x8_q8_0_generic ggml_gemv_q4_0_8x8_q8_0 | ||||
| #define ggml_gemv_q4_K_8x8_q8_K_generic ggml_gemv_q4_K_8x8_q8_K | ||||
| #define ggml_gemv_iq4_nl_4x4_q8_0_generic ggml_gemv_iq4_nl_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x4_q8_0_generic ggml_gemm_q4_0_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x8_q8_0_generic ggml_gemm_q4_0_4x8_q8_0 | ||||
| #define ggml_gemm_q4_0_8x8_q8_0_generic ggml_gemm_q4_0_8x8_q8_0 | ||||
| #define ggml_gemm_q4_K_8x8_q8_K_generic ggml_gemm_q4_K_8x8_q8_K | ||||
| #define ggml_gemm_iq4_nl_4x4_q8_0_generic ggml_gemm_iq4_nl_4x4_q8_0 | ||||
| #elif defined(__aarch64__) || defined(__arm__) | ||||
| // repack.cpp | ||||
| #define ggml_quantize_mat_q8_K_4x8_generic ggml_quantize_mat_q8_K_4x8 | ||||
| #define ggml_gemv_q4_K_8x8_q8_K_generic ggml_gemv_q4_K_8x8_q8_K | ||||
| #define ggml_gemm_q4_K_8x8_q8_K_generic ggml_gemm_q4_K_8x8_q8_K | ||||
| #elif defined(__x86_64__) || defined(__i386__) | ||||
| // repack.cpp | ||||
| #define ggml_quantize_mat_q8_0_4x4_generic ggml_quantize_mat_q8_0_4x4 | ||||
| #define ggml_gemv_q4_0_4x4_q8_0_generic ggml_gemv_q4_0_4x4_q8_0 | ||||
| #define ggml_gemv_q4_0_4x8_q8_0_generic ggml_gemv_q4_0_4x8_q8_0 | ||||
| #define ggml_gemv_iq4_nl_4x4_q8_0_generic ggml_gemv_iq4_nl_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x4_q8_0_generic ggml_gemm_q4_0_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x8_q8_0_generic ggml_gemm_q4_0_4x8_q8_0 | ||||
| #define ggml_gemm_iq4_nl_4x4_q8_0_generic ggml_gemm_iq4_nl_4x4_q8_0 | ||||
| #elif defined(__POWERPC__) | ||||
| // ref: https://github.com/ggml-org/llama.cpp/pull/14146#issuecomment-2972561679 | ||||
| // quants.c | ||||
| #define quantize_row_q8_K_generic quantize_row_q8_K | ||||
| #define ggml_vec_dot_tq1_0_q8_K_generic ggml_vec_dot_tq1_0_q8_K | ||||
| #define ggml_vec_dot_tq2_0_q8_K_generic ggml_vec_dot_tq2_0_q8_K | ||||
| #define ggml_vec_dot_iq1_m_q8_K_generic ggml_vec_dot_iq1_m_q8_K | ||||
| // repack.cpp | ||||
| #define ggml_quantize_mat_q8_0_4x4_generic ggml_quantize_mat_q8_0_4x4 | ||||
| #define ggml_quantize_mat_q8_0_4x8_generic ggml_quantize_mat_q8_0_4x8 | ||||
| #define ggml_quantize_mat_q8_K_4x8_generic ggml_quantize_mat_q8_K_4x8 | ||||
| #define ggml_gemv_q4_0_4x4_q8_0_generic ggml_gemv_q4_0_4x4_q8_0 | ||||
| #define ggml_gemv_q4_0_4x8_q8_0_generic ggml_gemv_q4_0_4x8_q8_0 | ||||
| #define ggml_gemv_q4_0_8x8_q8_0_generic ggml_gemv_q4_0_8x8_q8_0 | ||||
| #define ggml_gemv_q4_K_8x8_q8_K_generic ggml_gemv_q4_K_8x8_q8_K | ||||
| #define ggml_gemv_iq4_nl_4x4_q8_0_generic ggml_gemv_iq4_nl_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x4_q8_0_generic ggml_gemm_q4_0_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x8_q8_0_generic ggml_gemm_q4_0_4x8_q8_0 | ||||
| #define ggml_gemm_q4_0_8x8_q8_0_generic ggml_gemm_q4_0_8x8_q8_0 | ||||
| #define ggml_gemm_q4_K_8x8_q8_K_generic ggml_gemm_q4_K_8x8_q8_K | ||||
| #define ggml_gemm_iq4_nl_4x4_q8_0_generic ggml_gemm_iq4_nl_4x4_q8_0 | ||||
| #endif | ||||
| @@ -509,7 +509,7 @@ void ggml_barrier(struct ggml_threadpool * tp); | ||||
|  | ||||
| #define GGML_DO_PRAGMA_(x) _Pragma (#x) | ||||
| #define GGML_DO_PRAGMA(x) GGML_DO_PRAGMA_(x) | ||||
| #if defined(GGML_CPU_GENERIC) || defined(__HIPCC__) | ||||
| #if defined(GGML_CPU_GENERIC) || defined(__HIPCC__) || defined(__APPLE__) | ||||
| // Note for Apple targets: | ||||
| // - clang: aliases are not supported on darwin | ||||
| // - all native kernels need to be implemented in both x86 and arm files | ||||
|   | ||||
| @@ -5,6 +5,10 @@ | ||||
| #include "ggml-quants.h" | ||||
| #include "quants.h" | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| #include "apple-fallback.h" | ||||
| #endif | ||||
|  | ||||
| #include <string.h> | ||||
| #include <assert.h> | ||||
| #include <float.h> | ||||
|   | ||||
| @@ -84,33 +84,6 @@ void ggml_vec_dot_iq1_m_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, | ||||
| void ggml_vec_dot_iq4_nl_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc); | ||||
| void ggml_vec_dot_iq4_xs_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, size_t bx, const void * GGML_RESTRICT vy, size_t by, int nrc); | ||||
|  | ||||
| #if defined(GGML_CPU_GENERIC) | ||||
| #define quantize_row_q8_0_generic quantize_row_q8_0 | ||||
| #define quantize_row_q8_1_generic quantize_row_q8_1 | ||||
| #define quantize_row_q8_K_generic quantize_row_q8_K | ||||
| #define ggml_vec_dot_q4_0_q8_0_generic ggml_vec_dot_q4_0_q8_0 | ||||
| #define ggml_vec_dot_q4_1_q8_1_generic ggml_vec_dot_q4_1_q8_1 | ||||
| #define ggml_vec_dot_q5_0_q8_0_generic ggml_vec_dot_q5_0_q8_0 | ||||
| #define ggml_vec_dot_q5_1_q8_1_generic ggml_vec_dot_q5_1_q8_1 | ||||
| #define ggml_vec_dot_q8_0_q8_0_generic ggml_vec_dot_q8_0_q8_0 | ||||
| #define ggml_vec_dot_tq1_0_q8_K_generic ggml_vec_dot_tq1_0_q8_K | ||||
| #define ggml_vec_dot_tq2_0_q8_K_generic ggml_vec_dot_tq2_0_q8_K | ||||
| #define ggml_vec_dot_q2_K_q8_K_generic ggml_vec_dot_q2_K_q8_K | ||||
| #define ggml_vec_dot_q3_K_q8_K_generic ggml_vec_dot_q3_K_q8_K | ||||
| #define ggml_vec_dot_q4_K_q8_K_generic ggml_vec_dot_q4_K_q8_K | ||||
| #define ggml_vec_dot_q5_K_q8_K_generic ggml_vec_dot_q5_K_q8_K | ||||
| #define ggml_vec_dot_q6_K_q8_K_generic ggml_vec_dot_q6_K_q8_K | ||||
| #define ggml_vec_dot_iq2_xxs_q8_K_generic ggml_vec_dot_iq2_xxs_q8_K | ||||
| #define ggml_vec_dot_iq2_xs_q8_K_generic ggml_vec_dot_iq2_xs_q8_K | ||||
| #define ggml_vec_dot_iq2_s_q8_K_generic ggml_vec_dot_iq2_s_q8_K | ||||
| #define ggml_vec_dot_iq3_xxs_q8_K_generic ggml_vec_dot_iq3_xxs_q8_K | ||||
| #define ggml_vec_dot_iq3_s_q8_K_generic ggml_vec_dot_iq3_s_q8_K | ||||
| #define ggml_vec_dot_iq1_s_q8_K_generic ggml_vec_dot_iq1_s_q8_K | ||||
| #define ggml_vec_dot_iq1_m_q8_K_generic ggml_vec_dot_iq1_m_q8_K | ||||
| #define ggml_vec_dot_iq4_nl_q8_0_generic ggml_vec_dot_iq4_nl_q8_0 | ||||
| #define ggml_vec_dot_iq4_xs_q8_K_generic ggml_vec_dot_iq4_xs_q8_K | ||||
| #endif | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|   | ||||
| @@ -8,6 +8,10 @@ | ||||
| #include "ggml-cpu-impl.h" | ||||
| #include "traits.h" | ||||
|  | ||||
| #if defined(__APPLE__) | ||||
| #include "apple-fallback.h" | ||||
| #endif | ||||
|  | ||||
| #include <cmath> | ||||
| #include <cstring> | ||||
| #include <cassert> | ||||
|   | ||||
| @@ -67,7 +67,7 @@ extern "C" { | ||||
| // Workaround for clang: | ||||
| // clang++ complains: ``error: call to 'ggml_gemm_q4_0_4x4_q8_0' is ambiguous'' | ||||
| // repro: https://godbolt.org/z/oKdeWKonM (ICE), https://godbolt.org/z/1szq6P36v (ambiguous call) | ||||
| #if defined(GGML_CPU_CLANG_WORKAROUND) || !(defined(__GNUC__) && defined(__clang__)) || defined(__HIPCC__) | ||||
| #if defined(GGML_CPU_CLANG_WORKAROUND) || defined(__APPLE__) || !(defined(__GNUC__) && defined(__clang__)) || defined(__HIPCC__) | ||||
| void ggml_quantize_mat_q8_0_4x4(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k); | ||||
| void ggml_quantize_mat_q8_0_4x8(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k); | ||||
| void ggml_quantize_mat_q8_K_4x8(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k); | ||||
| @@ -98,22 +98,6 @@ void ggml_gemm_q4_0_8x8_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, | ||||
| void ggml_gemm_q4_K_8x8_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc); | ||||
| void ggml_gemm_iq4_nl_4x4_q8_0_generic(int n, float * GGML_RESTRICT s, size_t bs, const void * GGML_RESTRICT vx, const void * GGML_RESTRICT vy, int nr, int nc); | ||||
|  | ||||
| #if defined(GGML_CPU_GENERIC) | ||||
| #define ggml_quantize_mat_q8_0_4x4_generic ggml_quantize_mat_q8_0_4x4 | ||||
| #define ggml_quantize_mat_q8_0_4x8_generic ggml_quantize_mat_q8_0_4x8 | ||||
| #define ggml_quantize_mat_q8_K_4x8_generic ggml_quantize_mat_q8_K_4x8 | ||||
| #define ggml_gemv_q4_0_4x4_q8_0_generic ggml_gemv_q4_0_4x4_q8_0 | ||||
| #define ggml_gemv_q4_0_4x8_q8_0_generic ggml_gemv_q4_0_4x8_q8_0 | ||||
| #define ggml_gemv_q4_0_8x8_q8_0_generic ggml_gemv_q4_0_8x8_q8_0 | ||||
| #define ggml_gemv_q4_K_8x8_q8_K_generic ggml_gemv_q4_K_8x8_q8_K | ||||
| #define ggml_gemv_iq4_nl_4x4_q8_0_generic ggml_gemv_iq4_nl_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x4_q8_0_generic ggml_gemm_q4_0_4x4_q8_0 | ||||
| #define ggml_gemm_q4_0_4x8_q8_0_generic ggml_gemm_q4_0_4x8_q8_0 | ||||
| #define ggml_gemm_q4_0_8x8_q8_0_generic ggml_gemm_q4_0_8x8_q8_0 | ||||
| #define ggml_gemm_q4_K_8x8_q8_K_generic ggml_gemm_q4_K_8x8_q8_K | ||||
| #define ggml_gemm_iq4_nl_4x4_q8_0_generic ggml_gemm_iq4_nl_4x4_q8_0 | ||||
| #endif | ||||
|  | ||||
| #if defined(__cplusplus) | ||||
| } // extern "C" | ||||
| #endif | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 xctan
					xctan