stablediffusion-ggml: replace hand-maintained enum string arrays with upstream API calls#9192
Merged
Merged
Conversation
…PI functions Agent-Logs-Url: https://github.com/mudler/LocalAI/sessions/561fb489-89ed-4588-8f1e-7b967d91ba37 Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix static assert errors for string arrays in gosd.cpp
stablediffusion-ggml: replace hand-maintained enum string arrays with upstream API calls
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
gosd.cppmaintained 6 local string arrays mirroring enums fromleejet/stable-diffusion.cpp, each guarded by astatic_assert. Every upstream enum addition (e.g.SD_TYPE_NVFP4bumpingSD_TYPE_COUNT40→41) breaks the build at compile time.The upstream library already exposes all needed conversions in its public C API — use those instead.
Changes
gosd.cpp: Delete all 6 hand-maintained arrays (sample_method_str,schedulers,rng_type_str,prediction_str,lora_apply_mode_str,sd_type_str) and theirstatic_asserts (~100 lines). Replace all usages with upstream API calls:Enum→string lookups likewise replaced:
sample_method_str[x]→sd_sample_method_name((sample_method_t)x),schedulers[x]→sd_scheduler_name((scheduler_t)x).Makefile: BumpSTABLEDIFFUSION_GGML_VERSIONto1d6cb0f8c33ddadf1bff8aff40ec2e5b1ccb4940(the commit that introducedSD_TYPE_NVFP4).After this change, future upstream enum additions will never break the LocalAI build.
Original prompt
Problem
In
backend/go/stablediffusion-ggml/gosd.cpp, there are 6 hand-maintained string arrays that mirror enums from the upstreamleejet/stable-diffusion.cpplibrary, each guarded by astatic_assert:sample_method_str[]withstatic_assert(std::size(sample_method_str) == SAMPLE_METHOD_COUNT)schedulers[]withstatic_assert(std::size(schedulers) == SCHEDULER_COUNT)rng_type_str[]withstatic_assert(std::size(rng_type_str) == RNG_TYPE_COUNT)prediction_str[]withstatic_assert(std::size(prediction_str) == PREDICTION_COUNT)lora_apply_mode_str[]withstatic_assert(std::size(lora_apply_mode_str) == LORA_APPLY_MODE_COUNT)sd_type_str[]withstatic_assert(std::size(sd_type_str) == SD_TYPE_COUNT)Every time upstream adds a new enum value (e.g.
SD_TYPE_NVFP4 = 40bumpingSD_TYPE_COUNTfrom 40→41), thesestatic_asserts fail and the build breaks. This just happened in PR #9188.Solution
The upstream
stable-diffusion.cpplibrary already exposes all the necessary enum↔string conversion functions in its public C API (include/stable-diffusion.h):sd_type_tsd_type_name()str_to_sd_type()sample_method_tsd_sample_method_name()str_to_sample_method()scheduler_tsd_scheduler_name()str_to_scheduler()rng_type_tsd_rng_type_name()str_to_rng_type()prediction_tsd_prediction_name()str_to_prediction()lora_apply_mode_tsd_lora_apply_mode_name()str_to_lora_apply_mode()Required Changes
1. Update
backend/go/stablediffusion-ggml/gosd.cppDelete all 6 hand-maintained string arrays and their
static_asserts (approximately lines 31–130 in gosd.cpp).Replace all string-to-enum lookups that iterate over the local arrays with calls to the upstream
str_to_*()functions. For example:Replace all enum-to-string lookups like
sample_method_str[idx]with calls to the upstreamsd_*_name()functions:Apply the same pattern for all 6 enum types:
sample_method_str[x]→sd_sample_method_name((sample_method_t)x)schedulers[x]→sd_scheduler_name((scheduler_t)x)rng_type_str[x]→sd_rng_type_name((rng_type_t)x)prediction_str[x]→sd_prediction_name((prediction_t)x)lora_apply_mode_str[x]→sd_lora_apply_mode_name((lora_apply_mode_t)x)sd_type_str[x]→sd_type_name((sd_type_t)x)For string-to-enum:
sample_method_str→str_to_sample_method(str)(returnsSAMPLE_METHOD_COUNTif not found)schedulers→str_to_scheduler(str)(returnsSCHEDULER_COUNTif not found)rng_type_str→str_to_rng_type(str)(returnsRNG_TYPE_COUNTif not found)prediction_str→str_to_prediction(str)(returnsPREDICTION_COUNTif not found)lora_apply_mode_str→str_to_lora_apply_mode(str)(returnsLORA_APPLY_MODE_COUNTif not found)sd_type_str→str_to_sd_type(str)(returnsSD_TYPE_COUNTif not found)Make sure to carefully preserve all the existing logic and behavior — just remove the local arrays and use the upstream functions instead. The
*_COUNTsentinel values returned on failure are the same sentinel values the current code uses.2. Bump the stable-diffusion.cpp submodule version in the Makefile
In
backend/go/stablediffusion-ggml/Makefile, update:STABLEDIFFUSION_GGML_VERSION?=1d6cb0f8c33ddadf1bff8aff40ec2e5b1ccb4940This ensures the submodule is at the version that includes
SD_TYPE_NVFP4and thesd_type_name()/str_to_sd_type()functions.Result
After this change,
gosd.cppwill have zero local enum string arrays and will always stay in sync with upstream automatically. Future bumps of thestable-diffusion.cppsubmodule will never break the build due to added enum values.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: check the failure in the PR and the upstream changes that broke it
Assistant:
Let me investigate the PR failure and the upstream changes that caused it.
Assistant: Now let me look at the LocalAI code that consumes the upstream library to identify how the breaking changes affect it:
Assistant: Here's my analysis of the PR and the upstream breaking changes:
PR Summary
[PR #9188](https://github.com/mudler/LocalAI/pul...
This pull request was created from Copilot chat.