Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tcd sampler #3370

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JettHu
Copy link

@JettHu JettHu commented Apr 28, 2024

This PR solved #2985 . Add Trajectory Consistency Distillation Support.

  • Implemented a new sampling function sample_tcd.
  • TCD use alphas_cumprod to calculate_denoised & noise_scaling, so I add register_buffer alphas_cumprod in ModelSamplingDiscrete.
  • Add a tcd sampling in comfy_extras.nodes_model_advanced.ModelSamplingDiscrete
  • Add a SamplerTCD node in comfy_extras.nodes_custom_sampler for set eta (referred to as gamma in the paper).

The upper part of the screenshot below shows setting eta (gamma in paper) value through SamplerTCD. In the lower part, selecting tcd directly in KSampler will use the default 0.3 as gamma.

image

TCD, significantly improves image quality at low NFEs (Number of Function Evaluations, or steps), and can produce more detailed results than the teacher model at high NFEs. Just like lcm, it can be directly applied to various models, and combined with LoRA . Although tcd-lora can directly use common PF-ODE solvers (such as DDIM or SDE-DPM-Solver), implementing tcd-scheduler can bring better quality.

Diffusers library already integrated TCDScheduler.

If someone are in a hurry, you can use my plug-in repository.

@MoonRide303
Copy link
Contributor

MoonRide303 commented Apr 29, 2024

@JettHu it seems to be just a sampler in this PR, I don't see TCD scheduler included - am I missing something? Isn't TCD scheduler necessary too, for TCD method to work as intended?

I've tried TCD scheduler available as custom node from here: https://github.com/dfl/comfyui-tcd-scheduler, and I've got different results from sgm_uniform.

Just this PR:
image

This PR + TCD scheduler (custom node):
image

Test workflow:
SDXL + TCD workflow.json

UPDATE:
For lower number of steps (and eta) sgm_uniform seems to be working better than TCD sampler:
image

vs

image

And for higher number of steps (and eta) results seem to be getting similar:
image

vs

image

So... it seems TCD sampler with sgm_uniform scheduler should do.

@comfyanonymous?

@rabidcopy
Copy link

rabidcopy commented Apr 30, 2024

!!! Exception during processing !!!
Traceback (most recent call last):
  File "C:\Users\user\Desktop\comfyui\execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
  File "C:\Users\user\Desktop\comfyui\execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
  File "C:\Users\user\Desktop\comfyui\execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
  File "C:\Users\user\Desktop\comfyui\comfy_extras\nodes_custom_sampler.py", line 392, in sample
    samples = comfy.sample.sample_custom(model, noise, cfg, sampler, sigmas, positive, negative, latent_image, noise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=noise_seed)
  File "C:\Users\user\Desktop\comfyui\comfy\sample.py", line 42, in sample_custom
    samples = comfy.samplers.sample(model, noise, positive, negative, cfg, model.load_device, sampler, sigmas, model_options=model.model_options, latent_image=latent_image, denoise_mask=noise_mask, callback=callback, disable_pbar=disable_pbar, seed=seed)
  File "C:\Users\user\Desktop\comfyui\comfy\samplers.py", line 657, in sample
    return cfg_guider.sample(noise, latent_image, sampler, sigmas, denoise_mask, callback, disable_pbar, seed)
  File "C:\Users\user\Desktop\comfyui\comfy\samplers.py", line 644, in sample
    output = self.inner_sample(noise, latent_image, device, sampler, sigmas, denoise_mask, callback, disable_pbar, seed)
  File "C:\Users\user\Desktop\comfyui\comfy\samplers.py", line 623, in inner_sample
    samples = sampler.sample(self, sigmas, extra_args, callback, noise, latent_image, denoise_mask, disable_pbar)
  File "C:\Users\user\Desktop\comfyui\comfy\samplers.py", line 534, in sample
    samples = self.sampler_function(model_k, noise, sigmas, extra_args=extra_args, callback=k_callback, disable=disable_pbar, **self.extra_options)
  File "C:\Users\user\Desktop\comfyui\venv\lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "C:\Users\user\Desktop\comfyui\comfy\k_diffusion\sampling.py", line 830, in sample_tcd
    alpha_prod_s = model_sampling.alphas_cumprod[timesteps_s]
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

This came up when trying to us this PR while the original repo works fine, oddly. (Am several commits behind though..)

@JettHu
Copy link
Author

JettHu commented Apr 30, 2024

@JettHu it seems to be just a sampler in this PR, I don't see TCD scheduler included - am I missing something?

Sorry, it was my mistake. this PR does contain only a sampler. Because it's called scheduler in diffusers, I'm used to it. So the title of this PR should be called tcd sampler, which is more reasonable.

Isn't TCD scheduler necessary too, for TCD method to work as intended?
I've tried TCD scheduler available as custom node from here: https://github.com/dfl/comfyui-tcd-scheduler, and I've got different results from sgm_uniform.

TCD and LCM use the same scheduler, which are simple and sgm_uniform mentioned here. No additional TCD scheduler is required. After my testing, the sigmas of scheduler simple is consistent with diffusers. However both can give good results.

I have not seen the implementation details of comfyui-tcd-scheduler. As mentioned here, my implementation ComfyUI-TCD may be more consistent with the original TCD. The content of this PR basically comes from my repo comfyui-tcd ComfyUI-TCD.

