Image generation · Set image width, height, and steps2 / 7

Set image width, height, and steps

Example on GitHub(packages/sdk/examples/diffusion-flux2-klein.ts)

The previous lesson made our first image. This one tunes the size and the step count.

diffusion() takes a handful of options besides prompt. The three you'll reach for first are width, height, and steps. Width and height control the output resolution in pixels. steps controls how many denoising iterations the model runs. More steps means more refine work, and eventually the image stops changing much.

A 512×512 image at 20 steps runs in a few seconds on a GPU. Doubling resolution roughly quadruples the work. The seed option lets us pin the random noise, so we can pass any integer and get the same image twice.

Four knobs on the same diffusion() call: width / height (multiples of 16) set resolution, steps is denoising iterations, seed pins the noise. The four-knob call would look like:

const result = diffusion({
  modelId,
  prompt: "a watercolor cat on a sunny windowsill",
  width: 512,
  height: 512,
  steps: 20,
  seed: 42,
});

Two calls with matching seed and prompt produce byte-identical output. Split-layout inference streams step progress; drain it before awaiting outputs so inference isn't blocked:

if (result.progressStream) {
  for await (const step of result.progressStream) {
    console.log(`▸ step ${step.step}/${step.totalSteps}`);
  }
}
const outputs = await result.outputs;
const first = outputs[0];
if (first) fs.writeFileSync("output/image-generation/cat-watercolor.png", first);
console.log(`Generated ${outputs.length} image`);

Run it twice with an unchanged seed and we get byte-identical output. Change the prompt and we get a different image from that same starting noise.

Note: the seed is per-call, not per-model. Two diffusion() calls with a matching seed and prompt produce identical output; changing either breaks the reproducibility.

Questions

Question 1 of 3

Given width and height both double, by what factor does the generation work grow?

Question 2 of 3

What is seed's default value if you leave it unset?

Question 3 of 3

What is a key characteristic of diffusion() that requires seed to be passed on every call instead of set once at loadModel?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.