question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

scheduler leaky abstractions in pipelines

See original GitHub issue

Excited by the opportunities that Stable Diffusion has made for use with consumer hardware, I’ve started working with the code. I was looking to get an idea of what I’d need to do for something like #277 and studying the StableDiffusionPipeline class.

I’m concerned by the use of isinstance(self.scheduler, LMSDiscreteScheduler). If you can’t implement a new Scheduler without meddling with the internal implementation of the Pipeline, that points to some breakdown in the API design. We don’t want growing if isintance trees that grow with each New Scheduler PR.

It also introduces an ordering dependency between components that weren’t otherwise entangled: https://github.com/huggingface/diffusers/blob/e49dd03d2de347caf5f18d04657500412f57103b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L120-L124

It wasn’t obvious to me at first glance, but scheduler.set_timesteps modifies scheduler.sigmas. So now when working with that code we have to remember that we have to do that scheduler initialization before we finish preparing the initial latents, whereas those were independent operations before.

Another worrying comment is this: https://github.com/huggingface/diffusers/blob/e49dd03d2de347caf5f18d04657500412f57103b/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L126-L130

If “not all schedulers share the same signature,” it’s much harder to make and use Schedulers.

Suggestions

LMSDiscreteScheduler’s step function seems to want the step number instead of the time. Options:

  1. Change the signature of Scheduler.step to always include the index as well as the time. (It’s readily available and cheap to have on the stack.)
  2. Since LMS is linear (right there in the name!), could it take the usual timestep value and easily convert it back to its index value?

For DDIMScheduler’s extra eta parameter:

The **kwargs on StableDiffusionPipeline.__call__ seems to be unused, and eta seems to be passed straight through to scheduler.step with no other interaction. We could take eta out of the formal parameter list and rename kwargs to extra_step_kwargs.

I haven’t yet looked at other uses of Scheduler outside of this one pipeline, but those are my initial thoughts.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:12
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
keturncommented, Sep 18, 2022

When I originally looked at this, I was under the impression that there were some differences between whether sigmas or timesteps were computed up front and where some multiplication happened, but I figured that was solvable with a little algebra and a little refactoring without changing the overall design too much.

But when hlky got me looking at why some samplers weren’t behaving as well as others, I realized there’s a bigger difference between the design of Katherine’s sampler functions and diffusers’ Scheduler.step method: Samplers may use the model more than once per timestep.

The operation is very similar to that of guidance functions: Much like the current StableDiffusionPipeline offers an option to run two sets of data through the UNet to perform classifier-free guidance, the higher order functions featured in Elucidating and DPM-Solver (sample_heun and sample_dpm in k_diffusers) use the model to make multiple predictions and combine them for a better result.

I expect we’ll see a plethora of other guidance functions that rely on combining the results of multiple predictions, such as Composable Diffusion.

Them’s the findings. My thoughts about what to do about it are all tangled up with this project’s philosophy of Pipelines being single purpose and having some sort of pristine original implementation that shall be preserved.

Reading that again, I see that schedulers (if not pipelines) are intended to be building blocks, and that reassures me that we can come up with something we can build applications on. 😊

2reactions
garrett361commented, Sep 23, 2022

Edit: Nevermind, solved the below. First, join the HF Discord channel, then go to the #role-assigment channel, then click on 🎨 for access to diffusion-related channels, then follow the link.


@keturn I’m curious about the link you shared: hlky got me looking at why some samplers weren’t behaving

However, when I try to follow it, I’m met with an error message from Discord: You find yourself in a strange place. You don't have access to any text channels, or there are none in this server. Could you give more detailed instructions for how to see that conversation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Leaky Abstractions - Hacker News
The traditional posix filesystem is yet another leaky abstraction. Inflexible ACLS and metadata, a horrible and confusing consistency story, ...
Read more >
The Law of Leaky Abstractions - Joel on Software
This is what I call a leaky abstraction. TCP attempts to provide a complete abstraction of an underlying unreliable network, but sometimes, ...
Read more >
Scheduling Irregular Dataflow Pipelines on SIMD Architectures ...
A scheduler must both ensure high SIMD occupancy by gathering queued items into vectors and minimize costs associated with switching execution between stages....
Read more >
Privacy Budget Scheduling
2, right): This abstraction is used by pipelines to allocate and consume privacy budget from one or more private blocks. When creating a...
Read more >
spaCy behind the scenes: library patterns & design concepts ...
... complexities of machine learning, not hiding it away under leaky abstractions, ... including how to create the learning rate schedule.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found