Skip to content

SDXL server (imagewright.sdxl_server)

imagewright.sdxl_server

SDXL-Lightning image server: the /v1/images/generations contract over diffusers.

A lighter alternative to :mod:imagewright.flux_server that runs on CUDA, MPS or CPU: SDXL base with a ByteDance SDXL-Lightning UNet (2, 4 or 8 steps). It answers the same routes, request fields and response bodies as gutenberg_kg's serve/sdxl_server.py.

Run it with imagewright serve --backend sdxl. It needs the serve and sdxl extras. torch and diffusers are imported only inside the functions that need them, so this module imports on a core install.

Environment variables

SdxlServer.from_env reads these.

SDXL_MODEL Lightning variant: sdxl_lightning_2 | _4 (default) | _8 SDXL_BASE SDXL base repo (default: stabilityai/stable-diffusion-xl-base-1.0) SDXL_OFFLINE 1 = never reach HuggingFace; fail if weights are not cached MPS_DTYPE float16 (default; uses the fp16-fix VAE) | float32 (heavier) IMAGE_OUTPUT_DIR Where response_format=filepath images go (default: imagewright_images in the system temp directory)

SdxlServer(*, model='sdxl_lightning_4', base=DEFAULT_BASE, offline=False, output_dir=None)

The pipeline and its settings behind the SDXL app.

One render runs at a time under :attr:lock; a diffusers pipeline is not safe to call from two threads at once.

Parameters:

Name Type Description Default
model str

Lightning variant, a key of :data:LIGHTNING_STEPS.

'sdxl_lightning_4'
base str

SDXL base repo.

DEFAULT_BASE
offline bool

Never reach HuggingFace; fail if the weights are not cached.

False
output_dir Path | None

Where response_format=filepath images go.

None
Source code in src/imagewright/sdxl_server.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def __init__(
    self,
    *,
    model: str = "sdxl_lightning_4",
    base: str = DEFAULT_BASE,
    offline: bool = False,
    output_dir: Path | None = None,
) -> None:
    self.model = model
    self.base = base
    self.offline = offline
    self.output_dir = output_dir or env_output_dir()
    self.pipe: Any = None
    self.lock = threading.Lock()

from_env() classmethod

Build a server from the environment variables in the module docstring.

Returns:

Type Description
SdxlServer

The configured server.

Source code in src/imagewright/sdxl_server.py
153
154
155
156
157
158
159
160
161
162
163
164
@classmethod
def from_env(cls) -> "SdxlServer":
    """Build a server from the environment variables in the module docstring.

    :returns: The configured server.
    """
    return cls(
        model=os.environ.get("SDXL_MODEL", "sdxl_lightning_4"),
        base=os.environ.get("SDXL_BASE", DEFAULT_BASE),
        offline=env_flag("SDXL_OFFLINE"),
        output_dir=env_output_dir(),
    )

load_pipeline()

Build the SDXL + Lightning-UNet pipeline once and cache it.

Weights are fetched on first run and cached under ~/.cache/huggingface unless :attr:offline is set.

Returns:

Type Description
Any

The diffusers pipeline.

Raises:

Type Description
MissingExtraError

When the sdxl extra is not installed.

Source code in src/imagewright/sdxl_server.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def load_pipeline(self) -> Any:
    """Build the SDXL + Lightning-UNet pipeline once and cache it.

    Weights are fetched on first run and cached under ``~/.cache/huggingface``
    unless :attr:`offline` is set.

    :returns: The diffusers pipeline.
    :raises MissingExtraError: When the ``sdxl`` extra is not installed.
    """
    if self.pipe is not None:
        return self.pipe

    torch = _torch()
    diffusers = _import_extra("diffusers", "sdxl")
    hf_hub_download = _import_extra("huggingface_hub", "sdxl").hf_hub_download
    load_file = _import_extra("safetensors.torch", "sdxl").load_file

    device, dtype = device_dtype()
    steps = steps_for(self.model)
    local_only = self.offline
    if not local_only:
        print("[startup] weights are fetched on first run and cached (~7 GB).", flush=True)
    print(f"[startup] loading {self.base} + {self.model} on {device}/{dtype} ...", flush=True)

    pipe = diffusers.StableDiffusionXLPipeline.from_pretrained(
        self.base, torch_dtype=dtype, local_files_only=local_only
    ).to(device)

    # In float16 the stock SDXL VAE overflows to black images; swap in the fp16-fix VAE.
    if dtype == torch.float16:
        print(f"[startup] loading fp16-fix VAE {VAE_FP16_FIX} ...", flush=True)
        pipe.vae = diffusers.AutoencoderKL.from_pretrained(
            VAE_FP16_FIX, torch_dtype=dtype, local_files_only=local_only
        ).to(device)

    filename = f"sdxl_lightning_{steps}step_unet.safetensors"
    unet_path = hf_hub_download(LIGHTNING_REPO, filename, local_files_only=local_only)
    state = load_file(unet_path, device=device)
    missing, unexpected = pipe.unet.load_state_dict(state, strict=False)
    if missing or unexpected:
        print(
            f"[startup] UNet load: missing={len(missing)} unexpected={len(unexpected)}",
            flush=True,
        )

    # Lightning wants Euler with trailing timestep spacing.
    pipe.scheduler = diffusers.EulerDiscreteScheduler.from_config(
        pipe.scheduler.config, timestep_spacing="trailing"
    )
    try:
        pipe.enable_attention_slicing()
        pipe.vae.enable_slicing()
    except (AttributeError, RuntimeError, NotImplementedError):
        pass

    self.pipe = pipe
    print("[startup] pipeline ready", flush=True)
    return pipe

