| sd_ctx | R Documentation |
Loads a model and creates a context for image generation.
sd_ctx(
model_path = NULL,
vae_path = NULL,
taesd_path = NULL,
clip_l_path = NULL,
clip_g_path = NULL,
t5xxl_path = NULL,
llm_path = NULL,
diffusion_model_path = NULL,
control_net_path = NULL,
n_threads = 0L,
wtype = SD_TYPE$COUNT,
tensor_type_rules = NULL,
vae_decode_only = TRUE,
free_params_immediately = FALSE,
keep_clip_on_cpu = FALSE,
keep_vae_on_cpu = FALSE,
offload_params_to_cpu = FALSE,
max_vram = 0,
stream_layers = FALSE,
enable_mmap = FALSE,
vae_conv_direct = TRUE,
diffusion_conv_direct = FALSE,
diffusion_flash_attn = TRUE,
rng_type = RNG_TYPE$CUDA,
prediction = NULL,
lora_apply_mode = LORA_APPLY_MODE$AUTO,
model_type = "sd1",
vram_gb = NULL,
device_layout = "mono",
diffusion_gpu = -1L,
clip_gpu = -1L,
vae_gpu = -1L,
meta_backend = FALSE,
verbose = FALSE
)
model_path |
Path to the model file (safetensors, gguf, or checkpoint) |
vae_path |
Optional path to a separate VAE model |
taesd_path |
Optional path to TAESD model for preview |
clip_l_path |
Optional path to CLIP-L model |
clip_g_path |
Optional path to CLIP-G model |
t5xxl_path |
Optional path to T5-XXL model |
llm_path |
Optional path to an LLM text encoder (Qwen3 / Mistral-Small).
Required for models that use an LLM conditioner, e.g. FLUX.2 Klein (Qwen3),
FLUX.2 (Mistral-Small), Z-Image and Qwen-Image. Loaded into the
|
diffusion_model_path |
Optional path to separate diffusion model |
control_net_path |
Optional path to ControlNet model |
n_threads |
Number of CPU threads (0 = auto-detect) |
wtype |
Weight type for quantization (see |
tensor_type_rules |
Optional per-component weight type override, as a
comma-separated string of
Type names match ggml type names ( |
vae_decode_only |
If TRUE, only load VAE decoder (saves memory) |
free_params_immediately |
Free model params after first computation. If TRUE, the context can only be used for a single generation — subsequent calls will crash. Set to TRUE only when you need to save memory and will not reuse the context. Default is FALSE. |
keep_clip_on_cpu |
Keep CLIP model on CPU even when using GPU |
keep_vae_on_cpu |
Keep VAE on CPU even when using GPU |
offload_params_to_cpu |
Keep model weights in CPU RAM and stream them to the GPU on demand during compute (default FALSE). Lowers VRAM usage at the cost of CPU<->GPU transfers each step. Use when the model does not fit in GPU memory. |
max_vram |
GiB budget for graph-cut segmented parameter offload
(default 0 = disabled). A positive value caps GPU memory used by the compute
graph; |
stream_layers |
Enable residency + prefetch streaming of layers on top of
|
enable_mmap |
Memory-map model weights from disk instead of reading them into a malloc'd buffer (default FALSE). Lowers RAM footprint for large models (e.g. Flux); pages are loaded on demand by the OS and shared across processes. Ignored for zip-archived weights. May slow the first generation slightly as pages fault in. |
vae_conv_direct |
Use direct Conv2d implementation in VAE (default TRUE). Faster on GPU; skips im2col and uses direct convolution kernels. |
diffusion_conv_direct |
Use direct Conv2d in diffusion model (default FALSE). |
diffusion_flash_attn |
Enable flash attention for diffusion model (default TRUE). Set to FALSE if you experience issues with specific GPU drivers or backends. |
rng_type |
RNG type (see |
prediction |
Prediction type override (see |
lora_apply_mode |
LoRA application mode (see |
model_type |
Model architecture hint: |
vram_gb |
Override available VRAM in GB. When set, disables auto-detection
and uses this value for strategy routing. Default |
device_layout |
GPU layout preset for multi-GPU systems. One of:
Ignored when |
diffusion_gpu |
Vulkan GPU device index for the diffusion model.
Default |
clip_gpu |
Vulkan GPU device index for CLIP/T5 text encoders.
Default |
vae_gpu |
Vulkan GPU device index for VAE encoder/decoder.
Default |
meta_backend |
Logical flag to run the diffusion model through the ggml
meta backend ("second path", multi-GPU tensor split across all available
GPUs). Requires meta-backend support compiled in at install time (ggmlR
>= 0.7.8 exporting |
verbose |
If |
An external pointer to the SD context (class "sd_ctx") with
attributes model_type, vae_decode_only, vram_gb,
vram_total_gb, and vram_device.
## Not run:
ctx <- sd_ctx("model.safetensors")
imgs <- sd_txt2img(ctx, "a cat sitting on a chair")
sd_save_image(imgs[[1]], "cat.png")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.