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
@@ -76,7 +80,38 @@ common_time_meas::~common_time_meas() {
 //
 
 int32_t common_cpu_get_num_physical_cores() {
-#if defined(_AIX)
+#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(_AIX)
     int32_t logical_cpus = _system_configuration.ncpus;
     int32_t smt_threads = _system_configuration.smt_threads;
     if (smt_threads > 0) {
@@ -1033,6 +1068,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
@@ -1222,10 +1267,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);
     }
