Image generation · Generate image with Stable Diffusion6 / 7

Generate image with Stable Diffusion

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

FLUX.2 is one model family. Stable Diffusion is another, and the older of the two. SD 1.x and SD 2.x are available as a single all-in-one GGUF. There's no LLM encoder and no VAE to download alongside it.

SD 2.1 in this single-file format is much smaller on disk than FLUX.2-klein, so it loads faster on a fresh device and fits in less VRAM. The generation quality is lower than FLUX.2-klein at the same step count, but for short prompts and quick iteration it's the path of least resistance.

Stable Diffusion supports two sampling targets: epsilon (default) and v (the velocity, the change in noise). At high guidance, v is the cleaner choice with fewer artifacts. The modelConfig block setting v would look like so:

const modelId = await loadModel({
  modelSrc: SD_V2_1_1B_Q8_0,
  modelType: "sdcpp-generation",
  modelConfig: { prediction: "v" },
});

The generation call is the same diffusion({ modelId, prompt }) as the FLUX.2 lessons:

const result = diffusion({
  modelId,
  prompt: "a photo of a cat sitting on a windowsill",
});

Once the awaited PNG is in hand, write the first one and log the count:

const outputs = await result.outputs;
const first = outputs[0];
if (!first) throw new Error("No image returned from diffusion");
fs.writeFileSync("output/image-generation/cat.png", first);
console.log(`Generated ${outputs.length} image`);

SD 2.1 was trained for v-prediction; epsilon would produce a noisier result on this model.

Note: SD 2.1 does support img2img through the init_image option, the same as FLUX.2. Pass init_image alongside strength; the next chapter covers that path.

Questions

Question 1 of 3

Which of the following best describes the difference between Stable Diffusion's single-file layout and FLUX.2-klein's split layout?

Question 2 of 3

What is a key consideration behind setting modelConfig.prediction to v for SD 2.1 instead of leaving the default?

Question 3 of 3

What is Stable Diffusion's default sampling target if modelConfig.prediction is left unset?

index.ts
Loading editor...

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