QVAC Logo

diffusion( )

Generates images using a loaded diffusion model.

function diffusion(params: DiffusionClientParams): {
  progressStream: AsyncGenerator<DiffusionProgressTick>;
  outputs: Promise<Uint8Array[]>;
  stats: Promise<DiffusionStats | undefined>;
};

Parameters

NameTypeRequired?Description
paramsDiffusionClientParamsThe diffusion parameters

DiffusionClientParams

FieldTypeRequired?Description
modelIdstringThe identifier of the loaded diffusion model
promptstringText prompt describing the image to generate
negative_promptstringText describing what to avoid in the generated image
widthnumberImage width in pixels (must be a multiple of 8)
heightnumberImage height in pixels (must be a multiple of 8)
stepsnumberNumber of diffusion steps
cfg_scalenumberClassifier-free guidance scale for SD 1.x / 2.x / XL / SD3 models (typical range 1–20, default 7)
guidancenumberDistilled guidance for FLUX models (typical range 1–10, default 3.5)
sampling_methodSamplingMethodSampling algorithm
schedulerSchedulerNoise scheduler
seednumberRandom seed for reproducibility
batch_countnumberNumber of images to generate
vae_tilingbooleanEnable VAE tiling for large images on limited VRAM
cache_presetstringCache preset identifier

SamplingMethod

"euler" | "euler_a" | "heun" | "dpm2" | "dpm++2m" | "dpm++2mv2" | "dpm++2s_a" | "lcm" | "ipndm" | "ipndm_v" | "ddim_trailing" | "tcd" | "res_multistep" | "res_2s"

Scheduler

"discrete" | "karras" | "exponential" | "ays" | "gits" | "sgm_uniform" | "simple" | "lcm" | "smoothstep" | "kl_optimal" | "bong_tangent"

Returns

object — Object with the following fields:

FieldTypeDescription
progressStreamAsyncGenerator<DiffusionProgressTick>Stream of generation progress ticks
outputsPromise<Uint8Array[]>Generated image buffers (resolves when generation completes)
statsPromise<DiffusionStats | undefined>Performance statistics

DiffusionProgressTick

FieldTypeDescription
stepnumberCurrent diffusion step
totalStepsnumberTotal number of steps
elapsedMsnumberElapsed time in milliseconds

DiffusionStats

FieldTypeDescription
modelLoadMsnumber | undefinedModel loading time in milliseconds
generationMsnumber | undefinedSingle generation time in milliseconds
totalGenerationMsnumber | undefinedTotal generation time in milliseconds
totalWallMsnumber | undefinedTotal wall-clock time in milliseconds
totalStepsnumber | undefinedTotal diffusion steps performed
totalGenerationsnumber | undefinedNumber of generations completed
totalImagesnumber | undefinedNumber of images produced
totalPixelsnumber | undefinedTotal pixels generated
widthnumber | undefinedOutput image width
heightnumber | undefinedOutput image height
seednumber | undefinedSeed used for generation

Example

import fs from "fs";

// Basic usage
const { outputs, stats } = diffusion({ modelId, prompt: "a cat" });
const buffers = await outputs;
fs.writeFileSync("output.png", buffers[0]);

// With progress tracking
const { progressStream, outputs: images } = diffusion({
  modelId,
  prompt: "a cat sitting on a windowsill",
  width: 512,
  height: 512,
  steps: 20,
  cfg_scale: 7,
});

for await (const { step, totalSteps } of progressStream) {
  console.log(`${step}/${totalSteps}`);
}

const imageBuffers = await images;

On this page