From 9043053a25fb2416d94dfb8f2faf1280f00c8a57 Mon Sep 17 00:00:00 2001 From: Misha Skvortsov Date: Tue, 1 Dec 2026 17:23:43 +0300 Subject: [PATCH] Map file-backed weights for CPU-only inference on macOS --- ...002-map-explicit-cpu-models-on-macos.patch | 30 +++++++++++++++++++ atomic/patches/README.md | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 atomic/patches/0002-map-explicit-cpu-models-on-macos.patch diff --git a/atomic/patches/0002-map-explicit-cpu-models-on-macos.patch b/atomic/patches/0002-map-explicit-cpu-models-on-macos.patch new file mode 100644 index 0000000000..2248ecd3f7 --- /dev/null +++ b/atomic/patches/0002-map-explicit-cpu-models-on-macos.patch @@ -0,0 +1,30 @@ +Map file-backed weights by default for explicitly CPU-only generation on macOS. + +The signed 1163 build asks for another 2.43 GiB heap allocation for the +FLUX.2 Klein denoiser and can refuse it on a Mac where 1106 generates. +Let the existing mmap path serve compatible tensors from the model files. +The allocator still checks graph buffers and weights that require copying. +Preserve explicit mmap and weight placement choices. + +diff --git a/src/api/options.cpp b/src/api/options.cpp +index f056647..bcdb220 100644 +--- a/src/api/options.cpp ++++ b/src/api/options.cpp +@@ -885,6 +885,17 @@ sd_status_t sd::api::resolve(const sd_opt_t* params, Operation operation, OptCon + return error(SD_STATUS_UNSUPPORTED, entry.first.c_str(), "parameter does not apply to this operation"); + spec->apply(entry.second, config); + } ++#ifdef __APPLE__ ++ if (operation == Context && config.context.backend && !is_set(params, "enable_mmap") && !is_set(params, "params_backend")) { ++ SDBackendAssignment assignment; ++ if (sd_parse_backend_assignment(config.context.backend, &assignment, nullptr) && ++ assignment.default_name == "cpu" && ++ std::all_of(assignment.module_names.begin(), assignment.module_names.end(), [](const auto& module) { return module.second == "cpu"; })) { ++ // CPU inference can use file-backed weights without a second full-size allocation in RAM. ++ config.context.enable_mmap = true; ++ } ++ } ++#endif + const auto& cache = operation == Video ? config.video.cache : config.image.cache; + if ((operation == Image || operation == Video) && cache.mode != SD_CACHE_DISABLED && cache.start_percent >= cache.end_percent) + return error(SD_STATUS_INVALID_ARGUMENT, "cache", "start_percent must precede end_percent"); diff --git a/atomic/patches/README.md b/atomic/patches/README.md index 49b2728e69..aafe9d904d 100644 --- a/atomic/patches/README.md +++ b/atomic/patches/README.md @@ -9,8 +9,9 @@ upstream: a patch that lands upstream is deleted here on the next tag. patch that does not apply on top of the ones before it, with git's reason, and flags the ones that reverse cleanly as already upstream. -Current series (build system only; no source code differs from the upstream tag): +Current series: | Patch | Why | | --- | --- | | `0001-export-libwebm-symbols-for-clang-on-windows.patch` | clang for Windows does not set `MSVC`, so libwebm's DLL exported nothing and `sd-cli`/`sd-server` failed to link. | +| `0002-map-explicit-cpu-models-on-macos.patch` | On macOS, CPU-only generation maps compatible weights from the model files. Copying them into RAM could stop FLUX.2 Klein on machines where the published engine works. |