mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-10 19:06:33 +00:00
refactor: centralize model factories and defaults (#2198)
This commit is contained in:
1 parent
baf45b49e2
commit
cd7f986058
50 files changed
+2233
-943
No files matched your search
+112
-2
@@ -1,7 +1,7 @@
|
||||
# Model Configuration Conventions
|
||||
|
||||
This document describes the conventions for model configuration structs and
|
||||
weight-based configuration detection.
|
||||
This document describes model configuration structs, weight-based configuration
|
||||
detection, and the definitions used to integrate models into the runtime.
|
||||
|
||||
## Config Types
|
||||
|
||||
@@ -116,3 +116,113 @@ struct.
|
||||
If a model has no weight-derived config today, it may still provide
|
||||
`detect_from_weights` for API consistency, but it should not print a config
|
||||
detection log unless it actually derives values from weights.
|
||||
|
||||
## Model Integration Definitions
|
||||
|
||||
Every `SDVersion` has an explicit `ModelDefinition` in
|
||||
`src/model/integrations/`. Related variants share an integration file and runner
|
||||
factory. Network blocks and their weight-derived configuration remain under
|
||||
`src/model/diffusion/`; reusable text encoder implementations remain under
|
||||
`src/model/te/`. Weight-name conversion remains in `src/name_conversion.cpp`.
|
||||
|
||||
To add a model:
|
||||
|
||||
1. Add its identity to `SDVersion` without renumbering existing values.
|
||||
2. Implement the network and `*Config::detect_from_weights` following the
|
||||
conventions above, or reuse an existing runner.
|
||||
3. Add a definition in its integration file, including its component
|
||||
factory, traits, geometry, and sampling policy.
|
||||
4. Add the family's explicit registration in `src/model_registry.cpp`. For a new
|
||||
variant of a registered family, add its definition to that family's list.
|
||||
5. Add version detection signatures and any required weight-name mappings in
|
||||
`src/model_loader.cpp` and `src/name_conversion.cpp` respectively. Update shared
|
||||
conditioning or runtime code only when existing behavior cannot serve the model.
|
||||
|
||||
Use the existing family integration as the starting point for a variant. For a
|
||||
new family, `src/model/integrations/moss_image.cpp` is a compact example of a
|
||||
conditioner/diffusion factory and its definition. Definitions collect model
|
||||
construction and defaults; they do not replace version detection, name conversion,
|
||||
or model-specific inference algorithms.
|
||||
|
||||
The registry checks for missing, duplicate, and invalid version registrations.
|
||||
Explicit references keep integrations reachable in static-library builds. The
|
||||
existing CMake glob includes integration sources automatically. No legacy
|
||||
builder, display-name table, or prediction-default fallback needs updating.
|
||||
|
||||
### Detection And Weight Conversion
|
||||
|
||||
Version detection remains in `ModelLoader::get_sd_version()` in
|
||||
`src/model_loader.cpp`. Add model signatures there and preserve the order of
|
||||
overlapping checks. Detection uses tensor names and metadata, and can run before
|
||||
and after name conversion, so signatures must work at both stages. Do not load
|
||||
tensor data to identify an architecture. The registry looks up definitions by
|
||||
the detected version; it does not perform detection.
|
||||
|
||||
Weight-name mappings and their version-based dispatch remain centralized in
|
||||
`src/name_conversion.cpp`. Add new mappings there when needed. Inference, GGUF
|
||||
export, quantization, and LoRA conversion share the `convert_tensor_name` entry
|
||||
point; preserve its prefix normalization and component conversion order.
|
||||
|
||||
### Components And Conditioning
|
||||
|
||||
The core factory receives `model_builders::Context`, the tokenizer configuration,
|
||||
and the output runners. Backend checks, optional IP-Adapter setup, scale
|
||||
overrides, and tokenizer usage validation remain in the common builder. A
|
||||
factory returns failure if any required additional backend cannot initialize.
|
||||
|
||||
LLM architecture selection, tokenizer requirements, prompt preparation, and
|
||||
encoding remain centralized in `src/conditioning/conditioner.hpp`. Integration
|
||||
factories instantiate the conditioner with the model version; models requiring
|
||||
new LLM conditioning behavior still need changes in that shared implementation.
|
||||
|
||||
`vae_kind` is the single source of truth for the VAE family; compatibility
|
||||
helpers derive their results from it. `KL`, `FLUX`, and `FLUX2` share the
|
||||
`AutoEncoderKL` implementation while retaining their distinct latent formats.
|
||||
The related traits have separate responsibilities:
|
||||
|
||||
- `supports_video_generation` describes the model's video generation capability.
|
||||
- `video_tae` selects `TinyVideoAutoEncoder` instead of `TinyImageAutoEncoder`.
|
||||
Image models can use a video TAE, so this is independent of video generation.
|
||||
- `fake_vae` selects FakeVAE for pixel-space models. It is separate from
|
||||
`vae_kind`, which also controls latent-format compatibility helpers.
|
||||
- `vae_format_override` allows PiD's VAE format override.
|
||||
|
||||
`configure_vae` handles model-specific KL adjustments such as
|
||||
PixArt's scale and SDXL's convolution scale. VAE compatibility traits also drive
|
||||
latent transformations and previews through the existing helper functions.
|
||||
|
||||
### Sampling And Geometry
|
||||
|
||||
`ModelSamplingPolicy` groups the defaults first: `default_prediction`,
|
||||
`default_flow_shift`, `default_sample_method`, and `default_scheduler`. Set only
|
||||
the values that differ from the struct defaults. `prediction_from_weights` and
|
||||
`flow_shift_from_weights` preserve checkpoint-dependent defaults during automatic
|
||||
prediction selection; `probe_v_prediction` enables SD2's runtime v-prediction
|
||||
probe. Explicit prediction settings take precedence over automatic selection.
|
||||
MOSS's default unconditional skip layer stays
|
||||
in the diffusion engine, with active unconditional SLG taking precedence.
|
||||
|
||||
The resolved `prediction_t` selects the denoiser directly. Flux3, MiniMax H3 AV,
|
||||
and Ming-Image use `FLUX3_FLOW_PRED`, `H3_AV_FLOW_PRED`, and
|
||||
`MING_IMAGE_FLOW_PRED`; their CLI overrides are `flux3_flow`, `h3_av_flow`, and
|
||||
`ming_image_flow`. Explicit `sd3_flow` always selects `DiscreteFlowDenoiser`,
|
||||
and `flux_flow` always selects `FluxFlowDenoiser`, regardless of model version.
|
||||
Automatic selection retains each model's default implementation. New prediction
|
||||
values are appended before `PREDICTION_COUNT`, preserving existing valid values;
|
||||
clients using the automatic-selection sentinel must rebuild with the new header.
|
||||
|
||||
`ModelGeometry` declares `diffusion_down_factor`, `latent_channels`, and
|
||||
`image_channels`. `ref_image_preset` defaults to `"default"`; set it only for models
|
||||
that need another reference-image preset.
|
||||
|
||||
The common pipeline executes these policies; specialized video, multimodal,
|
||||
cache, and extension algorithms remain in their existing runtime components.
|
||||
Model-specific diffusion extra parameters are assembled in the diffusion engine.
|
||||
Video frame conversion and alignment also remain in the diffusion engine.
|
||||
`get_vae_scale_factor()` retains PiD's pixel-space special case and otherwise
|
||||
uses the VAE's scale factor.
|
||||
|
||||
Validate migrations against the previous implementation using synthetic metadata,
|
||||
name conversions, prompt preparation, default/override selection, and focused
|
||||
builds. Real generation checks are still needed to validate numerical behavior
|
||||
when changing network or encoding algorithms.
|
||||
Reference in new issue
Block a user