-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDpmPlusPlus2MScheduler.cpp
100 lines (80 loc) · 3.13 KB
/
DpmPlusPlus2MScheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "pch.h"
#include "DpmPlusPlus2MScheduler.h"
using namespace std;
namespace Axodox::MachineLearning
{
DpmPlusPlus2MScheduler::DpmPlusPlus2MScheduler(const StableDiffusionSchedulerOptions& options) :
StableDiffusionScheduler(options)
{
//Apply Karras sigmas
const auto rho = 7.f;
auto sigmaMax = _sigmas.front();
auto sigmaMin = *(_sigmas.end() - 2);
auto invRhoMin = pow(sigmaMin, 1.f / rho);
auto invRhoMax = pow(sigmaMax, 1.f / rho);
auto stepCount = _sigmas.size() - 1;
auto stepSize = 1.f / (stepCount - 1);
vector<float> timesteps(_timesteps.size());
vector<float> sigmas(_sigmas.size());
for (auto i = 0; i < stepCount; i++)
{
auto t = i * stepSize;
sigmas[i] = pow(invRhoMax + t * (invRhoMin - invRhoMax), rho);
timesteps[i] = SigmaToTime(sigmas[i]);
}
_sigmas = move(sigmas);
_timesteps = move(timesteps);
}
Tensor DpmPlusPlus2MScheduler::ApplyStep(const Tensor& input, const Tensor& output, size_t step)
{
auto currentSigma = _sigmas[step];
auto nextSigma = _sigmas[step + 1];
Tensor predictedOriginalSample;
if (_predictiontype == StableDiffusionSchedulerPredictionType::V)
{
predictedOriginalSample = output.BinaryOperation<float>(input, [currentSigma](float model_output, float sample) {
float sigmaSquaredPlusOne = currentSigma * currentSigma + 1;
return (model_output * (-currentSigma / std::sqrt(sigmaSquaredPlusOne))) + (sample / sigmaSquaredPlusOne);
});
}
else if (_predictiontype == StableDiffusionSchedulerPredictionType::Epsilon)
{
predictedOriginalSample = input.BinaryOperation<float>(output, [currentSigma](float a, float b) { return a - currentSigma * b; });
}
else
{
throw std::invalid_argument("Uninmplemented prediction type.");
}
float t = -log(currentSigma);
float tNext = -log(nextSigma);
float h = tNext - t;
Tensor denoised;
if (!_previousPredictedSample || nextSigma == 0)
{
denoised = predictedOriginalSample;
}
else
{
float hLast = t - -log(_sigmas[step - 1]);
float r = hLast / h;
auto x = 1.f + 1.f / (2.f * r);
auto y = 1.f / (2.f * r);
denoised = predictedOriginalSample.BinaryOperation<float>(_previousPredictedSample, [=](float a, float b) {
return x * a - y * b;
});
}
if (nextSigma != 0)
{
_previousPredictedSample = predictedOriginalSample;
}
else
{
_previousPredictedSample.Reset();
}
float x = nextSigma / currentSigma;
float y = exp(-h) - 1.f;
return input.BinaryOperation<float>(denoised, [=](float a, float b) {
return a * x - b * y;
});
}
}