Hello
I am trying to run this program on GPU, it generates a black image. On CPU it gives the perfect output.
The program is as follows:
import logging
from diffusers import AutoPipelineForText2Image, AutoencoderKL
import torch
import numpy as np
import random
import os
from PIL import Image
# =========================
# STEP 0: Logging Setup
# =========================
log_file = "generation_log.txt"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file),
logging.StreamHandler()
]
)
logging.info("Initializing...")
# =========================
# STEP 1: Environment Setup
# =========================
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
seed = random.randint(0, 9999999)
torch.manual_seed(seed)
np.random.seed(seed)
logging.info(f"Using seed: {seed}")
# ===============================
# STEP 2: Model and LoRA Setup
# ===============================
logging.info("Loading base model and LoRA weights...")
model_dir = "D:\\Ganu\\AIImage\\huggingface\\kohya_ss\\kohya_ss\\outputs"
lora_weights_path = os.path.join(model_dir, "model")
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
# Optional: Custom VAE (uncomment if needed)
# vae = AutoencoderKL.from_pretrained(
# "madebyollin/sdxl-vae-fp16-fix",
# torch_dtype=torch.float16
# ).to("cuda")
try:
pipeline = AutoPipelineForText2Image.from_pretrained(
model_id,
torch_dtype=torch.float16,
variant="fp16"
).to("cuda")
logging.info("Pipeline loaded to GPU with float16.")
except Exception as e:
logging.error(f"Failed to load model pipeline: {e}")
raise
# If using VAE:
# pipeline.vae = vae
pipeline.enable_attention_slicing()
pipeline.enable_vae_slicing()
try:
pipeline.load_lora_weights(lora_weights_path, weight_name="last.safetensors")
logging.info("LoRA weights loaded successfully.")
except ValueError as e:
logging.error("Invalid LoRA checkpoint. Check the format or compatibility.")
raise e
# =========================
# STEP 3: Prompt Inference
# =========================
text_prompt = (
"A wide, breathtaking landscape with all real vibrant nature-themed background, lush forests, mountains, and a Doctor standing prominently in the foreground"
)
negative_prompt = (
"text, letters, words, signage, logos, labels, writing, messy background, busy layout, clutter, double faces, abstract shapes, UI panels with words, overlapping elements, header, footer, top bar, navigation bar, bottom menu, toolbar, top text, website layout, browser frame, button row, page border, UI bar"
)
logging.info(f"Running inference with prompt: {text_prompt}")
try:
result = pipeline(
prompt=text_prompt,
negative_prompt=negative_prompt,
guidance_scale=7.5,
num_inference_steps=30
)
generated_image = result.images[0]
output_path = f"generated_image_{seed}.png"
generated_image.save(output_path)
logging.info(f"Image saved to: {output_path}")
generated_image.show()
except Exception as e:
logging.error(f"Error during image generation: {e}")
raise
The environment details are as follows
C:\Users\ADMIN>nvidia-smi
Wed May 14 15:17:51 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 566.36 Driver Version: 566.36 CUDA Version: 12.7 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce GTX 1650 WDDM | 00000000:01:00.0 On | N/A |
| N/A 62C P0 32W / 50W | 3833MiB / 4096MiB | 100% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 3008 C+G ...n\NVIDIA app\CEF\NVIDIA Overlay.exe N/A |
| 0 N/A N/A 6232 C+G ...b3d8bbwe\Microsoft.Media.Player.exe N/A |
| 0 N/A N/A 10308 C+G ...oogle\Chrome\Application\chrome.exe N/A |
| 0 N/A N/A 15020 C+G ...n\NVIDIA app\CEF\NVIDIA Overlay.exe N/A |
| 0 N/A N/A 16140 C+G C:\Windows\explorer.exe N/A |
| 0 N/A N/A 17036 C+G ...siveControlPanel\SystemSettings.exe N/A |
| 0 N/A N/A 17088 C+G ...oogle\Chrome\Application\chrome.exe N/A |
| 0 N/A N/A 17732 C+G ...CBS_cw5n1h2txyewy\TextInputHost.exe N/A |
| 0 N/A N/A 19012 C+G ...on\135.0.3179.98\msedgewebview2.exe N/A |
| 0 N/A N/A 19720 C+G ...t.LockApp_cw5n1h2txyewy\LockApp.exe N/A |
| 0 N/A N/A 20816 C+G ...2txyewy\StartMenuExperienceHost.exe N/A |
| 0 N/A N/A 20948 C+G ....Search_cw5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 21008 C+G ....Search_cw5n1h2txyewy\SearchApp.exe N/A |
| 0 N/A N/A 22108 C+G ...5n1h2txyewy\ShellExperienceHost.exe N/A |
| 0 N/A N/A 23296 C ...gface\kohya_ss\Python310\python.exe N/A |
| 0 N/A N/A 24012 C+G ...137.0_x64__dt26b99r8h8gj\RtkUWP.exe N/A |
+-----------------------------------------------------------------------------------------+
(venv) D:\Ganu\AIImage\project\Train-10Images-chatgptParameters\runs\1sstrun-23thApril2025\generation\1stGo>python
Python 3.10.10 (tags/v3.10.10:aad5f6a, Feb 7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(f"CUDA Available: {torch.cuda.is_available()}")
CUDA Available: True
>>> print(f"GPU Name: {torch.cuda.get_device_name(0)}")
GPU Name: NVIDIA GeForce GTX 1650
>>> print(f"PyTorch Version: {torch.__version__}")
PyTorch Version: 2.7.0+cu118
>>> print(f"CUDA Version: {torch.version.cuda}")
CUDA Version: 11.8
Any pointers?
P.S:
-
The GPU version was working before, but I cleaned my computer removing several apps possible some dlls and programs like Microsoft Visual Studio
-
I tried connecting with chatgpt and grok, but their suggestions made CPU work, but not the GPU
-
Logs donβt give any error