> ## Documentation Index
> Fetch the complete documentation index at: https://dripart-docs-recommend-assets-api.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Ksampler - ComfyUI 原生节点文档

> Ksampler 节点是 ComfyUI 中常用的采样节点。

<img src="https://mintcdn.com/dripart-docs-recommend-assets-api/zDHpPUeID0CgytL1/images/built-in-nodes/sampling/ksampler.jpg?fit=max&auto=format&n=zDHpPUeID0CgytL1&q=85&s=4552fb4dcaa57828b5f7064d215b7f3c" alt="Ksampler" width="1608" height="1739" data-path="images/built-in-nodes/sampling/ksampler.jpg" />

KSampler 节点用于对潜在图像（latent image）进行多步去噪采样。它结合正向和负向条件（prompt），通过指定的采样算法和调度器，生成高质量的潜在图像。常用于文生图、图生图等 AI 图像生成流程中。

## 参数说明

### 输入参数

| 参数名           | 类型           | 是否必填 | 默认值 | 说明                                             |
| ------------- | ------------ | ---- | --- | ---------------------------------------------- |
| model         | MODEL        | 是    | 无   | 用于去噪的模型（如 Stable Diffusion 模型）                 |
| seed          | INT          | 是    | 0   | 随机种子，保证生成结果可复现                                 |
| steps         | INT          | 是    | 20  | 去噪步数，步数越多图像越精细，速度越慢                            |
| cfg           | FLOAT        | 是    | 8.0 | Classifier-Free Guidance 系数，数值越高越贴合提示词，过高会影响质量 |
| sampler\_name | 枚举           | 是    | 无   | 采样算法名称，影响生成速度、风格和质量                            |
| scheduler     | 枚举           | 是    | 无   | 调度器，控制噪声去除的过程                                  |
| positive      | CONDITIONING | 是    | 无   | 正向条件，描述希望图像包含的内容                               |
| negative      | CONDITIONING | 是    | 无   | 负向条件，描述希望图像排除的内容                               |
| latent\_image | LATENT       | 是    | 无   | 待去噪的潜在图像，通常为噪声或上一步输出                           |
| denoise       | FLOAT        | 是    | 1.0 | 去噪强度，1.0为完全去噪，数值越低越保留原始结构，适合图生图                |

### 输出参数

| 输出名     | 类型     | 说明                    |
| ------- | ------ | --------------------- |
| samples | LATENT | 去噪后的潜在图像，可用于后续解码为最终图像 |

## 使用示例

<Card title="Stable diffusion 1.5 文生图工作流示例" icon="book" href="/zh/tutorials/basic/text-to-image">
  Stable diffusion 1.5 文生图工作流示例
</Card>

<Card title="Stable diffusion 1.5 图生图工作流示例" icon="book" href="/zh/tutorials/basic/image-to-image">
  Stable diffusion 1.5 图生图工作流示例
</Card>

## 源码

\[更新于2025年5月15日]

```Python theme={null}

def common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent, denoise=1.0, disable_noise=False, start_step=None, last_step=None, force_full_denoise=False):
    latent_image = latent["samples"]
    latent_image = comfy.sample.fix_empty_latent_channels(model, latent_image)

    if disable_noise:
        noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu")
    else:
        batch_inds = latent["batch_index"] if "batch_index" in latent else None
        noise = comfy.sample.prepare_noise(latent_image, seed, batch_inds)

    noise_mask = None
    if "noise_mask" in latent:
        noise_mask = latent["noise_mask"]

    callback = latent_preview.prepare_callback(model, steps)
    disable_pbar = not comfy.utils.PROGRESS_BAR_ENABLED
    samples = comfy.sample.sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image,
                                  denoise=denoise, disable_noise=disable_noise, start_step=start_step, last_step=last_step,
                                  force_full_denoise=force_full_denoise, noise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=seed)
    out = latent.copy()
    out["samples"] = samples
    return (out, )


class KSampler:
    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "model": ("MODEL", {"tooltip": "The model used for denoising the input latent."}),
                "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "control_after_generate": True, "tooltip": "The random seed used for creating the noise."}),
                "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "The number of steps used in the denoising process."}),
                "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step":0.1, "round": 0.01, "tooltip": "The Classifier-Free Guidance scale balances creativity and adherence to the prompt. Higher values result in images more closely matching the prompt however too high values will negatively impact quality."}),
                "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "The algorithm used when sampling, this can affect the quality, speed, and style of the generated output."}),
                "scheduler": (comfy.samplers.KSampler.SCHEDULERS, {"tooltip": "The scheduler controls how noise is gradually removed to form the image."}),
                "positive": ("CONDITIONING", {"tooltip": "The conditioning describing the attributes you want to include in the image."}),
                "negative": ("CONDITIONING", {"tooltip": "The conditioning describing the attributes you want to exclude from the image."}),
                "latent_image": ("LATENT", {"tooltip": "The latent image to denoise."}),
                "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "The amount of denoising applied, lower values will maintain the structure of the initial image allowing for image to image sampling."}),
            }
        }

    RETURN_TYPES = ("LATENT",)
    OUTPUT_TOOLTIPS = ("The denoised latent.",)
    FUNCTION = "sample"

    CATEGORY = "sampling"
    DESCRIPTION = "Uses the provided model, positive and negative conditioning to denoise the latent image."

    def sample(self, model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=1.0):
        return common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=denoise)

```
