This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a C++ REST API server that wraps the stable-diffusion.cpp library, providing HTTP endpoints for Stable Diffusion image generation. The server is built with a modular architecture featuring three main components: HTTP Server, Generation Queue, and Model Manager.
# Create build directory and configure
mkdir build && cd build
cmake ..
# Build the project (parallel build)
cmake --build . --parallel
# Install (optional)
cmake --install .
# Build with CUDA support (default: ON)
cmake -DSD_CUDA_SUPPORT=ON ..
# Build without CUDA
cmake -DSD_CUDA_SUPPORT=OFF ..
# Debug build
cmake -DCMAKE_BUILD_TYPE=Debug ..
# Release build (default)
cmake -DCMAKE_BUILD_TYPE=Release ..
# Clean build artifacts
cd build
cmake --build . --target clean
# Or delete build directory entirely
rm -rf build
mkdir build && cd build
cmake ..
cmake --build . --parallel
Required Parameters:
Both --models-dir and --checkpoints are required.
# Basic usage with required parameters
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints checkpoints
# The above resolves checkpoints to: /path/to/models/checkpoints
# Using absolute path for checkpoints
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints /absolute/path/to/checkpoints
# With custom port and host
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints checkpoints --host 0.0.0.0 --port 8080
# With verbose logging
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints checkpoints --verbose
# With optional model directories (relative paths)
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints checkpoints --lora-dir lora --vae-dir vae
# With optional model directories (absolute paths)
./stable-diffusion-rest-server --models-dir /path/to/models --checkpoints checkpoints --lora-dir /other/lora --vae-dir /other/vae
Path Resolution Logic:
--models-dir--models-dir /data/models --checkpoints checkpoints → /data/models/checkpoints--models-dir /data/models --checkpoints /other/checkpoints → /other/checkpointsHTTP Server (src/server.cpp, include/server.h)
registerEndpoints()setupCORS()Generation Queue (src/generation_queue.cpp, include/generation_queue.h)
JobInfo structuresenqueueRequest(), getQueueStatus(), cancelJob()Model Manager (src/model_manager.cpp, include/model_manager.h)
cancelScan()Server::serverThreadFunction())g_running flag for graceful shutdownModel types are bit flags (can be combined):
enum class ModelType {
LORA = 1, CHECKPOINT = 2, VAE = 4, PRESETS = 8,
PROMPTS = 16, NEG_PROMPTS = 32, TAESD = 64,
ESRGAN = 128, CONTROLNET = 256, UPSCALER = 512,
EMBEDDING = 1024
};
Supported file extensions by type:
.safetensors, .pt, .ckpt.json, .yaml, .yml.txt, .json.pth, .ptPOST /api/v1/generate - General image generationPOST /api/v1/text2img - Text-to-image generationPOST /api/v1/img2img - Image-to-image generationPOST /api/v1/controlnet - ControlNet generationGET /api/v1/models - List all available modelsGET /api/v1/models/{type} - List models by typeGET /api/v1/models/{id} - Get model detailsPOST /api/v1/models/load - Load a modelPOST /api/v1/models/unload - Unload a modelPOST /api/v1/models/scan - Rescan models directoryGET /api/v1/queue - Get queue statusGET /api/v1/jobs/{id} - Get job statusDELETE /api/v1/jobs/{id} - Cancel a jobDELETE /api/v1/queue - Clear queueGET /api/v1/health - Health checkGET /api/v1/status - API statusGET /api/v1/system - System capabilitiesDependencies are managed in cmake/FindDependencies.cmake using CMake's FetchContent:
The stable-diffusion.cpp library is downloaded and built automatically via ExternalProject_Add() in the root CMakeLists.txt. The specific git tag is pinned to master-334-d05e46c.
Both GenerationQueue and ModelManager use the Pimpl (Pointer to Implementation) idiom:
class GenerationQueue {
private:
class Impl;
std::unique_ptr<Impl> pImpl;
};
All implementation details are in the .cpp files, not headers. When modifying these classes, update the inner Impl class definition in the .cpp file.
g_server allows signal handler to trigger graceful shutdowng_running atomic flag and calls server->stop()The server requires explicit configuration of model directories:
Required Parameters:
--models-dir: Base directory for models (required)--checkpoints: Checkpoints directory (required)Optional Parameters:
--lora-dir, --vae-dir, --controlnet-dir, etc. (optional)Path Resolution:
/absolute/path/to/models) are used as-ischeckpoints) are resolved relative to --models-dirresolveDirectoryPath() function in main.cpp handles this logicThe GenerationRequest structure in generation_queue.h contains all parameters from stable-diffusion.cpp's CLI including:
include/server.hsrc/server.cppServer::registerEndpoints()sendJsonResponse() and sendErrorResponse() for consistent responsesModelType in model_manager.hmodelTypeToString() and stringToModelType() in model_manager.cppServerConfig struct if a new directory parameter is neededGenerationRequest struct in generation_queue.hServer::validateGenerationParameters()StableDiffusionWrapper to pass new parameters to underlying librarystd::shared_mutex - multiple readers OR single writerstd::mutex and std::condition_variablestd::lock_guard, std::unique_lock, std::shared_lock)std::atomic<bool> for g_running, m_isRunningThe stable-diffusion.cpp library is built as an external project. Include directories and libraries are configured via the sd-cpp interface target:
target_link_libraries(stable-diffusion-rest-server PRIVATE
sd-cpp
ggml
ggml-base
ggml-cpu
${DEPENDENCY_LIBRARIES}
)
When accessing stable-diffusion.cpp APIs, include from the installed headers:
#include <stable-diffusion.h>#include <ggml.h>The wrapper class StableDiffusionWrapper (stable_diffusion_wrapper.cpp/h) encapsulates all interactions with the stable-diffusion.cpp library.
The server includes an automatic model architecture detection system that analyzes checkpoint files to determine their type and required auxiliary models.
The system can detect the following architectures:
| Architecture | Required Files | Command-Line Flags |
|---|---|---|
| SD 1.5 | VAE (optional) | --vae vae-ft-mse-840000-ema-pruned.safetensors |
| SD 2.1 | VAE (optional) | --vae vae-ft-ema-560000.safetensors |
| SDXL Base/Refiner | VAE (optional) | --vae sdxl_vae.safetensors |
| Flux Schnell | VAE, CLIP-L, T5XXL | --vae ae.safetensors --clip-l clip_l.safetensors --t5xxl t5xxl_fp16.safetensors |
| Flux Dev | VAE, CLIP-L, T5XXL | --vae ae.safetensors --clip-l clip_l.safetensors --t5xxl t5xxl_fp16.safetensors |
| Flux Chroma | VAE, T5XXL | --vae ae.safetensors --t5xxl t5xxl_fp16.safetensors |
| SD3 | VAE, CLIP-L, CLIP-G, T5XXL | --vae sd3_vae.safetensors --clip-l clip_l.safetensors --clip-g clip_g.safetensors --t5xxl t5xxl_fp16.safetensors |
| Qwen2-VL | Qwen2VL, Qwen2VL-Vision | --qwen2vl qwen2vl.safetensors --qwen2vl-vision qwen2vl_vision.safetensors |
File Format Support:
Detection Method:
double_blocks, single_blocks tensorsjoint_blocks tensorsconditioner, text_encoder_2 tensorsAPI Integration:
/api/models endpointrequired_models array listing needed auxiliary filesmissing_models array if dependencies are not foundDuring model scanning (model_manager.cpp):
if (detectedType == ModelType::CHECKPOINT) {
ModelDetectionResult detection = ModelDetector::detectModel(info.fullPath);
info.architecture = detection.architectureName;
info.recommendedVAE = detection.recommendedVAE;
info.recommendedWidth = std::stoi(detection.suggestedParams["width"]);
// ... parse other recommended parameters
// Build required models list
if (detection.needsVAE) {
info.requiredModels.push_back("VAE: " + detection.recommendedVAE);
}
}
{
"name": "chroma-unlocked-v50-Q8_0.gguf",
"type": "checkpoint",
"architecture": "Flux Chroma (Unlocked)",
"recommended_vae": "ae.safetensors",
"recommended_width": 1024,
"recommended_height": 1024,
"recommended_steps": 20,
"recommended_sampler": "euler",
"required_models": [
"VAE: ae.safetensors",
"T5XXL: t5xxl_fp16.safetensors"
],
"missing_models": [],
"has_missing_dependencies": false
}
A standalone test binary can be built to test model detection:
cd build
cmake -DBUILD_MODEL_DETECTOR_TEST=ON ..
cmake --build . --target test_model_detector
# Run tests
./src/test_model_detector /data/SD_MODELS/checkpoints
The server will automatically use the correct parameters when loading models based on detected architecture. For architectures requiring multiple auxiliary models (Flux, SD3, Qwen), the server will:
See MODEL_DETECTION.md for complete documentation on the detection system.