Skip to content

Commit

Permalink
fix(cpp, ios): add prefix for iq2/iq3 func for avoid redef from anoth…
Browse files Browse the repository at this point in the history
…er libs used ggml
  • Loading branch information
jhen0409 committed Nov 6, 2024
1 parent ddf7135 commit 8ce5216
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
8 changes: 4 additions & 4 deletions cpp/ggml-quants.c
Original file line number Diff line number Diff line change
Expand Up @@ -13040,7 +13040,7 @@ static int iq2_compare_func(const void * left, const void * right) {
return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
}

void iq2xs_init_impl(enum lm_ggml_type type) {
void lm_iq2xs_init_impl(enum lm_ggml_type type) {
const int gindex = iq2_data_index(type);
const int grid_size = iq2_grid_size(type);
if (iq2_data[gindex].grid) {
Expand Down Expand Up @@ -13396,7 +13396,7 @@ void iq2xs_init_impl(enum lm_ggml_type type) {
free(dist2);
}

void iq2xs_free_impl(enum lm_ggml_type type) {
void lm_iq2xs_free_impl(enum lm_ggml_type type) {
LM_GGML_ASSERT(type == LM_GGML_TYPE_IQ2_XXS || type == LM_GGML_TYPE_IQ2_XS || type == LM_GGML_TYPE_IQ1_S || type == LM_GGML_TYPE_IQ1_M || type == LM_GGML_TYPE_IQ2_S);
const int gindex = iq2_data_index(type);
if (iq2_data[gindex].grid) {
Expand Down Expand Up @@ -13834,7 +13834,7 @@ static int iq3_compare_func(const void * left, const void * right) {
return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
}

void iq3xs_init_impl(int grid_size) {
void lm_iq3xs_init_impl(int grid_size) {
const int gindex = iq3_data_index(grid_size);
if (iq3_data[gindex].grid) {
return;
Expand Down Expand Up @@ -13990,7 +13990,7 @@ void iq3xs_init_impl(int grid_size) {
free(dist2);
}

void iq3xs_free_impl(int grid_size) {
void lm_iq3xs_free_impl(int grid_size) {
LM_GGML_ASSERT(grid_size == 256 || grid_size == 512);
const int gindex = iq3_data_index(grid_size);
if (iq3_data[gindex].grid) {
Expand Down
8 changes: 4 additions & 4 deletions cpp/ggml-quants.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ size_t quantize_q5_0(const float * LM_GGML_RESTRICT src, void * LM_GGML_RESTRICT
size_t quantize_q5_1(const float * LM_GGML_RESTRICT src, void * LM_GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);
size_t quantize_q8_0(const float * LM_GGML_RESTRICT src, void * LM_GGML_RESTRICT dst, int64_t nrows, int64_t n_per_row, const float * imatrix);

void iq2xs_init_impl(enum lm_ggml_type type);
void iq2xs_free_impl(enum lm_ggml_type type);
void iq3xs_init_impl(int grid_size);
void iq3xs_free_impl(int grid_size);
void lm_iq2xs_init_impl(enum lm_ggml_type type);
void lm_iq2xs_free_impl(enum lm_ggml_type type);
void lm_iq3xs_init_impl(int grid_size);
void lm_iq3xs_free_impl(int grid_size);

#ifdef __cplusplus
}
Expand Down
14 changes: 7 additions & 7 deletions cpp/ggml.c
Original file line number Diff line number Diff line change
Expand Up @@ -21793,9 +21793,9 @@ void lm_ggml_quantize_init(enum lm_ggml_type type) {
case LM_GGML_TYPE_IQ2_XS:
case LM_GGML_TYPE_IQ2_S:
case LM_GGML_TYPE_IQ1_S:
case LM_GGML_TYPE_IQ1_M: iq2xs_init_impl(type); break;
case LM_GGML_TYPE_IQ3_XXS: iq3xs_init_impl(256); break;
case LM_GGML_TYPE_IQ3_S: iq3xs_init_impl(512); break;
case LM_GGML_TYPE_IQ1_M: lm_iq2xs_init_impl(type); break;
case LM_GGML_TYPE_IQ3_XXS: lm_iq3xs_init_impl(256); break;
case LM_GGML_TYPE_IQ3_S: lm_iq3xs_init_impl(512); break;
default: // nothing
break;
}
Expand All @@ -21806,10 +21806,10 @@ void lm_ggml_quantize_init(enum lm_ggml_type type) {
void lm_ggml_quantize_free(void) {
lm_ggml_critical_section_start();

iq2xs_free_impl(LM_GGML_TYPE_IQ2_XXS);
iq2xs_free_impl(LM_GGML_TYPE_IQ2_XS);
iq2xs_free_impl(LM_GGML_TYPE_IQ1_S);
iq3xs_free_impl(256);
lm_iq2xs_free_impl(LM_GGML_TYPE_IQ2_XXS);
lm_iq2xs_free_impl(LM_GGML_TYPE_IQ2_XS);
lm_iq2xs_free_impl(LM_GGML_TYPE_IQ1_S);
lm_iq3xs_free_impl(256);

lm_ggml_critical_section_end();
}
Expand Down
25 changes: 23 additions & 2 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ cp ./llama.cpp/common/sampling.h ./cpp/sampling.h
cp ./llama.cpp/common/sampling.cpp ./cpp/sampling.cpp

# List of files to process
files=(
files_add_lm_prefix=(
"./cpp/llama-impl.h"
"./cpp/llama-vocab.h"
"./cpp/llama-vocab.cpp"
Expand Down Expand Up @@ -92,7 +92,7 @@ files=(

# Loop through each file and run the sed commands
OS=$(uname)
for file in "${files[@]}"; do
for file in "${files_add_lm_prefix[@]}"; do
# Add prefix to avoid redefinition with other libraries using ggml like whisper.rn
if [ "$OS" = "Darwin" ]; then
sed -i '' 's/GGML_/LM_GGML_/g' $file
Expand All @@ -109,6 +109,27 @@ for file in "${files[@]}"; do
fi
done

files_iq_add_lm_prefix=(
"./cpp/ggml-quants.h"
"./cpp/ggml-quants.c"
"./cpp/ggml.c"
)

for file in "${files_iq_add_lm_prefix[@]}"; do
# Add prefix to avoid redefinition with other libraries using ggml like whisper.rn
if [ "$OS" = "Darwin" ]; then
sed -i '' 's/iq2xs_init_impl/lm_iq2xs_init_impl/g' $file
sed -i '' 's/iq2xs_free_impl/lm_iq2xs_free_impl/g' $file
sed -i '' 's/iq3xs_init_impl/lm_iq3xs_init_impl/g' $file
sed -i '' 's/iq3xs_free_impl/lm_iq3xs_free_impl/g' $file
else
sed -i 's/iq2xs_init_impl/lm_iq2xs_init_impl/g' $file
sed -i 's/iq2xs_free_impl/lm_iq2xs_free_impl/g' $file
sed -i 's/iq3xs_init_impl/lm_iq3xs_init_impl/g' $file
sed -i 's/iq3xs_free_impl/lm_iq3xs_free_impl/g' $file
fi
done

echo "Replacement completed successfully!"

yarn example
Expand Down

0 comments on commit 8ce5216

Please sign in to comment.