render(prompt, negative_prompt, size, seed, steps)

Render one image under the lock.

Parameters:

Name Type Description Default
prompt str

Text description of the image.

required
negative_prompt str

What to steer away from.

required
size str | None

"WIDTHxHEIGHT"; one that does not parse gives 1024x1024.

required
seed int | None

Random seed, or None for a random one.

required
steps int | None

Used only when the model is not a known Lightning variant.

required

Returns:

Type Description
Image

The rendered image.

Source code in src/imagewright/sdxl_server.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def render(
    self,
    prompt: str,
    negative_prompt: str,
    size: str | None,
    seed: int | None,
    steps: int | None,
) -> PILImage:
    """Render one image under the lock.

    :param prompt: Text description of the image.
    :param negative_prompt: What to steer away from.
    :param size: ``"WIDTHxHEIGHT"``; one that does not parse gives 1024x1024.
    :param seed: Random seed, or None for a random one.
    :param steps: Used only when the model is not a known Lightning variant.
    :returns: The rendered image.
    """
    with self.lock:
        torch = _torch()
        width, height = parse_size(size)
        pipe = self.load_pipeline()
        device, _ = device_dtype()

        generator = None
        if seed is not None:
            try:
                generator = torch.Generator(device=device).manual_seed(int(seed))
            except (RuntimeError, TypeError):
                generator = None

        result = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            width=width,
            height=height,
            num_inference_steps=steps_for(self.model, steps),
            guidance_scale=1.0,  # Lightning: no CFG
            generator=generator,
        )
        image = result.images[0]

        # Return the allocator's freed blocks so idle memory drops back
        # between renders.
        if device == "mps":
            torch.mps.empty_cache()
        elif device == "cuda":
            torch.cuda.empty_cache()
        return image

create_app(server=None)

Build the FastAPI app.

On startup the app builds the pipeline (a no-op when server.pipe is already set), so the port opens only once the server can render. app.state.server is server.

Parameters:

Name Type Description Default
server SdxlServer | None

The pipeline and its settings. Default: :meth:SdxlServer.from_env.

None

Returns:

Type Description
Any

A fastapi.FastAPI app.

Source code in src/imagewright/sdxl_server.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def create_app(server: SdxlServer | None = None) -> Any:
    """Build the FastAPI app.

    On startup the app builds the pipeline (a no-op when ``server.pipe`` is
    already set), so the port opens only once the server can render.
    ``app.state.server`` is *server*.

    :param server: The pipeline and its settings. Default: :meth:`SdxlServer.from_env`.
    :returns: A ``fastapi.FastAPI`` app.
    """
    from fastapi import FastAPI  # noqa: PLC0415 - the `serve` extra
    from pydantic import BaseModel  # noqa: PLC0415 - the `serve` extra

    srv = server or SdxlServer.from_env()

    @asynccontextmanager
    async def lifespan(_app: FastAPI) -> AsyncGenerator[None]:
        srv.load_pipeline()
        yield

    app = FastAPI(title="imagewright SDXL-Lightning image server", lifespan=lifespan)
    app.state.server = srv

    class ImageGenRequest(BaseModel):
        model: str = "sdxl_lightning_4"
        prompt: str
        n: int = 1
        size: str = "1024x1024"
        quality: str | None = None
        num_inference_steps: int | None = None
        seed: int | None = None
        response_format: str = "b64_json"
        negative_prompt: str = "blurry, bad quality, distorted"

    @app.get("/health")
    def health():
        """Liveness probe.

        Does no model work, so it answers before the SDXL weights have been
        fetched or a pipeline built.
        """
        return {"status": "ok", "backend": "sdxl-lightning", "model": srv.model}

    @app.get("/v1/models")
    def list_models():
        """OpenAI-compatible single-model listing."""
        return {
            "object": "list",
            "data": [{"id": srv.model, "object": "model", "owned_by": "sdxl-lightning"}],
        }

    @app.post("/v1/images/generations")
    async def generate_image(req: ImageGenRequest):
        """Render in a worker thread and return the image as base64 or a file path."""
        image = await asyncio.to_thread(
            srv.render,
            req.prompt,
            req.negative_prompt,
            req.size,
            req.seed,
            req.num_inference_steps,
        )
        return image_reply(image, req.response_format, srv.output_dir)

    return app

