Back to Blog
Generative AI February 25, 2025 10 min read

ControlNet + Stable Diffusion: Production-Grade Image Generation

How I deployed Stable Diffusion with ControlNet at Ofoto — architecture decisions, API design, prompt engineering, and handling 100+ concurrent requests.

Architecture Overview

Runpod serverless endpoint + Redis queue + FastAPI router

ControlNet Pipeline

from diffusers import StableDiffusionControlNetPipeline, ControlNetModel

controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11p_sd15_canny')
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    'runwayml/stable-diffusion-v1-5',
    controlnet=controlnet,
    torch_dtype=torch.float16,
).to('cuda')

Production Optimizations

  1. torch.compile() — 35% speedup on A100
  2. xformers attention — 40% VRAM reduction
  3. Batched inference — process 4 images simultaneously
  4. NSFW filter — required for public APIs

Prompt Engineering for Consistency

Always include quality tokens: masterpiece, best quality, 8k, detailed

Stable DiffusionControlNetDiffusersProductionFastAPI
O

Ossama Elhakki

AI Engineer & ML Systems Builder — Morocco