← All notes

Text-to-image: diffusion fundamentals and local inference

Understand latent denoising and run a reviewable image-generation workflow with fixed prompts, seeds and parameters.

Basis of this note

Based on the Latent Diffusion paper and the official Hugging Face Diffusers API. The code is an instructional example, not run locally for this note and not evidence of a generative-model experiment. Check access, licenses and hardware requirements for the chosen checkpoint.

01 / What the model does during image generation

For a latent diffusion model such as Stable Diffusion XL, a text encoder first maps the prompt to conditioning vectors. A denoiser starts from random latent noise and predicts/removes noise across time steps scheduled by the sampler; a VAE decoder then maps the latent back to pixels. The model does not literally draw each word; it samples iteratively from a learned conditional distribution.

Working in latent space reduces the cost of high-resolution generation. Common inference controls include prompt, negative prompt, random seed, number of steps, guidance scale, resolution and scheduler. More steps add computation but do not guarantee monotonically better images; excessive guidance can create oversaturation or unnatural structure.

02 / Environment, checkpoint and first run

Use the official PyTorch selector to choose an install command for Windows, the GPU and CUDA version, then create an isolated Python environment and install Diffusers, Transformers, Accelerate and safetensors. Before using a checkpoint such as SDXL base, read its model card, license and access requirements. If model-host authentication is needed, keep the token in a local credential store.

For the first smoke test, use a smaller resolution and fewer steps to check dependencies, checkpoint download, memory and output path. CPU can test the workflow when a GPU is unavailable, but may be very slow; do not confuse an out-of-memory failure with a model-logic error.

  • Prepare a Python 3.10 environment and install a compatible PyTorch build using the official selector.
  • Install: python -m pip install diffusers transformers accelerate safetensors.
  • Confirm the model repository ID, revision/commit, license, access requirements and checkpoint format.
  • Fix the prompt, seed and resolution; save both the generated image and environment record.

03 / A minimal, reviewable SDXL example

The code uses a standard Diffusers pipeline. Before running, accept the selected checkpoint's license terms on its model host and complete any required authentication. Never place an access token in source code, notebook output or a public repository. This code has not been executed locally for this note.

python
from pathlib import Path
import torch
from diffusers import DiffusionPipeline

model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = DiffusionPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True,
).to("cuda")

prompt = (
    "editorial photograph of a quiet university research lab, "
    "a student examining a data visualization, soft morning window light, "
    "natural colors, documentary composition"
)
negative_prompt = "watermark, unreadable text, extra fingers, distorted face"
seed = 20261020
generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    num_inference_steps=30,
    guidance_scale=6.0,
    width=1024,
    height=1024,
    generator=generator,
).images[0]
output_dir = Path("outputs")
output_dir.mkdir(parents=True, exist_ok=True)
image.save(output_dir / "lab-scene.png")

SDXL checkpoints require substantial memory; check the license and hardware before the first run. The sample prompt is illustrative and has not been evaluated for image quality or research effectiveness.

04 / How to iterate on prompts systematically

Break a prompt into subject, action, setting, composition, lighting and constraints. Fix the seed, model revision and size. Change one factor at a time and log seed, scheduler, steps and guidance scale; changing model, prompt and parameters together makes the cause of a difference unclear. Inspect failure cases carefully for text, hands, precise spatial relations or specific individuals.

  • Prompt adherence: Do the subject, scene and composition match the request?
  • Image quality: Inspect structure, details, text artifacts, duplicated objects and bias.
  • License and provenance: Check the model card, output-use terms, likeness rights and trademarks.

A fixed seed helps compare changes under one configuration, but hardware, drivers, library versions and nondeterministic kernels can still change pixels. It does not guarantee bit-for-bit reproduction across machines.

Sources & further reading

Compiled from code and documents, with AI-assisted drafting and translation. Examples and proposed checks are learning material; only linked evidence establishes what was run.