sd_generate: Generate images (unified entry point)

View source: R/pipeline.R

sd_generateR Documentation

Generate images (unified entry point)

Description

Automatically selects the best generation strategy based on output resolution and available VRAM (set via vram_gb in sd_ctx). For txt2img, routes between direct generation, tiled sampling (MultiDiffusion), or highres fix. For img2img (when init_image is provided), routes between direct and tiled img2img.

Usage

sd_generate(
  ctx,
  prompt,
  negative_prompt = "",
  width = 512L,
  height = 512L,
  init_image = NULL,
  strength = 0.75,
  sample_method = SAMPLE_METHOD$EULER,
  sample_steps = 20L,
  cfg_scale = 7,
  seed = 42L,
  batch_count = 1L,
  scheduler = SCHEDULER$DISCRETE,
  clip_skip = -1L,
  eta = 0,
  hr_strength = 0.4,
  vae_mode = "auto",
  vae_tile_size = 64L,
  vae_tile_overlap = 0.25,
  cache_mode = c("off", "easy", "ucache"),
  cache_config = NULL
)

Arguments

ctx

SD context created by sd_ctx

prompt

Text prompt describing desired image

negative_prompt

Negative prompt (default "")

width

Image width in pixels (default 512)

height

Image height in pixels (default 512)

init_image

Optional init image for img2img. If provided, runs img2img instead of txt2img. Requires vae_decode_only = FALSE.

strength

Denoising strength for img2img (default 0.75). Ignored for txt2img.

sample_method

Sampling method (see SAMPLE_METHOD)

sample_steps

Number of sampling steps (default 20)

cfg_scale

Classifier-free guidance scale (default 7.0)

seed

Random seed (-1 for random)

batch_count

Number of images to generate (default 1)

scheduler

Scheduler type (see SCHEDULER)

clip_skip

Number of CLIP layers to skip (-1 = auto)

eta

Eta parameter for DDIM-like samplers

hr_strength

Denoising strength for highres fix refinement pass (default 0.4). Only used when auto-routing selects highres fix.

vae_mode

VAE processing mode: "normal", "tiled", or "auto" (VRAM-aware: queries free GPU memory and enables tiling only when estimated peak VAE usage exceeds available VRAM minus a 50 MB reserve). Default "auto".

vae_tile_size

Tile size for VAE tiling (default 64)

vae_tile_overlap

Overlap for VAE tiling (default 0.25)

cache_mode

Step caching mode: "off" (default), "easy" (EasyCache), or "ucache" (UCache).

cache_config

Optional fine-tuned cache config from sd_cache_params.

Details

When vram_gb is not set on the context, defaults to direct generation (equivalent to calling sd_txt2img or sd_img2img directly).

Value

List of SD images (or single image for highres fix path).

Examples

## Not run: 
# Simple — auto-routes based on detected VRAM
ctx <- sd_ctx("model.safetensors", model_type = "sd1",
              vae_decode_only = FALSE)
imgs <- sd_generate(ctx, "a cat", width = 2048, height = 2048)

# Manual override — force 4 GB VRAM limit
ctx4 <- sd_ctx("model.safetensors", model_type = "sd1",
               vram_gb = 4, vae_decode_only = FALSE)
imgs <- sd_generate(ctx4, "a cat", width = 2048, height = 2048)

## End(Not run)

sd2R documentation built on March 30, 2026, 5:08 p.m.