Skip to content

Commit db17187

Browse files
authored
Merge branch 'ggml-org:master' into master
2 parents 1fd26de + 21d31e0 commit db17187

54 files changed

Lines changed: 1325 additions & 960 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Instructions for adding support for new models: [HOWTO-add-model.md](docs/develo
242242
- [crashr/gppm](https://github.com/crashr/gppm) – launch llama.cpp instances utilizing NVIDIA Tesla P40 or P100 GPUs with reduced idle power consumption
243243
- [gpustack/gguf-parser](https://github.com/gpustack/gguf-parser-go/tree/main/cmd/gguf-parser) - review/check the GGUF file and estimate the memory usage
244244
- [Styled Lines](https://marketplace.unity.com/packages/tools/generative-ai/styled-lines-llama-cpp-model-292902) (proprietary licensed, async wrapper of inference part for game development in Unity3d with pre-built Mobile and Web platform wrappers and a model example)
245+
- [unslothai/unsloth](https://github.com/unslothai/unsloth) – 🦥 exports/saves fine-tuned and trained models to GGUF (Apache-2.0)
245246

246247
</details>
247248

‎common/common.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <sstream>
2727
#include <string>
2828
#include <thread>
29-
#include <unordered_map>
3029
#include <unordered_set>
3130
#include <vector>
3231

@@ -60,6 +59,14 @@
6059
#pragma warning(disable: 4244 4267) // possible loss of data
6160
#endif
6261

62+
common_time_meas::common_time_meas(int64_t & t_acc, bool disable) : t_start_us(disable ? -1 : ggml_time_us()), t_acc(t_acc) {}
63+
64+
common_time_meas::~common_time_meas() {
65+
if (t_start_us >= 0) {
66+
t_acc += ggml_time_us() - t_start_us;
67+
}
68+
}
69+
6370
//
6471
// CPU utils
6572
//

‎common/common.h‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
#pragma once
44

5+
#include "ggml-opt.h"
6+
#include "llama-cpp.h"
7+
58
#include <set>
69
#include <sstream>
710
#include <string>
811
#include <string_view>
912
#include <vector>
1013
#include <map>
11-
#include <sstream>
12-
#include <cmath>
13-
14-
#include "ggml-opt.h"
15-
#include "llama-cpp.h"
1614

1715
#ifdef _WIN32
1816
#define DIRECTORY_SEPARATOR '\\'
@@ -30,6 +28,15 @@
3028

3129
#define DEFAULT_MODEL_PATH "models/7B/ggml-model-f16.gguf"
3230

31+
struct common_time_meas {
32+
common_time_meas(int64_t & t_acc, bool disable = false);
33+
~common_time_meas();
34+
35+
const int64_t t_start_us;
36+
37+
int64_t & t_acc;
38+
};
39+
3340
struct common_adapter_lora_info {
3441
std::string path;
3542
float scale;

‎common/sampling.cpp‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#include "common.h"
44
#include "log.h"
55

6+
#include <algorithm>
67
#include <cmath>
8+
#include <cstring>
79
#include <unordered_map>
8-
#include <algorithm>
910

1011
// the ring buffer works similarly to std::deque, but with a fixed capacity
1112
// TODO: deduplicate with llama-impl.h
@@ -112,6 +113,13 @@ struct common_sampler {
112113

113114
llama_token_data_array cur_p;
114115

116+
void reset() {
117+
prev.clear();
118+
119+
llama_sampler_reset(grmr);
120+
llama_sampler_reset(chain);
121+
}
122+
115123
void set_logits(struct llama_context * ctx, int idx) {
116124
const auto * logits = llama_get_logits_ith(ctx, idx);
117125

@@ -128,6 +136,12 @@ struct common_sampler {
128136

129137
cur_p = { cur.data(), cur.size(), -1, false };
130138
}
139+
140+
common_time_meas tm() {
141+
return common_time_meas(t_total_us, params.no_perf);
142+
}
143+
144+
mutable int64_t t_total_us = 0;
131145
};
132146

133147
std::string common_params_sampling::print() const {
@@ -298,6 +312,8 @@ void common_sampler_free(struct common_sampler * gsmpl) {
298312
}
299313

300314
void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar) {
315+
const auto tm = gsmpl->tm();
316+
301317
if (accept_grammar) {
302318
llama_sampler_accept(gsmpl->grmr, token);
303319
}
@@ -308,9 +324,7 @@ void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, boo
308324
}
309325

310326
void common_sampler_reset(struct common_sampler * gsmpl) {
311-
llama_sampler_reset(gsmpl->grmr);
312-
313-
llama_sampler_reset(gsmpl->chain);
327+
gsmpl->reset();
314328
}
315329

316330
struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
@@ -327,16 +341,54 @@ struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
327341
void common_perf_print(const struct llama_context * ctx, const struct common_sampler * gsmpl) {
328342
// TODO: measure grammar performance
329343

344+
const double t_sampling_ms = gsmpl ? 1e-3*gsmpl->t_total_us : 0;
345+
346+
llama_perf_sampler_data data_smpl;
347+
llama_perf_context_data data_ctx;
348+
349+
memset(&data_smpl, 0, sizeof(data_smpl));
350+
memset(&data_ctx, 0, sizeof(data_ctx));
351+
330352
if (gsmpl) {
331-
llama_perf_sampler_print(gsmpl->chain);
353+
auto & data = data_smpl;
354+
355+
data = llama_perf_sampler(gsmpl->chain);
356+
357+
// note: the sampling time includes the samplers time + extra time spent in common/sampling
358+
LOG_INF("%s: sampling time = %10.2f ms\n", __func__, t_sampling_ms);
359+
LOG_INF("%s: samplers time = %10.2f ms / %5d tokens\n", __func__, data.t_sample_ms, data.n_sample);
332360
}
361+
333362
if (ctx) {
334-
llama_perf_context_print(ctx);
363+
auto & data = data_ctx;
364+
365+
data = llama_perf_context(ctx);
366+
367+
const double t_end_ms = 1e-3 * ggml_time_us();
368+
369+
const double t_total_ms = t_end_ms - data.t_start_ms;
370+
const double t_unacc_ms = t_total_ms - (t_sampling_ms + data.t_p_eval_ms + data.t_eval_ms);
371+
const double t_unacc_pc = 100.0 * t_unacc_ms / t_total_ms;
372+
373+
LOG_INF("%s: load time = %10.2f ms\n", __func__, data.t_load_ms);
374+
LOG_INF("%s: prompt eval time = %10.2f ms / %5d tokens (%8.2f ms per token, %8.2f tokens per second)\n",
375+
__func__, data.t_p_eval_ms, data.n_p_eval, data.t_p_eval_ms / data.n_p_eval, 1e3 / data.t_p_eval_ms * data.n_p_eval);
376+
LOG_INF("%s: eval time = %10.2f ms / %5d runs (%8.2f ms per token, %8.2f tokens per second)\n",
377+
__func__, data.t_eval_ms, data.n_eval, data.t_eval_ms / data.n_eval, 1e3 / data.t_eval_ms * data.n_eval);
378+
LOG_INF("%s: total time = %10.2f ms / %5d tokens\n", __func__, (t_end_ms - data.t_start_ms), (data.n_p_eval + data.n_eval));
379+
LOG_INF("%s: unaccounted time = %10.2f ms / %5.1f %% (total - sampling - prompt eval - eval) / (total)\n", __func__, t_unacc_ms, t_unacc_pc);
380+
LOG_INF("%s: graphs reused = %10d\n", __func__, data.n_reused);
381+
335382
llama_memory_breakdown_print(ctx);
336383
}
337384
}
338385

339386
llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
387+
llama_synchronize(ctx);
388+
389+
// start measuring sampling time after the llama_context synchronization in order to not measure any ongoing async operations
390+
const auto tm = gsmpl->tm();
391+
340392
gsmpl->set_logits(ctx, idx);
341393

342394
auto & grmr = gsmpl->grmr;
@@ -428,6 +480,8 @@ uint32_t common_sampler_get_seed(const struct common_sampler * gsmpl) {
428480
// helpers
429481

430482
llama_token_data_array * common_sampler_get_candidates(struct common_sampler * gsmpl, bool do_sort) {
483+
const auto tm = gsmpl->tm();
484+
431485
auto * res = &gsmpl->cur_p;
432486

433487
if (do_sort && !res->sorted) {

‎convert_lora_to_gguf.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,15 @@ def parse_args() -> argparse.Namespace:
277277
return parser.parse_args()
278278

279279

280-
def load_hparams_from_hf(hf_model_id: str) -> dict[str, Any]:
280+
def load_hparams_from_hf(hf_model_id: str) -> tuple[dict[str, Any], Path | None]:
281+
from huggingface_hub import try_to_load_from_cache
282+
281283
# normally, adapter does not come with base model config, we need to load it from AutoConfig
282284
config = AutoConfig.from_pretrained(hf_model_id)
283-
return config.to_dict()
285+
cache_dir = try_to_load_from_cache(hf_model_id, "config.json")
286+
cache_dir = Path(cache_dir).parent if isinstance(cache_dir, str) else None
287+
288+
return config.to_dict(), cache_dir
284289

285290

286291
if __name__ == '__main__':
@@ -325,13 +330,13 @@ def load_hparams_from_hf(hf_model_id: str) -> dict[str, Any]:
325330
# load base model
326331
if base_model_id is not None:
327332
logger.info(f"Loading base model from Hugging Face: {base_model_id}")
328-
hparams = load_hparams_from_hf(base_model_id)
333+
hparams, dir_base_model = load_hparams_from_hf(base_model_id)
329334
elif dir_base_model is None:
330335
if "base_model_name_or_path" in lparams:
331336
model_id = lparams["base_model_name_or_path"]
332337
logger.info(f"Loading base model from Hugging Face: {model_id}")
333338
try:
334-
hparams = load_hparams_from_hf(model_id)
339+
hparams, dir_base_model = load_hparams_from_hf(model_id)
335340
except OSError as e:
336341
logger.error(f"Failed to load base model config: {e}")
337342
logger.error("Please try downloading the base model and add its path to --base")
@@ -480,6 +485,7 @@ def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iter
480485
dir_lora_model=dir_lora,
481486
lora_alpha=alpha,
482487
hparams=hparams,
488+
remote_hf_model_id=base_model_id,
483489
)
484490

485491
logger.info("Exporting model...")

‎examples/eval-callback/eval-callback.cpp‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include "llama.h"
55
#include "ggml.h"
66

7+
#include <cmath>
78
#include <cstdio>
89
#include <string>
910
#include <vector>
10-
#include <numeric>
1111

1212
/**
1313
* This the arbitrary data which will be passed to each callback.
@@ -37,23 +37,23 @@ static inline float ggml_compute_bf16_to_fp32(ggml_bf16_t h) {
3737
return u.f;
3838
}
3939

40-
static float ggml_get_float_value(uint8_t * data, ggml_type type, const size_t * nb, size_t i0, size_t i1, size_t i2, size_t i3) {
40+
static float ggml_get_float_value(const uint8_t * data, ggml_type type, const size_t * nb, size_t i0, size_t i1, size_t i2, size_t i3) {
4141
size_t i = i3 * nb[3] + i2 * nb[2] + i1 * nb[1] + i0 * nb[0];
4242
float v;
4343
if (type == GGML_TYPE_F16) {
44-
v = ggml_fp16_to_fp32(*(ggml_fp16_t *) &data[i]);
44+
v = ggml_fp16_to_fp32(*(const ggml_fp16_t *) &data[i]);
4545
} else if (type == GGML_TYPE_F32) {
46-
v = *(float *) &data[i];
46+
v = *(const float *) &data[i];
4747
} else if (type == GGML_TYPE_I64) {
48-
v = (float) *(int64_t *) &data[i];
48+
v = (float) *(const int64_t *) &data[i];
4949
} else if (type == GGML_TYPE_I32) {
50-
v = (float) *(int32_t *) &data[i];
50+
v = (float) *(const int32_t *) &data[i];
5151
} else if (type == GGML_TYPE_I16) {
52-
v = (float) *(int16_t *) &data[i];
52+
v = (float) *(const int16_t *) &data[i];
5353
} else if (type == GGML_TYPE_I8) {
54-
v = (float) *(int8_t *) &data[i];
54+
v = (float) *(const int8_t *) &data[i];
5555
} else if (type == GGML_TYPE_BF16) {
56-
v = ggml_compute_bf16_to_fp32(*(ggml_bf16_t *) &data[i]);
56+
v = ggml_compute_bf16_to_fp32(*(const ggml_bf16_t *) &data[i]);
5757
} else {
5858
GGML_ABORT("fatal error");
5959
}

‎ggml/src/ggml-cpu/kleidiai/kernels.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
#include "kernels.h"
4141

42-
#define NELEMS(x) sizeof(x) / sizeof(*x)
42+
#define NELEMS(x) (sizeof(x) / sizeof(*x))
4343

4444
template<size_t(*Fn)(size_t,size_t,size_t)>
4545
static inline size_t kernel_offs_fn3(size_t a, size_t b, size_t c) {
@@ -635,6 +635,7 @@ static ggml_kleidiai_kernels gemm_gemv_kernels[] = {
635635
},
636636
#endif
637637
#endif
638+
{ /* Sentinel */ }
638639
};
639640

640641
static ggml_kleidiai_kernels gemm_gemv_kernels_q8[] = {
@@ -803,14 +804,15 @@ static ggml_kleidiai_kernels gemm_gemv_kernels_q8[] = {
803804
/* .op_type = */ GGML_TYPE_F32,
804805
},
805806
#endif
807+
{ /* Sentinel */ }
806808
};
807809

808810
ggml_kleidiai_kernels * ggml_kleidiai_select_kernels(cpu_feature cpu_features, const ggml_tensor * tensor) {
809811
ggml_kleidiai_kernels * kernel = nullptr;
810812

811813
if (tensor->op == GGML_OP_MUL_MAT && tensor->src[0] != nullptr && tensor->src[1] != nullptr) {
812814
#if defined(__ARM_FEATURE_SME) || defined(__ARM_FEATURE_DOTPROD) || defined(__ARM_FEATURE_MATMUL_INT8)
813-
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels); ++i) {
815+
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels) - 1; ++i) {
814816
if ((cpu_features & gemm_gemv_kernels[i].required_cpu) == gemm_gemv_kernels[i].required_cpu &&
815817
gemm_gemv_kernels[i].lhs_type == tensor->src[1]->type &&
816818
gemm_gemv_kernels[i].rhs_type == tensor->src[0]->type &&
@@ -820,7 +822,7 @@ ggml_kleidiai_kernels * ggml_kleidiai_select_kernels(cpu_feature cpu_features, c
820822
}
821823
}
822824
if (!kernel) {
823-
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels_q8); ++i) {
825+
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels_q8) - 1; ++i) {
824826
if ((cpu_features & gemm_gemv_kernels_q8[i].required_cpu) == gemm_gemv_kernels_q8[i].required_cpu &&
825827
gemm_gemv_kernels_q8[i].lhs_type == tensor->src[1]->type &&
826828
gemm_gemv_kernels_q8[i].rhs_type == tensor->src[0]->type &&
@@ -830,6 +832,10 @@ ggml_kleidiai_kernels * ggml_kleidiai_select_kernels(cpu_feature cpu_features, c
830832
}
831833
}
832834
}
835+
#else
836+
GGML_UNUSED(gemm_gemv_kernels);
837+
GGML_UNUSED(gemm_gemv_kernels_q8);
838+
GGML_UNUSED(cpu_features);
833839
#endif
834840
}
835841

@@ -840,12 +846,14 @@ ggml_kleidiai_kernels * ggml_kleidiai_select_kernels_q4_0(cpu_feature features)
840846
ggml_kleidiai_kernels * kernels = nullptr;
841847

842848
#if defined(__ARM_FEATURE_SME) || defined(__ARM_FEATURE_DOTPROD) || defined(__ARM_FEATURE_MATMUL_INT8)
843-
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels); ++i) {
849+
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels) - 1; ++i) {
844850
if ((features & gemm_gemv_kernels[i].required_cpu) == gemm_gemv_kernels[i].required_cpu) {
845851
kernels = &gemm_gemv_kernels[i];
846852
break;
847853
}
848854
}
855+
#else
856+
GGML_UNUSED(features);
849857
#endif
850858

851859
return kernels;
@@ -855,12 +863,14 @@ ggml_kleidiai_kernels * ggml_kleidiai_select_kernels_q8_0(cpu_feature features)
855863
ggml_kleidiai_kernels * kernels = nullptr;
856864

857865
#if defined(__ARM_FEATURE_SME) || defined(__ARM_FEATURE_DOTPROD) || defined(__ARM_FEATURE_MATMUL_INT8)
858-
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels_q8); ++i) {
866+
for (size_t i = 0; i < NELEMS(gemm_gemv_kernels_q8) - 1; ++i) {
859867
if ((features & gemm_gemv_kernels_q8[i].required_cpu) == gemm_gemv_kernels_q8[i].required_cpu) {
860868
kernels = &gemm_gemv_kernels_q8[i];
861869
break;
862870
}
863871
}
872+
#else
873+
GGML_UNUSED(features);
864874
#endif
865875

866876
return kernels;

‎ggml/src/ggml-cpu/ops.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9696,13 +9696,12 @@ static void ggml_compute_forward_solve_tri_f32(const struct ggml_compute_params
96969696
for (int64_t i00 = 0; i00 < n; ++i00) {
96979697
float sum = 0.0f;
96989698
for (int64_t t = 0; t < i00; ++t) {
9699-
sum += A_batch[i00 * n + t] * X_batch[i01 * n + t];
9699+
sum += A_batch[i00 * n + t] * X_batch[t * k + i01];
97009700
}
97019701

97029702
const float diag = A_batch[i00 * n + i00];
97039703
GGML_ASSERT(diag != 0.0f && "Zero diagonal in triangular matrix");
9704-
9705-
X_batch[i01 * n + i00] = (B_batch[i00 * k + i01] - sum) / diag;
9704+
X_batch[i00 * k + i01] = (B_batch[i00 * k + i01] - sum) / diag;
97069705
}
97079706
}
97089707
}

0 commit comments

Comments
 (0)