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

Implement DPM-Solver++ scheduler #59

Merged
merged 12 commits into from
Dec 15, 2022
Merged

Conversation

pcuenca
Copy link
Contributor

@pcuenca pcuenca commented Dec 11, 2022

Restricted to order 2, no dynamic thresholding.

It achieves similar quality to PNDM in 25 steps (~10s in my Mac).

Restricted to order 2, no dynamic thresholding.
@JustinMeans JustinMeans mentioned this pull request Dec 12, 2022
@msiracusa
Copy link
Collaborator

Thank you for your interest in contributing to Core ML Stable Diffusion! Please review CONTRIBUTING.md. If you would like to proceed with this pull request, please indicate your agreement to the terms outlined in CONTRIBUTING.md by checking the box below.

We appreciate your interest in the project!

  • I agree to the terms outlined in CONTRIBUTING.md

@pcuenca
Copy link
Contributor Author

pcuenca commented Dec 14, 2022

  • I agree to the terms outlined in CONTRIBUTING.md

}

self.alphas = betas.map({ 1.0 - $0 })
var alphasCumProd = self.alphas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will potentially create a copy. Should use the property directly instead.

Suggested change
var alphasCumProd = self.alphas
self.alphasCumProd = self.alphas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But alphasCumProd is a let var. This structure was taken from the original PNDM implementation: https://github.com/apple/ml-stable-diffusion/blob/main/swift/StableDiffusion/pipeline/Scheduler.swift#L68

self.alphasCumProd = alphasCumProd

// Currently we only support VP-type noise shedule
self.alpha_t = self.alphasCumProd.map { sqrt($0) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Accelerate to compute this, see sqrt

swift/StableDiffusion/pipeline/Scheduler.swift Outdated Show resolved Hide resolved
swift/StableDiffusion/pipeline/Scheduler.swift Outdated Show resolved Hide resolved
Comment on lines +75 to +76
self.alpha_t = vForce.sqrt(self.alphasCumProd)
self.sigma_t = vForce.sqrt(vDSP.subtract([Float](repeating: 1, count: self.alphasCumProd.count), self.alphasCumProd))
Copy link
Contributor Author

@pcuenca pcuenca Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added Accelerate, as suggested.

Personally, I believe the loss in legibility is not worth the potential performance gains in this case, especially in the line that computes sigma_t. This is just the initializer for the scheduler and not part of the denoising loop.

If there's a way to make it clearer I'd be happy to apply it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But I think it's good either way.

@msiracusa
Copy link
Collaborator

Thanks for adding this scheduler!

It would be nice to perhaps add a --scheduler option to the StableDiffusionSample CLI but it can also be done in a separate PR

@pcuenca
Copy link
Contributor Author

pcuenca commented Dec 14, 2022

@msiracusa great idea, just added that option :)

@msiracusa
Copy link
Collaborator

@msiracusa great idea, just added that option :)

I just tested at it works great. Thank you!

@msiracusa msiracusa merged commit 3758abc into apple:main Dec 15, 2022
takeyat pushed a commit to takeyat/ml-stable-diffusion that referenced this pull request Dec 25, 2022
* commit 'e07c4d00c387840f70fa3701fb3a51c2a32f37b8':
  Move guidanceScale as generation parameter (apple#46)
  Add brief instructions to download weights from the Hub (apple#10)
  Adds Negative Prompts (apple#61)
  Changed seed type into UInt32 (apple#47)
  fixes apple#77
  Update README.md (apple#66)
  Add Filename Character Limit (apple#19)
  Implement DPM-Solver++ scheduler (apple#59)
  Fix typos: Successfully facilitate getting pipeline overridden (apple#30)
  Undefined name: from typing import List (apple#31)
  Add Availability Annotations (apple#18)
  README improvements and reduceMemory option in Swift
@pcuenca pcuenca deleted the dpmpp-solver branch December 28, 2022 11:51
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.

3 participants