stable-diffusion.cpp/docs/development/c_api.md
leejet 532d2c15c4
Some checks failed
CI / Build and push container images (push) Has been cancelled
CI / ubuntu-latest-cmake (push) Has been cancelled
CI / ubuntu-latest-cmake-vulkan (push) Has been cancelled
CI / macOS-latest-cmake (push) Has been cancelled
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Has been cancelled
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Has been cancelled
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Has been cancelled
CI / windows-latest-rocm (push) Has been cancelled
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Has been cancelled
CI / release (push) Has been cancelled
refactor(api): adopt opaque options, tensors, and results (#2365)
2026-11-15 18:33:53 +08:00

63 lines
4 KiB
Markdown

# C API Internals
The public declarations live in [stable-diffusion.h](../../include/stable-diffusion.h).
See the [C API guide](../guides/c_api.md) for keys, value types and ownership rules.
## Internal modules
| Module | Responsibility |
| --- | --- |
| `src/api/options.h/.cpp` | Opaque options, key registration, setter validation and conversion to typed configuration. |
| `src/config.h/.cpp` | Typed configuration and the library defaults for each configuration group. |
| `src/api/tensor.h/.cpp` | Opaque tensor handles backed by `sd::Tensor<T>`, reference counting and conversion to private media views. |
| `src/core/media.h` | Private raw media views used by inference and codecs. |
| `src/api/result.h/.cpp` | Internal result storage, opaque result accessors, metadata attachment and media ownership. |
| `src/api/api.h/.cpp` | Context, generation, callback and query entry points, plus thread-local diagnostics and exception-to-status handling. |
| `src/generation_info.h/.cpp` | Construction of generation metadata JSON. |
| `src/pipeline/generation.h` | Internal generation entry declarations. |
| `src/stable-diffusion.cpp` | Internal typed image/video generation and model-specific validation. |
| `src/core/option_types.h/.cpp` | Internal mode enums, name lookup and parsing. |
Options are validated and converted once at the API boundary. The runtime reads
typed configuration fields. Internal callers such as ADetailer pass typed
configuration directly to the internal generation entry; they do not rebuild
options or call public setters.
The option map alone records whether a key was supplied. Zero, false, empty
strings and explicit automatic choices remain entries until `unset` removes
them. Each conversion starts with a fresh `OptConfig`, then overlays supplied
values and validates combinations. Reusing the output configuration cannot
retain a previous request's overrides. ADetailer applies its operation-specific
strength default before the overlay.
Fixed defaults live in the configuration constructors. Internal automatic
policies (such as model-selected sampler/scheduler and automatic eta/flow shift)
are resolved by the pipeline after refreshing the model sources. Their internal
sentinel values describe an automatic policy, not the presence of an input key.
Do not infer presence by comparing a typed value against its default.
CLI/server inference inputs use `std::optional`; their option builders emit
only engaged fields. Recipe defaults fill missing startup inputs; request
fields then override the configured recipe/startup values. Explicit empty
numeric arrays replace inherited arrays, and JSON `null` clears an optional
input. Frontend-only settings such as output formats and listen ports keep
local defaults. Frontend checks cover request syntax, file inputs and server
resource policy. Parameter ranges are checked by the public setters used by the
option builders; combinations and model applicability are checked when executing.
Examples include only public library headers and do not query library defaults.
Capability defaults describe configured overrides; null means that the library
selects the value, and result metadata reports effective values. Post-generation
ADetailer clears inherited dimensions and strength before applying pass overrides,
so its operation defaults also come from the library.
Result storage owns tensors and metadata. Tensor handles contain a dtype-specific
`sd::Tensor<T>` and an atomic reference count; options retain handles rather than
copying tensor payloads. Raw pipeline output buffers are converted to tensors at
the boundary and released there. Internal producers with an `sd::Tensor<T>` can
move it directly into a handle. Video results keep per-frame tensors and assemble
a contiguous video tensor only when requested. Cached assembly is synchronized.
Preview tensors are independent snapshots and may be retained by callbacks.
Metadata construction is separate from its attachment to a result.
These headers are private implementation details and are not installed as part
of the public C API.