Skip to main content

Overview

The model module provides a high-level interface for loading and working with diffusion models from HuggingFace or local paths. It automatically handles model optimization, device management, and inference.

model.load()

Load a diffusion model from HuggingFace or a local path.

Parameters

string
required
HuggingFace model ID (e.g., “stabilityai/stable-diffusion-xl-base-1.0”) or local path to model directory
string
default:"float16"
Data type for model weights. Options:
  • "float16" or "fp16" - Half precision (recommended for most GPUs)
  • "bfloat16" or "bf16" - Brain float (better for newer hardware)
  • "float32" or "fp32" - Full precision (slower but more accurate)
dict
Additional arguments passed to DiffusionPipeline.from_pretrained(). Common options:
  • variant - Model variant (e.g., “fp16”)
  • use_auth_token - HuggingFace authentication token
  • revision - Model revision to use

Returns

Model
Model instance ready for inference and training

Example


Model.to()

Move the model to a specific device (GPU, CPU, etc.).

Parameters

string
required
Device to move model to:
  • "cuda" - NVIDIA GPU (default CUDA device)
  • "cuda:0", "cuda:1", etc. - Specific CUDA device
  • "cpu" - CPU
  • "mps" - Apple Silicon GPU (Metal Performance Shaders)

Returns

Model
Returns self for method chaining

Example


Model.generate()

Generate images from text prompt(s).

Parameters

string | list[string]
required
Text prompt(s) describing the image to generate. Can be a single string or list of strings for batch generation.
string | list[string]
Negative prompt(s) describing what to avoid in the generation
integer
default:"50"
Number of denoising steps. More steps = higher quality but slower. Typical range: 20-100.
float
default:"7.5"
Classifier-free guidance scale. Higher values = stronger adherence to prompt. Typical range: 5.0-15.0.
integer
default:"1024"
Output image width in pixels. Must be divisible by 8.
integer
default:"1024"
Output image height in pixels. Must be divisible by 8.
integer
default:"1"
Number of images to generate per prompt
integer
Random seed for reproducible generation. If not specified, uses random seed.
dict
Additional arguments passed to the underlying pipeline (model-specific)

Returns

PIL.Image.Image | list[PIL.Image.Image]
Generated image(s). Returns a single PIL Image if one image is generated, or a list of images for batch generation.

Example


Model.train_lora()

Train a LoRA (Low-Rank Adaptation) adapter on a custom dataset. This is the simple, high-level interface for LoRA training.

Parameters

Dataset
required
Dataset to train on, created with dataset.load()
integer
default:"1000"
Number of training steps to perform
float | string
default:"1e-4"
Learning rate for training. Use a float value or "auto" for automatic learning rate selection.Typical values:
  • 1e-4 - Good starting point for most cases
  • 1e-5 - More conservative, slower convergence
  • 1e-3 - Aggressive, may cause instability
integer
default:"16"
LoRA rank. Lower rank = fewer parameters and faster training, but less expressive.Common values:
  • 8 - Very lightweight, fast training
  • 16 - Good balance (default)
  • 32 - More expressive, slower training
  • 64 - High capacity, slow training
integer
default:"32"
LoRA alpha scaling factor. Typically set to 2x the rank. Controls the magnitude of LoRA updates.
integer | string
default:"1"
Batch size for training. Use "auto" for automatic batch size selection based on available memory.
integer
default:"1"
Number of steps to accumulate gradients before updating weights. Effective batch size = batch_size � gradient_accumulation_steps.
string
Directory to save checkpoints. If not specified, checkpoints are not saved to disk.
integer
Save a checkpoint every N steps. Only applies if output_dir is specified.
dict
Additional training arguments passed to the trainer

Returns

dict
Dictionary containing trained LoRA weights that can be loaded later

Example

Notes

LoRA training is memory-efficient and fast compared to full fine-tuning. It only trains a small number of additional parameters while keeping the base model frozen.
Training requires significant GPU memory. If you run out of memory, try:
  • Reducing batch_size
  • Reducing rank
  • Using gradient_accumulation_steps to simulate larger batches

Type Reference

Model

The main model class returned by model.load().