Senaryo: 1000 adet görsel yüksek kalitede üretmek
- Çevre hazırlığı
- Ubuntu 22.04, RTX 3090, 24 GB NVMe.
pip install torch==2.2.0 torchvision diffusers transformers accelerateconda create -n sd bashveconda activate sd.
- 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 ... - 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) - İş akışı kontrolü
nvidia-smiile GPU hafızasını izleyin.torch.backends.cuda.matmul.allow_tf32=Trueile TF32 hızını açın.- Gerekirse
pipe.unet.config.sampleboyutunu 512→256 kadar düşürün.
- CLI alternatif (sd-webui)
Bash:python launch.py --prompt-file prompts.txt --n-prompt 1 --n-img 1 --batch-size 4 - Hata tespiti
RuntimeError: CUDA out of memory→ batchÇokKüçült,torch.cuda.empty_cache()çağır.diffuserslog dosyasının son satırları, “Pipeline was not executed” →torch.compiledenemek.
Ö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?