Download services/refine/diffusion.py from modventures/room-visualizer-v2: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/services/refine/diffusion.py
- Command line
-
hf download hf://spaces/modventures/room-visualizer-v2/services/refine/diffusion.py
-
curl -L -o diffusion.py https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/services/refine/diffusion.py
3.31 kB
| """The SDXL call itself. | |
| Kept deliberately thin: everything that can be reasoned about without a GPU | |
| lives in the sibling modules. What is here is the pipeline invocation, the | |
| resolution arithmetic it demands, and the out-of-memory path. | |
| """ | |
| from __future__ import annotations | |
| import time | |
| import torch | |
| from PIL import Image | |
| import state | |
| def get_processing_size(width: int, height: int, max_dimension: int) -> tuple[int, int]: | |
| """Fit within max_dimension, rounded to the multiple of 8 the VAE needs.""" | |
| scale = min(1.0, max_dimension / max(width, height)) | |
| sized = [] | |
| for value in (int(round(width * scale)), int(round(height * scale))): | |
| sized.append(max(64, int(round(value / 8.0)) * 8)) | |
| return sized[0], sized[1] | |
| def describe_vram(tag: str) -> str: | |
| if not torch.cuda.is_available(): | |
| return f"[VRAM {tag}] cpu" | |
| return ( | |
| f"[VRAM {tag}] allocated={torch.cuda.memory_allocated() / 1024 ** 3:.2f} GB " | |
| f"reserved={torch.cuda.memory_reserved() / 1024 ** 3:.2f} GB " | |
| f"peak={torch.cuda.max_memory_allocated() / 1024 ** 3:.2f} GB" | |
| ) | |
| class RefinementOutOfMemory(RuntimeError): | |
| """CUDA ran out of memory during a diffusion pass.""" | |
| def run_diffusion_pass( | |
| pipe, | |
| image: Image.Image, | |
| mask_image: Image.Image, | |
| depth_image: Image.Image, | |
| prompt: str, | |
| negative_prompt: str, | |
| profile: dict, | |
| width: int, | |
| height: int, | |
| seed: int, | |
| ) -> Image.Image: | |
| """Run one inpainting pass and return the raw generated image. | |
| Note that `strength` gates how many of `steps` actually execute, so a | |
| profile at strength 0.09 costs about two steps, not twenty-four. That is | |
| why a conservative material is so much cheaper than an aggressive one. | |
| """ | |
| generator = torch.Generator(device=str(state.device)).manual_seed(seed) | |
| started = time.perf_counter() | |
| if torch.cuda.is_available(): | |
| torch.cuda.synchronize() | |
| try: | |
| with torch.inference_mode(): | |
| generated = pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| image=image, | |
| mask_image=mask_image, | |
| control_image=depth_image, | |
| strength=profile["diffusion_strength"], | |
| guidance_scale=profile["guidance"], | |
| controlnet_conditioning_scale=profile["controlnet"], | |
| control_guidance_start=0.0, | |
| control_guidance_end=0.65, | |
| num_inference_steps=profile["steps"], | |
| height=height, | |
| width=width, | |
| generator=generator, | |
| ).images[0] | |
| except torch.cuda.OutOfMemoryError as exc: | |
| torch.cuda.empty_cache() | |
| raise RefinementOutOfMemory( | |
| f"CUDA out of memory at {width}x{height}. Lower refine.max_dimension " | |
| f"(try 768), or set refine.cpu_offload = true to trade speed for VRAM." | |
| ) from exc | |
| if torch.cuda.is_available(): | |
| torch.cuda.synchronize() | |
| print( | |
| f"[REFINE] diffusion pass {width}x{height} " | |
| f"strength={profile['diffusion_strength']} steps={profile['steps']} " | |
| f"took {time.perf_counter() - started:.2f}s", | |
| flush=True, | |
| ) | |
| print(describe_vram("after pass"), flush=True) | |
| return generated | |