device_dtype()

Resolve the device and dtype: CUDA float16, else MPS, else CPU float32.

MPS defaults to float16, which is safe because the fp16-fix VAE is swapped in; MPS_DTYPE=float32 forces the heavier full-precision path.

Returns:

Type Description
tuple[str, Any]

(device, torch dtype).

Source code in src/imagewright/sdxl_server.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def device_dtype() -> tuple[str, Any]:
    """Resolve the device and dtype: CUDA float16, else MPS, else CPU float32.

    MPS defaults to float16, which is safe because the fp16-fix VAE is swapped
    in; ``MPS_DTYPE=float32`` forces the heavier full-precision path.

    :returns: ``(device, torch dtype)``.
    """
    torch = _torch()
    if torch.cuda.is_available():
        return "cuda", torch.float16
    if getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
        want = os.environ.get("MPS_DTYPE", "float16").strip().lower()
        return "mps", (torch.float32 if want == "float32" else torch.float16)
    return "cpu", torch.float32

parse_size(size)

Parse "WIDTHxHEIGHT" into a (width, height) pair.

Parameters:

Name Type Description Default
size str | None

Size string such as "1024x1024" (case-insensitive x).

required

Returns:

Type Description
tuple[int, int]

The parsed pair, or :data:DEFAULT_DIMS when it does not parse to two positive ints.

Source code in src/imagewright/sdxl_server.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def parse_size(size: str | None) -> tuple[int, int]:
    """Parse ``"WIDTHxHEIGHT"`` into a ``(width, height)`` pair.

    :param size: Size string such as ``"1024x1024"`` (case-insensitive ``x``).
    :returns: The parsed pair, or :data:`DEFAULT_DIMS` when it does not parse to
        two positive ints.
    """
    if not size:
        return DEFAULT_DIMS
    try:
        w_str, h_str = size.lower().split("x", 1)
        width, height = int(w_str), int(h_str)
    except (ValueError, AttributeError):
        return DEFAULT_DIMS
    if width <= 0 or height <= 0:
        return DEFAULT_DIMS
    return width, height

require_extras()

Check that the SDXL server can run here.

Raises:

Type Description
MissingExtraError

Naming pip install "imagewright[serve,sdxl]".

Source code in src/imagewright/sdxl_server.py
55
56
57
58
59
60
61
62
def require_extras() -> None:
    """Check that the SDXL server can run here.

    :raises MissingExtraError: Naming ``pip install "imagewright[serve,sdxl]"``.
    """
    _require_modules(
        (*SERVE_MODULES, *_SDXL_MODULES, "huggingface_hub"), "serve,sdxl", "the SDXL server"
    )

steps_for(model, requested=None)

Resolve the inference step count.

SDXL-Lightning UNets are distilled for a fixed step count, so the model name wins over any per-request override: asking a 4-step UNet for 30 steps degrades the image rather than improving it.

Parameters:

Name Type Description Default
model str

Lightning variant name.

required
requested int | None

Per-request override, used only for an unknown variant.

None

Returns:

Type Description
int

Step count to run.

Source code in src/imagewright/sdxl_server.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def steps_for(model: str, requested: int | None = None) -> int:
    """Resolve the inference step count.

    SDXL-Lightning UNets are distilled for a fixed step count, so the model name
    wins over any per-request override: asking a 4-step UNet for 30 steps
    degrades the image rather than improving it.

    :param model: Lightning variant name.
    :param requested: Per-request override, used only for an unknown variant.
    :returns: Step count to run.
    """
    if model in LIGHTNING_STEPS:
        return LIGHTNING_STEPS[model]
    return requested if requested and requested > 0 else 4