pulsatrix 1.1.0
Loading...
Searching...
No Matches
noise_schedule.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstdint>
9#include <vector>
10
12#include "pulsatrix/tensor.hpp"
13
14namespace pulsatrix {
15
21 float skip = 0.0f;
22 float network = 0.0f;
23 float noise = 0.0f;
24};
25
55public:
69 explicit NoiseSchedule(int64_t num_timesteps, float beta_start = 1e-4f, float beta_end = 0.02f);
70
79 [[nodiscard]] static NoiseSchedule Cosine(int64_t num_timesteps, double offset = 0.008, double max_beta = 0.999);
80
82 [[nodiscard]] int64_t num_timesteps() const { return num_timesteps_; }
83
89 [[nodiscard]] float beta(int64_t t) const;
90
96 [[nodiscard]] float alpha(int64_t t) const;
97
103 [[nodiscard]] float alpha_bar(int64_t t) const;
104
122 [[nodiscard]] Tensor add_noise(const Tensor& x0, const Tensor& epsilon, int64_t t) const;
123
143 [[nodiscard]] Tensor denoise_step(const Tensor& x_t, const Tensor& predicted_epsilon, const Tensor& z,
144 int64_t t) const;
145
151 [[nodiscard]] ReverseStepCoefficients ddpm_coefficients(int64_t t) const;
152
164 [[nodiscard]] ReverseStepCoefficients ddim_coefficients(int64_t t, int64_t s, float eta = 0.0f) const;
165
170 [[nodiscard]] Tensor ddim_step(const Tensor& x_t, const Tensor& predicted_epsilon, const Tensor& z, int64_t t,
171 int64_t s, float eta = 0.0f) const;
172
173private:
174 struct FromBetas {};
176 NoiseSchedule(FromBetas, const std::vector<double>& betas);
177
182 [[nodiscard]] size_t index_of(int64_t t, const char* context) const;
183
184 int64_t num_timesteps_;
185 std::vector<float> beta_;
186 std::vector<float> alpha_;
187 std::vector<float> alpha_bar_;
188};
189
190} // namespace pulsatrix
The DDPM (Ho et al. 2020, arXiv:2006.11239) linear variance schedule plus the two tensor operations d...
Definition noise_schedule.hpp:54
float alpha_bar(int64_t t) const
alpha_bar_t = prod_{s=1..t} alpha_s (the cumulative product).
Tensor ddim_step(const Tensor &x_t, const Tensor &predicted_epsilon, const Tensor &z, int64_t t, int64_t s, float eta=0.0f) const
Applies ddim_coefficients(t, s, eta): skip * x_t - network * eps + noise * z.
static NoiseSchedule Cosine(int64_t num_timesteps, double offset=0.008, double max_beta=0.999)
The cosine schedule of Nichol & Dhariwal 2021 (arXiv:2102.09672): alpha_bar_t = f(t) / f(0) with f(t)...
NoiseSchedule(int64_t num_timesteps, float beta_start=1e-4f, float beta_end=0.02f)
Precomputes the linear beta schedule and its alpha / alpha_bar derivatives.
Tensor add_noise(const Tensor &x0, const Tensor &epsilon, int64_t t) const
Closed-form forward (noising) sample: x_t = sqrt(alpha_bar_t) * x0 + sqrt(1 - alpha_bar_t) * epsilon.
Tensor denoise_step(const Tensor &x_t, const Tensor &predicted_epsilon, const Tensor &z, int64_t t) const
One reverse (sampling) step, x_{t-1} = mean_term + sqrt(beta_t) * z where the mean term is (1/sqrt(al...
float beta(int64_t t) const
Variance beta_t at timestep t.
float alpha(int64_t t) const
alpha_t = 1 - beta_t.
ReverseStepCoefficients ddim_coefficients(int64_t t, int64_t s, float eta=0.0f) const
A DDIM step (Song et al. 2021, arXiv:2010.02502, eq. 12) from t to an earlier s: x_s = sqrt(alpha_bar...
ReverseStepCoefficients ddpm_coefficients(int64_t t) const
denoise_step()'s coefficients: skip 1/sqrt(alpha_t), network beta_t / (sqrt(alpha_t) sqrt(1 - alpha_b...
int64_t num_timesteps() const
Number of diffusion steps T this schedule was built for.
Definition noise_schedule.hpp:82
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Abstract interface isolating vendor-specific memory/compute operations from Tensor/ComputationGraph.
Definition acquisition_functions.hpp:16
One reverse step as x_prev = skip * x_t - network * eps_theta + noise * z (DIFF-5)....
Definition noise_schedule.hpp:20
float network
Definition noise_schedule.hpp:22
float skip
Definition noise_schedule.hpp:21
float noise
Definition noise_schedule.hpp:23
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).