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: |
'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 |
None
|
Source code in src/imagewright/sdxl_server.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
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 | |
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 |
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 | |
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
|
|
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 | |
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: |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A |
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 | |
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]
|
|
Source code in src/imagewright/sdxl_server.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
parse_size(size)
¶
Parse "WIDTHxHEIGHT" into a (width, height) pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
str | None
|
Size string such as |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The parsed pair, or :data: |
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 | |
require_extras()
¶
Check that the SDXL server can run here.
Raises:
| Type | Description |
|---|---|
MissingExtraError
|
Naming |
Source code in src/imagewright/sdxl_server.py
55 56 57 58 59 60 61 62 | |
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 | |