KSampler K采样器 |ComfyUI组件节点
文档说明
- 类名:
KSampler
- 类别:
采样
- 输出节点:
否
KSampler这个采样器,是这样工作的:它会根据提供的特定的模型和正、负两种条件,来改造提供的原始潜在图像信息。 首先,它会根据设定好的seed随机种子和denoise降噪强度,给原始图像数据加入一些噪声,然后输入预设的Model模型结合positive正向和negative负向的引导条件,去生成图像
Input 输入
参数名称 | 数据类型 | 必填 | 默认值 | 取值范围/选项 | 说明 |
---|---|---|---|---|---|
Model模型 | checkpoint模型 | 是 | 无 | - | 输入用于降噪过程的模型 |
seed随机种子 | Int整数 | 是 | 0 | 0 ~ 18446744073709551615 | 用于生成随机噪声,使用同样的“种子”可以生成相同的画面 |
steps步数 | Int整数 | 是 | 20 | 1 ~ 10000 | 去噪过程中要使用的步骤数,步数越多,结果越准确 |
cfg | float浮点数 | 是 | 8.0 | 0.0 ~ 100.0 | 控制生成的图像与输入条件的贴合程度,通常建议6-8 |
sampler_name采样器 | 界面选项 | 是 | 无 | 多种采样算法 | 选择用来降噪的采样器,不同采样器影响生成速度和风格 |
scheduler调度器 | 界面选项 | 是 | 无 | 多种调度器 | 控制噪声去除的方式,不同调度器会影响生成过程 |
Positive正向条件 | conditioning条件 | 是 | 无 | - | 用于引导降噪的正向条件,可理解为想要在画面中出现的内容 |
Negative负向条件 | conditioning条件 | 是 | 无 | - | 用于引导降噪的负向条件,可理解为不想要在画面中出现的内容 |
Latent_Image | Latent | 是 | 无 | - | 用于降噪的潜像 |
denoise降噪 | float浮点数 | 否 | 1.0 | 0.0 ~ 1.0 | 决定去除多少比例的噪声,值越小生成图像与输入图像关联越小,值越大越像输入图像 |
control_after_generate | 界面选项 | 否 | 无 | 随机/增量/减量/保持 | 提供在每次提示后更改种子数的能力,节点可以随机、增量、减量或保持种子数不变 |
Output 输出
参数名称 | 作用 |
---|---|
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": "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)