@JettHu
Copy link
Author

JettHu commented Apr 30, 2024

@rabidcopy Do you mind sharing your workflow and comfyui startup parameters?

I updated a version to ensure that the indices in sample_tcd are on the cpu.

@JettHu JettHu changed the title feat: add tcd scheduler feat: add tcd sampler Apr 30, 2024
@rabidcopy
Copy link

rabidcopy commented Apr 30, 2024

@rabidcopy Do you mind sharing your workflow and comfyui startup parameters?

I updated a version to ensure that the indices in sample_tcd are on the cpu.

Thanks for the update. The PR seems to work now without throwing any errors. I was using the workflow from here changed to use the node from this PR instead of your dedicated node for it. I think the device issue might have stemmed from my current usage of the DirectML backend as I am currently on Windows with an AMD GPU. I think the reason the dedicated node didn't have this issue for me is due to this line right here. https://github.com/JettHu/ComfyUI-TCD/blob/main/__init__.py#L61 But that's just a guess. Edit: Also for further context, I was inferencing an SDXL model that was loaded in low VRAM mode.

@RandomGitUser321
Copy link

@JettHu it seems to be just a sampler in this PR, I don't see TCD scheduler included - am I missing something? Isn't TCD scheduler necessary too, for TCD method to work as intended?

I've tried TCD scheduler available as custom node from here: https://github.com/dfl/comfyui-tcd-scheduler, and I've got different results from sgm_uniform.

So... it seems TCD sampler with sgm_uniform scheduler should do.

From the link you provided:
"NOTE: SamplerTCD is a WIP and currently just operates as DDIM with no gamma parameter. Please use SamplerTCD Euler A for the time being."

@JettHu
Copy link
Author

JettHu commented May 1, 2024

From the link you provided: "NOTE: SamplerTCD is a WIP and currently just operates as DDIM with no gamma parameter. Please use SamplerTCD Euler A for the time being."

@RandomGitUser321 This PR does not require an additional TCD Scheduler. It uses sgm_uniform and simple in the BasicScheduler like lcm sampler.

The upper part of the screenshot below shows setting eta (gamma in paper) value through SamplerTCD. In the lower part, selecting tcd directly in KSampler will use the default 0.3 as gamma.

image

If necessary, I can adjust the PR and rename eta to gamma. Maybe I can add a gamma parameter to ModelSamplingDiscrete, or add a new node called ModelSamplingDiscreteTCD. cc @comfyanonymous

@RandomGitUser321
Copy link

From the link you provided: "NOTE: SamplerTCD is a WIP and currently just operates as DDIM with no gamma parameter. Please use SamplerTCD Euler A for the time being."

@RandomGitUser321 This PR does not require an additional TCD Scheduler. It uses sgm_uniform and simple in the BasicScheduler like lcm sampler.

The upper part of the screenshot below shows setting eta (gamma in paper) value through SamplerTCD. In the lower part, selecting tcd directly in KSampler will use the default 0.3 as gamma.

Oh I wasn't talking about your version, yours seems to work fine. I was talking about the dlf version. There are two versions of the comfyui implementation, yours https://github.com/JettHu/ComfyUI-TCD and his https://github.com/dfl/comfyui-tcd-scheduler

@JettHu
Copy link
Author

JettHu commented May 2, 2024

Oh I wasn't talking about your version, yours seems to work fine. I was talking about the dlf version. There are two versions of the comfyui implementation, yours https://github.com/JettHu/ComfyUI-TCD and his https://github.com/dfl/comfyui-tcd-scheduler

Oh sorry, misunderstood

@dathide
Copy link

dathide commented May 4, 2024

There's a low chance this gets merged. If you look at the commit history, Comfy hardly ever merges pull requests that aren't bug fixes. I'm not complaining, this is their repo that works well for me after all.

@JettHu
Copy link
Author

JettHu commented May 5, 2024

There's a low chance this gets merged. If you look at the commit history, Comfy hardly ever merges pull requests that aren't bug fixes. I'm not complaining, this is their repo that works well for me after all.

That's a bit of a pity

@comfyanonymous
Copy link
Owner

This one is getting merged once I check that it matches the reference code.

mashb1t added a commit to mashb1t/Fooocus that referenced this pull request May 12, 2024
adapted code from comfyanonymous/ComfyUI#3370
TODO: check if virtual scheduler tcd is needed for using sampling_base ModelSamplingDiscreteDistilled or if it's better to use sgm_uniform directly without patching
@comfyanonymous
Copy link
Owner

official example:
test

this PR:
image

The quality of the images seems to be slightly worse than the official example

@MoonRide303
Copy link
Contributor

@comfyanonymous It works pretty fine for me - via SamplerTCD EulerA with a bit higher gamma:

SDXL + TCD workflow, original prompt.json

image

@MoonRide303
Copy link
Contributor

@mhh0318 @jabir-zheng @JettHu Could you guys please look into that difference mentioned by @comfyanonymous (between original diffusers and ComfyUI implementation), and double-check if sampler part is implemented properly?

I really like the output I get from this sampler, and I think it deserves to be inlcuded - just please make sure it's working as intended, so we wouldn't have to change it after users start using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants