-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
1746 lines (1502 loc) · 69.9 KB
/
server.py
File metadata and controls
1746 lines (1502 loc) · 69.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
224
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
273
274
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import base64
import glob
import os
import io
import re
import tempfile
from src.AutoEncoders.taesd import decode_latents_to_images
# Ensure we can import pipeline from this repo
import sys
import time
from typing import Any, Dict, List, Optional
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from src.Device.ModelCache import get_model_cache
from src.Core.Models.ModelFactory import list_available_models, list_available_controlnets
from src.FileManaging.ImageSaver import pop_image_bytes
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
# Logging setup
import asyncio
import logging
import uuid
from logging.handlers import RotatingFileHandler
# Create a module-level logger with rotating file handler and request-id support
class _RequestIdFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool: # pragma: no cover - simple utility
if not hasattr(record, "rid"):
record.rid = "-"
return True
def _setup_logger() -> logging.Logger:
os.makedirs("./logs", exist_ok=True)
logger = logging.getLogger("lightdiffusion.server")
if logger.handlers:
return logger
level_name = os.getenv("LD_SERVER_LOGLEVEL", "DEBUG").upper()
try:
level = getattr(logging, level_name, logging.DEBUG)
except Exception: # pragma: no cover
level = logging.DEBUG
logger.setLevel(level)
formatter = logging.Formatter(
fmt="%(asctime)s | %(levelname)s | %(name)s | rid=%(rid)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
file_handler = RotatingFileHandler(
filename=os.path.join("./logs", "server.log"),
maxBytes=5 * 1024 * 1024,
backupCount=3,
encoding="utf-8",
)
file_handler.setFormatter(formatter)
file_handler.addFilter(_RequestIdFilter())
logger.addHandler(file_handler)
# Also log to stderr for interactive runs; avoid duplicate handlers if uvicorn config already propagates
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
stream_handler.addFilter(_RequestIdFilter())
logger.addHandler(stream_handler)
logger.propagate = False
return logger
logger = _setup_logger()
logger.debug("server module loaded; cwd=%s", os.getcwd())
# Record server start time for telemetry
SERVER_START_TS = time.time()
try:
# Import app_instance to control preview behavior during generation
from src.user import app_instance as _app_instance
from src.user.pipeline import pipeline
except Exception as e:
# Defer import error to runtime response for clarity
pipeline = None # type: ignore
_pipeline_import_error = e
logger.exception("Failed to import pipeline: %s", e)
else:
_pipeline_import_error = None
logger.info("Pipeline and app_instance imported successfully")
class GenerateRequest(BaseModel):
prompt: str
negative_prompt: Optional[str] = ""
width: int = 512
height: int = 512
num_images: int = 1
batch_size: int = 1
scheduler: str = "ays"
sampler: str = "dpmpp_sde_cfgpp"
steps: int = 20
hiresfix: bool = False
adetailer: bool = False
enhance_prompt: bool = False
img2img_mode: bool = False
img2img_image: Optional[str] = None
img2img_denoise: float = 0.75 # Denoising strength: 0=keep original, 1=full generation
stable_fast: bool = False
reuse_seed: bool = False
realistic_model: bool = False
enable_multiscale: bool = False
multiscale_preset: Optional[str] = "balanced"
multiscale_intermittent: bool = True
multiscale_factor: float = 0.5
multiscale_fullres_start: int = 10
multiscale_fullres_end: int = 8
keep_models_loaded: bool = True
enable_preview: bool = False
# Preview fidelity for this request: 'low' | 'balanced' | 'high' (default: balanced)
preview_fidelity: str = "balanced"
# CFG-free sampling parameters
cfg_free_enabled: bool = False
cfg_free_start_percent: float = 70.0
# Token Merging parameters
tome_enabled: bool = False
tome_ratio: float = 0.5
tome_max_downsample: int = 1
# Advanced CFG optimization parameters (batched_cfg enabled by default for 8% speedup)
batched_cfg: bool = True
dynamic_cfg_rescaling: bool = False
dynamic_cfg_method: str = "variance"
dynamic_cfg_percentile: float = 95.0
dynamic_cfg_target_scale: float = 7.0
adaptive_noise_enabled: bool = False
adaptive_noise_method: str = "complexity"
# Guidance
cfg_scale: float = 7.0
guidance_scale: Optional[float] = None
seed: Optional[int] = None # If provided >=0 we will reuse it
# Model Selection
model_path: Optional[str] = None
refiner_model_path: Optional[str] = None
refiner_switch_step: Optional[int] = None
# ControlNet
controlnet_enabled: bool = False
controlnet_model: Optional[str] = None
controlnet_strength: float = 1.0
controlnet_type: str = "canny"
# torch.compile optimization (mutually exclusive with stable_fast)
torch_compile: Optional[bool] = None
vae_autotune: Optional[bool] = None
# Weight quantization format: None, "fp8", or "nvfp4"
weight_quantization: Optional[str] = None
# FP8 inference (auto-gated to supported hardware: Ada Lovelace+)
fp8_inference: bool = False
class SettingsPreferencesRequest(BaseModel):
torch_compile: bool = False
vae_autotune: bool = False
app = FastAPI(title="LightDiffusion Server", version="1.0.0")
@app.get("/api/controlnets")
async def get_controlnets():
"""List available ControlNet models."""
try:
models = list_available_controlnets()
return {"models": models}
except Exception as e:
logger.exception("Failed to list controlnets")
raise HTTPException(status_code=500, detail=str(e))
@app.on_event("startup")
async def startup_event():
"""Capture event loop reference and start background worker."""
global _main_event_loop
_main_event_loop = asyncio.get_running_loop()
# Migrate legacy include/last_seed.txt into the JSON settings store on startup
try:
from src.Core.SettingsStore import migrate_from_last_seed_txt
migrated_seed = migrate_from_last_seed_txt()
if migrated_seed is not None:
logger.info("Migrated legacy include/last_seed.txt -> last_seed=%s", migrated_seed)
except Exception:
logger.exception("Failed to migrate legacy last_seed.txt on startup")
await _generation_buffer.start()
logger.info("Server startup complete, event loop captured for preview broadcasting")
# Helpful, user-friendly startup URL(s) so users know what to open in a browser.
try:
port = int(os.environ.get("PORT") or os.environ.get("UVICORN_PORT") or 7861)
except Exception:
port = 7861
try:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
except Exception:
local_ip = "127.0.0.1"
logger.info("Open the UI in a browser: http://localhost:%d/ (or on your network: http://%s:%d/)", port, local_ip, port)
# Batching buffer -----------------------------------------------------------
LD_MAX_BATCH_SIZE = int(os.getenv("LD_MAX_BATCH_SIZE", "4"))
LD_BATCH_TIMEOUT = float(os.getenv("LD_BATCH_TIMEOUT", "0.5"))
# If set to true (1/true), the worker will wait the coalescing timeout when
# there is a single candidate in a chosen group; otherwise singletons are
# processed immediately. Default is to process singletons immediately to
# favor throughput and avoid perceived "stuck" behavior.
LD_BATCH_WAIT_SINGLETONS = os.getenv("LD_BATCH_WAIT_SINGLETONS", "0").lower() in ("1", "true", "yes")
# Limit total number of images we will process in a single pipeline run when
# coalescing many requests into a group. If the sum of images across the group
# is larger than this, we will split the group into smaller chunks and run the
# pipeline sequentially to avoid memory pressure and downstream save failures.
LD_MAX_IMAGES_PER_GROUP = int(os.getenv("LD_MAX_IMAGES_PER_GROUP", "256"))
def _normalized_image_key(value: Optional[str]) -> str:
"""Return a stable image identity key for batching decisions."""
if not value:
return ""
if value.startswith("data:"):
# Data URLs should already be normalized to a temp file before enqueue,
# but keep a deterministic fallback in case this helper is called early.
return value[:128]
try:
return os.path.abspath(os.path.realpath(value))
except Exception:
return str(value)
def _effective_guidance_scale(req: "GenerateRequest") -> float:
"""Normalize guidance scale for batch signatures and pipeline calls."""
return float(req.cfg_scale if req.guidance_scale is None else req.guidance_scale)
def _has_running_loop() -> bool:
try:
asyncio.get_running_loop()
return True
except RuntimeError:
return False
class PendingRequest:
def __init__(self, req: GenerateRequest, request_id: str):
self.req = req
self.request_id = request_id
self.arrival = time.time()
self.future: asyncio.Future = asyncio.get_running_loop().create_future()
class GenerationBuffer:
def __init__(self):
self._pending: List[PendingRequest] = []
self._loop: Optional[asyncio.AbstractEventLoop] = None
self._lock: asyncio.Lock
self._new_request: asyncio.Event
# Prefetching state
self._prefetch_lock: asyncio.Lock
self._prefetch_task: Optional[asyncio.Task] = None
self._current_prefetch_path: Optional[str] = None
# Statistics
self._items_processed = 0
self._batches_processed = 0
self._requests_processed = 0
self._cumulative_wait_time = 0.0
self._last_batch_ts = 0.0
self._worker_task: Optional[asyncio.Task] = None
self._reset_async_primitives(asyncio.get_running_loop() if _has_running_loop() else None)
def _reset_async_primitives(self, loop: Optional[asyncio.AbstractEventLoop]) -> None:
"""Recreate loop-bound synchronization primitives.
Test runs can start the in-process server multiple times on different
event loops. The queue's Event/Lock objects must be recreated when the
owning loop changes to avoid cross-loop RuntimeError during teardown.
"""
self._loop = loop
self._lock = asyncio.Lock()
self._new_request = asyncio.Event()
self._prefetch_lock = asyncio.Lock()
self._prefetch_task = None
self._current_prefetch_path = None
async def start(self):
"""Start the background worker task."""
current_loop = asyncio.get_running_loop()
if self._loop is not current_loop:
self._reset_async_primitives(current_loop)
if self._worker_task is None or self._worker_task.done():
self._worker_task = asyncio.create_task(self._worker())
logger.info("GenerationBuffer worker task started")
async def enqueue(self, pending: PendingRequest) -> dict:
"""Add a request to the queue and wait for completion."""
async with self._lock:
self._pending.append(pending)
self._new_request.set()
# Wait for the worker to process this request
return await pending.future
async def _look_ahead_and_prefetch(self, current_batch_signature: tuple):
"""Analyze remaining queue and pre-load the next model if different."""
from src.user.pipeline import resolve_checkpoint_path
async with self._lock:
if not self._pending:
return
# Find the next group that has a different signature
next_req = None
for p in self._pending:
sig = self._signature_for(p.req)
if sig != current_batch_signature:
next_req = p.req
break
if not next_req:
return
# Resolve the path for the next model
target_path = resolve_checkpoint_path(
realistic_model=next_req.realistic_model
)
# Perform prefetch outside the queue lock
async with self._prefetch_lock:
# Skip if already prefetched or currently prefetching the same path
if target_path == self._current_prefetch_path:
return
# Cancel existing prefetch if it's for a different model
if self._prefetch_task and not self._prefetch_task.done():
self._prefetch_task.cancel()
try:
await self._prefetch_task
except asyncio.CancelledError:
pass
self._current_prefetch_path = target_path
async def prefetch_task():
try:
logger.info("Prefetcher: Starting background load of %s", target_path)
# Load to CPU RAM using the optimized util
sd = await asyncio.to_thread(util.load_torch_file, target_path)
# Store in cache
get_model_cache().set_prefetched_model(target_path, sd)
logger.info("Prefetcher: Successfully pre-loaded %s into RAM", target_path)
except Exception as e:
logger.warning("Prefetcher: Failed to pre-load %s: %s", target_path, e)
finally:
self._current_prefetch_path = None
self._prefetch_task = asyncio.create_task(prefetch_task())
def _signature_for(self, req: GenerateRequest) -> tuple:
# Grouping signature - requests must match these to be batched
# Detect model type to determine if refiner is relevant
from src.Core.Models.ModelFactory import detect_model_type
is_sdxl = (detect_model_type(req.model_path) == "SDXL")
guidance_scale = _effective_guidance_scale(req)
normalized_img2img_image = _normalized_image_key(req.img2img_image)
return (
str(req.model_path), # Model must match
bool(req.realistic_model),
int(req.width),
int(req.height),
int(max(1, req.batch_size)),
bool(req.stable_fast),
bool(req.torch_compile),
bool(req.vae_autotune),
bool(req.fp8_inference),
str(req.weight_quantization),
bool(req.img2img_mode),
normalized_img2img_image,
float(req.img2img_denoise),
str(req.scheduler),
str(req.sampler),
int(req.steps),
float(guidance_scale),
bool(req.enhance_prompt),
bool(req.reuse_seed),
bool(req.enable_preview),
str(req.preview_fidelity),
# Treat multiscale options as batch-level — mixing them may
# change the sampling schedule and therefore cannot be
# safely combined into a single forward pass.
bool(req.enable_multiscale),
bool(req.multiscale_intermittent),
float(req.multiscale_factor),
int(req.multiscale_fullres_start),
int(req.multiscale_fullres_end),
bool(req.cfg_free_enabled),
float(req.cfg_free_start_percent),
bool(req.tome_enabled),
float(req.tome_ratio),
int(req.tome_max_downsample),
bool(req.batched_cfg),
bool(req.dynamic_cfg_rescaling),
str(req.dynamic_cfg_method),
float(req.dynamic_cfg_percentile),
float(req.dynamic_cfg_target_scale),
bool(req.adaptive_noise_enabled),
str(req.adaptive_noise_method),
# VRAM retention flags are also batch level
bool(req.keep_models_loaded),
# ControlNet (must match)
bool(req.controlnet_enabled),
str(req.controlnet_model),
float(req.controlnet_strength),
str(req.controlnet_type),
# Refiner (must match only if it will actually be used)
str(req.refiner_model_path) if is_sdxl else "",
(int(req.refiner_switch_step) if req.refiner_switch_step is not None else -1) if is_sdxl else -1,
# Note: hires_fix and adetailer remain intentionally NOT part of
# this signature because they are executed per-sample.
)
async def _worker(self):
logger.info("Batching worker started; max_batch=%s timeout=%s", LD_MAX_BATCH_SIZE, LD_BATCH_TIMEOUT)
while True:
await self._new_request.wait()
# Small throttle to coalesce multiple arrivals
await asyncio.sleep(0)
async with self._lock:
if not self._pending:
self._new_request.clear()
continue
# Group pending requests by signature
groups: Dict[tuple, List[PendingRequest]] = {}
for p in self._pending:
sig = self._signature_for(p.req)
groups.setdefault(sig, []).append(p)
# Choose the group with the oldest request
chosen_sig = None
oldest_time = float("inf")
for sig, arr in groups.items():
if arr and arr[0].arrival < oldest_time:
chosen_sig = sig
oldest_time = arr[0].arrival
if chosen_sig is None:
self._new_request.clear()
continue
candidates = groups[chosen_sig]
# Sort by arrival time (oldest first)
candidates.sort(key=lambda x: x.arrival)
# Debug: show group sizes for observability
try:
group_summary = {str(sig): len(arr) for sig, arr in groups.items()}
logger.debug("Batch worker: pending groups=%s chosen_sig=%s group_size=%d oldest_arrival=%.3f",
group_summary, str(chosen_sig), len(candidates), candidates[0].arrival if candidates else 0.0)
except Exception:
pass
# Determine whether to wait for coalescing when there's only a
# single candidate. This is controlled by LD_BATCH_WAIT_SINGLETONS
# so operators can toggle the behavior at runtime via env.
if len(candidates) == 1:
age = time.time() - candidates[0].arrival
if LD_BATCH_WAIT_SINGLETONS and age < LD_BATCH_TIMEOUT:
# Old behavior: wait a bit for more arrivals before
# processing a singleton so we can form a larger batch.
logger.debug("Singleton group for signature %s is too new (age=%.3fs < timeout=%.3fs). Sleeping to coalesce.", str(chosen_sig), age, LD_BATCH_TIMEOUT)
self._new_request.clear()
await asyncio.sleep(LD_BATCH_TIMEOUT)
continue
else:
# Eager processing path (default): process singletons
# immediately to avoid perceived "stuck" behavior.
logger.debug("Processing singleton group for signature %s immediately (age=%.3fs). LD_BATCH_WAIT_SINGLETONS=%s",
str(chosen_sig), age, LD_BATCH_WAIT_SINGLETONS)
# Keep ControlNet requests singleton for now. Its image-conditioned
# path has not been made batch-safe in the same way as text2img/img2img.
max_group_size = 1 if candidates[0].req.controlnet_enabled else LD_MAX_BATCH_SIZE
# Pick up to the allowed group size
to_process = candidates[:max_group_size]
# Remove selected items from pending list
for p in to_process:
try:
self._pending.remove(p)
except ValueError:
pass
if not self._pending:
self._new_request.clear()
# Trigger prefetching for the NEXT group while we process this one
await self._look_ahead_and_prefetch(chosen_sig)
# Process the selected group outside the lock
try:
try:
logger.debug("Processing group chosen_sig=%s items=%d request_ids=%s", str(chosen_sig), len(to_process), [p.request_id for p in to_process])
except Exception:
pass
await self._process_group(to_process)
# Update lightweight metrics only on success
try:
now_ts = time.time()
self._batches_processed += 1
self._items_processed += sum(
max(1, p.req.num_images) for p in to_process
)
self._requests_processed += len(to_process)
# Update cumulative wait time per-request
wait_total = sum(now_ts - p.arrival for p in to_process)
self._cumulative_wait_time += wait_total
self._last_batch_ts = now_ts
except Exception:
# Metrics must never crash the worker loop
logger.exception("Failed updating batch metrics")
except Exception as e:
logger.exception("Batch processing failed: %s", e)
async def _process_group(self, items: List[PendingRequest]):
# All items share a signature as enforced by the grouping logic.
if not items:
return
first_req = items[0].req
flat_samples: List[dict[str, Any]] = []
for p in items:
for _ in range(max(1, p.req.num_images)):
flat_samples.append(
{
"request_id": p.request_id,
"filename_prefix": f"LD-REQ-{p.request_id}",
"seed": p.req.seed if (p.req.seed is not None and p.req.seed >= 0) else None,
"hires_fix": bool(p.req.hiresfix),
"adetailer": bool(p.req.adetailer),
"prompt": p.req.prompt,
"negative_prompt": p.req.negative_prompt or "",
}
)
# Prepare pipeline kwargs based on the shared signature (take from first)
# Unique ID for this generation run; sent with every preview message
# so the frontend can discard stale previews from previous runs.
_gen_id = uuid.uuid4().hex[:12]
pipeline_kwargs = dict(
prompt=[],
w=first_req.width,
h=first_req.height,
number=0,
batch=0,
scheduler=first_req.scheduler,
sampler=first_req.sampler,
steps=first_req.steps,
cfg_scale=_effective_guidance_scale(first_req),
enhance_prompt=first_req.enhance_prompt,
img2img=first_req.img2img_mode,
img2img_denoise=first_req.img2img_denoise,
stable_fast=first_req.stable_fast,
reuse_seed=first_req.reuse_seed,
autohdr=True,
realistic_model=first_req.realistic_model,
model_path=first_req.model_path,
refiner_model_path=first_req.refiner_model_path,
refiner_switch_step=first_req.refiner_switch_step,
negative_prompt=[],
multiscale_preset=first_req.multiscale_preset,
enable_multiscale=first_req.enable_multiscale,
multiscale_factor=first_req.multiscale_factor,
multiscale_fullres_start=first_req.multiscale_fullres_start,
multiscale_fullres_end=first_req.multiscale_fullres_end,
multiscale_intermittent_fullres=first_req.multiscale_intermittent,
img2img_image=first_req.img2img_image,
request_filename_prefix=f"LD-REQ-{items[0].request_id}",
per_sample_info=[],
cfg_free_enabled=first_req.cfg_free_enabled,
cfg_free_start_percent=first_req.cfg_free_start_percent,
tome_enabled=first_req.tome_enabled,
tome_ratio=first_req.tome_ratio,
tome_max_downsample=first_req.tome_max_downsample,
# Advanced CFG optimizations (batched_cfg always enabled)
batched_cfg=first_req.batched_cfg,
dynamic_cfg_rescaling=first_req.dynamic_cfg_rescaling,
dynamic_cfg_method=first_req.dynamic_cfg_method,
dynamic_cfg_percentile=first_req.dynamic_cfg_percentile,
dynamic_cfg_target_scale=first_req.dynamic_cfg_target_scale,
adaptive_noise_enabled=first_req.adaptive_noise_enabled,
adaptive_noise_method=first_req.adaptive_noise_method,
# ControlNet
controlnet_model=first_req.controlnet_model if first_req.controlnet_enabled else None,
controlnet_strength=first_req.controlnet_strength,
controlnet_type=first_req.controlnet_type,
# torch.compile
torch_compile=first_req.torch_compile,
vae_autotune=first_req.vae_autotune,
# Weight quantization
weight_quantization=first_req.weight_quantization,
# FP8 inference
fp8_inference=first_req.fp8_inference,
# Add callback for WebSocket preview broadcasting
callback=make_server_callback(first_req.steps, generation_id=_gen_id),
)
# Notify clients that a new generation is starting so they can
# discard stale previews from the previous run.
sync_broadcast_preview(
step=0, total_steps=first_req.steps,
message_type="generation_start",
generation_id=_gen_id,
)
# Toggle preview state for the duration of the pipeline call
prev_preview_state = None
prev_keep_models_loaded = None
prev_preview_settings = None
try:
try:
prev_preview_state = _app_instance.app.previewer_var.get()
_app_instance.app.previewer_var.set(bool(first_req.enable_preview))
except Exception:
prev_preview_state = None
# Apply per-request preview fidelity overrides (format / quality / sRGB)
try:
prev_preview_settings = _apply_preview_fidelity_to_app(first_req)
except Exception:
prev_preview_settings = None
# Respect per-group model cache directive: toggle "keep loaded"
# so the sampling pipeline sees the requested caching behavior.
try:
model_cache = get_model_cache()
prev_keep_models_loaded = model_cache.get_keep_models_loaded()
model_cache.set_keep_models_loaded(bool(first_req.keep_models_loaded))
except Exception:
prev_keep_models_loaded = None
saved_map: Dict[str, List[dict]] = {}
total_images = len(flat_samples)
# Respect ImageSaver.MAX_IMAGES_PER_SAVE and the requested batch size.
# Multi-image runs always execute in deterministic chunks so that
# `batch_size` means "images per sampling pass" and `num_images`
# means "total outputs returned".
try:
from src.FileManaging import ImageSaver as _ImageSaver
_max_save_limit = getattr(_ImageSaver, "MAX_IMAGES_PER_SAVE", LD_MAX_IMAGES_PER_GROUP)
except Exception:
_max_save_limit = LD_MAX_IMAGES_PER_GROUP
max_save_limit = _max_save_limit if _max_save_limit and _max_save_limit > 0 else LD_MAX_IMAGES_PER_GROUP
requested_batch_size = max(1, int(first_req.batch_size))
max_chunk_size = min(requested_batch_size, LD_MAX_IMAGES_PER_GROUP, max_save_limit)
logger.info(
"Processing group of %d request(s) -> %d image(s) with effective batch_size=%d across %d chunk(s)",
len(items),
total_images,
max_chunk_size,
(total_images + max_chunk_size - 1) // max_chunk_size if max_chunk_size > 0 else 0,
)
chunks: list[list[dict[str, Any]]] = [
flat_samples[i : i + max_chunk_size]
for i in range(0, total_images, max_chunk_size)
]
try:
for chunk in chunks:
c_prompts = [entry["prompt"] for entry in chunk]
c_negatives = [entry["negative_prompt"] for entry in chunk]
c_per_sample_info = [
{
"request_id": entry["request_id"],
"filename_prefix": entry["filename_prefix"],
"seed": entry["seed"],
"hires_fix": entry["hires_fix"],
"adetailer": entry["adetailer"],
}
for entry in chunk
]
chunk_kwargs = dict(pipeline_kwargs)
chunk_kwargs["prompt"] = c_prompts
chunk_kwargs["negative_prompt"] = c_negatives
chunk_kwargs["number"] = len(c_prompts)
chunk_kwargs["batch"] = len(c_prompts)
chunk_kwargs["per_sample_info"] = c_per_sample_info
chunk_kwargs["request_filename_prefix"] = c_per_sample_info[0]["filename_prefix"] if c_per_sample_info else None
chunk_start_ts = time.time()
result = await asyncio.to_thread(pipeline, **chunk_kwargs)
if isinstance(result, dict) and "batched_results" in result:
for request_id, entries in result["batched_results"].items():
saved_map.setdefault(request_id, []).extend(entries)
else:
files = _find_images_since(chunk_start_ts)
for f in files:
name = os.path.basename(f)
for entry in chunk:
rid = entry["request_id"]
if f"LD-REQ-{rid}" in name:
saved_map.setdefault(rid, []).append({
"filename": name,
"subfolder": os.path.relpath(os.path.dirname(f), "./output"),
})
except InterruptedError:
logger.info(
"Generation interrupted for request_ids=%s",
[p.request_id for p in items],
)
sync_broadcast_preview(
step=0,
total_steps=first_req.steps,
message_type="error",
generation_id=_gen_id,
)
for p in items:
if not p.future.done():
p.future.set_exception(HTTPException(status_code=409, detail="Generation interrupted"))
return
# For each pending item, collect its images and set future result
for p in items:
imgs = saved_map.get(p.request_id, [])
# Filter and select the first N images requested
selected = imgs[: max(1, p.req.num_images)]
if not selected:
p.future.set_exception(HTTPException(status_code=500, detail="No images produced"))
continue
# Try to use in-memory byte buffer first (avoids disk I/O)
buffered_images = pop_image_bytes(f"LD-REQ-{p.request_id}")
b64_list = []
if buffered_images:
# Use in-memory bytes directly - zero disk reads
for buf_filename, buf_subfolder, png_bytes in buffered_images[:max(1, p.req.num_images)]:
b64_data = base64.b64encode(png_bytes).decode("utf-8")
mime_type = "image/png"
if buf_filename.lower().endswith((".jpg", ".jpeg")):
mime_type = "image/jpeg"
elif buf_filename.lower().endswith(".webp"):
mime_type = "image/webp"
b64_list.append(f"data:{mime_type};base64,{b64_data}")
else:
# Fallback to disk reads
for entry in selected:
if isinstance(entry, list):
# Safeguard against nested lists if any processor still returns them
entry = entry[0] if entry else {}
if not isinstance(entry, dict):
continue
filename = entry.get("filename", "")
path = os.path.join("./output", entry.get("subfolder", ""), filename)
try:
b64_data = _encode_png_to_base64(path)
mime_type = "image/png"
if filename.lower().endswith(".jpg") or filename.lower().endswith(".jpeg"):
mime_type = "image/jpeg"
elif filename.lower().endswith(".webp"):
mime_type = "image/webp"
b64_list.append(f"data:{mime_type};base64,{b64_data}")
except Exception as e:
logger.exception("Failed to read image for request %s: %s", p.request_id, e)
if len(b64_list) == 0:
p.future.set_exception(HTTPException(status_code=500, detail="Failed to read generated images"))
elif len(b64_list) == 1:
p.future.set_result({"image": b64_list[0]})
else:
p.future.set_result({"images": b64_list})
finally:
try:
if prev_preview_settings is not None:
_restore_preview_settings(prev_preview_settings)
except Exception:
pass
try:
if prev_preview_state is not None:
_app_instance.app.previewer_var.set(prev_preview_state)
except Exception:
pass
try:
# Restore previous model cache keep-loaded setting if we
# changed it above.
if prev_keep_models_loaded is not None:
try:
model_cache = get_model_cache()
model_cache.set_keep_models_loaded(bool(prev_keep_models_loaded))
except Exception:
pass
except Exception:
pass
# Instantiate the buffer and start it on startup
_generation_buffer = GenerationBuffer()
@app.on_event("startup")
async def _start_buffer():
await _generation_buffer.start()
@app.get("/health")
def health() -> Dict[str, str]:
return {"status": "ok"}
@app.get("/api/telemetry")
async def telemetry() -> Dict[str, Any]:
"""Return basic server and batching buffer telemetry.
Fields:
- uptime_seconds
- pending_count
- pending_by_signature (human-readable)
- pending_preview (list of small pending request summaries)
- worker_running
- max_batch_size, batch_timeout
- batches_processed, items_processed, last_batch_time
- pipeline_import_ok and pipeline_import_error
"""
rid = uuid.uuid4().hex[:8]
log = logging.LoggerAdapter(logger, {"rid": rid})
log.debug("telemetry requested")
now = time.time()
uptime = now - SERVER_START_TS
# Build a small snapshot of queue state under the buffer lock
async with _generation_buffer._lock:
pending_count = len(_generation_buffer._pending)
# Group pending requests by signature for visibility
sig_counts: Dict[str, int] = {}
pending_preview: List[Dict[str, Any]] = []
for p in _generation_buffer._pending:
try:
sig = _generation_buffer._signature_for(p.req)
sig_key = str(sig)
except Exception:
sig_key = "<unknown>"
sig_counts[sig_key] = sig_counts.get(sig_key, 0) + 1
# Keep preview small to avoid large payloads
preview = {
"request_id": p.request_id,
"waiting_s": round(now - p.arrival, 3),
"prompt_preview": (p.req.prompt[:120] + "…") if (p.req.prompt and len(p.req.prompt) > 120) else (p.req.prompt or ""),
}
pending_preview.append(preview)
batches_processed = _generation_buffer._batches_processed
items_processed = _generation_buffer._items_processed
last_batch_ts = _generation_buffer._last_batch_ts
worker_running = (
_generation_buffer._worker_task is not None
and (not _generation_buffer._worker_task.done())
)
# Compute average wait times
requests_processed = _generation_buffer._requests_processed
cumulative_wait = _generation_buffer._cumulative_wait_time
avg_processed_wait_s = (
(cumulative_wait / requests_processed) if requests_processed > 0 else None
)
# Pending average wait (current queue)
pending_avg_wait_s = (
(sum(now - p.arrival for p in _generation_buffer._pending) / pending_count)
if pending_count > 0
else 0.0
)
# Model cache telemetry (memory and loaded models)
memory_info_error = None
try:
model_cache = get_model_cache()
memory_info = model_cache.get_memory_info()
loaded_raw = model_cache.get_cached_sampling_models()
loaded_models = []
for m in loaded_raw:
try:
name = getattr(m, "name", None) or getattr(m, "__class__", type(m)).__name__
except Exception:
name = str(type(m))
loaded_models.append(name)
loaded_models_count = len(loaded_models)
except Exception as e:
# Don't fail telemetry if model cache query fails. Capture a short
# error string so callers can display a hint without exposing full
# stack traces. Device-side CUDA asserts can leave the device in an
# unusable state and will cause subsequent CUDA queries to fail; we
# surface a concise message here instead of crashing the endpoint.
try:
# Prefer a succinct message
memory_info_error = str(e)
except Exception:
memory_info_error = "unknown"
logger.exception("Failed to fetch model cache telemetry: %s", memory_info_error)
memory_info = None
loaded_models = []
loaded_models_count = 0
return {
"uptime_seconds": round(uptime, 3),
"server_start_ts": SERVER_START_TS,
"pending_count": pending_count,
"pending_by_signature": sig_counts,
"pending_preview": pending_preview[:20],
"worker_running": worker_running,
"max_batch_size": LD_MAX_BATCH_SIZE,
"batch_timeout": LD_BATCH_TIMEOUT,
"max_images_per_group": LD_MAX_IMAGES_PER_GROUP,
"batches_processed": batches_processed,
"items_processed": items_processed,
"requests_processed": requests_processed,
"last_batch_time": last_batch_ts,
"avg_processed_wait_s": avg_processed_wait_s,
"pending_avg_wait_s": pending_avg_wait_s,
"memory_info": memory_info,
"loaded_models_count": loaded_models_count,
"loaded_models": loaded_models,
"pipeline_import_ok": pipeline is not None,
"pipeline_import_error": str(_pipeline_import_error) if _pipeline_import_error is not None else None,
}
# Settings API ------------------------------------------------------------
def _read_settings_preferences() -> Dict[str, bool]:
from src.Core.SettingsStore import get_preferences
return get_preferences()
def _resolve_autotune_preferences(req: GenerateRequest) -> GenerateRequest:
prefs = _read_settings_preferences()
req.torch_compile = bool(prefs["torch_compile"] if req.torch_compile is None else req.torch_compile)
req.vae_autotune = bool(prefs["vae_autotune"] if req.vae_autotune is None else req.vae_autotune)
return req
def _reset_autotune_runtime_state() -> None:
"""Clear runtime model state so changed autotune preferences take effect."""
from src.Core.Pipeline import reset_default_pipeline
from src.Device.Device import clear_compiled_models
from src.Device.ModelCache import clear_model_cache
reset_default_pipeline()
clear_model_cache()
clear_compiled_models()
@app.get("/api/settings/preferences")
async def api_get_settings_preferences():
"""Return persisted server-wide generation preferences."""
try:
return _read_settings_preferences()
except Exception as e:
logger.exception("Failed to read settings preferences: %s", e)
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/settings/preferences")
async def api_post_settings_preferences(body: SettingsPreferencesRequest):
"""Persist server-wide generation preferences and reset runtime caches if needed."""
try:
from src.Core.SettingsStore import set_preferences
current = _read_settings_preferences()