pulsatrix 1.1.0
Loading...
Searching...
No Matches
trajectory_lrp.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <cstdint>
18#include <vector>
19
21#include "pulsatrix/module.hpp"
23
24namespace pulsatrix {
25
29 std::vector<float> x;
31 std::vector<float> condition;
33 double time = 0;
34};
35
41public:
42 virtual ~ContinuousDenoiser() = default;
44 [[nodiscard]] virtual std::vector<float> predict(const std::vector<float>& x_t, int64_t t) = 0;
46 [[nodiscard]] virtual DenoiserRelevance relevance(const std::vector<float>& relevance_eps) = 0;
47};
48
57
64class ConcatDenoiser final : public ContinuousDenoiser {
65public:
72 ConcatDenoiser(Module& network, int64_t dim, int64_t time_dim, std::vector<float> condition, DeviceBackend* backend,
75 void set_condition(std::vector<float> condition);
76 [[nodiscard]] const std::vector<float>& condition() const { return condition_; }
78 [[nodiscard]] std::vector<float> input_row(const std::vector<float>& x_t, int64_t t) const;
79 [[nodiscard]] std::vector<float> predict(const std::vector<float>& x_t, int64_t t) override;
80 [[nodiscard]] DenoiserRelevance relevance(const std::vector<float>& relevance_eps) override;
81
82private:
83 Module& network_;
84 int64_t dim_;
85 int64_t time_dim_;
86 std::vector<float> condition_;
87 DeviceBackend* backend_;
88 LRPRuleConfig config_;
89};
90
94 Ddpm,
96 Ddim,
97};
98
109
113 std::vector<std::vector<float>> x;
115 std::vector<int64_t> timesteps;
117 std::vector<ReverseStepCoefficients> coefficients;
119 std::vector<std::vector<float>> noise;
120
121 [[nodiscard]] const std::vector<float>& sample() const { return x.back(); }
122 [[nodiscard]] int64_t steps() const { return static_cast<int64_t>(timesteps.size()); }
123};
124
131 const std::vector<float>& x_T, const ContinuousSamplerConfig& config);
132
134[[nodiscard]] std::vector<float> StandardNormal(size_t n, uint64_t seed);
135
136// ---- seeds: a scalar of x_0 -------------------------------------------------------------------
137
140[[nodiscard]] std::vector<float> RegionSeed(const std::vector<float>& sample, const std::vector<int64_t>& region);
141
145[[nodiscard]] std::vector<float> ReconstructionSeed(const std::vector<float>& sample, const std::vector<float>& reference);
146
147// ---- the explainer --------------------------------------------------------------------------
148
150enum class TrajectorySplit {
174};
175
188
192 int64_t step = 0;
193 int64_t timestep = 0;
195 double seeded = 0;
198 double skip = 0;
199 double network = 0;
200 double noise = 0;
201 double absorbed = 0;
205 double signed_skip = 0;
206 double signed_network = 0;
207 double signed_noise = 0;
209 double network_to_x = 0;
211 double network_to_time = 0;
213 [[nodiscard]] double network_leak() const { return network - network_to_x - network_to_condition - network_to_time; }
214};
215
222 double value = 0;
224 std::vector<float> x_T;
226 std::vector<float> condition;
229 std::vector<std::vector<float>> condition_by_step;
231 std::vector<TrajectoryHop> hops;
232
233 [[nodiscard]] double x_T_total() const;
234 [[nodiscard]] double condition_total() const;
235 [[nodiscard]] double time_total() const;
236 [[nodiscard]] double noise_total() const;
237 [[nodiscard]] double absorbed_total() const;
238 [[nodiscard]] double leak_total() const;
239};
240
253 const std::vector<float>& seed, const TrajectoryLrpConfig& config = {});
254
255} // namespace pulsatrix
A Module that reads [x_t, embedding(t), c] as one row, conditioned by concatenation as in tests/diffu...
Definition trajectory_lrp.hpp:64
std::vector< float > input_row(const std::vector< float > &x_t, int64_t t) const
The row the network reads at timestep t.
ConcatDenoiser(Module &network, int64_t dim, int64_t time_dim, std::vector< float > condition, DeviceBackend *backend, LRPRuleConfig config=TrajectoryNetworkRules())
void set_condition(std::vector< float > condition)
Replaces the conditioning, for instance with another class.
DenoiserRelevance relevance(const std::vector< float > &relevance_eps) override
Relevance on the last predict()'s inputs, from relevance_eps on its output.
const std::vector< float > & condition() const
Definition trajectory_lrp.hpp:76
std::vector< float > predict(const std::vector< float > &x_t, int64_t t) override
The predicted noise at timestep t, the same size as x_t.
The noise predictor eps_theta as the explainer sees it, with its conditioning fixed: a forward pass t...
Definition trajectory_lrp.hpp:40
virtual ~ContinuousDenoiser()=default
virtual std::vector< float > predict(const std::vector< float > &x_t, int64_t t)=0
The predicted noise at timestep t, the same size as x_t.
virtual DenoiserRelevance relevance(const std::vector< float > &relevance_eps)=0
Relevance on the last predict()'s inputs, from relevance_eps on its output.
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
The DDPM (Ho et al. 2020, arXiv:2006.11239) linear variance schedule plus the two tensor operations d...
Definition noise_schedule.hpp:54
Selects which LRP rule variant a Module::propagate_relevance() call uses.
Abstract base every layer subclasses – NVI forward(), pure-virtual LRP contract.
Definition acquisition_functions.hpp:16
ContinuousSampler
Which reverse process to run.
Definition trajectory_lrp.hpp:92
@ Ddim
DDIM (Song et al. 2021) on evenly spaced timesteps; deterministic when eta = 0.
@ Ddpm
Ancestral DDPM (Ho et al. 2020): every timestep T..1, noise sqrt(beta_t), none at t = 1.
TrajectoryRelevance ExplainTrajectory(ContinuousDenoiser &model, const ContinuousTrajectory &trajectory, const std::vector< float > &seed, const TrajectoryLrpConfig &config={})
Explains seed, relevance on the trajectory's sample x_0 (RegionSeed, ReconstructionSeed,...
std::vector< float > StandardNormal(size_t n, uint64_t seed)
Draws n standard normal values from seed, as SampleContinuousDiffusion does.
std::vector< float > ReconstructionSeed(const std::vector< float > &sample, const std::vector< float > &reference)
The reconstruction error ||x_0 - reference||^2, as LRP for autoencoders seeds it (Kobayashi et al....
ContinuousTrajectory SampleContinuousDiffusion(ContinuousDenoiser &model, const NoiseSchedule &schedule, const std::vector< float > &x_T, const ContinuousSamplerConfig &config)
Runs the reverse process from x_T and records it.
TrajectorySplit
How the relevance on x_prev is split across skip, network and noise (h_1, h_2, h_3).
Definition trajectory_lrp.hpp:150
@ AbsoluteRatio
R_k = R |h_k| / (sum_j |h_j| + epsilon), the absolute-value ratio of LRP for ResNets (Otsuki et al....
@ SignedEpsilon
The signed epsilon rule, R_k = R h_k / (s + e sign(s)) with s = sum_j h_j and the stabilizer e = epsi...
@ SignedRatio
R_k = R h_k sign(s) / (sum_j |h_j| + epsilon): each term keeps the sign of its contribution to x_prev...
LRPRuleConfig TrajectoryNetworkRules()
The network rules ConcatDenoiser uses unless told otherwise: the epsilon rule with the bias in the de...
std::vector< float > RegionSeed(const std::vector< float > &sample, const std::vector< int64_t > &region)
The sum of sample over region: relevance x0_i on the region's elements, 0 elsewhere.
DDPM noise schedules (linear and cosine): precomputed beta/alpha/alpha_bar, forward noising,...
Definition trajectory_lrp.hpp:99
ContinuousSampler sampler
Definition trajectory_lrp.hpp:100
float eta
DDIM only: 0 is deterministic, 1 has DDPM's posterior variance.
Definition trajectory_lrp.hpp:105
int64_t steps
DDIM only: how many steps. The timesteps are round(T (k / steps)) for k = steps..1,...
Definition trajectory_lrp.hpp:103
uint64_t seed
Seeds the injected noise (an own Box-Muller over mt19937_64, the same on every platform).
Definition trajectory_lrp.hpp:107
A reverse process, recorded so it can be explained: every state and every noise draw.
Definition trajectory_lrp.hpp:111
std::vector< ReverseStepCoefficients > coefficients
Each step's coefficients.
Definition trajectory_lrp.hpp:117
std::vector< std::vector< float > > x
x[0] is x_T, x[k + 1] the state after step k, x.back() the sample x_0.
Definition trajectory_lrp.hpp:113
int64_t steps() const
Definition trajectory_lrp.hpp:122
std::vector< std::vector< float > > noise
Each step's noise z (zero when the step injects none).
Definition trajectory_lrp.hpp:119
const std::vector< float > & sample() const
Definition trajectory_lrp.hpp:121
std::vector< int64_t > timesteps
The timestep each step reads (descending).
Definition trajectory_lrp.hpp:115
Where the network's share of one step landed.
Definition trajectory_lrp.hpp:27
double time
On the timestep embedding: a sink, since the embedding is a constant.
Definition trajectory_lrp.hpp:33
std::vector< float > x
On x_t, one value per element.
Definition trajectory_lrp.hpp:29
std::vector< float > condition
On the conditioning, one value per conditioning input.
Definition trajectory_lrp.hpp:31
Configuration for LRP relevance propagation: which rule a module applies and its hyperparameters....
Definition lrp_rule_config.hpp:72
One reverse step's split, aggregated over elements.
Definition trajectory_lrp.hpp:190
int64_t step
The index into ContinuousTrajectory::timesteps, and that timestep.
Definition trajectory_lrp.hpp:192
double noise
Definition trajectory_lrp.hpp:200
double signed_network
Definition trajectory_lrp.hpp:206
double network_to_x
Where the network share landed: x_t, the conditioning, the timestep embedding.
Definition trajectory_lrp.hpp:209
double skip
Its split: skip (to x_t), network (into eps_theta), noise (a sink), and what the split's epsilon abso...
Definition trajectory_lrp.hpp:198
double absorbed
Definition trajectory_lrp.hpp:201
double network_leak() const
What the network's relevance pass didn't conserve: biases, norms, its stabilizers.
Definition trajectory_lrp.hpp:213
double network
Definition trajectory_lrp.hpp:199
double signed_noise
Definition trajectory_lrp.hpp:207
double seeded
Relevance arriving on x_prev.
Definition trajectory_lrp.hpp:195
double network_to_condition
Definition trajectory_lrp.hpp:210
double signed_skip
The signed fractions, logged next to the split: sum_i R_i h_k,i / s_i for each term,...
Definition trajectory_lrp.hpp:205
double network_to_time
Definition trajectory_lrp.hpp:211
int64_t timestep
Definition trajectory_lrp.hpp:193
Definition trajectory_lrp.hpp:176
double recompute_tolerance
Relative tolerance for the rerun check: recomputing each step from the stored x_t,...
Definition trajectory_lrp.hpp:186
double epsilon
AbsoluteRatio and SignedRatio: added to the denominator. SignedEpsilon: the stabilizer's scale.
Definition trajectory_lrp.hpp:182
TrajectorySplit split
SignedRatio: on DIFF-5's planted DDPM its per-step conditioning relevance tracked the effect of remov...
Definition trajectory_lrp.hpp:179
Where the seed's relevance went. The budget balances exactly: value = x_T + condition + time + noise ...
Definition trajectory_lrp.hpp:220
std::vector< TrajectoryHop > hops
One entry per step, in sampling order (step 0 reads x_T).
Definition trajectory_lrp.hpp:231
std::vector< std::vector< float > > condition_by_step
Relevance on the conditioning at each step (steps x condition values), so its concentration over time...
Definition trajectory_lrp.hpp:229
double value
The seed's total, the explained value.
Definition trajectory_lrp.hpp:222
std::vector< float > x_T
Relevance on the starting noise x_T, one value per element.
Definition trajectory_lrp.hpp:224
std::vector< float > condition
Relevance on the conditioning, summed over steps.
Definition trajectory_lrp.hpp:226