-
Notifications
You must be signed in to change notification settings - Fork 12.1k
ggml-cpu : split arch-specific implementations #13892
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
Conversation
I'll need a bit more time to get this PR rebased onto the latest master branch. |
@ggerganov @slaren |
ggml/src/ggml-cpu/ggml-cpu-impl.h
Outdated
|
||
#define GGML_DO_PRAGMA_(x) _Pragma (#x) | ||
#define GGML_DO_PRAGMA(x) GGML_DO_PRAGMA_(x) | ||
#if defined(GGML_CPU_GENERIC) || defined(__HIPCC__) | ||
// weak alias not working | ||
# define GGML_WEAK_ALIAS(name, alias) | ||
#elif defined(__GNUC__) | ||
// GCC/Clang on *nix | ||
# define GGML_WEAK_ALIAS(name, alias) GGML_DO_PRAGMA(weak name = alias) // NOLINT | ||
#elif defined(_MSC_VER) && defined (_WIN64) | ||
// MSVC | ||
// Note: C name mangling varies across different calling conventions | ||
// see https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170 | ||
# define GGML_WEAK_ALIAS(name, alias) GGML_DO_PRAGMA(comment(linker, "/alternatename:" #name "=" #alias)) | ||
#else | ||
# error "Unsupported compiler for GGML_WEAK_ALIAS" | ||
#endif | ||
|
||
#define GGML_CPU_NATIVE_IMPL(name) GGML_WEAK_ALIAS(name, name ## _generic) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"generic" implementations such as ggml_vec_dot_q4_0_q8_0_generic
are always defined, regardless of the build configuration. And by making these definitions "weak", we allow them to be overwritten by arch-specific implementation.
Is my understanding of this logic correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, on supported targets. The linker will first attempt to resolve implemented arch-specific symbols (like ggml_vec_dot_q4_0_q8_0
). If an optimized version is not found, it automatically falls back to the _generic
variants. I believe this approach offers improved maintainability at the cost of a slight increase in binary size due to these shadowed fallback implementations.
#endif | ||
for (; ib < nb; ++ib) { | ||
int sumi0 = 0; | ||
int sumi1 = 0; | ||
|
||
for (int j = 0; j < qk/2; ++j) { | ||
const int v0 = (x[ib].qs[j] & 0x0F) - 8; | ||
const int v1 = (x[ib].qs[j] >> 4) - 8; | ||
|
||
sumi0 += (v0 * y[ib].qs[j]); | ||
sumi1 += (v1 * y[ib].qs[j + qk/2]); | ||
} | ||
|
||
int sumi = sumi0 + sumi1; | ||
sumf += sumi*GGML_FP16_TO_FP32(x[ib].d)*GGML_FP16_TO_FP32(y[ib].d); | ||
} | ||
|
||
*s = sumf; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, each arch re-implements the generic version. For this big refactoring this is probably the correct approach in order to minimize the risk of introducing bugs. But after we merge, we should consider ways to de-duplicate the scalar implementations by reusing the generic calls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be feasible, though it's not as straightforward for tail elements like the ones in this function. Let's keep the deduplication work for another PR to make future bisecting easier.
Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
@xctan, nice to meet you and sorry to bother you. I'm your fan. I know you are a highly-skilled student in China's No.1 university and did many excellent contributions for llama.cpp and other highly-difficult open-source projects(especially the RISC-V community). here I have a minor question:
the previous benchmark data of the ggml-hexagon(via cDSP) is about 15 milliseconds, now is 6-7 milliseconds.
thanks too much. |
Encountered a similar issue: the speed on ft-2000 is reduced by one fold. |
If you have noticed a regression, please open a new issue with repro steps. |
hello, nice to meet you, (1)I'm not AI expert and have made a stupid mistake because of my little AI knowledge, (2)I'm not familiar with FT-2000(China's self-developed desktop/server SoC) and also doesn't have such device. Is this new issue submitted/filed by you or me? thanks. |
HI. |
Can you provide the dynamic symbol table in your build (e.g. output of |
thanks for your reply.
|
This has broken the build on PowerPC: #14138 |
hope this weird problem will be suddenly disappeared tomorrow. +whr819987540, @whr819987540, sorry to bother you, pls take a look if you have time, thanks! |
there is so much duplicate code in each cpu arch, i expect upstream will prune it eventually arch detection has no fallback if all the arches are not found, by right we should set GGML_CPU_GENERIC i should be relaxing its the weekend
…org#13892" This reverts commit 33809c9.
This PR reworks the arch-specific code organization, following the discussion in #13720. Key changes include splitting the former
ggml-cpu-quants.c
andggml-cpu-aarch64.cpp
into several more focused files. Additionally,aarch64
-related naming and identifiers, originally specific to AArch64 but now applicable to other architectures, have been updated to userepack
to improve clarity and avoid confusion.