跳转到主要内容
本文档由 AI 生成。如果您发现任何错误或有改进建议,欢迎贡献! 在 GitHub 上编辑
KSampler 的工作原理如下:它根据特定模型以及正向和负向条件,修改所提供的原始潜空间图像信息。 首先,根据设定的 seed(种子)和 denoise(去噪强度),向原始图像数据添加噪声,然后将预设的 Model(模型)与 positive(正向)和 negative(负向)引导条件结合,输入以生成图像。

输入

参数名数据类型是否必填默认值范围/选项描述
Modelcheckpoint-用于去噪过程的输入模型
种子Int00 ~ 18446744073709551615用于生成随机噪声,使用相同的“种子”将生成相同的图像
步数Int201 ~ 10000去噪过程中使用的步数,步数越多,结果越精确
cfgfloat8.00.0 ~ 100.0控制生成图像与输入条件的匹配程度,建议值为 6-8
采样器名称UI Option多种算法选择用于去噪的采样器,影响生成速度和风格
调度器UI Option多种调度器控制噪声的移除方式,影响生成过程
Positiveconditioning-引导去噪的正向条件,即你希望在图像中出现的内容
Negativeconditioning-引导去噪的负向条件,即你不希望在图像中出现的内容
Latent_ImageLatent-用于去噪的潜空间图像
降噪float1.00.0 ~ 1.0决定噪声移除比例,值越低,与输入图像的关联性越小
生成后的控制UI OptionRandom/Inc/Dec/Keep提供在每次提示后更改种子的能力

输出

参数功能
Latent输出采样器去噪后的潜空间数据

源代码

[更新于 2025 年 5 月 15 日]

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": "用于对输入潜空间数据进行去噪的模型。"}),
                "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff, "control_after_generate": True, "tooltip": "用于创建噪声的随机种子。"}),
                "steps": ("INT", {"default": 20, "min": 1, "max": 10000, "tooltip": "去噪过程中使用的步数。"}),
                "cfg": ("FLOAT", {"default": 8.0, "min": 0.0, "max": 100.0, "step":0.1, "round": 0.01, "tooltip": "无分类器引导比例,用于平衡创造性与对提示词的遵循程度。值越高,生成的图像与提示词越匹配,但过高会负面影响质量。"}),
                "sampler_name": (comfy.samplers.KSampler.SAMPLERS, {"tooltip": "采样时使用的算法,会影响生成结果的质量、速度和风格。"}),
                "scheduler": (comfy.samplers.KSampler.SCHEDULERS, {"tooltip": "调度器控制如何逐步移除噪声以形成图像。"}),
                "positive": ("CONDITIONING", {"tooltip": "描述你希望在图像中包含的属性的条件。"}),
                "negative": ("CONDITIONING", {"tooltip": "描述你希望从图像中排除的属性的条件。"}),
                "latent_image": ("LATENT", {"tooltip": "要去噪的潜空间图像。"}),
                "denoise": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01, "tooltip": "应用的去噪程度,较低的值将保持初始图像的结构,允许进行图生图采样。"}),
            }
        }

    RETURN_TYPES = ("LATENT",)
    OUTPUT_TOOLTIPS = ("去噪后的潜空间数据。",)
    FUNCTION = "sample"

    CATEGORY = "sampling"
    DESCRIPTION = "使用提供的模型、正向和负向条件对潜空间图像进行去噪。"

    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)