@@ -80,31 +80,30 @@ struct DiscreteSchedule : SigmaSchedule {
80
80
https://research.nvidia.com/labs/toronto-ai/AlignYourSteps/howto.html
81
81
*/
82
82
struct AYSSchedule : SigmaSchedule {
83
- /* interp and linearInterp adapted from dpilger26's NumCpp library:
83
+ /* interp and linear_interp adapted from dpilger26's NumCpp library:
84
84
* https://github.com/dpilger26/NumCpp/tree/5e40aab74d14e257d65d3dc385c9ff9e2120c60e */
85
85
constexpr double interp (double left, double right, double perc) noexcept {
86
86
return (left * (1 . - perc)) + (right * perc);
87
87
}
88
88
89
- /* This will make the assumption that the reference x and y values are
90
- * already sorted in ascending order because they are being generated as
89
+ /* This will make the assumption that the reference x and y values are
90
+ * already sorted in ascending order because they are being generated as
91
91
* such in the calling function */
92
- std::vector<double > linearInterp (std::vector<float > new_x,
93
- const std::vector< float > ref_x, const std::vector<float > ref_y)
94
- {
92
+ std::vector<double > linear_interp (std::vector<float > new_x,
93
+ const std::vector<float > ref_x,
94
+ const std::vector< float > ref_y) {
95
95
const size_t len_x = new_x.size ();
96
- size_t i = 0 ;
97
- size_t j = 0 ;
96
+ size_t i = 0 ;
97
+ size_t j = 0 ;
98
98
std::vector<double > new_y (len_x);
99
99
100
- if (ref_x.size () != ref_y.size ()) {
100
+ if (ref_x.size () != ref_y.size ()) {
101
101
LOG_ERROR (" Linear Interoplation Failed: length mismatch" );
102
- return new_y;
102
+ return new_y;
103
103
}
104
104
105
105
/* serves as the bounds checking for the below while loop */
106
- if ((new_x[0 ] < ref_x[0 ])
107
- || (new_x[new_x.size () - 1 ] > ref_x[ref_x.size () - 1 ])) {
106
+ if ((new_x[0 ] < ref_x[0 ]) || (new_x[new_x.size () - 1 ] > ref_x[ref_x.size () - 1 ])) {
108
107
LOG_ERROR (" Linear Interpolation Failed: bad bounds" );
109
108
return new_y;
110
109
}
@@ -115,8 +114,7 @@ struct AYSSchedule : SigmaSchedule {
115
114
continue ;
116
115
}
117
116
118
- const double perc = static_cast <double >(new_x[i] - ref_x[j])
119
- / static_cast <double >(ref_x[j + 1 ] - ref_x[j]);
117
+ const double perc = static_cast <double >(new_x[i] - ref_x[j]) / static_cast <double >(ref_x[j + 1 ] - ref_x[j]);
120
118
121
119
new_y[i] = interp (ref_y[j], ref_y[j + 1 ], perc);
122
120
i++;
@@ -125,8 +123,7 @@ struct AYSSchedule : SigmaSchedule {
125
123
return new_y;
126
124
}
127
125
128
- std::vector<float > linearSpace (const float start, const float end,
129
- const size_t num_points) {
126
+ std::vector<float > linear_space (const float start, const float end, const size_t num_points) {
130
127
std::vector<float > result (num_points);
131
128
const float inc = (end - start) / (static_cast <float >(num_points - 1 ));
132
129
@@ -141,10 +138,10 @@ struct AYSSchedule : SigmaSchedule {
141
138
return result;
142
139
}
143
140
144
- std::vector<float > logLinearInterpolation (std::vector<float > sigma_in,
145
- const size_t new_len) {
141
+ std::vector<float > log_linear_interpolation (std::vector<float > sigma_in,
142
+ const size_t new_len) {
146
143
const size_t s_len = sigma_in.size ();
147
- std::vector<float > x_vals = linearSpace (0 .f , 1 .f , s_len);
144
+ std::vector<float > x_vals = linear_space (0 .f , 1 .f , s_len);
148
145
std::vector<float > y_vals (s_len);
149
146
150
147
/* Reverses the input array to be ascending instead of descending,
@@ -153,8 +150,8 @@ struct AYSSchedule : SigmaSchedule {
153
150
y_vals[i] = std::log (sigma_in[s_len - i - 1 ]);
154
151
}
155
152
156
- std::vector<float > new_x_vals = linearSpace (0 .f , 1 .f , new_len);
157
- std::vector<double > new_y_vals = linearInterp (new_x_vals, x_vals, y_vals);
153
+ std::vector<float > new_x_vals = linear_space (0 .f , 1 .f , new_len);
154
+ std::vector<double > new_y_vals = linear_interp (new_x_vals, x_vals, y_vals);
158
155
std::vector<float > results (new_len);
159
156
160
157
for (size_t i = 0 ; i < new_len; i++) {
@@ -164,56 +161,55 @@ struct AYSSchedule : SigmaSchedule {
164
161
return results;
165
162
}
166
163
167
-
168
164
std::vector<float > get_sigmas (uint32_t len) {
169
165
const std::vector<float > noise_levels[] = {
170
166
/* SD1.5 */
171
- {14.6146412293 , 6.4745760956 , 3.8636745985 , 2.6946151520 ,
172
- 1.8841921177 , 1.3943805092 , 0.9642583904 , 0.6523686016 ,
173
- 0.3977456272 , 0.1515232662 , 0.0291671582 },
167
+ {14 .6146412293f , 6 .4745760956f , 3 . 8636745985f , 2 . 6946151520f ,
168
+ 1 . 8841921177f , 1 . 3943805092f , 0 . 9642583904f , 0 . 6523686016f ,
169
+ 0 . 3977456272f , 0 . 1515232662f , 0 . 0291671582f },
174
170
/* SDXL */
175
- {14.6146412293 , 6.3184485287 , 3.7681790315 , 2.1811480769 ,
176
- 1.3405244945 , 0.8620721141 , 0.5550693289 , 0.3798540708 ,
177
- 0.2332364134 , 0.1114188177 , 0.0291671582 },
171
+ {14 .6146412293f , 6 .3184485287f , 3 . 7681790315f , 2 . 1811480769f ,
172
+ 1 . 3405244945f , 0 . 8620721141f , 0 . 5550693289f , 0 . 3798540708f ,
173
+ 0 . 2332364134f , 0 . 1114188177f , 0 . 0291671582f },
178
174
/* SVD */
179
- {700.00 , 54.5 , 15.886 , 7.977 , 4.248 , 1.789 , 0.981 , 0.403 ,
180
- 0.173 , 0.034 , 0.002 },
175
+ {700 .00f , 54 .5f , 15 .886f , 7 .977f , 4 .248f , 1 .789f , 0 .981f , 0 .403f ,
176
+ 0 . 173f , 0 .034f , 0 .002f },
181
177
};
182
-
178
+
183
179
std::vector<float > inputs;
184
180
std::vector<float > results (len + 1 );
185
-
181
+
186
182
switch (version) {
187
183
case VERSION_2_x: /* fallthrough */
188
- LOG_WARN (" AYS not designed for SD2.X models" );
189
- case VERSION_1_x:
190
- LOG_INFO (" AYS using SD1.5 noise levels" );
184
+ LOG_WARN (" AYS not designed for SD2.X models" );
185
+ case VERSION_1_x:
186
+ LOG_INFO (" AYS using SD1.5 noise levels" );
191
187
inputs = noise_levels[0 ];
192
188
break ;
193
189
case VERSION_XL:
194
- LOG_INFO (" AYS using SDXL noise levels" );
190
+ LOG_INFO (" AYS using SDXL noise levels" );
195
191
inputs = noise_levels[1 ];
196
192
break ;
197
193
case VERSION_SVD:
198
- LOG_INFO (" AYS using SVD noise levels" );
194
+ LOG_INFO (" AYS using SVD noise levels" );
199
195
inputs = noise_levels[2 ];
200
196
break ;
201
197
default :
202
198
LOG_ERROR (" Version not compatable with AYS scheduler" );
203
199
return results;
204
200
}
205
-
201
+
206
202
/* Stretches those pre-calculated reference levels out to the desired
207
203
* size using log-linear interpolation */
208
204
if ((len + 1 ) != inputs.size ()) {
209
- results = logLinearInterpolation (inputs, len + 1 );
205
+ results = log_linear_interpolation (inputs, len + 1 );
210
206
} else {
211
- results = inputs;
212
- }
213
-
207
+ results = inputs;
208
+ }
209
+
214
210
/* Not sure if this is strictly neccessary */
215
211
results[len] = 0 .0f ;
216
-
212
+
217
213
return results;
218
214
}
219
215
};
@@ -230,7 +226,7 @@ struct KarrasSchedule : SigmaSchedule {
230
226
231
227
float min_inv_rho = pow (sigma_min, (1 .f / rho));
232
228
float max_inv_rho = pow (sigma_max, (1 .f / rho));
233
- for (uint32_t i = 0 ; i < n; i++) {
229
+ for (uint32_t i = 0 ; i < n; i++) {
234
230
// Eq. (5) from Karras et al 2022
235
231
result[i] = pow (max_inv_rho + (float )i / ((float )n - 1 .f ) * (min_inv_rho - max_inv_rho), rho);
236
232
}
0 commit comments