---
name: diffusers_generate
kind: function
lang: py
domain: ml
version: "1.0.0"
purity: impure
signature: "def diffusers_generate(pipe: Any, cfg: GenerationConfig) -> ImageGenResult"
description: "Ejecuta inferencia con un pipeline diffusers usando GenerationConfig. Mide duracion y pico de VRAM. Retorna ImageGenResult con imagen PIL, meta y metricas."
tags: [diffusers, ml, image-generation, inference, vram, metrics, pendiente-usar]
uses_functions: [genconfig_to_diffusers_kwargs_py_ml]
uses_types: [generation_config_py_ml, image_gen_result_py_ml]
returns: [image_gen_result_py_ml]
returns_optional: false
error_type: "error_go_core"
imports: [torch, diffusers]
params:
  - name: pipe
    desc: "Pipeline diffusers cargado y listo para inferencia (resultado de diffusers_load_pipeline, opcionalmente con scheduler y LoRA configurados)."
  - name: cfg
    desc: "Parametros de generacion. cfg.seed >= 0 para semilla fija; -1 usa time-based. cfg.sampler se incluye en meta pero no se aplica aqui (usar diffusers_set_scheduler antes)."
output: "ImageGenResult con image=PIL.Image.Image, meta={backend, model, sampler, actual_steps, seed, width, height, cfg_scale}, duration_ms en entero milisegundos, vram_peak_mb (None si no hay CUDA)."
tested: true
tests:
  - "genera imagen retorna ImageGenResult"
test_file_path: "python/functions/ml/tests/test_diffusers_backend.py"
file_path: "python/functions/ml/diffusers_generate.py"
---

## Ejemplo

```python
from diffusers_load_pipeline import diffusers_load_pipeline
from diffusers_generate import diffusers_generate
from generation_config import GenerationConfig
from model_ref import ModelRef

model = ModelRef(
    name="sd-turbo",
    model_type="sd15",
    path="/home/lucas/vaults/imagegen_models/diffusers/sd-turbo",
)
cfg = GenerationConfig(
    prompt="a photo of a cat",
    seed=42,
    steps=1,
    cfg_scale=0.0,
    sampler="euler",
    width=512,
    height=512,
    model=model,
)
pipe = diffusers_load_pipeline(model, device="cuda", dtype="fp16")
result = diffusers_generate(pipe, cfg)
# result.image -> PIL.Image.Image 512x512
# result.duration_ms -> int > 0
# result.meta["backend"] -> "diffusers"
```

## Notas

`cfg.seed = -1` genera seed aleatorio basado en `time.time()` (reproducible si
se guarda en `result.meta["seed"]`).

VRAM: `torch.cuda.reset_peak_memory_stats()` antes de inferencia,
`torch.cuda.max_memory_allocated() // 1024 // 1024` despues.

`genconfig_to_diffusers_kwargs` omite generator=None; esta funcion lo reemplaza
con `torch.Generator(device=device).manual_seed(seed)`.

Import lazy de torch — ImportError descriptivo si no instalado.
