Program not working on GPU but works on CPU

Hmm… Perhaps LoRA loading issue…?

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

#pipeline.enable_model_cpu_offload()

# 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