-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcommon_common.cpp.patch
More file actions
104 lines (102 loc) · 4.51 KB
/
Copy pathcommon_common.cpp.patch
File metadata and controls
104 lines (102 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
diff --git a/common/common.cpp b/common/common.cpp
--- a/llama.cpp/common/common.cpp
+++ b/llama.cpp/common/common.cpp
@@ -34,6 +34,10 @@
#include <sys/sysctl.h>
#endif
+#if defined(COSMOCC)
+#include <cosmo.h> // for IsLinux() and sysctlbyname() declarations
+#endif
+
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
@@ -72,7 +76,38 @@ common_time_meas::~common_time_meas() {
//
int32_t common_cpu_get_num_physical_cores() {
-#ifdef __linux__
+#if defined(COSMOCC)
+ // cosmocc cross-compiles for both Linux and macOS at once, so neither
+ // __linux__ nor __APPLE__ is defined here; detect the host OS at runtime.
+ if (IsLinux()) {
+ std::unordered_set<std::string> siblings;
+ for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
+ std::ifstream thread_siblings("/sys/devices/system/cpu/cpu"
+ + std::to_string(cpu) + "/topology/thread_siblings");
+ if (!thread_siblings.is_open()) {
+ break; // no more cpus
+ }
+ std::string line;
+ if (std::getline(thread_siblings, line)) {
+ siblings.insert(line);
+ }
+ }
+ if (!siblings.empty()) {
+ return static_cast<int32_t>(siblings.size());
+ }
+ } else {
+ int32_t num_physical_cores;
+ size_t len = sizeof(num_physical_cores);
+ int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
+ if (result == 0) {
+ return num_physical_cores;
+ }
+ result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
+ if (result == 0) {
+ return num_physical_cores;
+ }
+ }
+#elif defined(__linux__)
// enumerate the set of thread siblings, num entries is num cores
std::unordered_set<std::string> siblings;
for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
@@ -1012,6 +1047,16 @@ std::string fs_get_cache_directory() {
cache_directory = std::getenv("HOME") + std::string("/Library/Caches/");
#elif defined(_WIN32)
cache_directory = std::getenv("LOCALAPPDATA");
+#elif defined(COSMOCC)
+ // We don't know what OS we are running on at compile time, just CPU architecture.
+ // try various environment variables, fall back to ~/.cache.
+ if (std::getenv("LOCALAPPDATA")) {
+ cache_directory = std::getenv("LOCALAPPDATA");
+ } else if (std::getenv("XDG_CACHE_HOME")) {
+ cache_directory = std::getenv("XDG_CACHE_HOME");
+ } else {
+ cache_directory = std::getenv("HOME") + std::string("/.cache/");
+ }
#elif defined(__EMSCRIPTEN__)
GGML_ABORT("not implemented on this platform");
#else
@@ -1201,10 +1246,31 @@ common_init_result::common_init_result(common_params & params, bool model_only)
if (params.fit_params) {
COM_TRC("%s", "fitting params to device memory ...\n");
COM_TRC("%s", "(for bugs during this step try to reproduce them with -fit off, or provide --verbose logs if the bug only occurs with -fit on)\n");
+
+ // if a multimodal projection model is specified and will be loaded on GPU,
+ // add its estimated size to the fit margins so the fit leaves enough room for it
+ std::vector<size_t> fit_params_target = params.fit_params_target;
+ if (!params.mmproj.path.empty() && params.mmproj_use_gpu) {
+ struct gguf_init_params gparams = { /*.no_alloc =*/ true, /*.ctx =*/ nullptr };
+ struct gguf_context * gctx = gguf_init_from_file(params.mmproj.path.c_str(), gparams);
+ if (gctx != nullptr) {
+ size_t mmproj_size = 0;
+ for (int64_t i = 0; i < gguf_get_n_tensors(gctx); i++) {
+ mmproj_size += gguf_get_tensor_size(gctx, i);
+ }
+ gguf_free(gctx);
+ LOG_INF("%s: reserving %.2f MiB extra for mmproj model '%s'\n", __func__,
+ mmproj_size / (1024.0 * 1024.0), params.mmproj.path.c_str());
+ for (size_t & target : fit_params_target) {
+ target += mmproj_size;
+ }
+ }
+ }
+
common_fit_params(params.model.path.c_str(), &mparams, &cparams,
params.tensor_split,
params.tensor_buft_overrides.data(),
- params.fit_params_target.data(),
+ fit_params_target.data(),
params.fit_params_min_ctx,
params.verbosity >= LOG_LEVEL_DEBUG ? GGML_LOG_LEVEL_DEBUG : GGML_LOG_LEVEL_ERROR);
}