内置节点参考

Ksampler - ComfyUI 原生节点文档

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

Ksampler

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 去噪后的潜在图像,可用于后续解码为最终图像

使用示例

Stable diffusion 1.5 文生图工作流示例

Stable diffusion 1.5 文生图工作流示例

Stable diffusion 1.5 图生图工作流示例

Stable diffusion 1.5 图生图工作流示例

源码

[更新于2025年5月15日]

Python

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)

官方原文

本页来自 Comfy-Org 官方中文文档的固定版本,并转换为 xueai 静态页面。内容以官方持续更新的页面为准。

内容授权与修改

官方文档采用 GPL-3.0。本站保留许可证、固定提交号和修改说明,不代表 ComfyUI 或 Comfy-Org 对本站背书。

GPL-3.0来源和修改说明