Stable Diffusion Batch Görsel Üretimi: Sıkıntı Çözümü İçin Senaryo

ufuk_c

Yeni Üye
Katılım
Eyl 2026
Konular
1
Mesajlar
1
Tepki puanı
0
Görüntülenme
0

Senaryo: 1000 adet görsel yüksek kalitede üretmek​


  1. Çevre hazırlığı
  • Ubuntu 22.04, RTX 3090, 24 GB NVMe.
  • pip install torch==2.2.0 torchvision diffusers transformers accelerate
  • conda create -n sd bash ve conda activate sd.
  1. Prompt listesi oluşturma
    Bash:
    prompts.txt
    A serene forest in autumn, cinematic lighting, 4K, 8k
    A futuristic cyberpunk city at night, neon glow, photorealistic
    ...
  2. Batch script (batch_generate.py)
    Python:
    import torch
    from diffusers import StableDiffusionPipeline
    from torch.utils.data import DataLoader, IterableDataset
    
    class PromptDataset(IterableDataset):
        def __init__(self, file):
            with open(file) as f:
                self.prompts = [l.strip() for l in f if l.strip()]
    
        def __iter__(self):
            return iter(self.prompts)
    
    device = torch.device("cuda")
    pipe = StableDiffusionPipeline.from_pretrained(
        "runwayml/stable-diffusion-v1-5",
        torch_dtype=torch.float16,
        safety_checker=None,
    ).to(device)
    
    # Bellek‑verimli ayar
    pipe.enable_xformers_memory_efficient_attention()
    pipe.enable_vae_tiling()
    pipe.enable_model_cpu_offload()
    
    loader = DataLoader(PromptDataset("prompts.txt"), batch_size=4, drop_last=False)
    
    for batch in loader:
        try:
            with torch.autocast("cuda"):
                images = pipe(batch, num_inference_steps=50, guidance_scale=7.5).images
            for idx, img in enumerate(images):
                img.save(f"output_{loader.dataset.prompts.index(batch[idx])}.png")
        except RuntimeError as e:
            if "out of memory" in str(e):
                torch.cuda.empty_cache()
                print("OOM, batch düşürülüyor...")
                # Kısa yeniden deneme
                pipe.unet.to("cpu")
                pipe.unet.to(device)
  3. İş akışı kontrolü
  • nvidia-smi ile GPU hafızasını izleyin.
  • torch.backends.cuda.matmul.allow_tf32=True ile TF32 hızını açın.
  • Gerekirse pipe.unet.config.sample boyutunu 512→256 kadar düşürün.
  1. CLI alternatif (sd-webui)
    Bash:
    python launch.py --prompt-file prompts.txt --n-prompt 1 --n-img 1 --batch-size 4
  2. Hata tespiti
  • RuntimeError: CUDA out of memory → batchÇokKüçült, torch.cuda.empty_cache() çağır.
  • diffusers log dosyasının son satırları, “Pipeline was not executed” → torch.compile denemek.

Özet​


Batch üretim, GPU bellek yönetiminde doğru ayarlarla (xformers, tiling, offload) sorunsuz gerçekleşebilir. Hata alındığında, akıllı bir “try‑except” bloğu ile batch küçültmek, bellek sıfırlama ve pipeline yeniden yükleme yöntemleri uygulanır. Bu yapı, ileri seviye kullanıcıların yoğun görsel üretim senaryolarında karşılaşabileceği sıkıntıları çözmek için gerekli adımları içerir.

Soru: Siz hangi bellek‑verimli teknikleri kullanarak en yüksek throughput elde ediyorsunuz?

İlgili konular​

 
Geri
En Üst